parent
7f35d1d316
commit
53b7b02609
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 206 KiB |
@ -0,0 +1,21 @@ |
||||
package mightypork.rogue; |
||||
|
||||
|
||||
/** |
||||
* Deferred resource |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface Deferred { |
||||
|
||||
/** |
||||
* Load the actual resource if not loaded yet |
||||
*/ |
||||
public void load(); |
||||
|
||||
|
||||
/** |
||||
* @return true if already loaded |
||||
*/ |
||||
public boolean isLoaded(); |
||||
} |
@ -0,0 +1,112 @@ |
||||
package mightypork.rogue; |
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Queue; |
||||
import java.util.concurrent.ConcurrentLinkedQueue; |
||||
|
||||
import mightypork.rogue.bus.Subsystem; |
||||
import mightypork.rogue.bus.events.ActionRequest; |
||||
import mightypork.rogue.bus.events.RequestType; |
||||
import mightypork.rogue.bus.events.ScreenRequestEvent; |
||||
import mightypork.rogue.tasks.TaskTakeScreenshot; |
||||
import mightypork.rogue.util.Utils; |
||||
import mightypork.utils.control.bus.events.UpdateEvent; |
||||
import mightypork.utils.control.timing.TimerDelta; |
||||
|
||||
|
||||
public class MainLoop extends Subsystem implements ActionRequest.Listener { |
||||
|
||||
private Queue<Runnable> taskQueue = new ConcurrentLinkedQueue<Runnable>(); |
||||
private List<Runnable> regularTasks; |
||||
|
||||
|
||||
public MainLoop(App app, ArrayList<Runnable> loopTasks) { |
||||
super(app); |
||||
|
||||
this.regularTasks = loopTasks; |
||||
} |
||||
|
||||
/** timer */ |
||||
private TimerDelta timer; |
||||
private boolean running = true; |
||||
|
||||
|
||||
public void start() |
||||
{ |
||||
bus().queue(new ScreenRequestEvent("test.texture")); |
||||
|
||||
timer = new TimerDelta(); |
||||
|
||||
while (running) { |
||||
disp().beginFrame(); |
||||
|
||||
bus().send(new UpdateEvent(timer.getDelta())); |
||||
|
||||
for (Runnable r : regularTasks) { |
||||
r.run(); |
||||
} |
||||
|
||||
Runnable r; |
||||
while ((r = taskQueue.poll()) != null) { |
||||
r.run(); |
||||
} |
||||
|
||||
disp().endFrame(); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void deinit() |
||||
{ |
||||
running = false; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void requestAction(RequestType request) |
||||
{ |
||||
switch (request) { |
||||
case FULLSCREEN: |
||||
taskQueue.add(taskFullscreen); |
||||
break; |
||||
|
||||
case SCREENSHOT: |
||||
taskQueue.add(taskScreenshot); |
||||
break; |
||||
|
||||
case SHUTDOWN: |
||||
taskQueue.add(taskShutdown); |
||||
} |
||||
} |
||||
|
||||
private final Runnable taskScreenshot = new Runnable() { |
||||
|
||||
@Override |
||||
public void run() |
||||
{ |
||||
Res.getEffect("gui.shutter").play(1); |
||||
Utils.runAsThread(new TaskTakeScreenshot(disp())); |
||||
} |
||||
}; |
||||
|
||||
private final Runnable taskShutdown = new Runnable() { |
||||
|
||||
@Override |
||||
public void run() |
||||
{ |
||||
shutdown(); |
||||
} |
||||
}; |
||||
|
||||
private final Runnable taskFullscreen = new Runnable() { |
||||
|
||||
@Override |
||||
public void run() |
||||
{ |
||||
disp().switchFullscreen(); |
||||
} |
||||
}; |
||||
} |
@ -0,0 +1,87 @@ |
||||
package mightypork.rogue; |
||||
|
||||
|
||||
import mightypork.rogue.audio.EffectPlayer; |
||||
import mightypork.rogue.audio.LoopPlayer; |
||||
import mightypork.rogue.audio.SoundBank; |
||||
import mightypork.rogue.gui.screens.screenBouncy.ScreenTestAnim; |
||||
import mightypork.rogue.gui.screens.screenTextures.ScreenTextureTest; |
||||
import mightypork.rogue.render.textures.TextureBank; |
||||
import mightypork.rogue.render.textures.TxQuad; |
||||
|
||||
import org.newdawn.slick.opengl.Texture; |
||||
|
||||
|
||||
public class Res { |
||||
|
||||
private static TextureBank textures; |
||||
private static SoundBank sounds; |
||||
private static boolean initialized = false; |
||||
|
||||
|
||||
public static void load(App app) |
||||
{ |
||||
if (initialized) return; |
||||
initialized = true; |
||||
|
||||
textures = new TextureBank(app); |
||||
sounds = new SoundBank(app); |
||||
|
||||
loadSounds(app); |
||||
loadTextures(app); |
||||
loadFonts(app); |
||||
loadScreens(app); |
||||
} |
||||
|
||||
|
||||
private static void loadFonts(App app) |
||||
{ |
||||
|
||||
} |
||||
|
||||
|
||||
private static void loadTextures(App app) |
||||
{ |
||||
textures.loadTexture("test.kitten", "/res/img/kitten.png"); |
||||
} |
||||
|
||||
|
||||
private static void loadSounds(App app) |
||||
{ |
||||
sounds.addEffect("gui.shutter", "/res/audio/shutter.ogg", 1, 1); |
||||
|
||||
sounds.addLoop("test.wilderness", "/res/audio/wilderness.ogg", 1, 1, 3, 3); |
||||
} |
||||
|
||||
|
||||
private static void loadScreens(App app) |
||||
{ |
||||
app.screens.add("test.anim", new ScreenTestAnim(app)); |
||||
app.screens.add("test.texture", new ScreenTextureTest(app)); |
||||
} |
||||
|
||||
|
||||
public static TxQuad getTxQuad(String key) |
||||
{ |
||||
return textures.getTxQuad(key); |
||||
} |
||||
|
||||
|
||||
public static Texture getTexture(String key) |
||||
{ |
||||
return textures.getTexture(key); |
||||
} |
||||
|
||||
|
||||
public static LoopPlayer getLoop(String key) |
||||
{ |
||||
return sounds.getLoop(key); |
||||
} |
||||
|
||||
|
||||
public static EffectPlayer getEffect(String key) |
||||
{ |
||||
return sounds.getEffect(key); |
||||
} |
||||
|
||||
} |
@ -1,33 +0,0 @@ |
||||
package mightypork.rogue; |
||||
|
||||
|
||||
public class Resources { |
||||
|
||||
public static void load(AppAccess app) |
||||
{ |
||||
|
||||
loadSounds(app); |
||||
|
||||
loadTextures(app); |
||||
|
||||
loadFonts(app); |
||||
} |
||||
|
||||
|
||||
private static void loadFonts(AppAccess app) |
||||
{ |
||||
|
||||
} |
||||
|
||||
|
||||
private static void loadTextures(AppAccess app) |
||||
{ |
||||
|
||||
} |
||||
|
||||
|
||||
private static void loadSounds(AppAccess app) |
||||
{ |
||||
|
||||
} |
||||
} |
@ -1,4 +1,4 @@ |
||||
package mightypork.rogue.sounds; |
||||
package mightypork.rogue.audio; |
||||
|
||||
|
||||
import mightypork.utils.math.Calc; |
@ -0,0 +1,30 @@ |
||||
package mightypork.rogue.audio; |
||||
|
||||
|
||||
public class NullAudio extends DeferredAudio { |
||||
|
||||
public NullAudio() { |
||||
super(""); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void load() |
||||
{ |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean isLoaded() |
||||
{ |
||||
return true; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected boolean ensureLoaded() |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,90 @@ |
||||
package mightypork.rogue.audio; |
||||
|
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import mightypork.rogue.AppAccess; |
||||
import mightypork.rogue.AppAdapter; |
||||
import mightypork.utils.logging.Log; |
||||
|
||||
|
||||
public class SoundBank extends AppAdapter { |
||||
|
||||
private static final DeferredAudio NO_SOUND = new NullAudio(); |
||||
private static final LoopPlayer NULL_LOOP = new LoopPlayer(NO_SOUND, 0, 0, null); |
||||
private static final EffectPlayer NULL_EFFECT = new EffectPlayer(NO_SOUND, 0, 0, null); |
||||
|
||||
private Map<String, EffectPlayer> effects = new HashMap<String, EffectPlayer>(); |
||||
private Map<String, LoopPlayer> loops = new HashMap<String, LoopPlayer>(); |
||||
|
||||
|
||||
public SoundBank(AppAccess app) { |
||||
super(app); |
||||
if (snd() == null) throw new NullPointerException("SoundSystem cannot be null."); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Register effect resource |
||||
* |
||||
* @param key sound key |
||||
* @param resource resource path |
||||
* @param pitch default pitch (1 = unchanged) |
||||
* @param gain default gain (0-1) |
||||
*/ |
||||
public void addEffect(String key, String resource, double pitch, double gain) |
||||
{ |
||||
effects.put(key, snd().createEffect(resource, pitch, gain)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Register loop resource (music / effect loop) |
||||
* |
||||
* @param key sound key |
||||
* @param resource resource path |
||||
* @param pitch default pitch (1 = unchanged) |
||||
* @param gain default gain (0-1) |
||||
* @param fadeIn default time for fadeIn |
||||
* @param fadeOut default time for fadeOut |
||||
*/ |
||||
public void addLoop(String key, String resource, double pitch, double gain, double fadeIn, double fadeOut) |
||||
{ |
||||
loops.put(key, snd().createLoop(resource, pitch, gain, fadeIn, fadeOut)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Get a loop player for key |
||||
* |
||||
* @param key sound key |
||||
* @return loop player |
||||
*/ |
||||
public LoopPlayer getLoop(String key) |
||||
{ |
||||
LoopPlayer p = loops.get(key); |
||||
if (p == null) { |
||||
Log.w("Requesting unknown sound loop \"" + key + "\"."); |
||||
return NULL_LOOP; |
||||
} |
||||
return p; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Get a effect player for key |
||||
* |
||||
* @param key sound key |
||||
* @return effect player |
||||
*/ |
||||
public EffectPlayer getEffect(String key) |
||||
{ |
||||
EffectPlayer p = effects.get(key); |
||||
if (p == null) { |
||||
Log.w("Requesting unknown sound effect \"" + key + "\"."); |
||||
return NULL_EFFECT; |
||||
} |
||||
return p; |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
package mightypork.rogue.bus.events; |
||||
|
||||
|
||||
import mightypork.utils.control.bus.Event; |
||||
|
||||
|
||||
/** |
||||
* Request for action that should be performed in the main thread. |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public class ActionRequest implements Event<ActionRequest.Listener> { |
||||
|
||||
private RequestType type; |
||||
|
||||
|
||||
public ActionRequest(RequestType request) { |
||||
type = request; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(Listener handler) |
||||
{ |
||||
handler.requestAction(type); |
||||
} |
||||
|
||||
public interface Listener { |
||||
|
||||
public void requestAction(RequestType request); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,7 @@ |
||||
package mightypork.rogue.bus.events; |
||||
|
||||
|
||||
public enum RequestType |
||||
{ |
||||
FULLSCREEN, SCREENSHOT, SHUTDOWN; |
||||
} |
@ -0,0 +1,28 @@ |
||||
package mightypork.rogue.bus.events; |
||||
|
||||
|
||||
import mightypork.utils.control.bus.Event; |
||||
|
||||
|
||||
public class ScreenRequestEvent implements Event<ScreenRequestEvent.Listener> { |
||||
|
||||
private String scrName; |
||||
|
||||
|
||||
public ScreenRequestEvent(String screenKey) { |
||||
scrName = screenKey; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(Listener handler) |
||||
{ |
||||
handler.showScreen(scrName); |
||||
} |
||||
|
||||
public interface Listener { |
||||
|
||||
public void showScreen(String key); |
||||
} |
||||
|
||||
} |
@ -1,22 +0,0 @@ |
||||
package mightypork.rogue.display.screens; |
||||
|
||||
|
||||
import mightypork.rogue.AppAccess; |
||||
import mightypork.rogue.bus.Subsystem; |
||||
|
||||
|
||||
public class ScreenRegistry extends Subsystem { |
||||
|
||||
public ScreenRegistry(AppAccess app) { |
||||
super(app); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void deinit() |
||||
{ |
||||
// TODO Auto-generated method stub
|
||||
|
||||
} |
||||
|
||||
} |
@ -1,6 +0,0 @@ |
||||
package mightypork.rogue.display.screens.screenTextures; |
||||
|
||||
|
||||
public class ScreenTextureTest { |
||||
|
||||
} |
@ -1,4 +1,4 @@ |
||||
package mightypork.rogue.display.constraints; |
||||
package mightypork.rogue.gui.constraints; |
||||
|
||||
|
||||
import java.util.LinkedList; |
@ -1,4 +1,4 @@ |
||||
package mightypork.rogue.display.constraints; |
||||
package mightypork.rogue.gui.constraints; |
||||
|
||||
|
||||
/** |
@ -1,4 +1,4 @@ |
||||
package mightypork.rogue.display.constraints; |
||||
package mightypork.rogue.gui.constraints; |
||||
|
||||
|
||||
import mightypork.utils.math.constraints.ConstraintContext; |
@ -1,4 +1,4 @@ |
||||
package mightypork.rogue.display; |
||||
package mightypork.rogue.gui.screens; |
||||
|
||||
|
||||
import java.util.LinkedList; |
@ -1,8 +1,8 @@ |
||||
package mightypork.rogue.display; |
||||
package mightypork.rogue.gui.screens; |
||||
|
||||
|
||||
import mightypork.rogue.bus.ChildClient; |
||||
import mightypork.rogue.display.constraints.Renderable; |
||||
import mightypork.rogue.gui.constraints.Renderable; |
||||
import mightypork.utils.control.interf.Updateable; |
||||
import mightypork.utils.math.constraints.ConstraintContext; |
||||
import mightypork.utils.math.coord.Rect; |
@ -0,0 +1,55 @@ |
||||
package mightypork.rogue.gui.screens; |
||||
|
||||
|
||||
import java.util.HashMap; |
||||
|
||||
import mightypork.rogue.AppAccess; |
||||
import mightypork.rogue.bus.Subsystem; |
||||
import mightypork.rogue.bus.events.ScreenRequestEvent; |
||||
|
||||
|
||||
public class ScreenRegistry extends Subsystem implements ScreenRequestEvent.Listener { |
||||
|
||||
private HashMap<String, Screen> screens = new HashMap<String, Screen>(); |
||||
private Screen active = null; |
||||
|
||||
|
||||
public ScreenRegistry(AppAccess app) { |
||||
super(app); |
||||
} |
||||
|
||||
|
||||
public void add(String key, Screen screen) |
||||
{ |
||||
screens.put(key, screen); |
||||
addChildClient(screen); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void showScreen(String key) |
||||
{ |
||||
Screen toshow = screens.get(key); |
||||
if (toshow == null) throw new RuntimeException("Screen " + key + " not defined."); |
||||
|
||||
if (active != null) active.setActive(false); |
||||
|
||||
toshow.setActive(true); |
||||
|
||||
active = toshow; |
||||
} |
||||
|
||||
|
||||
public void render() |
||||
{ |
||||
if (active != null) active.render(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void deinit() |
||||
{ |
||||
// no impl
|
||||
} |
||||
|
||||
} |
@ -1,19 +1,19 @@ |
||||
package mightypork.rogue.display.screens.screenBouncy; |
||||
package mightypork.rogue.gui.screens.screenBouncy; |
||||
|
||||
|
||||
import mightypork.rogue.AppAccess; |
||||
import mightypork.rogue.display.LayeredScreen; |
||||
import mightypork.rogue.gui.screens.LayeredScreen; |
||||
import mightypork.rogue.input.KeyStroke; |
||||
|
||||
import org.lwjgl.input.Keyboard; |
||||
|
||||
|
||||
public class TestLayeredScreen extends LayeredScreen { |
||||
public class ScreenTestAnim extends LayeredScreen { |
||||
|
||||
private LayerBouncyBoxes layer; |
||||
|
||||
|
||||
public TestLayeredScreen(AppAccess app) { |
||||
public ScreenTestAnim(AppAccess app) { |
||||
super(app); |
||||
|
||||
layer = new LayerBouncyBoxes(this); |
@ -0,0 +1,104 @@ |
||||
package mightypork.rogue.gui.screens.screenTextures; |
||||
|
||||
|
||||
import static mightypork.utils.math.constraints.ConstraintFactory.*; |
||||
|
||||
import java.util.Random; |
||||
|
||||
import mightypork.rogue.AppAccess; |
||||
import mightypork.rogue.Res; |
||||
import mightypork.rogue.bus.events.ActionRequest; |
||||
import mightypork.rogue.bus.events.MouseButtonEvent; |
||||
import mightypork.rogue.bus.events.RequestType; |
||||
import mightypork.rogue.gui.screens.Screen; |
||||
import mightypork.rogue.input.KeyStroke; |
||||
import mightypork.rogue.render.Render; |
||||
import mightypork.utils.math.animation.AnimDouble; |
||||
import mightypork.utils.math.animation.Easing; |
||||
import mightypork.utils.math.constraints.RectConstraint; |
||||
import mightypork.utils.math.coord.Coord; |
||||
|
||||
import org.lwjgl.input.Keyboard; |
||||
|
||||
|
||||
public class ScreenTextureTest extends Screen implements MouseButtonEvent.Listener { |
||||
|
||||
private RectConstraint kittenbox; |
||||
|
||||
private AnimDouble s = new AnimDouble(400, Easing.SINE_BOTH); |
||||
private AnimDouble x = new AnimDouble(200, Easing.ELASTIC_OUT); |
||||
private AnimDouble y = new AnimDouble(200, Easing.ELASTIC_OUT); |
||||
|
||||
private Random rand = new Random(); |
||||
|
||||
|
||||
public ScreenTextureTest(AppAccess app) { |
||||
super(app); |
||||
|
||||
kittenbox = c_move(c_box_sized(this, c_n(s), c_n(s)), c_n(x), c_n(y)); |
||||
|
||||
bindKeyStroke(new KeyStroke(Keyboard.KEY_ESCAPE), new Runnable() { |
||||
|
||||
@Override |
||||
public void run() |
||||
{ |
||||
snd().fadeOutAllLoops(); |
||||
bus().schedule(new ActionRequest(RequestType.SHUTDOWN), 3); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void deinitScreen() |
||||
{ |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void onScreenEnter() |
||||
{ |
||||
System.out.println("YOLO"); |
||||
Res.getLoop("test.wilderness").fadeIn(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void onScreenLeave() |
||||
{ |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void renderScreen() |
||||
{ |
||||
Render.quadTextured(kittenbox.getRect(), Res.getTexture("test.kitten")); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void updateScreen(double delta) |
||||
{ |
||||
s.update(delta); |
||||
x.update(delta); |
||||
y.update(delta); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void receive(MouseButtonEvent event) |
||||
{ |
||||
if (!event.isDown()) return; |
||||
|
||||
Coord pos = event.getPos(); |
||||
|
||||
double newSize = 200 + rand.nextInt(600); |
||||
|
||||
double t = 2; |
||||
|
||||
s.fadeTo(newSize, t / 2D); |
||||
x.fadeTo(pos.x - newSize / 2D, t); |
||||
y.fadeTo(pos.y - newSize / 2D, t); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,349 @@ |
||||
package mightypork.rogue.render; |
||||
|
||||
|
||||
import static org.lwjgl.opengl.GL11.*; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import mightypork.rogue.render.textures.TxQuad; |
||||
import mightypork.utils.files.FileUtils; |
||||
import mightypork.utils.logging.Log; |
||||
import mightypork.utils.math.color.RGB; |
||||
import mightypork.utils.math.coord.Coord; |
||||
import mightypork.utils.math.coord.Rect; |
||||
|
||||
import org.newdawn.slick.opengl.SlickCallable; |
||||
import org.newdawn.slick.opengl.Texture; |
||||
import org.newdawn.slick.opengl.TextureImpl; |
||||
import org.newdawn.slick.opengl.TextureLoader; |
||||
import org.newdawn.slick.util.ResourceLoader; |
||||
|
||||
|
||||
/** |
||||
* Render utilities |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public class Render { |
||||
|
||||
private static final Coord AXIS_X = new Coord(1, 0, 0); |
||||
private static final Coord AXIS_Y = new Coord(0, 1, 0); |
||||
private static final Coord AXIS_Z = new Coord(0, 0, 1); |
||||
|
||||
private static boolean inited = false; |
||||
|
||||
|
||||
/** |
||||
* Bind GL color |
||||
* |
||||
* @param color RGB color |
||||
*/ |
||||
public static void setColor(RGB color) |
||||
{ |
||||
if (color != null) glColor4d(color.r, color.g, color.b, color.a); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Bind GL color |
||||
* |
||||
* @param color RGB color |
||||
* @param alpha alpha multiplier |
||||
*/ |
||||
public static void setColor(RGB color, double alpha) |
||||
{ |
||||
if (color != null) glColor4d(color.r, color.g, color.b, color.a * alpha); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Translate with coord |
||||
* |
||||
* @param coord coord |
||||
*/ |
||||
public static void translate(Coord coord) |
||||
{ |
||||
glTranslated(coord.x, coord.y, coord.z); |
||||
} |
||||
|
||||
|
||||
public static void rotateX(double angle) |
||||
{ |
||||
rotate(angle, AXIS_X); |
||||
} |
||||
|
||||
|
||||
public static void rotateY(double angle) |
||||
{ |
||||
rotate(angle, AXIS_Y); |
||||
} |
||||
|
||||
|
||||
public static void rotateZ(double angle) |
||||
{ |
||||
rotate(angle, AXIS_Z); |
||||
} |
||||
|
||||
|
||||
public static void rotate(double angle, Coord axis) |
||||
{ |
||||
Coord vec = axis.norm(1); |
||||
glRotated(angle, vec.x, vec.y, vec.z); |
||||
} |
||||
|
||||
|
||||
public static void pushState() |
||||
{ |
||||
SlickCallable.enterSafeBlock(); |
||||
} |
||||
|
||||
|
||||
public static void popState() |
||||
{ |
||||
SlickCallable.leaveSafeBlock(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Load texture |
||||
* |
||||
* @param resourcePath |
||||
* @return the loaded texture |
||||
*/ |
||||
public static Texture loadTexture(String resourcePath) |
||||
{ |
||||
if (!inited) { |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
||||
inited = true; |
||||
} |
||||
|
||||
try { |
||||
|
||||
String ext = FileUtils.getExtension(resourcePath).toUpperCase(); |
||||
|
||||
Log.f3("Loading texture " + ext + " at " + resourcePath); |
||||
|
||||
Texture texture = TextureLoader.getTexture(ext, ResourceLoader.getResourceAsStream(resourcePath)); |
||||
|
||||
if (texture == null) { |
||||
Log.w("Texture " + resourcePath + " could not be loaded."); |
||||
} |
||||
|
||||
return texture; |
||||
|
||||
} catch (IOException e) { |
||||
Log.e("Loading of texture " + resourcePath + " failed.", e); |
||||
throw new RuntimeException("Could not load texture " + resourcePath + ".", e); |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
/** |
||||
* Bind texture |
||||
* |
||||
* @param texture the texture |
||||
* @throws RuntimeException if not loaded yet |
||||
*/ |
||||
private static void bindTexture(Texture texture) throws RuntimeException |
||||
{ |
||||
texture.bind(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Unbind all |
||||
*/ |
||||
private static void unbindTexture() |
||||
{ |
||||
if (TextureImpl.getLastBind() != null) { |
||||
TextureImpl.bindNone(); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Render quad 2D |
||||
* |
||||
* @param rect rectangle |
||||
* @param color draw color |
||||
*/ |
||||
public static void quad(Rect rect, RGB color) |
||||
{ |
||||
setColor(color); |
||||
quad(rect); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Render quad |
||||
* |
||||
* @param quad the quad to draw (px) |
||||
*/ |
||||
public static void quad(Rect quad) |
||||
{ |
||||
double left = quad.xMin(); |
||||
double bottom = quad.yMin(); |
||||
double right = quad.yMax(); |
||||
double top = quad.yMax(); |
||||
|
||||
// draw with color
|
||||
unbindTexture(); |
||||
|
||||
// quad
|
||||
glBegin(GL_QUADS); |
||||
glVertex2d(left, top); |
||||
glVertex2d(right, top); |
||||
glVertex2d(right, bottom); |
||||
glVertex2d(left, bottom); |
||||
glEnd(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Render textured rect (texture must be binded already) |
||||
* |
||||
* @param quad rectangle (px) |
||||
* @param uvs texture coords (0-1) |
||||
*/ |
||||
private static void quadUV(Rect quad, Rect uvs) |
||||
{ |
||||
double left = quad.xMin(); |
||||
double bottom = quad.yMin(); |
||||
double right = quad.xMax(); |
||||
double top = quad.yMax(); |
||||
|
||||
double tleft = uvs.xMin(); |
||||
double tbottom = uvs.yMin(); |
||||
double tright = uvs.xMax(); |
||||
double ttop = uvs.yMax(); |
||||
|
||||
// quad with texture
|
||||
glBegin(GL_QUADS); |
||||
glTexCoord2d(tleft, ttop); |
||||
glVertex2d(left, top); |
||||
glTexCoord2d(tright, ttop); |
||||
glVertex2d(right, top); |
||||
glTexCoord2d(tright, tbottom); |
||||
glVertex2d(right, bottom); |
||||
glTexCoord2d(tleft, tbottom); |
||||
glVertex2d(left, bottom); |
||||
glEnd(); |
||||
} |
||||
|
||||
|
||||
public static void quadGradH(Rect quad, RGB colorLeft, RGB colorRight) |
||||
{ |
||||
double left = quad.xMin(); |
||||
double bottom = quad.yMin(); |
||||
double right = quad.yMax(); |
||||
double top = quad.yMax(); |
||||
|
||||
// draw with color
|
||||
unbindTexture(); |
||||
|
||||
glBegin(GL_QUADS); |
||||
setColor(colorLeft); |
||||
glVertex2d(left, top); |
||||
setColor(colorRight); |
||||
glVertex2d(right, top); |
||||
|
||||
setColor(colorRight); |
||||
glVertex2d(right, bottom); |
||||
setColor(colorLeft); |
||||
glVertex2d(left, bottom); |
||||
glEnd(); |
||||
} |
||||
|
||||
|
||||
public static void quadGradV(Rect quad, RGB colorTop, RGB colorBottom) |
||||
{ |
||||
double left = quad.xMin(); |
||||
double bottom = quad.yMin(); |
||||
double right = quad.yMax(); |
||||
double top = quad.yMax(); |
||||
|
||||
// draw with color
|
||||
unbindTexture(); |
||||
|
||||
glBegin(GL_QUADS); |
||||
setColor(colorTop); |
||||
glVertex2d(left, top); |
||||
glVertex2d(right, top); |
||||
|
||||
setColor(colorBottom); |
||||
glVertex2d(right, bottom); |
||||
glVertex2d(left, bottom); |
||||
glEnd(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Render textured rect |
||||
* |
||||
* @param quad rectangle (px) |
||||
* @param uvs texture coords rectangle (0-1) |
||||
* @param texture texture instance |
||||
* @param tint color tint |
||||
*/ |
||||
public static void quadTextured(Rect quad, Rect uvs, Texture texture, RGB tint) |
||||
{ |
||||
bindTexture(texture); |
||||
setColor(tint); |
||||
quadUV(quad, uvs); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Render textured rect |
||||
* |
||||
* @param quad rectangle (px) |
||||
* @param uvs texture coords rectangle (px) |
||||
* @param texture texture instance |
||||
*/ |
||||
public static void quadTextured(Rect quad, Rect uvs, Texture texture) |
||||
{ |
||||
quadTextured(quad, uvs, texture, RGB.WHITE); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Render textured rect |
||||
* |
||||
* @param quad rectangle (px) |
||||
* @param texture texture instance |
||||
*/ |
||||
public static void quadTextured(Rect quad, Texture texture) |
||||
{ |
||||
quadTextured(quad, Rect.one(), texture, RGB.WHITE); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Render textured rect |
||||
* |
||||
* @param quad rectangle (px) |
||||
* @param txquad texture quad |
||||
*/ |
||||
public static void quadTextured(Rect quad, TxQuad txquad) |
||||
{ |
||||
quadTextured(quad, txquad, RGB.WHITE); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Render textured rect |
||||
* |
||||
* @param quad rectangle (px) |
||||
* @param txquad texture instance |
||||
* @param tint color tint |
||||
*/ |
||||
public static void quadTextured(Rect quad, TxQuad txquad, RGB tint) |
||||
{ |
||||
quadTextured(quad, txquad.uvs, txquad.tx, tint); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,91 @@ |
||||
package mightypork.rogue.render.textures; |
||||
|
||||
|
||||
import mightypork.utils.math.coord.Rect; |
||||
|
||||
import org.newdawn.slick.opengl.Texture; |
||||
|
||||
|
||||
/** |
||||
* Texture Quad (describing a part of a texture) |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public class TxQuad { |
||||
|
||||
/** The texture */ |
||||
public Texture tx; |
||||
/** Coords in texture (0-1) */ |
||||
public Rect uvs; |
||||
|
||||
|
||||
/** |
||||
* TxQuad from origin and size in pixels |
||||
* |
||||
* @param tx texture |
||||
* @param xPx left top X (0-1) |
||||
* @param yPx left top Y (0-1) |
||||
* @param widthPx area width (0-1) |
||||
* @param heightPx area height (0-1) |
||||
* @return new TxQuad |
||||
*/ |
||||
public static TxQuad fromSizePx(Texture tx, double xPx, double yPx, double widthPx, double heightPx) |
||||
{ |
||||
double w = tx.getImageWidth(); |
||||
double h = tx.getImageHeight(); |
||||
|
||||
return fromSize(tx, xPx / w, yPx / h, widthPx / w, heightPx / h); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* TxQuad from origin and size 0-1 |
||||
* |
||||
* @param tx texture |
||||
* @param x1 left top X (0-1) |
||||
* @param y1 left top Y (0-1) |
||||
* @param width area width (0-1) |
||||
* @param height area height (0-1) |
||||
* @return new TxQuad |
||||
*/ |
||||
public static TxQuad fromSize(Texture tx, double x1, double y1, double width, double height) |
||||
{ |
||||
return new TxQuad(tx, x1, y1, x1 + width, y1 + height); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Make of coords |
||||
* |
||||
* @param tx texture |
||||
* @param x1 left top X (0-1) |
||||
* @param y1 left top Y (0-1) |
||||
* @param x2 right bottom X (0-1) |
||||
* @param y2 right bottom Y (0-1) |
||||
*/ |
||||
public TxQuad(Texture tx, double x1, double y1, double x2, double y2) { |
||||
this(tx, new Rect(x1, y1, x2, y2)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @param tx Texture |
||||
* @param uvs Rect of texturwe UVs (pixels - from left top) |
||||
*/ |
||||
public TxQuad(Texture tx, Rect uvs) { |
||||
this.tx = tx; |
||||
this.uvs = uvs.copy(); |
||||
} |
||||
|
||||
|
||||
public TxQuad(TxQuad txQuad) { |
||||
this.tx = txQuad.tx; |
||||
this.uvs = txQuad.uvs.copy(); |
||||
} |
||||
|
||||
|
||||
public TxQuad copy() |
||||
{ |
||||
return new TxQuad(this); |
||||
} |
||||
} |
@ -1,17 +0,0 @@ |
||||
package mightypork.rogue.sounds; |
||||
|
||||
|
||||
public class NullAudio extends AudioX { |
||||
|
||||
public NullAudio() { |
||||
super(""); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean load() |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,70 +0,0 @@ |
||||
package mightypork.rogue.textures; |
||||
|
||||
|
||||
import mightypork.utils.math.coord.Coord; |
||||
import mightypork.utils.math.coord.Rect; |
||||
|
||||
import org.newdawn.slick.opengl.Texture; |
||||
|
||||
|
||||
/** |
||||
* Texture Quad (describing a part of a texture) |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public class TxQuad { |
||||
|
||||
/** The texture */ |
||||
public Texture tx; |
||||
/** Coords in texture (pixels) */ |
||||
public Rect uvs; |
||||
/** Quad size */ |
||||
public Coord size; |
||||
|
||||
|
||||
/** |
||||
* Create TxQuad from left top coord and rect size |
||||
* |
||||
* @param tx texture |
||||
* @param x1 left top X |
||||
* @param y1 left top Y |
||||
* @param width area width |
||||
* @param height area height |
||||
* @return new TxQuad |
||||
*/ |
||||
public static TxQuad fromSize(Texture tx, int x1, int y1, int width, int height) |
||||
{ |
||||
return new TxQuad(tx, x1, y1, x1 + width, y1 + height); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Make of coords |
||||
* |
||||
* @param tx texture |
||||
* @param x1 x1 |
||||
* @param y1 y1 |
||||
* @param x2 x2 |
||||
* @param y2 y2 |
||||
*/ |
||||
public TxQuad(Texture tx, int x1, int y1, int x2, int y2) { |
||||
this(tx, new Rect(x1, y1, x2, y2)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @param tx Texture |
||||
* @param uvs Rect of texturwe UVs (pixels - from left top) |
||||
*/ |
||||
public TxQuad(Texture tx, Rect uvs) { |
||||
this.tx = tx; |
||||
this.uvs = uvs.copy(); |
||||
this.size = uvs.getSize(); |
||||
} |
||||
|
||||
|
||||
public TxQuad copy() |
||||
{ |
||||
return new TxQuad(tx, uvs); |
||||
} |
||||
} |
@ -0,0 +1,130 @@ |
||||
package mightypork.utils.control.bus; |
||||
|
||||
|
||||
import java.util.Collection; |
||||
import java.util.HashSet; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
|
||||
|
||||
/** |
||||
* HashSet that buffers add and remove calls and performs them all at once when |
||||
* a flush() method is called. |
||||
* |
||||
* @author MightyPork |
||||
* @param <E> element type |
||||
*/ |
||||
public class BufferedHashSet<E> extends HashSet<E> { |
||||
|
||||
private List<E> toAdd = new LinkedList<E>(); |
||||
private List<Object> toRemove = new LinkedList<Object>(); |
||||
private boolean buffering = false; |
||||
|
||||
|
||||
public BufferedHashSet() { |
||||
super(); |
||||
} |
||||
|
||||
|
||||
public BufferedHashSet(Collection<? extends E> c) { |
||||
super(c); |
||||
} |
||||
|
||||
|
||||
public BufferedHashSet(int initialCapacity, float loadFactor) { |
||||
super(initialCapacity, loadFactor); |
||||
} |
||||
|
||||
|
||||
public BufferedHashSet(int initialCapacity) { |
||||
super(initialCapacity); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean add(E e) |
||||
{ |
||||
if (buffering) { |
||||
toAdd.add(e); |
||||
} else { |
||||
super.add(e); |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean remove(Object e) |
||||
{ |
||||
if (buffering) { |
||||
toRemove.add(e); |
||||
} else { |
||||
super.remove(e); |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Flush all |
||||
*/ |
||||
private void flush() |
||||
{ |
||||
for (E e : toAdd) { |
||||
super.add(e); |
||||
} |
||||
|
||||
for (Object e : toRemove) { |
||||
super.remove(e); |
||||
} |
||||
|
||||
toAdd.clear(); |
||||
toRemove.clear(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean removeAll(Collection<?> c) |
||||
{ |
||||
throw new UnsupportedOperationException(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean containsAll(Collection<?> c) |
||||
{ |
||||
throw new UnsupportedOperationException(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean addAll(Collection<? extends E> c) |
||||
{ |
||||
throw new UnsupportedOperationException(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean retainAll(Collection<?> c) |
||||
{ |
||||
throw new UnsupportedOperationException(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Toggle buffering |
||||
* |
||||
* @param enable enable buffering |
||||
*/ |
||||
public void setBuffering(boolean enable) |
||||
{ |
||||
if (this.buffering && !enable) { |
||||
flush(); |
||||
} |
||||
|
||||
this.buffering = enable; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue