From dacb43df3ad038adbb3eb430b97f528f88d043b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 10 Apr 2014 09:39:11 +0200 Subject: [PATCH] tweaked gradient rendering --- .../gamecore/audio/SoundSystem.java | 2 +- .../gamecore/audio/players/LoopPlayer.java | 2 +- src/mightypork/gamecore/control/BaseApp.java | 2 +- .../control/bus/events/UpdateEvent.java | 2 +- .../control/{interf => timing}/Pollable.java | 2 +- .../timing}/Poller.java | 4 +- .../{interf => timing}/Updateable.java | 2 +- .../gui/components/painters/QuadPainter.java | 56 ++++++++ .../gamecore/gui/constraints/Constraints.java | 27 +++- .../gamecore/gui/constraints/RectCache.java | 3 +- .../gamecore/input/InputSystem.java | 2 +- src/mightypork/gamecore/render/Render.java | 47 ++++--- src/mightypork/rogue/App.java | 122 +++++++++--------- .../screens/test_bouncyboxes/BouncyBox.java | 2 +- .../test_cat_sound/LayerFlyingCat.java | 2 +- .../screens/test_font/ScreenTestFont.java | 2 +- .../test_render/LayerTestGradient.java | 8 +- .../utils/math/animation/AnimDouble.java | 2 +- .../utils/math/coord/CoordAnimated.java | 2 +- 19 files changed, 184 insertions(+), 107 deletions(-) rename src/mightypork/gamecore/control/{interf => timing}/Pollable.java (77%) rename src/mightypork/gamecore/{gui/constraints => control/timing}/Poller.java (86%) rename src/mightypork/gamecore/control/{interf => timing}/Updateable.java (84%) create mode 100644 src/mightypork/gamecore/gui/components/painters/QuadPainter.java diff --git a/src/mightypork/gamecore/audio/SoundSystem.java b/src/mightypork/gamecore/audio/SoundSystem.java index f6bbcf5..a32e150 100644 --- a/src/mightypork/gamecore/audio/SoundSystem.java +++ b/src/mightypork/gamecore/audio/SoundSystem.java @@ -10,7 +10,7 @@ import mightypork.gamecore.audio.players.LoopPlayer; import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.bus.clients.RootBusNode; import mightypork.gamecore.control.bus.events.ResourceLoadRequest; -import mightypork.gamecore.control.interf.Updateable; +import mightypork.gamecore.control.timing.Updateable; import mightypork.utils.math.Calc.Buffers; import mightypork.utils.math.coord.Coord; diff --git a/src/mightypork/gamecore/audio/players/LoopPlayer.java b/src/mightypork/gamecore/audio/players/LoopPlayer.java index f5442bc..ef60706 100644 --- a/src/mightypork/gamecore/audio/players/LoopPlayer.java +++ b/src/mightypork/gamecore/audio/players/LoopPlayer.java @@ -3,8 +3,8 @@ package mightypork.gamecore.audio.players; import mightypork.gamecore.audio.DeferredAudio; import mightypork.gamecore.audio.Volume; -import mightypork.gamecore.control.interf.Updateable; import mightypork.gamecore.control.timing.Pauseable; +import mightypork.gamecore.control.timing.Updateable; import mightypork.utils.math.animation.AnimDouble; import org.lwjgl.openal.AL10; diff --git a/src/mightypork/gamecore/control/BaseApp.java b/src/mightypork/gamecore/control/BaseApp.java index 623a694..f810537 100644 --- a/src/mightypork/gamecore/control/BaseApp.java +++ b/src/mightypork/gamecore/control/BaseApp.java @@ -10,7 +10,7 @@ import mightypork.gamecore.control.bus.EventBus; import mightypork.gamecore.control.bus.events.*; import mightypork.gamecore.control.interf.Destroyable; import mightypork.gamecore.control.interf.NoImpl; -import mightypork.gamecore.control.interf.Updateable; +import mightypork.gamecore.control.timing.Updateable; import mightypork.gamecore.gui.screens.ScreenRegistry; import mightypork.gamecore.input.InputSystem; import mightypork.gamecore.loading.AsyncResourceLoader; diff --git a/src/mightypork/gamecore/control/bus/events/UpdateEvent.java b/src/mightypork/gamecore/control/bus/events/UpdateEvent.java index 39fde24..f4fad33 100644 --- a/src/mightypork/gamecore/control/bus/events/UpdateEvent.java +++ b/src/mightypork/gamecore/control/bus/events/UpdateEvent.java @@ -3,7 +3,7 @@ package mightypork.gamecore.control.bus.events; import mightypork.gamecore.control.bus.events.types.ImmediateEvent; import mightypork.gamecore.control.bus.events.types.UnloggedEvent; -import mightypork.gamecore.control.interf.Updateable; +import mightypork.gamecore.control.timing.Updateable; /** diff --git a/src/mightypork/gamecore/control/interf/Pollable.java b/src/mightypork/gamecore/control/timing/Pollable.java similarity index 77% rename from src/mightypork/gamecore/control/interf/Pollable.java rename to src/mightypork/gamecore/control/timing/Pollable.java index 400492c..286b410 100644 --- a/src/mightypork/gamecore/control/interf/Pollable.java +++ b/src/mightypork/gamecore/control/timing/Pollable.java @@ -1,4 +1,4 @@ -package mightypork.gamecore.control.interf; +package mightypork.gamecore.control.timing; /** diff --git a/src/mightypork/gamecore/gui/constraints/Poller.java b/src/mightypork/gamecore/control/timing/Poller.java similarity index 86% rename from src/mightypork/gamecore/gui/constraints/Poller.java rename to src/mightypork/gamecore/control/timing/Poller.java index 1089d97..8469363 100644 --- a/src/mightypork/gamecore/gui/constraints/Poller.java +++ b/src/mightypork/gamecore/control/timing/Poller.java @@ -1,10 +1,10 @@ -package mightypork.gamecore.gui.constraints; +package mightypork.gamecore.control.timing; import java.util.LinkedHashSet; import java.util.Set; -import mightypork.gamecore.control.interf.Pollable; +import mightypork.gamecore.gui.constraints.RectCache; /** diff --git a/src/mightypork/gamecore/control/interf/Updateable.java b/src/mightypork/gamecore/control/timing/Updateable.java similarity index 84% rename from src/mightypork/gamecore/control/interf/Updateable.java rename to src/mightypork/gamecore/control/timing/Updateable.java index c8efdaa..10af7fe 100644 --- a/src/mightypork/gamecore/control/interf/Updateable.java +++ b/src/mightypork/gamecore/control/timing/Updateable.java @@ -1,4 +1,4 @@ -package mightypork.gamecore.control.interf; +package mightypork.gamecore.control.timing; /** diff --git a/src/mightypork/gamecore/gui/components/painters/QuadPainter.java b/src/mightypork/gamecore/gui/components/painters/QuadPainter.java new file mode 100644 index 0000000..0a8599b --- /dev/null +++ b/src/mightypork/gamecore/gui/components/painters/QuadPainter.java @@ -0,0 +1,56 @@ +package mightypork.gamecore.gui.components.painters; + + +import mightypork.gamecore.gui.components.PluggableRenderer; +import mightypork.gamecore.render.Render; +import mightypork.utils.math.color.RGB; + + +/** + * Draws image in given rect + * + * @author MightyPork + */ +public class QuadPainter extends PluggableRenderer { + + private final RGB colorHMinVMin; + private final RGB colorHMaxVMin; + private final RGB colorHMaxVMax; + private final RGB colorHMinVMax; + + + /** + * Painter with cokloured vertices. + * + * @param colorHMinVMin + * @param colorHMaxVMin + * @param colorHMaxVMax + * @param colorHMinVMax + */ + public QuadPainter(RGB colorHMinVMin, RGB colorHMaxVMin, RGB colorHMaxVMax, RGB colorHMinVMax) { + this.colorHMinVMin = colorHMinVMin; + this.colorHMaxVMin = colorHMaxVMin; + this.colorHMaxVMax = colorHMaxVMax; + this.colorHMinVMax = colorHMinVMax; + } + + + @Override + public void render() + { + Render.quadColor(getRect(), colorHMinVMin, colorHMaxVMin, colorHMaxVMax, colorHMinVMax); + } + + + public static QuadPainter gradH(RGB colorLeft, RGB colorRight) + { + return new QuadPainter(colorLeft, colorRight, colorRight, colorLeft); + } + + + public static QuadPainter gradV(RGB colorTop, RGB colorBottom) + { + return new QuadPainter(colorTop, colorTop, colorBottom, colorBottom); + } + +} diff --git a/src/mightypork/gamecore/gui/constraints/Constraints.java b/src/mightypork/gamecore/gui/constraints/Constraints.java index d852964..234fe5f 100644 --- a/src/mightypork/gamecore/gui/constraints/Constraints.java +++ b/src/mightypork/gamecore/gui/constraints/Constraints.java @@ -1,6 +1,7 @@ package mightypork.gamecore.gui.constraints; +import mightypork.gamecore.control.timing.Poller; import mightypork.gamecore.input.InputSystem; import mightypork.gamecore.render.DisplaySystem; import mightypork.utils.math.coord.Coord; @@ -352,7 +353,7 @@ public class Constraints { } - public static RectConstraint _shrink_up(final RectConstraint r, final Object shrink) + public static RectConstraint _shrink_top(final RectConstraint r, final Object shrink) { return new RectConstraint() { @@ -365,7 +366,7 @@ public class Constraints { } - public static RectConstraint _shrink_down(final RectConstraint r, final Object shrink) + public static RectConstraint _shrink_bottom(final RectConstraint r, final Object shrink) { return new RectConstraint() { @@ -484,6 +485,12 @@ public class Constraints { /* ================= Box creation ================= */ + public static RectConstraint _box(final Object side) + { + return _box(side, side); + } + + public static RectConstraint _box(final Object width, final Object height) { return new RectConstraint() { @@ -565,6 +572,22 @@ public class Constraints { } + public static RectConstraint _centered(final RectConstraint r, final RectConstraint centerTo) + { + return new RectConstraint() { + + @Override + public Rect getRect() + { + final Coord size = r.getRect().getSize(); + final Coord center = centerTo.getRect().getCenter(); + + return Rect.fromSize(center.x - size.x / 2D, center.y - size.y / 2D, size.x, size.y); + } + }; + } + + /** * Center rect around given coords * diff --git a/src/mightypork/gamecore/gui/constraints/RectCache.java b/src/mightypork/gamecore/gui/constraints/RectCache.java index dc89582..4015aa9 100644 --- a/src/mightypork/gamecore/gui/constraints/RectCache.java +++ b/src/mightypork/gamecore/gui/constraints/RectCache.java @@ -1,7 +1,8 @@ package mightypork.gamecore.gui.constraints; -import mightypork.gamecore.control.interf.Pollable; +import mightypork.gamecore.control.timing.Pollable; +import mightypork.gamecore.control.timing.Poller; import mightypork.utils.math.coord.Rect; diff --git a/src/mightypork/gamecore/input/InputSystem.java b/src/mightypork/gamecore/input/InputSystem.java index f3201cd..0b3ec44 100644 --- a/src/mightypork/gamecore/input/InputSystem.java +++ b/src/mightypork/gamecore/input/InputSystem.java @@ -6,7 +6,7 @@ import mightypork.gamecore.control.bus.clients.RootBusNode; import mightypork.gamecore.control.bus.events.KeyEvent; import mightypork.gamecore.control.bus.events.MouseButtonEvent; import mightypork.gamecore.control.bus.events.MouseMotionEvent; -import mightypork.gamecore.control.interf.Updateable; +import mightypork.gamecore.control.timing.Updateable; import mightypork.gamecore.render.DisplaySystem; import mightypork.rogue.events.ActionRequest; import mightypork.rogue.events.ActionRequest.RequestType; diff --git a/src/mightypork/gamecore/render/Render.java b/src/mightypork/gamecore/render/Render.java index 238388e..c5b889a 100644 --- a/src/mightypork/gamecore/render/Render.java +++ b/src/mightypork/gamecore/render/Render.java @@ -357,10 +357,25 @@ public class Render { * Draw quad with horizontal gradient * * @param quad drawn quad bounds - * @param colorLeft left color - * @param colorRight right color + * @param color1 left color + * @param color2 right color */ - public static void quadGradH(Rect quad, RGB colorLeft, RGB colorRight) + public static void quadGradH(Rect quad, RGB color1, RGB color2) + { + quadColor(quad, color1, color2, color2, color1); + } + + + /** + * Draw quad with coloured vertices. + * + * @param quad drawn quad bounds + * @param colorHMinVMin + * @param colorHMaxVMin + * @param colorHMaxVMax + * @param colorHMinVMax + */ + public static void quadColor(Rect quad, RGB colorHMinVMin, RGB colorHMaxVMin, RGB colorHMaxVMax, RGB colorHMinVMax) { final double x1 = quad.xMin(); final double y1 = quad.yMin(); @@ -371,14 +386,14 @@ public class Render { unbindTexture(); glBegin(GL_QUADS); - setColor(colorLeft); + setColor(colorHMinVMax); glVertex2d(x1, y2); - setColor(colorRight); + setColor(colorHMaxVMax); glVertex2d(x2, y2); - setColor(colorRight); + setColor(colorHMaxVMin); glVertex2d(x2, y1); - setColor(colorLeft); + setColor(colorHMinVMin); glVertex2d(x1, y1); glEnd(); } @@ -393,23 +408,7 @@ public class Render { */ public static void quadGradV(Rect quad, RGB color1, RGB color2) { - final double x1 = quad.xMin(); - final double y1 = quad.yMin(); - final double x2 = quad.xMax(); - final double y2 = quad.yMax(); - - // draw with color - unbindTexture(); - - glBegin(GL_QUADS); - setColor(color1); - glVertex2d(x1, y1); - glVertex2d(x2, y1); - - setColor(color2); - glVertex2d(x2, y2); - glVertex2d(x1, y2); - glEnd(); + quadColor(quad, color1, color1, color2, color2); } diff --git a/src/mightypork/rogue/App.java b/src/mightypork/rogue/App.java index 5e3092f..2e2bd78 100644 --- a/src/mightypork/rogue/App.java +++ b/src/mightypork/rogue/App.java @@ -56,57 +56,27 @@ public class App extends BaseApp { } - /** - * Handle a crash - * - * @param error - */ - public static void onCrash(Throwable error) - { - if (Log.ready()) { - Log.e("The game has crashed!", error); - } else { - System.err.println("The game has crashed!"); - error.printStackTrace(); - } - - if (inst != null) inst.shutdown(); - } - - @Override - protected void preInit() - { - // to get dot instead of comma in floats - Locale.setDefault(Locale.ENGLISH); - } - - - @Override - protected LogInstance createLog() + protected void initScreens(ScreenRegistry screens) { - final LogInstance log = Log.create("runtime", Paths.LOGS, 10); - log.setFileLevel(Level.WARNING); - log.setSysoutLevel(Level.ALL); - log.enable(Config.LOGGING_ENABLED); - log.enableSysout(Config.LOG_TO_STDOUT); + screens.add(new ScreenTestBouncy(this)); + screens.add(new ScreenTestCat(this)); + screens.add(new ScreenTestFont(this)); + screens.add(new ScreenTestRender(this)); - return log; + screens.showScreen("test.render"); } @Override - protected void initDisplay(DisplaySystem display) - { - display.createMainWindow(Const.WINDOW_W, Const.WINDOW_H, true, Config.START_IN_FS, Const.TITLEBAR); - display.setTargetFps(Const.FPS_RENDER); - } - - - @Override - protected void initSoundSystem(SoundSystem audio) + protected void initBus(EventBus bus) { - audio.setMasterVolume(1); + super.initBus(bus); + + // custom channels + bus.addChannel(ActionRequest.class, ActionRequest.Listener.class); + + //bus.detailedLogging = true; } @@ -146,28 +116,52 @@ public class App extends BaseApp { @Override - protected GameLoop createLoop() + protected void preInit() { - return new MainLoop(this); + // to get dot instead of comma in floats + Locale.setDefault(Locale.ENGLISH); } @Override - protected void initResources() + protected LogInstance createLog() { - Res.load(this); + final LogInstance log = Log.create("runtime", Paths.LOGS, 10); + log.setFileLevel(Level.WARNING); + log.setSysoutLevel(Level.ALL); + log.enable(Config.LOGGING_ENABLED); + log.enableSysout(Config.LOG_TO_STDOUT); + + return log; } @Override - protected void initScreens(ScreenRegistry screens) + protected void initDisplay(DisplaySystem display) { - screens.add(new ScreenTestBouncy(this)); - screens.add(new ScreenTestCat(this)); - screens.add(new ScreenTestFont(this)); - screens.add(new ScreenTestRender(this)); - - screens.showScreen("test.cat"); + display.createMainWindow(Const.WINDOW_W, Const.WINDOW_H, true, Config.START_IN_FS, Const.TITLEBAR); + display.setTargetFps(Const.FPS_RENDER); + } + + + @Override + protected void initSoundSystem(SoundSystem audio) + { + audio.setMasterVolume(1); + } + + + @Override + protected GameLoop createLoop() + { + return new MainLoop(this); + } + + + @Override + protected void initResources() + { + Res.load(this); } @@ -178,15 +172,21 @@ public class App extends BaseApp { } - @Override - protected void initBus(EventBus bus) + /** + * Handle a crash + * + * @param error + */ + public static void onCrash(Throwable error) { - super.initBus(bus); - - // custom channels - bus.addChannel(ActionRequest.class, ActionRequest.Listener.class); + if (Log.ready()) { + Log.e("The game has crashed!", error); + } else { + System.err.println("The game has crashed!"); + error.printStackTrace(); + } - bus.detailedLogging = true; + if (inst != null) inst.shutdown(); } } diff --git a/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java b/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java index 8127e04..c0e357b 100644 --- a/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java +++ b/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java @@ -5,7 +5,7 @@ import static mightypork.gamecore.gui.constraints.Constraints.*; import java.util.Random; -import mightypork.gamecore.control.interf.Updateable; +import mightypork.gamecore.control.timing.Updateable; import mightypork.gamecore.gui.components.PluggableRenderer; import mightypork.gamecore.gui.constraints.NumberConstraint; import mightypork.gamecore.gui.constraints.RectConstraint; diff --git a/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java b/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java index cd7f934..3d2557b 100644 --- a/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java +++ b/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java @@ -6,7 +6,7 @@ import static mightypork.gamecore.gui.constraints.Constraints.*; import java.util.Random; import mightypork.gamecore.control.bus.events.MouseButtonEvent; -import mightypork.gamecore.control.interf.Updateable; +import mightypork.gamecore.control.timing.Updateable; import mightypork.gamecore.gui.components.painters.ImagePainter; import mightypork.gamecore.gui.components.painters.TextPainter; import mightypork.gamecore.gui.constraints.RectConstraint; diff --git a/src/mightypork/rogue/screens/test_font/ScreenTestFont.java b/src/mightypork/rogue/screens/test_font/ScreenTestFont.java index 4481f50..004949a 100644 --- a/src/mightypork/rogue/screens/test_font/ScreenTestFont.java +++ b/src/mightypork/rogue/screens/test_font/ScreenTestFont.java @@ -22,7 +22,7 @@ public class ScreenTestFont extends Screen { tp = new TextPainter(Res.getFont("default"), Align.CENTER, RGB.GREEN); tp.setText("Hello World!"); - final RectConstraint strbox = _grow(_center(this), 0, _div(_height(this), 10)); + final RectConstraint strbox = _centered(_box(_div(_screenH, 10)), this); tp.setContext(strbox); } diff --git a/src/mightypork/rogue/screens/test_render/LayerTestGradient.java b/src/mightypork/rogue/screens/test_render/LayerTestGradient.java index 03fe9d3..dbf7522 100644 --- a/src/mightypork/rogue/screens/test_render/LayerTestGradient.java +++ b/src/mightypork/rogue/screens/test_render/LayerTestGradient.java @@ -2,7 +2,7 @@ package mightypork.rogue.screens.test_render; import static mightypork.gamecore.gui.constraints.Constraints.*; -import mightypork.gamecore.gui.constraints.Poller; +import mightypork.gamecore.control.timing.Poller; import mightypork.gamecore.gui.constraints.RectConstraint; import mightypork.gamecore.gui.screens.Screen; import mightypork.gamecore.gui.screens.ScreenLayer; @@ -17,24 +17,22 @@ public class LayerTestGradient extends ScreenLayer { private final RectConstraint pos1; private final RectConstraint pos2; - private final RectConstraint pos3; public LayerTestGradient(Screen screen) { super(screen); pos1 = _cache(p, _grow_down(_top_edge(this), 64)); - pos2 = _cache(p, _shrink_up(_grow_right(_left_edge(this), 64), 64)); - pos3 = _cache(p, _move(_grow(_center(this), 70, 10), 100, 100)); + pos2 = _cache(p, _shrink_top(_grow_right(_left_edge(this), 64), 64)); } @Override public void render() { + Render.quadColor(getRect(), RGB.WHITE, RGB.BLUE, RGB.BLACK, RGB.PURPLE); Render.quadGradH(pos1.getRect(), RGB.GREEN, RGB.RED); Render.quadGradV(pos2.getRect(), RGB.WHITE, RGB.PURPLE); - Render.quad(pos3.getRect(), RGB.RED); } diff --git a/src/mightypork/utils/math/animation/AnimDouble.java b/src/mightypork/utils/math/animation/AnimDouble.java index 6d3aec5..d5aab6e 100644 --- a/src/mightypork/utils/math/animation/AnimDouble.java +++ b/src/mightypork/utils/math/animation/AnimDouble.java @@ -1,8 +1,8 @@ package mightypork.utils.math.animation; -import mightypork.gamecore.control.interf.Updateable; import mightypork.gamecore.control.timing.Pauseable; +import mightypork.gamecore.control.timing.Updateable; import mightypork.gamecore.gui.constraints.NumberConstraint; import mightypork.utils.math.Calc; diff --git a/src/mightypork/utils/math/coord/CoordAnimated.java b/src/mightypork/utils/math/coord/CoordAnimated.java index f714715..320a81d 100644 --- a/src/mightypork/utils/math/coord/CoordAnimated.java +++ b/src/mightypork/utils/math/coord/CoordAnimated.java @@ -1,7 +1,7 @@ package mightypork.utils.math.coord; -import mightypork.gamecore.control.interf.Updateable; +import mightypork.gamecore.control.timing.Updateable; import mightypork.utils.math.Calc;