From 7b22babed64c30ffa1e136088df43fd9f3b2c8e3 Mon Sep 17 00:00:00 2001 From: raandoom Date: Wed, 17 Dec 2014 22:42:18 +0000 Subject: [PATCH] 2048: - fix load / save highscore - added message about game over - beautify text draw TODO: - optimize tile drawing: maybe separate canvas for every tile git-svn-id: svn://kolibrios.org@5238 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/games/2048/config.c | 10 ++-- programs/games/2048/defines.c | 6 ++ programs/games/2048/defines.h | 4 +- programs/games/2048/game.c | 106 +++++++++++++++++++++------------- programs/games/2048/rect.c | 43 +++++--------- programs/games/2048/rect.h | 4 +- 6 files changed, 98 insertions(+), 75 deletions(-) diff --git a/programs/games/2048/config.c b/programs/games/2048/config.c index cfcbf756ec..cf841322a5 100644 --- a/programs/games/2048/config.c +++ b/programs/games/2048/config.c @@ -22,7 +22,7 @@ __u32 config_load_highscore() fs_info cfg = {0}; cfg.func = 0; - cfg.size = 4; + cfg.size = sizeof(__u32); cfg.data = (char*)&highscore; cfg.name = path; @@ -31,7 +31,8 @@ __u32 config_load_highscore() __asm__ __volatile__("int $0x40":"=a"(ret),"=b"(rnum): "a"(70), - "b"((__u32)(&cfg))); + "b"((__u32)(&cfg)): + "memory"); if (ret || (rnum != 4)) highscore = 0; @@ -42,7 +43,7 @@ void config_save_highscore(__u32 score) { fs_info cfg = {0}; cfg.func = 2; - cfg.size = 4; + cfg.size = sizeof(__u32); cfg.data = (char*)&score; cfg.name = path; @@ -51,5 +52,6 @@ void config_save_highscore(__u32 score) __asm__ __volatile__("int $0x40":"=a"(ret),"=b"(wnum): "a"(70), - "b"((__u32)(&cfg))); + "b"((__u32)(&cfg)): + "memory"); } diff --git a/programs/games/2048/defines.c b/programs/games/2048/defines.c index ac8baf742b..ca1d6fbafe 100644 --- a/programs/games/2048/defines.c +++ b/programs/games/2048/defines.c @@ -16,3 +16,9 @@ inline void clear_key_buffer() { inline void vsync() { __asm__ __volatile__("int $0x40"::"a"(18),"b"(14)); } + +// Get text length in pixels +inline __u32 text_length_px(__u32 len) +{ + return len * FONT_WIDTH + len; +} diff --git a/programs/games/2048/defines.h b/programs/games/2048/defines.h index c6cf661d8b..61451e7497 100644 --- a/programs/games/2048/defines.h +++ b/programs/games/2048/defines.h @@ -10,6 +10,8 @@ inline void enable_scancode(); inline void clear_key_buffer(); inline void vsync(); +inline __u32 text_length_px(__u32 len); + #define false (0) #define true (1) @@ -26,7 +28,7 @@ inline void vsync(); #define WND_HEIGHT (400) // main window height #define GAME_BORDER (30) // minimum border size around board -#define GAME_BG_COLOR (0x34FAF8EF) // main window background color +#define GAME_BG_COLOR (0xFAF8EF) // main window background color #define SCORE_HEIGHT (21) // minimum height for score text diff --git a/programs/games/2048/game.c b/programs/games/2048/game.c index a250d52e9d..e61f9d06ef 100644 --- a/programs/games/2048/game.c +++ b/programs/games/2048/game.c @@ -1,30 +1,49 @@ #include "game.h" struct { - rect new_game_button;// new game button place + rect reset_button; // new game button place rect highscore_rect; // highscore place rect score_rect; // score place + rect over_rect; // game over window __u8 over; // flag for game over } game; -void game_draw_top() +void game_draw_ui() { - if (game.over) - { - __menuet__make_button(game.new_game_button.x, - game.new_game_button.y, - game.new_game_button.width, - game.new_game_button.height, - NEW_GAME_BUTTON, - BOARD_BG_COLOR); - rect_draw_text(&game.new_game_button,"NEW GAME",8,GAME_BG_COLOR); - } + __menuet__make_button(game.reset_button.x, + game.reset_button.y, + game.reset_button.width, + game.reset_button.height, + NEW_GAME_BUTTON, + BOARD_BG_COLOR); + rect_draw_text(&game.reset_button,"Restart",7,GAME_BG_COLOR,0); rect_draw(&game.highscore_rect,BOARD_BG_COLOR); - rect_draw_value(&game.highscore_rect,board_highscore(),GAME_BG_COLOR); + rect_draw_value(&game.highscore_rect,board_highscore(),GAME_BG_COLOR,0); rect_draw(&game.score_rect,BOARD_BG_COLOR); - rect_draw_value(&game.score_rect,board_score(),GAME_BG_COLOR); + rect_draw_value(&game.score_rect,board_score(),GAME_BG_COLOR,0); + + if (game.over) + { + __u16 line_step = FONT_HEIGHT * 2; + rect_draw(&game.over_rect,BOARD_BG_COLOR); + + rect line_rect = { + .x = game.over_rect.x, + .y = game.over_rect.y + line_step, + .width = game.over_rect.width, + .height = line_step + }; + + rect_draw_text(&line_rect,"It looks like there is",22,0xFFFFFF,0); + + line_rect.y += line_step; + rect_draw_text(&line_rect,"no more moves",13,0xFFFFFF,0); + + line_rect.y += line_step; + rect_draw_text(&line_rect,"available",9,0xFFFFFF,0); + } } void game_init() @@ -41,7 +60,7 @@ void game_init() (screen_h - WND_HEIGHT) / 2, WND_WIDTH, WND_HEIGHT, - GAME_BG_COLOR, + 0x34 << 24 | GAME_BG_COLOR, 0, (__u32)header); @@ -68,22 +87,29 @@ void game_init() board_init(&av_area); - 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) / 3; - game.new_game_button.height = SCORE_HEIGHT; + rect top_base = { + .x = av_area.x, + .y = (av_area.y - SCORE_HEIGHT) / 2, + .width = (av_area.width - BOARD_SPACING * 2) / 3, + .height = SCORE_HEIGHT + }; - 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.reset_button = top_base; - 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) / 3; - game.score_rect.height = SCORE_HEIGHT; + top_base.x += top_base.width + BOARD_SPACING; + game.highscore_rect = top_base; - game_draw_top(); + top_base.x += top_base.width + BOARD_SPACING; + game.score_rect = top_base; + + av_area.x += av_area.width / 4; + av_area.y += av_area.height / 4; + av_area.width /= 2; + av_area.height /= 2; + + game.over_rect = av_area; + + game_draw_ui(); __menuet__window_redraw(2); } @@ -100,15 +126,15 @@ void game_redraw() // start redraw __menuet__window_redraw(1); - __menuet__define_window(0, // __u16 x1 : ignored - 0, // __u16 y1 : ignored - 0, // __u16 xsize : ignored - 0, // __u16 ysize : ignored - GAME_BG_COLOR, // __u32 body_color - 0, // __u32 grab_color - (__u32)header); // __u32 frame_color or header + __menuet__define_window(0, // __u16 x1 : ignored + 0, // __u16 y1 : ignored + 0, // __u16 xsize : ignored + 0, // __u16 ysize : ignored + 0x34 << 24 | GAME_BG_COLOR, // __u32 body_color + 0, // __u32 grab_color + (__u32)header); // __u32 frame_color or header - game_draw_top(); + game_draw_ui(); board_redraw(); // end redraw @@ -124,7 +150,7 @@ void game_move_up() board_redraw(); game.over = !added || !board_has_moves(); - game_draw_top(); + game_draw_ui(); } } @@ -137,7 +163,7 @@ void game_move_down() board_redraw(); game.over = !added || !board_has_moves(); - game_draw_top(); + game_draw_ui(); } } @@ -150,7 +176,7 @@ void game_move_left() board_redraw(); game.over = !added || !board_has_moves(); - game_draw_top(); + game_draw_ui(); } } @@ -163,6 +189,6 @@ void game_move_right() board_redraw(); game.over = !added || !board_has_moves(); - game_draw_top(); + game_draw_ui(); } } diff --git a/programs/games/2048/rect.c b/programs/games/2048/rect.c index dedde6bded..02c59c3be3 100644 --- a/programs/games/2048/rect.c +++ b/programs/games/2048/rect.c @@ -57,42 +57,29 @@ __u8 rect_transform(rect* from, rect* to, __u16 step) (from->height == to->height); } -void rect_draw_text(rect *r, char *txt, __u32 len, __u32 color) +void rect_draw_text(rect *r, char *txt, __u32 len, __u32 color, __u32 frame_color) { - __menuet__write_text(r->x + 1 + (r->width - len * FONT_WIDTH - len) / 2, + // right down shadow + __menuet__write_text(r->x + 1 + (r->width - text_length_px(len)) / 2, r->y + 1 + (r->height - FONT_HEIGHT) / 2, - 0xFFFFFF,txt,len); - __menuet__write_text(r->x - 1 + (r->width - len * FONT_WIDTH - len) / 2, - r->y - 1 + (r->height - FONT_HEIGHT) / 2, - 0xFFFFFF,txt,len); - __menuet__write_text(r->x - 1 + (r->width - len * FONT_WIDTH - len) / 2, + frame_color,txt,len); + // right shadow + __menuet__write_text(r->x + 1 + (r->width - text_length_px(len)) / 2, + r->y + (r->height - FONT_HEIGHT) / 2, + frame_color,txt,len); + // down shadow + __menuet__write_text(r->x + (r->width - text_length_px(len)) / 2, r->y + 1 + (r->height - FONT_HEIGHT) / 2, - 0xFFFFFF,txt,len); - __menuet__write_text(r->x + 1 + (r->width - len * FONT_WIDTH - len) / 2, - r->y - 1 + (r->height - FONT_HEIGHT) / 2, - 0xFFFFFF,txt,len); + frame_color,txt,len); - __menuet__write_text(r->x + 1 + (r->width - len * FONT_WIDTH - len) / 2, + __menuet__write_text(r->x + (r->width - text_length_px(len)) / 2, r->y + (r->height - FONT_HEIGHT) / 2, - 0xFFFFFF,txt,len); - __menuet__write_text(r->x - 1 + (r->width - len * FONT_WIDTH - len) / 2, - r->y + (r->height - FONT_HEIGHT) / 2, - 0xFFFFFF,txt,len); - __menuet__write_text(r->x + (r->width - len * FONT_WIDTH - len) / 2, - r->y + 1 + (r->height - FONT_HEIGHT) / 2, - 0xFFFFFF,txt,len); - __menuet__write_text(r->x + (r->width - len * FONT_WIDTH - len) / 2, - r->y - 1 + (r->height - FONT_HEIGHT) / 2, - 0xFFFFFF,txt,len); - - __menuet__write_text(r->x + (r->width - len * FONT_WIDTH - len) / 2, - r->y + (r->height - FONT_HEIGHT) / 2, - 0,txt,len); + color,txt,len); } -void rect_draw_value(rect* r, __u32 v, __u32 color) +void rect_draw_value(rect* r, __u32 v, __u32 color, __u32 frame_color) { char buffer[16] = {0}; __u32 length = strlen(itoa(v,buffer,10)); - rect_draw_text(r,buffer,length,color); + rect_draw_text(r,buffer,length,color,frame_color); } diff --git a/programs/games/2048/rect.h b/programs/games/2048/rect.h index d03b16877f..2d0a867a93 100644 --- a/programs/games/2048/rect.h +++ b/programs/games/2048/rect.h @@ -24,9 +24,9 @@ void rect_draw(rect* r, __u32 color); __u8 rect_transform(rect* from, rect* to, __u16 step); // Draw text at the rect center -void rect_draw_text(rect* r, char* txt, __u32 len, __u32 color); +void rect_draw_text(rect* r, char* txt, __u32 len, __u32 color, __u32 frame_color); // Draw value as text at the rect center -void rect_draw_value(rect* r, __u32 v, __u32 color); +void rect_draw_value(rect* r, __u32 v, __u32 color, __u32 frame_color); #endif // RECT_H