IT Community - Software Programming, Web Development and Technical Support

Game looping in j2me

This is a discussion on Game looping in j2me within the Game Development forums, part of the Software Development category; Hi there, in action games that require constant screen update I use a thread to run the main game loop. ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Game Development

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-10-2007, 03:28 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 324
amansundar is on a distinguished road
Default Game looping in j2me

Hi there,

in action games that require constant screen update I use a thread to run the main game loop. Inside the loop the game speed can be controlled with the use of some MILLISECONDS_PER_TICK variable like this:

..........
long startTime = System.currentTimeMillis();
repaint(0, 0, WIDTH, HEIGHT);
serviceRepaints();
long timeTaken = System.currentTimeMillis() - startTime;
if (timeTaken < MILLIS_PER_TICK) {
synchronized (this) {
wait(MILLIS_PER_TICK - timeTaken);
}
}
..........

One very fancy tip to make the screen update faster is to update any String items only when necessary. Drawing with drawString is insanely slow and can drag the execution of your game loop remarkably.
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-10-2007, 03:31 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Default Re: Game looping in j2me

hi,
I'm just working on a few game demos following the guidelines set out by the Guidelines_for_Game_Developers_v1_0.pdf file from Forum Nokia.

The one thing I can't figure out is where to put my main game loop. I need a bit of code that is continually updating and doing things like changing the game world or controlling non player characters.

Whats the best way to do this? If I just create a procedure with a

while (OK_TO_KEEP_RUNNING_GAME) {
// do main game loop here
}

then obviously this will seize all of the execution time and stop the midlet from processing events such as KeyPressed. Is this ok? I.e. should I continue with this sort of procedure and process the user input inside my main game loop instead of responding to a KeyPressed event?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-10-2007, 03:35 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Post Re: Game looping in j2me

Hai,
Quick extension to this. I went ahead and created a thread in my main game screen FullCanvas to handle the main loop.

I'm guessing this is the correct way to do it (works fine for Applets) but I wouldn't mind hearing a confirmation from someone just to set my mind at ease
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-10-2007, 03:38 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 324
amansundar is on a distinguished road
Default Re: Game looping in j2me

hi there,
Having a loop to handle updating the game is fine, the key events will still work, like all events. In my games I have a Timer (which executes a TimerTask) to control the speed of the game, when the timer task calls my updateGame() procedure I simply check booleans in my program (set by the key events) to see if keys have been pressed and act accordingly.
I also have a seperate class to handle the graphics output (FullCanvas) which I think is a tidier way of programming, with each loop of the game I get the canvas to repaint() and then update the game itself.
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-10-2007, 03:48 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Default Re: Game looping in j2me

Hello,

I normally create at least three classes:
- MIDlet
- Canvas/FullCanvas
- GameControl

MIDlet just launches the application and creates the two other classes. Then it passes control to GameControl.

I put the main control loop in GameControl as the name implies. I create (dynamically) an image there which is the size of the screen. I pass all keyPresses from Canvas to GameControl. In GameControl there are basically two types of methods:
- method(s) to respond to keyPresses (like, set current position of a piece 1 position up)
- drawLoop to draw game items in their current positions and maybe some animation. This loop might be in a timed thread or TimerTask if there are autonomous (non-player controlled) items moving around.

Canvas just has a paint method to draw Image and reference to the Image object from GameControl I'm drawing on. This way I can use FullCanvas or Canvas depending on the device.

When you get these sorted out, you can create few more classes, like one to hold, store and retrieve settings and current state of the game to RMS, another to hold language strings and finally one to send high scores and load additional levels (in an own Thread) to a server.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-10-2007, 03:53 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Default Re: Game looping in j2me

geek,
Do you normally use the one canvas for all your graphic displays?

At the moment I've created a splash screen canvas, an options/start menu canvas and an in-game canvas. Problem is: when ever I call setCurrent(displayable) to move between screens you can see the application menu (7650) flash into view for a split second.

Aside from a few little quirks this seems to be quite a simple platform to write good games for. My first few demos have come together impressively quickly.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-10-2007, 03:56 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 324
amansundar is on a distinguished road
Default Re: Game looping in j2me

Hello,

I have similar structure to vinoth's for my game midlets. I have
- MIDlet for starting and stopping application and creation of the other objects
- GameCanvas (either FullCanvas or Canvas, selectable according to device) for just painting the image I want to show
- GameControl for contolling the functionality of the game
- Settings for language strings, game settings and high score list saving/loading

You really don't need any more than one canvas instance (or two if you implement both FullCanvas and Canvas) as all it does is to draw the game image in paint method and catch and pass the keypresses to GameControl. GameControl has a mutable image, which it manipulates and copies to the image, which canvas instance draws after repaint method is called.
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-10-2007, 05:12 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Default Re: Game looping in j2me

my gaming loop as blow ,it runs very smooth on emulator of JWKD. but when i run on emulator of nokia S60,it became very very slow,if i change "AniThread.sleep(TIMESLEEP-Rtime)" to "AniThread.yield()" it became very fast,but too fast for my game(FPS too high), i need use modth of "sleep" or "wait" ,but when i use them the game will be slow,somebody help me?

while (running)
{
Rtime=System.currentTimeMillis();
myMap.MoveMap();
repaint(0,0,canvasWidth, canvasHeight);
serviceRepaints();
Rtime=System.currentTimeMillis()-Rtime;
intsleep=(TIMESLEEP-Rtime);
if (Rtime < TIMESLEEP)
{
//AniThread.yield();
AniThread.sleep((int)TIMESLEEP-Rtime);
}
}
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 09-10-2007, 05:16 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 324
amansundar is on a distinguished road
Default Re: Game looping in j2me

In a case like this, Thread.yield() is likely to be essentially the same as Thread.sleep(0). Yielding only pauses for long enough to allow other threads a chance to run... if there are no other threads, there is no pause.

I'm assuming that this code is in the run() method associated with AniThread... note that yield() and sleep() are both class methods, and are not executed by a specific instance. You cannot cause a specific thread to yield or sleep, only the currently executing thread.

Quote:

myThread.sleep (100);

is a long way around for saying:

Quote:

Thread.sleep (100);

Don't rely on the emulator for accurate timing. The code you have appears reasonably OK... if it runs too fast with no sleep at all, then leave the sleep in. Try the application on the real device if you want to know how fast it will be.
__________________
cheers
Aman

Last edited by amansundar : 09-10-2007 at 05:21 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Game Physics nhoj Game Development 4 11-04-2008 03:26 AM
Looping through Fields Pvinothkumar HTML, CSS and Javascript Coding Techniques 6 10-16-2007 04:08 AM
looping structures in Ruby? vadivelanvaidyanathan Ruby 1 08-18-2007 01:53 AM
Best Game Lighting nhoj Game Development 1 04-29-2007 02:11 PM
Your favorite game? Pixel The Lounge 6 03-16-2007 01:48 AM


All times are GMT -7. The time now is 05:36 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0