Add textures

cpp
Elnu 3 years ago
parent 797ab34246
commit c2e3cd1679

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

@ -2,7 +2,9 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/System/Clock.hpp> #include <SFML/System/Clock.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp> #include <SFML/Window/Keyboard.hpp>
@ -10,28 +12,52 @@
#include <iterator> #include <iterator>
#include <string> #include <string>
#define WINDOW_WIDTH 280 #define TILE_SIZE 20
#define WINDOW_HEIGHT 400
#define GRID_WIDTH 14 #define GRID_WIDTH 14
#define GRID_HEIGHT 20 #define GRID_HEIGHT 20
#define WINDOW_WIDTH GRID_WIDTH * TILE_SIZE
#define WINDOW_HEIGHT GRID_HEIGHT * TILE_SIZE
class TileType { class TileType {
public: public:
static TileType white, red, green, blue, yellow, magenta, cyan; sf::IntRect texture_rect;
sf::Color color; sf::IntRect ghost_texture_rect;
TileType(sf::Color _color) { TileType(sf::IntRect _texture_rect, sf::IntRect _ghost_texture_rect) {
color = _color; texture_rect = _texture_rect;
ghost_texture_rect = _ghost_texture_rect;
} }
}; };
TileType TileType::white = TileType(sf::Color::White); TileType tile_type_0(
TileType TileType::red = TileType(sf::Color::Red); sf::IntRect(0, 0, TILE_SIZE, TILE_SIZE),
TileType TileType::green = TileType(sf::Color::Green); sf::IntRect(0, TILE_SIZE, TILE_SIZE, TILE_SIZE)
TileType TileType::blue = TileType(sf::Color::Blue); );
TileType TileType::yellow = TileType(sf::Color::Yellow); TileType tile_type_1(
TileType TileType::magenta = TileType(sf::Color::Magenta); sf::IntRect(TILE_SIZE, 0, TILE_SIZE, TILE_SIZE),
TileType TileType::cyan = TileType(sf::Color::Cyan); 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 { class BlockType {
public: public:
@ -51,37 +77,37 @@ class BlockType {
}; };
// https://gamedev.stackexchange.com/a/17978 // https://gamedev.stackexchange.com/a/17978
BlockType BlockType::i(&TileType::white, { BlockType BlockType::i(&tile_type_0, {
{0, 0, 0, 0}, {0, 0, 0, 0},
{1, 1, 1, 1}, {1, 1, 1, 1},
{0, 0, 0, 0}, {0, 0, 0, 0},
{0, 0, 0, 0} {0, 0, 0, 0}
}); });
BlockType BlockType::j(&TileType::red, { BlockType BlockType::j(&tile_type_1, {
{1, 0, 0}, {1, 0, 0},
{1, 1, 1}, {1, 1, 1},
{0, 0, 0} {0, 0, 0}
}); });
BlockType BlockType::l(&TileType::green, { BlockType BlockType::l(&tile_type_2, {
{0, 0, 1}, {0, 0, 1},
{1, 1, 1}, {1, 1, 1},
{0, 0, 0} {0, 0, 0}
}); });
BlockType BlockType::o(&TileType::blue, { BlockType BlockType::o(&tile_type_3, {
{1, 1}, {1, 1},
{1, 1} {1, 1}
}, false); }, false);
BlockType BlockType::s(&TileType::yellow, { BlockType BlockType::s(&tile_type_4, {
{0, 1, 1}, {0, 1, 1},
{1, 1, 0}, {1, 1, 0},
{0, 0, 0} {0, 0, 0}
}); });
BlockType BlockType::t(&TileType::magenta, { BlockType BlockType::t(&tile_type_5, {
{0, 1, 0}, {0, 1, 0},
{1, 1, 1}, {1, 1, 1},
{0, 0, 0} {0, 0, 0}
}); });
BlockType BlockType::z(&TileType::cyan, { BlockType BlockType::z(&tile_type_6, {
{1, 1, 0}, {1, 1, 0},
{0, 1, 1}, {0, 1, 1},
{0, 0, 0} {0, 0, 0}
@ -145,7 +171,6 @@ class Block {
int main() int main()
{ {
srand(time(NULL)); srand(time(NULL));
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "elnutris"); sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "elnutris");
window.setFramerateLimit(60); window.setFramerateLimit(60);
@ -154,9 +179,10 @@ int main()
TileType* grid[GRID_HEIGHT][GRID_WIDTH] = { nullptr }; TileType* grid[GRID_HEIGHT][GRID_WIDTH] = { nullptr };
int shape_width = WINDOW_WIDTH / GRID_WIDTH; sf::Texture texture;
int shape_height = WINDOW_HEIGHT / GRID_HEIGHT; texture.loadFromFile("../res/texture.png");
sf::RectangleShape shape(sf::Vector2f(shape_width, shape_height)); sf::Sprite sprite;
sprite.setTexture(texture);
bool snap, rotate, move_left, move_right; bool snap, rotate, move_left, move_right;
sf::Clock update_clock; sf::Clock update_clock;
@ -176,6 +202,8 @@ int main()
int update_interval = 250; int update_interval = 250;
auto clear_color = sf::Color(73, 52, 61);
while (window.isOpen()) while (window.isOpen())
{ {
sf::Event event; sf::Event event;
@ -280,18 +308,16 @@ int main()
} }
// Draw block // Draw block
window.clear(); window.clear(clear_color);
if (!landed) { if (!landed) {
sf::Color ghost_color = block.type->tile_type->color;
ghost_color.a = 64;
for (auto tile : block.get_tiles()) { for (auto tile : block.get_tiles()) {
int snap_y = tile.y + snap_offset; int snap_y = tile.y + snap_offset;
shape.setFillColor(block.type->tile_type->color); sprite.setTextureRect(block.type->tile_type->texture_rect);
shape.setPosition(tile.x * shape_width, tile.y * shape_height); sprite.setPosition(tile.x * TILE_SIZE, tile.y * TILE_SIZE);
window.draw(shape); window.draw(sprite);
shape.setFillColor(ghost_color); sprite.setTextureRect(block.type->tile_type->ghost_texture_rect);
shape.setPosition(tile.x * shape_width, snap_y * shape_height); sprite.setPosition(tile.x * TILE_SIZE, snap_y * TILE_SIZE);
window.draw(shape); window.draw(sprite);
} }
} }
@ -345,9 +371,9 @@ int main()
// If tile_type is a nullptr (no block), continue // If tile_type is a nullptr (no block), continue
continue; continue;
} }
shape.setFillColor(tile_type->color); sprite.setTextureRect(tile_type->texture_rect);
shape.setPosition(x * shape_width, y * shape_height); sprite.setPosition(x * TILE_SIZE, y * TILE_SIZE);
window.draw(shape); window.draw(sprite);
} }
} }
window.draw(text); window.draw(text);

Loading…
Cancel
Save