テキスチャー
守矢諏訪子 <suwako@076.moe>、2026-07-11 18:45
コミット 726d38f62c28ffffa98339ce97a6f06bc579c724
・親 3db2ba1
・ファイルを見る
5個のファイル、+118 -11
バイナリファイル
| @@ -0,0 +1,26 @@ | ||
| 1 | +unsigned char __tetris_block_png[] = { | |
| 2 | + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 3 | + 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, | |
| 4 | + 0x08, 0x03, 0x00, 0x00, 0x00, 0x44, 0xa4, 0x8a, 0xc6, 0x00, 0x00, 0x00, | |
| 5 | + 0x24, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0x1b, 0x1d, 0x1e, 0x27, | |
| 6 | + 0x28, 0x20, 0x3e, 0x3d, 0x32, 0x75, 0x71, 0x5e, 0xf8, 0xf8, 0xf2, 0xf9, | |
| 7 | + 0x26, 0x72, 0xfd, 0x97, 0x1f, 0xe6, 0xdb, 0x74, 0x66, 0xd9, 0xef, 0xa6, | |
| 8 | + 0xe2, 0x2e, 0xae, 0x81, 0xff, 0x7f, 0xca, 0x15, 0xbb, 0x00, 0x00, 0x00, | |
| 9 | + 0x0c, 0x74, 0x52, 0x4e, 0x53, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
| 10 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0x09, 0xc4, 0x50, 0x26, 0x00, 0x00, 0x00, | |
| 11 | + 0x88, 0x49, 0x44, 0x41, 0x54, 0x38, 0x8d, 0x95, 0xcd, 0x5b, 0x0e, 0xc0, | |
| 12 | + 0x30, 0x08, 0x03, 0x41, 0x43, 0x9a, 0xfb, 0x5f, 0xb9, 0x4a, 0xc9, 0x03, | |
| 13 | + 0x08, 0x20, 0xd5, 0xbf, 0x3b, 0x92, 0xc1, 0x4f, 0x39, 0x42, 0xa7, 0xb2, | |
| 14 | + 0x37, 0xf4, 0x4a, 0x50, 0x1b, 0x20, 0x17, 0xd4, 0x04, 0x64, 0x62, 0x74, | |
| 15 | + 0x01, 0xb1, 0xf8, 0xfa, 0x04, 0x91, 0x90, 0xbe, 0xc0, 0x2d, 0x66, 0xdf, | |
| 16 | + 0xc0, 0x8b, 0xd5, 0x0f, 0xb0, 0x62, 0x77, 0x05, 0xb4, 0x38, 0x5d, 0x83, | |
| 17 | + 0x23, 0x54, 0x37, 0x60, 0x09, 0xdd, 0x2d, 0x10, 0x61, 0xba, 0x03, 0x43, | |
| 18 | + 0xd8, 0xee, 0x41, 0x27, 0xd7, 0x7f, 0x03, 0x62, 0x2e, 0x2f, 0x88, 0xd9, | |
| 19 | + 0x0b, 0x5c, 0xdd, 0x09, 0xdc, 0xdd, 0x0a, 0x04, 0xdd, 0x08, 0x44, 0x5d, | |
| 20 | + 0x0b, 0x84, 0x5d, 0x09, 0xc4, 0xfd, 0x08, 0x24, 0x7d, 0x0b, 0x64, 0x7d, | |
| 21 | + 0x09, 0xa4, 0x7d, 0x0a, 0xe4, 0x5d, 0x04, 0x8a, 0xfe, 0x09, 0x54, 0x7d, | |
| 22 | + 0x08, 0x94, 0x9d, 0x99, 0x5e, 0x52, 0xa1, 0x0d, 0x62, 0x4e, 0x46, 0x2d, | |
| 23 | + 0xb4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, | |
| 24 | + 0x82 | |
| 25 | +}; | |
| 26 | +unsigned int __tetris_block_png_len = 265; |
| @@ -0,0 +1,20 @@ | ||
| 1 | +#pragma once | |
| 2 | + | |
| 3 | +#include <raylib.h> | |
| 4 | +#include <unordered_map> | |
| 5 | +#include <util/types.hh> | |
| 6 | + | |
| 7 | +namespace tetris::sys { | |
| 8 | + struct ImgMan { | |
| 9 | + public: | |
| 10 | + static ImgMan &Get(); | |
| 11 | + | |
| 12 | + Texture2D LoadTexture(const string &name, const u8 *data, i32 dataSize); | |
| 13 | + Texture2D GetTexture(const string &name); | |
| 14 | + void UnloadAll(); | |
| 15 | + | |
| 16 | + private: | |
| 17 | + ImgMan() = default; | |
| 18 | + std::unordered_map<string, Texture2D> m_Textures; | |
| 19 | + }; // struct ImgMan | |
| 20 | +} // namespace tetris::sys |
| @@ -1,9 +1,12 @@ | ||
| 1 | 1 | #include <tetris/scene/gamemain.hh> |
| 2 | +#include <tetris/sys/imgman.hh> | |
| 2 | 3 | #include <raylib.h> |
| 3 | 4 | #include <vector> |
| 4 | 5 | #include <cmath> |
| 5 | 6 | #include <algorithm> |
| 6 | 7 | |
| 8 | +#include <tetris/ass/tex_block.hh> | |
| 9 | + | |
| 7 | 10 | namespace tetris::scene { |
| 8 | 11 | Color tetrominoColors[7] = { |
| 9 | 12 | BLUE, |
| @@ -34,6 +37,7 @@ | ||
| 34 | 37 | }; |
| 35 | 38 | |
| 36 | 39 | Vector2 boardPos = { 0, 0 }; |
| 40 | + Texture2D blockTexture; | |
| 37 | 41 | |
| 38 | 42 | i8 nextPiece = 0; |
| 39 | 43 | Vector2 nextPos = { 0, 0 }; |
| @@ -51,6 +55,9 @@ | ||
| 51 | 55 | }; |
| 52 | 56 | |
| 53 | 57 | board.assign(m_VSize, std::vector<i32>(m_HSize, 0)); |
| 58 | + | |
| 59 | + blockTexture = sys::ImgMan::Get().LoadTexture("block", __tetris_block_png, __tetris_block_png_len); | |
| 60 | + | |
| 54 | 61 | nextPiece = GetRandomValue(0, 6); |
| 55 | 62 | spawn(); |
| 56 | 63 | updateBoardLayout(); |
| @@ -97,12 +104,19 @@ | ||
| 97 | 104 | // グリッド |
| 98 | 105 | for (u8 j = 0; j < m_VSize; ++j) { |
| 99 | 106 | for (u8 i = 0; i < m_HSize; ++i) { |
| 107 | + Color bCol = tetrominoColors[(board[j][i] - 1) % 7]; | |
| 108 | + Rectangle rect = { | |
| 109 | + .x = boardPos.x + i * (f32)m_SquareSize, | |
| 110 | + .y = boardPos.y + j * (f32)m_SquareSize, | |
| 111 | + .width = (f32)m_SquareSize, | |
| 112 | + .height = (f32)m_SquareSize, | |
| 113 | + }; | |
| 114 | + | |
| 100 | 115 | if (board[j][i] > 0) { |
| 101 | - Color bCol = tetrominoColors[(board[j][i] - 1) % 7]; | |
| 102 | - DrawRectangle(boardPos.x + i * m_SquareSize, boardPos.y + j * m_SquareSize, m_SquareSize, m_SquareSize, bCol); | |
| 103 | - DrawRectangleLines(boardPos.x + i * m_SquareSize, boardPos.y + j * m_SquareSize, m_SquareSize, m_SquareSize, DARKGRAY); | |
| 116 | + DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, bCol); | |
| 117 | + DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, DARKGRAY); | |
| 104 | 118 | } else { |
| 105 | - DrawRectangleLines(boardPos.x + i * m_SquareSize, boardPos.y + j * m_SquareSize, m_SquareSize, m_SquareSize, Fade(GRAY, .3f)); | |
| 119 | + DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, Fade(GRAY, .3f)); | |
| 106 | 120 | } |
| 107 | 121 | } |
| 108 | 122 | } |
| @@ -114,11 +128,15 @@ | ||
| 114 | 128 | i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1]; |
| 115 | 129 | |
| 116 | 130 | if (py >= 0 && px >= 0 && px < m_HSize && py < m_VSize) { |
| 117 | - f32 x = boardPos.x + px * m_SquareSize; | |
| 118 | - f32 y = boardPos.y + py * m_SquareSize; | |
| 131 | + Rectangle rect = { | |
| 132 | + .x = boardPos.x + px * (f32)m_SquareSize, | |
| 133 | + .y = boardPos.y + py * (f32)m_SquareSize, | |
| 134 | + .width = (f32)m_SquareSize, | |
| 135 | + .height = (f32)m_SquareSize, | |
| 136 | + }; | |
| 119 | 137 | |
| 120 | - DrawRectangle(x, y, m_SquareSize, m_SquareSize, pieceCol); | |
| 121 | - DrawRectangleLines(x, y, m_SquareSize, m_SquareSize, BLACK); | |
| 138 | + DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, pieceCol); | |
| 139 | + DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, BLACK); | |
| 122 | 140 | } |
| 123 | 141 | } |
| 124 | 142 | |
| @@ -164,7 +182,7 @@ | ||
| 164 | 182 | } |
| 165 | 183 | |
| 166 | 184 | void GameMain::Close() { |
| 167 | - | |
| 185 | + sys::ImgMan::Get().UnloadAll(); | |
| 168 | 186 | } |
| 169 | 187 | |
| 170 | 188 | ////////// |
| @@ -226,7 +244,9 @@ | ||
| 226 | 244 | for (int i = 0; i < 4; ++i) { |
| 227 | 245 | float x = nextPos.x + tetrominoShapes[nextPiece][0][i][0] * nextSquareSize; |
| 228 | 246 | float y = nextPos.y + tetrominoShapes[nextPiece][0][i][1] * nextSquareSize; |
| 229 | - DrawRectangle(x, y, nextSquareSize, nextSquareSize, col); | |
| 247 | + | |
| 248 | + Rectangle rect = { x, y, nextSquareSize, nextSquareSize }; | |
| 249 | + DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, col); | |
| 230 | 250 | DrawRectangleLines(x, y, nextSquareSize, nextSquareSize, BLACK); |
| 231 | 251 | } |
| 232 | 252 | } |
| @@ -239,7 +259,9 @@ | ||
| 239 | 259 | for (int i = 0; i < 4; ++i) { |
| 240 | 260 | float x = holdPos.x + tetrominoShapes[m_Hold][0][i][0] * holdSquareSize; |
| 241 | 261 | float y = holdPos.y + tetrominoShapes[m_Hold][0][i][1] * holdSquareSize; |
| 242 | - DrawRectangle(x, y, holdSquareSize, holdSquareSize, col); | |
| 262 | + Rectangle rect = { x, y, holdSquareSize, holdSquareSize }; | |
| 263 | + | |
| 264 | + DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, col); | |
| 243 | 265 | DrawRectangleLines(x, y, holdSquareSize, holdSquareSize, BLACK); |
| 244 | 266 | } |
| 245 | 267 | } |
| @@ -0,0 +1,39 @@ | ||
| 1 | +#include <tetris/sys/imgman.hh> | |
| 2 | + | |
| 3 | +namespace tetris::sys { | |
| 4 | + ImgMan &ImgMan::Get() { | |
| 5 | + static ImgMan instance; | |
| 6 | + return instance; | |
| 7 | + } | |
| 8 | + | |
| 9 | + Texture2D ImgMan::LoadTexture(const string &name, const u8 *data, i32 dataSize) { | |
| 10 | + if (m_Textures.contains(name)) { | |
| 11 | + return m_Textures[name]; | |
| 12 | + } | |
| 13 | + | |
| 14 | + Image img = LoadImageFromMemory(".png", data, dataSize); | |
| 15 | + if (img.data == nullptr) { | |
| 16 | + TraceLog(LOG_WARNING, "テキスチャーの読み込みに失敗: %s", name.c_str()); | |
| 17 | + return Texture2D{}; | |
| 18 | + } | |
| 19 | + | |
| 20 | + Texture2D tx = LoadTextureFromImage(img); | |
| 21 | + UnloadImage(img); | |
| 22 | + | |
| 23 | + SetTextureFilter(tx, TEXTURE_FILTER_BILINEAR); | |
| 24 | + | |
| 25 | + m_Textures[name] = tx; | |
| 26 | + return tx; | |
| 27 | + } | |
| 28 | + | |
| 29 | + Texture2D ImgMan::GetTexture(const string &name) { | |
| 30 | + auto it = m_Textures.find(name); | |
| 31 | + if (it != m_Textures.end()) return it->second; | |
| 32 | + return Texture2D{}; | |
| 33 | + } | |
| 34 | + | |
| 35 | + void ImgMan::UnloadAll() { | |
| 36 | + for (auto &[name, tx] : m_Textures) UnloadTexture(tx); | |
| 37 | + m_Textures.clear(); | |
| 38 | + } | |
| 39 | +} // namespace tetris::sys |