Archived

This topic is now archived and is closed to further replies.

Malikive

Newb Programmer Questions (Pac-Man Clone)

Recommended Posts

Malikive    139
Heyas everyone...I've been writing a pac-man clone over the past few weeks and I've got a few questions... first off, I'm very new to coding and I don't know many optimized ways to write programs or some of the functions to use to achieve a desired goal... On my computer, an AMD athlon XP ~ 1.7, 512 RAM, and a GF3 gfx card, I calibrated the speed to run at a desired level...I took the game and put it on a celeron 733 and it lags like never before...for a windowed game like pac-man with bitmap graphics what is the best way to do the timing? currently I only process things while the game is playing with a timer like this...
int Timer = 0;
if (Timer == 0)
     Timer = GetTickCount();
if (GetTickCount() - Timer < 1000/23)
{
     // Process User input, move characters, and draw the display

     Timer = 0;
}
Inside the If (GetT....<1000/23) section, I also use timers based on the GetTickCount() function to move objects at certain times...i.e. the ghosts and the eyes blinking.. It's bugging me that I'm doing something wrong and I just don't know where to look to find out if that's the way I should run things or not... Also, I use DDutil.h for the function DDLoadBitmap() to load my bitmap images in...is that a bad thing? or for a program like this is that ok, or is there a better way to load bitmaps into surfaces. Oh, one other thing...I move pac-man by 4 or 5 pixels when he moves...should the images move at 1 pixel at a time but refresh faster? And when I don't lock the entire Process_Game_Play stuff to a certain framerate, it slows down my whole system.. Any answers or suggestions to any of the previous questions would be GREATLY appreciated as I am a newcomber in the game programming industry...I wanna make things work, and I want em to work RIGHT!! Thank you all for reading and anyone who responds... Josh // Pac-Man Demo to date with many bugs and no where near complete....list notable things to change in responses as well please... Pac-Man Clone [edited by - Malikive on August 6, 2003 11:51:25 PM] [edited by - Malikive on August 6, 2003 11:52:03 PM] [edited by - Malikive on August 6, 2003 11:52:54 PM]

Share this post


Link to post
Share on other sites
ACAC    122
Your timing is all wrong..

static int prevTime = timeGetTime(); //means this value never resets ( static )

if(timeGetTime()- prevTime < 1000/30)//30fps

{
//do logic

prevTime = timeGetTime();
}

hope this helps.
Ash.

Share this post


Link to post
Share on other sites
BillPo    122
quote:
Original post by Malikive
And when I don''t lock the entire Process_Game_Play stuff to a certain framerate, it slows down my whole system..



Since you''re using GetTickCount() I''m assuming you''re writing your app as a Win32 program, perhaps in straight win32 code (hopefully). If this is the case then your whole system is probably slowing down because your program ends up hogging the cpu. In your message loop you should check if the program is active, if not cut back on your processing and let windows do its own stuff. Calling GetMessage will wait until a message arrives, allowing windows to do its stuff. PeekMessage returns immediately whether there''s a message or not, so using that only would make your program never stop to give windows its turn with the CPU.

Anyway, look up articles about the game/message loop in windows for more detailed info. I think there''s atleast one article about this on GameDev. Can''t remember if its any good.

Another thing you can do is download public source code (for say public game engines, or quake 2) and see how they implement their main game and message loops.

-out

Share this post


Link to post
Share on other sites
Malikive    139
Ok, ACAC you say I''m doing my timing completely wrong...few questions about that....should I declare the timer as a global stated like... static int Timer1 = timeGetTime()... or declare it in the GAME_PLAY section of the code the same way. and just for my own purposes...what does the static flag do exactly?

One other comment...was my way of doing timing that much different then the one you proposed?

I''ve also posted my source code that is extremely lacking because I realize I need some help...as of right now it''s all in one file a .cpp...if anyone could offer some suggestions for programming techniques or anything I woudl appreciate it...also any things that I am doing completely wrong and should stop doing immediately would also help out a lot. =)

BillPo....are you saying I should use GetMessage instead of PeekMessage to cooperate with windows? Again, I have the source code which at the very bottom shows my windows messaging loop if you are interested. This is a windowed program (my first attempt at making a windowed) so if it''s active I still want my computer to load like normal and not lag everything up if that''s understandable or even logical at all.

Again, I just wanna get on the right track before I keep up these bad habits and especially figure out how to do this timing so it''s pretty standard on any system.

Pac-Pac source code

Thanks, Josh

Share this post


Link to post
Share on other sites
smart_idiot    1298

static int prevTime = GetTickCount(); //means this value never resets ( static )

int currentTime = timeGetTime(); // We''ll use this variable since it might change between the two calls to GetTickCount.

if(currentTime - prevTime < 1000/30)//30fps

{
//do logic

prevTime = currentTime;
}
else
{
// We don''t have anything to do at the moment

// so lets not hog the CPU while we wait.

Sleep(10);
}

Share this post


Link to post
Share on other sites
smart_idiot    1298
static means that there is only one instance of the value, and it''s value will persist between function calls.

Another way to to do time independent animation (note: requires positions to be stored as floats or doubles)


static unsigned int prevTime = GetTickCount(); //means this value never resets ( static )

unsigned int currentTime = timeGetTime(); // We''ll use this variable since it might change between the two calls to GetTickCount.

if(currentTime >= prevTime) // This check in nessecary incase the counter ever wrapped around. If it did, it would be as if time suddenly jumpped backwards 49.7 days.

{
float /* or double */ speed = (currentTime-prevTime)/1000.0;

// say we want to move something right at 1 unit per second.

something.x += speed;

// say we want to move something else left at 5 units per second.

something_else.x -= speed*5;

// say we want to move an object down at half a unit per second.

object.y += speed/2;
}
prevTime = currentTime;


This way the game can run as fast or as slow at the computer will allow, without it appearing to run at different speeds.

Share this post


Link to post
Share on other sites
BillPo    122
Yeah, use GetMessage, but in conjunction with PeekMessage. How you do it really depends on what kind of game it is and how its set up. Its actually very complicated when you try to do multiplayer and single player games in the same binary. But your game is just pacman, so shouldn''t be too much trouble.

K, first thing, I''m going to assume that when your player alt-tabs or clicks to another window, basically, when your game window loses focus, that you will suspend the game somehow. Could be a simple pause, or could be some really complex set up that pauses game play but not rendering and sound or whatever.

Now basically, set up a boolean flag somewhere that you can access from your window proc as well as your main function. In your window proc set the flag when the application loses focus, and unset it when it regains focus. Then in your message loop do something like this
while(TRUE)
{
bMessageReturned = true;
if (bGameLostFocus)
{
if (!GetMessage(&msg, NULL, 0, 0))
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(!(Game_Main()))
return(msg.wParam); // Main Game Loop here

}
}


Now this probably isn''t perfect, but I hope it gives you the basic idea. With this set up the game would not update at all when the window loses focus. You can modify it to make it work the way you want

-out

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by smart_idiot

static int prevTime = GetTickCount(); //means this value never resets ( static )

int currentTime = timeGetTime(); // We''ll use this variable since it might change between the two calls to GetTickCount.

if(currentTime - prevTime < 1000/30)//30fps

{
//do logic

prevTime = currentTime;
}
else
{
// We don''t have anything to do at the moment

// so lets not hog the CPU while we wait.

Sleep(10);
}




so you would put your whole game in the if block?

Share this post


Link to post
Share on other sites