Formatting and bug
This commit is contained in:
parent
81af4498e7
commit
bdd4e17cad
77
src/2048.c
77
src/2048.c
|
@ -1,7 +1,7 @@
|
|||
#include "2048.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void game_add_block(Game *game) {
|
||||
|
||||
|
@ -22,7 +22,7 @@ void game_add_block(Game *game) {
|
|||
size_t x, y;
|
||||
int value;
|
||||
do {
|
||||
x = rand() % game->field_size_x;
|
||||
x = rand() % game->field_size_x;
|
||||
y = rand() % game->field_size_y;
|
||||
value = (rand() % 2) + 1;
|
||||
} while (game->field[x][y] != 0);
|
||||
|
@ -42,7 +42,7 @@ Game game_init(size_t field_size_x, size_t field_size_y) {
|
|||
}
|
||||
|
||||
time_t t;
|
||||
srand((unsigned) time(&t));
|
||||
srand((unsigned)time(&t));
|
||||
game_add_block(&game);
|
||||
|
||||
return game;
|
||||
|
@ -72,7 +72,7 @@ void game_move(Game *game, Direction direction) {
|
|||
a = j;
|
||||
b = i;
|
||||
}
|
||||
|
||||
|
||||
// make it so it can work with different directions
|
||||
int directionality = b; // TODO: rename ugly name
|
||||
if (direction == Down) {
|
||||
|
@ -80,21 +80,23 @@ void game_move(Game *game, Direction direction) {
|
|||
}
|
||||
int movement = 1;
|
||||
if (direction == Left || direction == Up) {
|
||||
if(direction == Left) {
|
||||
directionality = game->field_size_x - b - 1;
|
||||
} else {
|
||||
directionality = game->field_size_y - a - 1;
|
||||
if (direction == Left) {
|
||||
directionality = game->field_size_x - b - 1;
|
||||
}
|
||||
else {
|
||||
directionality = game->field_size_y - a - 1;
|
||||
}
|
||||
movement = -1;
|
||||
}
|
||||
|
||||
if(direction == Left || direction == Right) {
|
||||
if(field[a][directionality + movement] == 0) {
|
||||
field[a][directionality+movement] = field[a][directionality];
|
||||
if (direction == Left || direction == Right) {
|
||||
if (field[a][directionality + movement] == 0) {
|
||||
field[a][directionality + movement] = field[a][directionality];
|
||||
field[a][directionality] = 0;
|
||||
}
|
||||
} else {
|
||||
if(field[directionality + movement][b] == 0) {
|
||||
}
|
||||
else {
|
||||
if (field[directionality + movement][b] == 0) {
|
||||
field[directionality + movement][b] = field[directionality][b];
|
||||
field[directionality][b] = 0;
|
||||
}
|
||||
|
@ -112,33 +114,34 @@ void game_move(Game *game, Direction direction) {
|
|||
int directionality = b; // TODO: rename ugly name
|
||||
if (direction == Down) {
|
||||
directionality = a;
|
||||
|
||||
}
|
||||
int movement = 1;
|
||||
if (direction == Left || direction == Up) {
|
||||
if(direction == Left) {
|
||||
directionality = game->field_size_x - b - 1;
|
||||
} else {
|
||||
directionality = game->field_size_y - a - 1;
|
||||
if (direction == Left) {
|
||||
directionality = game->field_size_x - b - 1;
|
||||
}
|
||||
else {
|
||||
directionality = game->field_size_y - a - 1;
|
||||
}
|
||||
movement = -1;
|
||||
}
|
||||
// TODO: fix 444 <- 48, should be 84
|
||||
if (direction == Left || direction == Right) {
|
||||
if(field[a][directionality + movement] == field[a][directionality]) {
|
||||
field[a][directionality+movement] = field[a][directionality]*2;
|
||||
game->score += field[a][directionality+movement];
|
||||
if(field[a][directionality+movement] == 2048){
|
||||
if (field[a][directionality + movement] == field[a][directionality]) {
|
||||
field[a][directionality + movement] = field[a][directionality] * 2;
|
||||
game->score += field[a][directionality + movement];
|
||||
if (field[a][directionality + movement] == 2048) {
|
||||
game->won = true;
|
||||
}
|
||||
field[a][directionality] = 0;
|
||||
j++;
|
||||
}
|
||||
} else {
|
||||
if(field[directionality + movement][b] == field[directionality][b]) {
|
||||
field[directionality + movement][b] = field[directionality][b]*2;
|
||||
game->score += field[directionality + movement][b];
|
||||
if (field[directionality + movement][b]) {
|
||||
}
|
||||
else {
|
||||
if (field[directionality + movement][b] == field[directionality][b]) {
|
||||
field[directionality + movement][b] = field[directionality][b] * 2;
|
||||
game->score += field[directionality + movement][b];
|
||||
if (field[directionality + movement][b] == 2048) {
|
||||
game->won = true;
|
||||
}
|
||||
field[directionality][b] = 0;
|
||||
|
@ -161,20 +164,22 @@ void game_move(Game *game, Direction direction) {
|
|||
}
|
||||
int movement = 1;
|
||||
if (direction == Left || direction == Up) {
|
||||
if(direction == Left) {
|
||||
directionality = game->field_size_x - b - 1;
|
||||
} else {
|
||||
directionality = game->field_size_y - a - 1;
|
||||
if (direction == Left) {
|
||||
directionality = game->field_size_x - b - 1;
|
||||
}
|
||||
else {
|
||||
directionality = game->field_size_y - a - 1;
|
||||
}
|
||||
movement = -1;
|
||||
}
|
||||
if(direction == Left || direction == Right) {
|
||||
if(field[a][directionality + movement] == 0) {
|
||||
field[a][directionality+movement] = field[a][directionality];
|
||||
if (direction == Left || direction == Right) {
|
||||
if (field[a][directionality + movement] == 0) {
|
||||
field[a][directionality + movement] = field[a][directionality];
|
||||
field[a][directionality] = 0;
|
||||
}
|
||||
} else {
|
||||
if(field[directionality + movement][b] == 0) {
|
||||
}
|
||||
else {
|
||||
if (field[directionality + movement][b] == 0) {
|
||||
field[directionality + movement][b] = field[directionality][b];
|
||||
field[directionality][b] = 0;
|
||||
}
|
||||
|
|
117
src/gui.c
117
src/gui.c
|
@ -1,53 +1,50 @@
|
|||
#include "gui.h"
|
||||
#include <stdbool.h>
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
const SCREEN_WIDTH = 800;
|
||||
const SCREEN_HEIGHT = 920;
|
||||
|
||||
// these are meant to be immutable like in Rust
|
||||
// unfortunately, C does not allow that
|
||||
SDL_Window* window = NULL;
|
||||
SDL_Window *window = NULL;
|
||||
// SDL_Surface* screenSurface = NULL;
|
||||
SDL_Renderer *window_renderer = NULL;
|
||||
SDL_Texture* tile_texture = NULL;
|
||||
SDL_Texture* tile_textures[13];
|
||||
SDL_Texture *tile_texture = NULL;
|
||||
SDL_Texture *tile_textures[13];
|
||||
SDL_Rect tile_size;
|
||||
SDL_Texture* game_over_texture = NULL;
|
||||
TTF_Font* Sans = NULL;
|
||||
SDL_Color Black = {0, 0, 0};
|
||||
SDL_Texture *game_over_texture = NULL;
|
||||
TTF_Font *Sans = NULL;
|
||||
SDL_Color Black = { 0, 0, 0 };
|
||||
|
||||
SDL_Texture *texture_from_png(char *n) {
|
||||
SDL_Surface *s = IMG_Load(n);
|
||||
SDL_Texture *t = SDL_CreateTextureFromSurface( window_renderer, s);
|
||||
SDL_Texture *t = SDL_CreateTextureFromSurface(window_renderer, s);
|
||||
SDL_FreeSurface(s);
|
||||
return t;
|
||||
}
|
||||
|
||||
void gui_init() {
|
||||
|
||||
//Initialize SDL
|
||||
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
|
||||
{
|
||||
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
|
||||
// Initialize SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow( "2048", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
|
||||
if( window == NULL )
|
||||
{
|
||||
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
|
||||
window = SDL_CreateWindow("2048", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
if (window == NULL) {
|
||||
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
TTF_Init();
|
||||
// screenSurface = SDL_GetWindowSurface(window);
|
||||
// SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 255, 255, 255));
|
||||
|
||||
|
||||
// create renderer and render white window
|
||||
window_renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
SDL_SetRenderDrawColor(window_renderer, 0xFF, 0xFF, 0xFF, 0xFF );
|
||||
SDL_SetRenderDrawColor(window_renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
SDL_RenderClear(window_renderer);
|
||||
SDL_RenderPresent(window_renderer);
|
||||
// SDL_UpdateWindowSurface(window);
|
||||
|
@ -77,8 +74,8 @@ void gui_init() {
|
|||
}
|
||||
|
||||
void gui_destroy() {
|
||||
//Destroy window
|
||||
SDL_DestroyWindow( window );
|
||||
// Destroy window
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_DestroyRenderer(window_renderer);
|
||||
for (int i = 0; i < 13; i++) {
|
||||
SDL_DestroyTexture(tile_textures[i]);
|
||||
|
@ -86,9 +83,8 @@ void gui_destroy() {
|
|||
SDL_DestroyTexture(game_over_texture);
|
||||
|
||||
TTF_Quit();
|
||||
//Quit SDL subsystems
|
||||
// Quit SDL subsystems
|
||||
SDL_Quit();
|
||||
|
||||
}
|
||||
|
||||
void gui_loop(Game *game) {
|
||||
|
@ -96,43 +92,42 @@ void gui_loop(Game *game) {
|
|||
bool needs_redraw = true; // there's no need to redraw window when nothing's changed. will be set to false later. first run has to be always done
|
||||
SDL_Event e;
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
game_move(game, Left);
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
quit = 1;
|
||||
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;
|
||||
case SDL_QUIT:
|
||||
quit = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,11 +138,11 @@ void gui_loop(Game *game) {
|
|||
tile_rect.x = i * 175 + 50;
|
||||
for (int j = 0; j < game->field_size_y; j++) {
|
||||
tile_rect.y = j * 175 + 150;
|
||||
|
||||
|
||||
// get texture index from value
|
||||
for (int k = 0; k < 13; k++) {
|
||||
if (!(game->field[j][i] >> k)) {
|
||||
SDL_RenderCopy( window_renderer, tile_textures[k], NULL, &tile_rect);
|
||||
SDL_RenderCopy(window_renderer, tile_textures[k], NULL, &tile_rect);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -159,8 +154,8 @@ void gui_loop(Game *game) {
|
|||
char value[16];
|
||||
SDL_itoa(game->score, value, 16);
|
||||
strncat(label_text, value, strlen(value));
|
||||
SDL_Surface* score_label = TTF_RenderText_Blended(Sans, label_text, Black);
|
||||
SDL_Texture* score_label_texture = SDL_CreateTextureFromSurface(window_renderer, score_label);
|
||||
SDL_Surface *score_label = TTF_RenderText_Blended(Sans, label_text, Black);
|
||||
SDL_Texture *score_label_texture = SDL_CreateTextureFromSurface(window_renderer, score_label);
|
||||
SDL_Rect score_label_rect;
|
||||
SDL_QueryTexture(score_label_texture, NULL, NULL, &score_label_rect.w, &score_label_rect.h);
|
||||
score_label_rect.x = (SCREEN_WIDTH - score_label_rect.w) / 2;
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
#include "tui.h"
|
||||
#include "gui.h"
|
||||
#include "2048.h"
|
||||
#include "gui.h"
|
||||
#include "tui.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
Game game;
|
||||
game = game_init(4, 4);
|
||||
/*tui_init();
|
||||
|
||||
tui_loop(&game);
|
||||
|
||||
|
||||
tui_destroy();
|
||||
*/
|
||||
|
||||
|
|
54
src/tui.c
54
src/tui.c
|
@ -1,23 +1,23 @@
|
|||
#include "tui.h"
|
||||
#include <stdio.h>
|
||||
#include <curses.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const uint32_t space_between_cells_x = 6;
|
||||
const uint32_t space_between_cells_y = 2;
|
||||
|
||||
// entering and leaving screen using ANSI escape sequence
|
||||
void tui_init() {
|
||||
initscr();
|
||||
cbreak();
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
nonl();
|
||||
intrflush(stdscr, FALSE);
|
||||
nonl();
|
||||
intrflush(stdscr, FALSE);
|
||||
keypad(stdscr, TRUE);
|
||||
}
|
||||
void tui_destroy() {}
|
||||
void tui_destroy() { }
|
||||
|
||||
void tui_loop(Game *game) {
|
||||
while(true){
|
||||
while (true) {
|
||||
// printing the game state is fun
|
||||
clear();
|
||||
for (size_t i = 0; i < game->field_size_y; i++) {
|
||||
|
@ -32,26 +32,26 @@ void tui_loop(Game *game) {
|
|||
// these are just codes for arrows,
|
||||
// numpad and Vim bindings
|
||||
switch (input) {
|
||||
case 3:
|
||||
case 107:
|
||||
case 56:
|
||||
dir = Up;
|
||||
break;
|
||||
case 2:
|
||||
case 106:
|
||||
case 50:
|
||||
dir = Down;
|
||||
break;
|
||||
case 4:
|
||||
case 104:
|
||||
case 52:
|
||||
dir = Left;
|
||||
break;
|
||||
case 5:
|
||||
case 108:
|
||||
case 54:
|
||||
dir = Right;
|
||||
break;
|
||||
case 3:
|
||||
case 107:
|
||||
case 56:
|
||||
dir = Up;
|
||||
break;
|
||||
case 2:
|
||||
case 106:
|
||||
case 50:
|
||||
dir = Down;
|
||||
break;
|
||||
case 4:
|
||||
case 104:
|
||||
case 52:
|
||||
dir = Left;
|
||||
break;
|
||||
case 5:
|
||||
case 108:
|
||||
case 54:
|
||||
dir = Right;
|
||||
break;
|
||||
}
|
||||
game_move(game, dir);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue