Compare commits

..

1 Commits

Author SHA1 Message Date
Adolfo Reyna e406a06f61 Fix race condition in display refresh locking 2026-02-13 13:40:45 -05:00
9 changed files with 55 additions and 51 deletions
+6 -2
View File
@@ -88,7 +88,7 @@ void core1_entry() {
while (1) { while (1) {
// Wait for refresh request // Wait for refresh request
if (refresh_requested && refresh_buffer && refresh_display) { if (refresh_requested && refresh_buffer && refresh_display) {
refresh_in_progress = true; // refresh_in_progress is already set by Core 0 to lock the buffer
// Get local copies for safe access // Get local copies for safe access
LowLevelDisplay* display = refresh_display; LowLevelDisplay* display = refresh_display;
@@ -100,7 +100,7 @@ void core1_entry() {
// Clear flags // Clear flags
refresh_requested = false; refresh_requested = false;
refresh_in_progress = false; refresh_in_progress = false; // Unlock buffer for Core 0
} }
// Small delay to avoid busy-waiting // Small delay to avoid busy-waiting
@@ -124,6 +124,10 @@ bool refresh_screen_async(const uint8_t *buffer, LowLevelDisplay* display) {
return false; return false;
} }
// Lock the buffer immediately on Core 0 to prevent race condition
// Core 1 will unlock it (set to false) when done
refresh_in_progress = true;
// Queue refresh on Core 1 // Queue refresh on Core 1
refresh_buffer = buffer; refresh_buffer = buffer;
refresh_display = display; refresh_display = display;
+8 -8
View File
@@ -87,9 +87,9 @@ function draw()
local state = game.vars.state local state = game.vars.state
if state == STATE_MENU then if state == STATE_MENU then
renderer.text_scaled(game.width() / 2 - 35, game.height() / 2 - 30, "AIR HOCKEY", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 35, game.height() / 2 - 30, "AIR HOCKEY", true)
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2, "Tap to Start", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2, "Tap to Start", true)
renderer.text_scaled(game.width() / 2 - 60, game.height() / 2 + 20, "First to " .. tostring(MAX_SCORE), true, 2) renderer.text_scaled(game.width(, 2) / 2 - 60, game.height() / 2 + 20, "First to " .. tostring(MAX_SCORE), true)
elseif state == STATE_PLAYING or state == STATE_GAME_OVER then elseif state == STATE_PLAYING or state == STATE_GAME_OVER then
-- Draw center line -- Draw center line
@@ -109,14 +109,14 @@ function draw()
renderer.circle(math.floor(game.vars.puck_x + 0.5), math.floor(game.vars.puck_y + 0.5), PUCK_RADIUS, true, true) renderer.circle(math.floor(game.vars.puck_x + 0.5), math.floor(game.vars.puck_y + 0.5), PUCK_RADIUS, true, true)
-- Draw scores -- Draw scores
renderer.text_scaled(game.width() / 2 - 30, 5, tostring(game.vars.paddle_left_score), true, 2) renderer.text_scaled(game.width(, 2) / 2 - 30, 5, tostring(game.vars.paddle_left_score), true)
renderer.text_scaled(game.width() / 2 + 20, 5, tostring(game.vars.paddle_right_score), true, 2) renderer.text_scaled(game.width(, 2) / 2 + 20, 5, tostring(game.vars.paddle_right_score), true)
if state == STATE_GAME_OVER then if state == STATE_GAME_OVER then
local winner = game.vars.paddle_left_score > game.vars.paddle_right_score and "Player 1" or "Player 2" local winner = game.vars.paddle_left_score > game.vars.paddle_right_score and "Player 1" or "Player 2"
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 - 20, "GAME OVER", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2 - 20, "GAME OVER", true)
renderer.text_scaled(game.width() / 2 - 40, game.height() / 2, winner .. " Wins!", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 40, game.height() / 2, winner .. " Wins!", true)
renderer.text_scaled(game.width() / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true)
end end
end end
end end
+6 -6
View File
@@ -100,8 +100,8 @@ function draw()
local state = game.vars.state local state = game.vars.state
if state == STATE_MENU then if state == STATE_MENU then
renderer.text_scaled(game.width() / 2 - 35, game.height() / 2 - 30, "ASTEROIDS", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 35, game.height() / 2 - 30, "ASTEROIDS", true)
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2, "Tap to Start", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2, "Tap to Start", true)
elseif state == STATE_PLAYING or state == STATE_GAME_OVER then elseif state == STATE_PLAYING or state == STATE_GAME_OVER then
-- Draw asteroids (convert to integers) -- Draw asteroids (convert to integers)
@@ -134,12 +134,12 @@ function draw()
end end
-- Draw score -- Draw score
renderer.text_scaled(10, 10, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(10, 10, "Score: " .. tostring(game.vars.score, 2), true)
renderer.text_scaled(10, 20, "Level: " .. tostring(game.vars.level), true, 2) renderer.text_scaled(10, 20, "Level: " .. tostring(game.vars.level, 2), true)
if state == STATE_GAME_OVER then if state == STATE_GAME_OVER then
renderer.text_scaled(game.width() / 2 - 40, game.height() / 2, "GAME OVER", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 40, game.height() / 2, "GAME OVER", true)
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 + 20, "Tap to Menu", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2 + 20, "Tap to Menu", true)
end end
end end
end end
+1 -1
View File
@@ -73,7 +73,7 @@ function draw()
if game.vars.state == STATE_PAUSED then if game.vars.state == STATE_PAUSED then
renderer.text_scaled(10, 10, "PAUSED - Tap to start", true, 2) renderer.text_scaled(10, 10, "PAUSED - Tap to start", true, 2)
else else
renderer.text_scaled(10, 10, "Frames: " .. tostring(game.vars.frame_count), true, 2) renderer.text_scaled(10, 10, "Frames: " .. tostring(game.vars.frame_count, 2), true)
renderer.text_scaled(10, 25, "Tap to pause", true, 2) renderer.text_scaled(10, 25, "Tap to pause", true, 2)
end end
+10 -10
View File
@@ -92,8 +92,8 @@ function draw()
local state = game.vars.state local state = game.vars.state
if state == STATE_MENU then if state == STATE_MENU then
renderer.text_scaled(game.width() / 2 - 30, game.height() / 2 - 30, "BREAKOUT", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 30, game.height() / 2 - 30, "BREAKOUT", true)
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2, "Tap to Start", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2, "Tap to Start", true)
elseif state == STATE_PLAYING then elseif state == STATE_PLAYING then
-- Draw bricks -- Draw bricks
@@ -115,18 +115,18 @@ function draw()
renderer.circle(math.floor(game.vars.ball_x + 0.5), math.floor(game.vars.ball_y + 0.5), BALL_RADIUS, true, true) renderer.circle(math.floor(game.vars.ball_x + 0.5), math.floor(game.vars.ball_y + 0.5), BALL_RADIUS, true, true)
-- Draw score and lives -- Draw score and lives
renderer.text_scaled(5, 5, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(5, 5, "Score: " .. tostring(game.vars.score, 2), true)
renderer.text_scaled(game.width() - 50, 5, "Lives: " .. tostring(game.vars.lives), true, 2) renderer.text_scaled(game.width(, 2) - 50, 5, "Lives: " .. tostring(game.vars.lives), true)
elseif state == STATE_GAME_OVER then elseif state == STATE_GAME_OVER then
renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 20, "GAME OVER", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 40, game.height() / 2 - 20, "GAME OVER", true)
renderer.text_scaled(game.width() / 2 - 30, game.height() / 2, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(game.width(, 2) / 2 - 30, game.height() / 2, "Score: " .. tostring(game.vars.score), true)
renderer.text_scaled(game.width() / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true)
elseif state == STATE_LEVEL_COMPLETE then elseif state == STATE_LEVEL_COMPLETE then
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 - 20, "LEVEL COMPLETE!", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2 - 20, "LEVEL COMPLETE!", true)
renderer.text_scaled(game.width() / 2 - 30, game.height() / 2, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(game.width(, 2) / 2 - 30, game.height() / 2, "Score: " .. tostring(game.vars.score), true)
renderer.text_scaled(game.width() / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true)
end end
end end
+2 -2
View File
@@ -33,12 +33,12 @@ function draw()
-- Draw count (centered) -- Draw count (centered)
local count_text = "Count: " .. tostring(game.vars.count) local count_text = "Count: " .. tostring(game.vars.count)
renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 10, count_text, true, 2) renderer.text_scaled(game.width(, 2) / 2 - 40, game.height() / 2 - 10, count_text, true)
-- Draw last touch position -- Draw last touch position
if game.vars.count > 0 then if game.vars.count > 0 then
local pos_text = "Last: (" .. tostring(game.vars.last_x) .. ", " .. tostring(game.vars.last_y) .. ")" local pos_text = "Last: (" .. tostring(game.vars.last_x) .. ", " .. tostring(game.vars.last_y) .. ")"
renderer.text_scaled(20, game.height() - 30, pos_text, true, 2) renderer.text_scaled(20, game.height(, 2) - 30, pos_text, true)
-- Draw marker at last touch (convert to integers) -- Draw marker at last touch (convert to integers)
renderer.circle(math.floor(game.vars.last_x + 0.5), math.floor(game.vars.last_y + 0.5), 5, true, false) renderer.circle(math.floor(game.vars.last_x + 0.5), math.floor(game.vars.last_y + 0.5), 5, true, false)
+6 -6
View File
@@ -74,8 +74,8 @@ function draw()
local state = game.vars.state local state = game.vars.state
if state == STATE_MENU then if state == STATE_MENU then
renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 30, "FLAPPY BIRD", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 40, game.height() / 2 - 30, "FLAPPY BIRD", true)
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2, "Tap to Start", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2, "Tap to Start", true)
elseif state == STATE_PLAYING then elseif state == STATE_PLAYING then
-- Draw bird (convert to integer) -- Draw bird (convert to integer)
@@ -94,12 +94,12 @@ function draw()
end end
-- Draw score -- Draw score
renderer.text_scaled(10, 10, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(10, 10, "Score: " .. tostring(game.vars.score, 2), true)
elseif state == STATE_GAME_OVER then elseif state == STATE_GAME_OVER then
renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 30, "GAME OVER", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 40, game.height() / 2 - 30, "GAME OVER", true)
renderer.text_scaled(game.width() / 2 - 30, game.height() / 2, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(game.width(, 2) / 2 - 30, game.height() / 2, "Score: " .. tostring(game.vars.score), true)
renderer.text_scaled(game.width() / 2 - 60, game.height() / 2 + 20, "Tap to Restart", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 60, game.height() / 2 + 20, "Tap to Restart", true)
end end
end end
+10 -10
View File
@@ -81,9 +81,9 @@ function draw()
local state = game.vars.state local state = game.vars.state
if state == STATE_MENU then if state == STATE_MENU then
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 - 30, "LUNAR LANDER", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2 - 30, "LUNAR LANDER", true)
renderer.text_scaled(game.width() / 2 - 70, game.height() / 2 - 5, "Land in the zone safely", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 70, game.height() / 2 - 5, "Land in the zone safely", true)
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 + 20, "Tap to Start", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2 + 20, "Tap to Start", true)
elseif state == STATE_PLAYING or state == STATE_LANDED or state == STATE_CRASHED then elseif state == STATE_PLAYING or state == STATE_LANDED or state == STATE_CRASHED then
-- Draw terrain -- Draw terrain
@@ -111,18 +111,18 @@ function draw()
end end
-- Draw stats -- Draw stats
renderer.text_scaled(5, 5, "Fuel: " .. tostring(math.floor(game.vars.fuel)), true, 2) renderer.text_scaled(5, 5, "Fuel: " .. tostring(math.floor(game.vars.fuel, 2)), true)
renderer.text_scaled(5, 15, "Speed: " .. tostring(math.floor(game.vars.lander_vel_y * 10)), true, 2) renderer.text_scaled(5, 15, "Speed: " .. tostring(math.floor(game.vars.lander_vel_y * 10, 2)), true)
if state == STATE_LANDED then if state == STATE_LANDED then
renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 20, "LANDED!", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 40, game.height() / 2 - 20, "LANDED!", true)
renderer.text_scaled(game.width() / 2 - 30, game.height() / 2, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(game.width(, 2) / 2 - 30, game.height() / 2, "Score: " .. tostring(game.vars.score), true)
renderer.text_scaled(game.width() / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true)
end end
if state == STATE_CRASHED then if state == STATE_CRASHED then
renderer.text_scaled(game.width() / 2 - 40, game.height() / 2 - 20, "CRASHED!", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 40, game.height() / 2 - 20, "CRASHED!", true)
renderer.text_scaled(game.width() / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 60, game.height() / 2 + 20, "Tap to Menu", true)
end end
end end
end end
+6 -6
View File
@@ -177,8 +177,8 @@ function draw()
local state = game.vars.state local state = game.vars.state
if state == STATE_MENU then if state == STATE_MENU then
renderer.text_scaled(game.width() / 2 - 20, game.height() / 2 - 30, "TETRIS", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 20, game.height() / 2 - 30, "TETRIS", true)
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2, "Tap to Start", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2, "Tap to Start", true)
elseif state == STATE_PLAYING or state == STATE_GAME_OVER then elseif state == STATE_PLAYING or state == STATE_GAME_OVER then
-- Draw grid -- Draw grid
@@ -203,12 +203,12 @@ function draw()
end end
-- Draw score -- Draw score
renderer.text_scaled(game.width() - 50, 10, "Score: " .. tostring(game.vars.score), true, 2) renderer.text_scaled(game.width(, 2) - 50, 10, "Score: " .. tostring(game.vars.score), true)
renderer.text_scaled(game.width() - 50, 20, "Lines: " .. tostring(game.vars.lines), true, 2) renderer.text_scaled(game.width(, 2) - 50, 20, "Lines: " .. tostring(game.vars.lines), true)
if state == STATE_GAME_OVER then if state == STATE_GAME_OVER then
renderer.text_scaled(game.width() / 2 - 40, game.height() / 2, "GAME OVER", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 40, game.height() / 2, "GAME OVER", true)
renderer.text_scaled(game.width() / 2 - 50, game.height() / 2 + 20, "Tap to Menu", true, 2) renderer.text_scaled(game.width(, 2) / 2 - 50, game.height() / 2 + 20, "Tap to Menu", true)
end end
end end
end end