mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
write code for window handling
This commit is contained in:
50
src/io/window.c
Normal file
50
src/io/window.c
Normal 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
12
src/io/window.h
Normal 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);
|
||||||
Reference in New Issue
Block a user