Skip to content

Commit

Permalink
Refactor menu/automap background shading for optimization
Browse files Browse the repository at this point in the history
The trick is simple - a local copy of global variables of I_VideoBuffer (basically, "screen pixel") and shading factors are used inside 'for' loops. This is faster for the overall loop processing.
  • Loading branch information
JNechaevsky committed Jan 1, 2025
1 parent c797197 commit 12c574b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 32 deletions.
18 changes: 11 additions & 7 deletions src/doom/am_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -1225,17 +1225,21 @@ static void AM_clearFB (void)

static void AM_shadeBackground (void)
{
const int height = dp_screen_size > 10 ?
SCREENHEIGHT : (SCREENHEIGHT - (ST_HEIGHT * vid_resolution));
pixel_t *dest = I_VideoBuffer;
const int shade = automap_shading;
const int scr = (dp_screen_size > 10)
? SCREENWIDTH * SCREENHEIGHT
: SCREENWIDTH * (SCREENHEIGHT - ST_HEIGHT * vid_resolution);

for (int y = 0; y < SCREENWIDTH * height ; y++)
{
for (int i = 0; i < scr; i++)
{
#ifndef CRISPY_TRUECOLOR
I_VideoBuffer[y] = colormaps[((automap_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
*dest = colormaps[((automap_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
#else
I_VideoBuffer[y] = I_BlendDark(I_VideoBuffer[y], I_ShadeFactor[automap_shading]);
*dest = I_BlendDark(*dest, I_ShadeFactor[shade]);
#endif
}
++dest;
}
}

// -----------------------------------------------------------------------------
Expand Down
11 changes: 8 additions & 3 deletions src/doom/m_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,13 +928,18 @@ static void M_ShadeBackground (void)
{
if (dp_menu_shading)
{
for (int y = 0; y < SCREENWIDTH * SCREENHEIGHT; y++)
pixel_t *dest = I_VideoBuffer;
const int shade = dp_menu_shading;
const int scr = SCREENWIDTH * SCREENHEIGHT;

for (int i = 0; i < scr; i++)
{
#ifndef CRISPY_TRUECOLOR
I_VideoBuffer[y] = colormaps[((dp_menu_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
*dest = colormaps[((dp_menu_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
#else
I_VideoBuffer[y] = I_BlendDark(I_VideoBuffer[y], I_ShadeFactor[dp_menu_shading]);
*dest = I_BlendDark(*dest, I_ShadeFactor[shade]);
#endif
++dest;
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/heretic/am_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -1202,17 +1202,21 @@ static void AM_drawBackground (void)

static void AM_shadeBackground (void)
{
const int height = dp_screen_size > 10 ?
SCREENHEIGHT : (SCREENHEIGHT - (42 * vid_resolution));
pixel_t *dest = I_VideoBuffer;
const int shade = automap_shading;
const int scr = (dp_screen_size > 10)
? SCREENWIDTH * SCREENHEIGHT
: SCREENWIDTH * (SCREENHEIGHT - SBARHEIGHT);

for (int y = 0; y < SCREENWIDTH * height ; y++)
{
for (int i = 0; i < scr; i++)
{
#ifndef CRISPY_TRUECOLOR
I_VideoBuffer[y] = colormaps[((automap_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
*dest = colormaps[((automap_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
#else
I_VideoBuffer[y] = I_BlendDark(I_VideoBuffer[y], I_ShadeFactor[automap_shading]);
*dest = I_BlendDark(*dest, I_ShadeFactor[shade]);
#endif
}
++dest;
}
}

// -----------------------------------------------------------------------------
Expand Down
13 changes: 9 additions & 4 deletions src/heretic/mn_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,18 +792,23 @@ static void M_ScrollPages (boolean direction)
// [JN] Delay before shading.
static int shade_wait;

// [JN] Shade background while in CRL menu.
// [JN] Shade background while in active menu.
static void M_ShadeBackground (void)
{
if (dp_menu_shading)
{
for (int y = 0; y < SCREENWIDTH * SCREENHEIGHT; y++)
pixel_t *dest = I_VideoBuffer;
const int shade = dp_menu_shading;
const int scr = SCREENWIDTH * SCREENHEIGHT;

for (int i = 0; i < scr; i++)
{
#ifndef CRISPY_TRUECOLOR
I_VideoBuffer[y] = colormaps[((dp_menu_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
*dest = colormaps[((dp_menu_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
#else
I_VideoBuffer[y] = I_BlendDark(I_VideoBuffer[y], I_ShadeFactor[dp_menu_shading]);
*dest = I_BlendDark(*dest, I_ShadeFactor[shade]);
#endif
++dest;
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/hexen/am_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -1074,17 +1074,21 @@ static void AM_drawBackground (void)

static void AM_shadeBackground (void)
{
const int height = dp_screen_size > 10 ?
SCREENHEIGHT : (SCREENHEIGHT - SBARHEIGHT);
pixel_t *dest = I_VideoBuffer;
const int shade = automap_shading;
const int scr = (dp_screen_size > 10)
? SCREENWIDTH * SCREENHEIGHT
: SCREENWIDTH * (SCREENHEIGHT - SBARHEIGHT);

for (int y = 0; y < SCREENWIDTH * height ; y++)
{
for (int i = 0; i < scr; i++)
{
#ifndef CRISPY_TRUECOLOR
I_VideoBuffer[y] = colormaps[((automap_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
*dest = colormaps[((automap_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
#else
I_VideoBuffer[y] = I_BlendDark(I_VideoBuffer[y], I_ShadeFactor[automap_shading]);
*dest = I_BlendDark(*dest, I_ShadeFactor[shade]);
#endif
}
++dest;
}
}

// -----------------------------------------------------------------------------
Expand Down
13 changes: 9 additions & 4 deletions src/hexen/mn_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,18 +744,23 @@ static void M_ScrollPages (boolean direction)
// [JN] Delay before shading.
static int shade_wait;

// [JN] Shade background while in CRL menu.
// [JN] Shade background while in active menu.
static void M_ShadeBackground (void)
{
if (dp_menu_shading)
{
for (int y = 0; y < SCREENWIDTH * SCREENHEIGHT; y++)
pixel_t *dest = I_VideoBuffer;
const int shade = dp_menu_shading;
const int scr = SCREENWIDTH * SCREENHEIGHT;

for (int i = 0; i < scr; i++)
{
#ifndef CRISPY_TRUECOLOR
I_VideoBuffer[y] = colormaps[((dp_menu_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
*dest = colormaps[((dp_menu_shading + 3) * 2) * 256 + I_VideoBuffer[y]];
#else
I_VideoBuffer[y] = I_BlendDark(I_VideoBuffer[y], I_ShadeFactor[dp_menu_shading]);
*dest = I_BlendDark(*dest, I_ShadeFactor[shade]);
#endif
++dest;
}
}
}
Expand Down

0 comments on commit 12c574b

Please sign in to comment.