diff --git a/First Game Engine Project/.classpath b/First Game Engine Project/.classpath index 63b7e89..e866d18 100644 --- a/First Game Engine Project/.classpath +++ b/First Game Engine Project/.classpath @@ -2,5 +2,6 @@ + diff --git a/First Game Engine Project/.project b/First Game Engine Project/.project index 1906233..4491e84 100644 --- a/First Game Engine Project/.project +++ b/First Game Engine Project/.project @@ -1,6 +1,6 @@ - First Game Engine Project + First 2D Game UDP diff --git a/First Game Engine Project/src/mrdev023/Main.java b/First Game Engine Project/src/mrdev023/Main.java new file mode 100644 index 0000000..942c9b4 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/Main.java @@ -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); + } + +} diff --git a/First Game Engine Project/src/mrdev023/gameengine/GameEngine.java b/First Game Engine Project/src/mrdev023/gameengine/GameEngine.java new file mode 100644 index 0000000..27f5932 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/gameengine/GameEngine.java @@ -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(); + } + + + +} diff --git a/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java b/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java new file mode 100644 index 0000000..79cf864 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java @@ -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() { + + } + +} diff --git a/First Game Engine Project/src/mrdev023/gameengine/gamestate/main/GameState.java b/First Game Engine Project/src/mrdev023/gameengine/gamestate/main/GameState.java new file mode 100644 index 0000000..9f51aa6 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/gameengine/gamestate/main/GameState.java @@ -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(); + } + +} diff --git a/First Game Engine Project/src/mrdev023/gameengine/gamestate/main/IGameState.java b/First Game Engine Project/src/mrdev023/gameengine/gamestate/main/IGameState.java new file mode 100644 index 0000000..3c521fa --- /dev/null +++ b/First Game Engine Project/src/mrdev023/gameengine/gamestate/main/IGameState.java @@ -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(); + +} diff --git a/First Game Engine Project/src/mrdev023/opengl/DisplayManager.java b/First Game Engine Project/src/mrdev023/opengl/DisplayManager.java new file mode 100644 index 0000000..eacc035 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/opengl/DisplayManager.java @@ -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(){ + + } + +} diff --git a/First Game Engine Project/src/mrdev023/opengl/Frame.java b/First Game Engine Project/src/mrdev023/opengl/Frame.java new file mode 100644 index 0000000..d90db22 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/opengl/Frame.java @@ -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 Détruit la fenêtre + */ + public void destroy(){ + glfwDestroyWindow(windowID); + glfwTerminate(); + } + + /** + * @return true si la fenetre à été 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; + } +} diff --git a/First Game Engine Project/src/mrdev023/opengl/exception/FrameException.java b/First Game Engine Project/src/mrdev023/opengl/exception/FrameException.java new file mode 100644 index 0000000..a80945e --- /dev/null +++ b/First Game Engine Project/src/mrdev023/opengl/exception/FrameException.java @@ -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 !"); + } + +}