-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Renamed GameManager to Engine * Added EngineSettings class * Moved Scene from SceneWindow to Engine * Moved Renderer from SceneWindow to Engine * Moved Animator from SceneWindow to Engine
- Loading branch information
John
committed
Oct 31, 2018
1 parent
b260600
commit 8c3165d
Showing
20 changed files
with
454 additions
and
553 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
package com.johnsproject.jpge; | ||
|
||
import com.johnsproject.jpge.dto.Scene; | ||
import com.johnsproject.jpge.graphics.Animator; | ||
import com.johnsproject.jpge.graphics.Renderer; | ||
import com.johnsproject.jpge.graphics.SceneWindow; | ||
import com.johnsproject.jpge.io.KeyInputManager; | ||
import com.johnsproject.jpge.io.MouseInputManager; | ||
import com.johnsproject.jpge.physics.PhysicsAnimator; | ||
|
||
/** | ||
* The Engine class keeps everything working. | ||
* | ||
* @author John´s Project - John Konrad Ferraz Salomon | ||
*/ | ||
public class Engine { | ||
|
||
private static Engine instance; | ||
public static Engine getInstance() { | ||
if (instance == null) { | ||
instance = new Engine(); | ||
} | ||
return instance; | ||
} | ||
|
||
private Thread graphicsThread; | ||
private Thread inputThread; | ||
private Thread physicsThread; | ||
|
||
private EngineSettings settings = new EngineSettings(); | ||
private Scene scene = new Scene(); | ||
private Renderer renderer = new Renderer(); | ||
private Animator animator = new Animator(); | ||
private KeyInputManager keyManager = new KeyInputManager(); | ||
private MouseInputManager mouseManager = new MouseInputManager(); | ||
private PhysicsAnimator physicsAnimator = new PhysicsAnimator(); | ||
private SceneWindow sceneWindow = null; | ||
|
||
public Engine() { | ||
startGraphicsThread(); | ||
startPhysicsThread(); | ||
startInputThread(); | ||
} | ||
|
||
/** | ||
* Starts the engine. | ||
*/ | ||
public void play() { | ||
if (!settings.isPlaying()) { | ||
settings.setPlaying(true); | ||
startGraphicsThread(); | ||
startPhysicsThread(); | ||
} | ||
} | ||
|
||
/** | ||
* Pauses the engine. | ||
*/ | ||
public void pause() { | ||
if (settings.isPlaying()) { | ||
settings.setPlaying(false); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the {@link EngineSettings settings} of this engine. | ||
* | ||
* @return {@link EngineSettings settings} of this engine. | ||
*/ | ||
public EngineSettings getSettings() { | ||
return settings; | ||
} | ||
|
||
/** | ||
* Returns the {@link Scene} used by the engine. | ||
* | ||
* @return {@link Scene} used by the engine. | ||
*/ | ||
public Scene getScene() { | ||
return scene; | ||
} | ||
|
||
/** | ||
* Sets the {@link Scene} that the engine should use. | ||
* | ||
* @param scene {@link Scene} to set. | ||
*/ | ||
public void setScene(Scene scene) { | ||
this.scene = scene; | ||
} | ||
|
||
/** | ||
* Returns the {@link Renderer} used by the engine. | ||
* | ||
* @return {@link Renderer} used by the engine. | ||
*/ | ||
public Renderer getRenderer() { | ||
return renderer; | ||
} | ||
|
||
/** | ||
* Returns the {@link Animator} used by the engine. | ||
* | ||
* @return {@link Animator} used by the engine. | ||
*/ | ||
public Animator getAnimator() { | ||
return animator; | ||
} | ||
|
||
/** | ||
* Returns the {@link KeyInputManager} used by the engine. | ||
* | ||
* @return {@link KeyInputManager} used by the engine. | ||
*/ | ||
public KeyInputManager getKeyInputManager() { | ||
return keyManager; | ||
} | ||
|
||
/** | ||
* Returns the {@link MouseInputManager} used by the engine. | ||
* | ||
* @return {@link MouseInputManager} used by the engine. | ||
*/ | ||
public MouseInputManager getMouseInputManager() { | ||
return mouseManager; | ||
} | ||
|
||
/** | ||
* Returns the {@link PhysicsAnimator} used by the engine. | ||
* | ||
* @return {@link PhysicsAnimator} used by the engine. | ||
*/ | ||
public PhysicsAnimator getPhysicsAnimator() { | ||
return physicsAnimator; | ||
} | ||
|
||
/** | ||
* Returns the {@link SceneWindow} used by the engine. | ||
* | ||
* @return {@link SceneWindow} used by the engine. | ||
*/ | ||
public SceneWindow getSceneWindow() { | ||
return sceneWindow; | ||
} | ||
|
||
/** | ||
* Sets the {@link SceneWindow} that the engine should use. | ||
* | ||
* @param sceneWindow {@link SceneWindow} to set. | ||
*/ | ||
public void setSceneWindow(SceneWindow sceneWindow) { | ||
this.sceneWindow = sceneWindow; | ||
} | ||
|
||
private void startGraphicsThread() { | ||
graphicsThread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
int lastElapsed = 0; | ||
while (settings.isPlaying()) { | ||
lastElapsed = updateGraphics(lastElapsed); | ||
} | ||
} | ||
}); | ||
graphicsThread.start(); | ||
} | ||
|
||
private void startInputThread() { | ||
inputThread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
int lastElapsed = 0; | ||
while (true) { | ||
lastElapsed = updateInput(lastElapsed); | ||
} | ||
} | ||
}); | ||
inputThread.start(); | ||
} | ||
|
||
private void startPhysicsThread() { | ||
physicsThread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
int lastElapsed = 0; | ||
while (settings.isPlaying()) { | ||
lastElapsed = updatePhysics(lastElapsed); | ||
} | ||
} | ||
}); | ||
physicsThread.start(); | ||
} | ||
|
||
private int updateGraphics(int lastElapsed) { | ||
long before = System.nanoTime(); | ||
if (sceneWindow != null) { | ||
animator.animate(scene); | ||
renderer.render(scene, sceneWindow.getDepthBuffer()); | ||
sceneWindow.repaint(); | ||
} | ||
int elapsed = (int)(System.nanoTime() - before); | ||
Profiler.getInstance().getData().setGraphicsTime(elapsed); | ||
try { | ||
Thread.sleep(1000/settings.getGraphicsUpdateRate()); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
return elapsed; | ||
} | ||
|
||
private int updateInput(int lastElapsed) { | ||
long before = System.nanoTime(); | ||
keyManager.update(); | ||
mouseManager.update(); | ||
int elapsed = (int)(System.nanoTime() - before); | ||
Profiler.getInstance().getData().setInputTime(elapsed); | ||
try { | ||
Thread.sleep(1000/settings.getInputUpdateRate()); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
return elapsed; | ||
} | ||
|
||
private int updatePhysics(int lastElapsed) { | ||
long before = System.nanoTime(); | ||
|
||
int elapsed = (int)(System.nanoTime() - before); | ||
Profiler.getInstance().getData().setPhysicsTime(elapsed); | ||
try { | ||
Thread.sleep(1000/settings.getPhysicsUpdateRate()); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
return elapsed; | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/com/johnsproject/jpge/EngineSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.johnsproject.jpge; | ||
|
||
/** | ||
* The EngineSettings class stores the settings of the {@link Engine}. | ||
* | ||
* @author John´s Project - John Konrad Ferraz Salomon | ||
*/ | ||
public class EngineSettings { | ||
|
||
private int graphicsUpdateRate = 30; | ||
private int inputUpdateRate = 30; | ||
private int physicsUpdateRate = 30; | ||
|
||
private long startTime = System.nanoTime(); | ||
private boolean playing = true; | ||
|
||
/** | ||
* Returns how often graphics should be updated in a second. | ||
* Default is 30. | ||
* | ||
* @return how often graphics should be updated in a second. | ||
*/ | ||
public int getGraphicsUpdateRate() { | ||
return graphicsUpdateRate; | ||
} | ||
|
||
/** | ||
* Sets how often graphics should be updated in a second. | ||
* Default is 30. | ||
* | ||
* @param graphicsUpdateRate value to set. | ||
*/ | ||
public void setGraphicsUpdateRate(int graphicsUpdateRate) { | ||
this.graphicsUpdateRate = graphicsUpdateRate; | ||
} | ||
|
||
/** | ||
* Returns how often input should be updated in a second. | ||
* Default is 30. | ||
* | ||
* @return how often input should be updated in a second. | ||
*/ | ||
public int getInputUpdateRate() { | ||
return inputUpdateRate; | ||
} | ||
|
||
/** | ||
* Sets how often input should be updated in a second. | ||
* Default is 30. | ||
* | ||
* @param inputUpdateRate value to set. | ||
*/ | ||
public void setInputUpdateRate(int inputUpdateRate) { | ||
this.inputUpdateRate = inputUpdateRate; | ||
} | ||
|
||
/** | ||
* Returns how often physics should be updated in a second. | ||
* Default is 30. | ||
* | ||
* @return how often physics should be updated in a second. | ||
*/ | ||
public int getPhysicsUpdateRate() { | ||
return physicsUpdateRate; | ||
} | ||
|
||
/** | ||
* Sets how often physics should be updated in a second. | ||
* Default is 30. | ||
* | ||
* @param physicsUpdateRate value to set. | ||
*/ | ||
public void setPhysicsUpdateRate(int physicsUpdateRate) { | ||
this.physicsUpdateRate = physicsUpdateRate; | ||
} | ||
|
||
/** | ||
* Returns the time since the engine started in nanoseconds. | ||
* | ||
* @return time since the engine started in nanoseconds. | ||
*/ | ||
public long getPlayTime() { | ||
return System.nanoTime() - startTime; | ||
} | ||
|
||
/** | ||
* Returns if the engine is running. | ||
* | ||
* @return if the engine is running. | ||
*/ | ||
public boolean isPlaying() { | ||
return playing; | ||
} | ||
|
||
/** | ||
* Sets if the engine should run. | ||
* | ||
* @param playing value to set. | ||
*/ | ||
public void setPlaying(boolean playing) { | ||
this.playing = playing; | ||
} | ||
|
||
} |
Oops, something went wrong.