Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Woooo Using Multithreading gives me White Images  (Read 2698 times)

0 Members and 2 Guests are viewing this topic.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Woooo Using Multithreading gives me White Images
« on: December 30, 2009, 10:32:16 pm »
Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <list>
#include <map>

class ImageManager{
    private:
        std::map<std::string, sf::Image> images;
    public:
        ImageManager(){
        }
        sf::Image& Load(std::string file){
            if (images.find(file) == images.end()){
                images.insert(std::pair<std::string, sf::Image>(file, sf::Image()));
                images[file] = sf::Image();
                images[file].SetSmooth(false);
                images[file].LoadFromFile(file);
            }
            return images[file];
        }
        ~ImageManager(){
            images.clear();
        }
};

ImageManager Images;

class Object{
    private:
        sf::Sprite mySprite;
        sf::String myFilename, myParameter1, myParameter2;
    public:
        Object(){
        }
        Object(double X, double Y, sf::String Filepath, sf::String Param1, sf::String Param2){
            myFilename = Filepath;
            mySprite.SetImage(Images.Load(Filepath), true);
            mySprite.SetPosition(X, Y);
            myParameter1 = Param1;
            myParameter2 = Param2;
        }
        void LoadImage(sf::String Filepath){
            mySprite.SetImage(Images.Load(Filepath), true);
        }
        sf::Sprite& GetSprite(){
            return mySprite;
        }
        sf::String& GetParameter1(){
            return myParameter1;
        }
        sf::String& GetParameter2(){
            return myParameter2;
        }
        sf::String& GetFilename(){
            return myFilename;
        }
        ~Object(){
        }
};

sf::RenderWindow Window;
std::string Command;
sf::Vector2f LevelSize;
sf::Mutex Mutex;
Object CurrentObject;
std::list<Object> Objects;

bool CompareStrings(std::string stringsrc, std::string stringval){
    unsigned int length = stringval.length();
    if (stringsrc.length() < length)
        return false;
    for (unsigned int i = 0; i < length; i++){
        if (stringsrc[i] != stringval[i])
            return false;
    }
    return true;
}

void Console(void*){
    while (Window.IsOpened()){
        std::cin >> Command;
        if (CompareStrings(Command, "setsize")){
            std::cout << "Please enter the new Level Width: ";
            std::cin >> LevelSize.x;
            std::cout << "Recieved " << LevelSize.x << ". Please enter the new Level Height: ";
            std::cin >> LevelSize.y;
            std::cout << "Recieved " << LevelSize.y << ".\n";
        }
        else if (CompareStrings(Command, "setwidth")){
            std::cout << "Please enter the new Level Width: ";
            std::cin >> LevelSize.x;
            std::cout << "Recieved " << LevelSize.x << ".\n";
        }
        else if (CompareStrings(Command, "setheight")){
            std::cout << "Please enter the new Level Height: ";
            std::cin >> LevelSize.y;
            std::cout << "Recieved " << LevelSize.y << ".\n";
        }
        else if (CompareStrings(Command, "getsize")){
            std::cout << "Level Size is (" << LevelSize.x << ',' << LevelSize.y << ")\n";
        }
        else if (CompareStrings(Command, "setsprite")){
            std::cout << "Please enter the filename of the new sprite: ";
            std::string Filename;
            std::cin >> Filename;
            CurrentObject.GetFilename() = Filename;
            CurrentObject.LoadImage(Filename);
            std::cout << "Recieved " << Filename << ".\n";
        }
        else if (CompareStrings(Command, "setparameter1")){
            std::cout << "Please enter the new value for Parameter 1: ";
            std::string Param;
            std::cin >> Param;
            CurrentObject.GetParameter1() = Param;
            std::cout << "Recieved " << Param << ".\n";
        }
        else if (CompareStrings(Command, "setparameter2")){
            std::cout << "Please enter the new value for Parameter : ";
            std::string Param;
            std::cin >> Param;
            CurrentObject.GetParameter2() = Param;
            std::cout << "Recieved " << Param << ".\n";
        }
        else if (CompareStrings(Command, "getsprite")){
            std::string Filename = CurrentObject.GetFilename();
            std::cout << "The sprite's filename is " << Filename << ".\n";
        }
        else if (CompareStrings(Command, "getparameter1")){
            std::string Param = CurrentObject.GetParameter1();
            std::cout << "Parameter 1 is currently " << Param << ".\n";
        }
        else if (CompareStrings(Command, "getparameter2")){
            std::string Param = CurrentObject.GetParameter2();
            std::cout << "Parameter 2 is currently " << Param << ".\n";
        }
        else
            std::cout << "Invalid Function\n";
    }
}

int main(){
    LevelSize.x = 512;
    LevelSize.y = 512;
    Window.Create(sf::VideoMode(512, 512, 32), "Level Editor", sf::Style::Close);
    Window.SetFramerateLimit(50);
    sf::Thread Thread(&Console);
    Thread.Launch();
    while (Window.IsOpened()){
        sf::Event Event;
        while (Window.GetEvent(Event)){
            if (Event.Type == sf::Event::Closed || (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape)){
                Window.Close();
                break;
            }
        }
        CurrentObject.GetSprite().SetPosition(Window.GetInput().GetMouseX()-(Window.GetInput().GetMouseX()%4), Window.GetInput().GetMouseY()-(Window.GetInput().GetMouseY()%4));
        Window.Clear();
        Window.Draw(CurrentObject.GetSprite());
        Window.Display();
    }
    return EXIT_SUCCESS;
}

This huge piece of code draws a White Image to the screen when it should be drawing the sprite  give to it, which in this case is a Full-Saturation Spectrum Gradient. Do you think it has something to do with me not using a Mutex?
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Woooo Using Multithreading gives me White Images
« Reply #1 on: December 31, 2009, 12:06:04 am »
Which version of SFML do you use?
Laurent Gomila - SFML developer

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Woooo Using Multithreading gives me White Images
« Reply #2 on: December 31, 2009, 12:51:55 am »
Quote from: "Laurent"
Which version of SFML do you use?
SFML2 Build 1311.
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Woooo Using Multithreading gives me White Images
« Reply #3 on: December 31, 2009, 01:01:54 am »
Then you have to declare an instance of sf::Context at the beginning of every thread that uses the graphics module or OpenGL.

Sorry for the lack of tutorials, I should really write them ASAP.
Laurent Gomila - SFML developer

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Woooo Using Multithreading gives me White Images
« Reply #4 on: December 31, 2009, 01:51:55 am »
Quote from: "Laurent"
Then you have to declare an instance of sf::Context at the beginning of every thread that uses the graphics module or OpenGL.

Sorry for the lack of tutorials, I should really write them ASAP.
Alright, that fixed it.
I use the latest build of SFML2