GHAGH!!! I think my timing function is off.
relay01

I tried to find the problem myself but I've been looking for an hour and my hair hurts so I decided to finally ask. It's probably something stupid but here goes.

When I compile this:

#SelectExpand
1/********************************************************************* 2The RachMan Video Game early dev source code - dev 2.0a 3Author: Dustin Gray 4*********************************************************************/ 5#include <allegro.h> 6//includes the datafile headers 7#include "datf_dev.h" 8#include "datf_rach.h" 9 10//declares the time counter 11volatile long speed_counter = 0; 12void increment_speed_counter() //A function to increment the speed counter 13{ 14 speed_counter++; // This will just increment the speed counter by one 15} 16END_OF_FUNCTION(increment_speed_counter); 17 18 19//Declarations of integers 20int jump_height = 0; // RachMan's jump height 21int frame_counter;//counts the frames 22int last_key;//remembers the last key pressed 23int rach_pos_x = 0; // Rachman's horizontal position tracker 24int rach_pos_y = 0; // Rachman's vertical position tracker 25 26//declares the datafiles and bitmaps 27DATAFILE *dev = NULL; 28DATAFILE *rach = NULL; 29BITMAP* buffer; 30BITMAP* mapbuff; 31BITMAP* platform; 32 33// Rachman's bounding box info -- this is temporary till I make a struct for this kind of info 34int rachman_box_left = rach_pos_x + 10; 35int rachman_box_top = rach_pos_y + 10; 36int rachman_box_right = (rachman_box_left + 10); 37int rachman_box_bottom = (rachman_box_top + 10); 38int collision = FALSE; 39int gravity = TRUE; 40 41void Draw() 42{ 43 //draw_sprite(buffer, mapbuff, 0, 0); //draws the mapbuffer to the buffer 44 draw_sprite(screen, buffer, 0,0); //draws the buffer onto the screen 45} 46END_OF_FUNCTION(Draw); 47 48void Move_rachman() 49{ 50 if (rachman_box_bottom <= 240) 51 { 52 collision = TRUE; 53 } 54 if (collision == TRUE) 55 { 56 gravity = FALSE; 57 } 58 59 while (speed_counter >= 0) 60 { 61 if(key[KEY_RIGHT])// If the user hits the right key, change the picture's X coordinate 62 { 63 rach_pos_x ++;// Moving right so up the X coordinate by 1 64 } 65 else if(key[KEY_LEFT])// Ditto' - only for left key 66 { 67 rach_pos_x --;// Moving left, so lower the X coordinate by 1 68 } 69 else if (key[KEY_SPACE] && !key[KEY_RIGHT] && !key[KEY_LEFT] && jump_height <= 25) 70 { 71 rach_pos_y = rach_pos_y - 5; 72 jump_height ++; 73 } 74 else if (key[KEY_SPACE] && key[KEY_RIGHT] && jump_height <= 25) 75 { 76 rach_pos_y = rach_pos_y - 5; 77 rach_pos_x++; 78 jump_height ++; 79 } 80 else if (key[KEY_SPACE] && key[KEY_LEFT] && jump_height <= 25) 81 { 82 rach_pos_y = rach_pos_y - 5; 83 rach_pos_x--; 84 jump_height ++; 85 } 86 if (gravity == TRUE) 87 { 88 rach_pos_y = rach_pos_y + 2; 89 } 90 speed_counter --; 91 92 } // while(speed_counter) 93} // void Move_rachman 94END_OF_FUNCTION(Move_rachman); 95 96void Anim_rachman() 97{ 98 while (speed_counter >= 0) 99 { 100 101 frame_counter ++; // Increment our frame counter at the same speed of the speed counter 102 if(frame_counter >= 80) // resets the frames for animation 103 { 104 frame_counter = 0; 105 } 106// Display all animations for walking right 107 if(key[KEY_RIGHT] && !key[KEY_SPACE])//displayes animation for key right 108 { 109 if (frame_counter < 10) 110 { 111 draw_sprite(buffer, (BITMAP*)rach[rachrun0].dat, rach_pos_x, rach_pos_y); 112 } 113 else if(frame_counter >= 10 && frame_counter < 20) 114 { 115 draw_sprite(buffer, (BITMAP*)rach[rachrun1].dat , rach_pos_x, rach_pos_y);//Draw the first frame 116 } 117 else if(frame_counter >= 20 && frame_counter < 40) 118 { 119 draw_sprite(buffer, (BITMAP*)rach[rachrun2].dat , rach_pos_x, rach_pos_y);//Draw the second frame 120 } 121 else if(frame_counter >= 40 && frame_counter < 60) 122 { 123 draw_sprite(buffer, (BITMAP*)rach[rachrun3].dat, rach_pos_x, rach_pos_y);//Draw the third frame //to acheive better effect 124 } 125 else if (frame_counter >= 60 && frame_counter <= 80) 126 { 127 draw_sprite(buffer, (BITMAP*)rach[rachrun2].dat, rach_pos_x, rach_pos_y); //Draw the second frame again 128 } 129 last_key = 1; 130 } 131// Display all animations for walking left 132 else if(key[KEY_LEFT] && !key[KEY_SPACE])//displayes animation for key left and flips the original frames so it looks like it's moving left 133 { 134 if (frame_counter < 10) 135 { 136 draw_sprite_h_flip(buffer, (BITMAP*)rach[rachrun0].dat, rach_pos_x, rach_pos_y); 137 } 138 else if(frame_counter >= 10 && frame_counter < 20) 139 { 140 draw_sprite_h_flip(buffer, (BITMAP*)rach[rachrun1].dat , rach_pos_x, rach_pos_y);//Draw the first frame 141 } 142 else if(frame_counter >= 20 && frame_counter < 40) 143 { 144 draw_sprite_h_flip(buffer, (BITMAP*)rach[rachrun2].dat , rach_pos_x, rach_pos_y); 145 } 146 else if(frame_counter >= 40 && frame_counter < 60) 147 { 148 draw_sprite_h_flip(buffer, (BITMAP*)rach[rachrun3].dat, rach_pos_x, rach_pos_y); //to acheive better effect 149 } 150 else if (frame_counter >= 60 && frame_counter <= 80) 151 { 152 draw_sprite_h_flip(buffer, (BITMAP*)rach[rachrun2].dat, rach_pos_x, rach_pos_y); 153 } 154 last_key = 2; 155 } 156// Display all animations for just standing there like an idiot 157 else if (!key[KEY_RIGHT] && !key[KEY_LEFT] && !key[KEY_SPACE]) //displays animation if no key is pressed 158 { 159 if (last_key == 2) 160 { 161 draw_sprite_h_flip(buffer, (BITMAP*)rach[rachstand].dat, rach_pos_x, rach_pos_y); 162 } 163 else if (last_key == 1) 164 { 165 draw_sprite(buffer, (BITMAP*)rach[rachstand].dat, rach_pos_x, rach_pos_y); 166 } 167 else 168 { 169 draw_sprite(buffer, (BITMAP*)rach[rachstand].dat, rach_pos_x, rach_pos_y); 170 } 171 frame_counter = 0; //keeps frame count to zero so no animations are mixed up 172 } 173// Displays jumping animations (still a little buggy) 174 else if (key[KEY_SPACE]) 175 { 176 if(last_key == 2) 177 { 178 draw_sprite_h_flip(buffer, (BITMAP*)rach[rachjump].dat, rach_pos_x, rach_pos_y); 179 } 180 else if(last_key == 1) 181 { 182 draw_sprite(buffer, (BITMAP*)rach[rachjump].dat, rach_pos_x, rach_pos_y); 183 } 184 frame_counter = 0; 185 } 186 line(buffer, rachman_box_left, rachman_box_top, rachman_box_right, rachman_box_top, makecol(255,0,0)); 187 188 line(buffer, rachman_box_left, rachman_box_bottom, rachman_box_right, rachman_box_bottom, makecol(255,0,0)); 189 190 line(buffer, rachman_box_left, rachman_box_top, rachman_box_left, rachman_box_bottom, makecol(255,0,0)); 191 192 line(buffer, rachman_box_right, rachman_box_top, rachman_box_right, rachman_box_bottom, makecol(255,0,0)); 193 draw_sprite(screen, buffer, 0,0); //draws the buffer onto the screen 194 speed_counter --; 195 196 } // while 197} // void Anim_rachman 198END_OF_FUNCTION(Anim_rachman); 199 200void Game_loop() 201{ 202 Move_rachman(); 203 Anim_rachman(); 204} 205END_OF_FUNCTION(Game_loop); 206 207int main() 208{ 209 allegro_init(); //initializes allegro functions 210 install_keyboard(); //installs keyboard keys 211 install_timer(); //installs the timer duh 212 set_color_depth(16); //sets color depth to 16 bit 213 set_gfx_mode( GFX_AUTODETECT, 256, 256, 0, 0); //sets the graphics for the game 214 215 //timer functions 216 LOCK_VARIABLE(speed_counter); //Used to set the timer - which regulates the game's 217 LOCK_FUNCTION(increment_speed_counter);//speed. 218 install_int_ex(increment_speed_counter, BPS_TO_TIMER(80)); //Set our BPS (similar to fps) 219 220 mapbuff = create_bitmap( 256, 256);//bitmap to write the map too 221 buffer = create_bitmap( 256, 256);//bitmap to write onto the screen (double buffering helps eliminate flickering of the screen) 222 dev = load_datafile("dev.dat"); //loading datafiles 223 rach = load_datafile("rach.dat"); 224 platform = load_bitmap("platform.bmp", NULL); 225 226 while(!key[KEY_ESC]) 227 { 228 Game_loop(); 229 } 230 231 //cleanup functions 232 destroy_bitmap(buffer); 233 destroy_bitmap(mapbuff); 234 destroy_bitmap(platform); 235 unload_datafile(rach); 236 unload_datafile(dev); 237 238return 0; 239} 240END_OF_MAIN();

All I get is a blank window... just black... nothing more... I tried fiddling with it and eventually I'll get a character who runs and animates WAY too fast... this leads me to believe it's a problem with my timer... I'm not entirely certain how to create a good timer... I've been using this one that I found in a tutorial and it's worked until I re-wrote my code...
Any Ideas?
Suggestions for better timer functions?

EDIT:
Ok... I tried a few more things... and now I get a really slow character... just about all functionality is there though... hmm... why is it working better now though... I didn't really do much...?
I posted the updated code

Richard Phipps

Do a search on these forums for how to seperate your timing and logic functions. You are not doing it in the best way.

Onewing

Why are you commenting out:

install_int_ex(increment_speed_counter, BPS_TO_TIMER(80));

relay01
Quote:

Why are you commenting out:

install_int_ex(increment_speed_counter, BPS_TO_TIMER(80));

that was an attempt to fix it... didn't work... forgot to uncomment it... but the problem still persists

EDIT:
Ok... well It's working now... I'm not at all sure why but it is... But Phipps was right... Implementing 2 functions is bogging the time down so I have to find a better way to implement timing...

Suggestions?

A J

where is the error checking for everything that can fail ?
::)

Tomoso

What I do is only ever use 1 universal timer that controls the speed of the game loop. Everything else can be timed using that one timer. I think its better if I just show you. This is only an example, don't try and compile it

1//Timer Initialize\function blah blah
2// Everything else you wanna do before game loop
3 
4// Game Loop
5do
6{
7 while (speed_counter >= 0)
8 {
9 // Logic
10 // Input
11 // GFX Routines (by this I mean update any co-ords of gfx being drawn to
12 // screen not actually drawing. For instance a scrolling background may have
13 // to update its x,y etc...)
14 // Enemy AI
15 // End Logic
16 speed_counter--;
17 }
18 // Update Screen
19 // Clear buffer
20 // Draw to buffer (means draw everything)
21 // Draw buffer to screen
22 // End Update Screen
23} while(key[KEY_ESC]);

Thats a very rough idea of how I go about things, I dont know if its right, I just find it easier. Keep in mind i used class' and so forth for all objects and they each handle their own animation, AI. I have a game engine class that creates objects dynamically and just calls "m_pObject->Handle();" and it looks after itself. Of course I have to call each objects draw function into the GameEngines::Draw(); funtion also.

About using time within my objects though its pretty simple. Let us assume the timer is set to 60bps, so every time my game engine calls "m_pObject->Handle();" the class' "int m_FrameTimer++'s" if u get my meaning and resets at 60.
Then the class itself knows where its at as opposed to how long its got before the next draw.

I hope that helped, I really think I am bad at explaining things and went totally from the point :P I seem to know it in my head, I just never helped no one before.

Thread #586675. Printed from Allegro.cc