Archived

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

geez

Way to execute code while drawing??

Recommended Posts

geez    122
Hi, i would like to know if there is a possible way to keep drawing images while another function run or calculates stuff... I dont know i u understand, i want my program to keep moving images while it thinks, calculates. I though maybe at the end of my draw() fonction i would place a call to another function lets say think(). So between each frame the program thinks for a while (lets say i give him 15 milisec to think) and when its over, draws the other frame, then he continues to think from where he were for another 15milisec. etc... But its going to be complicated, so i was wondering if some1 had an idea on how to do this more easily. Thank you. I hope u understand what i mean.

Share this post


Link to post
Share on other sites
fractoid    703
What you said, doing your own timeslicing, is going to be the easiest. You can create a new thread that will run concurrently with your main thread, but while that''s easier to start off with, it''ll be impossible to make it robust unless you know exactly what you''re doing, and even then multithreading can be tricky.

Share this post


Link to post
Share on other sites
Rian    121
Threads might help, but they can raise far worse problems (at least with threads managed by the OS). You could create one thread that draws continuously as fast as it can and another thread that "thinks" as fast as it can, and the OS will bounce back and forth in small quanta, sending commands to the processor in small chunks and storing the results per thread. However, be warned that if both threads access the same data at any point, synchronization problems arise, and the problem becomes infinitely more complex.

If you use two threads, make sure that each thread only accesses variables disjoint from the other thread, ie. if you make the "Draw" thread deal with draw variables then insure that the "Think" thread never tries to manipulate any of the same variables, or even any variables that affect draw variables, or in anyway are related to draw variables.

The problem is this: you can never predict when the OS will jump between threads. Surprisingly, it will usually be in the middle of a command or line of code. So, if one thread tries to move an object by adding (1,1,1) to its position, and midway between the translation the other thread tries to draw the object, it could potentially be in at least three different positions, depending upon when the focus jumps. Also, if two threads try to access the same address in memory or if one thread changes the address of a variable without the other thread knowing, the app will crash and no one will understand why. These events are rare, but when the same calculations cycle through at hundreds of frames per second, they WILL occur, and more often then you might think. Trust me one this one!

For your problem, I suggest scripts instead. You can have your app draw every frame and then execute a few lines of code from a script, save the script''s state, and then repeat. This ensures that no synchronization problems arise. Read up on scripts. They are powerful tools and can make virtually any project easier, especially games.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
OMG, first of all, thread management is easy. It does not generate problems that are infinitely complex. Sharing data between threads can be implemented in a number of ways, but why don''t you try using Semaphores first. What language are you using to program?? I can give you tips for either C++ or Java threading.

Share this post


Link to post
Share on other sites
geez    122
Hi, thank you guys for ur reply im gonna look at both solutions and see which one will be better, but i think im gonna go with threads since anonymous poster suggested it. (he seems like a real good programer because of the other replies ive seen from him in different subject) Right now Im programming this game in C++ using VS.net, and actually, i dont know what Semaphores are. Looks like ive got some reading to do, ill look over the internet. Any1 know some good sites on threads, Semaphores and scripts?? Thanks.

Share this post


Link to post
Share on other sites
Thunder_Hawk    314
quote:
Original post by geez
he seems like a real good programer because of the other replies ive seen from him in different subject


Ummm, "Anonymous Poster" is the name designated to anyone who posts without first inputing a valid username. In other words, the chance of two APs in two different threads being the same person is rather low (heck, this is often true of two AP posts in just one thread).

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
geez    122
lol! I though it was his logging but ur right : its not written "member since:.... blah blah blah" besind his name. Thanks for the remark.

[edited by - geez on March 31, 2004 1:37:22 PM]

Share this post


Link to post
Share on other sites
llvllatrix    340
quote:

Hi, thank you guys for ur reply im gonna look at both solutions and see which one will be better, but i think im gonna go with threads since anonymous poster suggested it. (he seems like a real good programer because of the other replies ive seen from him in different subject) Right now Im programming this game in C++ using VS.net, and actually, i dont know what Semaphores are. Looks like ive got some reading to do, ill look over the internet. Any1 know some good sites on threads, Semaphores and scripts?? Thanks.



I recommend not using threads unless you need to. Take a look at the nehe basecode. It has exactly the structure you r looking for; update then draw.

Threads are really only applicable in certain situations; nd no you dont need semaphores if your threads are accessing data simultaneously. There are other options like critical sections (essentially using bools to control data access), events, and mutexes.

Threads come in handy in certain situations. For example if you are accessing the disk and need to process an animation you can use a thread. In windows they are used when you have a lengthy computation but need the app to respond to the user at the same time. In a game, however, threads only cause unnecessary overhead; nd that you cant have.

If you are just starting out the most important thing is to finish the project you are working on. Dont try to over complicate things simply because you want to use something fancy; there are tons of more structural and design issues you''ll have to deal with if you''re programming a game. Thats why many video games are still written in c.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Although matrix''s advice is good if you are actually concerned about deadlines & you have to turn in a product & thus don''t want to over-complicate things, but what if you really wanna learn something new? Consider threads as you have been, they are a good thing to learn... & how better to learn them then on your own time & for a fun project. Matrix''s advice on what NOT to use threads for is also correct. You don''t need seperate threads for the "draw" & "update" portions of your game. Look into something called a game-loop, its where you do computations for 1 frame, draw that frame, & then repeat. Take a look at this thread for possible uses for threads:

<a href = "http://www.gamedev.net/community/forums/topic.asp?topic_id=206444">a thread on threads in games</a>

I have a game with several entities involved & I use seperate threads to run each of the enemy entity''s AI modules. It actually removed some bad timing issues in the game & there are nifty ways you can threads. For instance, my entities all run a path-finding routine that can take quite a while even though its optimized. So what was happening was that on the frame where they need to find a new path you would see a small pause... cus that AI module had to run. So instead of running the whole AI module in just 1 frame each time the entity had to make a decision or chance direction, I would run it in a thread & the entity would only update when it finished. That way it spread the computation out over several frames. I do it a little differently, but thats the easiest way to explain it.

Share this post


Link to post
Share on other sites
llvllatrix    340
quote:

I have a game with several entities involved & I use seperate threads to run each of the enemy entity''s AI modules. It actually removed some bad timing issues in the game & there are nifty ways you can threads. For instance, my entities all run a path-finding routine that can take quite a while even though its optimized. So what was happening was that on the frame where they need to find a new path you would see a small pause... cus that AI module had to run. So instead of running the whole AI module in just 1 frame each time the entity had to make a decision or chance direction, I would run it in a thread & the entity would only update when it finished. That way it spread the computation out over several frames. I do it a little differently, but thats the easiest way to explain it.




thats a good example

Share this post


Link to post
Share on other sites
geez    122
Ok, hmm, let me tell you what my project is, so maybe you can help me better on which procedure to take. Im coding a chess game. My graphical interface is done : i can click and move the pieces on the board and i have to respect all the movements and the rules of the game. Now i would like to create a computer opponent, i dont want it to be a master undefeatable computer, but just to think that if he moves his piece to that place he can loose it. That can take a while to determine considering that he must look over all the movements possibilities and create a table of possible movements he can make. That cant be done between two frame, (the clock will stop running for example), i already made a dos version and that can take from 5sec up to 10sec "thinking" and building that table. So, if i want my clock and my animations to keep on moving i need another way : like thinking in background which could be done with threads right? ( im coding in c++ using VS .net with opengl libs and NEHE base code )

Share this post


Link to post
Share on other sites
llvllatrix    340
You got it

I suggust programming some sort of foreward processing algorithm; that is you build tables while your opponent is moving. The trick is to make it efficient.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
AH HA... you are coding an AI for a perfect information turn-based game. that is one of the specialties... you probably want to give the computer opponent more look-ahead than just 1 move & you can probably even give each piece a score, so that instead of just looking ahead a couple moves for whether or not he wins or loses... he could look ahead a few moves & just take the move that would maximize his score. (say 1 point for a pawn & 2 for a rook, & 3 for a queen, etc...)
If you are not familiar with such a program... let me know, & I will post something very useful tonight when I get home.
FYI - I won the AI competition at Purdue University in 2003.

Share this post


Link to post
Share on other sites
geez    122
Heh, i could certainly use ur help. Now that my multithread problem is resolved, im getting into AI . But I have a question actually if u could help me out: its how to give the movement a score?? maybe if he eats some1 else without beeing eaten back that would give him like +2 or to protect another piece +1 etc?? And im thinking about giving the choice to the player to choose the level of the comp: level 1, 2, 3.... which means, lets say his choice was lvl 1: the comp will play random moves, lvl 2: he makes all of is possible movements, then create a table of best moves, (looks 1 move ahead), lvl 3: he moves one of his possible movement, looks at all the possible movement the player can make, then pass on to his other possible movements, and so on... (looks 2 move ahead), lvl 4: moves his first possible movement, create the player''s possible movements, play the player''s first possible movement, create a table of score, passe on to the player''s second possible movement, update the table of score, third, fourth etc., then go back to the start then move his second possible movement, and here we go again... (looks 3 move ahead). And i think Im gonna stop there, because the player will make his move then come back tomorrow to find out that the comp is still thinking. Is this the best way to do so? I know that normal chess games just have a database of pro''s best moves, and they select the best one thatll fit into the game. But that''ll be too complicated for me, since i dont have that kind of database.

AI is the futur. :O)

Share this post


Link to post
Share on other sites