Witam, od jakiegoś czasu męczę się z tablicą sf::VertexArray i dzieleniem kodu na pliki. Pierwszy kod to main.cpp, a drugi to test.cpp
Pytanie: Dlaczego obiekt klasy Obiekt nie rysuje się?
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "test.cpp"
enum Stan
{
Menu, Wybor, Gra
};
void move();
void setPosition();
int main()
{
sf::RenderWindow aplikacja(sf::VideoMode(820, 720), "GRA");
aplikacja.setFramerateLimit(30);
Stan stan = Menu;
obiekt pdl;
//Tworzenie tekstur
sf::Texture menu_textura;
sf::Texture wybor_textura;
sf::Texture gra_textura;
sf::Texture player_texture;
sf::Texture drzewo_texture;
//Wczytywanie tekstur
menu_textura.loadFromFile ("tekstura-menu.png");
wybor_textura.loadFromFile ("wybor-tekstura.png");
gra_textura.loadFromFile ("gra-tekstura.png");
player_texture.loadFromFile ("tekstura-gracza.png");
drzewo_texture.loadFromFile ("tekstura-drzewo.png");
//Tworzenie sprite
sf::Sprite menu;
sf::Sprite wybor;
sf::Sprite gra;
sf::Sprite player(player_texture, sf::IntRect(0,0,100,200));
sf::Sprite drzewo;
//Ustawianie tekstur
menu.setTexture (menu_textura);
wybor.setTexture (wybor_textura);
gra.setTexture (gra_textura);
//player.setTextureRect(player_texture, sf::IntRect(0,0,100,200));
drzewo.setTexture (drzewo_texture);
drzewo.setPosition (200, 200);
player.setPosition (100, 100);
while( aplikacja.isOpen() )
{
sf::Event event;
while( aplikacja.pollEvent( event ) )
{
if( event.type == sf::Event::Closed )
aplikacja.close();
}
switch( stan )
{
case Menu:
// OBSLUGA ZDARZEM W MENU
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
{
stan = Gra;
}
break;
case Wybor:
// OBSLUGA ZDARZEN W WYBORZE
break;
case Gra:
// OBSLUGA ZDARZEN W GRZE
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
int top = player.getTextureRect().top;
player.setTextureRect(sf::IntRect(200, (top+200)%600, 100, 200));
player.move (0, -5);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
int top = player.getTextureRect().top;
player.setTextureRect(sf::IntRect(100, (top+200)%600, 100, 200));
player.move (0, 5);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
int top = player.getTextureRect().top;
player.setTextureRect(sf::IntRect(100, (top+200)%600, -100, 200));
player.move (-5, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
int top = player.getTextureRect().top;
player.setTextureRect(sf::IntRect(0, (top+200)%600, 100, 200));
player.move (5, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
{
int top = player.getTextureRect().top;
player.setTextureRect(sf::IntRect(300, (top+200)%600, 100, 200));
}
break;
}
aplikacja.clear();
switch( stan )
{
case Menu:
// WYSWIETLANIE W MENU
aplikacja.draw (menu);
aplikacja.draw (pdl);
break;
case Wybor:
// WYSWIETLANIE W WYBORZE POSTACI
aplikacja.draw (wybor);
break;
case Gra:
// WYSWIETLANIE W GRZE
aplikacja.draw (gra);
aplikacja.draw (drzewo);
aplikacja.draw (player);
break;
}
aplikacja.display();
} //while
return 0;
}
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
class obiekt
:public sf::Drawable
{
public:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
sf::VertexArray kwadrat (sf::Quads, 4);
kwadrat[0].position = sf::Vector2f (100, 100);
kwadrat[1].position = sf::Vector2f (100, 300);
kwadrat[2].position = sf::Vector2f (300, 300);
kwadrat[3].position = sf::Vector2f (100, 300);
sf::Texture textura;
textura.loadFromFile("podloga.jpg");
kwadrat[0].texCoords = sf::Vector2f(0,0);
kwadrat[1].texCoords = sf::Vector2f(0,100);
kwadrat[2].texCoords = sf::Vector2f(100,100);
kwadrat[3].texCoords = sf::Vector2f(100,0);
}
};