write code for window handling

This commit is contained in:
2025-06-24 19:52:00 +02:00
parent fc2924d128
commit 046f7affaf
2 changed files with 62 additions and 0 deletions

50
src/io/window.c Normal file
View File

@@ -0,0 +1,50 @@
#include "window.h"
#include <SDL.h>
#include <SDL_audio.h>
#include <SDL_error.h>
#include <SDL_video.h>
#include <assert.h>
#include "../error.h"
#include "../game/game.h"
#include "audio.h"
#include "input.h"
#include "render.h"
static SDL_Window* win = NULL;
static bool close = false;
void window_init(void) {
assert(!win && !close);
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
fatal(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError());
SDL_Window* const window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
fatal(ERROR_SDL_RENDERING_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError());
render_init(win);
audio_device_init(32000, AUDIO_S16, 1, 4096);
}
void window_close(void) {
close = true;
}
void window_loop(void) {
assert(win);
while (!close) {
game_update(input_getdat());
render_update();
}
}
void window_free(void) {
assert(win);
render_free();
SDL_DestroyWindow(win);
win = NULL;
close = false;
}

12
src/io/window.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include "../game/game.h"
#define PX_DENS 25 // pixel density; pixels per block
#define SCREEN_WIDTH ((COLUMNS + 6) * PX_DENS) // window width
#define SCREEN_HEIGHT ((COLUMNS) * PX_DENS / COLUMNS * ROWS) // window height
void window_init(void);
void window_close(void);
void window_loop(void);
void window_free(void);