+
Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.
Merged
32 changes: 32 additions & 0 deletions src/button.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <SFML/Graphics.hpp>
#include <memory>

class Button {
public:
Button(std::unique_ptr<sf::Shape> shape) : shape(std::move(shape)) {}

bool is_hovered(sf::RenderWindow& window) const {
return shape->getGlobalBounds().contains(
window.mapPixelToCoords(sf::Mouse::getPosition(window))
);
}

bool is_clicked(sf::RenderWindow& window) const {
return is_hovered(window)
&& sf::Mouse::isButtonPressed(sf::Mouse::Button::Left);
}

void render(sf::RenderWindow& window) const {
if (is_hovered(window)) {
shape->setOutlineThickness(4.0f);
shape->setOutlineColor(sf::Color::Yellow);
} else {
shape->setOutlineThickness(4.0f);
shape->setOutlineColor(sf::Color::Black);
}
window.draw(*shape);
}

private:
std::unique_ptr<sf::Shape> shape;
};
25 changes: 14 additions & 11 deletions src/card.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "card.hpp"

constexpr sf::Vector2i region_size(50, 66);
constexpr sf::Vector2i grid_size(8, 8);
constexpr sf::Vector2i REGION_SIZE(50, 66);
constexpr sf::Vector2i GRID_SIZE(8, 8);
constexpr float CARD_SCALE = 2.0f;

sf::Texture Card::spritesheet_("assets/images/cards.png");
std::vector<sf::Sprite> Card::sprites_;
Expand All @@ -10,27 +11,29 @@ sf::Sprite Card::sprite() const {
if (sprites_.empty()) {
for (int index = 0; index <= 63; index += 1) {
const sf::IntRect region(
{region_size.x * (index % grid_size.x),
region_size.y * (index / grid_size.x)},
region_size
{REGION_SIZE.x * (index % GRID_SIZE.x),
REGION_SIZE.y * (index / GRID_SIZE.x)},
REGION_SIZE
);
sf::Sprite sprite(spritesheet_, region);
sprite.setOrigin(sprite.getGlobalBounds().size / 2.0f);
sprite.setScale(sf::Vector2f(CARD_SCALE, CARD_SCALE));
sprites_.push_back(std::move(sprite));
}
}
return sprites_[atlas_index()];
}

sf::Sprite Card::back_sprite() {
constexpr int index = 55;
constexpr sf::IntRect region(
{region_size.x * (index % grid_size.x),
region_size.y * (index / grid_size.x)},
region_size
constexpr int INDEX = 55;
constexpr sf::IntRect REGION(
{REGION_SIZE.x * (INDEX % GRID_SIZE.x),
REGION_SIZE.y * (INDEX / GRID_SIZE.x)},
REGION_SIZE
);
sf::Sprite sprite(spritesheet_, region);
sf::Sprite sprite(spritesheet_, REGION);
sprite.setOrigin(sprite.getGlobalBounds().size / 2.0f);
sprite.setScale(sf::Vector2f(CARD_SCALE, CARD_SCALE));
return sprite;
}

Expand Down
6 changes: 2 additions & 4 deletions src/deck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Deck {

void render(sf::RenderTarget& render_target) const {
auto sprite = Card::back_sprite();
sprite.setScale({2.0f, 2.0f});
sprite.setPosition(
sf::Vector2f(render_target.getSize()) / 2.0f
- sf::Vector2f(170.0f, 0.0f)
Expand Down Expand Up @@ -67,9 +66,8 @@ class Deck {
}
for (int i = 0; i < 4; i++) {
cards_.push_back(std::make_unique<WildCard>(WildSymbol::Wild));
cards_.push_back(
std::make_unique<WildCard>(WildSymbol::WildDrawFour)
);
cards_.push_back(std::make_unique<WildCard>(WildSymbol::WildDrawFour
));
}
shuffle();
}
Expand Down
1 change: 0 additions & 1 deletion src/discard_pile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class DiscardPile {
std::uniform_real_distribution<float> distrib(-1.0f, 1.0f);
for (size_t i = 0; i < cards_.size(); i += 1) {
auto sprite = cards_[i]->sprite();
sprite.setScale({2.0f, 2.0f});
sprite.setPosition(sf::Vector2f(render_target.getSize()) / 2.0f);

// Generate random offset to make cards look naturally stacked.
Expand Down
16 changes: 9 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#include <SFML/Graphics.hpp>
#include <thread>

#include "state.hpp"

int main() {
auto window = sf::RenderWindow(sf::VideoMode({1536u, 864u}), "UNO");
window.setFramerateLimit(144);

State state;
State state(window);

std::thread thread([&]() {
while (window.isOpen()) {
state.update();
}
});
thread.detach();

while (window.isOpen()) {
while (const auto event = window.pollEvent()) {
Expand All @@ -20,12 +28,6 @@ int main() {
}
}

static sf::Clock clock;
if (clock.getElapsedTime().asSeconds() > 1.5f) {
state.update();
clock.restart();
}

window.clear();
state.render(window);
window.display();
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载