Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Simple (threaded) Abstract GameEngine  (Read 2926 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2012-08-26 04:49:02 »

Hello JGO, haven't posted something i've programmed in a while lol.

Here's a simple abstract Game Engine i made today, all in 1 file Shocked.

JAR file: http://www.mediafire.com/?79g2w8c3loi23el

USAGE:
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  
import org.elite.engine.GameEngine; // Import the GameEngine class inside the GameEngine.JAR hurr hurr

public class ExampleUsage {

   static GameEngine g; // simple game instance.
  static int interval = 10; // GameEngine Cycle interval
 
   public static void main(String[] args) {
      GameEngine game = new GameEngine(interval) {
         public void process() { // Abstract method
           methodYouCreated(); // Task being done
        }
      };
      g = game; // simple game instance.
     g.start(); // Get it running xD
  }

   protected static void methodYouCreated() {
      // Do whatever you would normally do in a GameLoop
  }
}

// few getters & setters for fast changes :)
// GameEngine.getGameSettings().setCycleInterval(1234); >> Interval changed from: 10 to: 1234.
// GameEngine.getGameSettings().getCycleInterval(); >> returns CycleInterval.


Hope somebody finds this useful  Yawn

Offline matheus23

JGO Kernel


Medals: 108
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2012-08-26 11:33:51 »

erm... yeah... actually useful... but did you use Riven's sleep "logic"?

And also, I myself would like to have overridable methods like "getTimeMilliseconds" or "getTimeNanoseconds".

And in the end I wouldn't call something like that a "GameEngine" Cheesy

But other than that, pretty useful. This is something which gets reinvented a lot.

EDIT: Btw, what means threaded?
does that mean, that the g.start() will return, but the game loop will run in another thread or is there more magic? Cheesy

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline GabrielBailey74
« Reply #2 - Posted 2012-08-26 22:49:23 »

just got tired of having to program a new thread/game loop lol.

'simple' GameEngine.java:
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  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
package org.elite.engine;

public abstract class GameEngine {

   protected boolean running;
   protected static int interval;
   protected static GameSettings gameSettings;

   public static GameSettings getGameSettings() {
      return GameEngine.gameSettings;
   }

   public GameEngine(int update) {
      interval = update;
      gameSettings = new GameSettings();
      Thread t = new Thread() {
         public void run() {
            try {
               while (running) {
                  process();
                  Thread.sleep(getGameSettings().getCycleInterval());
               }
            } catch (RuntimeException e) {
               e.printStackTrace();
               System.exit(0);
            } catch (InterruptedException e) {
               e.printStackTrace();
               System.exit(0);
            } catch (Exception e) {
               e.printStackTrace();
               System.exit(0);
            }
         }
      };
      t.start();
      String c = "New GameEngine processed [Interval: " + interval + "].";
      System.out.println(c);
   }

   public void start() {
      running = true;
      System.out.println("Game loop started");
   }

   public void stop() {
      running = false;
      System.out.println("Game loop stopped");
   }

   public abstract void process();

   public static class GameSettings {
      public static void setCycleInterval(int toSet) {
         int currentInterval = interval;
         interval = toSet;
         String c = "Interval changed from: " + currentInterval + " to: " + interval + ".";
         System.out.println(c);
      }

      public static int getCycleInterval() {
         return interval;
      }
   }
}


Probably shitty lol, gonna make a couple people rofl.
Found it handy for me Smiley

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

Junior Newbie





« Reply #3 - Posted 2012-09-06 04:48:48 »

1  
2  
                  process();
                  Thread.sleep(getGameSettings().getCycleInterval());

Might be a good idea to calculate the elapsed processing time and then sleep the remaining amount (cycleInterval - elapsed).  

Edit: Obviously you would bypass calling the sleep method if that calculated time was <= 0.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2012-09-06 04:57:55 »

I think the OP got banned...just saying...
Offline GabrielBailey74
« Reply #5 - Posted 2012-09-06 12:29:11 »

naw lol

Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-09-06 21:40:38 »

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  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
package org.elite.engine;

public abstract class GameEngine {
+   private boolean running;
-   protected static int interval;
-   protected static GameSettings gameSettings;
+   private GameSettings gameSettings;

+   public GameSettings getGameSettings() {
+      return gameSettings;
   }

+   public GameEngine(GameSettings settings) {
+      gameSettings = settings;
     
      Thread t = new Thread() {
         public void run() {
            try {
               while (running) {
                  process();
                  Thread.sleep(getGameSettings().getCycleInterval());
               }
-            } catch (RuntimeException e) {
-               e.printStackTrace();
-               System.exit(0);
-            } catch (InterruptedException e) {
-               e.printStackTrace();
-               System.exit(0);
            } catch (Exception e) {
               e.printStackTrace();
               System.exit(0);
            }
         }
      };
      t.start();
      String c = "New GameEngine processed [Interval: " + interval + "].";
      System.out.println(c);
   }

   public void start() {
      running = true;
      System.out.println("Game loop started");
   }

   public void stop() {
      running = false;
      System.out.println("Game loop stopped");
   }

   public abstract void process();

   public static class GameSettings {
+      private int interval;
+
+      public GameSettings(int interval) {
+         this.interval = interval;
+      }
     
+      public void setCycleInterval(int toSet) {
         int currentInterval = interval;
         interval = toSet;
         String c = "Interval changed from: " + currentInterval + " to: " + interval + ".";
         System.out.println(c);
      }

+      public int getCycleInterval() {
         return interval;
      }
   }
}


- Since each 'catch' clause has the same code, you could just fold them all under Exception.
- Also, why is "interval" static?
- I changed the whole GameSettings system to make it more OO, where you store the settings in a GameSettings object and pass it in to the constructor. I actually like this idea!
- As mentioned in many other threads, there are better ways to sleep and it's recommended you use them to improve your game loop Smiley

Offline Sickan

Senior Member


Medals: 9



« Reply #7 - Posted 2012-09-07 06:03:25 »

@ra4king I pressed your post twice trying to get rid of the highlighting before I realized that you put them there as part of the post.

Cheers Smiley
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-09-07 08:00:30 »

Hehe yeah Riven added that feature of highlighting removed and added lines using - and + respectively. Pretty cool stuff.

Btw, I just edited the post to make some more changes for better code Smiley

Offline StumpyStrust
« Reply #9 - Posted 2012-09-07 09:15:06 »

Isn't this just a fancy Thread.sleep(1000/yourFPS)? I really don't see anything special here. There is no delta time or anything.

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.

Pippogeek (37 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (41 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!