R0ut4 Temat założony przez niniejszego użytkownika |
[SFML/C++] redefinition of class, dzielene kodu » 2021-04-21 22:46:20 Ja się już kurde poddaje chyba :/ są trzy takie pliki: Main.cpp, tilemap.hpp, tilemap.cpp #include <SFML/Window.hpp> #include <SFML/Graphics.hpp> #include "tilemap.hpp"
enum Stan { Menu, Game, Eq };
int main() { sf::RenderWindow window( sf::VideoMode( 1280, 720 ), "Game" ); window.setFramerateLimit( 60 ); const int level[ ] = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0, 0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0, 2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, }; TileMap map1; if( !map1.load( "tilset.png", sf::Vector2u( 100, 100 ), level, 16, 8 ) ) { return - 1; } sf::Texture player; player.loadFromFile( "player.png" ); sf::Sprite spPlayer( player ); spPlayer.setOrigin( 25.f, 25.f ); spPlayer.setPosition( 640, 360 ); sf::View kamera; kamera.setCenter( spPlayer.getPosition() ); kamera.setSize ( 640.f, 360.f ); Stan stan; sf::Event e; while( window.isOpen() ) { while( window.pollEvent( e ) ) { if( e.type == sf::Event::Closed ) { window.close(); } } switch( stan ) { case Menu: { if( sf::Keyboard::isKeyPressed( sf::Keyboard::Enter ) ) { stan = Game; } } break; case Game: { if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) ) { spPlayer.move( 0, - 2 ); kamera.move( 0, - 2 ); } if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) ) { spPlayer.move( 0, 2 ); kamera.move( 0, 2 ); } if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) ) { spPlayer.move( - 2, 0 ); kamera.move( - 2, 0 ); } if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) ) { spPlayer.move( 2, 0 ); kamera.move( 2, 0 ); } } break; case Eq: break; } window.clear(); switch( stan ) { case Menu: break; case Game: { window.draw( map1 ); window.draw( spPlayer ); window.setView( kamera ); } break; case Eq: break; } window.display(); } }
#ifndef tilemap_hpp #define tilemap_hpp
class TileMap : public sf::Drawable , public sf::Transformable { public: bool load( const std::string & tileset, sf::Vector2u tileSize, const int * tiles, unsigned int width, unsigned int height ); private: virtual void draw( sf::RenderTarget & target, sf::RenderStates states ) const; }; #endif #include <SFML/Graphics.hpp> #include <SFML/Window.hpp> #include "tilemap.hpp"
class TileMap : public sf::Drawable , public sf::Transformable { public: bool load( const std::string & tileset, sf::Vector2u tileSize, const int * tiles, unsigned int width, unsigned int height ) { if( !m_tileset.loadFromFile( tileset ) ) { return false; } m_vertices.setPrimitiveType( sf::Quads ); m_vertices.resize( width * height * 4 ); for( unsigned int i = 0; i < width; i++ ) { for( unsigned int j = 0; j < height; j++ ) { int tileNumber = tiles[ i + j * width ] int tu = tileNumber %( m_tileset.getSize().x / tileSize.x ); int tv = tileNumber /( m_tileset.getSize().x / tileSize.x ); sf::Vertex * quad = & m_vertices[( i + j * width ) * 4 ]; quad[ 0 ].position = sf::Vector2f( i * tileSize.x, j * tileSize.y ); quad[ 1 ].position = sf::Vector2f(( i + 1 ) * tileSize.x, j * tileSize.y ); quad[ 2 ].position = sf::Vector2f(( i + 1 ) * tileSize.x,( j + 1 ) * tileSize.y ); quad[ 3 ].position = sf::Vector2f( i * tileSize.x,( j + 1 ) * tileSize.y ); quad[ 0 ].texCoords = sf::Vector2f( tu * tileSize.x, tv * tileSize.y ); quad[ 1 ].texCoords = sf::Vector2f(( tu + 1 ) * tileSize.x, tv * tileSize.y ); quad[ 2 ].texCoords = sf::Vector2f(( tu + 1 ) * tileSize.x,( tv + 1 ) * tileSize.y ); quad[ 3 ].texCoords = sf::Vector2f( tu * tileSize.x,( tv + 1 ) * tileSize.y ); } } return true; } private: virtual void draw( sf::RenderTarget & target, sf::RenderStates states ) const { virtual void draw( sf::RenderTarget & target, sf::RenderStates states ) const { states.transform *= getTransform(); states.texture = & m_tileset; target.draw( m_vertices, states ); } } };
ERROR: redefinition of "class Tilemap" (linia 5, plik tilemap.cpp) Przeszukałem całą 1 stronę googla :D i NIC :( |