Formatting and bug
This commit is contained in:
parent
81af4498e7
commit
bdd4e17cad
59
src/2048.c
59
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) {
|
||||
|
||||
|
@ -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;
|
||||
|
@ -80,21 +80,23 @@ void game_move(Game *game, Direction direction) {
|
|||
}
|
||||
int movement = 1;
|
||||
if (direction == Left || direction == Up) {
|
||||
if(direction == Left) {
|
||||
if (direction == Left) {
|
||||
directionality = game->field_size_x - b - 1;
|
||||
} else {
|
||||
}
|
||||
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) {
|
||||
if (direction == Left) {
|
||||
directionality = game->field_size_x - b - 1;
|
||||
} else {
|
||||
}
|
||||
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;
|
||||
}
|
||||
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]) {
|
||||
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) {
|
||||
if (direction == Left) {
|
||||
directionality = game->field_size_x - b - 1;
|
||||
} else {
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
|
53
src/gui.c
53
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,10 +92,9 @@ 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 ) {
|
||||
while (!quit) {
|
||||
while (SDL_PollEvent(&e) != 0) {
|
||||
switch (e.type) {
|
||||
/* Keyboard event */
|
||||
/* Pass the event data onto PrintKeyInfo() */
|
||||
case SDL_KEYDOWN:
|
||||
|
@ -147,7 +142,7 @@ void gui_loop(Game *game) {
|
|||
// 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,9 +1,8 @@
|
|||
#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();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#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;
|
||||
|
@ -14,10 +14,10 @@ void tui_init() {
|
|||
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++) {
|
||||
|
|
Loading…
Reference in New Issue