1
0
Fork 0

Create GameEngine

This commit is contained in:
Florian Richer (MrDev023) 2015-12-24 14:47:01 +01:00
parent 145ac187c9
commit 9505d4e887
10 changed files with 499 additions and 1 deletions

View file

@ -2,5 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/LWJGL3"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>First Game Engine Project</name>
<name>First 2D Game UDP</name>
<comment></comment>
<projects>
</projects>

View file

@ -0,0 +1,11 @@
package mrdev023;
import mrdev023.gameengine.*;
public class Main {
public static void main(String[] args) {
GameEngine.start("2D Game UDP", 800, 800);
}
}

View file

@ -0,0 +1,103 @@
package mrdev023.gameengine;
import mrdev023.gameengine.gamestate.main.*;
import mrdev023.opengl.*;
public class GameEngine {
private static Frame frame;
private static long current,previous,elapsedInfo,elapsedTicks;
private static int countFPS = 0,countTICKS = 0,FPS,TICKS;
private static boolean IsRunning = false;
private static GameState state = GameState.MAIN;
private static String title;
public static void start(String title,int width,int height){
try{
GameEngine.title = title;
frame = new Frame(title, width, height, false, false);
init();
loop();
}catch(Exception e){
e.printStackTrace();
}
}
public static void init(){
current = System.nanoTime();
state.init();
}
public static void loop(){
IsRunning = true;
while(IsRunning){
previous = current;
current = System.nanoTime();
elapsedInfo += current - previous;
elapsedTicks += current - previous;
if(frame.isClosedRequested()){
IsRunning = false;
continue;
}
if(elapsedTicks >= 1000000000/60){
frame.updateEvent();
state.updateKeyboard();
state.updateMouse();
state.update();
countTICKS++;
elapsedTicks -= 1000000000/60;
}else{
DisplayManager.clear();
DisplayManager.preRender2D();
DisplayManager.render2D();
DisplayManager.preRenderGUI();
DisplayManager.renderGUI();
frame.updateDisplay();
countFPS++;
}
if(elapsedInfo >= 1000000000){
FPS = countFPS;
countFPS = 0;
TICKS = countTICKS;
countTICKS = 0;
frame.setTitle(title + " | FPS:" + FPS + " | TICKS:" + TICKS);
elapsedInfo -= 1000000000;
}
}
destroy();
}
public static void destroy(){
state.destroy();
frame.destroy();
}
public static Frame getFrame() {
return frame;
}
public static boolean isRunning() {
return IsRunning;
}
public static void setRunning(boolean isRunning) {
IsRunning = isRunning;
}
public static GameState getGameState() {
return state;
}
public static void setGameState(GameState state) {
GameEngine.state.destroy();
GameEngine.state = state;
GameEngine.state.init();
}
}

View file

@ -0,0 +1,43 @@
package mrdev023.gameengine.gamestate;
import mrdev023.gameengine.gamestate.main.*;
public class MainState implements IGameState{
public void init() {
}
public void destroy() {
}
public void preRender2D() {
}
public void preRenderGUI() {
}
public void renderGUI() {
}
public void render2D() {
}
public void update() {
}
public void updateKeyboard() {
}
public void updateMouse() {
}
}

View file

@ -0,0 +1,51 @@
package mrdev023.gameengine.gamestate.main;
import mrdev023.gameengine.gamestate.*;
public enum GameState {
MAIN(new MainState());
IGameState state;
GameState(IGameState state){
this.state = state;
}
public void init(){
this.state.init();
}
public void destroy(){
this.state.destroy();
}
public void preRender2D(){
this.state.preRender2D();
}
public void preRenderGUI(){
this.state.preRenderGUI();
}
public void renderGUI(){
this.state.renderGUI();
}
public void render2D(){
this.state.render2D();
}
public void update(){
this.state.update();
}
public void updateKeyboard(){
this.state.updateKeyboard();
}
public void updateMouse(){
this.state.updateMouse();
}
}

View file

@ -0,0 +1,15 @@
package mrdev023.gameengine.gamestate.main;
public interface IGameState {
public void init();
public void destroy();
public void preRender2D();
public void preRenderGUI();
public void renderGUI();
public void render2D();
public void update();
public void updateKeyboard();
public void updateMouse();
}

View file

@ -0,0 +1,25 @@
package mrdev023.opengl;
public class DisplayManager {
public static void clear(){
}
public static void preRender2D(){
}
public static void preRenderGUI(){
}
public static void render2D(){
}
public static void renderGUI(){
}
}

View file

@ -0,0 +1,236 @@
package mrdev023.opengl;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import mrdev023.opengl.exception.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.glfw.Callbacks.*;
import org.lwjgl.glfw.GLFWWindowSizeCallback.SAM;
import java.nio.*;
public class Frame {
//Variables static de configuration de la fenetre
//-----------------------------------------------------------------------
public static final int
POSITION_CENTER = -1;
//-----------------------------------------------------------------------
//Variables internes de la fenetre
//-----------------------------------------------------------------------
private GLFWErrorCallback errorCallback;
private boolean resized = false;
private long windowID = 0;
private int width = 0,height = 0;
private String TITLE;
private boolean isResizable;
private int px,py;
private boolean isVSync;
//-----------------------------------------------------------------------
public Frame(String title,int w,int h,boolean isResizable,boolean isVSync,int px,int py) throws FrameException{
//definie la sortie d'erreur
errorCallback = new GLFWErrorCallback() {
public void invoke(int error, long description) {
System.err.println("ID : " + error + " | Description :" + description);
}
};
glfwSetErrorCallback(errorCallback);
//init
if(glfwInit() != GL11.GL_TRUE)throw new FrameException("GLFW not init");
//config de la fenetre
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GL11.GL_FALSE);
setResizable(isResizable);
this.isResizable = isResizable;
//creation de la fenetre
windowID = glfwCreateWindow(w,h,title,NULL,NULL);
this.TITLE = title;
this.width = w;
this.height = h;
if(windowID == NULL)throw new FrameException("Frame not created");
//definie la position de la fenetre
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
if(px == POSITION_CENTER){
this.px = (vidmode.width()-width)/2;
px = this.px;
}
if(py == POSITION_CENTER){
this.py = (vidmode.height()-height)/2;
py = this.py;
}
glfwSetWindowPos(windowID,px,py);
//Creer un context OpenGL
glfwMakeContextCurrent(windowID);
//VSync
if(isVSync){
glfwSwapInterval(1);
}
glfwSetWindowSizeCallback(windowID, new GLFWWindowSizeCallback() {
public void invoke(long window, int w, int h) {
resized=true;
width = w;
height = h;
}
});
glfwShowWindow(windowID);
glfwMakeContextCurrent(windowID);
GL.createCapabilities();
}
public Frame(String title,int w,int h,boolean isResizable,boolean isVSync) throws FrameException{
//definie la sortie d'erreur
errorCallback = new GLFWErrorCallback() {
public void invoke(int error, long description) {
System.err.println("ID : " + error + " | Description :" + description);
}
};
glfwSetErrorCallback(errorCallback);
//init
if(glfwInit() != GL11.GL_TRUE)throw new FrameException("GLFW not init");
//config de la fenetre
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GL11.GL_FALSE);
setResizable(isResizable);
this.isResizable = isResizable;
//creation de la fenetre
windowID = glfwCreateWindow(w,h,title,NULL,NULL);
this.TITLE = title;
this.width = w;
this.height = h;
if(windowID == NULL)throw new FrameException("Frame not created");
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
this.px = (vidmode.width()-width)/2;
this.py = (vidmode.height()-height)/2;
glfwSetWindowPos(windowID,px,py);
//Creer un context OpenGL
glfwMakeContextCurrent(windowID);
//VSync
if(isVSync){
glfwSwapInterval(1);
}
glfwSetWindowSizeCallback(windowID, new GLFWWindowSizeCallback() {
public void invoke(long window, int w, int h) {
resized=true;
width = w;
height = h;
}
});
glfwShowWindow(windowID);
glfwMakeContextCurrent(windowID);
GL.createCapabilities();
}
public void setFramePosition(int px,int py){
//definie la position de la fenetre
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
if(px == POSITION_CENTER){
this.px = (vidmode.width()-width)/2;
}
if(py == POSITION_CENTER){
this.py = (vidmode.height()-height)/2;
}
glfwSetWindowPos(windowID,px,py);
}
public void setResizable(boolean resizable){
if(resizable){
glfwWindowHint(GLFW_RESIZABLE, GL11.GL_TRUE);
isResizable = true;
}else{
glfwWindowHint(GLFW_RESIZABLE, GL11.GL_FALSE);
isResizable = false;
}
}
/**
* @Info Mets à jour les entrées du clavier
*/
public void updateEvent(){
glfwPollEvents();
}
/**
* @Info Mets à jour l'affichage de la fenêtre
*/
public void updateDisplay(){
glfwSwapBuffers(windowID);
}
/**
* @Info ©truit la fenêtre
*/
public void destroy(){
glfwDestroyWindow(windowID);
glfwTerminate();
}
/**
* @return true si la fenetre à é© agrandi ou pas
*/
public boolean wasResized(){
if(resized)return !(resized = !resized);
else return false;
}
/**
* @return index 0 = width et 1 = height
*/
public int[] getWindowSize(){
return new int[]{width,height};
}
/**
* @return width la largeur de l'ecran
*/
public int getWidth(){
return width;
}
/**
* @return height la hauteur de l'ecran
*/
public int getHeight(){
return height;
}
/**
* @return true si la croix rouge a été cliquer
*/
public boolean isClosedRequested(){
if(glfwWindowShouldClose(windowID) == GL11.GL_FALSE){
return false;
}else{
return true;
}
}
public void setTitle(String title){
glfwSetWindowTitle(windowID, title);
}
public long getWindowID(){
return windowID;
}
}

View file

@ -0,0 +1,13 @@
package mrdev023.opengl.exception;
public class FrameException extends Exception{
public FrameException(String content){
super("Frame not be created : " + content);
}
public FrameException(){
super("Frame not be created !");
}
}