aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfschildt <florian.schildt@protonmail.com>2025-12-13 04:12:10 +0100
committerfschildt <florian.schildt@protonmail.com>2025-12-13 04:12:10 +0100
commitebe4a92027aabc9c01caf0fbe791d22773abfe75 (patch)
treee697282a885f54de7af22f36a58c1a684f67faf0
parentf24d302be51f8cd2f4c5796283ea9f9a3be8d922 (diff)
snake: add score/highscoreHEADmaster
-rw-r--r--runtree/snake_highscore.txt1
-rw-r--r--src/common/MemoryManager.cpp9
-rw-r--r--src/common/MemoryManager.hpp4
-rw-r--r--src/games/snake/Snake.cpp58
-rw-r--r--src/games/snake/Snake.hpp13
-rw-r--r--src/games/tetris/Tetris.cpp10
6 files changed, 81 insertions, 14 deletions
diff --git a/runtree/snake_highscore.txt b/runtree/snake_highscore.txt
new file mode 100644
index 0000000..1e8b314
--- /dev/null
+++ b/runtree/snake_highscore.txt
@@ -0,0 +1 @@
+6
diff --git a/src/common/MemoryManager.cpp b/src/common/MemoryManager.cpp
index 285535b..8c149ff 100644
--- a/src/common/MemoryManager.cpp
+++ b/src/common/MemoryManager.cpp
@@ -2,6 +2,15 @@
std::vector<std::u32string> MemoryManager::s_frame_string32s;
+
+std::u32string
+int32_to_u32string(int32_t value)
+{
+ std::string str = std::to_string(value);
+ return std::u32string(str.begin(), str.end());
+}
+
+
std::u32string&
MemoryManager::GetString32(String32Id id)
{
diff --git a/src/common/MemoryManager.hpp b/src/common/MemoryManager.hpp
index c8c7de0..d8460dc 100644
--- a/src/common/MemoryManager.hpp
+++ b/src/common/MemoryManager.hpp
@@ -8,6 +8,10 @@
using String32Id = uint32_t;
+// temporary helper function
+std::u32string int32_to_u32string(int32_t value);
+
+
class MemoryManager {
public:
static std::u32string& GetString32(String32Id id);
diff --git a/src/games/snake/Snake.cpp b/src/games/snake/Snake.cpp
index b7c9004..de9dad7 100644
--- a/src/games/snake/Snake.cpp
+++ b/src/games/snake/Snake.cpp
@@ -2,18 +2,30 @@
#include "games/Game.hpp"
#include "renderer/Renderer.hpp"
#include "common/defs.hpp"
+#include "common/MemoryManager.hpp"
+#include "common/math.hpp"
#include <cstdint>
#include <imgui.h>
+#include <fstream>
std::mt19937 Snake::s_rng{std::random_device{}()};
Snake::Snake()
+ : m_font {s_dejavu_sans_mono_filepath, 22}
{
static_assert(max_map_width <= sizeof(m_body_bitmap[0])*8);
static_assert(max_map_height <= sizeof(m_body_bitmap[0])*8);
+
+ int32_t highscore = 0;
+ std::ifstream highscore_file_in {"snake_highscore.txt"};
+ if (highscore_file_in) {
+ highscore_file_in >> highscore;
+ highscore_file_in.close();
+ }
+ m_highscore = highscore;
}
void
@@ -42,6 +54,8 @@ Snake::Start()
m_dist = std::uniform_int_distribution<int32_t>(0, m_map_width*m_map_height - 3);
SpawnFood();
+ m_score = 0;
+
m_game_status = game_resume;
}
@@ -76,7 +90,7 @@ Snake::MaybeMoveSnake(float dt)
if ((head_pos.x < 0 || head_pos.x >= m_map_width) ||
(head_pos.y < 0 || head_pos.y >= m_map_height))
{
- m_game_status = game_over;
+ HandleGameOver();
return;
}
uint64_t head_bit = 1 << head_pos.x;
@@ -85,7 +99,7 @@ Snake::MaybeMoveSnake(float dt)
body_bits &= (uint32_t)~(1 << tail_pos.x);
}
if (head_bit & body_bits) {
- m_game_status = game_over;
+ HandleGameOver();
return;
}
@@ -102,6 +116,7 @@ Snake::MaybeMoveSnake(float dt)
if (m_body_positions[m_head] == m_food_position) {
+ m_score += 1;
SpawnFood();
}
else {
@@ -125,6 +140,24 @@ Snake::MaybeMoveSnake(float dt)
}
void
+Snake::HandleGameOver()
+{
+ m_game_status = game_over;
+ if (m_score > m_highscore) {
+ m_highscore = m_score;
+
+ std::ofstream highscore_file_out {highscore_path};
+ if (highscore_file_out) {
+ highscore_file_out << m_highscore << std::endl;
+ highscore_file_out.close();
+ }
+ else {
+ SDL_LogInfo(0, "Snake: cannot open %s for writing", highscore_path);
+ }
+ }
+}
+
+void
Snake::ProcessEvent(SDL_Event& event)
{
switch (event.type) {
@@ -306,5 +339,26 @@ Snake::Draw()
food_y + bodypart_size
};
g_renderer.PushRectangle(rect, color_food, z_food);
+
+
+ /* draw scores */
+ String32Id score_label = MemoryManager::EmplaceString32_Frame(U"Score");
+ String32Id score_value = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_score));
+
+ String32Id highscore_label = MemoryManager::EmplaceString32_Frame(U"Highscore");
+ String32Id highscore_value = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_highscore));
+
+
+ V2F32 score_pos = {1.3f, 2.5f};
+ V2F32 highscore_pos = {2.3f, 2.5f};
+ Color anyscore_color {0.9f, 0.9f, 0.9f, 1.0f};
+
+ g_renderer.PushString32(highscore_label, m_font, highscore_pos, anyscore_color, z_text);
+ highscore_pos.y -= 0.1f;
+ g_renderer.PushString32(highscore_value, m_font, highscore_pos, anyscore_color, z_text);
+
+ g_renderer.PushString32(score_label, m_font, score_pos, anyscore_color, z_text);
+ score_pos.y -= 0.1f;
+ g_renderer.PushString32(score_value, m_font, score_pos, anyscore_color, z_text);
}
diff --git a/src/games/snake/Snake.hpp b/src/games/snake/Snake.hpp
index 81fd30f..0303238 100644
--- a/src/games/snake/Snake.hpp
+++ b/src/games/snake/Snake.hpp
@@ -2,6 +2,7 @@
#include "games/Game.hpp"
#include "common/math.hpp"
+#include "common/Font.hpp"
#include <random>
@@ -29,18 +30,23 @@ private:
void MaybeMoveSnake(float dt_in_seconds);
void SpawnFood();
+ void HandleGameOver();
+
private:
static constexpr int32_t max_map_width = 16;
static constexpr int32_t max_map_height = 16;
static constexpr float tiles_per_second = 4.0f;
+ static constexpr char highscore_path[] = "snake_highscore.txt";
+
+ Font m_font;
static std::mt19937 s_rng;
std::uniform_int_distribution<int32_t> m_dist;
- int32_t m_starting_map_width = 16;
- int32_t m_starting_map_height = 16;
+ int32_t m_starting_map_width = 12;
+ int32_t m_starting_map_height = 12;
Direction m_direction;
Direction m_last_advanced_direction;
@@ -52,6 +58,9 @@ private:
uint64_t m_body_bitmap[max_map_height];
V2I32 m_body_positions[max_map_width * max_map_height];
V2I32 m_food_position;
+
+ int32_t m_score;
+ int32_t m_highscore;
};
diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp
index 0fee80a..7273cdd 100644
--- a/src/games/tetris/Tetris.cpp
+++ b/src/games/tetris/Tetris.cpp
@@ -13,13 +13,6 @@
#include <string>
-static std::u32string
-int32_to_u32string(int32_t value)
-{
- std::string str = std::to_string(value);
- return std::u32string(str.begin(), str.end());
-}
-
Tetris::Tetris()
: m_font{s_dejavu_sans_mono_filepath, 22}
@@ -172,9 +165,6 @@ Tetris::ReadHighscore()
highscore_file_in >> highscore;
highscore_file_in.close();
}
- else {
- SDL_LogInfo(0, "Tetris: cannot open %s for reading", s_tetris_highscore_path);
- }
return highscore;
}