You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
245 lines
4.1 KiB
245 lines
4.1 KiB
11 years ago
|
package mightypork.rogue.gui.screens;
|
||
11 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
import static org.lwjgl.opengl.GL11.*;
|
||
11 years ago
|
import mightypork.rogue.AppAccess;
|
||
11 years ago
|
import mightypork.rogue.bus.ChildClient;
|
||
11 years ago
|
import mightypork.rogue.bus.events.ScreenChangeEvent;
|
||
11 years ago
|
import mightypork.rogue.input.KeyBinder;
|
||
|
import mightypork.rogue.input.KeyBindingPool;
|
||
|
import mightypork.rogue.input.KeyStroke;
|
||
11 years ago
|
import mightypork.rogue.render.Render;
|
||
|
import mightypork.utils.control.interf.Destroyable;
|
||
11 years ago
|
import mightypork.utils.control.interf.Updateable;
|
||
11 years ago
|
import mightypork.utils.math.constraints.ConstraintContext;
|
||
11 years ago
|
import mightypork.utils.math.coord.Coord;
|
||
11 years ago
|
import mightypork.utils.math.coord.Rect;
|
||
11 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Screen class.<br>
|
||
|
* Screen animates 3D world, while contained panels render 2D overlays, process
|
||
|
* inputs and run the game logic.
|
||
|
*
|
||
|
* @author MightyPork
|
||
|
*/
|
||
11 years ago
|
public abstract class Screen extends ChildClient implements Destroyable, Updateable, KeyBinder, ConstraintContext, ScreenChangeEvent.Listener {
|
||
11 years ago
|
|
||
11 years ago
|
private final KeyBindingPool keybindings = new KeyBindingPool();
|
||
11 years ago
|
|
||
11 years ago
|
private boolean active;
|
||
11 years ago
|
private boolean needSetupGraphics = false;
|
||
|
private boolean needSetupViewport = false;
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
public Screen(AppAccess app) {
|
||
11 years ago
|
super(app);
|
||
11 years ago
|
|
||
11 years ago
|
// disable events initially
|
||
11 years ago
|
setListening(false);
|
||
|
|
||
|
addChildClient(keybindings);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
@Override
|
||
11 years ago
|
public final void bindKeyStroke(KeyStroke stroke, Runnable task)
|
||
11 years ago
|
{
|
||
|
keybindings.bindKeyStroke(stroke, task);
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
@Override
|
||
11 years ago
|
public final void unbindKeyStroke(KeyStroke stroke)
|
||
11 years ago
|
{
|
||
|
keybindings.unbindKeyStroke(stroke);
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
@Override
|
||
11 years ago
|
public final void destroy()
|
||
11 years ago
|
{
|
||
|
deinitScreen();
|
||
|
}
|
||
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Prepare for being shown
|
||
11 years ago
|
*
|
||
11 years ago
|
* @param shown true to show, false to hide
|
||
11 years ago
|
*/
|
||
|
public final void setActive(boolean shown)
|
||
|
{
|
||
|
if (shown) {
|
||
|
active = true;
|
||
11 years ago
|
needSetupGraphics = true;
|
||
|
needSetupViewport = true;
|
||
|
|
||
11 years ago
|
onSizeChanged(getRect().getSize());
|
||
|
onScreenEnter();
|
||
11 years ago
|
|
||
11 years ago
|
// enable events
|
||
|
setListening(true);
|
||
11 years ago
|
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onScreenLeave();
|
||
11 years ago
|
|
||
11 years ago
|
active = false;
|
||
11 years ago
|
|
||
11 years ago
|
// disable events
|
||
|
setListening(false);
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Clean up before screen is destroyed.
|
||
|
*/
|
||
|
protected abstract void deinitScreen();
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Called when the screen becomes active
|
||
|
*/
|
||
11 years ago
|
protected abstract void onScreenEnter();
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Called when the screen is no longer active
|
||
|
*/
|
||
11 years ago
|
protected abstract void onScreenLeave();
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Update GUI for new screen size
|
||
|
*
|
||
11 years ago
|
* @param size screen size
|
||
11 years ago
|
*/
|
||
11 years ago
|
protected void onSizeChanged(Coord size)
|
||
|
{
|
||
|
// no impl
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Render screen contents (context is ready for 2D rendering)
|
||
|
*/
|
||
|
protected abstract void renderScreen();
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* Update animations and timing.<br>
|
||
11 years ago
|
*
|
||
11 years ago
|
* @param delta time elapsed
|
||
11 years ago
|
*/
|
||
|
protected abstract void updateScreen(double delta);
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Render screen
|
||
|
*/
|
||
11 years ago
|
private void renderBegin()
|
||
11 years ago
|
{
|
||
11 years ago
|
Render.pushState();
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Render screen
|
||
|
*/
|
||
11 years ago
|
private void renderEnd()
|
||
11 years ago
|
{
|
||
11 years ago
|
Render.popState();
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* @return true if screen is the curretn screen
|
||
|
*/
|
||
11 years ago
|
public final boolean isActive()
|
||
11 years ago
|
{
|
||
|
return active;
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
@Override
|
||
|
public final void receive(ScreenChangeEvent event)
|
||
|
{
|
||
|
if (!isActive()) return;
|
||
11 years ago
|
|
||
11 years ago
|
onSizeChanged(event.getScreenSize());
|
||
11 years ago
|
|
||
|
needSetupViewport = true;
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Update and render the screen
|
||
|
*/
|
||
|
@Override
|
||
|
public final void update(double delta)
|
||
|
{
|
||
|
updateScreen(delta);
|
||
11 years ago
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public final Rect getRect()
|
||
|
{
|
||
|
return disp().getRect();
|
||
|
}
|
||
|
|
||
|
|
||
|
public final void render()
|
||
|
{
|
||
|
if (!isActive()) return;
|
||
|
|
||
|
if (needSetupGraphics) {
|
||
|
setupGraphics();
|
||
|
}
|
||
|
|
||
|
if (needSetupViewport) {
|
||
|
setupViewport();
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
renderBegin();
|
||
|
renderScreen();
|
||
|
renderEnd();
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
private void setupGraphics()
|
||
11 years ago
|
{
|
||
11 years ago
|
glMatrixMode(GL_MODELVIEW);
|
||
|
glLoadIdentity();
|
||
|
|
||
|
glDisable(GL_LIGHTING);
|
||
|
|
||
|
glClearDepth(1f);
|
||
|
glEnable(GL_DEPTH_TEST);
|
||
|
glDepthFunc(GL_LEQUAL);
|
||
|
|
||
|
glEnable(GL_NORMALIZE);
|
||
|
|
||
|
glShadeModel(GL_SMOOTH);
|
||
|
|
||
|
glEnable(GL_BLEND);
|
||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||
|
}
|
||
|
|
||
|
|
||
|
private void setupViewport()
|
||
|
{
|
||
|
// fix projection for changed size
|
||
|
glMatrixMode(GL_PROJECTION);
|
||
|
glLoadIdentity();
|
||
|
Coord s = disp().getSize();
|
||
|
glViewport(0, 0, s.xi(), s.yi());
|
||
|
glOrtho(0, s.x, s.y, 0, -1000, 1000);
|
||
|
|
||
|
// back to modelview
|
||
|
glMatrixMode(GL_MODELVIEW);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
}
|