getting rid of the slick (App)GameContainer

This commit is contained in:
yugecin 2017-01-09 22:42:59 +01:00
parent d5796e5a1e
commit e8cac712fc
10 changed files with 315 additions and 106 deletions

View File

@ -18,25 +18,23 @@
package yugecin.opsudance; package yugecin.opsudance;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.newdawn.slick.SlickException; import org.lwjgl.LWJGLException;
import yugecin.opsudance.core.Container; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.Demux;
public class OpsuDance { public class OpsuDance {
private final Demux stateDemultiplexer; private final DisplayContainer container;
private final Container container;
@Inject @Inject
public OpsuDance(Demux stateDemultiplexer, Container container) { public OpsuDance(DisplayContainer container) {
this.stateDemultiplexer = stateDemultiplexer;
this.container = container; this.container = container;
} }
public void start() { public void start() {
try { try {
container.start(); container.setIcons(new String[] { "icon16.png", "icon32.png" });
} catch (SlickException e) { container.run();
} catch (LWJGLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -1,68 +0,0 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.core;
import com.google.inject.Inject;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;
/**
* based on itdelatrisu.opsu.Container
*/
public class Container extends AppGameContainer {
@Inject
public Container(Demux demux) throws SlickException {
super(demux);
setShowFPS(false);
}
@Override
public void start() throws SlickException {
try {
setup();
getDelta();
while (running())
gameLoop();
} catch (Exception e) {
}
destroy();
}
@Override
protected void gameLoop() throws SlickException {
int delta = getDelta();
if (!Display.isVisible() && updateOnlyOnVisible) {
try { Thread.sleep(100); } catch (Exception e) {}
} else {
try {
updateAndRender(delta);
} catch (SlickException e) {
running = false;
return;
}
}
updateFPS();
Display.update();
if (Display.isCloseRequested()) {
if (game.closeRequested())
running = false;
}
}
}

View File

@ -18,7 +18,7 @@
package yugecin.opsudance.core; package yugecin.opsudance.core;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.newdawn.slick.*; import org.newdawn.slick.Graphics;
import yugecin.opsudance.kernel.InstanceContainer; import yugecin.opsudance.kernel.InstanceContainer;
import yugecin.opsudance.states.EmptyState; import yugecin.opsudance.states.EmptyState;
import yugecin.opsudance.states.GameState; import yugecin.opsudance.states.GameState;
@ -26,7 +26,10 @@ import yugecin.opsudance.states.transitions.FadeInTransitionState;
import yugecin.opsudance.states.transitions.FadeOutTransitionState; import yugecin.opsudance.states.transitions.FadeOutTransitionState;
import yugecin.opsudance.states.transitions.TransitionState; import yugecin.opsudance.states.transitions.TransitionState;
public class Demux implements Game { /**
* state demultiplexer, sends events to current state
*/
public class Demux {
private final InstanceContainer instanceContainer; private final InstanceContainer instanceContainer;
@ -40,35 +43,25 @@ public class Demux implements Game {
this.instanceContainer = instanceContainer; this.instanceContainer = instanceContainer;
} }
@Override // cannot do this in constructor, would cause circular dependency
public void init(GameContainer container) throws SlickException { public void init() {
state = instanceContainer.provide(EmptyState.class);
fadeOutTransitionState = instanceContainer.provide(FadeOutTransitionState.class); fadeOutTransitionState = instanceContainer.provide(FadeOutTransitionState.class);
fadeInTransitionState = instanceContainer.provide(FadeInTransitionState.class); fadeInTransitionState = instanceContainer.provide(FadeInTransitionState.class);
state = instanceContainer.provide(EmptyState.class);
state.enter();
} }
@Override public void update(int delta) {
public void update(GameContainer container, int delta) throws SlickException {
state.update(delta); state.update(delta);
} }
@Override public void render(Graphics g) {
public void render(GameContainer container, Graphics g) throws SlickException {
state.render(g); state.render(g);
} }
@Override public boolean onCloseRequest() {
public boolean closeRequested() {
// TODO for what is this used
return !isTransitioning(); return !isTransitioning();
} }
@Override
public String getTitle() {
return "opsu!dance";
}
public boolean isTransitioning() { public boolean isTransitioning() {
return state == fadeInTransitionState || state == fadeOutTransitionState; return state == fadeInTransitionState || state == fadeOutTransitionState;
} }

View File

@ -0,0 +1,209 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.core;
import com.google.inject.Inject;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.openal.AL;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.opengl.ImageIOImageData;
import org.newdawn.slick.opengl.InternalTextureLoader;
import org.newdawn.slick.opengl.LoadableImageData;
import org.newdawn.slick.opengl.TGAImageData;
import org.newdawn.slick.opengl.renderer.Renderer;
import org.newdawn.slick.opengl.renderer.SGL;
import org.newdawn.slick.util.Log;
import org.newdawn.slick.util.ResourceLoader;
import yugecin.opsudance.states.EmptyRedState;
import yugecin.opsudance.utils.GLHelper;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.List;
/**
* based on org.newdawn.slick.AppGameContainer
*/
public class DisplayContainer {
private static SGL GL = Renderer.get();
private final Demux demux;
private final DisplayMode nativeDisplayMode;
private final List<ResolutionChangeListener> resolutionChangeListeners;
private Graphics graphics;
public int width;
public int height;
private long lastFrame;
@Inject
public DisplayContainer(Demux demux) {
this.demux = demux;
this.nativeDisplayMode = Display.getDisplayMode();
this.resolutionChangeListeners = new LinkedList<>();
lastFrame = getTime();
}
public void addResolutionChangeListener(ResolutionChangeListener listener) {
resolutionChangeListeners.add(listener);
}
public void run() throws LWJGLException {
demux.init();
demux.switchStateNow(new EmptyRedState(null, null));
setup();
while(!(Display.isCloseRequested() && demux.onCloseRequest())) {
// TODO: lower fps when not visible Display.isVisible
int delta = getDelta();
GL.glClear(SGL.GL_COLOR_BUFFER_BIT);
/*
graphics.resetTransform();
graphics.resetFont();
graphics.resetLineWidth();
graphics.resetTransform();
*/
demux.update(delta);
demux.render(graphics);
Display.update(true);
Display.sync(60);
}
teardown();
}
private void setup() {
Display.setTitle("opsu!dance");
try {
// temp displaymode to not flash the screen with a 1ms black window
Display.setDisplayMode(new DisplayMode(100, 100));
Display.create();
setDisplayMode(640, 480, false);
} catch (LWJGLException e) {
e.printStackTrace();
// TODO errorhandler dialog here
Log.error("could not initialize GL", e);
}
}
private void teardown() {
Display.destroy();
AL.destroy();
}
public void setDisplayMode(int width, int height, boolean fullscreen) throws LWJGLException {
if (this.width == width && this.height == height) {
Display.setFullscreen(fullscreen);
return;
}
DisplayMode displayMode = null;
if (fullscreen) {
displayMode = GLHelper.findFullscreenDisplayMode(nativeDisplayMode.getBitsPerPixel(), nativeDisplayMode.getFrequency(), width, height);
}
if (displayMode == null) {
displayMode = new DisplayMode(width,height);
if (fullscreen) {
fullscreen = false;
Log.warn("could not find fullscreen displaymode for " + width + "x" + height);
}
}
this.width = displayMode.getWidth();
this.height = displayMode.getHeight();
Display.setDisplayMode(displayMode);
Display.setFullscreen(fullscreen);
initGL();
for (ResolutionChangeListener resolutionChangeListener : resolutionChangeListeners) {
resolutionChangeListener.onDisplayResolutionChanged(width, height);
}
if (displayMode.getBitsPerPixel() == 16) {
InternalTextureLoader.get().set16BitMode();
}
getDelta();
}
private void initGL() {
GL.initDisplay(width, height);
GL.enterOrtho(width, height);
graphics = new Graphics(width, height);
graphics.setAntiAlias(false);
/*
if (input == null) {
input = new Input(height);
}
input.init(height);
// no need to remove listeners?
//input.removeAllListeners();
if (game instanceof InputListener) {
input.removeListener((InputListener) game);
input.addListener((InputListener) game);
}
*/
}
private int getDelta() {
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
public long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void setIcons(String[] refs) {
ByteBuffer[] bufs = new ByteBuffer[refs.length];
for (int i = 0; i < refs.length; i++) {
LoadableImageData data;
boolean flip = true;
if (refs[i].endsWith(".tga")) {
data = new TGAImageData();
} else {
flip = false;
data = new ImageIOImageData();
}
try {
bufs[i] = data.loadImage(ResourceLoader.getResourceAsStream(refs[i]), flip, false, null);
} catch (Exception e) {
Log.error("failed to set the icon", e);
return;
}
}
Display.setIcon(bufs);
}
}

View File

@ -0,0 +1,24 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.core;
public interface ResolutionChangeListener {
void onDisplayResolutionChanged(int width, int height);
}

View File

@ -19,7 +19,7 @@ package yugecin.opsudance.kernel;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import yugecin.opsudance.PreStartupInitializer; import yugecin.opsudance.PreStartupInitializer;
import yugecin.opsudance.core.Container; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.Demux; import yugecin.opsudance.core.Demux;
import yugecin.opsudance.states.EmptyRedState; import yugecin.opsudance.states.EmptyRedState;
import yugecin.opsudance.states.EmptyState; import yugecin.opsudance.states.EmptyState;
@ -31,12 +31,12 @@ public class OpsuDanceModule extends AbstractModule {
protected void configure() { protected void configure() {
bind(InstanceContainer.class).to(InstanceResolver.class); bind(InstanceContainer.class).to(InstanceResolver.class);
bind(PreStartupInitializer.class).asEagerSingleton(); bind(PreStartupInitializer.class).asEagerSingleton();
bind(Demux.class).asEagerSingleton(); bind(DisplayContainer.class).asEagerSingleton();
bind(Container.class).asEagerSingleton();
bind(FadeInTransitionState.class).asEagerSingleton(); bind(FadeInTransitionState.class).asEagerSingleton();
bind(FadeOutTransitionState.class).asEagerSingleton(); bind(FadeOutTransitionState.class).asEagerSingleton();
bind(EmptyRedState.class).asEagerSingleton(); bind(EmptyRedState.class).asEagerSingleton();
bind(EmptyState.class).asEagerSingleton(); bind(EmptyState.class).asEagerSingleton();
bind(Demux.class).asEagerSingleton();
} }
} }

View File

@ -18,15 +18,15 @@
package yugecin.opsudance.states.transitions; package yugecin.opsudance.states.transitions;
import com.google.inject.Inject; import com.google.inject.Inject;
import yugecin.opsudance.core.Container;
import yugecin.opsudance.core.Demux; import yugecin.opsudance.core.Demux;
import yugecin.opsudance.core.DisplayContainer;
public class FadeInTransitionState extends FadeTransitionState { public class FadeInTransitionState extends FadeTransitionState {
private final Demux demux; private final Demux demux;
@Inject @Inject
public FadeInTransitionState(Container container, Demux demux) { public FadeInTransitionState(DisplayContainer container, Demux demux) {
super(container, 300); super(container, 300);
this.demux = demux; this.demux = demux;
} }

View File

@ -18,8 +18,8 @@
package yugecin.opsudance.states.transitions; package yugecin.opsudance.states.transitions;
import com.google.inject.Inject; import com.google.inject.Inject;
import yugecin.opsudance.core.Container;
import yugecin.opsudance.core.Demux; import yugecin.opsudance.core.Demux;
import yugecin.opsudance.core.DisplayContainer;
public class FadeOutTransitionState extends FadeTransitionState { public class FadeOutTransitionState extends FadeTransitionState {
@ -27,7 +27,7 @@ public class FadeOutTransitionState extends FadeTransitionState {
private final FadeInTransitionState fadeInTransitionState; private final FadeInTransitionState fadeInTransitionState;
@Inject @Inject
public FadeOutTransitionState(Container container, Demux demux, FadeInTransitionState fadeInTransitionState) { public FadeOutTransitionState(DisplayContainer container, Demux demux, FadeInTransitionState fadeInTransitionState) {
super(container, 200); super(container, 200);
this.demux = demux; this.demux = demux;
this.fadeInTransitionState = fadeInTransitionState; this.fadeInTransitionState = fadeInTransitionState;

View File

@ -19,21 +19,21 @@ package yugecin.opsudance.states.transitions;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics; import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.Container; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.states.GameState; import yugecin.opsudance.states.GameState;
public abstract class FadeTransitionState extends TransitionState { public abstract class FadeTransitionState extends TransitionState {
protected GameState applicableState; protected GameState applicableState;
private final Container container; private final DisplayContainer container;
protected final int fadeTargetTime; protected final int fadeTargetTime;
protected int fadeTime; protected int fadeTime;
private final Color black; private final Color black;
public FadeTransitionState(Container container, int fadeTargetTime) { public FadeTransitionState(DisplayContainer container, int fadeTargetTime) {
super(fadeTargetTime); super(fadeTargetTime);
this.container = container; this.container = container;
this.fadeTargetTime = fadeTargetTime; this.fadeTargetTime = fadeTargetTime;
@ -58,7 +58,7 @@ public abstract class FadeTransitionState extends TransitionState {
applicableState.render(g); applicableState.render(g);
black.a = getMaskAlphaLevel((float) fadeTime / fadeTargetTime); black.a = getMaskAlphaLevel((float) fadeTime / fadeTargetTime);
g.setColor(black); g.setColor(black);
g.fillRect(0, 0, container.getWidth(), container.getHeight()); g.fillRect(0, 0, container.width, container.height);
} }
@Override @Override

View File

@ -0,0 +1,53 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.utils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class GLHelper {
/**
* from org.newdawn.slick.AppGameContainer#setDisplayMode
*/
public static DisplayMode findFullscreenDisplayMode(int targetBPP, int targetFrequency, int width, int height) throws LWJGLException {
DisplayMode[] modes = Display.getAvailableDisplayModes();
DisplayMode foundMode = null;
int freq = 0;
int bpp = 0;
for (DisplayMode current : modes) {
if (current.getWidth() != width || current.getHeight() != height) {
continue;
}
if (current.getBitsPerPixel() == targetBPP && current.getFrequency() == targetFrequency) {
return current;
}
if (current.getFrequency() >= freq && (foundMode == null || current.getBitsPerPixel() >= bpp)) {
foundMode = current;
freq = foundMode.getFrequency();
bpp = foundMode.getBitsPerPixel();
}
}
return foundMode;
}
}