diff options
| author | 2019-10-26 00:35:49 -0400 | |
|---|---|---|
| committer | 2019-10-26 00:35:49 -0400 | |
| commit | f3e7787552eef537f6954c73d6d42ca60fb76e84 (patch) | |
| tree | d7a70b8eb111c46d14b29b9ad53dc1ba61e7f59c | |
| parent | 8f5264460a00ba3665dab32ade9dd7e19f532044 (diff) | |
| download | cetris-f3e7787552eef537f6954c73d6d42ca60fb76e84.tar.gz cetris-f3e7787552eef537f6954c73d6d42ca60fb76e84.tar.bz2 cetris-f3e7787552eef537f6954c73d6d42ca60fb76e84.zip | |
start new sdl interface
| -rw-r--r-- | frontends/sdl/meson.build | 10 | ||||
| -rw-r--r-- | frontends/sdl/sdl_ui.c | 121 | ||||
| -rw-r--r-- | meson.build | 4 | ||||
| -rw-r--r-- | meson_options.txt | 1 |
4 files changed, 136 insertions, 0 deletions
diff --git a/frontends/sdl/meson.build b/frontends/sdl/meson.build new file mode 100644 index 0000000..64d70e4 --- /dev/null +++ b/frontends/sdl/meson.build @@ -0,0 +1,10 @@ +src = ['sdl_ui.c'] + +threads = dependency('threads') +sdl = dependency('SDl2') +ttf = dependency('SDL2_ttf') + +executable('cetris', src, + dependencies: [threads, sdl, ttf], + include_directories: cetris_inc, + install: false) diff --git a/frontends/sdl/sdl_ui.c b/frontends/sdl/sdl_ui.c new file mode 100644 index 0000000..3584c9d --- /dev/null +++ b/frontends/sdl/sdl_ui.c @@ -0,0 +1,121 @@ +#define SDL_MAIN_HANDLED +#include <SDL2/SDL.h> +#include <SDL2/SDL_ttf.h> +#include <pthread.h> +#include <cetris.h> +#include <unistd.h> + +#define W 480 +#define H 640 + +#define X_OFFSET 25 +#define Y_OFFSET 20 + +SDL_Renderer* render; +TTF_Font* font; + +cetris_game g; + +uint8_t colors[8][3] = { + {0, 0, 0}, // Black + {127,219,255}, // Aqua + {61,153,112}, // Olive + {177,13,201}, // Purple + {240,18,190}, // Fuchsia + {255,133,27}, // Orange + {0,31,63}, // Navy + {255,220,0} // Yellow +}; + +#ifdef _WIN32 +DWORD WINAPI game_loop(void* data) { + while(1) { + Sleep((1.0/60.0) * 1000.0); + //if (!is_paused) + update_game_tick(&g); + } + return 0; +} +#else +void *game_loop(void) { + while(1) { + sleep((1.0/60.0) * 1000000.0); + //if (!is_paused) + update_game_tick(&g); + } + return 0; +} +#endif + +void setup() { + SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); + SDL_Window* win = SDL_CreateWindow("cetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN); + render = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED); + SDL_RenderSetLogicalSize(render, W, H); + if (!render) exit(fprintf(stderr, "err: could not create SDL renderer\n")); + + TTF_Init(); + font = TTF_OpenFont("LiberationMono-Regular.ttf", 18); +} + +void draw(cetris_game* g) { + SDL_Rect b = {0, 0, 25, 25}; + for (int x = 0; x < CETRIS_BOARD_X; x++) { + for (int y = g->highest_line; y < CETRIS_BOARD_Y; y++) { + if (g->board[x][y] & SLOT_OCCUPIED) { + b.x = X_OFFSET + (x * 25); + b.y = (Y_OFFSET + (y * 25)) - CETRIS_BOARD_VISABLE; + + uint8_t (*color)[3] = &(colors[(g->board[x][y] >> 5)]); + SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 255); + + if (g->line_remove_tick[y]) { + if (g->tick % 2 == 0) { + SDL_RenderDrawRect(render, &b); + } + } else { + SDL_RenderDrawRect(render, &b); + } + } + } + } +} + +int main(void) { + setup(); + + init_game(&g); + +#ifdef _WIN32 + HANDLE thread = CreateThread(NULL, 0, game_loop, NULL, 0, NULL); +#else + pthread_t thread; + pthread_create(&thread, NULL, (void*)game_loop, (void*)0); +#endif + + SDL_Event e; + for(;;) { + while(SDL_PollEvent(&e)) { + switch (e.type) { + case SDL_QUIT: + exit(0); + case SDL_KEYDOWN: + switch (e.key.keysym.sym) { + case SDLK_LEFT: + move_piece(&g, LEFT); break; + case SDLK_RIGHT: + move_piece(&g, RIGHT); break; + case SDLK_DOWN: + move_piece(&g, DOWN); break; + case SDLK_SPACE: + move_piece(&g, HARD_DROP); break; + case SDLK_UP: + move_piece(&g, ROTATE_CW); break; + } + } + } + draw(&g); + SDL_Delay(1000 / 60); + } + +} diff --git a/meson.build b/meson.build index 5bcff0f..91577b5 100644 --- a/meson.build +++ b/meson.build @@ -23,3 +23,7 @@ endif if get_option('gl-frontend') == true
subdir('frontends/gl')
endif
+
+if get_option('sdl-frontend') == true
+ subdir('frontends/sdl')
+endif
diff --git a/meson_options.txt b/meson_options.txt index b57b6ff..70b2bb2 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,2 +1,3 @@ option('curses-frontend', type : 'boolean', value : true)
option('gl-frontend', type : 'boolean', value : false)
+option('sdl-frontend', type : 'boolean', value : true)
|