#include "window.h" #include #include #include #include #include #include #include "../error.h" #include "../game/game.h" #include "../game/time.h" #include "SDL_ttf.h" #include "audio.h" #include "input.h" #include "render.h" static SDL_Window* win = NULL; static bool close = false; static audiodata music; void window_init(struct gamedata const* gdat) { 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()); if (TTF_Init() < 0) fatal(ERROR_SDL_INIT, "TTF could not initialize! TTF Error: %s", TTF_GetError()); win = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (win == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError()); render_init(win, gdat); audio_device_init(32000, AUDIO_S16, 1, 4096); music = audio_wav_load("korobeiniki.wav"); } void window_free(void) { assert(win); render_free(); SDL_DestroyWindow(win); win = NULL; close = false; audio_wav_unload(&music); audio_device_free(); } void window_open(void) { while (!close) { game_update(input_getdat()); render_update(); static time_t timeout = 0; if (time_poll(time_pull(), music.ms, &timeout)) audio_play(&music); } } void window_close(void) { close = true; }