Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Main Loop  (Read 2137 times)
0 Members and 1 Guest are viewing this topic.
Offline lucazd

Senior Newbie





« Posted 2005-07-21 20:07:48 »

Hello people!,
I was programming games in c++, and now Im moving to Java world  Grin.

In my game design, I use a GameContext interface:
1  
2  
3  
4  
5  
6  
public interface GameContext {
  public void start();                         // called by GamePlayer
 public void logic(InputEvents i); // InputEvents are all my game inputs
 public void render(Screen s);    // Screen is another class from my package
 public void end();                          // called by GamePlayer
}


And a GamePlayer, that plays the GameContext:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
class GamePlayer {
  public void play(GameContext g) {
     g.start();
     // game loop
      g.logic(InputEvents.getInstance());
       g.render(Screen.getInstance());
     // end loop
    g.end();
  }
  public void stop() { /* code */ }
}


My question is how the main game loop is coded in java, which classes should I use, and how?.
(I already now the FPS and that stuff, but I don't know how to code it, the API is huge!!!  Smiley)
I read some books and tutorials, but they are old or very confusing  Undecided.

Thanks in advance!
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #1 - Posted 2005-07-21 20:34:08 »

Well you could do something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
public class Game {

  private boolean running = true;

  /** Constructor */
  Game() {
    /* Init stuff */
  }

  /** Loop until game over */
  public void loop() {
    while (running) {
      // Read Input
      // Update stuff
      // Draw world
    }
  }

  /** Game entry point */
  public static void main(String[] args) {
    Game game = new Game();
    game.loop();
    System.exit(0);
  }
}


Regarding what goes in the main loop, it depends on what graphics and sound libraries you use.
1  
2  
3  
4  
5  
6  
7  
- Java2D - 2D drawing built into java
- Java3D - 3D scenegraph based drawing.  This is an extension, which is not installed on users computers by default
- JOGL    - OpenGL interface backed by sun - another extension
- JOAL     - OpenAl interface (3D Audio - its an extension (often used in conjunction with JOGL)
- LWJGL - Provides OpenGL, OpenAL support, Also Mouse & Keyboard support
- Xith3D - 3D Scenegraph  (I think, no experience)
- JME     - 3D Scenegraph (I think, no experience)


If you're doing a FPS, you will really need one of the 3D extension libraries.  JOGL or LWJGL will provide the basic OpenGL, but you will need to draw all your models yourself as triangles.  The scenegraph based extensions will allow you to load models exported from maya or 3ds and position them in the world space, but scenegraphs aren't as fast as hand tuned code.

Time flies like a bird. Fruit flies like a banana.
Offline lucazd

Senior Newbie





« Reply #2 - Posted 2005-07-21 20:58:09 »

Thanks for your reply  Smiley.

Now I need:
1) Catch inputs events and store them.
2) Double Buffer
3) Run the game at constant rate

1) For the key events, I store each key state in a variable, set it true when its pressed and false when its released?. How is the best mechanism?
The only way to "listen" those events is implementing the Component interface? (The only way I know to read inpur is with Component addKeyListener. Is there another way?)

2) this is ok?:
class Screen extends JPanel {
   Image buffer = createImage(300,200);
   public getGraphics getGrapchis() { return buffer.getGraphics(); }
   public void flush() { buffer.dispose(); }
}

3) What should I use for run the game a constant rate?, should I use Threads?
EDIT
I don't like to use extensions, there isn't another way?  Undecided


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #3 - Posted 2005-07-21 21:31:49 »

While Swing and AWT graphics interfaces are great for applications, they don't really cut it for games.  I would suggest one of these combos instead:

JInput (keyboard, mouse, controllers) + JOGL (OpenGL graphics)

or

LWJGL (Does the lot)

I have to admit a bias to LWJGL (lwjgl.org) at this point as I've not written a game with JOGL.

With LWJGL, polling the keyboard is as easy as:
1  
2  
3  
if ( Keyboard.isKeyDown(Keyboard.KEY_F) ) {
 // Do something
}


Creating a full screen graphics of your chosen resolution:
1  
2  
3  
4  
5  
mode = org.lwjgl.util.Display.setDisplayMode(
  org.lwjgl.util.Display.getAvailableDisplayModes(
  640, 480, -1, -1, 16, 32, 60, 85),
  new String[] {"width=800", "height=600", "bpp="+
  Display.getDisplayMode().getBitsPerPixel(), "freq=60"});


OK, that isn't obvious.  It means create a window at least 640 x 480 and preferably 800 x 600, with at least 16bpp colour depth, preferably with the colour depth already in use, and a frame rate of between 60fps and 85fps, with 60fps preferred.

It's a bit complex, but safer then insisting on a fixed size and then the computer says it can't do it.

To go fullscreen use
1  
2  
Display.create();
Display.setFullscreen(true);


To draw on the screen, just use the OpenGL functions anywhere you feel like. e.g.
1  
2  
3  
4  
5  
6  
7  
8  
GL11.glBegin(GL11.GL_LINES);
{
  GL11.glVertex2i(hw-10, hh);
  GL11.glVertex2i(hw+9, hh);
  GL11.glVertex2i(hw, hh-9);
  GL11.glVertex2i(hw, hh+10);
}
GL11.glEnd();


To swap the double buffer finish off the loop with

1  
Display.update();


Maybe someone else will provide some JOGL hints for comparison.



Time flies like a bird. Fruit flies like a banana.
Offline lucazd

Senior Newbie





« Reply #4 - Posted 2005-07-21 21:56:27 »

THANKS A LOT!

you help me a lot.
I like something simple, I write 2d games.
These libs JInput, JOGL, LWJGL are 100% portable, right?.
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #5 - Posted 2005-07-21 22:35:32 »

The libraries consist of part Java, part native library.  Native libraries are generally available for PC, Mac OSX & Linux.

I thought you were doing a FPS & hence needed 3D.  If you only need 2D, you could use the built in Java2D functionality (no extensions).  For Java2D, it's worth looking at Kevglass' Space Invaders tutorial

http://grexengine.com/sections/externalgames/articles/Kevin%20Glass-Space%20Invaders%20101-1.html

Best of Luck
Alan Grin 

BTW, regarding running at constant rate.  It can be done, however different computers run at different speeds, so you might overframe on slower computers.  Personally I prefer to run flat out & calculate the elapsed time between frames and use that to scale the user's inputs.

Time flies like a bird. Fruit flies like a banana.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2005-07-21 23:35:45 »

These libs JInput, JOGL, LWJGL are 100% portable, right?.

Jogl only does fullscreen on windows (and even then, not very reliably). LWJGL does fullscreen on all platforms.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline lucazd

Senior Newbie





« Reply #7 - Posted 2005-07-22 03:54:20 »

oh, then I can't use these libs  Sad.

Well, anyway, that space invaders tutorial explains what I was looking for,
Thanks again!
Offline role

Junior Member




Java manmoth is coming!


« Reply #8 - Posted 2005-07-22 09:59:02 »

Just a suggestion, if you want something simple and for making 2D games.
Perhaps you want to check out my game engine, Golden T Game Engine (GTGE):
http://www.goldenstudios.or.id/products/GTGE/
It's a simple 2D game engine (you can take my word!), built on top of Java2D by default and can use OpenGL via JOGL/LWJGL for more performance.
It's new enough since no one never mention it, but it has been developed by 1.5 year already.
It's stable enough and many games has been developed with GTGE.

These are the list of some games developed with it, and some of them are made by GTGE users:
http://www.games.goldenstudios.or.id/
The games are hosted on different site that currently is down, so most of the games can't be played right now. Sad

However the one that not hosted by us still can be played, like this:
http://www.istisoft.net/online/cyberion/cyberion.html
http://www.cs.uoregon.edu/~rmcclure/GumballGauntlet.zip
http://aragorn.oslo.nith.no/~moehen/java/games/WM/Warehouse.jnlp

There are some commercial games, but the game developer not allow me to publish it on my site.

Golden T Game Engine - Java2D/OpenGL Game Engine
Golden T Website | Golden T Forum
Offline lucazd

Senior Newbie





« Reply #9 - Posted 2005-07-22 20:48:38 »

cool, I'll take a look   Cheesy.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!