- 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
This commit is contained in:
raandoom 2014-12-17 22:42:18 +00:00
parent 5ff91b8eca
commit 7b22babed6
6 changed files with 98 additions and 75 deletions

View File

@ -22,7 +22,7 @@ __u32 config_load_highscore()
fs_info cfg = {0}; fs_info cfg = {0};
cfg.func = 0; cfg.func = 0;
cfg.size = 4; cfg.size = sizeof(__u32);
cfg.data = (char*)&highscore; cfg.data = (char*)&highscore;
cfg.name = path; cfg.name = path;
@ -31,7 +31,8 @@ __u32 config_load_highscore()
__asm__ __volatile__("int $0x40":"=a"(ret),"=b"(rnum): __asm__ __volatile__("int $0x40":"=a"(ret),"=b"(rnum):
"a"(70), "a"(70),
"b"((__u32)(&cfg))); "b"((__u32)(&cfg)):
"memory");
if (ret || (rnum != 4)) highscore = 0; if (ret || (rnum != 4)) highscore = 0;
@ -42,7 +43,7 @@ void config_save_highscore(__u32 score)
{ {
fs_info cfg = {0}; fs_info cfg = {0};
cfg.func = 2; cfg.func = 2;
cfg.size = 4; cfg.size = sizeof(__u32);
cfg.data = (char*)&score; cfg.data = (char*)&score;
cfg.name = path; cfg.name = path;
@ -51,5 +52,6 @@ void config_save_highscore(__u32 score)
__asm__ __volatile__("int $0x40":"=a"(ret),"=b"(wnum): __asm__ __volatile__("int $0x40":"=a"(ret),"=b"(wnum):
"a"(70), "a"(70),
"b"((__u32)(&cfg))); "b"((__u32)(&cfg)):
"memory");
} }

View File

@ -16,3 +16,9 @@ inline void clear_key_buffer() {
inline void vsync() { inline void vsync() {
__asm__ __volatile__("int $0x40"::"a"(18),"b"(14)); __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;
}

View File

@ -10,6 +10,8 @@ inline void enable_scancode();
inline void clear_key_buffer(); inline void clear_key_buffer();
inline void vsync(); inline void vsync();
inline __u32 text_length_px(__u32 len);
#define false (0) #define false (0)
#define true (1) #define true (1)
@ -26,7 +28,7 @@ inline void vsync();
#define WND_HEIGHT (400) // main window height #define WND_HEIGHT (400) // main window height
#define GAME_BORDER (30) // minimum border size around board #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 #define SCORE_HEIGHT (21) // minimum height for score text

View File

@ -1,30 +1,49 @@
#include "game.h" #include "game.h"
struct { struct {
rect new_game_button;// new game button place rect reset_button; // new game button place
rect highscore_rect; // highscore place rect highscore_rect; // highscore place
rect score_rect; // score place rect score_rect; // score place
rect over_rect; // game over window
__u8 over; // flag for game over __u8 over; // flag for game over
} game; } game;
void game_draw_top() void game_draw_ui()
{ {
if (game.over) __menuet__make_button(game.reset_button.x,
{ game.reset_button.y,
__menuet__make_button(game.new_game_button.x, game.reset_button.width,
game.new_game_button.y, game.reset_button.height,
game.new_game_button.width,
game.new_game_button.height,
NEW_GAME_BUTTON, NEW_GAME_BUTTON,
BOARD_BG_COLOR); BOARD_BG_COLOR);
rect_draw_text(&game.new_game_button,"NEW GAME",8,GAME_BG_COLOR); rect_draw_text(&game.reset_button,"Restart",7,GAME_BG_COLOR,0);
}
rect_draw(&game.highscore_rect,BOARD_BG_COLOR); 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(&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() void game_init()
@ -41,7 +60,7 @@ void game_init()
(screen_h - WND_HEIGHT) / 2, (screen_h - WND_HEIGHT) / 2,
WND_WIDTH, WND_WIDTH,
WND_HEIGHT, WND_HEIGHT,
GAME_BG_COLOR, 0x34 << 24 | GAME_BG_COLOR,
0, 0,
(__u32)header); (__u32)header);
@ -68,22 +87,29 @@ void game_init()
board_init(&av_area); board_init(&av_area);
game.new_game_button.x = av_area.x; rect top_base = {
game.new_game_button.y = (av_area.y - SCORE_HEIGHT) / 2; .x = av_area.x,
game.new_game_button.width = (av_area.width - BOARD_SPACING) / 3; .y = (av_area.y - SCORE_HEIGHT) / 2,
game.new_game_button.height = SCORE_HEIGHT; .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.reset_button = top_base;
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; top_base.x += top_base.width + BOARD_SPACING;
game.score_rect.y = (av_area.y - SCORE_HEIGHT) / 2; game.highscore_rect = top_base;
game.score_rect.width = (av_area.width - BOARD_SPACING) / 3;
game.score_rect.height = SCORE_HEIGHT;
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); __menuet__window_redraw(2);
} }
@ -104,11 +130,11 @@ void game_redraw()
0, // __u16 y1 : ignored 0, // __u16 y1 : ignored
0, // __u16 xsize : ignored 0, // __u16 xsize : ignored
0, // __u16 ysize : ignored 0, // __u16 ysize : ignored
GAME_BG_COLOR, // __u32 body_color 0x34 << 24 | GAME_BG_COLOR, // __u32 body_color
0, // __u32 grab_color 0, // __u32 grab_color
(__u32)header); // __u32 frame_color or header (__u32)header); // __u32 frame_color or header
game_draw_top(); game_draw_ui();
board_redraw(); board_redraw();
// end redraw // end redraw
@ -124,7 +150,7 @@ void game_move_up()
board_redraw(); board_redraw();
game.over = !added || !board_has_moves(); game.over = !added || !board_has_moves();
game_draw_top(); game_draw_ui();
} }
} }
@ -137,7 +163,7 @@ void game_move_down()
board_redraw(); board_redraw();
game.over = !added || !board_has_moves(); game.over = !added || !board_has_moves();
game_draw_top(); game_draw_ui();
} }
} }
@ -150,7 +176,7 @@ void game_move_left()
board_redraw(); board_redraw();
game.over = !added || !board_has_moves(); game.over = !added || !board_has_moves();
game_draw_top(); game_draw_ui();
} }
} }
@ -163,6 +189,6 @@ void game_move_right()
board_redraw(); board_redraw();
game.over = !added || !board_has_moves(); game.over = !added || !board_has_moves();
game_draw_top(); game_draw_ui();
} }
} }

View File

@ -57,42 +57,29 @@ __u8 rect_transform(rect* from, rect* to, __u16 step)
(from->height == to->height); (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, 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, // right shadow
r->y - 1 + (r->height - FONT_HEIGHT) / 2, __menuet__write_text(r->x + 1 + (r->width - text_length_px(len)) / 2,
0xFFFFFF,txt,len); r->y + (r->height - FONT_HEIGHT) / 2,
__menuet__write_text(r->x - 1 + (r->width - len * FONT_WIDTH - len) / 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, 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,
r->y - 1 + (r->height - FONT_HEIGHT) / 2,
0xFFFFFF,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, r->y + (r->height - FONT_HEIGHT) / 2,
0xFFFFFF,txt,len); color,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);
} }
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}; char buffer[16] = {0};
__u32 length = strlen(itoa(v,buffer,10)); __u32 length = strlen(itoa(v,buffer,10));
rect_draw_text(r,buffer,length,color); rect_draw_text(r,buffer,length,color,frame_color);
} }

View File

@ -24,9 +24,9 @@ void rect_draw(rect* r, __u32 color);
__u8 rect_transform(rect* from, rect* to, __u16 step); __u8 rect_transform(rect* from, rect* to, __u16 step);
// Draw text at the rect center // 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 // 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 #endif // RECT_H