SFML - Matriz na tela + Eventos do mouse

LibGdx - Mariz na tela - Pegando posição do mouse.

 Nesse tutorial eu mostro como é possivel desenhar uma matriz m x n na tela e como pegar a posição do mouse.
Veja o código de exemplo utilizado:
java
#include "SFML/Graphics.hpp" #include "SFML/audio.hpp" //Define e cria um retangulo na tela. //Entidade definida para desenhar a matriz na tela. sf::RectangleShape criaRetangulo(int x, int y, int width, int height) { sf::RectangleShape rectangle(sf::Vector2f(width, height)); rectangle.setFillColor(sf::Color::Blue); rectangle.setOutlineColor(sf::Color::Red); rectangle.setOutlineThickness(1.0f); rectangle.setPosition(x, y); return rectangle; } //Definição do que pode ter em cada posição. //É gerada uma matriz de celulas para definir //Qual posição tem água e qual foi clicada. enum Celulas { AGUA, ESTRELA }; //Trata eventos do loop de eventos. void trataEventos(sf::RenderWindow *window, Celulas matriz[10][10], sf::Sound *starSound, int dim) { //Trata as interações. sf::Event event; while ((*window).pollEvent(event)) { if (event.type == sf::Event::Closed) (*window).close(); if (event.type == sf::Event::MouseButtonPressed) { int linha = sf::Mouse::getPosition(*window).x / dim; int coluna = sf::Mouse::getPosition(*window).y / dim; matriz[linha][coluna] = ESTRELA; (*starSound).play(); } } //Fim do loop de eventos. } void desenhaRetangulos(sf::RenderWindow &window, sf::RectangleShape boxes[10][10], float x, float y) { sf::Color cor; for (int l = 0; l < 10; l++) { for (int c = 0; c < 10; c++) { if (boxes[l][c].getGlobalBounds().contains(x, y)) { cor = sf::Color::Green; } else { cor = sf::Color::Blue; } boxes[l][c].setFillColor(cor); window.draw(boxes[l][c]); } } } void desenhaEstrelas(sf::RenderWindow &window, Celulas matriz[10][10], int dim, sf::Sprite starImage) { float x, y; for (int l = 0; l < 10; l++) { for (int c = 0; c < 10; c++) { if (matriz[l][c] == ESTRELA) { x = l * dim; y = c * dim; starImage.setPosition(x, y); window.draw(starImage); } } } } //A função main é usada como função pricipal do jogo chamando funções auxiliares. int main() { /**********************Inicializa Objetos do Jogo**********************/ const int LARGURA = 500; const int ALTURA = 500; int dim = 50; // Opções de abertura da janela: sf::Style::Close + sf::Style::Titlebar + sf::Style::Resize sf::RenderWindow window(sf::VideoMode(LARGURA, ALTURA), "SFML works!", sf::Style::Close | sf::Style::Titlebar); window.setFramerateLimit(60); // Limita os frames window.setVerticalSyncEnabled(true); //limita a sincronização //Icone da janela. sf::Image image = sf::Image { }; image.loadFromFile("assets/cogumelo.png"); window.setIcon(image.getSize().x, image.getSize().y, image.getPixelsPtr()); sf::RectangleShape boxes[10][10]; //Matriz para desnhar os retângulos. Celulas matriz[10][10]; //Matriz para determinar se há algo na posição. for (int l = 0; l < 10; l++) { for (int c = 0; c < 10; c++) { boxes[l][c] = criaRetangulo(l * dim, c * dim, dim, dim); matriz[l][c] = AGUA; //Tudo marcado como agua. } } //Imagem da estrela sf::Texture textureStarImage; sf::Sprite starImage; textureStarImage.loadFromFile("assets/star.png"); textureStarImage.setSmooth(true); starImage.setTextureRect(sf::IntRect(0, 0, 50, 50)); starImage.setTexture(textureStarImage, true); float scala = (float) dim / textureStarImage.getSize().x; starImage.setScale(scala, scala); //Som da estrela sf::SoundBuffer starBuffer; sf::Sound starSound; starBuffer.loadFromFile("assets/star.wav"); starSound.setBuffer(starBuffer); /**********************Loop do jogo**********************/ while (window.isOpen()) { trataEventos(&window, matriz, &starSound, dim); float x = sf::Mouse::getPosition(window).x; float y = sf::Mouse::getPosition(window).y; /**********************Pinta o mundo**********************/ //Limpa a tela. window.clear(sf::Color::White); //Desenha retângulos desenhaRetangulos(window, boxes, x, y); // Desenha estrelas desenhaEstrelas(window, matriz, dim, starImage); window.display(); // Mostra na tela. sf::sleep(sf::milliseconds(50.0f)); //Aguarda. } //Fim do loop do jogo (janela aberta). return 0; }

Abaixo o link do projeto com o código fonte e os assets.

Projeto 

Postar um comentário

0 Comentários