Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arkanoid show score, lives. Fix NULL pointer dereference #188

Merged
merged 2 commits into from
Aug 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions applications/arkanoid/arkanoid_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ void move_ball(Canvas* canvas) {
released = false;
lives--;

snprintf(text, sizeof(text), "LIVES:%u", lives);
canvas_draw_str(canvas, 0, 90, text);

// arduboy.tunes.tone(175, 250);
if(rand_range(0, 2) == 0) {
dx = 1;
Expand Down Expand Up @@ -165,10 +162,7 @@ void move_ball(Canvas* canvas) {
//If A collison has occured
if(topBall <= bottomBrick && bottomBall >= topBrick &&
leftBall <= rightBrick && rightBall >= leftBrick) {
// Draw score
score += (level * 10);
snprintf(text, sizeof(text), "SCORE:%u", score);
canvas_draw_str(canvas, 80, 90, text);

brickCount++;
isHit[row][column] = true;
Expand Down Expand Up @@ -209,8 +203,42 @@ void move_ball(Canvas* canvas) {
}

void draw_lives(Canvas* canvas) {
sprintf(text, "LIVES:%u", lives);
canvas_draw_str(canvas, 0, 90, text);
if(lives == 3) {
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 7);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 7);
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 8);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 8);

canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 11);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 11);
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 12);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 12);

canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 15);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 15);
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 16);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 16);
} else if(lives == 2) {
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 7);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 7);
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 8);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 8);

canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 11);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 11);
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 12);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 12);
} else {
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 7);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 7);
canvas_draw_dot(canvas, 4, FLIPPER_LCD_HEIGHT - 8);
canvas_draw_dot(canvas, 3, FLIPPER_LCD_HEIGHT - 8);
}
}

void draw_score(Canvas* canvas) {
snprintf(text, sizeof(text), "%u", score);
canvas_draw_str_aligned(canvas, FLIPPER_LCD_WIDTH - 2, FLIPPER_LCD_HEIGHT - 6, AlignRight, AlignBottom, text);
}

void draw_ball(Canvas* canvas) {
Expand Down Expand Up @@ -286,8 +314,9 @@ static void arkanoid_draw_callback(Canvas* const canvas, void* ctx) {

if(lives > 0) {
draw_paddle(canvas);

draw_ball(canvas);
draw_score(canvas);
draw_lives(canvas);

if(brickCount == ROWS * COLUMNS) {
level++;
Expand Down Expand Up @@ -320,6 +349,7 @@ static void arkanoid_update_timer_callback(FuriMessageQueue* event_queue) {

int32_t arkanoid_game_app(void* p) {
UNUSED(p);
int32_t return_code = 0;
// Set random seed from interrupts
srand(DWT->CYCCNT);

Expand All @@ -331,8 +361,8 @@ int32_t arkanoid_game_app(void* p) {
ValueMutex state_mutex;
if(!init_mutex(&state_mutex, arkanoid_state, sizeof(ArkanoidState))) {
FURI_LOG_E(TAG, "Cannot create mutex\r\n");
free(arkanoid_state);
return 255;
return_code = 255;
goto free_and_exit;
}

// Set system callbacks
Expand Down Expand Up @@ -401,12 +431,16 @@ int32_t arkanoid_game_app(void* p) {
view_port_update(view_port);
release_mutex(&state_mutex, arkanoid_state);
}

furi_timer_free(timer);
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
furi_record_close("gui");
view_port_free(view_port);
delete_mutex(&state_mutex);

free_and_exit:
free(arkanoid_state);
furi_message_queue_free(event_queue);

return 0;
return return_code;
}