score crap
This commit is contained in:
parent
558086f56f
commit
81af4498e7
12
src/2048.c
12
src/2048.c
|
@ -32,6 +32,7 @@ void game_add_block(Game *game) {
|
||||||
Game game_init(size_t field_size_x, size_t field_size_y) {
|
Game game_init(size_t field_size_x, size_t field_size_y) {
|
||||||
Game game;
|
Game game;
|
||||||
game.game_over = false;
|
game.game_over = false;
|
||||||
|
game.won = false;
|
||||||
game.field_size_x = field_size_x;
|
game.field_size_x = field_size_x;
|
||||||
game.field_size_y = field_size_y;
|
game.field_size_y = field_size_y;
|
||||||
game.score = 0;
|
game.score = 0;
|
||||||
|
@ -56,6 +57,9 @@ void game_destroy(Game game) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_move(Game *game, Direction direction) {
|
void game_move(Game *game, Direction direction) {
|
||||||
|
if (game->game_over || game->won) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
for (int i = 0; i < game->field_size_y; i++) {
|
for (int i = 0; i < game->field_size_y; i++) {
|
||||||
uint16_t **field = game->field;
|
uint16_t **field = game->field;
|
||||||
// move everything on one side
|
// move everything on one side
|
||||||
|
@ -123,12 +127,20 @@ void game_move(Game *game, Direction direction) {
|
||||||
if (direction == Left || direction == Right) {
|
if (direction == Left || direction == Right) {
|
||||||
if(field[a][directionality + movement] == field[a][directionality]) {
|
if(field[a][directionality + movement] == field[a][directionality]) {
|
||||||
field[a][directionality+movement] = field[a][directionality]*2;
|
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;
|
field[a][directionality] = 0;
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(field[directionality + movement][b] == field[directionality][b]) {
|
if(field[directionality + movement][b] == field[directionality][b]) {
|
||||||
field[directionality + movement][b] = field[directionality][b]*2;
|
field[directionality + movement][b] = field[directionality][b]*2;
|
||||||
|
game->score += field[directionality + movement][b];
|
||||||
|
if (field[directionality + movement][b]) {
|
||||||
|
game->won = true;
|
||||||
|
}
|
||||||
field[directionality][b] = 0;
|
field[directionality][b] = 0;
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ typedef struct Game
|
||||||
size_t field_size_x, field_size_y;
|
size_t field_size_x, field_size_y;
|
||||||
uint16_t **field;
|
uint16_t **field;
|
||||||
bool game_over;
|
bool game_over;
|
||||||
|
bool won;
|
||||||
} Game;
|
} Game;
|
||||||
|
|
||||||
Game game_init(size_t field_size_x, size_t field_size_y);
|
Game game_init(size_t field_size_x, size_t field_size_y);
|
||||||
|
|
31
src/gui.c
31
src/gui.c
|
@ -1,6 +1,5 @@
|
||||||
#include "gui.h"
|
#include "gui.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_image.h>
|
#include <SDL_image.h>
|
||||||
#include <SDL_ttf.h>
|
#include <SDL_ttf.h>
|
||||||
|
@ -8,6 +7,8 @@
|
||||||
const SCREEN_WIDTH = 800;
|
const SCREEN_WIDTH = 800;
|
||||||
const SCREEN_HEIGHT = 920;
|
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_Surface* screenSurface = NULL;
|
||||||
SDL_Renderer *window_renderer = NULL;
|
SDL_Renderer *window_renderer = NULL;
|
||||||
|
@ -15,6 +16,8 @@ SDL_Texture* tile_texture = NULL;
|
||||||
SDL_Texture* tile_textures[13];
|
SDL_Texture* tile_textures[13];
|
||||||
SDL_Rect tile_size;
|
SDL_Rect tile_size;
|
||||||
SDL_Texture* game_over_texture = NULL;
|
SDL_Texture* game_over_texture = NULL;
|
||||||
|
TTF_Font* Sans = NULL;
|
||||||
|
SDL_Color Black = {0, 0, 0};
|
||||||
|
|
||||||
SDL_Texture *texture_from_png(char *n) {
|
SDL_Texture *texture_from_png(char *n) {
|
||||||
SDL_Surface *s = IMG_Load(n);
|
SDL_Surface *s = IMG_Load(n);
|
||||||
|
@ -36,6 +39,8 @@ void gui_init() {
|
||||||
{
|
{
|
||||||
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
|
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TTF_Init();
|
||||||
// screenSurface = SDL_GetWindowSurface(window);
|
// screenSurface = SDL_GetWindowSurface(window);
|
||||||
// SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 255, 255, 255));
|
// SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 255, 255, 255));
|
||||||
|
|
||||||
|
@ -50,6 +55,7 @@ void gui_init() {
|
||||||
// load up textures
|
// load up textures
|
||||||
tile_texture = texture_from_png("res/tile.png");
|
tile_texture = texture_from_png("res/tile.png");
|
||||||
tile_textures[0] = tile_texture;
|
tile_textures[0] = tile_texture;
|
||||||
|
// there's no texture for 1 because it's unnecesary - sad pikachu face
|
||||||
tile_textures[2] = 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[3] = texture_from_png("res/4.png");
|
||||||
tile_textures[4] = texture_from_png("res/8.png");
|
tile_textures[4] = texture_from_png("res/8.png");
|
||||||
|
@ -66,6 +72,8 @@ void gui_init() {
|
||||||
SDL_QueryTexture(tile_texture, NULL, NULL, &tile_size.w, &tile_size.h);
|
SDL_QueryTexture(tile_texture, NULL, NULL, &tile_size.w, &tile_size.h);
|
||||||
|
|
||||||
game_over_texture = texture_from_png("res/game_over.png");
|
game_over_texture = texture_from_png("res/game_over.png");
|
||||||
|
|
||||||
|
Sans = TTF_OpenFont("/usr/share/fonts/liberation/LiberationSerif-Regular.ttf", 92);
|
||||||
}
|
}
|
||||||
|
|
||||||
void gui_destroy() {
|
void gui_destroy() {
|
||||||
|
@ -77,6 +85,7 @@ void gui_destroy() {
|
||||||
}
|
}
|
||||||
SDL_DestroyTexture(game_over_texture);
|
SDL_DestroyTexture(game_over_texture);
|
||||||
|
|
||||||
|
TTF_Quit();
|
||||||
//Quit SDL subsystems
|
//Quit SDL subsystems
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|
||||||
|
@ -142,17 +151,31 @@ void gui_loop(Game *game) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// SDL_RenderCopy( window_renderer, tile_texture, NULL, &tile_rect);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// score - I am too lazy to optimize this further
|
||||||
|
char label_text[24] = "Score: 0x";
|
||||||
|
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_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;
|
||||||
|
score_label_rect.y = 25;
|
||||||
|
SDL_RenderCopy(window_renderer, score_label_texture, NULL, &score_label_rect);
|
||||||
|
SDL_FreeSurface(score_label);
|
||||||
|
SDL_DestroyTexture(score_label_texture);
|
||||||
|
|
||||||
if (game->game_over) {
|
if (game->game_over) {
|
||||||
// render game over screen
|
// render game over screen
|
||||||
SDL_RenderCopy(window_renderer, game_over_texture, NULL, NULL);
|
SDL_RenderCopy(window_renderer, game_over_texture, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_RenderPresent(window_renderer);
|
SDL_RenderPresent(window_renderer);
|
||||||
needs_redraw = false;
|
needs_redraw = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue