diff options
Diffstat (limited to 'src/games/snake')
| -rw-r--r-- | src/games/snake/Snake.cpp | 58 | ||||
| -rw-r--r-- | src/games/snake/Snake.hpp | 13 |
2 files changed, 67 insertions, 4 deletions
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; }; |
