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.


Topics - lrx

Pages: [1]
1
General / window event Pulling - draw relation (2 threads)
« on: May 12, 2013, 02:10:02 am »
Hello
Settings:
I've dug out my old code and decided to give multithreaded game engine approach a shot.
The idea is to run drawing loop and update+event loop in separate threads.

drawing is done in "new thread" while event pulling and updating is supposed to run in main thread.

Problem:
Why does my drawing loop slow down aprox. 50 times if I don't put update/event loop to sleep for a short duration?
Since the two should run independently of each other (no communication is done yet, no synchronization, nothing) ran on multi-core cpu, the only connection between these loops is that I draw to same RenderWindow as I pull events from.

In the end of the day I do plan to limit how many times a sec each of these loops executes, but that doesn't change fact that I wonder how they influence each other or if my though flow is flawed :P


Fairly minimal example
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>

sf::RenderWindow window;
sf::View Camera;

void Draw()
{
        //time and fps variables
        int counter = 0;
        int fps = 0;
        sf::Text fpsText;
        sf::Clock clk;
        sf::Font font;
        sf::Time fpsTime = sf::Time::Zero;
        std::stringstream ss;

        font.loadFromFile("corbel.ttf");
        fpsText.setFont(font);

        //samething to draw
        sf::VertexArray va;
        va.setPrimitiveType(sf::Triangles);
        va.append(sf::Vector2f(10, 10));
        va.append(sf::Vector2f(100, 10));
        va.append(sf::Vector2f(100, 100));
        va[0].color = sf::Color::Red;
        va[1].color = sf::Color::Blue;
        va[2].color = sf::Color::Green;

        window.setActive(true);

        while(window.isOpen())
        {                              
                window.setView(Camera);

                window.clear();
                window.draw(va); //drawing of everything here

                window.setView(window.getDefaultView());

                //fps
                counter++;             
                fpsTime+=clk.restart();;
                if(fpsTime.asSeconds()>=1)
                {
                        fps = (int)(counter/fpsTime.asSeconds());
                        counter=0;
                        fpsTime = sf::Time::Zero;
                        ss.str(std::string());
                        ss.clear();
                        ss<<fps;
                        fpsText.setString(ss.str());
                }
                window.draw(fpsText);
                window.display();
        }
};


int main()
{      
        window.create(sf::VideoMode(1680, 1050),"");
        window.setVerticalSyncEnabled(false);
        window.setActive(false);

        //drawing thread start
        sf::Thread RenderThread(Draw);
        RenderThread.launch();

        //GameLoop
        while(window.isOpen())
        {
                //Event Handling
                sf::Event event;
                while(window.pollEvent(event))
                {
                        switch (event.type)
                        {
                                case sf::Event::Closed:
                                        window.close();
                                        RenderThread.wait();
                                        break;
                                case sf::Event::KeyPressed:    
                                {
                                        switch (event.key.code)
                                        {
                                                case sf::Keyboard::Escape:
                                                        window.close();
                                                        RenderThread.wait();
                                                        break;
                                                //more key events here
                                        default:
                                                break;
                                        }
                                        break;
                                }
                                default:
                                        break;
                        }
                }

                //update here

                //sleep to allow other thread to work
                sf::sleep(sf::milliseconds(3));
        }
}
 

2
Network / Networking tutorial?
« on: August 11, 2012, 06:33:37 am »
Hello,

I want to take brief break from all that tiles I've been drawing past days and learn some basics about network programming. I threw topic into google and found people recommending to skip Winsock and move straight away to some other api, They actually mentioned SFML as first choice for beginners. But there not being any up to date tutorial, I have to wonder, if there are few enough changes to go with 1.6 tutorial and adapt it to 2.0 on fly or go hardcore to boost asio or yet something else? What do you think?

3
General / threads, events and windows
« on: August 05, 2012, 06:56:55 pm »
Hello
Again toying with fresh built sfml2rc I've encountered strange problem that was non existing in previous built(RC built on April 12th).
It might not be best practice but nonetheless this is what I am using at the moment:
Thread one - event pulling;
Thread two - drawing and calculating;
with old build of sfml everything ran smooth and fast. However with fresh one it seems like window is not "in focus" by default or something of sort and I have to click/hold on Title bar which will boost fps. After that clicking back and forth on different windows(chrome/VS) causes similar thing, although clinking away from window boosts fps and clicking into Drawing area of render window slaps it down.
this happens in "SFML-2.0-rc-69-ge4ea686" build on W7x64, in both VS10 and VS11 each with their own built ddls, most recent nVidia WHQL drivers. I can not confirm which release is the one that works fine, only date it was build on(and downloaded) - April 12th.

This does not happen if i put drawing in same thread as event pulling

Problem occurs with this code
#include <SFML/Graphics.hpp>

void Draw();

sf::RenderWindow window;

int main()
{      

        //Initialize window
        window.create(sf::VideoMode(1680, 1050),"");
        window.setActive(false);

        //thread for draw function
        sf::Thread RenderThread(&Draw);
        RenderThread.launch();

        //GameLoop
        while(window.isOpen())
        {
                //Event Handling
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                        if(event.type == sf::Event::KeyPressed)
                        {
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                                        window.close();
                        }
                }
        }
        return 0;
}

void Draw()
{
        int counter = 0;
        int fps = 0;
        sf::Text fpsText;
        sf::Clock clk;
        sf::Font font;
        font.loadFromFile("corbel.ttf");
        fpsText.setFont(font);

        window.setActive(true);
        window.setVerticalSyncEnabled(false);
       
        while(window.isOpen())
        {
                counter++;
                if(clk.getElapsedTime().asSeconds()>=1)
                {
                        fps = counter/clk.getElapsedTime().asSeconds();
                        counter=0;
                        clk.restart();
                        std::string sss;
                        char tempB[20];
                        sprintf(tempB, "%d", fps);
                        sss = tempB;
                        fpsText.setString(sss);
                }
               
                window.clear();
                window.draw(fpsText);
                window.display();
        }
};
 

I know I am asking a lot of questions, but this really does bother me.

4
Graphics / sf::Text in VS2012RC
« on: August 04, 2012, 09:15:11 pm »
Hello
I believe I've found bug, either in sfml or vs. Regardless following statement from documentation is no longer true.
Quote
Note that you don't need to load a font to draw text, SFML comes with a built-in font that is implicitely used by default.
Is no longer true, and to display any text you indeed have to load font first.

VS 2012 RC
SFML2 RC built for above

5
Graphics / Vertexes are slow
« on: August 04, 2012, 06:54:56 am »
Hello again

I was advised to use vertexes and views in my Tile Map engine(bleh again I know). Reading this forum I got impression that they are supposed to be fast. So I've written sample code to test this and I can not get decent fps and using View causes horizontal lines disorder on image. Possibly I don't understand how to use Vertexes. I am trying to get 300fps+ on 1680x1050 resolution which means I have to display around 360 tiles each frame + possible other objects later one.
 Here is what I'm doing:
1. setup 2d vector of VertexArrays as sf::Quads
2. ONCE for each Tilemap[][] setup 4 vertexes(position and texture coords)
3. in main loop go though all Tilemap[][] and call draw(Tilemap
  • [y], &texture);


Resulting in around 30 fps, which is by far not enough, especially if I wanted to add more sprites on top of that later on.

Am I doing it wrong or It just has to be this way? Maybe using this method would be faster?

#include <SFML/Graphics.hpp>

#define TILE_IMG_W 64
#define TILE_IMG_H 64

//function declarations
//Getting rect defining part of texture to use
sf::Rect<int> GetSourceRectangle(int tileIndex);
//converting index numbers to Draw position
sf::Vector2i IndxToIso(int x, int y);

void Draw();
//Preparing Map
void init();

sf::RenderWindow window;
sf::Texture tileTexture;
sf::View* Cam;

std::vector< std::vector< sf::VertexArray > > Tilemap;

//MapSize = size*size
int size = 50;

int main()
{      
        Cam = new sf::View(sf::FloatRect(0, 0, 1680, 1050));
       
        //Load Texture
        tileTexture.loadFromFile("part4_tileset.png");
       
        //Initialize window
        window.create(sf::VideoMode(1680, 1050),"");
        window.setActive(false);

        //Setup map,
        init();

        //thread for draw function
        sf::Thread RenderThread(&Draw);
        RenderThread.launch();

        //GameLoop
        while(window.isOpen())
        {
                //Event Handling
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                        if(event.type == sf::Event::KeyPressed)
                        {
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                                        window.close();
                        }
                       
                }

        }
        return 0;
}

void Draw()
{
        int counter = 0;
        int fps = 0;
        sf::Text fpsText;
        sf::Clock clk;

        window.setActive(true);
        window.setVerticalSyncEnabled(false);

        while(window.isOpen())
        {
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        Cam->move(-10, 0);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        Cam->move(10, 0);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        Cam->move(0, -10);
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        Cam->move(0, 10);

                window.setView(*Cam);
                window.clear();

                //Drawing loops
                for(int y=0; y<size; y++)
                {
                        for(int x=0; x<size; x++)
                        {
                                window.draw(Tilemap[x][y], &tileTexture);
                        }
                }

                //fps
                counter++;
                if(clk.getElapsedTime().asSeconds()>=1)
                {
                        fps = counter/clk.getElapsedTime().asSeconds();
                        counter=0;
                        clk.restart();
                        std::string sss;
                        char tempB[20];
                        sprintf(tempB, "%d", fps);
                        sss = tempB;
                        fpsText.setString(sss);
                }
               
                window.setView(window.getDefaultView());
                window.draw(fpsText);
                window.display();
        }
};

void init()
{
        //Temp Quad
        sf::Vertex vertices[4];
       
        //preparing vectors
        Tilemap.resize(size);
        for(int i=0; i<size; i++)
                Tilemap[i].resize(size);
       
        //setting VertexArrays for each tle
        for(int y=0; y<size; y++)
        {
                for(int x=0; x<size; x++)
                {
                        sf::Vector2i position = IndxToIso(x, y);
                        sf::Rect<int> TexPos = GetSourceRectangle(0);

                        Tilemap[x][y].setPrimitiveType(sf::Quads);

                        Tilemap[x][y].append(sf::Vertex(sf::Vector2f(position.x, position.y), sf::Vector2f(TexPos.left, TexPos.top)));
                        Tilemap[x][y].append(sf::Vertex(sf::Vector2f(position.x + TILE_IMG_W, position.y), sf::Vector2f(TexPos.left + TexPos.width, TexPos.top)));
                        Tilemap[x][y].append(sf::Vertex(sf::Vector2f(position.x + TILE_IMG_W, position.y + TILE_IMG_H), sf::Vector2f(TexPos.left + TexPos.width, TexPos.top + TexPos.height)));
                        Tilemap[x][y].append(sf::Vertex(sf::Vector2f(position.x, position.y + TILE_IMG_H), sf::Vector2f(TexPos.left, TexPos.top + TexPos.height)));
                }
        }                                      
};

sf::Rect<int> GetSourceRectangle(int tileIndex)
{
        sf::Vector2u TileSetSize = tileTexture.getSize();
        int tileY = tileIndex / (TileSetSize.x / TILE_IMG_W);
        int tileX = tileIndex % (TileSetSize.x / TILE_IMG_W);

        sf::Rect<int> tempR(tileX * TILE_IMG_W, tileY * TILE_IMG_H, TILE_IMG_W, TILE_IMG_H);
        return tempR;
};

sf::Vector2i IndxToIso(int x, int y)
{
        sf::Vector2i temp;

        temp.x = (x-y) * 33;
        temp.y = (x+y) * 17;

        return temp;
};
 

6
General / Maximized Window causes fps drop
« on: August 03, 2012, 06:26:27 am »
Hello
While working on my project I've encountered strange phenomenon.

With almost empty window I get few thousands of fps, however the very moment I maximize it, fps drops to around 200. I find this to be awkward and disturbing. Is there any particular reason for this happening?

W7 x64
VS2010
SFML2 RC
Nvidia

#include <SFML/Graphics.hpp>
#include <iostream>

void Draw(sf::RenderWindow* win)
{
        win->setActive(true);

        int fps = 0;
        sf::Text fpsText;
        sf::Clock clk;
        std::string str;
        char temp[20];

        while(win->isOpen())
        {
                fps = 1/clk.getElapsedTime().asSeconds();
                       
                clk.restart();
                       
                sprintf(temp, "%d", fps);
                str = temp;
                fpsText.setString(str);
       
                win->clear();
                win->draw(fpsText);
                win->display();
        }
};

int main()
{
        sf::RenderWindow window;

        window.create(sf::VideoMode(1680, 1050),"");
       
        window.setVerticalSyncEnabled(false);
       
        //Drawing thread
        window.setActive(false);
        sf::Thread eventPull(&Draw, &window);
        eventPull.launch();
       
       
        while(window.isOpen())
        {
                sf::Event event;
                while(window.pollEvent(event)); //needed to maximize
        }

        return 0;
}

7
General / Tile Map engine - tearing problem
« on: July 29, 2012, 08:06:54 pm »
Hello there!

I've been writing simple tile map engine for fun and learning purposes. I's been going well till I decided to walk off path pointed by tutorial I've been using and went and made diamond shaped map instead of staggered one. Some google'ing gave me decent code to base on. It's all working well aside from fact that during scrolling(up and right mostly) there are spaces between tiles. They however are never there when scrolling stops.
Problem persists regardless of draw() method being ran in different thread or at the end of "game loop".
Same deal with Vsynch on and off.
I feel there is no need to post whole code as it would be just too much so I'll include only drawing and event handling(game) methods.
I've tried to clean up code a bit and add comments every now and then, there is a lot of temporary code, flagged with //temp comment or just empty one //

All help and tips and constructive critique will be appreciated. If it turns out whole code is needed let me know  and I will include it.

Since its missing from included code and is not obviously named, "test" is sf::RectangleShape and is used to imitate window bounds to see how many tiles are drawn out of it.

Language: c++
SFML: 2.0 RC

void Game::Draw()
{
        //temp fps
        int counter=0;
        int fps3 = 0;
        sf::Text fpsText;
        sf::Clock clk;
        //
        window.setActive(true);
        window.setVerticalSyncEnabled(false);
       
        //first tiles to draw
        int startX, startY;
        //futherest tiles to draw
        int maxX, maxY;
        //minimum X index to draw
        int minX;
        //check if futherest and min tiles were drawn
        bool mGotXmax;
        bool nGotXmin;

        //increse before reaching min/max, decrese after(bump)
        int nBuff, mBuff;
        //delay
        int n,m;


        while(window.isOpen())
        {
                window.clear();
                //initial values for every run
                if(tempDrawChange==1)
                {
                        //Fullscreen Calc here
                        minX = IndxFromIso(Cam->CameraPos.x, Cam->CameraPos.y).x-1;
                        maxX = IndxFromIso(Cam->CameraPos.x+window.getSize().x, Cam->CameraPos.y+window.getSize().y).x + 2;

                        maxY = IndxFromIso(Cam->CameraPos.x, Cam->CameraPos.y+window.getSize().y).y + 2;

                        startX = IndxFromIso(Cam->CameraPos.x+window.getSize().x, Cam->CameraPos.y).x+1;
                        startY = IndxFromIso(Cam->CameraPos.x+window.getSize().x, Cam->CameraPos.y).y;
                }
                else
                {
                        //rect calc here
                        minX = IndxFromIso(Cam->CameraPos.x+test.getPosition().x, Cam->CameraPos.y+test.getPosition().y).x-1;
                        maxX = IndxFromIso(Cam->CameraPos.x+test.getPosition().x+test.getSize().x, Cam->CameraPos.y+test.getPosition().y+test.getSize().y).x+2;

                        maxY = IndxFromIso(Cam->CameraPos.x+test.getPosition().x, Cam->CameraPos.y+test.getPosition().y+test.getSize().y).y+2;

                        startX = IndxFromIso(Cam->CameraPos.x+test.getPosition().x+test.getSize().x, Cam->CameraPos.y+test.getPosition().y).x+1;
                        startY = IndxFromIso(Cam->CameraPos.x+test.getPosition().x+test.getSize().x, Cam->CameraPos.y+test.getPosition().y).y;
                }
               
                nBuff = 1;
                mBuff = 1;

                mGotXmax = false;
                nGotXmin = false;

                n=0;
                m=3;

                //drawing loops
                for(int y=startY; y<maxY; y++)
                {
                        for(int x=startX-nBuff; x<startX+mBuff; x++)
                        {
                                //Drawing, multiple layers
                                if(x>0 && x< Map->MapW-1 && y>0 && y< Map->MapH-1)
                                {
                                        for(int i=0; i<Map->MapGrid[x][y].TileID.size(); i++)
                                        {
                                                sf::Vector2i position = IndxToIso(x, y);

                                                TileSprite.setTextureRect(TileSet.GetSourceRectangle(Map->MapGrid[x][y].TileID[i]));
                                                TileSprite.setPosition(position.x-Cam->CameraPos.x, position.y-Cam->CameraPos.y);
                                        }
                                }

                                window.draw(TileSprite);
                        }
                        //bump mechanics
                        if(startX+mBuff==maxX)
                                mGotXmax = true;
                        if(startX-nBuff==minX)
                                nGotXmin = true;

                        if(mGotXmax)
                        {
                                if(m>0)
                                        m--;
                                else
                                        mBuff--;
                        }
                        else
                        {
                                mBuff++;
                        }

                        if(nGotXmin)
                        {
                                if(n>0)
                                        n--;
                                else
                                        nBuff--;
                        }
                        else
                        {
                                nBuff++;
                        }
                }
                //fps temp
                counter++;
                if(clk.getElapsedTime().asSeconds()>=1)
                {
                        fps3 = counter/clk.getElapsedTime().asSeconds();
                        counter=0;
                        clk.restart();
                        std::string sss;
                        char tempB[20];
                        sprintf(tempB, "%d", fps3);
                        sss = tempB;
                        fpsText.setString(sss);
                }
                //

                window.draw(fpsText);
                window.draw(test);
                window.display();
        }
};

void Game::Run()
{
        //temp pressed time
        bool a = true;
        sf::Clock clk;
        //

        window.setActive(false);

        //new thread for draw function
        sf::Thread RenderThread(&Game::Draw, this);
        RenderThread.launch();
       
        while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
                        if(event.type == sf::Event::KeyPressed)
                        {
                                controls.KeyPressed[event.key.code] = true;

                                //temp pressed time
                                if(a)
                                {
                                        a = false;
                                        clk.restart();
                                }
                        }
                        if(event.type == sf::Event::KeyReleased)
                        {
                                controls.KeyPressed[event.key.code] = false;
                               
                                //temp pressed time
                                std::cout<<"Pressed for "<<clk.getElapsedTime().asSeconds()<<std::endl;
                                a = true;

                                //temp draw mode
                                if(event.key.code == sf::Keyboard::F1)
                                        tempDrawChange*=-1;
                        }
                        if(event.type == sf::Event::MouseButtonPressed)
                        {
                                //temp for debug
                                sf::Vector2i index = IndxFromIso(event.mouseButton.x+Cam->CameraPos.x, event.mouseButton.y+Cam->CameraPos.y);
                                std::cout<<"Index: "<<index.x<<" "<<index.y<<std::endl;
                                std::cout<<"Mouse: "<<event.mouseButton.x<<" | "<<event.mouseButton.y<<std::endl;
                                std::cout<<"Camera: "<<Cam->CameraPos.x<<" "<<Cam->CameraPos.y<<std::endl;
                                std::cout<<"M-C: "<<event.mouseButton.x+Cam->CameraPos.x<<" "<<event.mouseButton.y+Cam->CameraPos.y<<std::endl<<std::endl;
                        }
        }

                if(controls.isPressed(sf::Keyboard::Escape))
                        window.close();

                if(controls.isPressed(sf::Keyboard::Right))
                {
                        Cam->CameraPos.x++;
                }
                if(controls.isPressed(sf::Keyboard::Left))
                {
                        Cam->CameraPos.x--;
                }
                if(controls.isPressed(sf::Keyboard::Down))
                {
                        Cam->CameraPos.y++;
                }
                if(controls.isPressed(sf::Keyboard::Up))
                {
                        Cam->CameraPos.y--;
                }
    }
};
 

Pages: [1]
anything