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

Author Topic: Draw background sprite is very slow  (Read 5999 times)

0 Members and 1 Guest are viewing this topic.

ciacioz

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Draw background sprite is very slow
« on: December 20, 2013, 08:22:14 pm »
Hi, I have a game with resolution of 1280x720 but my gfx card cant create a texture directly with that size so I'm   creating 4 textures and draw them.
The problem is that draw that sprites is really slow, about 6/7 FPS.
Could someone tell me if I'm doing something wrong?

Thanks in advance, here's the code:


public void ShowLocation(Location Location)
        {
            if (_sprites.Count == 0)
            {
                Stream stream = new MemoryStream(Location.Background.Bytes);

                for (Int32 y = 0; y < _heigth; y += _heigth / 2)
                {
                    for (Int32 x = 0; x < _width; x += _width / 2)
                    {
                        IntRect view = new IntRect(x, y, _width / 2, _heigth / 2);                        
                        Texture texture = new Texture(stream, view);
                        Sprite sprite = new Sprite(texture);
                        sprite.Position = new Vector2f((float)x, (float)y);                        
                        _textures.Add(texture);
                        _sprites.Add(sprite);                          
                    }
                }

                stream.Dispose();
            }

            foreach (Sprite s in _sprites)
            {
                _window.Draw(s);                
            }
        }

 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Draw background sprite is very slow
« Reply #1 on: December 20, 2013, 08:35:52 pm »
First of all if your graphics card can't even handle 1280x1280 textures then it must be pretty old, so don't expect the best performance.

Second reread the tutorials about loading of textures. Loading textures each frame will really kill performance.

And third, you should explain how you are measuring FPS.
« Last Edit: December 20, 2013, 08:39:01 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

ciacioz

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Draw background sprite is very slow
« Reply #2 on: December 20, 2013, 08:41:24 pm »
I know my graphycs card is "pretty" old but I dont understand why with 4 sprite/texture the FPS drops this way.

The creation of the textures is done only the first time I load the background because I store the four texture in a list and from the second time when I display the background I use the spretes/texture stored in the list so I don't recreate them.

Is there a better way than my method that a I could use?

 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Draw background sprite is very slow
« Reply #3 on: December 20, 2013, 08:44:40 pm »
How are you measuring FPS? Also make sure your graphics driver is up to date.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

ciacioz

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Draw background sprite is very slow
« Reply #4 on: December 21, 2013, 12:57:09 am »
The Driver is up to date, I already check for update and try to uninstall current driver and installing the last one from AMD site.
The strangest thing is that the performance are very slow even for a gfx card like that, I play game like batman arkham asylum and nverwinter mmorpg so it seems strange that I could not handle 4 texture.

this is the code of my game loop where I count the FPS, the method is simple, every second I count how many loop I've made.

            DateTime start = DateTime.Now;
            Int32 countFps = 0;
            Int32 maxFPS = 0;          

            while (!_quit)
            {                
                _presentationManager.DispatchEvents();
                _presentationManager.Clear();
               
                //This is the function that render background image. Without this the FPS are about 100
                _presentationManager.ShowLocation(_currentLocation);                

                UpdateMainActor();                

                //Render the player
                _presentationManager.ShowActor(_mainActor);
               
                _presentationManager.ShowFPS(maxFPS);

                _presentationManager.Display();                

                DateTime end = DateTime.Now;

                TimeSpan duration = end - start;
                if(duration.Seconds >=1 )
                {                    
                    start = DateTime.Now;
                    maxFPS = countFps;
                    countFps = 0;                    
                }
                else
                    countFps++;
               
            }
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Draw background sprite is very slow
« Reply #5 on: December 21, 2013, 02:27:35 am »
I'm not sure what kind of FPS measuring code that is (or even how it is supposed to work). So why don't you try this code and let me know what the result in the console is.

        static void Main(string[] args)
        {
            SFML.Graphics.RenderWindow window = new SFML.Graphics.RenderWindow(new SFML.Window.VideoMode(500, 500), "FPS Test");
            window.SetVerticalSyncEnabled(false);
            System.Collections.Generic.List<SFML.Graphics.Texture> texturelist = new System.Collections.Generic.List<SFML.Graphics.Texture>();
            System.Collections.Generic.List<SFML.Graphics.Sprite> spritelist = new System.Collections.Generic.List<SFML.Graphics.Sprite>();
            System.Random rnd = new System.Random();
            for (int i = 0; i < 10; i++)
            {
                SFML.Graphics.Texture texture = new SFML.Graphics.Texture(".\\animation.png");
                texturelist.Add(texture);
                SFML.Graphics.Sprite sprite = new SFML.Graphics.Sprite(texture);
                sprite.Position = new SFML.Window.Vector2f((float)rnd.Next(30, 470), (float)rnd.Next(30, 470));
                spritelist.Add(sprite);
            }
            window.Closed += (sender, e) => { window.Close(); };
            System.Diagnostics.Stopwatch fpstimer = new System.Diagnostics.Stopwatch();
            fpstimer.Restart();
            while (window.IsOpen())
            {
                Console.WriteLine("FPS:  " + 1f / (float)fpstimer.ElapsedMilliseconds * 1000f);
                fpstimer.Restart();
                window.DispatchEvents();
                window.Clear();
                foreach (var sprite in spritelist)
                {
                    window.Draw(sprite);
                }
                window.Display();
            }
        }

And another thing, if you want SFML style Clock and Time classes (and many other boilerplate helper classes) take a look at the NetEXT library in my signature.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

ciacioz

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Draw background sprite is very slow
« Reply #6 on: December 21, 2013, 10:26:53 am »
Thanks for your code and your advices.
I tried the code and using the devices.png image in the resource folder of the shader example included in the library I scored 29/30 FPS.
I tried to use a png with the portion of the screen I use in my code and the results are 11/12 FPS. I noticed also that the FPS drops a lot if I resize the window with a bigger one and increase a lot if I resize with a small one.
So my conclusion is that... I have to update my gfx card :)

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Draw background sprite is very slow
« Reply #7 on: April 04, 2014, 05:15:46 am »
Here's a big hint.  Preload all the things that could be slowing you down. :P  It will speed up your code a lot, especially if you have to do a lot of things at once. :)  If preloaded it will take away the issues with some of the FPS drops since loading each loop is costly.  Also why not store the images in a resource file so you don't have to load from a file path.  Should speed things up more but not sure.  It'll also make packing up and shipping the project a lot faster if you want to shop someone else your work. :)
I have many ideas but need the help of others to find way to make use of them.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Draw background sprite is very slow
« Reply #8 on: April 04, 2014, 05:31:24 am »
You seriously don't need to be necroing these old threads......
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything