Skip to content

Commit

Permalink
Added multithreading to SceneRenderer's rendering pipeline!!
Browse files Browse the repository at this point in the history
-Added comments to the CameraEvent class
-Renamed EventDispatcher of graphics event package to
GraphicsEventDispatcher
-Added comments to the CameraListener interface
-Added comments to the GraphicsEventDispatcher class
-Added comments to the EventDispatcher class
-Added comments to the UpdateListener interface
-Added comments to the UpdateEvent class
-Changed GameManager update from swing Timers to Threads
-Changed KeyInputManager thread synchronizing from semaphores to
synchronized list
-Changed SceneRenderer to work with 2 threads, 1 for transformations,
and other for drawing (needs to be refactored, its a mess now)
John committed Sep 4, 2018
1 parent 95220f5 commit ee9face
Showing 14 changed files with 336 additions and 205 deletions.
34 changes: 22 additions & 12 deletions ProjectHistory
Original file line number Diff line number Diff line change
@@ -114,15 +114,25 @@ v0.1 - 24/08/2018
-Improved MathUtils sine, cosine, tangent performance
-Improved affine texture mapper performance (added bitshift to uv interpolation)
03/09/2018
-Added comments to the Animation class.
-Added comments to the Mesh class.
-Added comments to the Light class.
-Added comments to the Scene class.
-Added comments to the Camera class.
-Added comments to the Texture class.
-Added comments to the Material class.
-Added comments to the Transform class.
-Added comments to the SceneFrame class.
-Added comments to the SceneObject class.
-Added comments to the SceneAnimator class.
-Added comments to the SceneRenderer class.
-Added comments to the Animation class
-Added comments to the Mesh class
-Added comments to the Light class
-Added comments to the Scene class
-Added comments to the Camera class
-Added comments to the Texture class
-Added comments to the Material class
-Added comments to the Transform class
-Added comments to the SceneFrame class
-Added comments to the SceneObject class
-Added comments to the SceneAnimator class
-Added comments to the SceneRenderer class
-Added comments to the CameraEvent class
-Renamed EventDispatcher of graphics event package to GraphicsEventDispatcher
-Added comments to the CameraListener interface
-Added comments to the GraphicsEventDispatcher class
-Added comments to the EventDispatcher class
-Added comments to the UpdateListener interface
-Added comments to the UpdateEvent class
-Changed GameManager update from swing Timers to Threads
-Changed KeyInputManager thread synchronizing from semaphores to synchronized list
-Changed SceneRenderer to work with 2 threads, 1 for transformations, and other for drawing (needs to be refactored, its a mess now)
118 changes: 64 additions & 54 deletions src/main/java/com/johnsproject/jpge/GameManager.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.johnsproject.jpge;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import com.johnsproject.jpge.event.EventDispatcher;
import com.johnsproject.jpge.event.UpdateEvent;
import com.johnsproject.jpge.event.UpdateEvent.UpdateType;
@@ -23,70 +18,85 @@ public static GameManager getInstance() {
private int inputUpdateRate = 30;
private int physicsUpdateRate = 30;

private Timer graphicsTimer;
private Timer inputTimer;
private Timer physicsTimer;
private Thread graphicsThread;
private Thread inputThread;
private Thread physicsThread;

public GameManager() {
updateGraphics();
updateInput();
updatePhysics();
}

public void play() {
graphicsTimer.start();
inputTimer.start();
physicsTimer.start();
}
// public void play() {
// graphicsThread.start();
// inputTimer.start();
// physicsTimer.start();
// }

public void pause() {
graphicsTimer.stop();
inputTimer.stop();
physicsTimer.stop();
}
// public void pause() {
// graphicsTimer.();
// inputTimer.stop();
// physicsTimer.stop();
// }

void updateGraphics() {
graphicsTimer = new Timer(0, null);
graphicsTimer.setInitialDelay((1000/targetFPS));
graphicsTimer.addActionListener(new ActionListener() {
//int delay = (1000/targetFPS);
int lastElapsed = 0;
public void actionPerformed(ActionEvent evt) {
int before = (int)System.nanoTime();
EventDispatcher.getInstance().dispatchUpdateEvent(new UpdateEvent(lastElapsed, UpdateType.graphics));
//graphicsTimer.setInitialDelay(Math.abs(((int)lastElapsed>>1) / delay)>>48);
lastElapsed = (int)System.nanoTime() - before;
graphicsTimer.restart();
}
});
graphicsTimer.start();
graphicsThread = new Thread(new Runnable() {
@Override
public void run() {
int lastElapsed = 0, before = 0;
while (true) {
before = (int)System.nanoTime();
EventDispatcher.getInstance().dispatchUpdateEvent(new UpdateEvent(lastElapsed, UpdateType.graphics));
lastElapsed = (int)System.nanoTime() - before;
try {
Thread.sleep(1000/targetFPS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
graphicsThread.start();
}

void updateInput() {
inputTimer = new Timer(1000/inputUpdateRate, null);
inputTimer.addActionListener(new ActionListener() {
int lastElapsed = 0;
public void actionPerformed(ActionEvent evt) {
int before = (int)System.nanoTime();
EventDispatcher.getInstance().dispatchUpdateEvent(new UpdateEvent(lastElapsed, UpdateType.input));
lastElapsed = (int)System.nanoTime() - before;
inputTimer.restart();
}
});
inputTimer.start();
inputThread = new Thread(new Runnable() {
@Override
public void run() {
int lastElapsed = 0, before = 0;
while (true) {
before = (int)System.nanoTime();
EventDispatcher.getInstance().dispatchUpdateEvent(new UpdateEvent(lastElapsed, UpdateType.input));
lastElapsed = (int)System.nanoTime() - before;
try {
Thread.sleep(1000/inputUpdateRate);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
inputThread.start();
}

void updatePhysics() {
physicsTimer = new Timer(1000/physicsUpdateRate, null);
physicsTimer.addActionListener(new ActionListener() {
int lastElapsed = 0;
public void actionPerformed(ActionEvent evt) {
int before = (int)System.nanoTime();
EventDispatcher.getInstance().dispatchUpdateEvent(new UpdateEvent(lastElapsed, UpdateType.physics));
lastElapsed = (int)System.nanoTime() - before;
physicsTimer.restart();
}
});
physicsTimer.start();
physicsThread = new Thread(new Runnable() {
@Override
public void run() {
int lastElapsed = 0, before = 0;
while (true) {
before = (int)System.nanoTime();
EventDispatcher.getInstance().dispatchUpdateEvent(new UpdateEvent(lastElapsed, UpdateType.physics));
lastElapsed = (int)System.nanoTime() - before;
try {
Thread.sleep(1000/physicsUpdateRate);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
physicsThread.start();
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/johnsproject/jpge/event/EventDispatcher.java
Original file line number Diff line number Diff line change
@@ -4,25 +4,47 @@
import java.util.Collections;
import java.util.List;

/**
* The EventDispatcher class is used to dispatch update events.
*
* @author John´s Project - John Konrad Ferraz Salomon
*/
public class EventDispatcher {

private static EventDispatcher instance;

private List<UpdateListener> updateListeners = Collections.synchronizedList(new ArrayList<UpdateListener>());

/**
* Returns a instance of the EventDispatcher class.
*
* @return instance of the EventDispatcher class.
*/
public static EventDispatcher getInstance() {
if (instance == null) {
instance = new EventDispatcher();
}
return instance;
}

/**
* Adds the given {@link UpdateListener} to the list of {@link UpdateListener UpdateListeners}
* that are called when dispatch method is called.
*
* @param listener listener to add.
*/
public void addUpdateListener(UpdateListener listener) {
synchronized (updateListeners) {
updateListeners.add(listener);
}
}

/**
* Tells this EventDispatcher to dispatch the given update event
* to all its {@link UpdateListener UpdateListeners}.
*
* @param event event to dispatch.
*/
public void dispatchUpdateEvent(UpdateEvent event) {
synchronized (updateListeners) {
for (UpdateListener updateListener : updateListeners) {
22 changes: 22 additions & 0 deletions src/main/java/com/johnsproject/jpge/event/UpdateEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.johnsproject.jpge.event;

/**
* The UpdateEvent class is used to tell the engine that
* it is time to update graphics, input or physics.
*
* @author John´s Project - John Konrad Ferraz Salomon
*/
public class UpdateEvent {

private int time = 0;
@@ -11,15 +17,31 @@ public enum UpdateType{
physics
}

/**
* Creates a new instance of the UpdateEvent class filled with the given values.
*
* @param elapsedTime last elapsed update time.
* @param eventType type of this event.
*/
public UpdateEvent(int elapsedTime, UpdateType eventType) {
this.time = elapsedTime;
this.type = eventType;
}

/**
* Returns the last elapsed update time.
*
* @return last elapsed update time.
*/
public int getElapsedTime() {
return time;
}

/**
* Returns the type of this event.
*
* @return type of this event.
*/
public UpdateType getUpdateType() {
return type;
}
6 changes: 6 additions & 0 deletions src/main/java/com/johnsproject/jpge/event/UpdateListener.java
Original file line number Diff line number Diff line change
@@ -2,6 +2,12 @@

import java.util.EventListener;

/**
* The CameraEvent interface is used to dispatch {@link UpdateEvent UpdateEvents}
* by the {@link EventDispatcher}.
*
* @author John´s Project - John Konrad Ferraz Salomon
*/
public interface UpdateListener extends EventListener{
void update(UpdateEvent event);
}
4 changes: 2 additions & 2 deletions src/main/java/com/johnsproject/jpge/graphics/Scene.java
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ public List<Light> getLights() {
public void addCamera(Camera camera){
synchronized (cameras) {
cameras.add(camera);
EventDispatcher.getInstance().dispatchAddEvent(new CameraEvent(camera));
GraphicsEventDispatcher.getInstance().dispatchAddEvent(new CameraEvent(camera));
}
}

@@ -108,7 +108,7 @@ public void addCamera(Camera camera){
*/
public void removeCamera(Camera camera){
synchronized (cameras) {
EventDispatcher.getInstance().dispatchRemoveEvent(new CameraEvent(camera));
GraphicsEventDispatcher.getInstance().dispatchRemoveEvent(new CameraEvent(camera));
cameras.remove(camera);
}
}
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ void initializeFrame(){
e.printStackTrace();
}
//this.addKeyListener(KeyInputManager.getInstance());
EventDispatcher.getInstance().addCameraListener(this);
GraphicsEventDispatcher.getInstance().addCameraListener(this);
com.johnsproject.jpge.event.EventDispatcher.getInstance().addUpdateListener(this);
GameManager.getInstance();
this.setLayout(null);
Loading

0 comments on commit ee9face

Please sign in to comment.