Skip to content

Commit

Permalink
Toggle crisp upscaling in-game
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Jan 8, 2023
1 parent cf21a2e commit fa3b56a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
53 changes: 51 additions & 2 deletions src/SDLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,21 @@ int SDLUi_EventFilter(void*, SDL_Event *event)
if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST && s_isForeground)
{
FreezeGameTickCount();
EnableMusic(false);
//EnableMusic(false);
s_isForeground = false;
}
else if (event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED && !s_isForeground)
{
UnfreezeGameTickCount();
EnableMusic(musicOn);
//EnableMusic(musicOn);
s_isForeground = true;

DoFullRepaint();
}
else if (event->window.event == SDL_WINDOWEVENT_RESIZED)
{
SDLU_CreateRendererTexture();
}
break;
}
}
Expand All @@ -141,6 +145,51 @@ int SDLUi_EventFilter(void*, SDL_Event *event)
}


void SDLU_CreateRendererTexture()
{
if (!g_renderer)
return;

if (!crispUpscaling)
{
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
SDL_RenderSetIntegerScale(g_renderer, SDL_FALSE);
}
else
{
int minWidth = 640;
int minHeight = widescreen ? 360 : 480;

int currentWidth = 0;
int currentHeight = 0;
#if SDL_VERSION_ATLEAST(2,26,0)
SDL_GetWindowSizeInPixels(g_window, &currentWidth, &currentHeight);
#else
SDL_GetWindowSize(g_window, &currentWidth, &currentHeight);
#endif

if (currentWidth < minWidth || currentHeight < minHeight)
{
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
SDL_RenderSetIntegerScale(g_renderer, SDL_FALSE);
}
else
{
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
SDL_RenderSetIntegerScale(g_renderer, SDL_TRUE);
}
}

if (g_windowTexture)
SDL_DestroyTexture(g_windowTexture);

g_windowTexture = SDL_CreateTexture(g_renderer,
SDL_PIXELFORMAT_RGB888,
SDL_TEXTUREACCESS_STREAMING,
640, 480);
}


void SDLU_Init()
{
SDL_SetEventFilter(SDLUi_EventFilter, NULL);
Expand Down
1 change: 1 addition & 0 deletions src/SDLU.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define MASK_DEPTH 8

void SDLU_Init();
void SDLU_CreateRendererTexture();
SDL_Rect* SDLU_MRectToSDLRect( const MRect* in, SDL_Rect* out );
MRect* SDLU_SDLRectToMRect( const SDL_Rect* in, MRect* out );
int SDLU_BlitSurface( SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect );
Expand Down
18 changes: 9 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ static char candyCrisisResources[512];
MBoolean fullscreen = false;
MBoolean widescreen = false;
MBoolean useNewTitle = widescreen;
MBoolean crispUpscaling = false;
int windowedScale = 2;
MBoolean crispUpscaling = true;

int main(int argc, char *argv[])
{
Expand Down Expand Up @@ -563,14 +562,18 @@ void ReserveMonitor( void )
// SDL_ShowCursor( SDL_DISABLE );

SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, crispUpscaling? "0": "best");

int resW = 640;
int resH = widescreen? 360: 480;

SDL_CreateWindowAndRenderer(resW*windowedScale, resH*windowedScale, SDL_WINDOW_RESIZABLE, &g_window, &g_renderer);
SDL_Rect displayBounds = { .x = 0, .y = 0, .w = 640, .h = 480 };
SDL_GetDisplayUsableBounds(0, &displayBounds);
int scaleX = (displayBounds.w * 95/100) / resW; // allow covering at most 95% of the screen
int scaleY = (displayBounds.h * 95/100) / resH;
int scale = scaleX < scaleY ? scaleX : scaleY;
scale = scale < 1 ? 1 : scale;

SDL_RenderSetIntegerScale(g_renderer, crispUpscaling ? SDL_TRUE : SDL_FALSE);
SDL_CreateWindowAndRenderer(resW* scale, resH* scale, SDL_WINDOW_RESIZABLE, &g_window, &g_renderer);

SDL_RenderSetLogicalSize(g_renderer, resW, resH);
SDL_SetWindowTitle(g_window, "Candy Crisis");
Expand All @@ -582,10 +585,7 @@ void ReserveMonitor( void )
g_frontSurface = SDL_CreateRGBSurface(0, 640, 480, 32,
RED_MASK, GREEN_MASK, BLUE_MASK, 0);

g_windowTexture = SDL_CreateTexture(g_renderer,
SDL_PIXELFORMAT_RGB888,
SDL_TEXTUREACCESS_STREAMING,
640, 480);
SDLU_CreateRendererTexture();

SetFullscreen(fullscreen);
}
Expand Down
13 changes: 11 additions & 2 deletions src/pause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,14 @@ static void DrawDialogLogo( MRect *pauseRect, int shade )

enum
{
kWarp = -2,
kNothing = -1,

// main pause screen (kEndGame is reused in continue and register)
kMusic = 0, kResume,
kSound, kEndGame,
kFullscreen, kControls,
kWarp, kSecret,
kScalingMode, kSecret,

// continue screen
kContinue,
Expand Down Expand Up @@ -718,7 +719,8 @@ static void DrawPauseContents( int *item, int shade )
int index;
const char *line[] = { "\x01 Music", "\x03 Resume",
"\x01 Sound", "\x03 End Game",
"\x01 Fullscreen", "\x03 Controls"
"\x01 Fullscreen", "\x03 Controls",
"\x01 Crisp Scaling",
};

const int itemCount = arrsize(line);
Expand All @@ -728,6 +730,7 @@ static void DrawPauseContents( int *item, int shade )
if( !musicOn ) line[kMusic] = "\x02 Music";
if( !soundOn ) line[kSound] = "\x02 Sound";
if( !fullscreen ) line[kFullscreen] = "\x02 Fullscreen";
if (!crispUpscaling) line[kScalingMode] = "\x02 Crisp Scaling";

SDLU_AcquireSurface( drawSurface );

Expand Down Expand Up @@ -950,6 +953,12 @@ static MBoolean PauseSelected( int *item, unsigned char inKey, SDL_Keycode inSDL
PlayMono( kClick );
return false;

case kScalingMode:
crispUpscaling = !crispUpscaling;
SDLU_CreateRendererTexture();
PlayMono(kClick);
return false;

case kEndGame:
case kResume:
case kControls:
Expand Down

0 comments on commit fa3b56a

Please sign in to comment.