データの保存

守矢諏訪子 <suwako@076.moe>、2026-07-18 00:59
コミット dcd5d36dd480d7d26eed54d47c5a4bc17ee8c2de ・親 bb34bef ・ファイルを見る

9個のファイル、+286 -9

@@ -46,6 +46,11 @@
4646 void DrawUpdate();
4747 void Close();
4848
49+ public:
50+ u32 GetScore() const { return m_Score; }
51+ u16 GetLevel() const { return m_Level; }
52+ u16 GetLines() const { return m_Lines; }
53+
4954 private:
5055 void spawn();
5156 void lock();
@@ -73,9 +78,9 @@
7378 private:
7479 Game *m_Game = nullptr;
7580
76- u8 m_Score = 0;
77- u8 m_Level = 1;
78- u8 m_Lines = 0;
81+ u32 m_Score = 0;
82+ u16 m_Level = 1;
83+ u16 m_Lines = 0;
7984 u8 m_Gravity = 30;
8085
8186 u8 m_HSize = 10;
@@ -0,0 +1,50 @@
1+/**
2+Copyright (c) テクニカル諏訪子
3+
4+Permission is hereby granted to any person obtaining a copy of the software
5+Tetris (the "Software") to use, modify, merge, copy, publish, distribute,
6+sublicense, and/or sell copies of the Software, subject to the following conditions:
7+
8+ 1. **Origin Attribution**:
9+ - You must not misrepresent the origin of the Software; you must not claim
10+ you created the original Software.
11+ - If the Software is used in a product, you must either:
12+ a. Provide clear attribution in the product's documentation, user interface,
13+ or other visible areas, **OR**
14+ b. Pay the original developers a fee they specify in writing.
15+ 2. **Usage Restriction**:
16+ - The Software, or any derivative works, dependencies, or libraries
17+ incorporating it, must not be used for censorship or to suppress freedom of
18+ speech, expression, or creativity. Prohibited uses include, but are not
19+ limited to:
20+ - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
21+ ideas, or objective reality.
22+ - Tools or systems designed to restrict access to information or
23+ artistic works.
24+ 3. **Notice Preservation**:
25+ - This license and the above copyright notice must remain intact in all copies
26+ of the source code.
27+
28+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
29+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
30+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32+CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
33+OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34+*/
35+#pragma once
36+
37+#include <util/types.hh>
38+
39+namespace tetris::sys {
40+ struct SaveData {
41+ cstr magic = "SAV";
42+ const u8 Version = 1;
43+ u32 highScore = 0;
44+ u16 mostLines = 0;
45+ u16 bestLevel = 0;
46+ }; // struct SaveData
47+
48+ SaveData GetScore();
49+ bool SaveScore(u32 score, u16 lines, u16 level);
50+} // namespace tetris::sys
@@ -34,6 +34,8 @@
3434 */
3535 #pragma once
3636
37+#include <util/types.hh>
38+
3739 namespace tetris {
3840 enum class Scene {
3941 Title,
@@ -52,6 +54,11 @@
5254 void Close();
5355 void SetNextScene(Scene scene);
5456 Scene GetCurrentScene();
57+
58+ public:
59+ u32 GetScore();
60+ u16 GetLines();
61+ u16 GetLevel();
5562
5663 private:
5764 Scene m_CurScene = Scene::Title;
@@ -0,0 +1,32 @@
1+/*
2+# 076 Free License
3+
4+Copyright (c) テクニカル諏訪子
5+
6+Permission is hereby granted to any person obtaining a copy of the software
7+C++ Utils (the "Software") to use, modify, merge, copy, publish, distribute,
8+sublicense, and/or sell copies of the Software, subject to the following conditions:
9+
10+ 1. **Origin Attribution**:
11+ - You must not misrepresent the origin of the Software; you must not claim
12+ you created the original Software.
13+ 2. **Notice Preservation**:
14+ - This license and the above copyright notice must remain intact in all copies
15+ of the source code.
16+
17+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21+CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22+OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+*/
24+#ifndef FILE_HH
25+#define FILE_HH
26+
27+#include <util/types.hh>
28+
29+vector<u8> read_file(const string &filename, bool binary = false);
30+bool write_file(const string &filename, vector<string> contents, bool binary = false);
31+
32+#endif // FILE_HH
@@ -90,6 +90,10 @@
9090 std::vector<std::vector<i32>> board = {};
9191
9292 void GameMain::Init(Game *game) {
93+ m_Score = 0;
94+ m_Level = 1;
95+ m_Lines = 0;
96+
9397 m_Game = game;
9498 boardPos = {
9599 .x = (GetScreenWidth() - m_HSize * m_SquareSize) / 2.f - 150,
@@ -423,9 +427,6 @@
423427 m_FallSpeed = .8f;
424428 m_GameOver = false;
425429 m_CanHold = true;
426- m_Score = 0;
427- m_Level = 1;
428- m_Lines = 0;
429430 m_Gravity = 30;
430431 m_HSize = 10;
431432 m_VSize = 20;
@@ -36,6 +36,7 @@
3636 #include <tetris/sys/imgman.hh>
3737 #include <tetris/sys/assman.hh>
3838 #include <tetris/sys/sndman.hh>
39+#include <tetris/sys/save.hh>
3940 #include <raylib.h>
4041
4142 #include <tetris/ass/bgm_gameover.hh>
@@ -62,12 +63,16 @@
6263 snd.LoadSfx("sfx_gameover", ass_gameover_sfx_ogg, ass_gameover_sfx_ogg_len);
6364
6465 sys::SndMan::Get().PlaySfx("sfx_gameover");
66+
67+ sys::SaveData saveData = sys::GetScore();
68+ m_CurScore = game->GetScore();
69+ m_HighScore = saveData.highScore;
70+ sys::SaveScore(game->GetScore(), game->GetLines(), game->GetLevel());
6571 }
6672
6773 void GameOver::FuncUpdate() {
6874 m_InputTimer += GetFrameTime();
6975 #ifndef NDEBUG
70- std::cout << "Timer: " << m_InputTimer << std::endl;
7176 DrawText(TextFormat("Timer: %.2f", m_InputTimer), 10, 10, 20, MAGENTA);
7277 #endif
7378 if (m_InputTimer > MinTimePassed) handleInput();
@@ -79,8 +84,15 @@
7984
8085 Font font = GetFontDefault();
8186
82- int textWidth = MeasureText("GAME OVER", 36.f);
83- DrawText("GAME OVER", GetScreenWidth() / 2 - textWidth / 2, GetScreenHeight() / 2 - 100.f, 36.f, RED);
87+ string score = "SCORE: " + std::to_string(m_CurScore);
88+ string hScore = "HIGH SCORE: " + std::to_string(m_HighScore);
89+ int textWidthGO = MeasureText("GAME OVER", 36.f);
90+ int textWidthSC = MeasureText(score.c_str(), 24.f);
91+ int textWidthHS = MeasureText(hScore.c_str(), 24.f);
92+
93+ DrawText("GAME OVER", GetScreenWidth() / 2 - textWidthGO / 2, GetScreenHeight() / 2 - 300.f + 72.f, 36.f, RED);
94+ DrawText(score.c_str(), GetScreenWidth() / 2 - textWidthSC / 2, GetScreenHeight() / 2 - 300.f + 128.f, 24.f, RED);
95+ DrawText(hScore.c_str(), GetScreenWidth() / 2 - textWidthHS / 2, GetScreenHeight() / 2 - 300.f + 160.f, 24.f, RED);
8496
8597 Vector2 menuPos = {
8698 GetScreenWidth() / 2.f,
@@ -0,0 +1,83 @@
1+/**
2+Copyright (c) テクニカル諏訪子
3+
4+Permission is hereby granted to any person obtaining a copy of the software
5+Tetris (the "Software") to use, modify, merge, copy, publish, distribute,
6+sublicense, and/or sell copies of the Software, subject to the following conditions:
7+
8+ 1. **Origin Attribution**:
9+ - You must not misrepresent the origin of the Software; you must not claim
10+ you created the original Software.
11+ - If the Software is used in a product, you must either:
12+ a. Provide clear attribution in the product's documentation, user interface,
13+ or other visible areas, **OR**
14+ b. Pay the original developers a fee they specify in writing.
15+ 2. **Usage Restriction**:
16+ - The Software, or any derivative works, dependencies, or libraries
17+ incorporating it, must not be used for censorship or to suppress freedom of
18+ speech, expression, or creativity. Prohibited uses include, but are not
19+ limited to:
20+ - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
21+ ideas, or objective reality.
22+ - Tools or systems designed to restrict access to information or
23+ artistic works.
24+ 3. **Notice Preservation**:
25+ - This license and the above copyright notice must remain intact in all copies
26+ of the source code.
27+
28+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
29+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
30+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32+CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
33+OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34+*/
35+#include <tetris/sys/save.hh>
36+#include <util/file.hh>
37+#include <sstream>
38+#include <iostream>
39+
40+namespace tetris::sys {
41+ SaveData GetScore() {
42+ SaveData data;
43+ vector<u8> fromFile = read_file("tetris.sav", true);
44+ if (fromFile.empty()) return data;
45+
46+ string content(fromFile.begin(), fromFile.end());
47+ vector<string> lines;
48+ std::stringstream ss(content);
49+ string line;
50+ while (getline(ss, line)) {
51+ if (!line.empty() && line.back() == '\r') line.pop_back();
52+ if (!line.empty()) lines.push_back(line);
53+ }
54+
55+ if (lines.size() < 4) return data;
56+ if (lines[0] != string(data.magic)) return data;
57+ data.highScore = stoul(lines[2]);
58+ data.mostLines = static_cast<u16>(stoul(lines[3]));
59+ data.bestLevel = static_cast<u16>(stoul(lines[4]));
60+
61+ return data;
62+ }
63+
64+ bool SaveScore(u32 score, u16 lines, u16 level) {
65+ SaveData data = GetScore();
66+ std::cout << score << " | " << data.highScore << std::endl;
67+ std::cout << lines << " | " << data.mostLines << std::endl;
68+ std::cout << level << " | " << data.bestLevel << std::endl;
69+ if (score > data.highScore) data.highScore = score;
70+ if (lines > data.mostLines) data.mostLines = lines;
71+ if (level > data.bestLevel) data.bestLevel = level;
72+
73+ vector<string> dat{
74+ string(data.magic),
75+ std::to_string(data.Version),
76+ std::to_string(data.highScore),
77+ std::to_string(data.mostLines),
78+ std::to_string(data.bestLevel),
79+ };
80+
81+ return write_file("tetris.sav", dat, true);
82+ }
83+} // namespace tetris::sys
変更 src/tetris.cc
@@ -111,4 +111,16 @@
111111 Scene Game::GetCurrentScene() {
112112 return m_CurScene;
113113 }
114+
115+ u32 Game::GetScore() {
116+ return g_GameMain.GetScore();
117+ }
118+
119+ u16 Game::GetLines() {
120+ return g_GameMain.GetLines();
121+ }
122+
123+ u16 Game::GetLevel() {
124+ return g_GameMain.GetLevel();
125+ }
114126 } // namespace tetris
@@ -0,0 +1,75 @@
1+/*
2+# 076 Free License
3+
4+Copyright (c) テクニカル諏訪子
5+
6+Permission is hereby granted to any person obtaining a copy of the software
7+C++ Utils (the "Software") to use, modify, merge, copy, publish, distribute,
8+sublicense, and/or sell copies of the Software, subject to the following conditions:
9+
10+ 1. **Origin Attribution**:
11+ - You must not misrepresent the origin of the Software; you must not claim
12+ you created the original Software.
13+ 2. **Notice Preservation**:
14+ - This license and the above copyright notice must remain intact in all copies
15+ of the source code.
16+
17+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21+CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22+OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+*/
24+#include <util/file.hh>
25+
26+#include <iostream>
27+#include <fstream>
28+
29+vector<u8> read_file(const string &filename, bool binary) {
30+ std::ios::openmode flags;
31+ if (binary) {
32+ flags = std::ios::binary | std::ios::ate;
33+ } else {
34+ flags = std::ios::in;
35+ }
36+
37+ std::ifstream file(filename, flags);
38+ if (!file) {
39+ return {};
40+ }
41+
42+ const auto size = file.tellg();
43+ file.seekg(0);
44+ vector<u8> data(static_cast<size_t>(size));
45+ if (!file.read(reinterpret_cast<char *>(data.data()), size)) {
46+ return {};
47+ }
48+
49+ file.close();
50+
51+ return data;
52+}
53+
54+bool write_file(const string &filename, vector<string> contents, bool binary) {
55+ std::ios::openmode flags;
56+ if (binary) {
57+ flags = std::ios::binary | std::ios::ate;
58+ } else {
59+ flags = std::ios::out | std::ios::trunc;
60+ }
61+
62+ std::ofstream file(filename, flags);
63+ if (!file) {
64+ return false;
65+ }
66+
67+ for (const auto &c : contents) {
68+ file << c;
69+ file << "\n";
70+ }
71+
72+ file.close();
73+
74+ return true;
75+}