Closed Thread Icon

Preserved Topic: explaining the "GAME" programming concept (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=16544" title="Pages that link to Preserved Topic: explaining the &amp;quot;GAME&amp;quot; programming concept (Page 1 of 1)" rel="nofollow" >Preserved Topic: explaining the &quot;GAME&quot; programming concept <span class="small">(Page 1 of 1)</span>\

 
lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 06-07-2002 10:31

Hello,

I've always wondered....without using threads, multi tasking and stuff.....how could a normal procedural code make a game which does:

1. allows you to move your character on the screen
2. same time displays your score, time left
3. draw moving backgrounds and stuff
4. animates the enemy characters smoothly and intelligently...

I mean if i code this:

drawHero()
drawEnemy()

the enemy won't appear until the drawHere() returns...

so how does that happen w/o multi threads (interrupts, ...) and complex programming? (only using procedure code (straight forward code))


It would be nice if you can show me any code that moves two balls one upward and other downward in the same time and smoothly...


thanks,


lal.

WebShaman
Maniac (V) Mad Scientist

From: Happy Hunting Grounds...
Insane since: Mar 2001

posted posted 06-07-2002 10:44

At the same time??? Hmmm...well, maybe some of the code wizards will prove me wrong...but I don't think that is possible because of the way a computer works...one operation after another...unless you have a machine that does parrallel operations, that is...

One way would be to use the same object twice....then you could 'affect' them both at the same time...sort of like RayWs 5K page...

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-07-2002 11:04

the trick here is a) an basically turn based game (even if these turns happen very fast (like 30 times a second)
b) drawing on a hidden surface...

so you code would look like
while not terminated
{
handleInput()
decideAIAction()
calculateWhateverHappend(userinput + aiinput)
draw()
SwapHiddenAndVisibleSurface() //here's the magic :-)
}

draw being something like.
drawbackground() //(or scroll background)
drawscenery() //if you have some
drawHero()
drawEnemies()
drawStats()

so long,
Tyberius Prime

edit: Forgot the magic

[This message has been edited by Tyberius Prime (edited 06-07-2002).]

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 06-07-2002 11:35

ts,

quote:
handleInput()
decideAIAction()
calculateWhateverHappend(userinput + aiinput)
draw()
SwapHiddenAndVisibleSurface() //here's the magic :-)


before swapping hidden and visible...
when does it calculate and process and render w/o the user feels the delay?

i mean the old dos games that used to run on i386 were perfect and used to animate lots of characters at the same time...

i simply want a text based animation but i want to know how to animte things smoothly *apparently* at the same time...



silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 06-07-2002 11:39

Now, it was my understanding that most games ARE coded procedurally. In fact, very few games actually take advantage of multithreading capabilities or interrupts. In fact, windows 2k isn't a TRUE multi-threader (I may be wrong). It just does task switching VERY VERY fast so that it seems that things are happening simultaneously.

Okay, take your two ball example. It is extremely simple to make two balls APPEAR to move in different directions simultaneously. Take a standard screen refresh rate of 60 Hz. The entire screen is redrawn 60 times every second. Much faster than is noticeable to the human eye. If I move one ball during the odd frames and one ball during the even frames, you won't even notice that they are being drawn at different times. Although it is procedural, we use certain "tricks" to make it look simultaneous.

(smartass answer: take a circular transparent image with two black balls at either end. Rotate image. One ball moves up, one ball moves down. You never said "straight" up and down ).

the same thing happens in most games. Tyberius prime gave a great example. Although this looks like a lot of code, the time to execute each instruction is measured in nanoseconds. So, once all the input is processed, then the prgram does cleanup. For example, test for collision, update object statistics (i.e. if i am hit, i lose 5 hit points), gameAI, and so on. After one complete cycle, it all begins again.

However, this does stretch the limit of what you mean by procedural code. You could write it all out, but usually we use objects, classes, functions, etc. to make things a little simpler for us.

Example:
Code1:
int main()
{
int x;
x = 1;
x = x + 23;
x = x + 42;
return x;
}

or

Code2
int add_int(int x);

int main()
{
int x;
x = 1;
x = add(x,23);
x = add(x,42);
return x;
}

Now, you have code that does the same thing, but one is strictly procedural and the other uses a function to help simplify things. The computer doesn't care what you use either way, but the second example is easier to read and simpler for human beings to understand.

Anyway, sorry for getting longwinded, but I rarely get to discuss code anymore.

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 06-07-2002 12:36

still not convinced...
you tell me i have to make all my computations before the users notices the objects being moved not in the same time?

what if there is real thinking behind the scene...I'ld still see lots of objects being handleded at the same time..

take WarCraft, Red Alert, or any similar...you can have 1000+ unit in a game...and yet you see them all at the same time!

if you call computeAndAnimateUnit() 1000 times would them all return within less than a sec? so that my eye don't notice the flicker...?!

any books recommendation?

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 06-07-2002 13:19

I'm not sure about how it is done in the *big* games, but when a friend and I did a couple of games in BASIC we calculated the new color of every pixel on the screen (was only 320x200 with 256 colors) and then stored it in an array. When all of the changes had been made to the screen we pasted the array into the 'screen memory' (not sure what the right term for it is.. was a long time ago ) so that all of the changes seemed to happen at once.

If you want to I might be able to find a couple of the good, old .bas files and upload them...



_________________________
Anyone who has lost track of time when using a computer knows the propensity to dream, the urge to make dreams come true and the tendency to miss lunch.
- copied from the wall of cell 408 -

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 06-07-2002 14:03

Veneficuz, sure..i can read basic ...

massacre
Bipolar (III) Inmate

From: the space between us
Insane since: Dec 2001

posted posted 06-07-2002 22:14

damn, this forum is full of well- and hypertrained monkey brains!
~runs to the library and begin to study~

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 06-08-2002 05:55

Well, there are several ways to optimize your instructions so that you don't notice a thing. Games with a massive amount of units such as Red Alert and the like use several ways to fudge things and to optimize things.

Now, to use a REALLY simplified example. A Pentium 1GHz can execute processor instructions at the rate of approximately 1 billion instructions per second. Now, these are simple processor instructions. So, let's say a single line of code, on average, translates into ten processor instructions. That's still 100 million instructions per second. Now, this is a simple example but you can see how several processes can be executed in the space of a single second.

Also, there are ways of programming things that are very efficient. In fact, a few instructions can make a big difference. In my optimization course, I was able to cut runtime down from 45 minutes to seven seconds just by optimizing a single function and a few lines of code. It's all in the way the code works and understanding computers and coding.

Another thing is that animate and draw aren't done in one step. Usually, you would calculate everything before drawing. Example:

simple algorithm:
1. find all units onscreen
2. calculate unit position
3. update unit statistics
4. update unit animation frame
4. repeat for each unit onscreen
5. draw unit

Again, each of these steps is done VERY VERY fast and you get about 100 million instructions per second to execute(for a Pentium 1GHz).

Another thing is that drawing things to the screen is very fast becuase video memory and the back buffer do a really good job of putting things onscreen.

For a really good reference, try Game Programming in Directx by Mickey Kawick. It's a great book and it includes examples from Starcraft and Age of Empires (just to name a few). However, to really get into the stuff in the book, you need a good grasp of windows programming (Create_Window and all that bullshit that MS decided to use as API functions; they do the job, but I think their naming conventions sucks).


lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 06-08-2002 09:41

10x silence!

I also found this book very good: http://www.amazon.com/exec/obidos/ASIN/0596000065/102-6559996-9753704

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 06-08-2002 10:14

Hehe, right. That should be 1 GHz = 1 million cycles per second, not 1 billion. But it still means you get a shitload of instructions per second.

Sorry about the mixup.

:edit: Oh, and that Game programming gems book on the link lallous posted has some really great stuff in it. :edit:



[This message has been edited by silence (edited 06-08-2002).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 06-08-2002 10:20

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 06-08-2002 11:11

Yeah, what InI said.

I'm still at teh object oriented programming stage myself.

« BackwardsOnwards »

Show Forum Drop Down Menu