Skip to content

Commit

Permalink
pass screen size in instead of polling it later
Browse files Browse the repository at this point in the history
and add some comments
  • Loading branch information
Bigpet committed Jun 8, 2015
1 parent fde0836 commit 7bcd161
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 116 deletions.
23 changes: 0 additions & 23 deletions src/itdelatrisu/opsu/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,6 @@ private enum Resolution {
/** The current skin. */
private static Skin skin;

/** Resolution that the latest Container passed to "setDisplayMode" was set to. */
private static Resolution lastSetResolution = Resolution.RES_1024_768;

/** Frame limiters. */
private static final int[] targetFPS = { 60, 120, 240 };

Expand Down Expand Up @@ -597,8 +594,6 @@ public static void setDisplayMode(Container app) {
// set borderless window if dimensions match screen size
boolean borderless = (screenWidth == resolution.getWidth() && screenHeight == resolution.getHeight());
System.setProperty("org.lwjgl.opengl.Window.undecorated", Boolean.toString(borderless));

lastSetResolution = resolution;
}

// /**
Expand Down Expand Up @@ -893,24 +888,6 @@ public static File getReplayDir() {
return replayDir;
}

/**
* Returns the horizontal Resolution.
* If no game has been started then a default value is returned.
* @return the horizontal resolution of the latest game instance to be started.
*/
public static int getLatestResolutionWidth() {
return lastSetResolution.getWidth();
}

/**
* Returns the vertical Resolution.
* If no game has been started then a default value is returned.
* @return the vertical resolution of the latest game instance to be started.
*/
public static int getLatestResolutionHeight() {
return lastSetResolution.getHeight();
}

/**
* Returns the current skin directory.
* If invalid, this will create a "Skins" folder in the root directory.
Expand Down
8 changes: 0 additions & 8 deletions src/itdelatrisu/opsu/objects/Slider.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,12 @@
import itdelatrisu.opsu.objects.curves.CircumscribedCircle;
import itdelatrisu.opsu.objects.curves.Curve;
import itdelatrisu.opsu.objects.curves.LinearBezier;
import itdelatrisu.opsu.render.Rendertarget;
import itdelatrisu.opsu.states.Game;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;
import org.lwjgl.opengl.GL30;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.util.Log;

/**
* Data type representing a slider object.
Expand Down
50 changes: 32 additions & 18 deletions src/itdelatrisu/opsu/objects/curves/Curve.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public abstract class Curve {
/** Points generated along the curve should be spaced this far apart. */
protected static float CURVE_POINTS_SEPERATION = 5;

/** The width and height of the display container this curve gets drawn into */
protected static int containerWidth = 0, containerHeight = 0;

/** The associated HitObject. */
protected HitObject hitObject;

Expand Down Expand Up @@ -70,6 +73,19 @@ protected Curve(HitObject hitObject, Color color) {
this.renderState = null;
}

/**
* Set the width and height of the container that Curves get drawn into
* Should be called before any curves are drawn.
* @param width
* @param height
*/
public static void init(int width, int height, float circleSize)
{
containerWidth = width;
containerHeight = height;
CurveRenderState.init(width, height, circleSize);
}

/**
* Returns the point on the curve at a value t.
* @param t the t value [0, 1]
Expand All @@ -82,26 +98,24 @@ protected Curve(HitObject hitObject, Color color) {
* @param color the color filter
*/
public void draw(Color color) {
if ( curve == null){
Log.error("draw curve"+this);
return;
if (curve == null) {
return;
}
if (Options.GameOption.NEW_SLIDER.getBooleanValue()) {
if (renderState == null) {
renderState = new CurveRenderState(hitObject);
}
renderState.draw(color, curve);
} else {
Image hitCircle = GameImage.HITCIRCLE.getImage();
Image hitCircleOverlay = GameImage.HITCIRCLE_OVERLAY.getImage();
for (int i = 0; i < curve.length; i++) {
hitCircleOverlay.drawCentered(curve[i].x, curve[i].y, Utils.COLOR_WHITE_FADE);
}
if (Options.GameOption.NEW_SLIDER.getBooleanValue()) {
if(renderState == null)
{
renderState = new CurveRenderState(scale,hitObject);
}
renderState.draw(color,curve);
} else {
Image hitCircle = GameImage.HITCIRCLE.getImage();
Image hitCircleOverlay = GameImage.HITCIRCLE_OVERLAY.getImage();
for (int i = 0; i < curve.length; i++) {
hitCircleOverlay.drawCentered(curve[i].x, curve[i].y, Utils.COLOR_WHITE_FADE);
}
for (int i = 0; i < curve.length; i++){
hitCircle.drawCentered(curve[i].x, curve[i].y, color);
}
for (int i = 0; i < curve.length; i++) {
hitCircle.drawCentered(curve[i].x, curve[i].y, color);
}
}
}

/**
Expand Down
132 changes: 76 additions & 56 deletions src/itdelatrisu/opsu/render/CurveRenderState.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,54 @@
*/
public class CurveRenderState {

/** The width and height of the display container this curve gets drawn into */
protected static int containerWidth, containerHeight;

/** cached drawn slider, only used if new style sliders are activated */
public Rendertarget fbo;

/** thickness of the curve */
float scale;
protected static int scale;

/** The HitObject associated with the curve to be drawn */
HitObject hitObject;

/** Static state that's needed to draw the new style curves */
private static final NewCurveStyleState staticState = new NewCurveStyleState();

public CurveRenderState(float scale, HitObject hitObject) {

/**
* Set the width and height of the container that Curves get drawn into
* Should be called before any curves are drawn.
* @param width
* @param height
*/
public static void init(int width, int height, float circleSize)
{
containerWidth = width;
containerHeight = height;
//equivalent to what happens in Slider.init
scale = (int) (104 - (circleSize * 8));
scale = (int) (scale * HitObject.getXMultiplier()); // convert from Osupixels (640x480)
FrameBufferCache.init(width, height);
}

/**
* Creates an object to hold the render state that's necessary to draw a curve.
* @param hitObject the HitObject that represents this curve, just used as a
* unique ID
*/
public CurveRenderState(HitObject hitObject) {
fbo = null;
this.scale = scale;
this.hitObject = hitObject;
}

/**
* Draw a curve to the screen that's tinted with `color`. The first time
* this is called this caches the image result of the curve and on subsequent
* runs it just draws the cached copy to the screen.
* @param color tint of the curve
* @param curve the points along the curve to be drawn
*/
public void draw(Color color, Vec2f[] curve) {
float alpha = color.a;
//if this curve hasn't been drawn, draw it and cache the result
Expand Down Expand Up @@ -108,6 +138,11 @@ public void discardCache() {
FrameBufferCache.getInstance().freeMappingFor(hitObject);
}

/**
* Just a structure to hold all the important OpenGL state that needs to be
* changed to draw the curve. This is used to backup and restore the state
* so that the code outside of this (mainly Slick2D) doesn't break
*/
private class RenderState {
boolean smoothedPoly;
boolean blendEnabled;
Expand All @@ -119,6 +154,11 @@ private class RenderState {
int oldArrayBuffer;
}

/**
* Backup the current state of the relevant OpenGL state and change it to
* what's needed to draw the curve
* @return
*/
private RenderState startRender() {
RenderState state = new RenderState();
state.smoothedPoly = GL11.glGetBoolean(GL11.GL_POLYGON_SMOOTH);
Expand Down Expand Up @@ -154,6 +194,10 @@ private RenderState startRender() {
return state;
}

/**
* Restore the old OpenGL state that's backed up in {@code state}
* @param state the old state to restore
*/
private void endRender(RenderState state) {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
Expand Down Expand Up @@ -186,12 +230,12 @@ private void endRender(RenderState state) {
* @param curve the points along the curve
*/
private void draw_curve(Color color, Vec2f[] curve) {
staticState.initGradient();
RenderState state = startRender();
int vtx_buf;
//floatsize * (position + texture coordinates) * (number of cones) * (vertices in a cone)
//The size is: floatsize * (position + texture coordinates) * (number of cones) * (vertices in a cone)
FloatBuffer buff = BufferUtils.createByteBuffer(4 * (4 + 2) * (2 * curve.length - 1) * (NewCurveStyleState.DIVIDES + 2)).asFloatBuffer();
staticState.initShaderProgram();
staticState.initGradient();
vtx_buf = GL15.glGenBuffers();
for (int i = 0; i < curve.length; ++i) {
float x = curve[i].x;
Expand All @@ -211,7 +255,20 @@ private void draw_curve(Color color, Vec2f[] curve) {
buff.flip();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vtx_buf);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buff, GL15.GL_STATIC_DRAW);
staticState.render(color, curve.length * 2 - 1, vtx_buf);
GL20.glUseProgram(staticState.program);
GL20.glEnableVertexAttribArray(staticState.attribLoc);
GL20.glEnableVertexAttribArray(staticState.texCoordLoc);
GL20.glUniform1i(staticState.texLoc, 0);
//stride is 6*4 for the floats (4 bytes) (u,v)(x,y,z,w)
//2*4 is for skipping the first 2 floats (u,v)
GL20.glVertexAttribPointer(staticState.attribLoc, 4, GL11.GL_FLOAT, false, 6 * 4, 2 * 4);
GL20.glVertexAttribPointer(staticState.texCoordLoc, 2, GL11.GL_FLOAT, false, 6 * 4, 0);
GL20.glUniform3f(staticState.colLoc, color.r, color.g, color.b);
for (int i = 0; i < curve.length*2-1; ++i) {
GL11.glDrawArrays(GL11.GL_TRIANGLE_FAN, i * (NewCurveStyleState.DIVIDES + 2), NewCurveStyleState.DIVIDES + 2);
}
GL20.glDisableVertexAttribArray(staticState.texCoordLoc);
GL20.glDisableVertexAttribArray(staticState.attribLoc);
GL15.glDeleteBuffers(vtx_buf);
endRender(state);
}
Expand All @@ -226,8 +283,8 @@ private void draw_curve(Color color, Vec2f[] curve) {
* @param DIVIDES the base of the cone is a regular polygon with this many sides
*/
protected void fillCone(FloatBuffer buff, float x1, float y1, final int DIVIDES) {
float divx = Options.getLatestResolutionWidth() / 2.0f;
float divy = Options.getLatestResolutionHeight() / 2.0f;
float divx = containerWidth / 2.0f;
float divy = containerHeight / 2.0f;
float offx = -1.0f;
float offy = 1.0f;
float x, y;
Expand Down Expand Up @@ -271,7 +328,7 @@ protected void fillCone(FloatBuffer buff, float x1, float y1, final int DIVIDES)
* Contains all the necessary state that needs to be tracked to draw curves
* in the new style and not re-create the shader each time.
*
* @author Bigpet {@literal <dravorek@gmail.com>}
* @author Bigpet {@literal <dravorek (at) gmail.com>}
*/
private static class NewCurveStyleState {

Expand All @@ -281,29 +338,22 @@ private static class NewCurveStyleState {
*/
protected static final int DIVIDES = 30;

/**
* OpenGL shader program ID used to draw and recolor the curve
*/
/** OpenGL shader program ID used to draw and recolor the curve */
protected int program = 0;

/**
* OpenGL shader attribute location of the vertex position attribute
*/
/** OpenGL shader attribute location of the vertex position attribute */
protected int attribLoc = 0;

/**
* OpenGL shader attribute location of the texture coordinate attribute
*/
/** OpenGL shader attribute location of the texture coordinate attribute */
protected int texCoordLoc = 0;

/**
* OpenGL shader uniform location of the color attribute
*/
/** OpenGL shader uniform location of the color attribute */
protected int colLoc = 0;

/**
* OpenGL texture id for the gradient texture for the curve
*/
/** OpenGL shader uniform location of the texture sampler attribute */
protected int texLoc = 0;

/** OpenGL texture id for the gradient texture for the curve */
protected int gradientTexture = 0;

/**
Expand All @@ -324,7 +374,6 @@ public void initGradient()
buff.put((byte)(255*col.g));
buff.put((byte)(255*col.b));
buff.put((byte)(255*col.a));

}
buff.flip();
GL11.glBindTexture(GL11.GL_TEXTURE_1D, gradientTexture);
Expand Down Expand Up @@ -391,38 +440,9 @@ public void initShaderProgram() {
}
attribLoc = GL20.glGetAttribLocation(program, "in_position");
texCoordLoc = GL20.glGetAttribLocation(program, "in_tex_coord");
int texLoc = GL20.glGetUniformLocation(program, "tex");
texLoc = GL20.glGetUniformLocation(program, "tex");
colLoc = GL20.glGetUniformLocation(program, "col_tint");
GL20.glUniform1i(texLoc, 0);
}
}

/**
* Make the drawcalls for the curve. This requires the {@code vertexBufferObject} to be filled with
* {@code numCones * (NewCurveStyleState.DIVIDES+2)} sets of (u,v,x,y,z,w) floats
* @param color the color of the curve to be drawn
* @param numCones the number of points in the {code vertexBufferObject}
* @param vertexBufferObject the OpenGL vertex buffer object ID that
* contains the positions and texture coordinates of the cones
*/
public void render(Color color, int numCones, int vertexBufferObject) {
GL20.glUseProgram(program);
GL20.glEnableVertexAttribArray(attribLoc);
GL20.glEnableVertexAttribArray(texCoordLoc);
//stride is 6*4 for the floats (4 bytes) (u,v)(x,y,z,w)
//2*4 is for skipping the first 2 floats (u,v)
GL20.glVertexAttribPointer(attribLoc, 4, GL11.GL_FLOAT, false, 6 * 4, 2 * 4);
GL20.glVertexAttribPointer(texCoordLoc, 2, GL11.GL_FLOAT, false, 6 * 4, 0);

GL20.glUniform3f(colLoc, color.r, color.g, color.b);
for (int i = 0; i < numCones; ++i) {
GL11.glDrawArrays(GL11.GL_TRIANGLE_FAN, i * (NewCurveStyleState.DIVIDES + 2), NewCurveStyleState.DIVIDES + 2);
}

GL20.glDisableVertexAttribArray(texCoordLoc);
GL20.glDisableVertexAttribArray(attribLoc);
}

}

}
Loading

0 comments on commit 7bcd161

Please sign in to comment.