keyboard input mapping
This commit is contained in:
parent
fcb13db79e
commit
273b86b77d
20
src/2048.c
20
src/2048.c
|
@ -1,8 +1,24 @@
|
|||
#include "2048.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void game_add_block(Game game) {
|
||||
|
||||
bool is_full = true;
|
||||
for (int i = 0; i < game.field_size_y; i++) {
|
||||
for (int j = 0; j < game.field_size_x; j++) {
|
||||
if (game.field[j][i] == 0) {
|
||||
is_full = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_full) {
|
||||
game.game_over = true;
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t x, y;
|
||||
int value;
|
||||
do {
|
||||
|
@ -15,6 +31,7 @@ void game_add_block(Game game) {
|
|||
|
||||
Game game_init(size_t field_size_x, size_t field_size_y) {
|
||||
Game game;
|
||||
game.game_over = false;
|
||||
game.field_size_x = field_size_x;
|
||||
game.field_size_y = field_size_y;
|
||||
game.score = 0;
|
||||
|
@ -38,7 +55,7 @@ void game_destroy(Game game) {
|
|||
game.field = NULL;
|
||||
}
|
||||
|
||||
uint8_t game_move(Game *game, Direction direction) {
|
||||
void game_move(Game *game, Direction direction) {
|
||||
for (int i = 0; i < game->field_size_y; i++) {
|
||||
uint16_t **field = game->field;
|
||||
// move everything on one side
|
||||
|
@ -154,5 +171,4 @@ uint8_t game_move(Game *game, Direction direction) {
|
|||
}
|
||||
|
||||
game_add_block(*game);
|
||||
return 0;
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifndef HEADER_2048
|
||||
#define HEADER_2048
|
||||
|
@ -17,10 +18,11 @@ typedef struct Game
|
|||
uint32_t score;
|
||||
size_t field_size_x, field_size_y;
|
||||
uint16_t **field;
|
||||
bool game_over;
|
||||
} Game;
|
||||
|
||||
Game game_init(size_t field_size_x, size_t field_size_y);
|
||||
uint8_t game_move(Game *game, Direction direction);
|
||||
void game_move(Game *game, Direction direction);
|
||||
void game_destroy(Game game);
|
||||
|
||||
#endif
|
49
src/gui.c
49
src/gui.c
|
@ -49,7 +49,7 @@ void gui_init() {
|
|||
// load up textures
|
||||
tile_texture = texture_from_png("res/tile.png");
|
||||
tile_textures[0] = tile_texture;
|
||||
tile_textures[1] = texture_from_png("res/2.png");
|
||||
tile_textures[2] = texture_from_png("res/2.png");
|
||||
tile_textures[3] = texture_from_png("res/4.png");
|
||||
tile_textures[4] = texture_from_png("res/8.png");
|
||||
tile_textures[5] = texture_from_png("res/16.png");
|
||||
|
@ -69,6 +69,9 @@ void gui_destroy() {
|
|||
//Destroy window
|
||||
SDL_DestroyWindow( window );
|
||||
SDL_DestroyRenderer(window_renderer);
|
||||
for (int i = 0; i < 13; i++) {
|
||||
SDL_DestroyTexture(tile_textures[i]);
|
||||
}
|
||||
|
||||
//Quit SDL subsystems
|
||||
SDL_Quit();
|
||||
|
@ -83,11 +86,42 @@ void gui_loop(Game *game) {
|
|||
while(!quit) {
|
||||
while( SDL_PollEvent( &e ) != 0 )
|
||||
{
|
||||
switch( e.type ) {
|
||||
/* Keyboard event */
|
||||
/* Pass the event data onto PrintKeyInfo() */
|
||||
case SDL_KEYDOWN:
|
||||
switch (e.key.keysym.sym) {
|
||||
case SDLK_h:
|
||||
case SDLK_LEFT:
|
||||
case SDLK_4:
|
||||
needs_redraw = true;
|
||||
//User requests quit
|
||||
if( e.type == SDL_QUIT )
|
||||
{
|
||||
quit = true;
|
||||
game_move(game, Left);
|
||||
break;
|
||||
case SDLK_l:
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_6:
|
||||
needs_redraw = true;
|
||||
game_move(game, Right);
|
||||
break;
|
||||
case SDLK_j:
|
||||
case SDLK_DOWN:
|
||||
case SDLK_2:
|
||||
needs_redraw = true;
|
||||
game_move(game, Down);
|
||||
break;
|
||||
case SDLK_k:
|
||||
case SDLK_UP:
|
||||
case SDLK_8:
|
||||
needs_redraw = true;
|
||||
game_move(game, Up);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
/* SDL_QUIT event (window close) */
|
||||
case SDL_QUIT:
|
||||
quit = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,7 +135,7 @@ void gui_loop(Game *game) {
|
|||
|
||||
// get texture index from value
|
||||
for (int k = 0; k < 13; k++) {
|
||||
if (!(game->field[i][j] >> k)) {
|
||||
if (!(game->field[j][i] >> k)) {
|
||||
SDL_RenderCopy( window_renderer, tile_textures[k], NULL, &tile_rect);
|
||||
break;
|
||||
}
|
||||
|
@ -109,6 +143,9 @@ void gui_loop(Game *game) {
|
|||
// SDL_RenderCopy( window_renderer, tile_texture, NULL, &tile_rect);
|
||||
}
|
||||
}
|
||||
if (game->game_over) {
|
||||
// render game over screen
|
||||
}
|
||||
SDL_RenderPresent(window_renderer);
|
||||
needs_redraw = false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue