From fa79114e3e76db43ef02c24edbcd6a5edc0eaaec Mon Sep 17 00:00:00 2001 From: "Florian Richer (MrDev023)" Date: Sun, 27 Dec 2015 16:29:42 +0100 Subject: [PATCH] Update --- First Game Engine Project/res/font/font.png | Bin 0 -> 1379 bytes .../src/mrdev023/audio/Audio.java | 341 ++++++++++++++++++ .../src/mrdev023/gameengine/GameEngine.java | 19 + .../gameengine/gamestate/MainState.java | 39 +- .../src/mrdev023/math/Color4f.java | 77 ++++ .../src/mrdev023/math/Mathf.java | 58 +++ .../src/mrdev023/math/Vector2f.java | 41 +++ .../mrdev023/network/client/MainClient.java | 3 +- .../src/mrdev023/network/common/Client.java | 4 +- .../src/mrdev023/network/common/Register.java | 34 ++ .../network/packet/ClientConnect.java | 5 +- .../network/packet/ClientDisconnect.java | 10 +- .../network/packet/MessagePacket.java | 4 +- .../network/packet/OutOfSocketPacket.java | 4 +- .../mrdev023/network/server/MainServer.java | 5 +- .../src/mrdev023/opengl/DisplayManager.java | 29 +- .../src/mrdev023/opengl/Frame.java | 4 +- .../src/mrdev023/opengl/Input.java | 245 +++++++++++++ .../src/mrdev023/opengl/TextFont.java | 41 +++ .../src/mrdev023/opengl/Texture.java | 99 +++++ .../src/mrdev023/opengl/gui/Message.java | 34 ++ 21 files changed, 1075 insertions(+), 21 deletions(-) create mode 100644 First Game Engine Project/res/font/font.png create mode 100644 First Game Engine Project/src/mrdev023/audio/Audio.java create mode 100644 First Game Engine Project/src/mrdev023/math/Color4f.java create mode 100644 First Game Engine Project/src/mrdev023/math/Mathf.java create mode 100644 First Game Engine Project/src/mrdev023/math/Vector2f.java create mode 100644 First Game Engine Project/src/mrdev023/network/common/Register.java create mode 100644 First Game Engine Project/src/mrdev023/opengl/Input.java create mode 100644 First Game Engine Project/src/mrdev023/opengl/TextFont.java create mode 100644 First Game Engine Project/src/mrdev023/opengl/Texture.java create mode 100644 First Game Engine Project/src/mrdev023/opengl/gui/Message.java diff --git a/First Game Engine Project/res/font/font.png b/First Game Engine Project/res/font/font.png new file mode 100644 index 0000000000000000000000000000000000000000..f614b318ec9595461ab3528c247894fedad2a2b2 GIT binary patch literal 1379 zcmV-p1)TbcP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D02*{fSaefwW^{L9 za%BKeVQFr3E>1;MAa*k@H7+qQF!XYv000EiNklc^&G~vZvycB$29aOA zUvUWRj8EU+$Ift`?XG+I%8sEXSEiYB?ogIdGX{~z@%8s1Fy+tKV?>tmE4vHq+kXMX z`$IT?g?-EI3qo1X3r?-w*CTt3$TEKK9wTy^!ugN- z0#Nll`2}RAXKKrbd{^~+@&y1k<0Efl=Pq{Qc>6gR>WXVlRa|pgxeLs>?#|z_$2fM# zXzyUe@iu?P&TPy!_T5<=BU34l_4qD~AN2*`0#m-SU-<&y-m~NvT*Z!Z?_zhJ^zmrN zJf|z}Id^uPW2l{XvagZ=k$y_Q~vkA0H3gL zdGQ58R&i=&k5TsUNI6CvZ}XL1@;2_jPd&Pi43Boq`|LR9thnzOaa_x@o@UgHQQpDS zGX~8ZQs%iD(I5X`fbkbF(p}^kgU&bvrkr=w)A#prdw;gOGCp=Zx|Mt1ch^}z+hauL zyDDSM+}cqg_xrxbINm3twKHSXth2zjjx*me;&`6Bz`p$#0Dpn{&oO?fbXTs{UqHQ+ zacA%uhn@3|x_p0YZ+}S3s@EuEW;J!J^CK*S+%>#v(AjsvyQSAe${86OJ4P=oK~*q zci7+WyW6?%o;$1ZcQ{kldzIr6e=#>2*;b4wdd`@uv@_$%aGve1!P+=>W{jG3$l^HX zSFN9)eXep!d&)JZ6W*0|XTGlr)773=3w`nk$+_8lXRk!z+_e%8%AyMxT_9eCtj+Nttb z53Eajo-2!J%etIL6x>;r)8tUrao+s>z5q0XnA6zR)Ht^>XH?eFT{ZGHj(r-DdSqS7 zHK)!vq@6RPTlGiYm35HOl=hfwrdFPHnY*3$AYqxb6YJh{7E#TySG>wIS6$|ssb`!F zTHy#>K=1N+e+XyaG2)o#vF?o04$j=i?2A z>$`g7U0LUh!)Kl99<4k%rvj_Ee}C)YLO}j5x04_wF%{nVX#%uNb}3dBpr&xAPt(EE7A*na%9| zvpq)I!J}KUu8jA 1.0f)gain = 1.0f; + if(gain < 0.0f)gain = 0.0f; + alSourcef(source, AL_GAIN, gain); + } + + public void setPitch(float pitch){ + if(source == 0 || buffer == 0) return; + if(pitch < 0.0f)pitch = 0.0f; + alSourcef(source, AL_PITCH, pitch); + } + + + public float getGain(){ + if(source == 0 || buffer == 0) return 0; + return alGetSourcef(source, AL_GAIN); + } + + public float getPitch(){ + if(source == 0 || buffer == 0) return 0; + return alGetSourcef(source, AL_PITCH); + } + + public void setLooping(boolean looping){ + if(source == 0 || buffer == 0) return; + if(looping){ + alSourcef(source, AL_LOOPING, AL_TRUE); + }else{ + alSourcef(source, AL_LOOPING, AL_FALSE); + } + } + + public void destroySound(){ + alDeleteSources(source); + alDeleteBuffers(buffer); + source = 0; + buffer = 0; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) throws Exception { + this.fileName = fileName; + destroySound(); + setSound(); + } + + public int getBuffer() { + return buffer; + } + + public void setBuffer(int buffer) { + this.buffer = buffer; + } + + public int getSource() { + return source; + } + + public void setSource(int source) { + this.source = source; + } + //------------------------------------------------------ + +} diff --git a/First Game Engine Project/src/mrdev023/gameengine/GameEngine.java b/First Game Engine Project/src/mrdev023/gameengine/GameEngine.java index 23f3516..26f4bd9 100644 --- a/First Game Engine Project/src/mrdev023/gameengine/GameEngine.java +++ b/First Game Engine Project/src/mrdev023/gameengine/GameEngine.java @@ -1,7 +1,11 @@ package mrdev023.gameengine; +import java.io.*; + +import mrdev023.audio.*; import mrdev023.gameengine.gamestate.main.*; import mrdev023.network.client.*; +import mrdev023.network.common.*; import mrdev023.network.packet.*; import mrdev023.opengl.*; @@ -28,8 +32,19 @@ public class GameEngine { } public static void init(){ +// try{ +// File file = new File("/log/" + System.currentTimeMillis() + ".txt"); +// if(!file.exists()){ +// file.createNewFile(); +// } +// PrintStream out = new PrintStream(file); +// System.setOut(out); +// }catch(Exception e){e.printStackTrace();} + Input.init(); current = System.nanoTime(); state.init(); + Texture.init(); + Audio.create(); } public static void loop(){ @@ -48,6 +63,7 @@ public class GameEngine { if(elapsedTicks >= 1000000000/60){ frame.updateEvent(); + Input.update(); state.updateKeyboard(); state.updateMouse(); state.update(); @@ -83,7 +99,10 @@ public class GameEngine { public static void destroy(){ state.destroy(); + Audio.destroy(); + Input.destroy(); frame.destroy(); + MainClient.send(new ClientDisconnect(MainClient.pseudo)); MainClient.client.destroy(); } diff --git a/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java b/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java index 79cf864..8cc03ff 100644 --- a/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java +++ b/First Game Engine Project/src/mrdev023/gameengine/gamestate/MainState.java @@ -1,9 +1,23 @@ package mrdev023.gameengine.gamestate; +import static org.lwjgl.glfw.GLFW.*; + +import java.awt.*; +import java.util.*; + +import javax.swing.*; + +import mrdev023.gameengine.*; import mrdev023.gameengine.gamestate.main.*; +import mrdev023.network.client.*; +import mrdev023.network.packet.*; +import mrdev023.opengl.*; +import mrdev023.opengl.gui.*; public class MainState implements IGameState{ + public static ArrayList messages = new ArrayList(); + public void init() { } @@ -21,7 +35,9 @@ public class MainState implements IGameState{ } public void renderGUI() { - + for(int i = 0;i < messages.size();i++){ + messages.get(i).render(10, 10 + i * 20); + } } public void render2D() { @@ -29,11 +45,28 @@ public class MainState implements IGameState{ } public void update() { - + ArrayList rmList = new ArrayList(); + for(Message m : messages){ + if(m.isTimeOut()){ + rmList.add(m); + } + } + for(Message m : rmList){ + messages.remove(m); + } + rmList.clear(); } public void updateKeyboard() { - + if(Input.isKeyDown(GLFW_KEY_M)){ + try { + MainClient.client.send(new MessagePacket(JOptionPane.showInputDialog(null, "Entrez votre message :"),MainClient.pseudo)); + } catch (HeadlessException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } } public void updateMouse() { diff --git a/First Game Engine Project/src/mrdev023/math/Color4f.java b/First Game Engine Project/src/mrdev023/math/Color4f.java new file mode 100644 index 0000000..401fa47 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/math/Color4f.java @@ -0,0 +1,77 @@ +package mrdev023.math; + +import static org.lwjgl.opengl.GL11.*; + +public class Color4f { + + public static final Color4f + RED = new Color4f(1,0,0,1), + BLUE = new Color4f(0,0,1,1), + GREEN = new Color4f(0,1,0,1), + YELLOW = new Color4f(1,1,0,1), + PURPLE = new Color4f(1,0,1,1), + CYAN = new Color4f(0,1,1,1), + BLACK = new Color4f(0,0,0,1), + WHITE = new Color4f(1,1,1,1); + + public float r,g,b,a; + + public Color4f(float r,float g,float b,float a){ + this.r = r; + this.g = g; + this.b = b; + this.a = a; + } + + public static Color4f mul (Color4f a, float b){ + return new Color4f(a.r * b,a.g * b,a.b * b,a.a * b); + } + + public static Color4f mul (Color4f a, Color4f b){ + return new Color4f((a.r + b.r)/2.0f,(a.g + b.g)/2.0f,(a.b + b.b)/2.0f,(a.a + b.a)/2.0f); + } + + public Color4f() { + } + + public float getR() { + return r; + } + + public void setR(float r) { + this.r = r; + } + + public float getG() { + return g; + } + + public void setG(float g) { + this.g = g; + } + + public float getB() { + return b; + } + + public void setB(float b) { + this.b = b; + } + + public float getA() { + return a; + } + + public void setA(float a) { + this.a = a; + } + + public void bind(){ + glColor4f(r,g,b,a); + } + + public void unbind(){ + BLACK.bind(); + } + +} diff --git a/First Game Engine Project/src/mrdev023/math/Mathf.java b/First Game Engine Project/src/mrdev023/math/Mathf.java new file mode 100644 index 0000000..a48a987 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/math/Mathf.java @@ -0,0 +1,58 @@ +package mrdev023.math; + +public class Mathf { + + public static final float PI = (float)Math.PI; + public static final float EPSILON = 1.401298e-45f; + + public static float cos(float angle){ + return (float)Math.cos(angle); + } + + public static float acos(float angle){ + return (float)Math.acos(angle); + } + + public static float sin(float angle){ + return (float)Math.sin(angle); + } + + public static float asin(float angle){ + return (float)Math.asin(angle); + } + + public static float toRadians(float angle){ + return (float)Math.toRadians(angle); + } + + public static float toDegrees(float angle){ + return (float)Math.toDegrees(angle); + } + + public static float atan2(float a,float b){ + return (float)Math.atan2(a,b); + } + + public static float cut(float nbre,float a){ + return (float)((int)(nbre*Math.pow(10, a))/Math.pow(10, a)); + } + + public static boolean equals(float a,float b,float tolerance){ + return (a + tolerance >= b) && (a - tolerance <= b); + } + + public static float sqrt(float a){ + return (float)Math.sqrt(a); + } + + public static float clamp(float value, float min, float max) { + if(value < min){ + value = min; + } + if(value > max){ + value = max; + } + return value; + } + +} diff --git a/First Game Engine Project/src/mrdev023/math/Vector2f.java b/First Game Engine Project/src/mrdev023/math/Vector2f.java new file mode 100644 index 0000000..6d37fce --- /dev/null +++ b/First Game Engine Project/src/mrdev023/math/Vector2f.java @@ -0,0 +1,41 @@ +package mrdev023.math; + +import java.util.*; + +public class Vector2f { + + public float x,y; + + public Vector2f(){ + x = 0; + y = 0; + } + + public Vector2f(float x,float y){ + this.x = x; + this.y = y; + } + + public float getX() { + return x; + } + + public void setX(float x) { + this.x = x; + } + + public float getY() { + return y; + } + + public void setY(float y) { + this.y = y; + } + + public String toString(){ + StringJoiner st = new StringJoiner(",","vec2(",")"); + st.add("" + x); + st.add("" + y); + return st.toString(); + } +} diff --git a/First Game Engine Project/src/mrdev023/network/client/MainClient.java b/First Game Engine Project/src/mrdev023/network/client/MainClient.java index b57d7d8..4ddc522 100644 --- a/First Game Engine Project/src/mrdev023/network/client/MainClient.java +++ b/First Game Engine Project/src/mrdev023/network/client/MainClient.java @@ -15,8 +15,9 @@ public class MainClient { public static String pseudo = ""; public static void startClient() { + Register.registerClass(); try { - String input = JOptionPane.showInputDialog(null,"Entrer ip:port du serveur"); + String input = "127.0.0.1:9999";//JOptionPane.showInputDialog(null,"Entrer ip:port du serveur"); String[] i = input.split(":"); String pseudo = JOptionPane.showInputDialog(null,"Pseudo:"); client = new Client(InetAddress.getByName(i[0]),Integer.parseInt(i[1])); diff --git a/First Game Engine Project/src/mrdev023/network/common/Client.java b/First Game Engine Project/src/mrdev023/network/common/Client.java index 076037b..293374b 100644 --- a/First Game Engine Project/src/mrdev023/network/common/Client.java +++ b/First Game Engine Project/src/mrdev023/network/common/Client.java @@ -53,7 +53,7 @@ public class Client extends Thread{ DataBuffer dataBuff = new DataBuffer(); dataBuff.setData(data); try { - String name = dataBuff.getString(); + String name = Register.getClass(dataBuff.getInt()); IPacket obj = (IPacket)Class.forName(name).newInstance(); if(obj == null)continue; obj.read(dataBuff); @@ -88,7 +88,7 @@ public class Client extends Thread{ public void send(IPacket packet) throws Exception{ DataBuffer data = new DataBuffer(); - data.put(packet.getClass().getName()); + data.put(Register.getClassID(packet.getClass())); packet.write(data); DatagramPacket sendPacket = new DatagramPacket(data.getData(),data.getData().length,address,port); client.send(sendPacket); diff --git a/First Game Engine Project/src/mrdev023/network/common/Register.java b/First Game Engine Project/src/mrdev023/network/common/Register.java new file mode 100644 index 0000000..404e8a0 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/network/common/Register.java @@ -0,0 +1,34 @@ +package mrdev023.network.common; + +import java.util.*; + +import mrdev023.network.packet.*; + +public class Register { + + private static ArrayList classReg = new ArrayList(); + + public static void registerClass(){ + addClass(ChangePseudoPacket.class); + addClass(ClientConnect.class); + addClass(ClientDisconnect.class); + addClass(MessagePacket.class); + addClass(MessageTestPacket.class); + addClass(OutOfSocketPacket.class); + addClass(PingClientPacket.class); + } + + public static void addClass(Class cl){ + classReg.add(cl.getName()); + } + + public static String getClass(int i){ + return classReg.get(i); + } + + public static int getClassID(Class cl){ + for(int i = 0;i < classReg.size();i++)if(cl.getName().equals(classReg.get(i)))return i; + return -1; + } + +} diff --git a/First Game Engine Project/src/mrdev023/network/packet/ClientConnect.java b/First Game Engine Project/src/mrdev023/network/packet/ClientConnect.java index b62f7bc..5c325e4 100644 --- a/First Game Engine Project/src/mrdev023/network/packet/ClientConnect.java +++ b/First Game Engine Project/src/mrdev023/network/packet/ClientConnect.java @@ -2,8 +2,10 @@ package mrdev023.network.packet; import java.net.*; +import mrdev023.gameengine.gamestate.*; import mrdev023.network.common.*; import mrdev023.network.packet.main.*; +import mrdev023.opengl.gui.*; public class ClientConnect implements IPacket{ @@ -21,7 +23,8 @@ public class ClientConnect implements IPacket{ } public void manage(Client client, IPacket packet) throws Exception { - + MainState.messages.add(new Message(pseudo + " has connected !")); + System.out.println("hello"); } public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception { diff --git a/First Game Engine Project/src/mrdev023/network/packet/ClientDisconnect.java b/First Game Engine Project/src/mrdev023/network/packet/ClientDisconnect.java index 36445b4..59db0d9 100644 --- a/First Game Engine Project/src/mrdev023/network/packet/ClientDisconnect.java +++ b/First Game Engine Project/src/mrdev023/network/packet/ClientDisconnect.java @@ -2,8 +2,11 @@ package mrdev023.network.packet; import java.net.*; +import mrdev023.gameengine.gamestate.*; import mrdev023.network.common.*; import mrdev023.network.packet.main.*; +import mrdev023.network.server.*; +import mrdev023.opengl.gui.*; public class ClientDisconnect implements IPacket{ @@ -21,11 +24,14 @@ public String pseudo; } public void manage(Client client, IPacket packet) throws Exception { - + MainState.messages.add(new Message(pseudo + " has disconnected !")); } public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception { - + System.out.println(client.getPseudo() + " has diconnected " + client.getAddress().getHostName() + ":" + client.getPort()); + MainServer.sendToClients(new ClientDisconnect(client.getPseudo())); + client.setRunning(false); + MainServer.clients.remove(client); } } diff --git a/First Game Engine Project/src/mrdev023/network/packet/MessagePacket.java b/First Game Engine Project/src/mrdev023/network/packet/MessagePacket.java index 95a3084..ac8afbe 100644 --- a/First Game Engine Project/src/mrdev023/network/packet/MessagePacket.java +++ b/First Game Engine Project/src/mrdev023/network/packet/MessagePacket.java @@ -2,9 +2,11 @@ package mrdev023.network.packet; import java.net.*; +import mrdev023.gameengine.gamestate.*; import mrdev023.network.common.*; import mrdev023.network.packet.main.*; import mrdev023.network.server.*; +import mrdev023.opengl.gui.*; public class MessagePacket implements IPacket{ @@ -25,7 +27,7 @@ public class MessagePacket implements IPacket{ } public void manage(Client client, IPacket packet) throws Exception { - System.out.println(pseudo + ": " + message); + MainState.messages.add(new Message(pseudo + " : " + message)); } public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception { diff --git a/First Game Engine Project/src/mrdev023/network/packet/OutOfSocketPacket.java b/First Game Engine Project/src/mrdev023/network/packet/OutOfSocketPacket.java index 06f6934..ddc2626 100644 --- a/First Game Engine Project/src/mrdev023/network/packet/OutOfSocketPacket.java +++ b/First Game Engine Project/src/mrdev023/network/packet/OutOfSocketPacket.java @@ -4,6 +4,7 @@ import java.net.*; import javax.swing.*; +import mrdev023.gameengine.*; import mrdev023.network.common.*; import mrdev023.network.packet.main.*; @@ -19,8 +20,7 @@ public class OutOfSocketPacket implements IPacket{ public void manage(Client client, IPacket packet) throws Exception { JOptionPane.showMessageDialog(null,"OutOfSocket","Error",JOptionPane.ERROR_MESSAGE); - client.setRunning(false); - System.exit(0); + GameEngine.setRunning(false); } public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception { diff --git a/First Game Engine Project/src/mrdev023/network/server/MainServer.java b/First Game Engine Project/src/mrdev023/network/server/MainServer.java index 217c5ff..d0f1162 100644 --- a/First Game Engine Project/src/mrdev023/network/server/MainServer.java +++ b/First Game Engine Project/src/mrdev023/network/server/MainServer.java @@ -40,6 +40,7 @@ public class MainServer extends Thread{ server = new DatagramSocket(9999); System.out.println("Server binding " + InetAddress.getLocalHost().getHostAddress() + ":" + PORT); isRunning = true; + Register.registerClass(); (new Thread(new MainServer())).start(); while(isRunning){ byte[] data = new byte[DataBuffer.SIZE]; @@ -49,7 +50,7 @@ public class MainServer extends Thread{ if(p.getAddress() != null){ DataBuffer buffer = new DataBuffer(); buffer.setData(data); - IPacket packet = (IPacket)Class.forName(buffer.getString()).newInstance(); + IPacket packet = (IPacket)Class.forName(Register.getClass(buffer.getInt())).newInstance(); packet.read(buffer); Client c = getClient(p.getAddress(), p.getPort()); if(c == null){ @@ -112,7 +113,7 @@ public class MainServer extends Thread{ public static void send(IPacket packet,InetAddress ip,int port){ try { DataBuffer buffer = new DataBuffer(); - buffer.put(packet.getClass().getName()); + buffer.put(Register.getClassID(packet.getClass())); packet.write(buffer); DatagramPacket p = new DatagramPacket(buffer.getData(), buffer.getData().length,ip,port); server.send(p); diff --git a/First Game Engine Project/src/mrdev023/opengl/DisplayManager.java b/First Game Engine Project/src/mrdev023/opengl/DisplayManager.java index eacc035..83eacda 100644 --- a/First Game Engine Project/src/mrdev023/opengl/DisplayManager.java +++ b/First Game Engine Project/src/mrdev023/opengl/DisplayManager.java @@ -1,25 +1,44 @@ package mrdev023.opengl; +import mrdev023.gameengine.*; +import static org.lwjgl.opengl.GL11.*; + public class DisplayManager { public static void clear(){ - + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } public static void preRender2D(){ - + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, GameEngine.getFrame().getWidth(), 0, GameEngine.getFrame().getHeight(), -1, 1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glEnable(GL_TEXTURE_2D); + glEnable (GL_BLEND); + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + GameEngine.getGameState().preRender2D(); } public static void preRenderGUI(){ - + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, GameEngine.getFrame().getWidth(), 0, GameEngine.getFrame().getHeight(), -1, 1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glEnable(GL_TEXTURE_2D); + glEnable (GL_BLEND); + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + GameEngine.getGameState().preRenderGUI(); } public static void render2D(){ - + GameEngine.getGameState().render2D(); } public static void renderGUI(){ - + GameEngine.getGameState().renderGUI(); } } diff --git a/First Game Engine Project/src/mrdev023/opengl/Frame.java b/First Game Engine Project/src/mrdev023/opengl/Frame.java index d90db22..8763658 100644 --- a/First Game Engine Project/src/mrdev023/opengl/Frame.java +++ b/First Game Engine Project/src/mrdev023/opengl/Frame.java @@ -36,7 +36,7 @@ public class Frame { //definie la sortie d'erreur errorCallback = new GLFWErrorCallback() { public void invoke(int error, long description) { - System.err.println("ID : " + error + " | Description :" + description); + //System.err.println("ID : " + error + " | Description :" + description); } }; glfwSetErrorCallback(errorCallback); @@ -94,7 +94,7 @@ public class Frame { //definie la sortie d'erreur errorCallback = new GLFWErrorCallback() { public void invoke(int error, long description) { - System.err.println("ID : " + error + " | Description :" + description); + //System.err.println("ID : " + error + " | Description :" + description); } }; glfwSetErrorCallback(errorCallback); diff --git a/First Game Engine Project/src/mrdev023/opengl/Input.java b/First Game Engine Project/src/mrdev023/opengl/Input.java new file mode 100644 index 0000000..880436d --- /dev/null +++ b/First Game Engine Project/src/mrdev023/opengl/Input.java @@ -0,0 +1,245 @@ +package mrdev023.opengl; + +import static org.lwjgl.glfw.GLFW.*; + +import java.util.*; +import java.util.Map.*; + +import org.lwjgl.glfw.*; + +import mrdev023.gameengine.*; +import mrdev023.math.*; +import mrdev023.opengl.*; + +public class Input{ + + public static GLFWScrollCallback scroll; + public static GLFWCursorPosCallback mousePos; + + private static Vector2f mousePosition = new Vector2f(); + private static Vector2f dMouse = new Vector2f(); + private static Vector2f previousDMouse = new Vector2f(); + + public static final int NONE = 0,PRESSED = 1,RELEASED = 2,REPEATED = 3,UP = 2,DOWN = 1, + NBRE_KEY = 0x15D,NBRE_BUTTON = 10, + MOUSE_OFFSET = NBRE_KEY + 1,MOUSE_WHEEL_OFFSET = MOUSE_OFFSET + NBRE_BUTTON + 1; + + private static HashMap state = new HashMap(); + + private static double ywheel = 0; + + public static void init(){ + glfwSetScrollCallback(GameEngine.getFrame().getWindowID(), scroll = new GLFWScrollCallback() { + public void invoke(long window, double xoffset, double yoffset) { + scroll(window, xoffset, yoffset); + } + }); + glfwSetCursorPosCallback(GameEngine.getFrame().getWindowID(), mousePos = new GLFWCursorPosCallback() { + public void invoke(long window, double xpos, double ypos) { + mousepos(window, xpos, ypos); + } + }); + for(int i = 0;i < NBRE_KEY;i++){ + state.put(i, NONE); + } + for(int i = 0;i < NBRE_BUTTON;i++){ + state.put(i + MOUSE_OFFSET, NONE); + } + state.put(MOUSE_WHEEL_OFFSET, NONE); + } + + public static void update(){ + for(Entry set : state.entrySet()){ + int i = set.getKey(); + int st = set.getValue(); + if(i > -1 && i < NBRE_KEY){ + if(glfwGetKey(GameEngine.getFrame().getWindowID(), i) == 0 && st == NONE)continue; + if(glfwGetKey(GameEngine.getFrame().getWindowID(), i) == 1 && st == NONE){ + state.replace(i, PRESSED); + }else if(glfwGetKey(GameEngine.getFrame().getWindowID(), i) == 1 && st == PRESSED){ + state.replace(i, REPEATED); + }else if(glfwGetKey(GameEngine.getFrame().getWindowID(), i) == 0 && (st == PRESSED || st == REPEATED)){ + state.replace(i, RELEASED); + }else if(glfwGetKey(GameEngine.getFrame().getWindowID(), i) == 0 && st == RELEASED){ + state.replace(i, NONE); + } + }else if(i >= MOUSE_OFFSET && i < MOUSE_OFFSET + NBRE_BUTTON){ + if(glfwGetMouseButton(GameEngine.getFrame().getWindowID(), i - MOUSE_OFFSET) == 0 && st == NONE)continue; + if(glfwGetMouseButton(GameEngine.getFrame().getWindowID(), i - MOUSE_OFFSET) == 1 && st == NONE){ + state.replace(i, PRESSED); + }else if(glfwGetMouseButton(GameEngine.getFrame().getWindowID(), i - MOUSE_OFFSET) == 1 && st == PRESSED){ + state.replace(i, REPEATED); + }else if(glfwGetMouseButton(GameEngine.getFrame().getWindowID(), i - MOUSE_OFFSET) == 0 && (st == PRESSED || st == REPEATED)){ + state.replace(i, RELEASED); + }else if(glfwGetMouseButton(GameEngine.getFrame().getWindowID(), i - MOUSE_OFFSET) == 0 && st == RELEASED){ + state.replace(i, NONE); + } + } + } + int st = state.get(MOUSE_WHEEL_OFFSET); + if(ywheel > 0 && (st == NONE || st == UP)){ + state.replace(MOUSE_WHEEL_OFFSET, UP); + }else if(ywheel < 0 && (st == NONE || st == DOWN)){ + state.replace(MOUSE_WHEEL_OFFSET, DOWN); + }else if(ywheel == 0 && (st == DOWN || st == UP)){ + state.replace(MOUSE_WHEEL_OFFSET, NONE); + } + ywheel = 0; + if(dMouse.equals(previousDMouse)){ + dMouse = new Vector2f(); + }else{ + previousDMouse = dMouse; + } + } + + public static void destroy(){ + mousePos.release(); + scroll.release(); + } + + public static void scroll(long window, double xoffset, double yoffset) { + ywheel = yoffset; + } + + public static void mousepos(long window, double xpos, double ypos) { + dMouse.x = (float) (xpos - mousePosition.x); + dMouse.y = (float) (ypos - mousePosition.y); + mousePosition.x = (float) xpos; + mousePosition.y = (float) ypos; + } + + public static boolean isButtonDown(int button){ + return state.get(button + MOUSE_OFFSET) == PRESSED; + } + + public static boolean isButtonUp(int button){ + return state.get(button + MOUSE_OFFSET) == RELEASED; + } + + public static boolean isButton(int button){ + return state.get(button + MOUSE_OFFSET) == PRESSED || state.get(button + MOUSE_OFFSET) == REPEATED; + } + + public static int isButtonState(int button){ + return state.get(button + MOUSE_OFFSET); + } + + public static boolean isKeyDown(int key){ + return state.get(key) == PRESSED; + } + + public static boolean isKeyUp(int key){ + return state.get(key) == RELEASED; + } + + public static boolean isKey(int key){ + return state.get(key) == PRESSED || state.get(key) == REPEATED; + } + + public static int isKeyState(int key){ + return state.get(key); + } + + public static int isMouseWheelState(){ + return state.get(MOUSE_WHEEL_OFFSET); + } + + public static boolean isMouseWheelUp(){ + return state.get(MOUSE_WHEEL_OFFSET) == UP; + } + + public static boolean isMouseWheelDown(){ + return state.get(MOUSE_WHEEL_OFFSET) == DOWN; + } + + public static GLFWScrollCallback getScroll() { + return scroll; + } + + public static void setScroll(GLFWScrollCallback scroll) { + Input.scroll = scroll; + } + + public static GLFWCursorPosCallback getMousePos() { + return mousePos; + } + + public static void setMousePos(GLFWCursorPosCallback mousePos) { + Input.mousePos = mousePos; + } + + public static Vector2f getMousePosition() { + return mousePosition; + } + + public static void setMousePosition(Vector2f mousePosition) { + Input.mousePosition = mousePosition; + } + + public static Vector2f getDMouse() { + return dMouse; + } + + public static void setDMouse(Vector2f dMouse) { + Input.dMouse = dMouse; + } + + public static HashMap getState() { + return state; + } + + public static void setState(HashMap state) { + Input.state = state; + } + + public static double getYwheel() { + return ywheel; + } + + public static void setYwheel(double ywheel) { + Input.ywheel = ywheel; + } + + public static int getNone() { + return NONE; + } + + public static int getPressed() { + return PRESSED; + } + + public static int getReleased() { + return RELEASED; + } + + public static int getRepeated() { + return REPEATED; + } + + public static int getUp() { + return UP; + } + + public static int getDown() { + return DOWN; + } + + public static int getNbreKey() { + return NBRE_KEY; + } + + public static int getNbreButton() { + return NBRE_BUTTON; + } + + public static int getMouseOffset() { + return MOUSE_OFFSET; + } + + public static int getMouseWheelOffset() { + return MOUSE_WHEEL_OFFSET; + } + + + +} diff --git a/First Game Engine Project/src/mrdev023/opengl/TextFont.java b/First Game Engine Project/src/mrdev023/opengl/TextFont.java new file mode 100644 index 0000000..242850d --- /dev/null +++ b/First Game Engine Project/src/mrdev023/opengl/TextFont.java @@ -0,0 +1,41 @@ +package mrdev023.opengl; + +import static org.lwjgl.opengl.GL11.*; + +public class TextFont { + + public static final String font = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "+ + "0123456789.,!?'\"-+=/\\%()<>:; "; + + public static void drawText(String title,int x,int y,int size,float alpha){ + int id = 0; + title = title.toUpperCase(); + glBindTexture(GL_TEXTURE_2D,Texture.FONT.getID()); + glBegin(GL_QUADS); + for(int i = 0;i < title.length();i++){ + id = font.indexOf(title.charAt(i)); + int xx = x + size * i; + int yy = y; + int ty = (id >= 32)?1:0; + int tx = id % 32; + + glColor4f(1,1,1,alpha); + + glTexCoord2f((0 + tx)/32.0f, (0 + ty)/2.0f); + glVertex2i(xx,yy + size); + + glTexCoord2f((1 + tx)/32.0f, (0 + ty)/2.0f); + glVertex2i(xx + size,yy + size); + + glTexCoord2f((1 + tx)/32.0f, (1 + ty)/2.0f); + glVertex2i(xx + size,yy); + + glTexCoord2f((0 + tx)/32.0f, (1 + ty)/2.0f); + glVertex2i(xx,yy); + + } + glEnd(); + glBindTexture(GL_TEXTURE_2D,0); + } + +} diff --git a/First Game Engine Project/src/mrdev023/opengl/Texture.java b/First Game Engine Project/src/mrdev023/opengl/Texture.java new file mode 100644 index 0000000..dc7fa3d --- /dev/null +++ b/First Game Engine Project/src/mrdev023/opengl/Texture.java @@ -0,0 +1,99 @@ +package mrdev023.opengl; + +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.*; + +public class Texture { + + public static Texture FONT; + + int width, height; + int id; + + public static void init() { + System.out.println("---------------------------- Load Texture --------------------------------------------"); + FONT = Texture.loadTexture("res/font/font.png"); + System.out.println("--------------------------------------------------------------------------------------"); + } + + 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); + } + + +} diff --git a/First Game Engine Project/src/mrdev023/opengl/gui/Message.java b/First Game Engine Project/src/mrdev023/opengl/gui/Message.java new file mode 100644 index 0000000..c658620 --- /dev/null +++ b/First Game Engine Project/src/mrdev023/opengl/gui/Message.java @@ -0,0 +1,34 @@ +package mrdev023.opengl.gui; + +import mrdev023.opengl.*; + +public class Message { + + public static final int TIME_OUT = 5000; + private long current, previous, time = 0; + private String message; + + + public Message(String message){ + current = System.currentTimeMillis(); + this.message = message; + } + + public void render(int x,int y){ + previous = current; + current = System.currentTimeMillis(); + time += current - previous; + + if(time < TIME_OUT - 1000){ + TextFont.drawText(message, x, y, 16, 1); + }else{ + TextFont.drawText(message, x, y, 16, (TIME_OUT - time)/1000f); + } + + } + + public boolean isTimeOut(){ + return time > TIME_OUT; + } + +}