レイアウト

守矢諏訪子 <suwako@076.moe>、2026-07-11 16:11
コミット 3e5068a8c00192673309de5da37b02c77ff66387 ・親 079cc87 ・ファイルを見る

7個のファイル、+293 -0

追加 CREDITS.md
@@ -0,0 +1,10 @@
1+背景アート:
2+https://pixabay.com/users/brzozkamarcin-10835605/
3+
4+BGM:
5+https://x.com/hiiragi_magne
6+https://www.youtube.com/channel/UCk6PbY3vAH9T5wrrAb1mNUQ
7+https://www.youtube.com/channel/UC4u0N-Ein-_9UHkktJ4Uqww
8+
9+SFX:
10+https://pixabay.com/users/phatphrogstudio-54178146/
@@ -0,0 +1,27 @@
1+#pragma once
2+
3+#include <util/types.hh>
4+
5+namespace tetris::scene {
6+ struct GameMain {
7+ public:
8+ void Init();
9+ void FuncUpdate();
10+ void DrawUpdate();
11+ void Close();
12+
13+ private:
14+ void updateBoardLayout();
15+ void drawNextPiece();
16+
17+ private:
18+ u8 m_Score = 0;
19+ u8 m_Level = 1;
20+ u8 m_Lines = 0;
21+ u8 m_Gravity = 30;
22+
23+ u8 m_HSize = 10;
24+ u8 m_VSize = 20;
25+ u8 m_SquareSize = 20;
26+ }; // struct GameMain
27+} // namespace tetris::scene
@@ -0,0 +1,24 @@
1+#pragma once
2+
3+namespace tetris {
4+ enum class Scene {
5+ GameMain,
6+ }; // enum class Scene
7+
8+ struct Game {
9+ public:
10+ bool isRunning;
11+ bool isChangeScene;
12+
13+ void Init();
14+ void FuncUpdate();
15+ void DrawUpdate();
16+ void Close();
17+ void SetNextScene(Scene scene);
18+ Scene GetCurrentScene();
19+
20+ private:
21+ Scene curScene = Scene::GameMain;
22+ Scene nextScene = Scene::GameMain;
23+ }; // struct Game
24+} // namespace tetris
@@ -15,6 +15,9 @@
1515 typedef int32_t i32;
1616 typedef int64_t i64;
1717
18+typedef float f32;
19+typedef double f64;
20+
1821 typedef const char *cstr;
1922 typedef void *any;
2023
変更 main.cc
@@ -1,5 +1,42 @@
1+#include <tetris/tetris.hh>
2+#include <util/types.hh>
3+#include <raylib.h>
14
5+constexpr i32 screenWidth = 900;
6+constexpr i32 screenHeight = 700;
27
38 int main() {
9+#if PRODUCTION_BUILD == 1
10+ SetTraceLogLevel(LOG_NONE);
11+#endif
12+ SetConfigFlags(FLAG_WINDOW_RESIZABLE);
13+ InitWindow(screenWidth, screenHeight, "テトリス");
14+ SetExitKey(KEY_NULL);
15+ SetTargetFPS(120);
16+
17+ tetris::Game game;
18+ game.Init();
19+
20+ while (game.isRunning && !WindowShouldClose()) {
21+ if (IsKeyPressed(KEY_Q)) {
22+ game.isRunning = false;
23+ }
24+
25+ if (game.isChangeScene) {
26+ game.Close();
27+ game.Init();
28+ }
29+
30+ game.FuncUpdate();
31+
32+ BeginDrawing();
33+ ClearBackground(BLACK);
34+ game.DrawUpdate();
35+ EndDrawing();
36+ }
37+
38+ game.Close();
39+ CloseWindow();
40+
441 return 0;
542 }
@@ -0,0 +1,140 @@
1+#include <tetris/scene/gamemain.hh>
2+#include <raylib.h>
3+#include <vector>
4+#include <cmath>
5+#include <algorithm>
6+
7+namespace tetris::scene {
8+ Color tetrominoColors[7] = {
9+ BLUE,
10+ YELLOW,
11+ GREEN,
12+ RED,
13+ PURPLE,
14+ ORANGE,
15+ SKYBLUE,
16+ };
17+
18+ Vector2 boardPos = { 0, 0 };
19+
20+ i8 nextPiece = 0;
21+ Vector2 nextPos = {};
22+ f32 nextSquareSize = 0.f;
23+
24+ std::vector<std::vector<i32>> board = {};
25+
26+ void GameMain::Init() {
27+ boardPos = {
28+ .x = (GetScreenWidth() - m_HSize * m_SquareSize) / 2.f - 150,
29+ .y = (GetScreenHeight() - m_VSize * m_SquareSize) / 2.f,
30+ };
31+
32+ board.assign(m_VSize, std::vector<i32>(m_HSize, 0));
33+ nextPiece = GetRandomValue(0, 6);
34+ updateBoardLayout();
35+ }
36+
37+ void GameMain::FuncUpdate() {
38+ updateBoardLayout();
39+ }
40+
41+ void GameMain::DrawUpdate() {
42+ i32 titleSize = std::max(30, (i32)(m_SquareSize * 1.2f));
43+ DrawText("TETRIS", GetScreenWidth() / 2 - titleSize * 2, 30, titleSize, WHITE);
44+
45+ // Draw the game board border
46+ Rectangle rect = {
47+ .x = boardPos.x - 5,
48+ .y = boardPos.y - 5,
49+ .width = (f32)(m_HSize * m_SquareSize + 10),
50+ .height = (f32)(m_VSize * m_SquareSize + 10),
51+ };
52+ DrawRectangleLinesEx(rect, 5, LIGHTGRAY);
53+
54+ // グリッド
55+ for (u8 j = 0; j < m_VSize; ++j) {
56+ for (u8 i = 0; i < m_HSize; ++i) {
57+ if (board[j][i] > 0) {
58+ Color bCol = tetrominoColors[(board[j][i] - 1) % 7];
59+ DrawRectangle(boardPos.x + i * m_SquareSize, boardPos.y + j * m_SquareSize, m_SquareSize, m_SquareSize, bCol);
60+ DrawRectangleLines(boardPos.x + i * m_SquareSize, boardPos.y + j * m_SquareSize, m_SquareSize, m_SquareSize, DARKGRAY);
61+ } else {
62+ DrawRectangleLines(boardPos.x + i * m_SquareSize, boardPos.y + j * m_SquareSize, m_SquareSize, m_SquareSize, Fade(GRAY, .3f));
63+ }
64+ }
65+ }
66+
67+ Rectangle nextPanel = {
68+ .x = nextPos.x - 10,
69+ .y = nextPos.y - 10,
70+ .width = 4 * nextSquareSize + 20,
71+ .height = 4 * nextSquareSize + 20,
72+ };
73+
74+ // 次のピース
75+ DrawRectangleRec(nextPanel, DARKGRAY);
76+ DrawRectangleLinesEx(nextPanel, 3, LIGHTGRAY);
77+ DrawText("NEXT", nextPos.x + 10, nextPos.y - 30, 20, WHITE);
78+
79+ drawNextPiece();
80+
81+ f32 uiX = nextPos.x;
82+ f32 uiY = nextPos.y + 180;
83+
84+ DrawText(TextFormat("SCORE"), uiX, uiY, 24, WHITE);
85+ DrawText(TextFormat("%08d", m_Score), uiX, uiY + 35, 28, YELLOW);
86+
87+ DrawText(TextFormat("LEVEL"), uiX, uiY + 90, 24, WHITE);
88+ DrawText(TextFormat("%02d", m_Level), uiX, uiY + 125, 32, ORANGE);
89+
90+ DrawText(TextFormat("LINES"), uiX, uiY + 180, 24, WHITE);
91+ DrawText(TextFormat("%04d", m_Lines), uiX, uiY + 215, 28, SKYBLUE);
92+ }
93+
94+ void GameMain::Close() {
95+
96+ }
97+
98+ //////////
99+
100+ void GameMain::updateBoardLayout() {
101+ i32 screenW = GetScreenWidth();
102+ i32 screenH = GetScreenHeight();
103+
104+ f32 maxW = (screenW * .55f) / (f32)m_HSize;
105+ f32 maxH = (screenH * .85f) / (f32)m_VSize;
106+
107+ m_SquareSize = std::min(maxW, maxH);
108+ m_SquareSize = std::clamp((f32)m_SquareSize, 20.f, 60.f);
109+
110+ boardPos.x = (screenW * .5f - m_HSize * m_SquareSize) / 2.f - 40;
111+ boardPos.y = (screenH - m_VSize * m_SquareSize) / 2.f;
112+
113+ nextSquareSize = m_SquareSize * .75f;
114+ nextPos = {
115+ .x = boardPos.x + m_HSize * m_SquareSize + 80,
116+ .y = boardPos.y + 60,
117+ };
118+ }
119+
120+ void GameMain::drawNextPiece() {
121+ const u32 shapes[7][4][2] = {
122+ {{0,1},{1,1},{2,1},{3,1}}, // I
123+ {{0,0},{0,1},{1,0},{1,1}}, // O
124+ {{0,1},{1,1},{2,1},{2,0}}, // S
125+ {{0,0},{1,0},{1,1},{2,1}}, // Z
126+ {{1,0},{0,1},{1,1},{2,1}}, // T
127+ {{0,1},{1,1},{2,1},{2,2}}, // J
128+ {{2,1},{1,1},{0,1},{0,2}} // L
129+ };
130+
131+ Color col = tetrominoColors[nextPiece];
132+
133+ for (int i = 0; i < 4; ++i) {
134+ float x = nextPos.x + shapes[nextPiece][i][0] * nextSquareSize;
135+ float y = nextPos.y + shapes[nextPiece][i][1] * nextSquareSize;
136+ DrawRectangle(x, y, nextSquareSize, nextSquareSize, col);
137+ DrawRectangleLines(x, y, nextSquareSize, nextSquareSize, BLACK);
138+ }
139+ }
140+} // namespace tetris::scene
追加 src/tetris.cc
@@ -0,0 +1,52 @@
1+#include <tetris/tetris.hh>
2+#include <tetris/scene/gamemain.hh>
3+
4+namespace tetris {
5+ scene::GameMain g_GameMain;
6+
7+ void Game::Init() {
8+ isRunning = true;
9+ isChangeScene = false;
10+
11+ switch (nextScene) {
12+ case Scene::GameMain:
13+ g_GameMain.Init();
14+ break;
15+ }
16+ }
17+
18+ void Game::FuncUpdate() {
19+ switch (curScene) {
20+ case Scene::GameMain:
21+ g_GameMain.FuncUpdate();
22+ break;
23+ }
24+ }
25+
26+ void Game::DrawUpdate() {
27+ switch (curScene) {
28+ case Scene::GameMain:
29+ g_GameMain.DrawUpdate();
30+ break;
31+ }
32+ }
33+
34+ void Game::Close() {
35+ switch (curScene) {
36+ case Scene::GameMain:
37+ g_GameMain.Close();
38+ break;
39+ }
40+
41+ curScene = nextScene;
42+ }
43+
44+ void Game::SetNextScene(Scene scene) {
45+ nextScene = scene;
46+ isChangeScene = true;
47+ }
48+
49+ Scene Game::GetCurrentScene() {
50+ return curScene;
51+ }
52+} // namespace tetris