Witam mam problem w sfml-u i nie jestem w stanie go naprawić. Program dziele na 4 pliki, a kod jest dość obszerny; problem polega jednak na, tym że program się zawiesza tylko przez algorytm znajdowania drogi (i tylko gdy ten algorytm jest używany wewnątrz sfmla w konsoli nie sprawia problemów). Należy podkreślić że program nie zawiesza się za każdym razem lecz wystarczająco często by był yo poważny błąd.Oto kod:
main.cpp
#include "game.hpp"
int main()
{
sf::RenderWindow app( sf::VideoMode( 800, 600 ), "SFML window" );
app.setFramerateLimit( 60 );
sf::Texture tcross; tcross.loadFromFile( "cross.png" );
tcross.setRepeated( true );
sf::Sprite cross( tcross );
cross.setTextureRect( sf::IntRect( 0, 0, 800, 600 ) );
sf::RenderTexture render;
sf::Sprite render_sprite( render.getTexture() );
sf::Texture texture;
texture.loadFromFile( "soldier.png" );
GameObject sprite; sprite.setTexture( texture );
sprite.setPosition( 120, 120 );
todraw.push_back( & sprite );
GameObject sprite2; sprite2.setTexture( texture );
sprite2.setPosition( 80, 120 );
todraw.push_back( & sprite2 );
sf::Texture inter; inter.loadFromFile( "interface\\interface.png" ); sf::Sprite tspr( inter );
sf::RenderTexture interface; interface.draw( tspr ); interface.display();
sf::Sprite interface_sprite( interface.getTexture() );
FMap map( 20, 15 ); Way way, way2;
sf::Event event;
while( app.isOpen() )
{
while( app.pollEvent( event ) ) {
if( event.type == sf::Event::Closed ) app.close(); }
if( sf::Mouse::isButtonPressed( sf::Mouse::Left ) )
{
sf::Vector2i pos = sf::Vector2i( sprite.getPosition().x / 40, sprite.getPosition().y / 40 );
sf::Vector2i mos = sf::Vector2i( sf::Mouse::getPosition( app ).x / 40, sf::Mouse::getPosition( app ).y / 40 );
map.findway( pos, mos, way );
}
if( !way.empty() ) sprite.move( follow_way( sprite, way ) );
else sprite.move( follow_point( sprite, sf::Vector2i( sprite.getPosition().x / 40, sprite.getPosition().y / 40 ) ) );
app.clear( sf::Color::White );
app.draw( cross );
for( unsigned int i = 0; i < todraw.size(); i++ )
{
app.draw( * todraw[ i ] );
}
app.display();
}
return EXIT_SUCCESS;
}
game.cpp
#include <SFML/Graphics.hpp>
#include <vector>
#include "findway_alg.h"
#define FIELD 40
sf::Vector2f follow_point( sf::Sprite spr, sf::Vector2i way, int speed = 1 )
{
sf::Vector2f mov;
if( way.x * 40 > spr.getPosition().x ) mov.x = 1;
if( way.x * 40 < spr.getPosition().x ) mov.x =- 1;
if( way.x * 40 == spr.getPosition().x ) mov.x = 0;
if( way.y * 40 > spr.getPosition().y ) mov.y = 1;
if( way.y * 40 < spr.getPosition().y ) mov.y =- 1;
if( way.y * 40 == spr.getPosition().y ) mov.y = 0;
return mov;
}
sf::Vector2f follow_way( sf::Sprite spr, Way & way, int speed = 1 )
{
if( way.empty() ) return sf::Vector2f( 0, 0 );
sf::Vector2f temp =( sf::Vector2f ) way[ 0 ];
if( way[ 0 ].x == spr.getPosition().x / 40 && way[ 0 ].y == spr.getPosition().y / 40 ) way.erase( way.begin() );
if( !way.empty() )
{ return follow_point( spr, way[ 0 ], speed ); }
else { return follow_point( spr,( sf::Vector2i ) temp, 0 ); }
}
class GameObject
: public sf::Sprite
{
};
std::vector < GameObject *> todraw;
findway_alg.h
#ifndef FINDWAY_ALG_H
#define FINDWAY_ALG_H
#include <SFML/System/Vector2.hpp>
#include <vector>
typedef std::vector < sf::Vector2i > Way;
class FMap
{
public:
int tab[ 500 ];
int fmap[ 500 ];
int width, height, size;
FMap();
FMap( int, int );
~FMap();
void clean();
void create( int, int );
void findway( sf::Vector2i start, sf::Vector2i destination, Way & way );
};
#endif
i .cpp
#include "findway_alg.h"
sf::Vector2i dir[] = { sf::Vector2i( 0, - 1 ), sf::Vector2i( - 1, 0 ),
sf::Vector2i( 1, 0 ), sf::Vector2i( 0, 1 ), sf::Vector2i( 1, 1 ), sf::Vector2i( - 1, - 1 ), sf::Vector2i( 1, - 1 ), sf::Vector2i( - 1, 1 ) };
FMap::FMap()
{
width = 0; height = 0; size = 0;
}
FMap::~FMap()
{
}
void FMap::clean()
{
for( int i = 0; i < size; i++ )
{
tab[ i ] = 0;
}
}
void FMap::create( int w, int h )
{
width = w; height = h;
size = width * height;
clean();
for( int i = 0; i < size; i++ )
{
fmap[ i ] =- 1;
}
}
FMap::FMap( int w, int h )
{
create( w, h );
}
void FMap::findway( sf::Vector2i start, sf::Vector2i destination, Way & way )
{
way.clear();
if( start == destination || destination.x < 0 || destination.x >= width || destination.y < 0 || destination.y >= height ) return;
bool reached = false;
for( int i = 0; i < size; i++ )
{
fmap[ i ] =- 1;
}
fmap[ start.x + start.y * width ] = 0;
way.push_back( start );
while( !way.empty() &&!reached )
{
sf::Vector2i v = way[ way.size() - 1 ];
way.pop_back();
for( int i = 0; i < 8; i++ )
{
sf::Vector2i w( v.x + dir[ i ].x, v.y + dir[ i ].y );
if( w.x < 0 || w.x >= width || w.y < 0 || w.y >= width || tab[ w.x + width * w.y ] > 0 || fmap[ w.x + w.y * width ] >- 1 ) continue;
fmap[ w.x + w.y * width ] = fmap[ v.x + v.y * width ] + 1;
way.insert( way.begin(), w );
if( w == destination )
{
reached = true;
break;
}
}
}
way.clear();
if( !reached )
{
for( int j = 0; way.empty(); j++ )
for( int i = 0; i < 8; i++ )
{
sf::Vector2i w( destination.x + dir[ i ].x * j, destination.y + dir[ i ].y * j );
if( w.x < 0 || w.x >= width || w.y < 0 || w.y >= width || tab[ w.x + width * w.y ] > 0 || fmap[ w.x + w.y * width ] ==- 1 ) continue;
way.push_back( w );
}
sf::Vector2i closest = way[ 0 ]; int len = fmap[ way[ 0 ].x + way[ 0 ].y * width ];
for( unsigned int i = 1; i < way.size(); i++ )
{
if( fmap[ way[ i ].x + way[ i ].y * width ] < fmap[ closest.x + closest.y * width ] )
{
closest = way[ i ]; len = fmap[ way[ i ].x + way[ i ].y * width ];
}
}
way.clear();
way.resize( len );
way[ way.size() - 1 ] = closest;
for( unsigned int j = 0; j < way.size(); j++ )
{
for( int i = 0; i < 8; i++ )
{
sf::Vector2i w( way[ way.size() - 1 - j ].x + dir[ i ].x, way[ way.size() - 1 - j ].y + dir[ i ].y );
if( w.x < 0 || w.x >= width || w.y < 0 || w.y >= width || tab[ w.x + width * w.y ] > 0 || fmap[ w.x + w.y * width ] ==- 1 ) continue;
if( fmap[ w.x + w.y * width ] < fmap[ way[ way.size() - 1 - j ].x + way[ way.size() - 1 - j ].y * width ] ) { way[ way.size() - 2 - j ] = w; break; }
}
}
return;
}
way.resize( fmap[ destination.x + destination.y * width ] );
way[ way.size() - 1 ] = destination;
for( unsigned int j = 0; j < way.size(); j++ )
{
for( int i = 0; i < 8; i++ )
{
sf::Vector2i w( way[ way.size() - 1 - j ].x + dir[ i ].x, way[ way.size() - 1 - j ].y + dir[ i ].y );
if( w.x < 0 || w.x >= width || w.y < 0 || w.y >= width || tab[ w.x + width * w.y ] > 0 || fmap[ w.x + w.y * width ] ==- 1 ) continue;
if( fmap[ w.x + w.y * width ] < fmap[ way[ way.size() - 1 - j ].x + way[ way.size() - 1 - j ].y * width ] ) { way[ way.size() - 2 - j ] = w; break; }
}
}
return;
}
debuger code::blocks'a wyrzuca:
#0 774143E0 ntdll!RtlCreateProcessParametersEx() (C:\Windows\system32\ntdll.dll:??)
#1 00770000 ?? () (??:??)
#2 774135B7 ntdll!RtlReleasePebLock() (C:\Windows\system32\ntdll.dll:??)
#3 00770000 ?? () (??:??)
#4 774134A2 ntdll!LdrLoadAlternateResourceModuleEx() (C:\Windows\system32\ntdll.dll:??)
#5 00774920 ?? () (??:??)
#6 774B172E ntdll!RtlValidateHeap() (C:\Windows\system32\ntdll.dll:??)
#7 00770000 ?? () (??:??)
#8 7746AC29 ntdll!AlpcMaxAllowedMessageLength() (C:\Windows\system32\ntdll.dll:??)
#9 00770000 ?? () (??:??)
#10 774134A2 ntdll!LdrLoadAlternateResourceModuleEx() (C:\Windows\system32\ntdll.dll:??)
#11 00774920 ?? () (??:??)
#12 754498CD msvcrt!free() (C:\Windows\syswow64\msvcrt.dll:??)
#13 00770000 ?? () (??:??)
#14 6E188040 __gnu_cxx::new_allocator<sf::Event>::deallocate(this=0x7745ec, __p=0x774928) (d:/programmes/mingw32-4.7/bin/../lib/gcc/mingw32/4.7.2/include/c++/ext/new_allocator.h:100)
#15 6E189125 std::_Deque_base<sf::Event, std::allocator<sf::Event> >::_M_deallocate_node(this=0x7745ec, __p=0x774928) (d:/programmes/mingw32-4.7/bin/../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_deque.h:540)
#16 6E189D7E std::deque<sf::Event, std::allocator<sf::Event> >::_M_pop_front_aux(this=0x7745ec) (d:/programmes/mingw32-4.7/bin/../lib/gcc/mingw32/4.7.2/include/c++/bits/deque.tcc:521)
#17 6E18A169 std::deque<sf::Event, std::allocator<sf::Event> >::pop_front(this=0x7745ec) (d:/programmes/mingw32-4.7/bin/../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_deque.h:1411)
#18 6E18A36B std::queue<sf::Event, std::deque<sf::Event, std::allocator<sf::Event> > >::pop(this=0x7745ec) (d:/programmes/mingw32-4.7/bin/../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_queue.h:240)
#19 6E18325B sf::priv::WindowImpl::popEvent(this=0x7745e8, event=..., block=false) (D:\developpement\sfml\sfml\src\SFML\Window\WindowImpl.cpp:128)
#20 6E182970 sf::Window::pollEvent(this=0x28e6a0, event=...) (D:\developpement\sfml\sfml\src\SFML\Window\Window.cpp:186)
#21 00402EA4 main() (C:\Programy\CodeBlocks\C++\RTS Project\main.cpp:36)
podkreślając 14 linijkę