Hints to increase performance, and some GLEW issue.

Hello everyone! It has been a while! So, I am working on this somewhat large game project (2d using both SDL2.0 and OpenGL 2.1). Since a while, everything has been going fine. But now, I am starting to face problems with performance and dropping FPS when having over 50 (if over 70, the game just crashes) character objects. (its a beat em up game, so its normal to have 200 enemies fight you at once. Especially when its a One Piece one ;D.)

So here is a pseudo code representing the game's main loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "EVERYTHING_NEEDED"

initialize_OpenGL_SDL_STUFF(); /*That is: Creating the window, Enabling
 "GL_TEXTURE_2D", setting all matrices to their Identity, call "glOrtho()" to 
have the ordinary coordinates, enabling "BL_BLEND" for alpha channeled pics and
 sprites */
CreateAndCall"LOAD"ObjectWhichParsesCharactersFromTxtFilesAndStoresThemInAVectorContainer(); 
/*The LOAD object acts like the mother container. It contains another, sized 
1000, Vector which contains Objects of the characters. These Objects contains 
another huge vector which contains data about the character's basic actions and
 attack sequences (they are parsed from the external txt files) */ 
StartCountingForFPS();

StartTheMainLoop (int x=0; x<NoOfObjects; x++){
    ChecksForInput();

    RenderBG();
    UpdateObjectsAnimation(&"LOAD".LOADED.OBJECTS[x]); 
    RenderObjects(&"LOAD".LOADED.OBJECTS[x]); /* I use "glBindTexture()" followed by "glBegin(GL_QUADS); glEnd(); */  
    RenderShadows(&"LOAD".LOADED.OBJECTS[x]); /*Same Here*/

    CheckAndRegulateFrameRate();
    RestartCountingForFPS();
    SDL_GL_SwapWindow(MainWindow);
    UseStd::sort()ToRearrangeTheObjectsAccordingToTheirZpositions(); /*Because 
the characters with higher Z-pos. needs to be rendered first. Now this seems to
 consume a lot of memory for me. Any thing I can use instead? */
}


Now hoping someone would catch a flaw in the above composition, here are some other performance regarding questions:

1-Does passing large objects as parameters for function costy? Or is it just that its already stored on the memory, so is it just a matter of sending its pointer (automatically?)

2-When should I use vector containers, and when should I use normal arrays? I know vectors are used to store the data in the heap, when arrays are to keep it in the stack. But they also say that vectors are somewhat slower.. I NEED PERFORMANCE YO! So suppose I want to have an int array with size 150. Should I use a vector or an array?

3-I've used python before, and I never used for loops there because they were costy; a normal conditioned while loop with that value incrementing each loop worked faster. Does that apply in C++?

4-Does allocating, though not using, large amounts of array slots cost much memory?

5-When having a class which it's object is to be called and created a lot, is it better to have its related functions inside the class's scope or outside as one function for all?

6-Any other approaches or common beginners' mistakes I should follow/avoid?

Any input will really be appreciated. Thanks in advance!!

Regards,

Edit:
Some other question on the way:

7-Is there a noticeable difference between using ints or floats? (In terms of performance?)

8-Is there a difference between using functions or streams to convert between data types? (which is faster?)
Last edited on
1-Yes, if you pass by value. Try by reference instead, const if you don't need to change the object. If you absolutely need to work on a copy of the large object, perhaps you might want to rethink your design.

2-If you only need exactly 150 ints of space, just use a static array, IMO. Unless you absolutely need to operate on copies of the array, I suppose. Then maybe a resized vector might make the copy process easier.

3-Definitely use for loops when appropriate.

4-It costs exactly much memory as you have allocated but not used. ;)

5-Not exactly sure what you are asking, but I don't think there would be a noticeable difference between calling a method or a global function that takes an explicit pointer to the object to operate on.
1-Making sure I understood:
1
2
3
4
//The wrong way:
Update(object);
//The right way:
Update(&object);


2, 3, 4: Thanks!!! XD!

5-What I meant:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Should I be doing this:

class WAWA{
     int a, b, c, d, e, f;
     void addvelocity(void){
         b+=5; 
    }
};

WAWA arraya[1000];

for (int a=0; a< 1000; a++{
    arraya[a].addvelocity();
}
    
    


//Or this: ????

class WAWO{
     int a, b, c, d, e, f;
};
void addvelocity(WAWO * wawoobject){
    wawoobject->b+=5; 
}

WAWO arrayo[1000];

for (int o=0; o< 1000; o++{
    addvelocity(&arrayo[o]);
}


Thanks again!
If you're not using timers all over the place. I found a huge increase in performance in putting timers on almost everything and then making functions executed within those timers be executed as few times as possible per second.
RenderObjects(&"LOAD".LOADED.OBJECTS[x]); /* I use "glBindTexture()" followed by "glBegin(GL_QUADS); glEnd(); */

This is killing you.

"Immediate mode" (usage of glBegin/glEnd) is deprecated. You're passing loads of vertex information to the GPU every frame. Look into vertex arrays, or better yet, VBOs (vertex buffer objects).
@Mats: Timers everywhere? I don't think I really got you. Can you elaborate please?

@xismn: Hmph, had a read about them, and I guess you made a very valid point there. Have been trying to set GLEW for 2 days now, but I can't get it to work (Linker errors?).. It seems I have to "compile" the library itself to get the .a file out of the lib ?_? Any clues for how to do that???

Thanks in advance.
What are the specific errors you're receiving?
Here you go: http://i.imgur.com/gpXEdMs.png
Directories: http://i.imgur.com/DqOBWC7.png , http://i.imgur.com/3lR0qHh.png
Linker options: http://i.imgur.com/6K4thij.png
Header files: http://i.imgur.com/eryNOYC.png

Now that was with Code Blocks. Tried with Orwell's Dev C++, same result.
Last edited on
Not too familiar with Code Blocks. Are your linker settings universal for all builds (debug, release)?

Here's a long shot: instead of

1
2
#define GL_GLEXT_PROTOTYPES
#define GLEW_STATIC 


Try this:

1
2
3
4
#undef GL_VERSION_1_5
#define GL_GLEXT_PROTOTYPES 1
#define GL3_PROTOTYPES 1
#define GLEW_STATIC 1 




Not exactly sure what's causing your errors, I'll do some more research and get back to you.
Tried... didn't work =(. And thanks for dedicating some of your time to help in my issue. I really appreciate this!
Topic archived. No new replies allowed.