2017-01-09 22:42:59 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2017-01-15 23:36:21 +01:00
|
|
|
import itdelatrisu.opsu.GameImage;
|
|
|
|
import itdelatrisu.opsu.ui.Fonts;
|
2017-01-09 22:42:59 +01:00
|
|
|
import org.lwjgl.LWJGLException;
|
|
|
|
import org.lwjgl.Sys;
|
|
|
|
import org.lwjgl.openal.AL;
|
|
|
|
import org.lwjgl.opengl.Display;
|
|
|
|
import org.lwjgl.opengl.DisplayMode;
|
2017-01-11 20:41:13 +01:00
|
|
|
import org.lwjgl.opengl.GL11;
|
2017-01-09 22:42:59 +01:00
|
|
|
import org.newdawn.slick.Graphics;
|
2017-01-10 12:43:05 +01:00
|
|
|
import org.newdawn.slick.Input;
|
2017-01-09 22:42:59 +01:00
|
|
|
import org.newdawn.slick.opengl.InternalTextureLoader;
|
|
|
|
import org.newdawn.slick.opengl.renderer.Renderer;
|
|
|
|
import org.newdawn.slick.opengl.renderer.SGL;
|
|
|
|
import org.newdawn.slick.util.Log;
|
2017-01-15 22:46:09 +01:00
|
|
|
import yugecin.opsudance.core.events.EventBus;
|
2017-01-16 21:53:48 +01:00
|
|
|
import yugecin.opsudance.core.errorhandling.ErrorDumpable;
|
2017-01-15 22:46:09 +01:00
|
|
|
import yugecin.opsudance.events.ResolutionChangedEvent;
|
2017-01-09 22:42:59 +01:00
|
|
|
import yugecin.opsudance.utils.GLHelper;
|
|
|
|
|
2017-01-11 20:41:13 +01:00
|
|
|
import java.io.StringWriter;
|
2017-01-09 22:42:59 +01:00
|
|
|
|
2017-01-16 21:53:48 +01:00
|
|
|
import static yugecin.opsudance.core.Entrypoint.sout;
|
2017-01-09 23:10:51 +01:00
|
|
|
|
2017-01-09 22:42:59 +01:00
|
|
|
/**
|
|
|
|
* based on org.newdawn.slick.AppGameContainer
|
|
|
|
*/
|
2017-01-11 20:41:13 +01:00
|
|
|
public class DisplayContainer implements ErrorDumpable {
|
2017-01-09 22:42:59 +01:00
|
|
|
|
|
|
|
private static SGL GL = Renderer.get();
|
|
|
|
|
2017-01-15 22:46:09 +01:00
|
|
|
public final EventBus eventBus;
|
2017-01-13 16:28:36 +01:00
|
|
|
public final Demux demux;
|
2017-01-15 22:46:09 +01:00
|
|
|
|
2017-01-09 22:42:59 +01:00
|
|
|
private final DisplayMode nativeDisplayMode;
|
|
|
|
|
|
|
|
private Graphics graphics;
|
2017-01-10 12:43:05 +01:00
|
|
|
private Input input;
|
2017-01-09 22:42:59 +01:00
|
|
|
|
|
|
|
public int width;
|
|
|
|
public int height;
|
|
|
|
|
2017-01-10 13:02:40 +01:00
|
|
|
public int targetRenderInterval;
|
|
|
|
public int targetBackgroundRenderInterval;
|
|
|
|
|
|
|
|
public int realRenderInterval;
|
|
|
|
public int delta;
|
|
|
|
|
|
|
|
public int timeSinceLastRender;
|
|
|
|
|
2017-01-09 22:42:59 +01:00
|
|
|
private long lastFrame;
|
|
|
|
|
2017-01-11 20:41:13 +01:00
|
|
|
private String glVersion;
|
|
|
|
private String glVendor;
|
|
|
|
|
2017-01-15 22:46:09 +01:00
|
|
|
public DisplayContainer(Demux demux, EventBus eventBus) {
|
2017-01-09 22:42:59 +01:00
|
|
|
this.demux = demux;
|
2017-01-15 22:46:09 +01:00
|
|
|
this.eventBus = eventBus;
|
2017-01-09 22:42:59 +01:00
|
|
|
this.nativeDisplayMode = Display.getDisplayMode();
|
2017-01-10 13:02:40 +01:00
|
|
|
targetRenderInterval = 16; // ~60 fps
|
|
|
|
targetBackgroundRenderInterval = 41; // ~24 fps
|
2017-01-09 22:42:59 +01:00
|
|
|
lastFrame = getTime();
|
2017-01-15 23:38:20 +01:00
|
|
|
delta = 1;
|
|
|
|
realRenderInterval = 1;
|
2017-01-09 22:42:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:41:13 +01:00
|
|
|
public void run() throws LWJGLException {
|
2017-01-09 22:42:59 +01:00
|
|
|
while(!(Display.isCloseRequested() && demux.onCloseRequest())) {
|
2017-01-10 13:02:40 +01:00
|
|
|
delta = getDelta();
|
|
|
|
|
|
|
|
timeSinceLastRender += delta;
|
|
|
|
|
2017-01-10 12:43:05 +01:00
|
|
|
input.poll(width, height);
|
2017-01-10 14:11:15 +01:00
|
|
|
demux.update(delta);
|
2017-01-10 13:02:40 +01:00
|
|
|
|
|
|
|
int maxRenderInterval;
|
|
|
|
if (Display.isVisible() && Display.isActive()) {
|
|
|
|
maxRenderInterval = targetRenderInterval;
|
|
|
|
} else {
|
|
|
|
maxRenderInterval = targetBackgroundRenderInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeSinceLastRender >= maxRenderInterval) {
|
|
|
|
GL.glClear(SGL.GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
graphics.resetTransform();
|
|
|
|
graphics.resetFont();
|
|
|
|
graphics.resetLineWidth();
|
|
|
|
graphics.resetTransform();
|
|
|
|
*/
|
|
|
|
|
2017-01-10 14:11:15 +01:00
|
|
|
demux.preRenderUpdate(timeSinceLastRender);
|
2017-01-10 13:02:40 +01:00
|
|
|
demux.render(graphics);
|
|
|
|
|
|
|
|
realRenderInterval = timeSinceLastRender;
|
|
|
|
timeSinceLastRender = 0;
|
|
|
|
|
|
|
|
Display.update(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Display.processMessages();
|
2017-01-12 12:45:14 +01:00
|
|
|
Display.sync(1000); // TODO add option to change this, to not eat CPUs
|
2017-01-09 22:42:59 +01:00
|
|
|
}
|
|
|
|
teardown();
|
|
|
|
}
|
|
|
|
|
2017-01-15 23:36:21 +01:00
|
|
|
public void setup() throws Exception {
|
2017-01-11 20:41:13 +01:00
|
|
|
width = height = -1;
|
2017-01-10 13:16:57 +01:00
|
|
|
Input.disableControllers();
|
2017-01-09 22:42:59 +01:00
|
|
|
Display.setTitle("opsu!dance");
|
2017-01-11 20:41:13 +01:00
|
|
|
// temp displaymode to not flash the screen with a 1ms black window
|
|
|
|
Display.setDisplayMode(new DisplayMode(100, 100));
|
|
|
|
Display.create();
|
|
|
|
GLHelper.setIcons(new String[] { "icon16.png", "icon32.png" });
|
2017-01-15 23:36:21 +01:00
|
|
|
setDisplayMode(800, 600, false);
|
2017-01-11 20:46:10 +01:00
|
|
|
sout("GL ready");
|
2017-01-11 20:41:13 +01:00
|
|
|
glVersion = GL11.glGetString(GL11.GL_VERSION);
|
|
|
|
glVendor = GL11.glGetString(GL11.GL_VENDOR);
|
2017-01-09 22:42:59 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:41:13 +01:00
|
|
|
public void teardown() {
|
2017-01-09 22:42:59 +01:00
|
|
|
Display.destroy();
|
|
|
|
AL.destroy();
|
|
|
|
}
|
|
|
|
|
2017-01-15 23:36:21 +01:00
|
|
|
public void setDisplayMode(int width, int height, boolean fullscreen) throws Exception {
|
2017-01-09 22:42:59 +01:00
|
|
|
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();
|
|
|
|
|
2017-01-15 22:46:09 +01:00
|
|
|
eventBus.post(new ResolutionChangedEvent(this.width, this.height));
|
2017-01-09 22:42:59 +01:00
|
|
|
|
|
|
|
if (displayMode.getBitsPerPixel() == 16) {
|
|
|
|
InternalTextureLoader.get().set16BitMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 23:36:21 +01:00
|
|
|
private void initGL() throws Exception {
|
2017-01-09 22:42:59 +01:00
|
|
|
GL.initDisplay(width, height);
|
|
|
|
GL.enterOrtho(width, height);
|
|
|
|
|
|
|
|
graphics = new Graphics(width, height);
|
|
|
|
graphics.setAntiAlias(false);
|
|
|
|
|
2017-01-10 12:43:05 +01:00
|
|
|
input = new Input(height);
|
|
|
|
input.addKeyListener(demux);
|
|
|
|
input.addMouseListener(demux);
|
2017-01-15 23:36:21 +01:00
|
|
|
|
|
|
|
GameImage.init(width, height);
|
|
|
|
Fonts.init();
|
2017-01-09 22:42:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private int getDelta() {
|
|
|
|
long time = getTime();
|
|
|
|
int delta = (int) (time - lastFrame);
|
|
|
|
lastFrame = time;
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getTime() {
|
|
|
|
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:41:13 +01:00
|
|
|
@Override
|
|
|
|
public void writeErrorDump(StringWriter dump) {
|
2017-01-15 00:11:52 +01:00
|
|
|
dump.append("> DisplayContainer dump\n");
|
|
|
|
dump.append("OpenGL version: ").append(glVersion).append( "(").append(glVendor).append(")\n");
|
|
|
|
demux.writeErrorDump(dump);
|
2017-01-11 20:41:13 +01:00
|
|
|
}
|
|
|
|
|
2017-01-09 22:42:59 +01:00
|
|
|
}
|