Add Game Engine Main File
This commit is contained in:
parent
929fcb19f2
commit
569f2dfb00
39 changed files with 2503 additions and 29 deletions
36
src/globalgamejam/render/Camera.java
Normal file
36
src/globalgamejam/render/Camera.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
package globalgamejam.render;
|
||||
import static org.lwjgl.glfw.GLFW.*;
|
||||
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import globalgamejam.*;
|
||||
import globalgamejam.input.*;
|
||||
import globalgamejam.math.*;
|
||||
|
||||
/**
|
||||
* Class created by MrDev023 (Florian RICHER) on 14/01/2017
|
||||
*/
|
||||
public class Camera {
|
||||
|
||||
public static Matrix4f matrix = new Matrix4f();
|
||||
public static final float SPEED = 1.0f;//Speed de base
|
||||
|
||||
|
||||
|
||||
public static float rot = 0.0f;//rotation de la camera
|
||||
public static Vector2f pos = new Vector2f();
|
||||
|
||||
public static void update(){
|
||||
float speed = SPEED * Main.delta;//speed reel par frame en fonction des fps
|
||||
//class Input pour tous ce qui est entrer et sortis
|
||||
|
||||
}
|
||||
|
||||
public static void transform(){
|
||||
matrix.loadIdentity();
|
||||
matrix.rotate(new Quaternion(new Vector3f(0,0,1),rot));
|
||||
matrix.tranlate(-pos.x, -pos.y, 0);
|
||||
}
|
||||
|
||||
}
|
46
src/globalgamejam/render/DisplayManager.java
Normal file
46
src/globalgamejam/render/DisplayManager.java
Normal file
|
@ -0,0 +1,46 @@
|
|||
package globalgamejam.render;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
|
||||
import globalgamejam.*;
|
||||
import globalgamejam.math.*;
|
||||
|
||||
/**
|
||||
* Class created by MrDev023 (Florian RICHER) on 14/01/2017
|
||||
*/
|
||||
public class DisplayManager {
|
||||
|
||||
public static Matrix4f projection = new Matrix4f();
|
||||
|
||||
public static void clear(){
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
public static void preRender2D(){
|
||||
projection.loadIdentity();
|
||||
projection.Ortho2D(0, Main.WIDTH, 0, Main.HEIGHT, -1, 1);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LESS);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
public static void preRenderGUI(){
|
||||
projection.loadIdentity();
|
||||
//Permet de centrer la camera au centre de l'ecran
|
||||
projection.Ortho2D(-Main.WIDTH/2.0f, Main.WIDTH/2.0f, -Main.HEIGHT/2.0f, Main.HEIGHT/2.0f, -1, 1);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LESS);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
public static void render2D(){
|
||||
Main.game.render2D();
|
||||
}
|
||||
|
||||
public static void renderGUI(){
|
||||
Main.game.renderGUI();
|
||||
}
|
||||
|
||||
}
|
89
src/globalgamejam/render/Shaders.java
Normal file
89
src/globalgamejam/render/Shaders.java
Normal file
|
@ -0,0 +1,89 @@
|
|||
package globalgamejam.render;
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
|
||||
import globalgamejam.input.*;
|
||||
import globalgamejam.math.*;
|
||||
|
||||
/**
|
||||
* Class created by MrDev023 (Florian RICHER) on 14/01/2017
|
||||
*/
|
||||
public class Shaders {
|
||||
|
||||
public int program;
|
||||
|
||||
public Shaders(String vertexFile,String fragmentFile) throws Exception{
|
||||
String fragmentShader = IO.loadFile(fragmentFile);
|
||||
String vertexShader = IO.loadFile(vertexFile);
|
||||
|
||||
if(program != -1)glDeleteProgram(program);
|
||||
program = glCreateProgram();
|
||||
int vert = glCreateShader(GL_VERTEX_SHADER);
|
||||
int frag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(vert, vertexShader);
|
||||
glShaderSource(frag, fragmentShader);
|
||||
glCompileShader(vert);
|
||||
if (glGetShaderi(vert, GL_COMPILE_STATUS) == GL_FALSE) {
|
||||
System.err.println(glGetShaderInfoLog(vert, 2048));
|
||||
System.exit(1);
|
||||
}else{
|
||||
System.out.println("Vertex compiled !");
|
||||
}
|
||||
glCompileShader(frag);
|
||||
if (glGetShaderi(frag, GL_COMPILE_STATUS) == GL_FALSE) {
|
||||
System.err.println(glGetShaderInfoLog(frag, 2048));
|
||||
System.exit(1);
|
||||
}else{
|
||||
System.out.println("Fragment compiled !");
|
||||
}
|
||||
glAttachShader(program, vert);
|
||||
glAttachShader(program, frag);
|
||||
glLinkProgram(program);
|
||||
glValidateProgram(program);
|
||||
glDeleteShader(frag);
|
||||
glDeleteShader(vert);
|
||||
}
|
||||
|
||||
public void bind(){
|
||||
glUseProgram(program);
|
||||
}
|
||||
|
||||
public void unbind(){
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
public int getAttribLocation(String name){
|
||||
return glGetAttribLocation(program, name);
|
||||
}
|
||||
|
||||
public void destroy(){
|
||||
if(program == 0)return;
|
||||
if(glIsProgram(program))unbind();
|
||||
glDeleteProgram(program);
|
||||
}
|
||||
|
||||
public void uniform(String name,float v){
|
||||
glUniform1f(glGetUniformLocation(program, name), v);
|
||||
}
|
||||
|
||||
public void uniform(String name,Vector3f vec){
|
||||
glUniform3f(glGetUniformLocation(program, name), vec.x,vec.y,vec.z);
|
||||
}
|
||||
|
||||
public void uniform(String name,Vector4f vec){
|
||||
glUniform4f(glGetUniformLocation(program, name), vec.x,vec.y,vec.z,vec.w);
|
||||
}
|
||||
|
||||
public void uniform(String name,Matrix4f mat){
|
||||
glUniformMatrix4fv(glGetUniformLocation(program, name),true, mat.getBuffer());
|
||||
}
|
||||
|
||||
public void uniform(String name, Color4f v) {
|
||||
glUniform4f(glGetUniformLocation(program, name), v.getR(),v.getG(),v.getB(),v.getA());
|
||||
}
|
||||
|
||||
public void uniform(String name,int v){
|
||||
glUniform1i(glGetUniformLocation(program,name), v);
|
||||
}
|
||||
|
||||
}
|
96
src/globalgamejam/render/Texture.java
Normal file
96
src/globalgamejam/render/Texture.java
Normal file
|
@ -0,0 +1,96 @@
|
|||
package globalgamejam.render;
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.opengl.GL12.*;
|
||||
|
||||
import java.awt.image.*;
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
|
||||
import javax.imageio.*;
|
||||
|
||||
import org.lwjgl.*;
|
||||
|
||||
/**
|
||||
* Class created by MrDev023 (Florian RICHER) on 14/01/2017
|
||||
*/
|
||||
public class Texture {
|
||||
|
||||
int width, height;
|
||||
int id;
|
||||
|
||||
public Texture(int width,int height,int id){
|
||||
this.id = id;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public static Texture loadTexture(String path){
|
||||
try {
|
||||
BufferedImage image = ImageIO.read(new File(path));
|
||||
int width = image.getWidth();
|
||||
int height = image.getHeight();
|
||||
int[] pixels = new int[width * height];
|
||||
|
||||
image.getRGB(0, 0, width, height, pixels, 0,width);
|
||||
|
||||
int[] data = new int[pixels.length];
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
int a = (pixels[i] & 0xff000000) >> 24;
|
||||
int r = (pixels[i] & 0xff0000) >> 16;
|
||||
int g = (pixels[i] & 0xff00) >> 8;
|
||||
int b = (pixels[i] & 0xff);
|
||||
|
||||
data[i] = a << 24 | b << 16 | g << 8 | r;
|
||||
}
|
||||
|
||||
IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
|
||||
buffer.put(data);
|
||||
buffer.flip();
|
||||
|
||||
int id = glGenTextures();
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
System.out.println("Texture loaded ! " + width + "x" + height + " id:" + id);
|
||||
|
||||
return new Texture(width, height, id);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public int getID(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public void bind(){
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
}
|
||||
|
||||
public void unbind(){
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
public void destroy(){
|
||||
glDeleteTextures(id);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue