From c2e3cd1679a3b48483a929576fb29aade1bff00b Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sun, 6 Mar 2022 17:03:37 -0800 Subject: [PATCH] Add textures --- res/texture.png | Bin 0 -> 8980 bytes src/Main.cpp | 98 ++++++++++++++++++++++++++++++------------------ 2 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 res/texture.png diff --git a/res/texture.png b/res/texture.png new file mode 100644 index 0000000000000000000000000000000000000000..63079fd9fb985b70a782b3c4050a7c984b783adc GIT binary patch literal 8980 zcmeHtXEdB$)b{AT1c}ZVB}5xDqeh9|MT_W+F_`FP)DXQzi54QFhY-CJLG%*6w;)2a z5Td2>4o_d-`{!Bfd;dK%>%QmQ=j?s$y|2B`S#!=zw62aS1t}9L005v+S5wl*e#T-i zUSd4#ojG~+2mqiy_cJuc=)=8%ZXT|7C?_Nku7(d&=#I1_!d;e4Qe1{ob8_dP%B zbXq>>WygCwVitF`d*hJR;Q`Uh^+Uxo%1YSWOxAZj z<5xg~lR(wgz=vOQ>G+R-c^C@T7{~pr^Z962JM7noC_X7{nfl@=_$||m=l;?hAvAli zfE#K|Y5sFz z`dq*c+xi8U~? zI~?K48f%}{`WP6UvSJp0CO5zEhcKo82wdMCJ26`tI2T3Ve!gdAxA)oi$BWX;1djXA zlj08IdoK-_UzI#AG&xC9_l@QG02A+jVdtA#r7V}fsTPp7@O_9$u55VE za6F!4Eo|W4yJp_zWPN0Ved$zT`*Sbk?t+ub@F{OXEzGsko0hgukac(Z_OZcSaA&b$ z4`{w-FELwBJF{`Bs(RQm`7>OS@J?#-fcu&^F7wdDDIYqdb-ot>Z!zLzDfqla^>}=y zvh`JHmccwnTBrHlyDPr%2QojV>ux7!{aTYeoFpwH{S;dtMVKqCBeuRq>C8gNWZzJK zI4_EDt7SB6x}|eK-%y#VQgl3@@Src&is;Up0O?+TW6uWY`sBK^Ma{~<(v`eSo-6Jh zVmtfzgj|}UP^+7v9DM>E>ZaMx=RQ{Z*HZWI6_6*4M@GH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H literal 0 HcmV?d00001 diff --git a/src/Main.cpp b/src/Main.cpp index a7016c3..41b8bb6 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -2,7 +2,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -10,28 +12,52 @@ #include #include -#define WINDOW_WIDTH 280 -#define WINDOW_HEIGHT 400 +#define TILE_SIZE 20 #define GRID_WIDTH 14 #define GRID_HEIGHT 20 +#define WINDOW_WIDTH GRID_WIDTH * TILE_SIZE +#define WINDOW_HEIGHT GRID_HEIGHT * TILE_SIZE + class TileType { public: - static TileType white, red, green, blue, yellow, magenta, cyan; - sf::Color color; - TileType(sf::Color _color) { - color = _color; + sf::IntRect texture_rect; + sf::IntRect ghost_texture_rect; + TileType(sf::IntRect _texture_rect, sf::IntRect _ghost_texture_rect) { + texture_rect = _texture_rect; + ghost_texture_rect = _ghost_texture_rect; } }; -TileType TileType::white = TileType(sf::Color::White); -TileType TileType::red = TileType(sf::Color::Red); -TileType TileType::green = TileType(sf::Color::Green); -TileType TileType::blue = TileType(sf::Color::Blue); -TileType TileType::yellow = TileType(sf::Color::Yellow); -TileType TileType::magenta = TileType(sf::Color::Magenta); -TileType TileType::cyan = TileType(sf::Color::Cyan); +TileType tile_type_0( + sf::IntRect(0, 0, TILE_SIZE, TILE_SIZE), + sf::IntRect(0, TILE_SIZE, TILE_SIZE, TILE_SIZE) +); +TileType tile_type_1( + sf::IntRect(TILE_SIZE, 0, TILE_SIZE, TILE_SIZE), + sf::IntRect(TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE) +); +TileType tile_type_2( + sf::IntRect(TILE_SIZE * 2, 0, TILE_SIZE, TILE_SIZE), + sf::IntRect(TILE_SIZE * 2, TILE_SIZE, TILE_SIZE, TILE_SIZE) +); +TileType tile_type_3( + sf::IntRect(TILE_SIZE * 3, 0, TILE_SIZE, TILE_SIZE), + sf::IntRect(TILE_SIZE * 3, TILE_SIZE, TILE_SIZE, TILE_SIZE) +); +TileType tile_type_4( + sf::IntRect(TILE_SIZE * 4, 0, TILE_SIZE, TILE_SIZE), + sf::IntRect(TILE_SIZE * 4, TILE_SIZE, TILE_SIZE, TILE_SIZE) +); +TileType tile_type_5( + sf::IntRect(TILE_SIZE * 5, 0, TILE_SIZE, TILE_SIZE), + sf::IntRect(TILE_SIZE * 5, TILE_SIZE, TILE_SIZE, TILE_SIZE) +); +TileType tile_type_6( + sf::IntRect(TILE_SIZE * 6, 0, TILE_SIZE, TILE_SIZE), + sf::IntRect(TILE_SIZE * 6, TILE_SIZE, TILE_SIZE, TILE_SIZE) +); class BlockType { public: @@ -51,37 +77,37 @@ class BlockType { }; // https://gamedev.stackexchange.com/a/17978 -BlockType BlockType::i(&TileType::white, { +BlockType BlockType::i(&tile_type_0, { {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0} }); -BlockType BlockType::j(&TileType::red, { +BlockType BlockType::j(&tile_type_1, { {1, 0, 0}, {1, 1, 1}, {0, 0, 0} }); -BlockType BlockType::l(&TileType::green, { +BlockType BlockType::l(&tile_type_2, { {0, 0, 1}, {1, 1, 1}, {0, 0, 0} }); -BlockType BlockType::o(&TileType::blue, { +BlockType BlockType::o(&tile_type_3, { {1, 1}, {1, 1} }, false); -BlockType BlockType::s(&TileType::yellow, { +BlockType BlockType::s(&tile_type_4, { {0, 1, 1}, {1, 1, 0}, {0, 0, 0} }); -BlockType BlockType::t(&TileType::magenta, { +BlockType BlockType::t(&tile_type_5, { {0, 1, 0}, {1, 1, 1}, {0, 0, 0} }); -BlockType BlockType::z(&TileType::cyan, { +BlockType BlockType::z(&tile_type_6, { {1, 1, 0}, {0, 1, 1}, {0, 0, 0} @@ -145,7 +171,6 @@ class Block { int main() { srand(time(NULL)); - sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "elnutris"); window.setFramerateLimit(60); @@ -154,9 +179,10 @@ int main() TileType* grid[GRID_HEIGHT][GRID_WIDTH] = { nullptr }; - int shape_width = WINDOW_WIDTH / GRID_WIDTH; - int shape_height = WINDOW_HEIGHT / GRID_HEIGHT; - sf::RectangleShape shape(sf::Vector2f(shape_width, shape_height)); + sf::Texture texture; + texture.loadFromFile("../res/texture.png"); + sf::Sprite sprite; + sprite.setTexture(texture); bool snap, rotate, move_left, move_right; sf::Clock update_clock; @@ -176,6 +202,8 @@ int main() int update_interval = 250; + auto clear_color = sf::Color(73, 52, 61); + while (window.isOpen()) { sf::Event event; @@ -280,18 +308,16 @@ int main() } // Draw block - window.clear(); + window.clear(clear_color); if (!landed) { - sf::Color ghost_color = block.type->tile_type->color; - ghost_color.a = 64; for (auto tile : block.get_tiles()) { int snap_y = tile.y + snap_offset; - shape.setFillColor(block.type->tile_type->color); - shape.setPosition(tile.x * shape_width, tile.y * shape_height); - window.draw(shape); - shape.setFillColor(ghost_color); - shape.setPosition(tile.x * shape_width, snap_y * shape_height); - window.draw(shape); + sprite.setTextureRect(block.type->tile_type->texture_rect); + sprite.setPosition(tile.x * TILE_SIZE, tile.y * TILE_SIZE); + window.draw(sprite); + sprite.setTextureRect(block.type->tile_type->ghost_texture_rect); + sprite.setPosition(tile.x * TILE_SIZE, snap_y * TILE_SIZE); + window.draw(sprite); } } @@ -345,9 +371,9 @@ int main() // If tile_type is a nullptr (no block), continue continue; } - shape.setFillColor(tile_type->color); - shape.setPosition(x * shape_width, y * shape_height); - window.draw(shape); + sprite.setTextureRect(tile_type->texture_rect); + sprite.setPosition(x * TILE_SIZE, y * TILE_SIZE); + window.draw(sprite); } } window.draw(text);