Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up doom_get_framebuffer code #18

Closed
wants to merge 0 commits into from
Closed

clean up doom_get_framebuffer code #18

wants to merge 0 commits into from

Conversation

ColleagueRiley
Copy link
Contributor

You repeat code if there are 3 or 4 channels, this can be easily reduced. Also, you don't need the redundant else because it would've returned by then anyway.

PureDOOM.h Outdated
int kpal = screen_buffer[i] * 3;
final_screen_buffer[k + 0] = screen_palette[kpal + 0];
final_screen_buffer[k + 1] = screen_palette[kpal + 1];
final_screen_buffer[k + 2] = screen_palette[kpal + 2];

if (channels == 4)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I repeat code because this line creates branching in a loop that should be as tight as possible.
Altho branch prediction will make this fast anyway, I rather keep every channel code separated. It's not like it's a lot of code anyway, to guarantee tightess loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'm not too worried about the speed for that.

  1. the compiler might optimize it out anyway.

  2. your library works with a smaller bitmap, so that part of processing can be slower.

I think it should stay small because it allows the user to change the size themselves and/or modify the smaller bitmap if they so choose. This comment is in response to one of your todo items.

Looking back at this code again, if we care about speed maybe it would be faster to use something like memcpy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be fixed by using a macro I'd think.

Eg. DOOM_SUPPORT_ALPHA or something

Then the logic is done in preprocessing only once.

README.md Outdated
@@ -17,6 +17,7 @@ See end of file for license information.
- Menu option to disable mouse move Forward/Backward
- Crosshair option
- Always-Run option
- Print message when discovered a secret
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

src/DOOM/DOOM.c Outdated
{
return 0;
}
return 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Daivuk
Copy link
Owner

Daivuk commented Apr 6, 2024

This needs to be rebased and squashed

@ColleagueRiley
Copy link
Contributor Author

Sure, I would like to work out the issues first.

src/DOOM/DOOM.c Outdated
Comment on lines 644 to 656
else if (channels == 4)
{
for (i = 0, len = SCREENWIDTH * SCREENHEIGHT; i < len; ++i)
{
int k = i * 4;
int kpal = screen_buffer[i] * 3;
final_screen_buffer[k + 0] = screen_palette[kpal + 0];
final_screen_buffer[k + 1] = screen_palette[kpal + 1];
final_screen_buffer[k + 2] = screen_palette[kpal + 2];
final_screen_buffer[k + 3] = 255;
}
return final_screen_buffer;
}
Copy link
Owner

@Daivuk Daivuk Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put back the duplicated code. Good thinking though. But it's ok to duplicate code that have difference purpose, especially small block like this. Some CPUs will have the branch prediction, but some others like on micro controllers, might not. Reminder this is to be run on anything. I prefer keeping it separate and branch-less if it doesnt have to.

The 2 other changes are good. (The return 0 and the readme fix)
Rebase & Squash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the whole point of this pull request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're probably right when it comes to the CPU. But also I'd think this is something that would be caught by the compiler first.

@ColleagueRiley
Copy link
Contributor Author

My bad, it should be doom_memset not memset. Honestly, I don't know why you're using functions for this instead of macros It would be so much easier for everyone.

eg.

#ifndef DOOM_MALLOC
#define DOOM_MALLOC free
#define DOOM_FREE free
#endif

src/DOOM/DOOM.c Outdated
@@ -556,6 +556,7 @@ void doom_init(int argc, char** argv, int flags)

screen_buffer = doom_malloc(SCREENWIDTH * SCREENHEIGHT);
final_screen_buffer = doom_malloc(SCREENWIDTH * SCREENHEIGHT * 4);
doom_memset(final_screen_buffer, 0x000000FF, SCREENWIDTH * SCREENHEIGHT * 4);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, an oversight on my part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants