Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  stock with concept - game stage/scene  (Read 776 times)
0 Members and 1 Guest are viewing this topic.
Offline edgar287

Junior Newbie





« Posted 2013-07-18 02:12:42 »

Hello guys ...
I have been programming my first game in lwjgl and I get stuck thinking how to make some sort of game stage/scene management in the main class (who create the Display and AL)

I make the following Interface
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
package interfaces;

import gametools.TimerTool;

public interface Stage {

   public void setUpObjects();
   
   public void draw();

   public void update(TimerTool timer);

   public void input();

   public void unloadObjects();

}

and an Abstract class for my game stages classes...
but Im not sure how to manage the navigation between one stage to another ...

I follow some tutorials that propose making something like this in the main class
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  
26  
27  
28  
29  
30  
public enum GameState {
      INTRO, MAIN_MENU, GAME;
   }

private void checkInput() {
      switch (state) {
      case INTRO:
         if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
            state=State.MAIN_MENU;
         }
         if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            Display.destroy();
            System.exit(0);
         }
         break;
      case GAME:
         if(Keyboard.isKeyDown(Keyboard.KEY_BACK)){
            state=State.MAIN_MENU;
         }
         break;
      case MAIN_MENU:
         if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
            state=State.GAME;
         }
         if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
            state=State.INTRO;
         }
         break;
      }
   }

and rendering and updating different stuff depending on the current stage ... but it is pretty basic ...
How can i manage this transition when each stage have his own class (extending my Abstract Stage class) and make each class capable to say to the main class go to n stage, unload his content and the main class load the next stage?, and if its possible want to render and update two stages at the same time (like OPTION over MAIN_MENU, and OPTIONS over GAME).

Has been thinking in static stages or something like that but nothing accomplish what I want ...  Clueless

can anyone make me some suggestions ...
thank you a lot...

by the way ... forgive me if my english is a bit primitive ... its not my native language
Offline Redocdam

Senior Devvie


Medals: 17



« Reply #1 - Posted 2013-07-18 20:50:21 »

I didn't see anything specific to OpenGL, so if I missed it then let me know.
That aside here are my thoughts on your predicament.

One Way of Doing This
I think the best way to do it is to split it up into 3 different classes or rather 2 classes and the all your sub classes who extend the abstract class.
Basically you have a handler class who is responsible for handling the basic graphics, like initializing any OpenGL and LWJGL Display settings.
It would also handle the transitioning between your stages.

For the stages themselves, they would contain all the objects you need to load, unload and how you want to draw stuff for that stage.

But let's take a look at the code first. It should make a bit more sense. Though do keep in mind I'm sure there's more ways to do this and I make no claims to being a pinnacle of proper programming practices.

Here's the stage Handler
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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
public class StageHandler{
   public static final int STAGE1 =1;
   public static final int STAGE2 =2;
   public static final int STAGE3 =3;
   public static final int OPTIONS =4;

   public Map<Integer, AbstractStage> stage = null; //A map of all your abstract stages  

   public boolean running = false; //If the rendering of the engine is running      
   
   private AbstractStage currentstage= null; //The current stage you want to draw
   
   private boolean change = false;   //If you need to change the stage
   private int newstage = 0; //The id value for a stage you want to change to  
   
   public void StageHandlder(){
      this.stage = new HashMap<Integer, AbstractStage>();
     
      this.stage.put(StageHandler.STAGE1, new Stage_One(this));
      this.stage.put(StageHandler.STAGE1, new Stage_Two(this));
      this.stage.put(StageHandler.STAGE1, new Stage_Three(this));
      this.stage.put(StageHandler.STAGE1, new Stage_Options(this));
   }      

   public void startRender(){ //Start the render loop
      this.running = true;
      this.changeStage(StageHandler.Stage1);

      while(running){ //Your drawing loop
         if(change){
            this.changeStage(this.newstage); //Change the stage
         }
         
         this.currentstage.draw(); //Draw all the stuff in your stage
      }
   }

   public void postStageChange(int stageid){ //Prep for a stage change
      this.change = true;
      this.newstage = stageid;
   }

   private void changeStage(int stageid){ //Change to a new stage
      if(this.currentstage!= null){ //Avoid the start up null
         this.currentstage.unload(); //Unload the stage you are transitioning from
      }
      this.currentstage = stage.get(stageid); //Get the stage you are transitioning to
      this.currentstage.load(); //Load all the stuff for the stage
      this.change = false;
   }
}


And here's the Abstract class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public abstract class AbstractStage{
   
   private StageHandler stagehandler = null; //Used as a reference back to the handler
   
   public AbstractStage(StageHandler stagehandler){
      this.stagehandler = stagehandler;
   )
   
   //Load, unload and drawing stuff
   public abstract void load();
   public abstract void unload();
   public abstract void draw();

   void changeStage(int stageid){
      this.stagehandler.postStageChange(stageid);
   }
}


Now in order to change stages, all you have to do from your extended abstract sub is call the stage you want to change to (let's use Stage3 as an example):
1  
this.changeStage(StageHandler.STAGE3);


This setup does leave the door open for calling out a stage and rendering it from within anywhere; the catch being it needs to already be loaded. For example, you would need load the Options stage when the options are entered and then unload them after they're exited. (I'm not a big fan of this, see below).
To do that, all you need to do this from wthin a stage:
1  
this.stagehandler.stage.get(StageHandler.OPTIONS).draw();


Some Things to Keep in Mind
I'm not really sure you would want to draw 2 stages at the same time. Particularly with the options menu. That seems like something which should be taken care of with a class which handles the drawing of windows. Trying to combine window logic with stage logic could get messy.

This is only the tip of inter-class communication. If you're running a multi-threaded application, you'll need to take care when passing messages back and forth.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #2 - Posted 2013-07-18 20:54:56 »

You might want to see how the Stage implementation works in LibGDX's scene2d.  The code is all very readable.  Your Stage is an interface, so you're already doing GDX one better there.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline edgar287

Junior Newbie





« Reply #3 - Posted 2013-07-19 00:33:15 »

So ... another class as a stage handler .... what a clever idea, i will try to implement this and see if its results to me .. thank you 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.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!