Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Updating game and input

This thread is locked; no one can reply to it. rss feed Print
Updating game and input
dothedru22
Member #7,325
June 2006

Hey I got my main game loop..and im not sure what should be updated using the fps time. I have it like this.

1void D_DoGame(Player_S* p , Enemy_S* e , Level_S* lev , BITMAP* b) {
2
3 D_TimerSetup();
4 
5 while (!key[KEY_ESC]) {
6
7 while (ticks > 0) {
8
9 D_UpdateInput(p);
10 D_CheckCollision(p , e , lev);
11 ticks --;
12 }
13
14
15 D_UpdatePlayerDraw(p , b);
16 D_DrawLevel_1(b);
17 D_Flip(b);
18 }
19 
20 destroy_bitmap(p->playerImg); //player image
21 destroy_bitmap(p->gunImg); //gun image
22 destroy_bitmap(b); //buffer
23}

1void D_UpdateInput(Player_S* p) {
2
3 clear_keybuf();
4 
5 p->isMouse = false; //the mouse is not pressed
6 p->isJump = false; //jump is false if w(up) is not pressed
7 
8 //if the player is on the ground reset all the jumping varibles
9 if (p->y >= GROUND - 3) {
10
11 p->jumpTime = 0;
12 p->velX = 0.0f ;
13 p->velY = 0.0f;
14 p->y = GROUND;
15 }
16 if (p->isJump == false && p->y <= GROUND - 1) { p->velY += .05; } //if player is above ground put him back down
17
18 //jump only if key is w(up) and the jump timer is less than the jump timer
19 if (key[KEY_W] && p->jumpTime <= MAX_JUMP_TIME && p->isJump == false) {
20
21 p->isJump = true;
22 p->velY = -1.5f;
23 p->jumpTime ++;
24 }
25 
26 if (key[KEY_A]) { p->x -= 1.0f; p->keyCheck = D_KEYLEFT; } // Left
27 if (key[KEY_D]) { p->x += 1.0f; p->keyCheck = D_KEYRIGHT; } // Right
28 
29 if (key[KEY_UP]) { p->crossY -= 1.5f; }
30 if (key[KEY_DOWN]) { p->crossY += 1.5f; }
31 if (key[KEY_R]) { D_Reload(p); } //reload
32
33 if (!mouse_b & 1) { p->mouseTime = 0; }
34
35 //left click(fire)
36 if ((mouse_b & 1) && p->isMouse == false && p->mouseTime < 1) {
37
38 p->mouseTime ++ ;
39 p->isMouse = true;
40 D_TakeAmmo(p , 1);
41 }
42 
43 if (mouse_b & 2) { p->isMouse = true; } //right click 2nd fire
44
45 p->gunX = p->x + 25;
46 p->gunY = p->y + 25;
47 
48 p->crossX = p->x + 120;
49
50
51 D_FindMouseDistance(p);
52}
53 
54void D_UpdatePlayerDraw(Player_S* p , BITMAP* b) {
55 
56 if (p->keyCheck == D_KEYRIGHT) {
57
58 draw_sprite(b , p->playerImg , p->x += p->velX , p->y += p->velY);
59 }
60 else if (p->keyCheck == D_KEYLEFT) {
61 
62 draw_sprite_h_flip(b , p->playerImg , p->x += p->velX , p->y += p->velY);
63 }
64
65 pivot_sprite(b , p->gunImg , p->gunX , p->gunY , 0 , 10 ,
66 fixatan2(((p->crossY + 5) - p->gunY), p->crossX - p->gunX)); //gun
67 
68 circle(b , p->crossX , p->crossY , 0 , WHITE); //cursor
69
70 textprintf_ex(b , font , 10 , 10 , WHITE , -1 , "Health = %d" , p->health);
71 textprintf_ex(b , font , 10 , 20 , WHITE , -1 , "Ammo = %d" , p->ammo);
72 textprintf_ex(b , font , 10 , 30 , WHITE , -1 , "Distance = %f" , p->mouseDistance);
73}

Im just wandering if the collision detection (not really in collision detection right now) should be with the input or drawing. like the jump checks and stuff in the Player update.. the reseting of variables etc. thanks for any help. not sure if it makes much sense.

Kauhiz
Member #4,798
July 2004

I'm not sure if I understood your question. If you're asking when collision detection should be done, you should do it as a part of your logic. I don't know what the code you posted has to do with it, though.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

count
Member #5,401
January 2005

You HAVE TO do collision detection with the logic.
Otherwise it´s useless.

Imagine this:
1) logic: move left
2) draw: skipped (slow machine)
3) logic: move left through wall (collision)
4) draw: skipped
5) logic: move left
6) draw: check for collisions (there are none); draw.

So you walked through a wall because your collision detection has been skipped most of the time.

The proper way it should be like this:
1) logic: move left
2) draw: skipped (slow machine)
3) logic: move left through wall; check for collision; move left not possible; stay in front of the wall
4) draw: skipped
5) logic: move left still not possible; stay in front of wall
6) draw: draw.

Now you stand in front of the wall, where you should stand.
I hope you see the point.

dothedru22
Member #7,325
June 2006

ok that makes sense..the problem is my game works fine most of the time..but when I have other programs running it slows the game down obviously. but instead of just running slower the player just doesnt jump that high. like ususally the player jumps pretty high..but if i have a lot of programs running it just jumps a tad.. it may have to do with the jumping code and what not..(not sure?) ..or maybe the order of how I check collisions...but for wall collisions , etc shouldnt that be checked in the actual move functions. (instead of being checked all the time)

Quote:

If you're asking when collision detection should be done, you should do it as a part of your logic. I don't know what the code you posted has to do with it, though.

well. I was prolly gonna but stuff like : (is the player touching the ground? if he is not then put the player back on the ground) in the collision detection.. right now its with the input. but i assume it should be part of collision detection.

Kauhiz
Member #4,798
July 2004

About the jumping, it could be that the player jumps as high, but the screen doesn't get updated, so the frame where he's at the full height of the jump never gets drawn.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

joshua.tilson
Member #7,552
July 2006
avatar

I don't have an answer for you unfortunatley, but i am experiencing similar problems. I have been working on a PONG type game, my first game, first real program etc. It works fine on my home computer but when i compile it and run it at work, it runs abysmally slow...

count
Member #5,401
January 2005

http://www.allegro.cc/manual/miscellaneous/frequently-asked-questions-(faq)/general-problems/bd35ed0706dad8b5c3aa3b3a91cdba2c

When you write your code like the above example (which is pretty easy) your program will run with the same speed on any pc.

Very important is to seperate logic and drawing.

dothedru22
Member #7,325
June 2006

see my problem is what to put in updateinput and what to put in updatedraw..because i have some code to stop the mouse.(like keep the mouse in a certain area.) when i put this code in the input(logic too) the mouse goes to far. if it put this code in with the drawing it works fine. see im pivoting a sprite based on the mouse movement so i need the mouse to stay within a certain range. the jumping collision is also messed up..sometimes the player goes to far beyond the ground. if i put this in the drawing its fine. thats why its not working out for me to put input and drawing seperate. im sure its something im doing wrong. couldnt be also that im passing a million pointers and stuff into every function.

1if (p->keyCheck == D_KEYRIGHT) {
2 
3 if (mouse_y < (p->y - 14)) { mouse_y = p->y - 14; }
4 if (mouse_y > (p->y + 60)) { mouse_y = p->y + 60; }
5 
6 p->gunX = p->x + 25;
7 p->gunY = p->y + 25;
8 mouse_x = p->x + 100;
9
10 draw_sprite(b , p->playerImg , p->x += p->velX , p->y += p->velY); //player
11 
12 pivot_sprite(b , p->gunImg , p->gunX , p->gunY , 0 , 10 ,
13 fixatan2((mouse_y - p->gunY) , (mouse_x - p->gunX))); //gun
14
15 //circle(b , mouse_x , mouse_y , 3 , WHITE);
16 }

although im sure hand coding that is not how it should be done. as i was typing this i came across the position mouse function and the mouse range function..oh well.. and i guess my game was wierd because i had my bps_timer setting at 150. i didnt know it should be at like 60. everything runs slower obviously but it seems to make the game smoother.

Kauhiz
Member #4,798
July 2004

Your game should have 3 different parts; input, logic and output (mainly drawing). It sounds to me like you're missing the logic part alltogether. Stuff to do with the mouse you should have in your input part. Collision detection etc. goes into logic. Drawing should only have the draw functions, you should do any calculations necessary for drawing as a part of your logic loop.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

dothedru22
Member #7,325
June 2006

ok..yea that makes sense. but should logic be updated with the drawing or with the input. thats basically the question i have...thanks.

Trent Gamblin
Member #261
April 2000
avatar

dothedru22 said:

ok..yea that makes sense. but should logic be updated with the drawing or with the input. thats basically the question i have...thanks.

Update your logic together with but after your input and separate from your drawing.

dothedru22
Member #7,325
June 2006

alright thats cool. thanks.. i got it now..the main thing was the fps and the way i was doing the mouse.. i was sortof hand coding what the position mouse and the set mouse range function did..so it really didnt work out..thanks..later

Go to: