make window freeing more manual

This commit is contained in:
2025-06-26 11:02:14 +02:00
parent a94e2d147b
commit c1d7f71a02
3 changed files with 9 additions and 9 deletions

View File

@@ -18,7 +18,7 @@ static SDL_Window* win = NULL;
static bool close = false; static bool close = false;
static audiodata music; static audiodata music;
static void window_init(struct gamedata const* gdat) { void window_init(struct gamedata const* gdat) {
assert(!win && !close); assert(!win && !close);
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
fatal(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError()); fatal(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError());
@@ -32,7 +32,7 @@ static void window_init(struct gamedata const* gdat) {
music = audio_wav_load("korobeiniki.wav"); music = audio_wav_load("korobeiniki.wav");
} }
static void window_free(void) { void window_free(void) {
assert(win); assert(win);
render_free(); render_free();
SDL_DestroyWindow(win); SDL_DestroyWindow(win);
@@ -43,9 +43,7 @@ static void window_free(void) {
audio_device_free(); audio_device_free();
} }
void window_open(struct gamedata const* gdat) { void window_open(void) {
window_init(gdat);
while (!close) { while (!close) {
game_update(input_getdat()); game_update(input_getdat());
render_update(); render_update();
@@ -54,8 +52,6 @@ void window_open(struct gamedata const* gdat) {
if (time_poll(time_pull(), music.ms, &timeout)) if (time_poll(time_pull(), music.ms, &timeout))
audio_play(&music); audio_play(&music);
} }
window_free();
} }
void window_close(void) { void window_close(void) {

View File

@@ -6,5 +6,7 @@
#define SCREEN_WIDTH ((COLUMNS + 6) * PX_DENS) // window width #define SCREEN_WIDTH ((COLUMNS + 6) * PX_DENS) // window width
#define SCREEN_HEIGHT ((COLUMNS) * PX_DENS / COLUMNS * ROWS) // window height #define SCREEN_HEIGHT ((COLUMNS) * PX_DENS / COLUMNS * ROWS) // window height
void window_open(struct gamedata const*); void window_init(struct gamedata const*);
void window_open(void);
void window_close(void); void window_close(void);
void window_free(void);

View File

@@ -8,6 +8,7 @@
static void stop(void) { static void stop(void) {
debug("stopping...", ); debug("stopping...", );
window_close(); window_close();
window_free();
SDL_Quit(); SDL_Quit();
} }
@@ -17,7 +18,8 @@ int main(int argc, char** argv) {
// register stop as exit function // register stop as exit function
atexit(stop); atexit(stop);
window_open(game_init()); window_init(game_init());
window_open();
return 0; return 0;
} }