R0ut4 Temat założony przez niniejszego użytkownika |
» 2021-04-25 16:03:17 OK. UDAŁO SIĘ! Tak jakby :/ Tzn: kompiluje się i wszystko jest tak jak powinno poza dwoma frzeczami: 1. Mam dwa Sprity gracza, jeden statyczny, a drugim się mogę ruszać 2. Kamera nie podąża za graczem ruchomym :/ player.cpp #include "player.hpp" #include <SFML/Graphics.hpp> #include <SFML/Window.hpp> void Gracz::draw( sf::RenderTarget & target, sf::RenderStates states ) const { sf::VertexArray P( sf::Quads, 4 ); P[ 0 ].position = sf::Vector2f( 0, 0 ); P[ 1 ].position = sf::Vector2f( 100, 0 ); P[ 2 ].position = sf::Vector2f( 100, 100 ); P[ 3 ].position = sf::Vector2f( 0, 100 ); sf::Texture textura; textura.loadFromFile( "player.png" ); P[ 0 ].texCoords = sf::Vector2f( 0, 0 ); P[ 1 ].texCoords = sf::Vector2f( 50, 0 ); P[ 2 ].texCoords = sf::Vector2f( 50, 50 ); P[ 3 ].texCoords = sf::Vector2f( 0, 50 ); states.texture = & textura; target.draw( P, states ); states.transform *= getTransform(); target.draw( P, states ); }
playermove.cpp #include <SFML/Graphics.hpp> #include <SFML/Window.hpp> #include "playermove.hpp" #include "player.hpp"
void playerMove( Gracz & hero, sf::View cam, int HM, int WM, Stan G, Stan G1, Stan ST ) { if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) ) { hero.move( 0, - 2 ); cam.move( 0, - 2 ); } if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) ) { hero.move( 0, 2 ); cam.move( 0, 2 ); } if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) ) { hero.move( - 2, 0 ); cam.move( - 2, 0 ); } if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) ) { hero.move( 2, 0 ); cam.move( 2, 0 ); } }
main.cpp #include <SFML/Window.hpp> #include <SFML/Graphics.hpp> #include "tilemap.hpp" #include "playermove.hpp" #include "player.hpp"
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, }; const int level1[ ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 2, 2, 1, 1, 1, 2, 2, 3, 0, 0, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 3, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; TileMap map1; if( !map1.load(( "tileset.png" ), sf::Vector2u( 100, 100 ), level, 16, 8 ) ) { return - 1; } TileMap map2; if( !map2.load(( "tileset.png" ), sf::Vector2u( 100, 100 ), level1, 16, 8 ) ) { return - 1; } Gracz spPlayer; spPlayer.setOrigin( 25.f, 25.f ); spPlayer.setPosition( 640, 360 ); sf::View kamera; kamera.setCenter( spPlayer.getPosition() ); kamera.setSize( 1280.f, 720.f ); Stan start; int widthmap = 1280, heightmap = 720; sf::Event e; while( window.isOpen() ) { while( window.pollEvent( e ) ) { if( e.type == sf::Event::Closed ) { window.close(); } } switch( start ) { case Menu: { if( sf::Keyboard::isKeyPressed( sf::Keyboard::Enter ) ) { start = Game; } } break; case Game: { playerMove( spPlayer, kamera, heightmap, widthmap, Game, Game1, start ); } break; case Game1: break; case Eq: break; } window.clear(); switch( start ) { case Menu: break; case Game: { window.draw( map1 ); window.draw( spPlayer ); window.setView( kamera ); } break; case Game1: { window.draw( map1 ); window.draw( spPlayer ); window.setView( kamera ); } break; case Eq: break; } window.display(); } }
|