@@ -2,50 +2,32 @@

import no.grabit.NCLauncher.graphics.Shader;
import no.grabit.NCLauncher.graphics.Texture;
import no.grabit.NCLauncher.scenegraph.GameComponent;
import no.grabit.NCLauncher.scenegraph.GameObject;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.util.ResourceLoader;

import java.awt.Font;
import java.awt.Color;
import java.awt.*;
import java.nio.FloatBuffer;

/**
* Created by Ole on 26/05/2015.
*/
public class Label extends GameObject {

public static Font medievalFont;
private static boolean initialized;
public static final int POSITION_CENTERED = 0;
public static final int POSITION_CENTERED_MIDDLE_LEFT_TO_RIGHT = 1;

public static void init() {
if (initialized)
return;
initialized = true;

try {
medievalFont = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream("fonts/JimNightshade-Regular.ttf"));
// medievalFont = new java.awt.Font("Times New Roman", Font.PLAIN, 24);
} catch (Exception e) {
e.printStackTrace();
}
}

public static Font medievalFont;
private static boolean initialized;
protected String text;
private float size;
protected Font actualFont;
protected UnicodeFont font;
protected int positioning = POSITION_CENTERED;

private float size;
public Label(String tag, Font font, String text, float size, int positioningMode) {
super("label." + tag);
this.actualFont = font;
@@ -57,6 +39,19 @@ public Label(String tag, Font font, String text, float size, int positioningMode
getTransform().getScale().set(0.001f, -0.001f);
}

public static void init() {
if (initialized)
return;
initialized = true;

try {
medievalFont = Font.createFont(Font.TRUETYPE_FONT, ResourceLoader.getResourceAsStream("fonts/JimNightshade-Regular.ttf"));
// medievalFont = new java.awt.Font("Times New Roman", Font.PLAIN, 24);
} catch (Exception e) {
e.printStackTrace();
}
}

private void updateFont() {
actualFont = actualFont.deriveFont(size);
font = new UnicodeFont(actualFont);
@@ -1,14 +1,14 @@
package no.grabit.NCLauncher.main;

import no.grabit.NCLauncher.graphics.Shader;
import no.grabit.NCLauncher.input.Button;
import no.grabit.NCLauncher.input.InputLabel;
import no.grabit.NCLauncher.input.Label;
import no.grabit.NCLauncher.graphics.Shader;
import no.grabit.NCLauncher.scenegraph.GameObject;
import no.grabit.NCLauncher.util.Time;
import org.lwjgl.input.*;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector4f;
@@ -24,13 +24,59 @@ public class Launcher implements Runnable {

private GameObject root;

public static void main(String[] args) {
Launcher launcher = new Launcher();
new Thread(launcher, "main loop").start();
}

private static void init() {
initDisplay();
initGL();
}

private static void initGL() {
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
glOrtho(-((float) Display.getWidth() / (float) Display.getHeight()), ((float) Display.getWidth() / (float) Display.getHeight()), -1, 1, 1, -1);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.15f, 0.3f, 0.8f, 1.0f);
// glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}

private static void initDisplay() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
Display.setResizable(true);
Display.setTitle(Launcher.TITLE);
Display.setVSyncEnabled(true);
// Mouse.setNativeCursor(new Cursor(1, 1, 0, 0, 1, BufferUtils.createIntBuffer(1), null));
} catch (Exception e) {
e.printStackTrace();
System.exit(-69);
}
}

public static float getMouseX() {
return ((float) Mouse.getX() - Display.getWidth() / 2) / (float) Display.getHeight() * 2;
}

public static float getMouseY() {
return ((float) Mouse.getY() - Display.getHeight() / 2) / (float) Display.getHeight() * 2;
}

public void run() {
init();
initGame();

long lastTime = System.nanoTime();

while(!Display.isCloseRequested()) {
while (!Display.isCloseRequested()) {
long now = System.nanoTime();
Time.setDeltaTime(now, lastTime);
Time.addTime(Time.deltaTime());
@@ -57,29 +103,19 @@ private void render() {
Shader.unbind();
}

public static void main(String[] args) {
Launcher launcher = new Launcher();
new Thread(launcher, "main loop").start();
}

private void handleDisplayUpdate() {
Display.update();

if(Display.wasResized()) {
if (Display.wasResized()) {
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-((float)Display.getWidth() / (float) Display.getHeight()), ((float)Display.getWidth() / (float) Display.getHeight()), -1, 1, 1, -1);
glOrtho(-((float) Display.getWidth() / (float) Display.getHeight()), ((float) Display.getWidth() / (float) Display.getHeight()), -1, 1, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
Display.sync(Display.getDesktopDisplayMode().getFrequency());
}

private static void init() {
initDisplay();
initGL();
}

private void initGame() {
Label.init();
root = new GameObject("root");
@@ -109,40 +145,4 @@ private void initGame() {
root.add(button);
}

private static void initGL() {
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
glOrtho(-((float)Display.getWidth() / (float) Display.getHeight()), ((float)Display.getWidth() / (float) Display.getHeight()), -1, 1, 1, -1);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.15f, 0.3f, 0.8f, 1.0f);
// glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}

private static void initDisplay() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
Display.setResizable(true);
Display.setTitle(Launcher.TITLE);
Display.setVSyncEnabled(true);
// Mouse.setNativeCursor(new Cursor(1, 1, 0, 0, 1, BufferUtils.createIntBuffer(1), null));
} catch(Exception e) {
e.printStackTrace();
System.exit(-69);
}
}

public static float getMouseX() {
return ((float) Mouse.getX() - Display.getWidth() / 2) / (float) Display.getHeight() * 2;
}

public static float getMouseY() {
return ((float) Mouse.getY() - Display.getHeight() / 2) / (float) Display.getHeight() * 2;
}

}
@@ -10,10 +10,9 @@
*/
public abstract class GameComponent implements Serializable {
private static final long serialVersionUID = 1L;

private final String tag;
private GameObject parent;
private Transform transform;
private final String tag;

public GameComponent(String tag) {
this.tag = tag;
@@ -26,16 +25,16 @@ public GameComponent(String tag) {

public abstract void exit();

public final void setParent(GameObject parent) {
this.parent = parent;
}

public final GameObject getParent() {
return parent;
}

public final void setParent(GameObject parent) {
this.parent = parent;
}

public final GameObject getRoot() {
if(parent == null)
if (parent == null)
return null;

return parent.getRoot();
@@ -48,7 +47,7 @@ public final Transform getTransform() {
public final Matrix4f getModelView() {
Matrix4f modelView = getTransform().getModelView();

if(parent == null)
if (parent == null)
return modelView;
else
return Matrix4f.mul(parent.getModelView(), modelView, null);
@@ -12,16 +12,14 @@
*/
public class GameObject implements Serializable {
private static final long serialVersionUID = 2L;

private final String tag;
/**
* GameObject class.
* parent - the GameObject over this one on the hierarchy.
* */
*/

private GameObject parent;
private Transform transform;
private final String tag;

private List<GameObject> children;
private List<GameComponent> components;

@@ -32,68 +30,71 @@ public GameObject(String tag) {
transform = new Transform();
}

protected void updateObject() {}
protected void updateObject() {
}

protected void renderObject() {}
protected void renderObject() {
}

protected void exitObject() {}
protected void exitObject() {
}

public final void update() {
updateObject();

for(GameComponent component : components) {
for (GameComponent component : components) {
component.update();
}
for(GameObject child : children) {
for (GameObject child : children) {
child.update();
}
}

public final void render() {
renderObject();

for(GameComponent component : components) {
for (GameComponent component : components) {
component.render();
}
for(GameObject child : children) {
for (GameObject child : children) {
child.render();
}
}

public final void exit() {
exitObject();

for(GameComponent component : components) {
for (GameComponent component : components) {
component.exit();
}
for(GameObject child : children) {
for (GameObject child : children) {
child.exit();
}
}

public final void add(GameObject child) {
if(child == null) return;
if (child == null) return;
child.parent = this;
children.add(child);
}

public final void remove(GameObject child) {
if(child == null) return;
if (child == null) return;
child.parent = null;
if(children.contains(child))
if (children.contains(child))
children.remove(child);
}

public final void add(GameComponent component) {
if(component == null) return;
if (component == null) return;
component.setParent(this);
components.add(component);
}

public final void remove(GameComponent component) {
if(component == null) return;
if (component == null) return;
component.setParent(null);
if(components.contains(component))
if (components.contains(component))
components.remove(component);
}

@@ -104,7 +105,7 @@ public final Transform getTransform() {
public final Matrix4f getModelView() {
Matrix4f modelView = getTransform().getModelView();

if(parent == null)
if (parent == null)
return modelView;
else
return Matrix4f.mul(parent.getModelView(), modelView, null);
@@ -116,8 +117,8 @@ public final GameObject getParent() {

public final GameObject getRoot() {
GameObject cur = this;
while(true) {
if(cur.parent == null)
while (true) {
if (cur.parent == null)
break;

cur = cur.parent;
@@ -22,7 +22,7 @@ public static String readFileAsString(String fileName) {

Scanner scanner = new Scanner(file);

while(scanner.hasNextLine()) {
while (scanner.hasNextLine()) {
result += scanner.nextLine() + "\n";
}

@@ -8,16 +8,13 @@ public final class Time {
private static float delta;
private static long time;

private Time() {}
private Time() {
}

public static void addTime(float delta) {
Time.time += delta * 1_000_000_000L;
}

public static void setTime(long time) {
Time.time = time;
}

public static void setDeltaTime(long now, long lastTime) {
Time.delta = (now - lastTime) / 1_000_000_000.0f;
}
@@ -30,6 +27,10 @@ public static long getTime() {
return time;
}

public static void setTime(long time) {
Time.time = time;
}

public static long getSecond() {
return 1_000_000_000L;
}