use EventBus instead of ResolutionChangeListener

This commit is contained in:
yugecin 2017-01-15 22:46:09 +01:00
parent ec39392689
commit 54b1b3bb1c
4 changed files with 24 additions and 19 deletions

View File

@ -30,12 +30,12 @@ import org.newdawn.slick.opengl.InternalTextureLoader;
import org.newdawn.slick.opengl.renderer.Renderer; import org.newdawn.slick.opengl.renderer.Renderer;
import org.newdawn.slick.opengl.renderer.SGL; import org.newdawn.slick.opengl.renderer.SGL;
import org.newdawn.slick.util.Log; import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.errorhandling.ErrorDumpable; import yugecin.opsudance.errorhandling.ErrorDumpable;
import yugecin.opsudance.events.ResolutionChangedEvent;
import yugecin.opsudance.utils.GLHelper; import yugecin.opsudance.utils.GLHelper;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.LinkedList;
import java.util.List;
import static yugecin.opsudance.kernel.Entrypoint.sout; import static yugecin.opsudance.kernel.Entrypoint.sout;
@ -46,9 +46,10 @@ public class DisplayContainer implements ErrorDumpable {
private static SGL GL = Renderer.get(); private static SGL GL = Renderer.get();
public final EventBus eventBus;
public final Demux demux; public final Demux demux;
private final DisplayMode nativeDisplayMode; private final DisplayMode nativeDisplayMode;
private final List<ResolutionChangeListener> resolutionChangeListeners;
private Graphics graphics; private Graphics graphics;
private Input input; private Input input;
@ -70,19 +71,15 @@ public class DisplayContainer implements ErrorDumpable {
private String glVendor; private String glVendor;
@Inject @Inject
public DisplayContainer(Demux demux) { public DisplayContainer(Demux demux, EventBus eventBus) {
this.demux = demux; this.demux = demux;
this.eventBus = eventBus;
this.nativeDisplayMode = Display.getDisplayMode(); this.nativeDisplayMode = Display.getDisplayMode();
this.resolutionChangeListeners = new LinkedList<>();
targetRenderInterval = 16; // ~60 fps targetRenderInterval = 16; // ~60 fps
targetBackgroundRenderInterval = 41; // ~24 fps targetBackgroundRenderInterval = 41; // ~24 fps
lastFrame = getTime(); lastFrame = getTime();
} }
public void addResolutionChangeListener(ResolutionChangeListener listener) {
resolutionChangeListeners.add(listener);
}
public void run() throws LWJGLException { public void run() throws LWJGLException {
while(!(Display.isCloseRequested() && demux.onCloseRequest())) { while(!(Display.isCloseRequested() && demux.onCloseRequest())) {
delta = getDelta(); delta = getDelta();
@ -170,9 +167,7 @@ public class DisplayContainer implements ErrorDumpable {
initGL(); initGL();
for (ResolutionChangeListener resolutionChangeListener : resolutionChangeListeners) { eventBus.post(new ResolutionChangedEvent(this.width, this.height));
resolutionChangeListener.onDisplayResolutionChanged(width, height);
}
if (displayMode.getBitsPerPixel() == 16) { if (displayMode.getBitsPerPixel() == 16) {
InternalTextureLoader.get().set16BitMode(); InternalTextureLoader.get().set16BitMode();

View File

@ -18,11 +18,12 @@
package yugecin.opsudance.core.state; package yugecin.opsudance.core.state;
import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.ResolutionChangeListener; import yugecin.opsudance.core.events.EventListener;
import yugecin.opsudance.events.ResolutionChangedEvent;
import java.io.StringWriter; import java.io.StringWriter;
public abstract class BaseOpsuState implements OpsuState, ResolutionChangeListener { public abstract class BaseOpsuState implements OpsuState, EventListener<ResolutionChangedEvent> {
protected final DisplayContainer displayContainer; protected final DisplayContainer displayContainer;
@ -34,14 +35,14 @@ public abstract class BaseOpsuState implements OpsuState, ResolutionChangeListen
public BaseOpsuState(DisplayContainer displayContainer) { public BaseOpsuState(DisplayContainer displayContainer) {
this.displayContainer = displayContainer; this.displayContainer = displayContainer;
displayContainer.addResolutionChangeListener(this); displayContainer.eventBus.subscribe(ResolutionChangedEvent.class, this);
} }
protected void revalidate() { protected void revalidate() {
} }
@Override @Override
public void onDisplayResolutionChanged(int width, int height) { public void onEvent(ResolutionChangedEvent event) {
if (isCurrentState) { if (isCurrentState) {
revalidate(); revalidate();
return; return;

View File

@ -15,10 +15,16 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>. * along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/ */
package yugecin.opsudance.core; package yugecin.opsudance.events;
public interface ResolutionChangeListener { public class ResolutionChangedEvent {
void onDisplayResolutionChanged(int width, int height); public final int width;
public final int height;
public ResolutionChangedEvent(int width, int height) {
this.width = width;
this.height = height;
}
} }

View File

@ -21,6 +21,7 @@ import com.google.inject.AbstractModule;
import yugecin.opsudance.PreStartupInitializer; import yugecin.opsudance.PreStartupInitializer;
import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.Demux; import yugecin.opsudance.core.Demux;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.state.transitions.EmptyTransitionState; import yugecin.opsudance.core.state.transitions.EmptyTransitionState;
import yugecin.opsudance.errorhandling.ErrorHandler; import yugecin.opsudance.errorhandling.ErrorHandler;
import yugecin.opsudance.states.EmptyRedState; import yugecin.opsudance.states.EmptyRedState;
@ -31,6 +32,8 @@ import yugecin.opsudance.core.state.transitions.FadeOutTransitionState;
public class OpsuDanceModule extends AbstractModule { public class OpsuDanceModule extends AbstractModule {
protected void configure() { protected void configure() {
bind(EventBus.class).asEagerSingleton();
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(Demux.class).asEagerSingleton();