Update events.c

This commit is contained in:
Jason Bou-Samra
2024-08-30 19:20:13 +10:00
committed by GitHub
parent 3bc00fa22d
commit d0f8e52d9a
+39 -25
View File
@@ -14,70 +14,83 @@ bool gradient_d = false; // gradient direction flag
int handle_events () { int handle_events () {
//////////////////////////////// GAME LOOP ////////////////////////// ////////////////////////////// EVENT LOOP //////////////////////////
while (restart == false) { while (restart == false) {
SDL_PollEvent (&event); SDL_PollEvent (&event);
switch (event.type) { switch (event.type) {
case SDL_KEYDOWN: case SDL_KEYDOWN:
// gradient colour cycle
if(event.key.keysym.sym == SDLK_c if(event.key.keysym.sym == SDLK_c
&& (event.key.keysym.mod & KMOD_CTRL) != 0 ) { && (event.key.keysym.mod & KMOD_CTRL) != 0 ) {
colour = colour + 10; colour = colour + 10;
if (colour >= 69) {colour = 0;} if (colour >= 69) {colour = 0;}
backdrop(colour); backdrop(colour);
break; break;
} // gradient colour cycle }
if( event.key.keysym.sym == SDLK_x // invert gradient
&& (event.key.keysym.mod & KMOD_CTRL) != 0 ) { if( event.key.keysym.sym == SDLK_x
&& (event.key.keysym.mod & KMOD_CTRL) != 0 ) {
gradient_d = !gradient_d; gradient_d = !gradient_d;
break; break;
} // invert gradient }
if (event.key.keysym.sym == SDLK_LEFT) { // left arrow
if (event.key.keysym.sym == SDLK_LEFT) {
if (col == 0) {break;} if (col == 0 | grid[0][row + 2][col - 1] != 0) {break;}
grid[0][row + 1][col] = 0; for (int i = -1; i < 3; i++) {
grid[0][row + 0][col] = 0; grid[0][row + i][col] = 0; // zero out old column
grid[0][row + 2][col] = 0; }
grid[0][row - 1][col] = 0;
col--; col--;
disp_column (row, col); disp_column (row, col);
break; break;
} // left arrow }
// right arrow
if (event.key.keysym.sym == SDLK_RIGHT) { if (event.key.keysym.sym == SDLK_RIGHT) {
if (col == 7) {break;} if (col == 7 | grid[0][row + 2][col + 1] != 0) {break;}
grid[0][row + 1][col] = 0; for (int i = -1; i < 3; i++) {
grid[0][row + 0][col] = 0; grid[0][row + i][col] = 0; // zero out old column
grid[0][row + 2][col] = 0; }
grid[0][row - 1][col] = 0;
col++; col++;
disp_column (row, col); disp_column (row, col);
break; break;
} // right arrow }
// next piece (brick/block) preview
if (event.key.keysym.sym == SDLK_n) { if (event.key.keysym.sym == SDLK_n) {
next_p = !next_p; next_p = !next_p;
break; break;
} // next piece (brick/block) preview }
// restart
if( event.key.keysym.sym == SDLK_r if( event.key.keysym.sym == SDLK_r
&& (event.key.keysym.mod & KMOD_CTRL) != 0 ) { && (event.key.keysym.mod & KMOD_CTRL) != 0 ) {
restart = true; restart = true;
break; break;
} // restart }
// rotate tiles
if(event.key.keysym.sym == SDLK_r if(event.key.keysym.sym == SDLK_r
&& (event.key.keysym.mod & KMOD_SHIFT) != 0 ) { && (event.key.keysym.mod & KMOD_SHIFT) != 0 ) {
rotate_col(); rotate_col();
disp_column (row, col); disp_column (row, col);
break; break;
} // rotate tiles }
// drop/fall block/piece/brick
if (event.key.keysym.sym == SDLK_SPACE) { if (event.key.keysym.sym == SDLK_SPACE) {
for (int i = -1; i < 3; i++) {
grid[0][row + i][col] = 0; // zero out old column
}
row = 18 - (18-colcnt[col]);
disp_column (row, col);
break; break;
} // drop/fall block/piece/brick }
// change tile design/pattern
if( event.key.keysym.sym == SDLK_s if( event.key.keysym.sym == SDLK_s
&& (event.key.keysym.mod & KMOD_CTRL) != 0 ) { && (event.key.keysym.mod & KMOD_CTRL) != 0 ) {
tile_shp++; tile_shp++;
@@ -85,12 +98,13 @@ int handle_events () {
tile_shp = 0; tile_shp = 0;
} }
break; break;
} // change tile design/pattern }
// pause
if (event.key.keysym.sym == SDLK_p) { if (event.key.keysym.sym == SDLK_p) {
pause_f = !pause_f; pause_f = !pause_f;
break; break;
} // pause }
} }
Ren_game (); Ren_game ();
} }