Skip to content

Commit

Permalink
Make it possible to resize window
Browse files Browse the repository at this point in the history
Another feature for parity with SDL1 version

Issue simenheg#3
  • Loading branch information
stianval committed Dec 11, 2024
1 parent d5860b9 commit e9c4a96
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
43 changes: 39 additions & 4 deletions src/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,69 @@ unsigned int WINDOW_W = DEFAULT_WINDOW_W;

unsigned int WINDOW_H = DEFAULT_WINDOW_H;

SDL_Rect gameScreenOnWindowRect = { 0 };

bool screenFreeze = false;

/**
* Initialize the application window and main screen surfaces.
* Initialize the application window.
*
* @return 1 if the initialization was successful, 0 if not.
*/
int initScreen(void)
int initWindow(void)
{
if (renderer) {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
screen_t = NULL; /* Destroying the renderer will destroy the texture */
}
int width = WINDOW_W;
int height = WINDOW_H;
if (fullscreen) {
if (width * DEFAULT_WINDOW_H > height * DEFAULT_WINDOW_W) {
height = width * DEFAULT_WINDOW_H / DEFAULT_WINDOW_W;
} else {
width = height * DEFAULT_WINDOW_W / DEFAULT_WINDOW_H;
}
}
window = SDL_CreateWindow("Zatacka X",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WINDOW_W, WINDOW_H,
width, height,
(fullscreen ?
SDL_WINDOW_FULLSCREEN :
SDL_WINDOW_RESIZABLE));

SDL_GetWindowSize(window, &width, &height);
gameScreenOnWindowRect.w = WINDOW_W;
gameScreenOnWindowRect.h = WINDOW_H;
gameScreenOnWindowRect.y = (height - WINDOW_H) / 2;
gameScreenOnWindowRect.x = (width - WINDOW_W) / 2;

renderer = SDL_CreateRenderer(window, -1, 0);

return window && renderer;
}

/**
* Initialize the main screen surfaces.
*
* @return 1 if the initialization was successful, 0 if not.
*/
int initScreen(void)
{
SDL_FreeSurface(screen);
screen = SDL_CreateRGBSurface(0, WINDOW_W, WINDOW_H, 32,
0x00ff0000,
0x0000ff00,
0x000000ff,
0xff000000);

gameScreenOnWindowRect.w = WINDOW_W;
gameScreenOnWindowRect.h = WINDOW_H;

if (screen_t) {
SDL_DestroyTexture(screen_t);
}
screen_t = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
Expand All @@ -75,5 +110,5 @@ int initScreen(void)

initParticleScreen(WINDOW_W, WINDOW_H);

return (screen == NULL || gameScreen == NULL) ? 0 : 1;
return screen && screen_t && gameScreen && particleScreen;
}
3 changes: 3 additions & 0 deletions src/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ extern SDL_Surface *gameScreen;
extern unsigned int WINDOW_W;
extern unsigned int WINDOW_H;

extern SDL_Rect gameScreenOnWindowRect;

/*
* True to temporarily freeze what's currently being displayed.
*/
extern bool screenFreeze;

int initWindow(void);
int initScreen(void);

#endif
20 changes: 12 additions & 8 deletions src/zatackax.c
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ void displayGame(void)

SDL_UpdateTexture(screen_t, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screen_t, NULL, NULL);
SDL_RenderCopy(renderer, screen_t, NULL, &gameScreenOnWindowRect);
SDL_RenderPresent(renderer);
}

Expand Down Expand Up @@ -1011,7 +1011,7 @@ void displayGameStart(void)

SDL_UpdateTexture(screen_t, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screen_t, NULL, NULL);
SDL_RenderCopy(renderer, screen_t, NULL, &gameScreenOnWindowRect);
SDL_RenderPresent(renderer);
}

Expand Down Expand Up @@ -1315,7 +1315,7 @@ void displayMainMenu(void)

SDL_UpdateTexture(screen_t, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screen_t, NULL, NULL);
SDL_RenderCopy(renderer, screen_t, NULL, &gameScreenOnWindowRect);
SDL_RenderPresent(renderer);
}

Expand Down Expand Up @@ -1462,7 +1462,7 @@ void displayWepMenu(void)

SDL_UpdateTexture(screen_t, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screen_t, NULL, NULL);
SDL_RenderCopy(renderer, screen_t, NULL, &gameScreenOnWindowRect);
SDL_RenderPresent(renderer);
}

Expand All @@ -1478,6 +1478,7 @@ int logicSettingsMenu(void)
case 0: /* Toggle fullscreen */
playSound(SOUND_BEEP, sound);
fullscreen ^= 1;
initWindow();
initScreen();
initHitMap(WINDOW_W, WINDOW_H);
break;
Expand Down Expand Up @@ -1647,7 +1648,7 @@ void displaySettingsMenu(void)

SDL_UpdateTexture(screen_t, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screen_t, NULL, NULL);
SDL_RenderCopy(renderer, screen_t, NULL, &gameScreenOnWindowRect);
SDL_RenderPresent(renderer);
}

Expand Down Expand Up @@ -1694,7 +1695,7 @@ void displayPlayerMenu(void)

SDL_UpdateTexture(screen_t, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screen_t, NULL, NULL);
SDL_RenderCopy(renderer, screen_t, NULL, &gameScreenOnWindowRect);
SDL_RenderPresent(renderer);
}

Expand Down Expand Up @@ -1815,7 +1816,7 @@ void displayPConfMenu(void)

SDL_UpdateTexture(screen_t, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screen_t, NULL, NULL);
SDL_RenderCopy(renderer, screen_t, NULL, &gameScreenOnWindowRect);
SDL_RenderPresent(renderer);
}

Expand Down Expand Up @@ -2093,6 +2094,9 @@ int main(void)
initPlayers1();
restoreSettings();

if (!initWindow())
return 1;

if (!initScreen())
return 1;

Expand Down Expand Up @@ -2218,7 +2222,7 @@ int main(void)
else if (event.type == SDL_QUIT) {
exitGame(0);
}
else if (event.type == SDL_WINDOWEVENT_RESIZED) {
else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
WINDOW_W = event.window.data1;
WINDOW_H = event.window.data2;

Expand Down

0 comments on commit e9c4a96

Please sign in to comment.