Skip to content

Commit

Permalink
YES, IT'S OPTIMIZED!!!!! 60 FPS SMOOTH!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Mythical-Atlas committed May 14, 2020
1 parent cb14a97 commit e6d5be9
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 23 deletions.
Binary file modified Sonic Fan Game/bin/objects/Player.class
Binary file not shown.
Binary file modified Sonic Fan Game/bin/objects/Ring.class
Binary file not shown.
Binary file modified Sonic Fan Game/bin/objects/Spring.class
Binary file not shown.
2 changes: 1 addition & 1 deletion Sonic Fan Game/src/main/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void loop() {
// if the time since the start if the last frame is 1/60 of a second, update

while(!glfwWindowShouldClose(glfwWindow)) {
if(Time.getTime() - beginTime >= 1.0f / 30.0f || first) {
if(Time.getTime() - beginTime >= 1.0f / 60.0f || first) {
dt = Time.getTime() - beginTime;
beginTime = Time.getTime();

Expand Down
2 changes: 1 addition & 1 deletion Sonic Fan Game/src/objects/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ else if(crouching0) {

public void draw(float dt, Shader shader, Camera camera) {
manageAnimations(dt);
manageAnimations(dt); // 30fps only
//manageAnimations(dt); // 30fps only

/*if(DRAW_MASKS) {
Shape temp;
Expand Down
2 changes: 1 addition & 1 deletion Sonic Fan Game/src/objects/Ring.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void draw(int scaleX, int scaleY, float dt, Shader shader, Camera camera)

anim.draw(pos.x, pos.y, scaleX, scaleY, shader, camera);
anim.update(1/*dt / (1.0f / 60.0f)*/);
anim.update(1/*dt / (1.0f / 60.0f)*/); // 30fps only
//anim.update(1/*dt / (1.0f / 60.0f)*/); // 30fps only

if(destroy == 2 && anim.finished) {destroy = 3;}
}
Expand Down
2 changes: 1 addition & 1 deletion Sonic Fan Game/src/objects/Spring.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void draw(int scaleX, int scaleY, float dt, Shader shader, Camera camera)
else {
anim.draw(pos.x, pos.y, scaleX, scaleY, shader, camera);
anim.update(1/*dt / (1.0f / 60.0f)*/);
anim.update(1/*dt / (1.0f / 60.0f)*/); // 30fps only
//anim.update(1/*dt / (1.0f / 60.0f)*/); // 30fps only

if(anim.finished) {
anim.reset();
Expand Down
30 changes: 24 additions & 6 deletions Sonic Fan Game/src/rendering/SpriteRenderBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import static org.lwjgl.opengl.GL33.*;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.system.MemoryUtil;

public class SpriteRenderBatch {
private static final int POSITIONS_SIZE = 3;
Expand All @@ -20,7 +22,7 @@ public class SpriteRenderBatch {
private static final int MAX_TEXTURES = 32;

private Texture[] textures;
private float[] vertices;
private ByteBuffer vertices;
private int[] elements;
private int[] texSlots;

Expand All @@ -34,8 +36,8 @@ public class SpriteRenderBatch {

public SpriteRenderBatch() {
textures = null;
vertices = new float[MAX_ARRAY_SIZE];
elements = new int[vertices.length / 4 * 6];
vertices = MemoryUtil.memAlloc(MAX_ARRAY_SIZE * Float.BYTES);
elements = new int[MAX_ARRAY_SIZE / 4 * 6];

for(int i = 0; i < elements.length; i += 6) {
elements[i + 0] = i * 4 + 2;
Expand Down Expand Up @@ -75,15 +77,16 @@ public boolean add(Image image) {
loadVertices(image.getPositions2(), v * POSITIONS_SIZE, POSITIONS_SIZE);
loadVertices(image.getColors(), v * COLORS_SIZE, COLORS_SIZE);
loadVertices(image.getUVMaps(), v * UV_MAPS_SIZE, UV_MAPS_SIZE);
vertices[index++] = texID;
vertices.putFloat(texID);
index++;
}
}
return(true);
}
else {return(false);}
}

private void loadVertices(float[] items, int start, int length) {for(int i = start; i < start + length; i++) {vertices[index++] = items[i];}}
private void loadVertices(float[] items, int start, int length) {for(int i = start; i < start + length; i++, index++) {vertices.putFloat(items[i]);}}

public void load() {
vaoID = glGenVertexArrays();
Expand All @@ -108,11 +111,24 @@ public void load() {

glVertexAttribPointer(3, TEX_ID_SIZE, GL_FLOAT, false, VERTEX_SIZE * Float.BYTES, (POSITIONS_SIZE + COLORS_SIZE + UV_MAPS_SIZE) * Float.BYTES);
glEnableVertexAttribArray(3);

//vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY, vertices.capacity(), vertices);
}

public void reset() {
textures = null;
vertices.clear();
textureIndex = 0;
spriteIndex = 0;
index = 0;
}

public void draw(Shader shader, Camera camera) {
if(spriteIndex == 0) {return;}

//glUnmapBuffer(GL_ARRAY_BUFFER);

vertices.flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices);

Expand All @@ -134,7 +150,7 @@ public void draw(Shader shader, Camera camera) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glDrawElements(GL_TRIANGLES, spriteIndex * 6, GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, 0, index / 10);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
Expand All @@ -143,5 +159,7 @@ public void draw(Shader shader, Camera camera) {
for(int i = 0; i < textures.length; i++) {textures[i].unbind();}

shader.detach();

glUnmapBuffer(GL_ARRAY_BUFFER);
}
}
41 changes: 32 additions & 9 deletions Sonic Fan Game/src/rendering/SpriteRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,50 @@ public class SpriteRenderer {

private SpriteRenderBatch[] batches;

private SpriteRenderer() {batches = null;}
private int batchIndex;

private SpriteRenderer() {
batches = null;
batchIndex = 0;
}

public static SpriteRenderer get() {
if(singleton == null) {singleton = new SpriteRenderer();}
return(singleton);
}

public static void reset() {get().batches = null;}
public static void reset() {if(get().batches != null) {
for(int i = 0; i < get().batches.length; i++) {get().batches[i].reset();}}
get().batchIndex = 0;
}

public static void add(Image image) {
/*if(get().batches == null) {System.out.println("batches = 0");}
else {System.out.println("batches = " + get().batches.length);}
System.out.println("batch index = " + get().batchIndex);*/

if(get().batches == null) {
get().batches = append(get().batches, new SpriteRenderBatch());
get().batches[get().batches.length - 1].load();
get().batches[get().batchIndex].load();
}

boolean success = get().batches[get().batches.length - 1].add(image);
if(!success) {
get().batches = append(get().batches, new SpriteRenderBatch());
get().batches[get().batches.length - 1].load();
get().batches[get().batches.length - 1].add(image);
else {
boolean success = get().batches[get().batchIndex].add(image);
if(!success) {
if(++get().batchIndex == get().batches.length) {
get().batches = append(get().batches, new SpriteRenderBatch());
get().batches[get().batchIndex].load();
}
//else {get().batches[get().batchIndex].reset();}
}
}

/*if(get().batches == null) {System.out.println("batches = 0");}
else {System.out.println("batches = " + get().batches.length);}
System.out.println("batch index = " + get().batchIndex);*/

get().batches[get().batchIndex].add(image);

System.out.println();
}

public static void draw(Shader shader, Camera camera) {if(get().batches != null) {for(int i = 0; i < get().batches.length; i++) {get().batches[i].draw(shader, camera);}}}
Expand Down
11 changes: 7 additions & 4 deletions Sonic Fan Game/src/scenes/MainScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ public void update(float dt) {
moveCamera(dt);

// 30fps only
player.update(dt, layer0, layer1, layer2, layer1Triggers, layer2Triggers, platforms, rings, springs);
/*player.update(dt, layer0, layer1, layer2, layer1Triggers, layer2Triggers, platforms, rings, springs);
if(rings != null) {
int[] removals = null;
for(int i = 0; i < rings.length; i++) {if(rings[i].destroy == 3) {removals = append(removals, i);}}
if(removals != null) {for(int i = 0; i < removals.length; i++) {rings = removeIndex(rings, removals[i]);}}
}
moveCamera(dt);
moveCamera(dt);*/
// 30fps only

/*Loader.leafBG.draw(new int[]{200, 100, 50}, player, graphics);*/
Expand All @@ -170,11 +170,14 @@ public void update(float dt) {
if(springs != null) {for(int i = 0; i < springs.length; i++) {springs[i].draw(SCALE, SCALE, dt, defaultShader, camera);}}
if(rings != null) {for(int i = 0; i < rings.length; i++) {rings[i].draw(SCALE, SCALE, dt, defaultShader, camera);}}
player.draw(dt, defaultShader, camera);
hud.draw(dt, player, defaultShader, camera);

SpriteRenderer.draw(spriteShader, camera);
//SpriteRenderer.draw(spriteShader, camera);

if(!showTileMasks) {leafForest1Map.draw(2, SCALE, SCALE, defaultShader, camera);}

//SpriteRenderer.reset();
hud.draw(dt, player, defaultShader, camera);
SpriteRenderer.draw(spriteShader, camera);
}

public void checkKeysPressed() {
Expand Down

0 comments on commit e6d5be9

Please sign in to comment.