PS: J'ai également utilisé StackOverflow et Cplusplus
BREF. ici j'ai un autre soucis relatif aux thread, ma variable n'est pas modifiée.
loading.h
//
// CGameStart.h
// SFML Gamestates
//
// Created by Pierre van Houtryve on 6/02/14.
// Copyright (c) 2014 Pierre van Houtryve. All rights reserved.
//
#ifndef __SFML_Gamestates__CGameStart__
#define __SFML_Gamestates__CGameStart__
#include <iostream>
#include "/Users/pierre/Documents/Mako GUI/Tests/SFML Gamestates/tMap.h"
#include "SFML/Graphics.hpp"
#include "gameengine.h"
#include "gamestate.h"
#include "ResourcePath.hpp"
class CLoad : public CGameState {
public:
void Init();
void Cleanup();
void Pause();
void Resume();
void HandleEvents(CGameEngine* game);
void Update(CGameEngine* game);
void Draw(CGameEngine* game);
private:
int m_rtr =404;
sf::Mutex gMutex;
sf::Texture ld_bar_txt;
sf::Sprite ld_bar;
sf::Clock clock;
sf::Time tm;
sf::Event Event;
sf::Vector2u v2u;
int m = 0;
sf::Text text;
sf::Font font;
bool first = false;
public:
void loadFiles ()
{
if(!(ld_bar_txt.loadFromFile(resourcePath()+"LC_LOAD.png")) || !(font.loadFromFile(resourcePath() + "mini_pixel-7.ttf")))
{
gMutex.lock();
m_rtr=-1;
cout << " An error occured " << endl;
gMutex.unlock();
}
else
{
gMutex.lock();
m_rtr=1;
cout << "Game ressources succesfully loaded !" << endl;
gMutex.unlock();
}
}
};
#endif /* defined(__SFML_Gamestates__CGameStart__) */
//
// loading.cpp
// Game
//
// Created by Pierre van Houtryve on 9/02/14.
// Copyright (c) 2014 Pierre van Houtryve. All rights reserved.
//
#include "loading.h"
#include <stdio.h>
#include "ResourcePath.hpp"
#include "SFML/System.hpp"
/*
sf::Texture ld_bar_txt;
sf::Sprite ld_bar;
sf::Clock clock;
sf::Time tm;
*/
CLoad object;
sf::Thread thread1(&CLoad::loadFiles,&object);
void CLoad::Init()
{
ld_bar_txt.setSmooth(1);
ld_bar.setTextureRect(sf::IntRect(102,102,102,102));
text.setFont(font);
text.setCharacterSize(35);
ld_bar.setTexture(ld_bar_txt);
clock.restart();;
}
void CLoad::Cleanup()
{
}
void CLoad::Pause()
{
}
void CLoad::Resume()
{
}
void CLoad::HandleEvents(CGameEngine* game)
{
while(game->screen->pollEvent(Event))
{
if(Event.type == sf::Event::Closed)
{
game->screen->close();
this->Cleanup();
game->Quit();
exit(0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
game->screen->close();
this->Cleanup();
game->Quit();
exit(0);
}
}
}
void CLoad::Update(CGameEngine* game)
{
if(!first)
{
thread1.launch();
first=true;
cout << "Thread launch" << endl;
}
if(m_rtr == 404)
{
cout << "0";
// Je ne fais rien car ma fonction n'as pas encore renvoyée son résultat
}
else if(m_rtr!=1)
{
// Résultat négatif
cout << "Cannot load ressources " << endl;
exit(-1);
}
else if (m_rtr==1)
{
cout << "Finished" << endl;
// ici j'aurais mis mon change gamestate, etc...
}
text.setString(L"Game is loading...");
text.setColor(sf::Color::White);
v2u = game->screen->getSize();
ld_bar.setPosition((v2u.x/2)-75,(v2u.y/2)-75);
text.setPosition((v2u.x/2)-105,(v2u.y/2)-155);
if(m<=7)
m++;
else if(m>7)
m=0;
tm = clock.getElapsedTime();
if(tm.asMilliseconds()>1)
{
ld_bar.setTextureRect(sf::IntRect(m*102,0,102,102));
clock.restart();
}
}
void CLoad::Draw(CGameEngine* game)
{
if (m_rtr==1)
{
ld_bar.setTextureRect(sf::IntRect(1,1,102,816));
game->screen->draw(ld_bar);
cout << "2";
}
else
{
game->screen->draw(ld_bar);
game->screen->draw(text);
cout << "1";
}
game->screen->display();
game->screen->clear();
}
Et mon 'output' console
CGameEngine Init
Thread launch
01Game ressources succesfully loaded !
0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
Merci