diff options
| author | 2019-10-26 16:27:34 -0400 | |
|---|---|---|
| committer | 2019-10-26 16:27:34 -0400 | |
| commit | d118ad41559a5137703597d0be86e3eaaf85a92f (patch) | |
| tree | 56b7aa85e21547b4ffc4362fbe2b09aa137dfdfb /frontends/sdl | |
| parent | 330f4f2c8abdaf694d64c73d0c11c597787a3be7 (diff) | |
| download | cetris-d118ad41559a5137703597d0be86e3eaaf85a92f.tar.gz cetris-d118ad41559a5137703597d0be86e3eaaf85a92f.tar.bz2 cetris-d118ad41559a5137703597d0be86e3eaaf85a92f.zip | |
add das and almost finish sdl ui
Diffstat (limited to 'frontends/sdl')
| -rw-r--r-- | frontends/sdl/config.ini | 9 | ||||
| -rw-r--r-- | frontends/sdl/meson.build | 4 | ||||
| -rw-r--r-- | frontends/sdl/sdl_ui.c | 119 |
3 files changed, 109 insertions, 23 deletions
diff --git a/frontends/sdl/config.ini b/frontends/sdl/config.ini new file mode 100644 index 0000000..5ca9c34 --- /dev/null +++ b/frontends/sdl/config.ini @@ -0,0 +1,9 @@ +[das] +das = 220 +arr = 15 + +[game] +drop_delay = 20 +next_piece_delay = 40 +lock_delay = 30 +wait_on_clear = 0 diff --git a/frontends/sdl/meson.build b/frontends/sdl/meson.build index 64d70e4..d7e7f44 100644 --- a/frontends/sdl/meson.build +++ b/frontends/sdl/meson.build @@ -4,6 +4,10 @@ threads = dependency('threads') sdl = dependency('SDl2') ttf = dependency('SDL2_ttf') +configure_file(input: 'config.ini', + output: 'config.ini', + copy: true, install: false) + executable('cetris', src, dependencies: [threads, sdl, ttf], include_directories: cetris_inc, diff --git a/frontends/sdl/sdl_ui.c b/frontends/sdl/sdl_ui.c index 0efd9cb..c29fd1d 100644 --- a/frontends/sdl/sdl_ui.c +++ b/frontends/sdl/sdl_ui.c @@ -7,19 +7,24 @@ #include <pthread.h> #include <unistd.h> #include <time.h> + #include <cetris.h> +#include <ini.h> #define W 480 #define H 640 -#define X_OFFSET 60 -#define Y_OFFSET 30 +#define BLOCK_SIZE 25 + +#define X_OFFSET (W/2) - (BLOCK_SIZE * 5) +#define Y_OFFSET (H/2) - (BLOCK_SIZE * 10) SDL_Renderer* render; TTF_Font* font; cetris_game g; +/* uint8_t colors[8][3] = { {0, 0, 0}, // Black {127,219,255}, // Aqua @@ -30,6 +35,17 @@ uint8_t colors[8][3] = { {0,31,63}, // Navy {255,220,0} // Yellow }; +*/ + +uint8_t colors[7][3] = { + {174,198,207}, // Aqua + {170,221,119}, // Olive + {177,156,217}, // Purple + {255,209,220}, // Fuchsia + {255,179,71}, // Orange + {119,158,203}, // Navy + {253,253,150} // Yellow +}; #ifdef _WIN32 DWORD WINAPI game_loop(void* data) { @@ -56,6 +72,7 @@ void setup() { 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")); + SDL_SetRenderDrawBlendMode(render, SDL_BLENDMODE_BLEND); //TTF_Init(); //font = TTF_OpenFont("LiberationMono-Regular.ttf", 18); @@ -66,36 +83,47 @@ void draw() { SDL_RenderClear(render); SDL_Rect board = {X_OFFSET, Y_OFFSET, 250, 500}; - SDL_SetRenderDrawColor(render, 240, 240, 240, 255); + SDL_SetRenderDrawColor(render, 235, 235, 235, 255); SDL_RenderFillRect(render, &board); SDL_RenderDrawRect(render, &board); SDL_SetRenderDrawColor(render, 255, 255, 255, 255); for (int x = 0; x < CETRIS_BOARD_X + 1; x++) { - int rx = X_OFFSET + 1 + (x * 25); + int rx = X_OFFSET + 1 + (x * BLOCK_SIZE); SDL_RenderDrawLine(render, rx, Y_OFFSET + 1, rx, Y_OFFSET + 500); } for (int y = 0; y < CETRIS_BOARD_Y - CETRIS_BOARD_VISABLE + 1; y++) { - int ry = Y_OFFSET + (y * 25); + int ry = Y_OFFSET + (y * BLOCK_SIZE); SDL_RenderDrawLine(render, X_OFFSET + 1, ry, X_OFFSET + 250, ry); } - SDL_Rect b = {0, 0, 25, 25}; + SDL_Rect b = {0, 0, BLOCK_SIZE - 1, BLOCK_SIZE - 1}; + SDL_Rect w = {0, 0, BLOCK_SIZE + 1, BLOCK_SIZE + 1}; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if ((g.current.m[y]>>(3 - x))&1) { - b.x = X_OFFSET + ((x + g.current.pos.x) * 25); - b.y = (Y_OFFSET + ((y + g.current.pos.y) * 25)) - (CETRIS_BOARD_VISABLE * 25); - + b.x = X_OFFSET + ((x + g.current.pos.x) * BLOCK_SIZE); + b.y = (Y_OFFSET + ((y + g.current.pos.y) * BLOCK_SIZE)) - (CETRIS_BOARD_VISABLE * BLOCK_SIZE); + uint8_t (*color)[3] = &(colors[g.current.t]); SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 255); - + + SDL_RenderFillRect(render, &b); SDL_RenderDrawRect(render, &b); - b.y = (Y_OFFSET + ((y + g.current.ghost_y) * 25)) - (CETRIS_BOARD_VISABLE * 25); + SDL_SetRenderDrawColor(render, 240, 240, 240, 255); + w.y = b.y - 1; + w.x = b.x - 1; + SDL_RenderDrawRect(render, &w); + + b.y = (Y_OFFSET + ((y + g.current.ghost_y) * BLOCK_SIZE)) - (CETRIS_BOARD_VISABLE * BLOCK_SIZE); + + w.y = b.y - 1; + SDL_RenderDrawRect(render, &w); - SDL_SetRenderDrawColor(render, 0, 0, 0, 255); + SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 180); + SDL_RenderFillRect(render, &b); SDL_RenderDrawRect(render, &b); } @@ -108,22 +136,29 @@ void draw() { 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 * 25); + b.x = X_OFFSET + (x * BLOCK_SIZE); + b.y = (Y_OFFSET + (y * BLOCK_SIZE)) - (CETRIS_BOARD_VISABLE * BLOCK_SIZE); - 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); + if ((int)g.tick % 2 == 0) { + continue; } - } else { - SDL_RenderDrawRect(render, &b); } + + SDL_SetRenderDrawColor(render, 240, 240, 240, 255); + w.y = b.y - 1; + w.x = b.x - 1; + SDL_RenderDrawRect(render, &w); + + uint8_t (*color)[3] = &(colors[(g.board[x][y] >> 5)]); + SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 255); + SDL_RenderFillRect(render, &b); + + SDL_RenderDrawRect(render, &b); } } } + SDL_RenderPresent(render); } @@ -131,6 +166,20 @@ int main(void) { setup(); init_game(&g); + + ini_parser p; + load_ini_file(&p, "config.ini"); + + int das = atoi(get_ini_value(&p, "das", "das")); + g.config.das_das = das * .06f; + + int arr = atoi(get_ini_value(&p, "das", "arr")); + g.config.das_arr = arr * .06f; + + g.config.drop_period = atoi(get_ini_value(&p, "game", "drop_delay")) * .06f; + g.config.next_piece_delay = atoi(get_ini_value(&p, "game", "next_piece_delay")); + g.config.lock_delay = atoi(get_ini_value(&p, "game", "lock_delay")); + g.config.wait_on_clear = atoi(get_ini_value(&p, "game", "wait_on_clear")); #ifdef _WIN32 HANDLE thread = CreateThread(NULL, 0, game_loop, NULL, 0, NULL); @@ -152,16 +201,40 @@ int main(void) { case SDLK_RIGHT: move_piece(&g, RIGHT); break; case SDLK_DOWN: - move_piece(&g, DOWN); break; + move_piece(&g, USER_DOWN); break; case SDLK_SPACE: move_piece(&g, HARD_DROP); break; case SDLK_UP: move_piece(&g, ROTATE_CW); break; + case 'c': + hold_piece(&g); break; + case 'x': + move_piece(&g, ROTATE_CW); break; + case 'z': + move_piece(&g, ROTATE_CCW); break; + } + break; + case SDL_KEYUP: + switch (e.key.keysym.sym) { + case SDLK_LEFT: + unhold_piece(&g, LEFT); break; + case SDLK_RIGHT: + unhold_piece(&g, RIGHT); break; + case SDLK_DOWN: + unhold_piece(&g, USER_DOWN); break; + case SDLK_SPACE: + unhold_piece(&g, HARD_DROP); break; + case SDLK_UP: + unhold_piece(&g, ROTATE_CW); break; + case 'x': + unhold_piece(&g, ROTATE_CW); break; + case 'z': + unhold_piece(&g, ROTATE_CCW); break; } } } draw(); - SDL_Delay(1000 / 60); + SDL_Delay(1000 / 144); } } |