Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Increasing Game Speed

Author
Message
Goldsoul80
14
Years of Service
User Offline
Joined: 17th Oct 2011
Location:
Posted: 20th Nov 2011 19:49
I would like a few opinions. (I'd like them to pertain to increasing game speed) My first question, would my game run faster if I were to load a bunch of images to use as sprites or if I were to compile many of those images into fewer larger images and use get image to use them as sprites. (I use .png images by the way because I'd like to use the alpha they provide) At times, during gameplay, my game may have as many as 400 small sprites on screen at a time. I think that loading 11 images and using them as sprites might be slower than loading 1 image with the 11 images on it. My colleagues, your thoughts?
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 20th Nov 2011 21:00 Edited at: 20th Nov 2011 21:54
Can you include just a few lines from your code so that I can see how you are setting up your sprites? Mainly the backsave, texture flags, sizes, etc.

- basically an example of your load image/load bitmap, get image/paste image, set sprite and sprite; lines


Are you scaling, rotating etc... or do you have frames that handle this?


To answer your actual question, "generally" it's more efficient to load the single larger image/texture and extract your sprite image. But, And I could be wrong here... unless you were loading images during the game loop, and not all at once in the beginning, I doubt there would be a difference. And, in some cases I can see the latter being more efficient. Do you need all 400 sprites at the beginning of the level, and throughout the level? I think how the sprites are being used, and how and when the call to update there draw would have a greater impact. Other optimizations I used to use, probably don't play as big of a role now... honestly I'm not sure. For example, I have a habit of keeping image sizes a power of 2.

There are also methods of getting further lossless compression out of .png files, like pngcrush. But again, I don't think that the size of the images is your actual bottleneck.

~ZENassem
Goldsoul80
14
Years of Service
User Offline
Joined: 17th Oct 2011
Location:
Posted: 20th Nov 2011 21:21 Edited at: 21st Nov 2011 01:19
At the beginning of my gameplay code I just call set sprite 1, 0, 1 (that's my player sprite but I heard it changes all the backsaves.

This is the main part of my code that this question would probably refer to.



(tried doing the code snippet thing, didn't work...)

Many segments of my code regarding bullet fire are just like this. I don't make all of the bullets at once, the first segment controls how often the bullets are fired and when. The second section checks to see if the bullet exists and controls it. The pattern is: create the instance of the bullet, create the bullet (scale or rotate if needed), then control the bullet automatically until it dies (ebulletlife(eb) = 0).
I haven't used the get image, paste sprite, or load bitmap commands and I load all of my images before the level starts (or before I get to a player select screen or the title screen)
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 20th Nov 2011 23:05
Quote: "tried doing the code snippet thing, didn't work..."

Fixed it for you - check out the code tags to see how you should use them.

Goldsoul80
14
Years of Service
User Offline
Joined: 17th Oct 2011
Location:
Posted: 20th Nov 2011 23:32
Thanks
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 20th Nov 2011 23:50
One optimisation I'd suggest straight off is to create a ready made sin and cos tables for faster calculations as you're running through 10,000 iterations.

Apart from that, do you need 10,000 bullets???

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Goldsoul80
14
Years of Service
User Offline
Joined: 17th Oct 2011
Location:
Posted: 21st Nov 2011 01:11
I wasn't sure how many I'd be using in the beginning. 10,000 was just leeway. The most I've ever counted on screen was about 530. I have different patterns but this one has had the most bullets. Maybe if I shortened it, it would speed up? Also, explain the sin/cos tables thing please? (I think I might know but just in case...) Thank you all for all your help by the way.
Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 21st Nov 2011 01:19 Edited at: 21st Nov 2011 01:54
Quote: "Also, explain the sin/cos tables thing please? (I think I might know but just in case...) Thank you all for all your help by the way."


Unless you're just doing the same sin/cos calculations(i.e. 10,000+ of sin(90) or cos(45) ), it's not worth the large memory cost of making a sin/cos look-up table. As modern implementations of sin/cos/tan use built-in hardware functions that are extremely fast, they probably aren't your bottleneck.

Regarding your original question regarding 11 smaller images or 1 large image, when you use get image you're still making 12 images according to DBPro(11 for the individual smaller images and 1 for the large image). The only thing you'll be saving resource-wise will be the file-loading time and that'll be negligible.

Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 21st Nov 2011 01:28 Edited at: 21st Nov 2011 01:32
Photoshop has an adaptive palette mode. You can use this to make images 256 colours. These images are really fast, and the colour loss isn't as bad as you might think. I only use it when I am desperate though. You have to use the same palette for all of your images as well.... I think... it has been ages since I did it.

DarkDISCUSSION
15
Years of Service
User Offline
Joined: 6th Jul 2011
Location: Ft Madison, IA
Posted: 21st Nov 2011 03:06
Quote: "Photoshop has an adaptive palette mode"

I would recommend GIMP for that because it's FREE and easy to use.
http://www.gimp.org/

Kind Regards,
Captain Wicker
http://captainwicker.webs.com/
Goldsoul80
14
Years of Service
User Offline
Joined: 17th Oct 2011
Location:
Posted: 21st Nov 2011 04:16
In conclusion, whether I load 11 images or "get image" 11 times doesn't matter too much then. Regarding the bullets, the for/next loop may just be too large and is slowing my game down.

(I use photoshop and gimp depending on whether I need large or small images)

To jump into my next question, could the way I set up my window affect the speed?



(The window resolution of the properties menu says that it's currently set on 640x480)
I heard that using set window off (not using at the second) means that the VSyncOn flag should be off. I've tried this an my program runs at a beautiful... 15 FPS... (I need 60 or at least 50)

What do you all think?
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 21st Nov 2011 12:46 Edited at: 21st Nov 2011 13:39
Imho you are looking to gain performance in areas that are not at the root of your bottleneck. I myself have not attempted coding a game that has that many projectiles, so perhaps I am off the mark; but I think.. rotating 10,000 sprites with virtually analog freedom of degrees, updating the position and checking collision of 10,000 sprites, plus a bunch of other checks, every iteration of the game-loop: is going slow down your game.

Without seeing everything... my personal opinion is that you are doing way too much; both in number and translations of sprites for mere bullets. I'll probably have to port something over from c++ to give a an example, but... in general you shouldn't need all of that going on. Keeping in mind we haven't even delved into your collision code, other than bullets that are off the screen.

The only problem is, we don't know exactly how your game works, but regardless you need to rethink the implementation. The idea is... to seperate what is being perceived visually by the player and what is actually taking place behind the scenes. There may be a lot of bullets in the visual sense, but we can limit the number that control the behavior of a group and register a hit. Also, the spot on, rotation of 10,000 bullets is more than likely very unecessary; even a few selectable degrees of rotation will suffice; say increments of 15...(or 10) degrees 0,15,30,45,60,75,90...

With a large amount of bullets in a given area, sphere collision is approximate enough to register hits. Unless you have some special reasoning, not every bullet fired (especially when we are talking in the realm of thousands) needs to be treated like a sniper bullet.

I think you should focus on creating the look of thousands of bullets, while trimming the logic down to master bullets in a given group to knock the number down to 100's or dozens. Again, until I can either 1. Port over some massive bullet code, or 2. gain insight into your need for that many projectiles.... Much of what I am saying may not make sense.

If this is a 2d bullet hell or manic shooter game, than collisions of thousands of bullets can be handled by: dividing your board into quadrants. Register each object to it's respective grid cell, based on it's x,y. Hit-test those objects which lie in the same quadrant cell. This way, you will only need to test the quadrants where the enemy or player is, and that means you get even better optimized performance.

~ZENassem
Goldsoul80
14
Years of Service
User Offline
Joined: 17th Oct 2011
Location:
Posted: 21st Nov 2011 14:16 Edited at: 21st Nov 2011 17:40


(I use these lines to move just about everything because it gives me better flexibility than the combination of rotate and move sprite.)

I never planned on using all 10,000 of those sprites. I had no clue of how many I'd be using at anyone time so I gave myself some room and put that number. Given what you've told me, cutting down those numbers from my player/bullet collisions (I use sphere collision, also looking for elliptical collision), enemy bullet creations, and the arrays that hold all the bullet data would prove very helpful to me huh?

(~Update~) I changed the upper limit of bullets from 30000 to 22000 cutting all the for/next loops to 1/5 of their size and the frame change was remarkable! Rather, now my game is running too fast so I need to lower many of the speed variables.

I planned on using C++ by now but I have yet to solve the "unable to create .exe" issue my compiler has. (visual c++ 2008 express version)

That last bit sounds impressive but I've never heard of it, so if someone could elaborate of that, it would be helpful. (and yes, it's going to be a bullet hell game.)
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 22nd Nov 2011 03:35 Edited at: 22nd Nov 2011 03:39
In the for next loops it's fine to leave the max really high in case you ever need that qty but most of the time you can just end the loop early by adding a check to see if it needs to keep going. In super quick pseudo:




Goldsoul80
14
Years of Service
User Offline
Joined: 17th Oct 2011
Location:
Posted: 22nd Nov 2011 13:34 Edited at: 22nd Nov 2011 13:38
That's basically how I had my code before but I had like 20 or so code segments going through that. As soon as I lowered the upper limit, my game sped up exponentially. I have all of the routines for attacks running at the same time but I check to see if a butllet's flag is set to any specific attack before the bullet's created.



and



It might be fine if there are a few large loops like that but, as I've seen, too many will slow things down.

(~wait a sec~) I see what you're saying. Alter the variable in the loop to reach the end as fast as possible without checking the other thousands. (Might be useful for a different problem I'm working on)
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 23rd Nov 2011 04:48 Edited at: 23rd Nov 2011 05:04
Quote: "(~wait a sec~) I see what you're saying. Alter the variable in the loop to reach the end as fast as possible without checking the other thousands"


correct as soon as you know you've processed the last thing that actually needs processing, there is no need to keep looking at anything else. of course it gets a little more complex if you have a bullet id system that would allow gaps like bullets exist 1, 2, 5, 7, 10 with the missing ones currently inactive. as soon as it hits a gap it would quit without working the ones after that. you can add a marker to allow for a relatively small gap with no performance hit, something like:



this will allow 20 nonexistant bullets to pass before it decides that it has hit the end and jumps the eb variable to exit

edit:
alternatively to jumping early, you can setup a global maxbullet variable which gets updated when you create a new bullet and the upper limit of the for:next can be set to that

for eb=20000 to maxbullet
blah blah
next eb

this setup is better for values with a linear progression than things which can jump back and forth.

I use each for different things, really just depending on what it is i'm cycling through and what i'm trying to do.


Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 23rd Nov 2011 12:49
Instead of doing:


I'd do this:


Using the Exit command will immediately exit the for/next loop without having to set the counter variable.

My signature is NOT a moderator plaything! Stop changing it!
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 23rd Nov 2011 22:51
ah cool. I thought that only applied to do:loop and repeat:until type loops. good to know, thanks.


Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 24th Nov 2011 08:43
It applies to every loop.

My signature is NOT a moderator plaything! Stop changing it!
Goldsoul80
14
Years of Service
User Offline
Joined: 17th Oct 2011
Location:
Posted: 17th Dec 2011 02:36
Okay, so I have another question. 3d Models. What kinds of things can I do to speed up my game when 3d objects are present? I make my models in blender and I often export them as .x files. Also, scrolling object textures tends to slow down my programs fairly often. Is that typical and does the size of the object matter? I only use the scroll object texture command on a couple of my levels.

Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 18th Dec 2011 21:14
it's size (scale) doesn't matter so much really, more importantly is it's number of polygons and the size of the textures. also DBP handles multiple limbs on an object easier than multiple objects, most of the time.


cyril
18
Years of Service
User Offline
Joined: 6th Aug 2007
Location: 7 miles away from big ben
Posted: 23rd Dec 2011 18:23
Quote: "it's size (scale) doesn't matter so much really, more importantly is it's number of polygons and the size of the textures. also DBP handles multiple limbs on an object easier than multiple objects, most of the time."


There is two extra problem to consider with scale:

-If the scale is too small the camera would clip the object too early.
-Likewise a too large scale is not good for array and position handling as well as increased chance of Z fighting.

As for Polygon counts, DBP uses the back to front space rendering with the volume of a pyramid. In short, all object within the field of view and in the camera's range is drawn, starting with the objects at the back (furthest from the camera) to the front.

This means if you were looking at a wall in the direction of the main area of your levels the wall might only be 2 polygons but the rest of the level within this volume will be drawn first before the wall and this is waste of rendering time and your fps will be lower than expected.

You'll either have to design your game to use the field of view pyramid effectively (reduce the unit cubed volume of the rendering pyramid) or code an culling function into your game, if your level size becomes too big. For smaller levels this is not much of a issue since the fps will still be very high and there might not be much to cull off anyway.

As for textures, everything is drawn on a per-pixel bases, the only time compressing your image becomes effective is in the memory consumption.

with this said its more about the total amount of pixels drawn to the screen at any one time and how long this takes, and this is determined separately from the polygon limit, and is known as a fill rate.

In theory many small textures are just as fast as a few large textures, if the total pixels passed to the Gpu are the same.

But DBP uses a number of tricks to speed texture drawing up as well as control and handle texture size to screen space with the help of mipmap and texture sorting* (*If enabled)

Mipmaps are basically small copies of your texture with the size half for each texture, the good news about this is that it reduces your total pixel count for objects where it would be impossible to make out the pixel of the texture on the screen and its automatically on by default.

Texture sorting is available if you use the "Texture object" command and it basically renders in the order of the texture ID, so everything with the same texture is render together, this can be use if you know a particular groups of objects will be drawn is a fixed order: i.e. a 3D Heads up display in game.
Max P
16
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 23rd Dec 2011 19:20
Quote: "This means if you were looking at a wall in the direction of the main area of your levels the wall might only be 2 polygons but the rest of the level within this volume will be drawn first before the wall and this is waste of rendering time and your fps will be lower than expected."

You can use my library for that, it is almost finished (link in my signature)

DBPro can handle quite a few polygons if you keep the objects large, rendering one object with 100,000 polygons is a lot faster then rendering 100 objects with 1000 polygons each.

Login to post a reply

Server time is: 2026-07-10 13:45:21
Your offset time is: 2026-07-10 13:45:21