Bonjour,
CE SUJET FAIT SUITE A MON SUJET SUR LES THREADS DE SFML, J'AI DECIDE D'UTILISER CEUX DE C++11
Bref, voici mon problème :
http://prntscr.com/2s71jsJe ne sais pas d'ou vient cette erreur... :/
//
// 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"
#include <thread>
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:
sf::Sprite ld_bar;
sf::Clock clock;
sf::Time tm;
sf::Event Event;
sf::Vector2u v2u;
int m = 0;
sf::Text text;
bool first = false;
std::thread *loadRsc;
};
#endif /* defined(__SFML_Gamestates__CGameStart__) */
.cpp
//
// 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"
#include <thread>
/*
sf::Texture ld_bar_txt;
sf::Sprite ld_bar;
sf::Clock clock;
sf::Time tm;
*/
int rtr_val=404;
CLoad val;
//CLoad object;
sf::Texture ld_bar_txt;
sf::Font font;
void loadFiles (int &rtr)
{
if(ld_bar_txt.loadFromFile(resourcePath()+"LC_LOAD.png") || font.loadFromFile(resourcePath() + "mini_pixel-7.ttf"))
{
rtr = 1;
cout << "Game ressources succesfully loaded !" << endl;
}
else
{
rtr = -1;
cout << " An error occured " << endl;
}
}
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);
loadRsc = new std::thread(&loadFiles,rtr_val);
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)
{
first=true;
if(loadRsc->joinable())
loadRsc->join();
cout << "Thread launch" << endl;
}
if(rtr_val== 404)
{
cout << "0";
// Je ne fais rien car ma fonction n'as pas encore renvoyée son résultat
}
else if(rtr_val==-1)
{
// Résultat négatif
cout << "Cannot load ressources " << endl;
exit(-1);
}
else if (rtr_val==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 (rtr_val==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();
}
Celui qui résous mon problème, je lui fait des bisous partout <3
**Le code source complet est en piece jointe**
Bref, j'ai essayé tout ces derniers jour, j'ai bossé au moins 10H pour tenter de résoudre ces bugs...
Si quelqu'un à une autre solution pour un chargement des ressources multi-threadé, je suis preneur !
Merci.