Dark Temat założony przez niniejszego użytkownika |
[SFML, C++] Map/set iterator not dereferencable » 2011-09-10 17:15:18 Mam problem, chociaż aplikacja się odpala to wywala mi errora właśnie z tym, oto kod:
main.cpp
#include "head.h"
int main() { sf::RenderWindow okno( sf::VideoMode( 800, 600, 32 ), "Crion" ); okno.SetFramerateLimit( 60 ); Coords * DrawingAll = new Coords; DrawingAll->DrawAll( & okno ); return 0; }
head.h
#ifndef head_h #define head_h #include <SFML/Graphics.hpp> #include <iostream> #include <vector> #include <map>
using namespace std;
struct Point { int X, Y; }; class Coordinate { int Width; int Height; public: Point ObjectPoint; Coordinate() { } Coordinate( int X, int Y, int W, int H ) { Width = W; Height = H; ObjectPoint.X = X; ObjectPoint.Y = Y; } };
class Image { public: map < string, sf::Image > ImageMap; void AddImage( string FileName ) { sf::Image & ImageI = ImageMap[ FileName ]; if( ImageI.LoadFromFile( FileName ) ) { ImageI.SetSmooth( false ); } else cout << "Błąd wczytywania " << FileName << endl; } void ListOfImage() { if( !ImageMap.empty() ) { map < string, sf::Image >::iterator itImageMap; short int Lp = 1; cout << "Lista obrazow" << endl; for( itImageMap = ImageMap.begin(); itImageMap != ImageMap.end(); itImageMap++ ) { cout << Lp << ". " <<( * itImageMap ).first << endl; Lp++; } } else cout << "Brak images" << endl; } };
class Sprite : public Image { protected: map < string, sf::Sprite >::iterator itSprajtMap; public: map < string, sf::Sprite > SprajtMap; void AddNameSprite( string SprajtName, string ImageSrc ) { sf::Sprite & SprajtO = SprajtMap[ SprajtName ]; SprajtO.SetImage(( * ImageMap.find( ImageSrc ) ).second ); } void ListSprajtIm() { if( !SprajtMap.empty() ) { cout << "Sprajty" << endl; for( itSprajtMap = SprajtMap.begin(); itSprajtMap != SprajtMap.end(); itSprajtMap++ ) if( itSprajtMap != SprajtMap.end() ) cout << "SprajtMap - " <<( * itSprajtMap ).first << endl; } else cout << "Brak jakiegokolwiek sprite'a" << endl; } };
class Coords : public Sprite { protected: map < string, Coordinate > CoordsMap; public: Coords() { } void DrawAll( sf::RenderWindow * window ); void AddCoordSprite( string Name ) { for( itSprajtMap = SprajtMap.begin(); itSprajtMap != SprajtMap.end(); itSprajtMap++ ) { if( Name ==( * itSprajtMap ).first ) { double dx =( * SprajtMap.find( Name ) ).second.GetPosition().x; double dy =( * SprajtMap.find( Name ) ).second.GetPosition().y; double dw =( * ImageMap.find( Name ) ).second.GetWidth(); double dh =( * ImageMap.find( Name ) ).second.GetHeight(); CoordsMap[ Name ] = Coordinate( dx, dy, dw, dh ); } } } };
#endif
game.cpp
#include "head.h"
void Coords::DrawAll( sf::RenderWindow * okno ) { int map[ 14 ][ 20 ] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 }, { 1, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 }, { 1, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 3, 3, 2, 3, 3, 3, 3, 3, 1 }, { 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 1 }, { 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 }, { 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 }, { 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1 }, { 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 }, { 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 1 }, { 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 1 }, { 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 1 }, { 1, 4, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }; int y_map = 14; int x_map = 20; sf::Event Event; Sprite * SprajtyP = new Sprite; SprajtyP->AddImage( "platfa.png" ); SprajtyP->AddNameSprite( "platfa", "platfa.png" ); SprajtyP->AddImage( "kamien1.png" ); SprajtyP->AddNameSprite( "kamien", "kamien1.png" ); SprajtyP->AddImage( "ludz1.png" ); SprajtyP->AddNameSprite( "hero", "ludz1.png" ); SprajtyP->ListOfImage(); SprajtyP->ListSprajtIm(); sf::String text; text.SetText( "Skok" ); text.SetPosition( 400, 50 ); sf::Sprite & Boh =( * SprajtyP->SprajtMap.find( "Boh" ) ).second; sf::Image & BohImg =( * SprajtyP->ImageMap.find( "ludz1.png" ) ).second; BohImg.CreateMaskFromColor( sf::Color::Black, 0 ); Boh.SetCenter( BohImg.GetWidth() / 2.0f, BohImg.GetHeight() / 2.0f ); sf::Sprite & platfa =( * SprajtyP->SprajtMap.find( "platfa" ) ).second; sf::Sprite & kamien =( * SprajtyP->SprajtMap.find( "kamien" ) ).second; sf::Image & ziemiaI =( * SprajtyP->ImageMap.find( "platfa.png" ) ).second; int klatka = 0; short int szerokosc_kafel_1 = ziemiaI.GetWidth(); short int dlugosc_kafel_1 = ziemiaI.GetHeight(); double a = 800; double v = 0; double dt = 0; double vx = 0; double ax = 1000; double vx_max = 350; bool jump = true; for( int y = 0; y < y_map; y++ ) { for( int x = 0; x < x_map; x++ ) { if( map[ y ][ x ] == 4 ) { Boh.SetPosition( x * szerokosc_kafel_1 + BohImg.GetWidth() / 2.0f, y * dlugosc_kafel_1 ); } } } while( okno->IsOpened() ) { for( int y = 0; y < y_map; y++ ) { for( int x = 0; x < x_map; x++ ) { if( map[ y ][ x ] == 2 ) { platfa.SetPosition( x * szerokosc_kafel_1, y * dlugosc_kafel_1 ); okno->Draw( platfa ); } if( map[ y ][ x ] == 1 ) { kamien.SetPosition( x * szerokosc_kafel_1, y * dlugosc_kafel_1 ); okno->Draw( kamien ); } } } bool stand; const sf::Input & Input = okno->GetInput(); bool left = Input.IsKeyDown( sf::Key::Left ); bool right = Input.IsKeyDown( sf::Key::Right ); bool up = Input.IsKeyDown( sf::Key::Up ); bool down = Input.IsKeyDown( sf::Key::Down ); if( !left && !right && !up && !down ) stand = true; else stand = false; dt = double( okno->GetFrameTime() ); if( klatka <= 30 ) { if( klatka < 15 ) Boh.SetSubRect( sf::IntRect( 0, 0, 40, 40 ) ); else Boh.SetSubRect( sf::IntRect( 0, 40, 40, 80 ) ); klatka++; } else klatka = 0; v += a * dt; if( left ) { if( vx - ax * dt > - vx_max ) vx -= ax * dt; } else if( right ) { if( vx + ax * dt < vx_max ) vx += ax * dt; } else { if( jump ) { if( vx > 0 ) vx -= ax * dt / 10; if( vx < 0 ) vx += ax * dt / 10; if( Boh.GetPosition().y < 400 ) v -= v * dt / 2; if( v <= - 55000 * dt ) v = - 55000 * dt; } else { if( vx > 0 ) vx -= ax * dt / 2; if( vx < 0 ) vx += ax * dt / 2; } if( vx <= vx_max / 10 && vx >= 0 ) vx = 0; if( vx >= - vx_max / 10 && vx <= 0 ) vx = 0; } Boh.Move( vx * dt, v * dt ); if( Boh.GetPosition().y >= 400 ) { Boh.SetY( 400 ); jump = false; } else jump = true; if( Boh.GetPosition().y <= 0 ) Boh.SetY( 0 ); if( Boh.GetPosition().x <= 0 ) Boh.SetX( 0 ); if( Boh.GetPosition().x >= 750 ) Boh.SetX( 750 ); if( jump ) okno->Draw( text ); while( okno->GetEvent( Event ) ) { if( Event.Type == sf::Event::Closed ) okno->Close(); if(( Event.Type == sf::Event::KeyPressed ) &&( Event.Key.Code == sf::Key::Escape ) ) okno->Close(); if( up && !jump ) v -= 55000 * dt; } okno->Draw( Boh ); okno->Display(); okno->Clear(); } } |