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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - NPS

Pages: [1] 2 3
1
System / Re: [2.0] Using a thread to load game resources
« on: January 24, 2013, 08:45:46 pm »
It gets worse - on my laptop I always get said error when loading using another thread - not only with sf::RenderTarget. :P

2
System / Re: [2.0] Using a thread to load game resources
« on: January 22, 2013, 06:27:10 pm »
So as I've said, now I'm calling sf::RenderTexture::create() method from the main thread and that is working fine on my PC (nvidia card). But later I tried running the same code on my laptop (ati card) and 2 things happen: I don't see images using sf::RenderTexture (as was the case initially on my PC) and also the application crashes after closing the window saying something about uncaught exception and "access violation reading location...". :P

3
System / Re: [2.0] Using a thread to load game resources
« on: January 21, 2013, 06:59:42 pm »
You don't exactly get the concept of minimal code, do you?
I didn't want to cut out anything relevant (and trust me - I've cut out a lot already). I my case the problem is with sf::RenderTexture (vide my last post's edit note). I don't see any sf::RenderTexture in your code. :P

4
System / Re: [2.0] Using a thread to load game resources
« on: January 21, 2013, 01:03:08 pm »
It would be something like this. The number on the left (1337) displays fine, the number on the right (7331, loaded in a separate thread) doesn't show up at all.

main.cpp:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "DigitDisplay.hpp"

typedef sf::Vector2<float> float2;

sf::RenderWindow window;
DigitDisplay numberNormal;
DigitDisplay numberThread;

void LoadResources(void)
{
    numberThread.Load("data/digits.png", "7331");
}

int main(void)
{
    numberNormal.Load("data/digits.png", "1337");
    numberNormal.SetPosition(float2(300.0f, 300.0f));
    sf::Thread thread(LoadResources);
    thread.launch();
    thread.wait();
    numberThread.SetPosition(float2(600.0f, 300.0f));

    window.create(sf::VideoMode(800, 600, 32), "SFML Framework",
        sf::Style::Titlebar | sf::Style::Close);
    window.setVerticalSyncEnabled(true);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }
        window.clear(sf::Color::Black);
        numberNormal.Render();
        numberThread.Render();
        window.display();
    }
    return EXIT_SUCCESS;
}
 
DigitDisplay.hpp:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

typedef unsigned int uint;
typedef sf::Vector2<int> int2;
typedef sf::Vector2<float> float2;

extern sf::RenderWindow window;

class DigitDisplay
{
    private:
    sf::Texture digitImage;
    std::vector<sf::Sprite> digits;
    sf::RenderTexture renderTexture;
    sf::Sprite textureSprite;
    std::string text;
    float2 position;

    void CreateRenderTexture(void);
    inline int GetDigit(char c);

    public:
    void Load(std::string filename, std::string _text = "");
    void Render(void);
    void SetPosition(float2 _position);
};
 
DigitDisplay.cpp:
#include "DigitDisplay.hpp"

void DigitDisplay::CreateRenderTexture(void)
{
    int2 size(digitImage.getSize().x * text.size() / 10, digitImage.getSize().y);
    renderTexture.create(size.x, size.y);
    textureSprite.setTexture(renderTexture.getTexture());
    textureSprite.setTextureRect(sf::IntRect(0, 0, size.x, size.y));
}

int DigitDisplay::GetDigit(char c)
{
    switch (c)
    {
        case '0':
            return 0;
        case '1':
            return 1;
        case '2':
            return 2;
        case '3':
            return 3;
        case '4':
            return 4;
        case '5':
            return 5;
        case '6':
            return 6;
        case '7':
            return 7;
        case '8':
            return 8;
        case '9':
            return 9;
        default:
            return -1;
    }
}

void DigitDisplay::Load(std::string filename, std::string _text)
{
    digitImage.loadFromFile(filename);
    uint digitWidth = digitImage.getSize().x / 10;
    uint frameHeight = digitImage.getSize().y;
    digits.resize(10);
    for (int i = 0 ; i < 10 ; ++i)
    {
        digits[i].setTexture(digitImage);
        digits[i].setTextureRect(sf::IntRect(i * digitWidth, 0, digitWidth, frameHeight));
    }
    text = _text;
    if (text.size())
    {
        CreateRenderTexture();
    }
}

void DigitDisplay::Render(void)
{
    renderTexture.clear(sf::Color::Transparent);
    float digitWidth = digitImage.getSize().x * 0.1f;
    for (uint i = 0 ; i < text.size() ; ++i)
    {
        int digit = GetDigit(text[i]);
        digits[digit].setPosition(i * digitWidth, 0.0f);
        renderTexture.draw(digits[digit]);
    }
    renderTexture.display();
    textureSprite.setPosition(position);
    window.draw(textureSprite);
}

void DigitDisplay::SetPosition(float2 _position)
{
    position = _position;
}
 

======================================================================
Edit: I've made some progress. I found out that when I call DigitDisplay::CreateRenderTexture() from the 2nd thread nothing shows up on the screen. However, when I call it from the main thread (after loading the image and setting up sf::Sprite in DigitDisplay in 2nd thread) the image shows up on the screen and everything seems to be working fine.

Can't I use sf::RenderTexture::create() in another thread? If I can, how to do it correctly?

5
System / Re: [2.0] Using a thread to load game resources
« on: January 21, 2013, 12:15:43 pm »
So I am using latest graphics driver and sources version.

6
System / Re: [2.0] Using a thread to load game resources
« on: January 21, 2013, 11:16:22 am »
I have GTX 460. Is "SFML 2.0 snapshot" the latest version?

7
System / [2.0] Using a thread to load game resources
« on: January 20, 2013, 03:03:26 am »
I'm making a loading screend and I thought I needed a second thread to load the game resources in. In the meantime the main thread would keep running main game loop, handling events, showing "loading" image and keeping window responsive. So the second thread only loads the resources and then ends.

For simplicity's sake (if I simplify my problem too much and you won't be able to help me then I'll go into more detail and paste some code but I'd like to avoid that) let's say a have this big Game class which has all resources as fields and and auxiliary private Load() method (which only loads these resources, nothing else). My game is very small so one big class is enough, actually. Anyway, in the main thread I call a Game's method (let's call it PublicLoad()) which starts a new thread giving it its Load() method and "this" pointer, then I keep running the main loop until the data are loaded (I have a boolean variable "loaded" which is set to "true" by the 2nd thread when the loading is done and the main thread keeps checking this variable).

After all that, the Game class should start "playing" the game using the loaded resources. But this doesn't work as inteded, as I can see some of the resources loaded and used, and others not (I can hear the music and sounds, I can see some images but not others). Although, after checking the resources with debugger everything seems loaded and fine). Just nothing shows up on the screen.

Also actually the only things that I can't see on my screen are classes using sf::RenderTexture, I don't know if it's related to using threads in anyway.

Oh yeah, and the same Game class works fine when I'm loading using only the main thread.

Edit: Ok, it's definitely something with sf::RenderTexture, because if I draw directly into sf::RenderWindow instead of sf::RenderTarget (and then from it to the window) everything works fine.

Any idea? Should I post some code (what code would be helpful here)?

8
Graphics / Re: Parallax background with a couple of images
« on: January 20, 2013, 12:58:51 am »
I know, that's what I mean, that was a mental leap. :P Thx for confirmation anyway.

9
Graphics / Re: Parallax background with a couple of images
« on: January 20, 2013, 12:23:00 am »
Ok, I have a follow up question. It doesn't concern parallax background as it's usually rectangle but what if I want to draw transparent stuff to sf::RenderTexture and then that in window? Doesn't sf::RenderTexture require me to clear it with a fixed color? Is there a way not to clear it/clear it with "transparent color"? :P

(I see there's something called sf::Color::Transparent but it's description says "transparent (black)" so it is really transparent and does it apply here?)

Edit: Ok, tests suggest sf::Color::Transparent is what I need. As for "transparent (black)", my guess is it's always transparent unless something can't be transparent (like window), then it's black. Can anyone confirm?

10
Graphics / Re: Parallax background with a couple of images
« on: January 18, 2013, 03:10:40 pm »
Well, in my case I just need a few (tens?) images and some particles to run on pc at 60 fps, so I hope (and think) sf::Sprite will be enough. Thx.

11
Graphics / Re: Parallax background with a couple of images
« on: January 18, 2013, 02:32:17 pm »
I'm assuming it's fast?

Btw finally a way to render to texture in an easy and simple way. :D

12
Graphics / Parallax background with a couple of images
« on: January 18, 2013, 02:17:27 pm »
I've implemented parallax background which arbitrary number of images that slide at different speeds and loop. I'm using setTextureRect() for that of course and since when the images loop I have to draw 2 parts of the same image I also use setPosition().

Everything works great but I thought it'd be nice to be able to rotate and scale my parallax. When I do this right now everything breaks and gets put apart. Of course, I could implement calculating new positions for different angles and scales but it would be much simpler and cleaner if I could create the parallax frame (with scale 1.0 and no rotation), copy it to a new image and then rotate and scale that image.

My 2 questions - would it be fast (pixel operations like to be very slow) and how to do that (although I might probably google that so I'm most interested in the first question)?

13
Graphics / Re: Linker warning, linker error :P
« on: January 18, 2013, 11:35:57 am »
Yeeeah, 2.0 is stable as hell. :P

14
Graphics / Re: Linker warning, linker error :P
« on: January 18, 2013, 10:41:29 am »
Ok, turns out it's a completely different problem (either I didn't notice it before, or I did have different problem then, not sure now :P). Sounds/music cause crashes at exit now, probably bug #30. So what should I do now?

15
Audio / Re: Multiple sounds don't work
« on: January 18, 2013, 09:34:47 am »
So obvious now that I know the answer. :P Thx!

Pages: [1] 2 3