Hi there, i'm new to SFML and a novice to c++ so i came into a problem today when i try to build my own player class. The bug occurs when i press a key in a running thread, everything runs ok until i press a key any key, The function in the class have a while loop when it checks is a key is pressed and if it is then is displays a message in the console.
Here is the code with the problem if anyone knows what i did wrong can you explain it to me.
The player.h
#include "GlobalVariabiles.h"
class Player
{
public:
Player(void);
virtual ~Player(void);
void setPlayerName(const std::string& name);
void player_move();
std::string getPlayerName();
sf::Sprite renderPlayer();
private:
void setPlayerTexture(const std::string& path);
sf::Texture player_texture;
sf::Sprite player_sprite;
sf::Thread move_thread;
};
And the player.cpp
#include "Player.h"
bool playerexist;
std::string Player_Name;
Player::Player(void)
:move_thread(&Player::player_move, this)
{
playerexist = true;
std::cout << "A new Player have bean created !" << std::endl;
this->move_thread.launch();
}
Player::~Player(void)
{
playerexist = false;
this->move_thread.terminate();
//this->move_thread.~Thread();
}
void Player::player_move()
{
while (playerexist){
std::cout << "Runing";
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
std::cout << "Player moved Up" << std::endl;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
std::cout << "Player moved Right" << std::endl;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
std::cout << "Player moved Down" << std::endl;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
std::cout << "Player moved Left" << std::endl;
}
}
}
Thanks in advance.
Well about the debug thing i tried all the thing i can think but i don't know how to make it work ... i try to put SFML_STATIC with and without release works but debug don't i put all the dependencies -d files but i don't know who to make it work.
Here is the debug output:
1>------ Build started: Project: Nagix V1.1, Configuration: Debug Win32 ------
1>Build started 10/24/2013 11:12:58 PM.
1>InitializeBuildStatus:
1> Touching "Debug\Nagix V1.1.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1> SoundEngine.cpp
1> RenderEngine.cpp
1> Player.cpp
1> main.cpp
1>d:\programs\programare\game\nagix v1.1\nagix v1.1\main.cpp(49): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> d:\programs\visual studio 2012\vc\include\conio.h(131) : see declaration of 'getch'
1> Generating Code...
1>sfml-audio-s.lib(SoundStream.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(SoundStream.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(Music.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(Music.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(SoundSource.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(SoundSource.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(AudioDevice.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(AudioDevice.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(SoundFile.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(SoundFile.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>sfml-audio-s.lib(ALCheck.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
1>sfml-audio-s.lib(ALCheck.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>RenderEngine.obj : error LNK2001: unresolved external symbol "public: static class sf::RenderStates const sf::RenderStates::Default" (?Default@RenderStates@sf@@2V12@B)
1>sfml-audio-s.lib(SoundStream.cpp.obj) : error LNK2019: unresolved external symbol "public: static class sf::Time const sf::Time::Zero" (?Zero@Time@sf@@2V12@B) referenced in function "private: bool __thiscall sf::SoundStream::fillAndPushBuffer(unsigned int)" (?fillAndPushBuffer@SoundStream@sf@@AAE_NI@Z)
1>D:\Programs\Programare\Game\Nagix V1.1\Debug\Nagix V1.1.exe : fatal error LNK1120: 2 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:06.31
And i tried to simplified my code but it still don't work i tried to run it off the main.
void x()
{
while (1==1)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
std::cout << "Player moved Up" << std::endl;
}
}
}
int main()
{
sf::Thread a(&x);
a.launch();
std::cout << "ok" << std::endl;
getch();
return 0;
}
It's still crashes when i press a key ... maybe this is not the right way to do it ... i don't know.