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

Pages: 1 [2]
16
Graphics / Re: Vertexes are slow
« on: August 05, 2012, 01:29:31 am »
This is still bad way of doing things right?
If you join the tiles every iteration into the vertex array then yes this is still bad.

If I was not to do that I would have to keep whole map stored in vertex array at all times, which sounds like bad idea considering size of it.
But I guess that's why guy in different topic was supposed to divide map into smaller chunks.
Would that work? Like divide whole map into chunks of approximately 1/4 of display and just keep track of which chunk to display?
that would mean ~9 calls in worst case.

17
Graphics / Re: Vertexes are slow
« on: August 05, 2012, 01:17:12 am »
OK! I can't do it ;/ now I'm just pumping Vertexes representing each corner of each Tile into single VertexArray then drawing them, it boosted my fps a bit, but still is far cry from what I expected and need :F
This is still bad way of doing things right?

18
Graphics / Re: sf::Text in VS2012RC
« on: August 04, 2012, 10:08:27 pm »
No I mean the one that I was using before today, since I had to get new dlls for new VS I just recompiled it

19
Graphics / Re: sf::Text in VS2012RC
« on: August 04, 2012, 09:28:15 pm »
I need to start using search before posting :( guess my previous build of SFML used for vs10 wasn't up to date

20
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

21
Graphics / Re: Vertexes are slow
« on: August 04, 2012, 08:13:00 am »
I thought that might be the case :F

Regarding iterating order - it's going to be huge problem for me. Algorithm I've to "pick tiles to display" requires to go though x first, but I'll look into other solutions.

I will work on these stuff and report back should I encounter more troubles

22
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;
};
 

23
General / Re: Maximized Window causes fps drop
« on: August 03, 2012, 07:32:46 am »
I'm not used to use profilers but from what it shown me I gather in fault here is ntdll.dll

24
General / Re: Maximized Window causes fps drop
« on: August 03, 2012, 07:01:57 am »
Logic behind putting second thread there was to gain 3000 fps which is lost just by having event pulling in same loop as drawing :)
Effect is nearly identical with this code, although drop is from around 1000 to 200 on my machine.
But I see your point, it might be OS not SFML :)
#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window;

        window.create(sf::VideoMode(1680, 1050),"");
       
        window.setVerticalSyncEnabled(false);
       
        int fps = 0;
        sf::Text fpsText;
        sf::Clock clk;
        std::string str;
        char temp[20];

        while(window.isOpen())
        {
                sf::Event event;
                while(window.pollEvent(event));//needed to maximize

                fps = 1/clk.getElapsedTime().asSeconds();
                       
                clk.restart();
                       
                sprintf(temp, "%d", fps);
                str = temp;
                fpsText.setString(str);
       
                window.clear();
                window.draw(fpsText);
                window.display();
               
        }

        return 0;
}

25
General / Re: Problem with sf::Clock
« on: August 03, 2012, 06:31:42 am »
Why would you wait till everything is drawn and displayed before you reset the clock? If you're not doing much in the rest of the program, could it possibly be that this part consumes most of the time?

That could mean that something like this happens:
getElapsedTime()
drawing & displaying
clock.reset()
insignificant other stuff
getElapsedTime()
...

Now given that you're not doing much else you'd be practically resetting "right before" getting the time again, and therefore it will take a while for frameTime to become >= 1 seconds.

Does that even make sense? It's four in the morning right now.  ;D

I second this! This is exactly the same conclusion I just got to independently testing something else:) Well in my case I got nearly infinite(out of scale) fps, but that's just difference in way we calculate it
Just reset clock just after you've read time and it should work

26
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;
}

27
General / Re: Tile Map engine - tearing problem
« on: July 30, 2012, 02:08:11 am »
From a supporter perspective I have to give you a big thanks! ;D
Most of the time when I/we request a full minimal example the troubled user will find all sorts of excuses not to provide such a minimal example, but you did without asking why/how/what/etc. Thanks! :)
Well it's only logical that if I want to get helped I have to play by your rules :)

As to rest of your post :
  • "Framerate movement" -  Yes I am aware of that, just didn't bother just for sake of sample, even more since it still is not implemented in general project :)
  • That's news to me, I could have sworn they were atomic, but since I'm beginner I will have to believe you
  • I see, It seemed like good idea at time, since that's what I had been doing in my first small project in SDL. Will implement
  • That seems to be useful feature, will try to use that instead of my wanna be camera

Points taken thanks a lot.

And indeed putting sf::Keyboard::isKeyPressed() for camera movement in the same thread as drawing eliminated problem. Should have thought of that myself :-[

So I guess I should run all input that directly changes drawing place in same thread as Draw(), and all logic as what is happening over time/animations/"Computer Player Logic" in different thread?

I will toy with vertexes, Views and such tomorrow since it's kinda late already :)
Thanks again for help

28
General / Re: Tile Map engine - tearing problem
« on: July 29, 2012, 11:58:45 pm »
Ok I will look into Vertexes :) However I'm afraid problem doesn't lay there and it might not work. Whole idea is not to draw whole map but only visible part :) Regardless I'll look into it.
As for screen shots and minimal sample that, That's all I can make :( any less and it wouldn't replicate the problem.

Screens

To run this code you will need This tile set image
#include <SFML/Graphics.hpp>

sf::RenderWindow window;
sf::Sprite tile;
sf::Texture tileTexture;

int CamX = -400;
int CamY = -100;

sf::Vector2i IndxToIso(int x, int y)
{
        sf::Vector2i temp;
        //hardcoded valuse, TileSep X and Y, increse to create spacing between tiles
        temp.x = (x-y) * 32;
        temp.y = (x+y) * 16;

        return temp;
};

void Draw();

int main()
{
        //initialize window and all that junk
        tileTexture.loadFromFile("part4_tileset.png");
        tile.setTexture(tileTexture);

        window.create(sf::VideoMode(800, 600),"");
        window.setActive(false);

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

        //siplificed Controllset
        bool KeyPressed[4] = {false, false, false, false};

        //GameLoop
        while(window.isOpen())
        {
                //Event Handling
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::KeyPressed)
                        {
                                if(event.key.code == sf::Keyboard::Left)
                                        KeyPressed[0] = true;
                                if(event.key.code == sf::Keyboard::Right)
                                        KeyPressed[1] = true;
                                if(event.key.code == sf::Keyboard::Up)
                                        KeyPressed[2] = true;
                                if(event.key.code == sf::Keyboard::Down)
                                        KeyPressed[3] = true;
                        }
                        if(event.type == sf::Event::KeyReleased)
                        {
                                if(event.key.code == sf::Keyboard::Left)
                                        KeyPressed[0] = false;
                                if(event.key.code == sf::Keyboard::Right)
                                        KeyPressed[1] = false;
                                if(event.key.code == sf::Keyboard::Up)
                                        KeyPressed[2] = false;
                                if(event.key.code == sf::Keyboard::Down)
                                        KeyPressed[3] = false;
                        }
                }

                //Game logic Update!
                if(KeyPressed[0])
                        CamX--;
                if(KeyPressed[1])
                        CamX++;
                if(KeyPressed[2])
                        CamY--;
                if(KeyPressed[3])
                        CamY++;
               
        }
}

void Draw()
{
        window.setActive(true);
        window.setVerticalSyncEnabled(false);
       
        while(window.isOpen())
        {
                window.clear();

                //drawing loops
                for(int y=0; y<20; y++)
                {
                        for(int x=0; x<20; x++)
                        {
                                //position where tile will be drawn based on index
                                sf::Vector2i position = IndxToIso(x, y);
                                //set rect on TileSet img to draw certain tile (size of single tile img)
                                tile.setTextureRect(sf::Rect<int>(0,0,64,64));
                                //set position of tile
                                tile.setPosition(position.x-CamX, position.y-CamY);

                                window.draw(tile);
                        }
                }
                window.display();
        }
};
 

29
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 [2]