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 - Chunker120

Pages: 1 [2] 3
16
This code is not complete nor minimal. Please read this: http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368

A typical minimal example for this kind of error would be the following:

#include <thread>

void f(int) {}

int main()
{
    std::thread t(f, 5);
    return 0;
}

Well it's not complete because the rest doesn't matter, it's just a bunch of functions for handling input and game logic, and since it's a large game it would take up a lot of useless space, it's the only code that can be troubled, the rest has absolutely no issues which is why I didn't want to bother you with it. Here's another attempt, though the code above basically contains the same thing. Also I can't post the whole source code to make the code runable, I have 14 files, and using all of them in the same file where this problem occurs. This is basically the only line of code that can possibly cause this issue.

#include <functional>
#include <thread>

void loadWorld(sf::RenderWindow &winMain) {};

int main()
{
        sf::RenderWindow winMain;
        std::thread loadingThread(loadWorld, winMain);

        winMain.setSize(sf::Vector2u(SCREEN_W, SCREEN_H));
        winMain.setTitle("Council of Torment");
       
        icon.loadFromFile("resources\\icon.png");
        winMain.setIcon(50, 50, icon.getPixelsPtr());
        winMain.setFramerateLimit(60);

        loadingThread.join();
}

I'm sorry if this wasn't good either, but it's the only piece of code that can actually have an error, there's no point in showing anything more or less, because it's just two lines of code, which cause the error. Replacing std::Thread with sf::Thread would probably fix it, since that's what I've been using up to the point when I heard std::thread is better.

But supposing the code is not good, how would you make a thread that runs my kind of function?

17
Can you show a complete and minimal code that reproduces this problem, instead of isolated fragments?

The code is very confusing (I am not a clean code person), but this is basically the structure:

Game.cpp

Game mygame;

void loadWorld(sf::RenderWindow &winMain)
{
      // some stuff gets initialized here, I need the renderwindow paramater to calculate some camera bounds
}

// handle input and render the stuff here.

int main()
{
        sf::RenderWindow winMain;
        std::thread loadingThread(loadWorld, winMain);

        winMain.setSize(sf::Vector2u(SCREEN_W, SCREEN_H));
        winMain.setTitle("My game");
        mygame.paused = false;

        sf::Image icon;
        sf::Clock clock;

        LoadingScreen = new loadingScreen();
        GameState = new gameState(LOADING_SCREEN);
       
        icon.loadFromFile("resources\\icon.png");
        winMain.setIcon(50, 50, icon.getPixelsPtr());
        winMain.setFramerateLimit(60);
        LoadingScreen -> loadingContext = IN_GAME;

        loadingThread.join();

        while(winMain.isOpen())
        {
                sf::Time dt = clock.restart();
                mygame.handleInput(winMain);
                mygame.gameLogic(winMain);
        }
}

The game is basically a class, to encapsulate everything and keep the main function isolated. Everything worked fine when I used a thread before, but main was in its own file, and the renderwindow was global so I was calling extern sf::RenderWindow winMain instead of just passing it as an argument in my methods.

18
You would have to wrap winMain into a std::ref so that it is passed by reference and not copied internally by the thread, but according to the error message this is not your problem. What's your compiler?
Latest version of mingw, it should work fine

19
General / Can't figure out how to work with std::thread and SFML
« on: May 30, 2013, 07:14:13 pm »
In a previous thread I asked for some help and was told to use std::thread.

I basically declared a function:

void initialize(sf::RenderWindow &winMain);

...

int main()
{
    sf::RenderWindow winMain;
    std::thread loadingThread(initialize, winMain);
 
     ...
 
     loadingThread.join();
}

But it doesn't work. I just want it to run a thread with a function that needs my renderwindow passed as a paramater to it. The error:

Error   1   error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments

What could I be doing wrong? I'm just confused. I'm sorry I couldn't show the full source code, but it would just confuse you further.

If something is fundamentally wrong, please tell me, how would I make a thread that uses a function declared in the same file which has a pointer paramater?

20
If you want maximum compatibility (i.e. you want your code to compile also on C++03 platforms), then don't use C++11 at all. std::bind is a C++11 function.

Note that this is just about compiling your app on other platforms, not running it. Once it's compiled it can run everywhere.

Well let's say I'll ditch the whole C++11 aspect of it, how would I use sf::Thread in this context, regarding my initial question?

21
If you use C++11 you should prefer std::thread.
Yet you did mention that using sfml's thread is a better choice if you want to distribute your code and/or run it on more platforms. My game does indeed use C++11, but I don't know what to pick. Is it fully safe to use std::thread platform-wise?

22
Let's say I have a class called Game, and a public method that looks like this:

void Game::initialize(sf::RenderWindow &winMain);

How would I go about creating a thread that uses this function? I tried these but it didn't work:

sf::Thread loadingThread(&Game::loadWorld, &myGame, winMain);
sf::Thread loadingThread(std::bind(&Game::loadWorld, winMain), &myGame);

23
I'm a windows user and still learning so I couldn't say for sure what your problem is, but have you tried using an OpenGL window instead to see if that changes anything? If it doesn't, it might be something to do with the way your code is put together and that would need someone more experienced to have a look.

I have noticed in general games just don't like alt-tabbing a lot, especially if they're in fullscreen, that's why developers often offer up windowed modes.

I really have no knowledge in OpenGL. if I did have any openGL knowledge I probably wouldn't be using SFML, which makes everything much simpler for me. :P

The code structure is pretty good, but it's this window problem that's causing me problems.

24
I am making a game, and I noticed that it has some weird issues. For example, when I switch to the game window I can't switch to another window, almost as if it's stuck (The game is running smooth though). And sometimes, when using ALT+TAB to switch betwen windows, the game crashes and says abort() was called.

In another linux thread I heard that using a global sf::RenderWindow is very bad, and I wanted to fix it however I have been using this as a design choice for a while. Here's how my code is currently structured:

I declare a render window in main.cpp and wherever I need it I just call extern MyWindow and use the render window rather than using it as a function argument, mostly because it looks cleaner.

Is there a simpler way to fix this flaw? Is it the real cause of my problem?

25
General / Re: SFML how to handle large spritesheets?
« on: May 18, 2013, 03:41:46 pm »
No, the performance drop shouldn't be because of the spritesheets. After all, OpenGL holds the texture in memory anway, you just use different texture coordinates.

If the limiting factor are the number of draw calls, an optimization would be to use vertex arrays instead of sprites. But before, you should profile the application to see the real bottlenecks.

I see, I was mainly asking this because I was getting 65 FPS before, and 63 FPS after adding a few humans in-game. Perhaps it's the architecture of the class rather than the spritesheet.

26
General / SFML how to handle large spritesheets?
« on: May 18, 2013, 03:00:56 pm »
I am working on a basic game with NPCs, and each NPC has a resolution of 120x1030, it holds all animations in a single file. Each animation has 4 frames and I just add them under each other.

The problem is, I have noticed some small performance drops (Got a pretty weak computer) when trying to use sprites with the spritesheet (I have a sf::IntRect set up, so it only displays a portion of the sprite).

Is the small performance drop because of the large spritesheets? If so, can I do anything to optimize it? (Putting them outside the spritesheet is not an option because I need a lot of different humans)

27
General / How to flip sprite horizontal?
« on: May 12, 2013, 04:44:07 pm »
I have an animation in a top-down perspective, but I want the sprite to get flipped horizontal if the player faces east. The sprite class doesn't by default have any methods that would help me achieve that, so how would I go around flipping a sprite horizontally?

28
It works, no conversion, nothing, all I had to do was call window.setView(camera);

Thanks!

29
What you want is the other one (since you want to map sprite coords to window pixels -- mapCoordsToPixel). But why don't you use a second view instead? Call window.setView(wndow.getDefaultView()) before drawing your sprite.

And... there's no tutorial, so I don't know what you want me to update ;)

Initially I was referring to the 1.6 tutorial, but the ones on the internet also tell to use convertCoords, not to mention there's a convertCoords in the 2.0 documentation that also gives a very small brief, I was pretty confused, having used that thing before. I'll try the other one though and tell you if that works.

30
I've built SFML myself using the latest snapshot, everything works fine, however I noticed that there's no longer a convertCoords option, and instead there are some mapCoordinatesToPixels method and another one, every source tells me to use convertcoords which no longer exists, the tutorials should really be updated but what I'm really here for is to find out how to actually convert coords in the latest snapshot?

All I want is to render a camera and a sprite, and I don't want the sprite to move with the camera. Here's my code:

#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow winMain(sf::VideoMode(500, 500, 32), "Title");
        sf::View camera = winMain.getDefaultView();
        sf::Event mainEvent;

        sf::Texture logo_tex;
        logo_tex.loadFromFile("textlogo.png");
        sf::Sprite logo_spr(logo_tex);
        logo_spr.setPosition(350, 350);

        winMain.setView(camera);

        while(winMain.isOpen())
        {
                winMain.mapPixelToCoords(logo_spr.getPosition());

                while(winMain.pollEvent(mainEvent))
                {
                        if(mainEvent.type == sf::Event::Closed)
                                winMain.close();

                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) camera.move(1, 0);
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) camera.move(-1, 0);
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) camera.move(0, -1);
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) camera.move(0, 1);
                }

                winMain.clear();
                winMain.draw(logo_spr);
                winMain.display();
        }
}

However I'm getting an error when trying to convert the coords, it tells me the paramater is vector2f and not vector2i.

Any ideas?

Pages: 1 [2] 3