Skip to content

Commit

Permalink
Fix Twos game initialization
Browse files Browse the repository at this point in the history
Revert to use lv_table, because the memory assignment for
`lv_obj_create` runs out of memory in the fourth row
  • Loading branch information
NeroBurner committed Oct 16, 2021
1 parent 7489997 commit b13aec2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 80 deletions.
132 changes: 56 additions & 76 deletions src/displayapp/screens/Twos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,66 @@ namespace {
const lv_color_t TWOS_COLOR_128_PLUS = lv_color_hex(0x007dc5);
const lv_color_t TWOS_BORDER_DEFAULT = lv_color_hex(0xbbada0);
const lv_color_t TWOS_BORDER_NEW = lv_color_darken(lv_color_hex(0xbbada0), LV_OPA_50);

static void draw_part_event_cb(lv_event_t* event){
lv_obj_draw_part_dsc_t *dsc = static_cast<lv_obj_draw_part_dsc_t*>(lv_event_get_param(event));
if(dsc->part == LV_PART_ITEMS) {
switch (dsc->value) {
case 0:
dsc->rect_dsc->bg_color = TWOS_COLOR_0;
break;
case 2:
case 4:
dsc->rect_dsc->bg_color = TWOS_COLOR_2_4;
break;
case 8:
case 16:
dsc->rect_dsc->bg_color = TWOS_COLOR_8_16;
break;
case 32:
case 64:
dsc->rect_dsc->bg_color = TWOS_COLOR_32_64;
break;
default:
dsc->rect_dsc->bg_color = TWOS_COLOR_128_PLUS;
break;

}
}
}
}

Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) {

// create styles to apply to different valued tiles
lv_style_init(&style_cell_default);
lv_style_init(&style_cell1);
lv_style_init(&style_cell2);
lv_style_init(&style_cell3);
lv_style_init(&style_cell4);
lv_style_init(&style_cell5);

lv_style_set_border_color(&style_cell_default, TWOS_BORDER_DEFAULT);
lv_style_set_border_width(&style_cell_default, 3);
lv_style_set_bg_opa(&style_cell_default, LV_OPA_COVER);
lv_style_set_bg_color(&style_cell_default, TWOS_COLOR_0);
lv_style_set_pad_top(&style_cell_default, 25);
lv_style_set_text_color(&style_cell_default, lv_color_black());
lv_style_set_radius(&style_cell_default, 0);
lv_style_set_pad_all(&style_cell_default, 0);

// format grid display
gridDisplay = lv_table_create(lv_scr_act());
lv_obj_add_style(gridDisplay, &style_cell_default, LV_PART_ITEMS | LV_STATE_DEFAULT);
lv_table_set_col_cnt(gridDisplay, 4);
lv_table_set_row_cnt(gridDisplay, 4);
lv_table_set_col_width(gridDisplay, 0, LV_HOR_RES / 4);
lv_table_set_col_width(gridDisplay, 1, LV_HOR_RES / 4);
lv_table_set_col_width(gridDisplay, 2, LV_HOR_RES / 4);
lv_table_set_col_width(gridDisplay, 3, LV_HOR_RES / 4);
lv_obj_align(gridDisplay, LV_ALIGN_BOTTOM_MID, 0, 0);

twosContainer = lv_obj_create(lv_scr_act());
lv_obj_set_size(twosContainer, LV_HOR_RES, LV_VER_RES-40);
lv_obj_align(twosContainer, LV_ALIGN_BOTTOM_MID, 0, 0);
int cell_w = LV_HOR_RES/4;
int cell_h = (LV_VER_RES-40)/4;
lv_obj_t *align_to = twosContainer;
lv_align_t align_by = LV_ALIGN_TOP_LEFT;
// initialize grid
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
Tile *curTile = &grid[row][col];

curTile->box = lv_obj_create(twosContainer);
lv_obj_add_style(curTile->box, &style_cell_default, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_size(curTile->box, cell_w, cell_h);

curTile->lbl = lv_label_create(curTile->box);
lv_obj_clear_flag(curTile->lbl, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_center(curTile->lbl);
lv_label_set_text(curTile->lbl, "");
lv_label_set_long_mode(curTile->lbl, LV_LABEL_LONG_CLIP);

lv_obj_align_to(curTile->box, align_to, align_by, 0, 0);
if(col != 3){
align_to = curTile->box;
align_by = LV_ALIGN_OUT_RIGHT_MID;
} else{
align_to = grid[row][0].box;
align_by = LV_ALIGN_OUT_BOTTOM_MID;
}

grid[row][col].value = 0;
}
}
placeNewTile();
Expand All @@ -74,7 +86,7 @@ Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) {
scoreText = lv_label_create(lv_scr_act());
lv_obj_set_width(scoreText, LV_HOR_RES);
lv_obj_set_style_text_align(scoreText, LV_ALIGN_LEFT_MID, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_align(scoreText, LV_ALIGN_TOP_LEFT, -10, 10);
lv_obj_align(scoreText, LV_ALIGN_TOP_LEFT, 0, 10);
lv_label_set_recolor(scoreText, true);
lv_label_set_text_fmt(scoreText, "Score #FFFF00 %i#", score);

Expand All @@ -83,10 +95,17 @@ Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) {
lv_obj_set_size(backgroundLabel, 240, 240);
lv_obj_set_pos(backgroundLabel, 0, 0);
lv_label_set_text(backgroundLabel, "");

// Need a draw callback to color the cells
lv_obj_add_event_cb(gridDisplay, draw_part_event_cb, LV_EVENT_DRAW_PART_BEGIN, gridDisplay->user_data);
}

Twos::~Twos() {
lv_style_reset(&style_cell_default);
lv_style_reset(&style_cell1);
lv_style_reset(&style_cell2);
lv_style_reset(&style_cell3);
lv_style_reset(&style_cell4);
lv_style_reset(&style_cell5);
lv_obj_clean(lv_scr_act());
}

Expand Down Expand Up @@ -125,34 +144,6 @@ bool Twos::placeNewTile() {
return true;
}

void Twos::updateTileColor(Tile *tile){
lv_color_t color;
switch (tile->value) {
case 0:
color = TWOS_COLOR_0;
break;
case 2:
case 4:
color = TWOS_COLOR_2_4;
break;
case 8:
case 16:
color = TWOS_COLOR_8_16;
break;
case 32:
case 64:
color = TWOS_COLOR_32_64;
break;
default:
color = TWOS_COLOR_128_PLUS;
break;
}
lv_obj_set_style_bg_color(tile->box, color, LV_PART_MAIN | LV_STATE_DEFAULT);

lv_color_t borderColor = tile->isNew ? TWOS_BORDER_NEW : TWOS_BORDER_DEFAULT;
lv_obj_set_style_border_color(tile->box, borderColor, LV_PART_MAIN | LV_STATE_DEFAULT);
}

bool Twos::tryMerge(Tile grid[][4], int& newRow, int& newCol, int oldRow, int oldCol) {
if (grid[newRow][newCol].value == grid[oldRow][oldCol].value) {
if ((newCol != oldCol) || (newRow != oldRow)) {
Expand All @@ -164,9 +155,6 @@ bool Twos::tryMerge(Tile grid[][4], int& newRow, int& newCol, int oldRow, int ol
grid[oldRow][oldCol].value = 0;
grid[newRow][newCol].merged = true;

grid[oldRow][oldCol].changed = true;
grid[newRow][newCol].changed = true;

return true;
}
}
Expand All @@ -178,10 +166,6 @@ bool Twos::tryMove(Tile grid[][4], int newRow, int newCol, int oldRow, int oldCo
if (((newCol >= 0) && (newCol != oldCol)) || ((newRow >= 0) && (newRow != oldRow))) {
grid[newRow][newCol].value = grid[oldRow][oldCol].value;
grid[oldRow][oldCol].value = 0;

grid[oldRow][oldCol].changed = true;
grid[newRow][newCol].changed = true;

return true;
}
return false;
Expand Down Expand Up @@ -297,14 +281,10 @@ bool Twos::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
void Twos::updateGridDisplay(Tile grid[][4]) {
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
Tile *curTile = &grid[row][col];
if (curTile->changed){
if (curTile->value) {
lv_label_set_text(curTile->lbl, std::to_string(curTile->value).c_str());
} else {
lv_label_set_text(curTile->lbl, "");
}
updateTileColor(curTile);
if (grid[row][col].value) {
lv_table_set_cell_value(gridDisplay, row, col, (std::to_string(grid[row][col].value)).c_str());
} else {
lv_table_set_cell_value(gridDisplay, row, col, "");
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/displayapp/screens/Twos.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace Pinetime {
bool changed = true;
bool isNew = false;
unsigned int value = 0;
lv_obj_t *box = nullptr;
lv_obj_t *lbl = nullptr;
};
namespace Screens {
class Twos : public Screen {
Expand All @@ -30,14 +28,13 @@ namespace Pinetime {
lv_style_t style_cell5;

lv_obj_t* scoreText;
lv_obj_t* twosContainer;
lv_obj_t* gridDisplay;
Tile grid[4][4];
unsigned int score = 0;
void updateGridDisplay(Tile grid[][4]);
bool tryMerge(Tile grid[][4], int& newRow, int& newCol, int oldRow, int oldCol);
bool tryMove(Tile grid[][4], int newRow, int newCol, int oldRow, int oldCol);
bool placeNewTile();
void updateTileColor(Tile* tile);
};
}
}
Expand Down

0 comments on commit b13aec2

Please sign in to comment.