Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 835 | chris | 1 | package com.gebauz.bauzoid.app; |
| 2 | |||
| 3 | import com.badlogic.gdx.ApplicationListener; |
||
| 4 | import com.gebauz.bauzoid.game.Game; |
||
| 5 | import com.gebauz.bauzoid.game.Game.IGameFactory; |
||
| 6 | |||
| 7 | /** Base class for applications using Bauzoid engine. |
||
| 8 | * Implemented as a Generic, T is reached through to Game<T> |
||
| 9 | */ |
||
| 10 | public abstract class BauzoidApp implements ApplicationListener |
||
| 11 | { |
||
| 12 | public interface IAdHandler |
||
| 13 | { |
||
| 14 | public void show(boolean visible); |
||
| 15 | } |
||
| 16 | |||
| 17 | private String mGameTitle = "BauzoidGame"; |
||
| 18 | protected Game mGame = null; |
||
| 19 | private long mSystemTimeMs = System.currentTimeMillis(); |
||
| 20 | private boolean mAdsEnabled = false; |
||
| 21 | |||
| 22 | private IGameFactory mGameFactory = null; |
||
| 23 | private IAdHandler mAdHandler = null; |
||
| 24 | |||
| 25 | public BauzoidApp(String title, boolean adsEnabled) |
||
| 26 | { |
||
| 27 | this(title, adsEnabled, null, null); |
||
| 28 | } |
||
| 29 | |||
| 30 | public BauzoidApp(String title, boolean adsEnabled, IGameFactory gameFactory) |
||
| 31 | { |
||
| 32 | this(title, adsEnabled, gameFactory, null); |
||
| 33 | } |
||
| 34 | |||
| 35 | public BauzoidApp(String title, boolean adsEnabled, IAdHandler handler) |
||
| 36 | { |
||
| 37 | this(title, adsEnabled, null, handler); |
||
| 38 | } |
||
| 39 | |||
| 40 | public BauzoidApp(String title, boolean adsEnabled, IGameFactory gameFactory, IAdHandler handler) |
||
| 41 | { |
||
| 42 | mGameTitle = title; |
||
| 43 | mAdsEnabled = adsEnabled; |
||
| 44 | mGameFactory = gameFactory; |
||
| 45 | mAdHandler = handler; |
||
| 46 | } |
||
| 47 | |||
| 48 | @Override |
||
| 49 | public void create() |
||
| 50 | { |
||
| 51 | mGame = new Game(this, mGameTitle, mAdsEnabled, mGameFactory); |
||
| 52 | mGame.init(); |
||
| 53 | |||
| 54 | initGame(); |
||
| 55 | |||
| 56 | mSystemTimeMs = System.currentTimeMillis(); |
||
| 57 | } |
||
| 58 | |||
| 59 | @Override |
||
| 60 | public void dispose() |
||
| 61 | { |
||
| 62 | exitGame(); |
||
| 63 | |||
| 64 | mGame.exit(); |
||
| 65 | mGame = null; |
||
| 66 | } |
||
| 67 | |||
| 68 | @Override |
||
| 69 | public void pause() |
||
| 70 | { |
||
| 71 | mGame.pause(); |
||
| 72 | } |
||
| 73 | |||
| 74 | @Override |
||
| 75 | public void resume() |
||
| 76 | { |
||
| 77 | mGame.resume(); |
||
| 78 | } |
||
| 79 | |||
| 80 | @Override |
||
| 81 | public void resize(int w, int h) |
||
| 82 | { |
||
| 83 | mGame.resize(w, h); |
||
| 84 | } |
||
| 85 | |||
| 86 | @Override |
||
| 87 | public void render() |
||
| 88 | { |
||
| 89 | float deltaTime = updateInternalTimer(); |
||
| 90 | |||
| 91 | //deltaTime = 1/5.0f; |
||
| 92 | |||
| 93 | mGame.update(deltaTime); |
||
| 94 | |||
| 95 | /*try |
||
| 96 | { |
||
| 97 | Thread.sleep(500); |
||
| 98 | } |
||
| 99 | catch (InterruptedException e) |
||
| 100 | { |
||
| 101 | // TODO Auto-generated catch block |
||
| 102 | e.printStackTrace(); |
||
| 103 | }*/ |
||
| 104 | |||
| 105 | mGame.render(); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** Update the internal timer and return the resulting deltaTime. Can be used for times when a single method will use a lot of time. */ |
||
| 109 | public float updateInternalTimer() |
||
| 110 | { |
||
| 111 | long currentTimeMs = System.currentTimeMillis(); |
||
| 112 | long deltaTimeMs = currentTimeMs - mSystemTimeMs; |
||
| 113 | mSystemTimeMs = currentTimeMs; |
||
| 114 | |||
| 115 | return (float)deltaTimeMs / 1000.0f; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** Called after the engine internals have been initialized - this is the point to kick off the initial game state. */ |
||
| 119 | public abstract void initGame(); |
||
| 120 | |||
| 121 | /** Called before the engine internals are cleaned up. */ |
||
| 122 | public abstract void exitGame(); |
||
| 123 | |||
| 124 | /** Get the instance of the game. */ |
||
| 125 | public Game getGame() |
||
| 126 | { |
||
| 127 | return mGame; |
||
| 128 | } |
||
| 129 | |||
| 130 | public long getSystemTimeMs() |
||
| 131 | { |
||
| 132 | return mSystemTimeMs; |
||
| 133 | } |
||
| 134 | |||
| 135 | public void showAds(boolean visible) |
||
| 136 | { |
||
| 137 | if (mAdHandler != null) |
||
| 138 | mAdHandler.show(visible); |
||
| 139 | } |
||
| 140 | |||
| 141 | public void setAdHandler(IAdHandler handler) |
||
| 142 | { |
||
| 143 | mAdHandler = handler; |
||
| 144 | } |
||
| 145 | |||
| 146 | } |