Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Crazy fast...why?

This thread is locked; no one can reply to it. rss feed Print
Crazy fast...why?
SkaxCo
Member #8,323
February 2007

I can't figure out what is wrong with this. I'm using that thingy- the

while(speed_counter > 0){
  //do stuff
  speed_counter--;
}

But it is going insanely fast!

There is also
volatile long speed_counter = 0;
in a header file.

1int game(){
2 zombo = load_datafile("ZomOn.dat");
3 blit((BITMAP*)zombo[intro].dat, screen, 0, 0, 0, 0, 480, 640);
4 while(!mouse_b);
5 /*init junk*/
6 LOCK_VARIABLE(speed_counter); //Used to set the timer - which regulates the game's speed
7 LOCK_FUNCTION(increment_speed_counter);
8 install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));//Set our BPS
9 zombie horde[250];
10 player pc;
11 pc.init_player();
12 for(int i = 0; i < 249; i++){horde<i>.alive = 0;}
13 int mouse_clicked, last_button, current_button = 0; //For too see if the mouse has JUST been clicked
14 int zalive = 0, halive = 1, mines = 3;
15 int lvl = 1, prog = 0;
16 while((!key[KEY_ESC])&&(halive)){
17 while(speed_counter > 0){
18 /*=====raise me some zombies==========*/
19 for(int i = 0; i < 250; i++){
20 if(zalive < lvl){
21 if(!horde<i>.alive){
22 horde<i>.animate();
23 zalive++;
24 i = 51;
25 }
26 }
27 if(horde<i>.y > 451){
28 horde<i>.alive = 0;
29 pc.mines--;
30 zalive--;
31 }
32 }
33 /*=draw teh zombies, check 4 hits=====*/
34 for(int i = 0; i < 250; i++){
35 if(horde<i>.alive){
36 horde<i>.y += horde<i>.fall_speed;
37 horde<i>.frame++;
38 if(horde<i>.frame > 8)
39 horde<i>.frame = 1;
40 }
41 }
42 /*====find if killing a zombie========*/
43 last_button = current_button;
44 current_button = mouse_b;
45 mouse_clicked = (current_button && !last_button);
46 if(mouse_clicked){
47 for(int i = 0; i < 250; i++){
48 if(((mouse_x > horde<i>.x)&&(mouse_x < (horde<i>.x + 30)))&&((mouse_y > horde<i>.y)&&(mouse_y < (horde<i>.y + 30)))){
49 horde<i>.alive = 0;
50 zalive--;
51 pc.score += 1;
52 (lvl < 5)?(prog += (10-lvl)):(prog++);
53 if(prog == (25*lvl)){
54 lvl++;
55 prog = 0;
56 }
57 }
58 }
59 }
60 speed_counter--;
61 }
62 /*###=====#=======#=====##############*/
63 /*##=START=DRAWING=STUFF=#############*/
64 /*###=====#=======#=====##############*/
65 blit(buffy, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
66 } return 0;
67}

There is lots of drawing but that code is fine. For some reason it flies through it, determines you died, and brings up the death screen. What is wrong with this code?

Seppl
Member #4,656
May 2004
avatar

the first possible bug that comes to my mind:
Did you forget to initialize speed_counter with 0 ...?
If you wrote something like this
volatile int speed_counter;
it might be initialized with a value of 223499... so your program would do thousands of loops as fast as it can... dunno, maybe

__________________________________
All you need is lunch - The Rutles

SkaxCo
Member #8,323
February 2007

No, its volatile long speed_counter = 0;

HardTranceFan
Member #7,317
June 2006
avatar

My guess is that it's because everything's running at 60 BPS, and you don't have a pausing or slowdown coded in the sections that need it.

You're raising, moving and animating zombies at 60 times per second. I would have thought animation of 60 times per second is too much. 8-10 frames a sec might suit a little better. You can achieve this by using a counter in the animation method.

One way to determine whether this is the problem is to temporarily change the BPS_TO_TIMER(60) to BPS_TO_TIMER(6).

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

SkaxCo
Member #8,323
February 2007

All changing that does is change te pause before it ends. It appears as though the while(...){...} statement is having a large problem.

HardTranceFan
Member #7,317
June 2006
avatar

Err, yes, silly me - I should have realised :-[.

What happens if you try the following:

Add a variable (int delay) to your zombie class, and initialise it to 10 for every zombie. Then update the section of code that draws the zombies.

      /*=draw the zombies, check 4 hits=====*/
      for(int i = 0; i < 250; i++){
        if(horde<i>.alive){
          if (!(--horde<i>.delay)){
            horde<i>.y += horde<i>.fall_speed;
            horde<i>.frame++;
            if(horde<i>.frame > 8)
              horde<i>.frame = 1;
            horde<i>.delay = 10;
          }
        }
      }

If this helps the animation, try the something similar for the 'raise me some zombies' section.

BTW, I'm having a wild guess at what the problem is that you're experiencing. This may not solve it at all. But, it's worth a crack.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

SkaxCo
Member #8,323
February 2007

To everyone who helped: my most profuse thanks.
It works! Well, except for a r...thats unrelated. Thanks.

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

There is also

volatile long speed_counter = 0;

in a header file.

This is your first (and maybe biggest) problem.

Quote:

What is wrong with this code?

Hmm...

  • Whitespace is like a younger child. You seem to be neglecting it :(

  • Timing code and all other code simply do not get along. You should separate them before they cause more bar fights.

  • Tierney operators are like politics, you must careful what you let them do. See code below.

(lvl < 5)?(prog += (10-lvl)):(prog++);

// I believe most programmers would agree this version is cleaner:

prog += (lvl < 5) ? (10 - lvl) : 1;

In short: Maybe you should decide a few code distribution systems you enjoy using and explore them more thoroughly.

Go to: