diff options
| author | 2019-03-19 19:06:32 -0400 | |
|---|---|---|
| committer | 2019-03-19 19:06:32 -0400 | |
| commit | 625b2cd4f1b6d923add27ba749f2faa276eadd1a (patch) | |
| tree | 327ab886188716554b4f7dd34be2b2dfe876b284 | |
| parent | 7649bf08fe950563a6ac4c7816f3bf187779c38a (diff) | |
| download | cetris-625b2cd4f1b6d923add27ba749f2faa276eadd1a.tar.gz cetris-625b2cd4f1b6d923add27ba749f2faa276eadd1a.tar.bz2 cetris-625b2cd4f1b6d923add27ba749f2faa276eadd1a.zip | |
change movement again
| -rw-r--r-- | frontend/ncurses/ncurses_ui.c | 2 | ||||
| -rw-r--r-- | frontend/sdl/sdl_ui.c | 46 | ||||
| -rw-r--r-- | meson.build | 8 | ||||
| -rw-r--r-- | src/cetris.c | 76 | ||||
| -rw-r--r-- | src/cetris.h | 18 | ||||
| -rw-r--r-- | src/input.c | 63 | ||||
| -rw-r--r-- | src/matrix.c | 23 | ||||
| -rw-r--r-- | src/matrix.h | 4 |
8 files changed, 104 insertions, 136 deletions
diff --git a/frontend/ncurses/ncurses_ui.c b/frontend/ncurses/ncurses_ui.c index 9c95227..11e3f67 100644 --- a/frontend/ncurses/ncurses_ui.c +++ b/frontend/ncurses/ncurses_ui.c @@ -68,7 +68,7 @@ void curses_init() { setlocale(LC_CTYPE, "");
initscr();
noecho();
- keypad(stdscr, TRUE);
+ //keypad(stdscr, TRUE);
curs_set(0);
timeout(1000 / CETRIS_HZ);
diff --git a/frontend/sdl/sdl_ui.c b/frontend/sdl/sdl_ui.c index 40f7aab..f2ac2f2 100644 --- a/frontend/sdl/sdl_ui.c +++ b/frontend/sdl/sdl_ui.c @@ -1,4 +1,5 @@ #ifdef _WIN32 +#define _CRT_SECURE_NO_WARNINGS #define SDL_DISABLE_IMMINTRIN_H #define SDL_MAIN_HANDLED #include <SDL.h> @@ -26,9 +27,9 @@ TTF_Font* font; TTF_Font* big_font; typedef struct { - int r; - int g; - int b; + Uint8 r; + Uint8 g; + Uint8 b; } sdl_color; sdl_color colors[8] = { @@ -69,8 +70,8 @@ void draw_text(char* string, int x, int y, int black) { msgsurf = TTF_RenderText_Blended(font, msg, (SDL_Color){255, 255, 255, 255}); } SDL_Texture *msgtex = SDL_CreateTextureFromSurface(render, msgsurf); - SDL_Rect fromrec = {0, 0, msgsurf->w, msgsurf->h}; - SDL_Rect torec = {x, y, msgsurf->w, msgsurf->h}; + SDL_Rect fromrec = (SDL_Rect){0, 0, msgsurf->w, msgsurf->h}; + SDL_Rect torec = (SDL_Rect){x, y, msgsurf->w, msgsurf->h}; SDL_RenderCopy(render, msgtex, &fromrec, &torec); SDL_DestroyTexture(msgtex); SDL_FreeSurface(msgsurf); @@ -184,30 +185,33 @@ int main(void) { case SDL_KEYDOWN: switch (e.key.keysym.sym) { case SDLK_LEFT: - move_left(&g); break; + move_peice(&g, LEFT); break; case SDLK_RIGHT: - move_right(&g); break; + move_peice(&g, RIGHT); break; case SDLK_DOWN: - move_down(&g); break; + move_peice(&g, DOWN); break; case SDLK_SPACE: - move_hard_drop(&g); break; + move_peice(&g, HARD_DROP); break; case SDLK_UP: - rotate_clockwise(&g); break; + move_peice(&g, ROTATE_CW); break; case SDLK_r: if (g.game_over) init_game(&g); } - break; + break; case SDL_KEYUP: - switch (e.key.keysym.sym) { - case SDLK_LEFT: - case SDLK_RIGHT: - case SDLK_DOWN: - case SDLK_SPACE: - case SDLK_UP: - clear_held_key(&g); - break; - } - break; + switch (e.key.keysym.sym) { + case SDLK_LEFT: + stop_holding(&g, LEFT); break; + case SDLK_RIGHT: + stop_holding(&g, RIGHT); break; + case SDLK_DOWN: + stop_holding(&g, DOWN); break; + case SDLK_SPACE: + stop_holding(&g, HARD_DROP); break; + case SDLK_UP: + stop_holding(&g, ROTATE_CW); break; + } + break; } } update_game_tick(&g); diff --git a/meson.build b/meson.build index e1a243c..0ddeafa 100644 --- a/meson.build +++ b/meson.build @@ -28,10 +28,10 @@ if get_option('build_frontends') == true sdl2 = dependency('sdl2')
sdl2_ttf = meson.get_compiler('c').find_library('SDL2_ttf')
endif
- executable('cetris_curses', [src, 'frontend/ncurses/ncurses_ui.c'],
- dependencies: curses,
- include_directories: inc,
- install : false)
+ #executable('cetris_curses', [src, 'frontend/ncurses/ncurses_ui.c'],
+ # dependencies: curses,
+ # include_directories: inc,
+ # install : false)
executable('cetris_sdl', [src, 'frontend/sdl/sdl_ui.c'],
include_directories: [inc, sdlinc],
dependencies: [sdl2, sdl2_ttf],
diff --git a/src/cetris.c b/src/cetris.c index 419fdaf..e6bbc8d 100644 --- a/src/cetris.c +++ b/src/cetris.c @@ -42,11 +42,11 @@ void init_game(struct cetris_game* g) { g->current_index = 0;
- g->held_move = 0;
- g->prev_move = 0;
- g->next_move_tick = 0;
- g->can_rotate = 0;
- g->can_hard_drop = 0;
+ memset(g->held_moves, 0, sizeof(uint8_t) * 7);
+ g->das_repeat = 0;
+ g->prev_das_move = 0;
+ g->das_move_tick = 0;
+ g->down_move_tick = 0;
g->lines = 0;
g->level = 1;
@@ -135,9 +135,7 @@ void update_game_tick(struct cetris_game* g) { did_move = 1;
}
- if (did_move) {
- wipe_board(g);
- }
+ if (did_move) wipe_board(g);
g->tick++;
}
@@ -233,44 +231,32 @@ void wipe_board(struct cetris_game* g) { /* MOVEMENT FUNCTIONS */
-void move_down(struct cetris_game* g) {
- if (g->held_move != DOWN) {
- move_current(g, basic_movements[DOWN]);
- }
- g->held_move = DOWN;
-}
-
-void move_right(struct cetris_game* g) {
- if (g->held_move != RIGHT) {
- move_current(g, basic_movements[RIGHT]);
- }
- g->held_move = RIGHT;
-}
-
-void move_left(struct cetris_game* g) {
- if (g->held_move != LEFT) {
- move_current(g, basic_movements[LEFT]);
- }
- g->held_move = LEFT;
-}
-
-void move_hard_drop(struct cetris_game* g) {
- if (g->held_move != HARD_DROP) {
- hard_drop(g);
- }
- g->held_move = HARD_DROP;
-}
-
-void rotate_clockwise(struct cetris_game* g) {
- if (g->held_move != ROTATE_CW) {
- rotate_matrix(g, 1);
+void move_peice(struct cetris_game* g, input_t move) {
+ if (!g->held_moves[move]) {
+ switch (move) {
+ case LEFT:
+ case RIGHT:
+ case DOWN:
+ move_current(g, basic_movements[move]);
+ break;
+ case HARD_DROP:
+ hard_drop(g);
+ break;
+ case ROTATE_CW:
+ rotate_matrix(g, 1);
+ break;
+ case ROTATE_CCW:
+ rotate_matrix(g, 0);
+ break;
+ }
}
- g->held_move = ROTATE_CW;
+ g->held_moves[move] = 1;
}
-void rotate_counterclockwise(struct cetris_game* g) {
- if (g->held_move != ROTATE_CCW) {
- rotate_matrix(g, 0);
- }
- g->held_move = ROTATE_CCW;
+void stop_holding(struct cetris_game* g, input_t move) {
+ g->held_moves[move] = 0;
+ if (move == RIGHT || move == LEFT) {
+ g->das_move_tick = 0;
+ g->das_repeat = 0;
+ } else if (move == DOWN) g->down_move_tick = 0;
}
diff --git a/src/cetris.h b/src/cetris.h index 2690163..8264842 100644 --- a/src/cetris.h +++ b/src/cetris.h @@ -65,11 +65,11 @@ struct cetris_game { uint8_t current_index;
/* input_manager */
- input_t held_move;
- input_t prev_move;
- int next_move_tick;
- uint8_t can_rotate;
- uint8_t can_hard_drop;
+ uint8_t held_moves[7];
+ int das_repeat;
+ input_t prev_das_move;
+ int das_move_tick;
+ int down_move_tick;
/* internal game tick */
int tick;
@@ -95,9 +95,5 @@ void wipe_board(struct cetris_game* g); void init_game(struct cetris_game* g);
void update_game_tick(struct cetris_game* g);
-void move_down(struct cetris_game* g);
-void move_left(struct cetris_game* g);
-void move_right(struct cetris_game* g);
-void move_hard_drop(struct cetris_game* g);
-void rotate_clockwise(struct cetris_game* g);
-void rotate_counterclockwise(struct cetris_game* g);
+void move_peice(struct cetris_game* g, input_t move);
+void stop_holding(struct cetris_game* g, input_t move);
diff --git a/src/input.c b/src/input.c index e3b61b8..9fd9142 100644 --- a/src/input.c +++ b/src/input.c @@ -7,44 +7,45 @@ #include "cetris.h" uint8_t handle_inputs(struct cetris_game* g) { - uint8_t did_move = 0; - if (g->held_move && !g->next_move_tick) { - if (g->held_move == RIGHT || g->held_move == LEFT) { - if (g->prev_move == g->held_move) { - g->next_move_tick = g->tick + CETRIS_DAS_PERIOD; - } else { - g->next_move_tick = g->tick + CETRIS_DAS_DELAY; - } - } else { - g->next_move_tick = g->tick + CETRIS_DROP_PERIOD; + if ((g->held_moves[RIGHT] || g->held_moves[LEFT]) && !g->das_move_tick) { + if (g->das_repeat == 0) { + g->das_move_tick = g->tick + CETRIS_DAS_DELAY; + } else if (g->das_repeat > 0) { + g->das_move_tick = g->tick + CETRIS_DAS_PERIOD; } } - if (g->next_move_tick && g->tick >= g->next_move_tick) { - switch (g->held_move) { - case DOWN: - g->score++; - move_current(g, basic_movements[DOWN]); - break; - case LEFT: - move_current(g, basic_movements[LEFT]); - break; - case RIGHT: - move_current(g, basic_movements[RIGHT]); - break; + if (g->held_moves[DOWN] && !g->down_move_tick) { + g->down_move_tick = g->tick + CETRIS_DROP_PERIOD; + } + + uint8_t did_move = 0; + if (g->das_move_tick && g->tick >= g->das_move_tick) { + if (g->held_moves[RIGHT]) { + if (g->prev_das_move == RIGHT || g->das_repeat == 0) g->das_repeat++; + else g->das_repeat = 0; + move_current(g, basic_movements[RIGHT]); + g->prev_das_move = RIGHT; + } + + if (g->held_moves[LEFT]) { + if (g->prev_das_move == LEFT || g->das_repeat == 0) g->das_repeat++; + else g->das_repeat = 0; + move_current(g, basic_movements[LEFT]); + g->prev_das_move = LEFT; } + + g->das_move_tick = 0; did_move = 1; } - if (did_move) { - g->next_move_tick = 0; - g->prev_move = g->held_move; + if (g->down_move_tick && g->tick >= g->down_move_tick) { + if (g->held_moves[DOWN]) { + move_current(g, basic_movements[DOWN]); + } + + g->down_move_tick = 0; + did_move = 1; } return did_move; } - -void clear_held_key(struct cetris_game* g) { - g->prev_move = 0; - g->held_move = 0; - g->next_move_tick = 0; -} diff --git a/src/matrix.c b/src/matrix.c index 1fabdce..cac9c47 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -142,7 +142,8 @@ void hard_drop(struct cetris_game* g) { void rotate_matrix(struct cetris_game* g, int clockwise) { if (g->current.t == O) return; - rstate next; int8_t wall_kick; + rstate next; + int8_t wall_kick; switch (g->current.r) { case INIT: if (clockwise) { @@ -197,26 +198,6 @@ void rotate_matrix(struct cetris_game* g, int clockwise) { } } - /* - uint8_t wall_kick; - switch (g->current.r) { - case INIT: - wall_kick = (next == RRIGHT) ? 0 : 7; - break; - case ONCE_RIGHT: - wall_kick = (next == INIT) ? 1 : 2; - break; - case ONCE_LEFT: - wall_kick = (next == INIT) ? 6 : 5; - break; - case TWICE: - wall_kick = (next == RRIGHT) ? 3 : 4; - break; - default: // check for invalid rotations - assert(0); - } - */ - vec2 kick; uint8_t set_current = 0; uint8_t did_kick = 0; diff --git a/src/matrix.h b/src/matrix.h index f75e1e0..3ab0252 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -5,8 +5,8 @@ #include "types.h" #include "cetris.h" -const piece_matrix default_matrices[7]; -const vec2 basic_movements[4]; +extern const piece_matrix default_matrices[7]; +extern const vec2 basic_movements[4]; void move_current(struct cetris_game* g, vec2 offset); void overlay_current_matrix(struct cetris_game* g); |