From 518d1e922a1a482f7fe14d1608a8192d750fa0e9 Mon Sep 17 00:00:00 2001 From: raandoom Date: Mon, 15 Dec 2014 22:59:51 +0000 Subject: [PATCH] 2048: - load / save highscore testing TODO: - optimize tile drawing: maybe separate canvas for every tile - game over screen - last game save / load git-svn-id: svn://kolibrios.org@5232 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/games/2048/Makefile | 2 +- programs/games/2048/Tupfile.lua | 2 +- programs/games/2048/board.c | 20 ++++++++++++ programs/games/2048/board.h | 1 + programs/games/2048/config.c | 55 +++++++++++++++++++++++++++++++++ programs/games/2048/config.h | 12 +++++++ programs/games/2048/game.c | 15 +++++++-- 7 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 programs/games/2048/config.c create mode 100644 programs/games/2048/config.h diff --git a/programs/games/2048/Makefile b/programs/games/2048/Makefile index 1b5cef06c..5601a3340 100755 --- a/programs/games/2048/Makefile +++ b/programs/games/2048/Makefile @@ -1,3 +1,3 @@ OUTFILE = 2048 -OBJS = main.o game.o board.o paint.o cell.o rect.o defines.o +OBJS = main.o game.o board.o config.o paint.o cell.o rect.o defines.o include $(MENUETDEV)/makefiles/Makefile_for_program diff --git a/programs/games/2048/Tupfile.lua b/programs/games/2048/Tupfile.lua index 77a92085f..fad127676 100644 --- a/programs/games/2048/Tupfile.lua +++ b/programs/games/2048/Tupfile.lua @@ -2,5 +2,5 @@ if tup.getconfig('NO_GCC') ~= "" then return end HELPERDIR = (tup.getconfig("HELPERDIR") == "") and "../.." or tup.getconfig("HELPERDIR") tup.include(HELPERDIR .. "/use_gcc.lua") tup.include(HELPERDIR .. "/use_menuetlibc.lua") -compile_gcc{"main.c", "defines.c", "rect.c", "paint.c", "cell.c", "board.c", "game.c"} +compile_gcc{"main.c", "defines.c", "rect.c", "paint.c", "cell.c", "config.c", "board.c", "game.c"} link_gcc("2048") diff --git a/programs/games/2048/board.c b/programs/games/2048/board.c index e931edfb0..91c045b5b 100644 --- a/programs/games/2048/board.c +++ b/programs/games/2048/board.c @@ -1,4 +1,5 @@ #include "board.h" +#include "config.h" rect base_cell = {0}; tile null_tile = {0}; @@ -10,6 +11,7 @@ struct { __u16 empty_index[BOARD_MAP_SIZE];// empty cells indexes __u16 empty_count; // empty cells count __u32 score; + __u32 highscore; } board = {0}; // Get tile index for row and column @@ -56,6 +58,10 @@ __u32 random_u32(__u32 max); void board_init(rect* r) { + __u32 high = config_load_highscore(); + if (high > board.highscore) + board.highscore = high; + // seed for random number generator srand(__menuet__getsystemclock()); @@ -92,6 +98,7 @@ void board_init(rect* r) void board_delete() { + config_save_highscore(board.highscore); canvas_delete(); } @@ -167,6 +174,8 @@ __u8 board_up() { moved = true; board.score += indtile->value * 2; + if (board.score > board.highscore) + board.highscore = board.score; tempboard_merge_tile(temp_board,ind,preind); row = 0; } @@ -217,6 +226,8 @@ __u8 board_down() { moved = true; board.score += indtile->value * 2; + if (board.score > board.highscore) + board.highscore = board.score; tempboard_merge_tile(temp_board,ind,preind); row = BOARD_COUNT; } @@ -266,6 +277,8 @@ __u8 board_left() { moved = true; board.score += indtile->value * 2; + if (board.score > board.highscore) + board.highscore = board.score; tempboard_merge_tile(temp_board,ind,preind); column = 0; } @@ -316,6 +329,8 @@ __u8 board_right() { moved = true; board.score += indtile->value * 2; + if (board.score > board.highscore) + board.highscore = board.score; tempboard_merge_tile(temp_board,ind,preind); column = BOARD_COUNT; } @@ -396,6 +411,11 @@ __u32 board_score() return board.score; } +__u32 board_highscore() +{ + return board.highscore; +} + void board_update_empty_info() { board.empty_count = 0; diff --git a/programs/games/2048/board.h b/programs/games/2048/board.h index 24f23b925..4a259ff97 100644 --- a/programs/games/2048/board.h +++ b/programs/games/2048/board.h @@ -23,6 +23,7 @@ __u8 board_has_moves(); // Get score __u32 board_score(); +__u32 board_highscore(); // Try to move all tiles up // Will return true if something moved or false - if not diff --git a/programs/games/2048/config.c b/programs/games/2048/config.c new file mode 100644 index 000000000..cfcbf756e --- /dev/null +++ b/programs/games/2048/config.c @@ -0,0 +1,55 @@ +#include "config.h" + +#pragma pack(push,1) + +typedef struct { + __u32 func; + __u32 l_off; + __u32 h_off_flags; + __u32 size; + char* data; + char null; + char* name; +} fs_info; + +#pragma pack(pop) + +char path[] = "/sys/games/2048.dat"; + +__u32 config_load_highscore() +{ + __u32 highscore = 0; + + fs_info cfg = {0}; + cfg.func = 0; + cfg.size = 4; + cfg.data = (char*)&highscore; + cfg.name = path; + + __u32 ret = 0; + __u32 rnum = 0; + + __asm__ __volatile__("int $0x40":"=a"(ret),"=b"(rnum): + "a"(70), + "b"((__u32)(&cfg))); + + if (ret || (rnum != 4)) highscore = 0; + + return highscore; +} + +void config_save_highscore(__u32 score) +{ + fs_info cfg = {0}; + cfg.func = 2; + cfg.size = 4; + cfg.data = (char*)&score; + cfg.name = path; + + __u32 ret = 0; + __u32 wnum = 0; + + __asm__ __volatile__("int $0x40":"=a"(ret),"=b"(wnum): + "a"(70), + "b"((__u32)(&cfg))); +} diff --git a/programs/games/2048/config.h b/programs/games/2048/config.h new file mode 100644 index 000000000..346802c6e --- /dev/null +++ b/programs/games/2048/config.h @@ -0,0 +1,12 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include "defines.h" + +// Get saved highscore +__u32 config_load_highscore(); + +// Save current highscore +void config_save_highscore(__u32 score); + +#endif // CONFIG_H diff --git a/programs/games/2048/game.c b/programs/games/2048/game.c index 700b3a090..a250d52e9 100644 --- a/programs/games/2048/game.c +++ b/programs/games/2048/game.c @@ -2,6 +2,7 @@ struct { rect new_game_button;// new game button place + rect highscore_rect; // highscore place rect score_rect; // score place __u8 over; // flag for game over } game; @@ -19,6 +20,9 @@ void game_draw_top() rect_draw_text(&game.new_game_button,"NEW GAME",8,GAME_BG_COLOR); } + rect_draw(&game.highscore_rect,BOARD_BG_COLOR); + rect_draw_value(&game.highscore_rect,board_highscore(),GAME_BG_COLOR); + rect_draw(&game.score_rect,BOARD_BG_COLOR); rect_draw_value(&game.score_rect,board_score(),GAME_BG_COLOR); } @@ -66,12 +70,17 @@ void game_init() game.new_game_button.x = av_area.x; game.new_game_button.y = (av_area.y - SCORE_HEIGHT) / 2; - game.new_game_button.width = (av_area.width - BOARD_SPACING) / 2; + game.new_game_button.width = (av_area.width - BOARD_SPACING) / 3; game.new_game_button.height = SCORE_HEIGHT; - game.score_rect.x = av_area.x + (av_area.width + BOARD_SPACING) / 2; + game.highscore_rect.x = av_area.x + (av_area.width + BOARD_SPACING) / 3; + game.highscore_rect.y = (av_area.y - SCORE_HEIGHT) / 2; + game.highscore_rect.width = (av_area.width - BOARD_SPACING) / 3; + game.highscore_rect.height = SCORE_HEIGHT; + + game.score_rect.x = av_area.x + (av_area.width + BOARD_SPACING) * 2 / 3; game.score_rect.y = (av_area.y - SCORE_HEIGHT) / 2; - game.score_rect.width = (av_area.width - BOARD_SPACING) / 2; + game.score_rect.width = (av_area.width - BOARD_SPACING) / 3; game.score_rect.height = SCORE_HEIGHT; game_draw_top();