Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Optimizing code: Hunters

Credits go to Goalie Ca and Jonatan Hedborg for helping out!
This thread is locked; no one can reply to it. rss feed Print
Optimizing code: Hunters
Kikaru
Member #7,616
August 2006
avatar

Well, as I am developing my current game, Hunters, I have seen some freezing of the program. This is mostly due to only having half the memory I normally do, but it made me think: how can I optimize my code? I have included both the main file and the header with this post. I hope my code is well documented.

Cookies for help! :)

Jonatan Hedborg
Member #4,886
July 2004
avatar

My eyes are bleeding.

No offense, but you really need to split up that code a bit more before i will even try to look at it seriously.

Your main function is about 470 lines long.
You almost exclusively use globals.
You declare/define all your classes in one .h file.

But enough of that :)

At a glance;

while (clock() < log_time)
{}

Use proper timing (search the forum), and see if the problems clear up.

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Goalie Ca
Member #2,579
July 2002
avatar

Use a profiler and isolate some functions for us or write some pseudo code.. i don't have time to go through code but if you posted snippets or ideas i and others could make some quick comments.

In terms of memory: pass by reference, mmap large files, use a memory pool if you have lots of allocation/deallocation. Make your contructors as simple as possible and use initialization syntax for variables rather than assignment. (some compilers still haven't figured this out in all cases).

-------------
Bah weep granah weep nini bong!

Jonatan Hedborg
Member #4,886
July 2004
avatar

I really don't think you have anything that is overly time-/memory- consuming.
Remake your timing function. clock() has very (!) poor accuracy; it only changes value about 6-7 times per second (on winXP).

Try it yourself:

1#include <stdio.h>
2#include <time.h>
3#include <stdlib.h>
4 
5int main() {
6 clock_t lastVal;
7 int i;
8 
9 lastVal = -1;
10 for(i=0; i<10000000; i++) {
11 if( lastVal != clock() ) {
12 lastVal = clock();
13 printf("lastVal: %i\n",lastVal);
14 }
15 }
16 system("PAUSE");
17}

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Kikaru
Member #7,616
August 2006
avatar

Well, I did this:

long timer;
void plus_time() {timer++};
install_int(plus_time, 1);

instead of clock(), and it runs a lot better now. Thanks! :)
As for the code, it was in just 1 file for quite a while. I split it for a bit of convenience. I use globals out of habit, and it seems to work just fine. (I really don't like using non-global variables that much) :-[

Jonatan Hedborg
Member #4,886
July 2004
avatar

You should try to think about the design before you start coding, so you don't have to write everything as a global "just in case". But anyway; glad it worked better with allegro's timer :)

Also, you should read the timer section in the manual fort he "proper" way to do it (volatile etc).

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Kikaru
Member #7,616
August 2006
avatar

I have considered replacing the shot and enemy arrays with vectors. Think that would help at all? :)

SiegeLord
Member #7,827
October 2006
avatar

I think it would. It would conveniently remove the cap on max number of enemies... ;)

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Kikaru
Member #7,616
August 2006
avatar

Oh. Yeah. Cool. BTW, the actual max number is 80 right now... :)

Hey, I think I found something: When I include <vector>, I get warning about includes with:
<ul> <li> windows.h</li> <li> c++io.h</li> <li> fpos.h</li> <li> gthr.h</li> <li> gthr-default.h</li> <li> wingdi.h</li> <li> iosfwd.h</li> <li> stl_algobase.h</li> <li> vector.h</li> </ul>
Does anyone know what's wrong? ???

Peter Wang
Member #23
April 2000

Quote:

clock() has very (!) poor accuracy; it only changes value about 6-7 times per second (on winXP).

It does not do what you think it does...

Kikaru
Member #7,616
August 2006
avatar

Well, anyone know why <vector> is trying to include "windows.h"? Please? ???

Jonatan Hedborg
Member #4,886
July 2004
avatar

It returns the number of "ticks" (1000/second on my machine) since the process started. Only, it does so in rather large "jumps".

Or am i wrong?

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Goalie Ca
Member #2,579
July 2002
avatar

kikaru, can you paste all of your include statements in order.

-------------
Bah weep granah weep nini bong!

Kikaru
Member #7,616
August 2006
avatar

Ok, here:

#include <allegro.h>
#include <math.h>
#include <stdio.h>
#include "hunters.h"
#include <vector>

hunters.h has this in the include:

#include <allegro.h>
#include <math.h>
#include <stdio.h>
#include <vector>

[EDIT]
Hey, isn't there like "winalleg.h" or something that I need to include? ???

[EDIT 2]
I added "winalleg.h", and it work perfectly! Thanks for your help guys! :D

Go to: