- 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
This commit is contained in:
raandoom 2014-12-15 22:59:51 +00:00
parent 3fdad8ab94
commit 518d1e922a
7 changed files with 102 additions and 5 deletions

View File

@ -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

View File

@ -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")

View File

@ -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;

View File

@ -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

View File

@ -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)));
}

View File

@ -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

View File

@ -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();