Skip to content

Commit

Permalink
Clean fade without using window gamma ramp
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Jan 8, 2023
1 parent 237566f commit 2c8cf7b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
28 changes: 16 additions & 12 deletions src/SDLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ static MPoint s_mousePosition;
// for event loop
static MBoolean s_isForeground = true;

// for fade out / fade in
static float s_fadeGamma = 1;

// for checktyping
struct BufferedKey
{
Expand Down Expand Up @@ -292,18 +295,7 @@ void SDLU_BlitFrontSurface( SDL_Surface* source, SDL_Rect* sourceSDLRect, SDL_Re

void SDLU_SetBrightness( float b )
{
if (!fullscreen)
return;

Uint16 table[256];
int index;

for( index=0; index<256; index++ )
{
table[index] = (int)(index * b * 257.0f); // 255 * 257 = 65535
}

SDL_SetWindowGammaRamp(g_window, table, table, table);
s_fadeGamma = b;
}

void SDLU_Yield()
Expand Down Expand Up @@ -428,10 +420,20 @@ void SDLU_ReleaseSurface( SDL_Surface* surface )

void SDLU_Present()
{
SDL_SetRenderDrawColor(g_renderer, 0, 0, 0, 255);

SDL_UpdateTexture(g_windowTexture, NULL, g_frontSurface->pixels, g_frontSurface->pitch);
SDL_RenderClear(g_renderer);

SDL_RenderCopy(g_renderer, g_windowTexture, widescreen ? &g_widescreenCrop : NULL, NULL);

if (s_fadeGamma < 1.0)
{
SDL_SetRenderDrawBlendMode(g_renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(g_renderer, 0, 0, 0, (Uint8)((1.0f - s_fadeGamma) * 255.0f));
SDL_RenderFillRect(g_renderer, NULL);
}

SDL_RenderPresent(g_renderer);

#if 0
Expand All @@ -445,7 +447,9 @@ void SDLU_Present()
if (elapsed > k_fpsSampleInterval)
{
float fps = s_fpsAccumulator / (elapsed / 1000.0f);
#if _DEBUG
printf("FPS: %.1f\n", fps);
#endif
s_fpsAccumulator = 0;
s_fpsSampleStart = now;
}
Expand Down
40 changes: 20 additions & 20 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,19 +663,18 @@ void QuickFadeIn( MRGBColor *color )
{
(void) color; // is unused

if (fullscreen) {
for( float percent=0.0f; percent<1.0f; percent += 0.04f )
for( float percent=0.0f; percent<1.0f; percent += 0.04f )
{
int c = MTickCount( );
SDLU_SetBrightness( percent );
while( c == MTickCount( ) )
{
MTicks c = MTickCount( );
SDLU_SetBrightness( percent );
while( c == MTickCount( ) )
{
SDLU_Yield();
}
SDLU_Present();
SDLU_Yield();
}
}

SDLU_SetBrightness( 1.0 );
}
SDLU_SetBrightness( 1.0 );

SDL_Delay(200);
}
Expand All @@ -684,19 +683,20 @@ void QuickFadeOut( MRGBColor *color )
{
(void) color; // is unused

if (fullscreen) {
for( float percent=1.0f; percent>0.0f; percent -= 0.04f )
for( float percent=1.0f; percent>0.0f; percent -= 0.04f )
{
int c = MTickCount( );
SDLU_SetBrightness( percent );
while( c == MTickCount( ) )
{
MTicks c = MTickCount( );
SDLU_SetBrightness( percent );
while( c == MTickCount( ) )
{
SDLU_Yield();
}
SDLU_Present();
SDLU_Yield();
}
}

SDLU_SetBrightness( 0.0 );
}
SDLU_SetBrightness( 0.0 );
SDLU_Present();
SDLU_Present();

SDL_Delay(200);
}
Expand Down

0 comments on commit 2c8cf7b

Please sign in to comment.