some cleaning and added volatile keyword

v5stable
ondra 11 years ago
parent 94f0b4c3e4
commit 7d61de7c61
  1. 2
      src/mightypork/gamecore/audio/DeferredAudio.java
  2. 5
      src/mightypork/gamecore/audio/JointVolume.java
  3. 2
      src/mightypork/gamecore/audio/SoundSystem.java
  4. 2
      src/mightypork/gamecore/control/AppAdapter.java
  5. 1
      src/mightypork/gamecore/control/BaseApp.java
  6. 6
      src/mightypork/gamecore/control/GameLoop.java
  7. 7
      src/mightypork/gamecore/control/SlickLogRedirector.java
  8. 1
      src/mightypork/gamecore/control/bus/EventBus.java
  9. 15
      src/mightypork/gamecore/gui/Action.java
  10. 12
      src/mightypork/gamecore/gui/ActionTrigger.java
  11. 8
      src/mightypork/gamecore/gui/renderers/ElementHolder.java
  12. 8
      src/mightypork/gamecore/gui/renderers/TextPainter.java
  13. 88
      src/mightypork/gamecore/gui/screens/Screen.java
  14. 2
      src/mightypork/gamecore/gui/screens/ScreenLayer.java
  15. 14
      src/mightypork/gamecore/gui/screens/ScreenRegistry.java
  16. 7
      src/mightypork/gamecore/input/ActionTrigger.java
  17. 6
      src/mightypork/gamecore/loading/AsyncResourceLoader.java
  18. 4
      src/mightypork/gamecore/loading/BaseDeferredResource.java
  19. 6
      src/mightypork/gamecore/render/DisplaySystem.java
  20. 3
      src/mightypork/rogue/App.java
  21. 55
      src/mightypork/rogue/MainLoop.java
  22. 56
      src/mightypork/rogue/TaskTakeScreenshot.java
  23. 2
      src/mightypork/rogue/screens/test_bouncyboxes/ScreenTestBouncy.java
  24. 2
      src/mightypork/rogue/screens/test_cat_sound/ScreenTestCat.java
  25. 2
      src/mightypork/rogue/screens/test_font/ScreenTestFont.java

@ -86,7 +86,7 @@ public class DeferredAudio extends BaseDeferredResource {
@Override @Override
protected synchronized final void loadResource(String resource) throws IOException protected void loadResource(String resource) throws IOException
{ {
final String ext = FileUtils.getExtension(resource); final String ext = FileUtils.getExtension(resource);

@ -2,7 +2,6 @@ package mightypork.gamecore.audio;
import mightypork.utils.math.Calc; import mightypork.utils.math.Calc;
import mightypork.utils.objects.Mutable;
/** /**
@ -12,7 +11,7 @@ import mightypork.utils.objects.Mutable;
*/ */
public class JointVolume extends Volume { public class JointVolume extends Volume {
private final Mutable<Double>[] volumes; private final Volume[] volumes;
/** /**
@ -34,7 +33,7 @@ public class JointVolume extends Volume {
public Double get() public Double get()
{ {
double d = super.get(); double d = super.get();
for (final Mutable<Double> v : volumes) for (final Volume v : volumes)
d *= v.get(); d *= v.get();
return Calc.clampd(d, 0, 1); return Calc.clampd(d, 0, 1);

@ -84,7 +84,7 @@ public class SoundSystem extends RootBusNode implements Updateable {
@Override @Override
public final void deinit() public void deinit()
{ {
for (final DeferredAudio r : resources) { for (final DeferredAudio r : resources) {
r.destroy(); r.destroy();

@ -53,7 +53,7 @@ public class AppAdapter implements AppAccess {
@Override @Override
public void shutdown() public final void shutdown()
{ {
app.shutdown(); app.shutdown();
} }

@ -15,7 +15,6 @@ import mightypork.gamecore.gui.screens.ScreenRegistry;
import mightypork.gamecore.input.InputSystem; import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.loading.AsyncResourceLoader; import mightypork.gamecore.loading.AsyncResourceLoader;
import mightypork.gamecore.render.DisplaySystem; import mightypork.gamecore.render.DisplaySystem;
import mightypork.rogue.util.SlickLogRedirector;
import mightypork.utils.files.InstanceLock; import mightypork.utils.files.InstanceLock;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import mightypork.utils.logging.LogInstance; import mightypork.utils.logging.LogInstance;

@ -22,7 +22,7 @@ public abstract class GameLoop extends AppModule implements MainLoopTaskRequest.
private final Queue<Runnable> taskQueue = new ConcurrentLinkedQueue<>(); private final Queue<Runnable> taskQueue = new ConcurrentLinkedQueue<>();
private TimerDelta timer; private TimerDelta timer;
private Renderable rootRenderable; private Renderable rootRenderable;
private boolean running = true; private volatile boolean running = true;
/** /**
@ -85,14 +85,14 @@ public abstract class GameLoop extends AppModule implements MainLoopTaskRequest.
@Override @Override
protected final void deinit() protected void deinit()
{ {
running = false; running = false;
} }
@Override @Override
public final synchronized void queueTask(Runnable request) public synchronized void queueTask(Runnable request)
{ {
taskQueue.add(request); taskQueue.add(request);
} }

@ -1,4 +1,4 @@
package mightypork.rogue.util; package mightypork.gamecore.control;
import mightypork.utils.logging.LogInstance; import mightypork.utils.logging.LogInstance;
@ -6,6 +6,11 @@ import mightypork.utils.logging.LogInstance;
import org.newdawn.slick.util.LogSystem; import org.newdawn.slick.util.LogSystem;
/**
* Used to redirect slick log into main logger.
*
* @author MightyPork
*/
public class SlickLogRedirector implements LogSystem { public class SlickLogRedirector implements LogSystem {
LogInstance l; LogInstance l;

@ -44,6 +44,7 @@ final public class EventBus implements Destroyable {
*/ */
public EventBus() { public EventBus() {
busThread = new QueuePollingThread(); busThread = new QueuePollingThread();
busThread.setDaemon(true);
busThread.start(); busThread.start();
} }

@ -1,4 +1,4 @@
package mightypork.gamecore.input; package mightypork.gamecore.gui;
/** /**
@ -16,14 +16,23 @@ public abstract class Action implements Runnable {
* *
* @param enable true to enable * @param enable true to enable
*/ */
public final void enable(boolean enable) public void setEnabled(boolean enable)
{ {
this.enabled = enable; this.enabled = enable;
} }
/**
* @return true if this action is enabled.
*/
public boolean isEnabled()
{
return enabled;
}
@Override @Override
public final void run() public void run()
{ {
if (enabled) execute(); if (enabled) execute();
} }

@ -0,0 +1,12 @@
package mightypork.gamecore.gui;
/**
* Element that can be assigned an action (ie. button);
*
* @author MightyPork
*/
public interface ActionTrigger {
void setAction(Action action);
}

@ -34,14 +34,14 @@ public abstract class ElementHolder extends BusNode implements PluggableRenderab
@Override @Override
public final void setContext(RectConstraint context) public void setContext(RectConstraint context)
{ {
this.context = context; this.context = context;
} }
@Override @Override
public final void render() public void render()
{ {
for (final Renderable element : elements) { for (final Renderable element : elements) {
element.render(); element.render();
@ -50,7 +50,7 @@ public abstract class ElementHolder extends BusNode implements PluggableRenderab
@Override @Override
public final Rect getRect() public Rect getRect()
{ {
return context.getRect(); return context.getRect();
} }
@ -70,7 +70,7 @@ public abstract class ElementHolder extends BusNode implements PluggableRenderab
* *
* @param elem element; it's context will be set to the constraint. * @param elem element; it's context will be set to the constraint.
*/ */
public final void attach(PluggableRenderable elem) public void attach(PluggableRenderable elem)
{ {
if (elem == null) return; if (elem == null) return;

@ -63,25 +63,25 @@ public class TextPainter extends PluggableRenderer {
} }
public final void setColor(RGB color) public void setColor(RGB color)
{ {
this.color = color; this.color = color;
} }
public final void setAlign(Align align) public void setAlign(Align align)
{ {
this.align = align; this.align = align;
} }
public final void setText(String text) public void setText(String text)
{ {
this.text = new StringWrapper(text); this.text = new StringWrapper(text);
} }
public final void setText(StringProvider text) public void setText(StringProvider text)
{ {
this.text = text; this.text = text;
} }

@ -24,8 +24,8 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
private final KeyBindingPool keybindings = new KeyBindingPool(); private final KeyBindingPool keybindings = new KeyBindingPool();
private boolean active; private volatile boolean active;
private boolean needSetupViewport = false; private volatile boolean needSetupViewport = false;
public Screen(AppAccess app) { public Screen(AppAccess app) {
@ -81,45 +81,7 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
/** /**
* Called when the screen becomes active * @return true if screen is the current screen
*/
@NoImpl
protected void onScreenEnter()
{
//
}
/**
* Called when the screen is no longer active
*/
@NoImpl
protected void onScreenLeave()
{
//
}
/**
* Update GUI for new screen size
*
* @param size screen size
*/
@NoImpl
protected void onSizeChanged(Coord size)
{
//
}
/**
* Render screen contents (context is ready for 2D rendering)
*/
protected abstract void renderScreen();
/**
* @return true if screen is the curretn screen
*/ */
public final boolean isActive() public final boolean isActive()
{ {
@ -128,7 +90,7 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@Override @Override
public final void receive(ScreenChangeEvent event) public void receive(ScreenChangeEvent event)
{ {
if (!isActive()) return; if (!isActive()) return;
@ -139,7 +101,7 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@Override @Override
public final Rect getRect() public Rect getRect()
{ {
return getDisplay().getRect(); return getDisplay().getRect();
} }
@ -160,8 +122,46 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
} }
/**
* Called when the screen becomes active
*/
@NoImpl
protected void onScreenEnter()
{
//
}
/**
* Called when the screen is no longer active
*/
@NoImpl
protected void onScreenLeave()
{
//
}
/**
* Update GUI for new screen size
*
* @param size screen size
*/
@NoImpl
protected void onSizeChanged(Coord size)
{
//
}
/**
* Render screen contents (context is ready for 2D rendering)
*/
protected abstract void renderScreen();
/** /**
* @return screen identifier to be used for requests. * @return screen identifier to be used for requests.
*/ */
public abstract String getId(); public abstract String getName();
} }

@ -51,7 +51,7 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
@Override @Override
public final Rect getRect() public Rect getRect()
{ {
return screen.getRect(); return screen.getRect();
} }

@ -11,10 +11,15 @@ import mightypork.gamecore.gui.renderers.Renderable;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
/**
* Game screens holder; Takes care of rendering and screen requests.
*
* @author MightyPork
*/
public class ScreenRegistry extends AppModule implements ScreenRequestEvent.Listener, Renderable { public class ScreenRegistry extends AppModule implements ScreenRequestEvent.Listener, Renderable {
private final HashMap<String, Screen> screens = new HashMap<>(); private final HashMap<String, Screen> screens = new HashMap<>();
private Screen active = null; private volatile Screen active = null;
public ScreenRegistry(AppAccess app) { public ScreenRegistry(AppAccess app) {
@ -22,9 +27,14 @@ public class ScreenRegistry extends AppModule implements ScreenRequestEvent.List
} }
/**
* Add a screen
*
* @param screen added screen
*/
public void add(Screen screen) public void add(Screen screen)
{ {
screens.put(screen.getId(), screen); screens.put(screen.getName(), screen);
addChildClient(screen); addChildClient(screen);
} }

@ -1,7 +0,0 @@
package mightypork.gamecore.input;
public interface ActionTrigger {
void setAction(Action action);
}

@ -21,13 +21,15 @@ public class AsyncResourceLoader extends Thread implements ResourceLoadRequest.L
public static void launch(BusAccess app) public static void launch(BusAccess app)
{ {
(new AsyncResourceLoader(app)).start(); Thread loader = new AsyncResourceLoader(app);
loader.setDaemon(true);
loader.start();
} }
private final ExecutorService exs = Executors.newCachedThreadPool(); private final ExecutorService exs = Executors.newCachedThreadPool();
private final LinkedBlockingQueue<DeferredResource> toLoad = new LinkedBlockingQueue<>(); private final LinkedBlockingQueue<DeferredResource> toLoad = new LinkedBlockingQueue<>();
private boolean stopped; private volatile boolean stopped;
private final BusAccess app; private final BusAccess app;

@ -17,8 +17,8 @@ import mightypork.utils.logging.LoggedName;
public abstract class BaseDeferredResource implements DeferredResource, Destroyable { public abstract class BaseDeferredResource implements DeferredResource, Destroyable {
private final String resource; private final String resource;
private boolean loadFailed = false; private volatile boolean loadFailed = false;
private boolean loadAttempted = false; private volatile boolean loadAttempted = false;
public BaseDeferredResource(String resource) { public BaseDeferredResource(String resource) {

@ -6,7 +6,9 @@ import static org.lwjgl.opengl.GL11.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppModule;
import mightypork.gamecore.control.bus.clients.RootBusNode; import mightypork.gamecore.control.bus.clients.RootBusNode;
import mightypork.gamecore.control.bus.events.DestroyEvent;
import mightypork.gamecore.control.bus.events.ScreenChangeEvent; import mightypork.gamecore.control.bus.events.ScreenChangeEvent;
import mightypork.gamecore.control.timing.FpsMeter; import mightypork.gamecore.control.timing.FpsMeter;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
@ -21,7 +23,7 @@ import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.DisplayMode;
public class DisplaySystem extends RootBusNode implements RectConstraint { public class DisplaySystem extends AppModule implements RectConstraint {
private DisplayMode windowDisplayMode; private DisplayMode windowDisplayMode;
private int targetFps; private int targetFps;
@ -204,7 +206,7 @@ public class DisplaySystem extends RootBusNode implements RectConstraint {
/** /**
* @return current FPS * @return current FPS
*/ */
public final long getFps() public long getFps()
{ {
return fpsMeter.getFPS(); return fpsMeter.getFPS();
} }

@ -89,6 +89,7 @@ public class App extends BaseApp {
log.setSysoutLevel(Level.ALL); log.setSysoutLevel(Level.ALL);
log.enable(Config.LOGGING_ENABLED); log.enable(Config.LOGGING_ENABLED);
log.enableSysout(Config.LOG_TO_STDOUT); log.enableSysout(Config.LOG_TO_STDOUT);
return log; return log;
} }
@ -182,6 +183,8 @@ public class App extends BaseApp {
// custom channels // custom channels
bus.addChannel(ActionRequest.class, ActionRequest.Listener.class); bus.addChannel(ActionRequest.class, ActionRequest.Listener.class);
bus.detailedLogging = true;
} }
} }

@ -1,12 +1,21 @@
package mightypork.rogue; package mightypork.rogue;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import mightypork.gamecore.control.BaseApp; import mightypork.gamecore.control.BaseApp;
import mightypork.gamecore.control.GameLoop; import mightypork.gamecore.control.GameLoop;
import mightypork.gamecore.input.Action; import mightypork.gamecore.gui.Action;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.gamecore.render.Screenshot;
import mightypork.rogue.events.ActionRequest; import mightypork.rogue.events.ActionRequest;
import mightypork.rogue.events.ActionRequest.RequestType; import mightypork.rogue.events.ActionRequest.RequestType;
import mightypork.rogue.util.Utils; import mightypork.rogue.util.Utils;
import mightypork.utils.logging.Log;
public class MainLoop extends GameLoop implements ActionRequest.Listener { public class MainLoop extends GameLoop implements ActionRequest.Listener {
@ -33,7 +42,7 @@ public class MainLoop extends GameLoop implements ActionRequest.Listener {
} }
} }
/** Take a screenshot */ /* Take a screenshot */
private final Action taskScreenshot = new Action() { private final Action taskScreenshot = new Action() {
@Override @Override
@ -44,7 +53,7 @@ public class MainLoop extends GameLoop implements ActionRequest.Listener {
} }
}; };
/** Shutdown the application */ /* Shutdown the application */
private final Action taskShutdown = new Action() { private final Action taskShutdown = new Action() {
@Override @Override
@ -54,7 +63,7 @@ public class MainLoop extends GameLoop implements ActionRequest.Listener {
} }
}; };
/** Toggle fullscreen */ /* Toggle fullscreen */
private final Action taskFullscreen = new Action() { private final Action taskFullscreen = new Action() {
@Override @Override
@ -63,4 +72,42 @@ public class MainLoop extends GameLoop implements ActionRequest.Listener {
getDisplay().switchFullscreen(); getDisplay().switchFullscreen();
} }
}; };
private class TaskTakeScreenshot implements Runnable {
private final Screenshot scr;
public TaskTakeScreenshot() {
scr = DisplaySystem.takeScreenshot();
}
@Override
public void run()
{
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
final String fname = df.format(new Date());
// generate unique filename
File file;
int index = 0;
while (true) {
file = new File(Paths.SCREENSHOTS, fname + (index > 0 ? "-" + index : "") + ".png");
if (!file.exists()) break;
index++;
}
Log.f3("Saving screenshot to file: " + file);
// save to disk
try {
scr.save(file);
} catch (final IOException e) {
Log.e("Failed to save screenshot.", e);
}
}
}
} }

@ -1,56 +0,0 @@
package mightypork.rogue;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.gamecore.render.Screenshot;
import mightypork.utils.logging.Log;
public class TaskTakeScreenshot implements Runnable {
private final Screenshot scr;
public TaskTakeScreenshot() {
scr = DisplaySystem.takeScreenshot();
}
@Override
public void run()
{
final String fname = getUniqueScreenshotName();
// generate unique filename
File file;
int index = 0;
while (true) {
file = new File(Paths.SCREENSHOTS, fname + (index > 0 ? "-" + index : "") + ".png");
if (!file.exists()) break;
index++;
}
Log.f3("Saving screenshot to file: " + file);
// save to disk
try {
scr.save(file);
} catch (final IOException e) {
Log.e("Failed to save screenshot.", e);
}
}
private static String getUniqueScreenshotName()
{
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
return df.format(new Date());
}
}

@ -35,7 +35,7 @@ public class ScreenTestBouncy extends LayeredScreen {
@Override @Override
public String getId() public String getName()
{ {
return "test.bouncy"; return "test.bouncy";
} }

@ -57,7 +57,7 @@ public class ScreenTestCat extends LayeredScreen {
@Override @Override
public String getId() public String getName()
{ {
return "test.cat"; return "test.cat";
} }

@ -36,7 +36,7 @@ public class ScreenTestFont extends Screen {
@Override @Override
public String getId() public String getName()
{ {
return "test.font"; return "test.font";
} }

Loading…
Cancel
Save