From ccd442ac7565e6bdd6e9308de50d39bb67b2d37a Mon Sep 17 00:00:00 2001 From: brausjonas <47791011+jonasbraus@users.noreply.github.com> Date: Thu, 10 Nov 2022 18:15:14 +0100 Subject: [PATCH] added 2D stuff --- Rendr/.idea/codeStyles/codeStyleConfig.xml | 5 + Rendr/.idea/misc.xml | 2 +- Rendr/res/blue.png | Bin 0 -> 4449 bytes Rendr/res/circle.obj | 196 +++++++++++++++++++++ Rendr/res/green.png | Bin 0 -> 4448 bytes Rendr/res/red.png | Bin 0 -> 4448 bytes Rendr/res/yellow.png | Bin 0 -> 4448 bytes Rendr/src/Engine2D/Circle.java | 18 ++ Rendr/src/Engine2D/Input.java | 114 ++++++++++++ Rendr/src/Engine2D/Key.java | 7 + Rendr/src/Engine2D/Line.java | 23 +++ Rendr/src/Engine2D/Mouse.java | 6 + Rendr/src/Engine2D/Rectangle.java | 19 ++ Rendr/src/Engine2D/Shape.java | 44 +++++ Rendr/src/Engine2D/Window2D.java | 181 +++++++++++++++++++ Rendr/src/EngineTest/MainGameLoop.java | 4 +- Rendr/src/EngineTest/SwingTest.java | 16 +- Rendr/src/EngineTest/Test2D.java | 47 +++++ Rendr/src/RenderEngine/DisplayManager.java | 8 +- Rendr/src/Test2D/TicTacToe.java | 68 +++++++ 20 files changed, 750 insertions(+), 8 deletions(-) create mode 100644 Rendr/.idea/codeStyles/codeStyleConfig.xml create mode 100644 Rendr/res/blue.png create mode 100644 Rendr/res/circle.obj create mode 100644 Rendr/res/green.png create mode 100644 Rendr/res/red.png create mode 100644 Rendr/res/yellow.png create mode 100644 Rendr/src/Engine2D/Circle.java create mode 100644 Rendr/src/Engine2D/Input.java create mode 100644 Rendr/src/Engine2D/Key.java create mode 100644 Rendr/src/Engine2D/Line.java create mode 100644 Rendr/src/Engine2D/Mouse.java create mode 100644 Rendr/src/Engine2D/Rectangle.java create mode 100644 Rendr/src/Engine2D/Shape.java create mode 100644 Rendr/src/Engine2D/Window2D.java create mode 100644 Rendr/src/EngineTest/Test2D.java create mode 100644 Rendr/src/Test2D/TicTacToe.java diff --git a/Rendr/.idea/codeStyles/codeStyleConfig.xml b/Rendr/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/Rendr/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/Rendr/.idea/misc.xml b/Rendr/.idea/misc.xml index 98cf9b9..ce60bf5 100644 --- a/Rendr/.idea/misc.xml +++ b/Rendr/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Rendr/res/blue.png b/Rendr/res/blue.png new file mode 100644 index 0000000000000000000000000000000000000000..9c4a8c527fef895ed73daef88dfc034f59b3e247 GIT binary patch literal 4449 zcmeAS@N?(olHy`uVBq!ia0y~yV15C@985rwLkFEV11Zh|kH}&M20djEW~^9hUj`Ip zFY)wsWq-!a$z!DMUTAU)C?r|p8d2h$pPQSSSHj?2l$uzQnxasiS(2gP?&%v4-pD7; zz#wqj)5S5Q;?~=1hKvjh9EUeF)}K#koUA2Q1DhJmcXsu$!kuwfvj z*!2?j1d3j2cmgHxsNxCa005f~3wZ1SK-d!`#v)-)priooF+rjyKoJhNmvAh?LIlMj zC^3POjIkys6upE!fs%}|#{`L<7=}qPcC@P literal 0 HcmV?d00001 diff --git a/Rendr/res/circle.obj b/Rendr/res/circle.obj new file mode 100644 index 0000000..c03eb49 --- /dev/null +++ b/Rendr/res/circle.obj @@ -0,0 +1,196 @@ +# Blender v3.3.1 OBJ File: '' +# www.blender.org +o Circle +v 0.000000 0.000000 -1.000000 +v -0.195090 0.000000 -0.980785 +v -0.382683 0.000000 -0.923880 +v -0.555570 0.000000 -0.831470 +v -0.707107 0.000000 -0.707107 +v -0.831470 0.000000 -0.555570 +v -0.923880 0.000000 -0.382683 +v -0.980785 0.000000 -0.195090 +v -1.000000 0.000000 0.000000 +v -0.980785 0.000000 0.195090 +v -0.923880 0.000000 0.382683 +v -0.831470 0.000000 0.555570 +v -0.707107 0.000000 0.707107 +v -0.555570 0.000000 0.831470 +v -0.382683 0.000000 0.923880 +v -0.195090 0.000000 0.980785 +v 0.000000 0.000000 1.000000 +v 0.195090 0.000000 0.980785 +v 0.382683 0.000000 0.923880 +v 0.555570 0.000000 0.831470 +v 0.707107 0.000000 0.707107 +v 0.831470 0.000000 0.555570 +v 0.923880 0.000000 0.382683 +v 0.980785 0.000000 0.195090 +v 1.000000 0.000000 0.000000 +v 0.980785 0.000000 -0.195090 +v 0.923880 0.000000 -0.382683 +v 0.831470 0.000000 -0.555570 +v 0.707107 0.000000 -0.707107 +v 0.555570 0.000000 -0.831470 +v 0.382683 0.000000 -0.923880 +v 0.195090 0.000000 -0.980785 +v 0.000000 0.000000 -0.941431 +v -0.183664 0.000000 -0.923341 +v -0.360270 0.000000 -0.869769 +v -0.523031 0.000000 -0.782771 +v -0.665692 0.000000 -0.665692 +v -0.782771 0.000000 -0.523031 +v -0.869769 0.000000 -0.360270 +v -0.923341 0.000000 -0.183664 +v -0.941431 0.000000 -0.000000 +v -0.923341 0.000000 0.183664 +v -0.869769 0.000000 0.360270 +v -0.782771 0.000000 0.523031 +v -0.665692 0.000000 0.665692 +v -0.523031 0.000000 0.782771 +v -0.360270 0.000000 0.869769 +v -0.183664 0.000000 0.923341 +v 0.000000 0.000000 0.941431 +v 0.183664 0.000000 0.923341 +v 0.360270 0.000000 0.869769 +v 0.523031 0.000000 0.782771 +v 0.665692 0.000000 0.665692 +v 0.782771 0.000000 0.523031 +v 0.869769 0.000000 0.360270 +v 0.923341 0.000000 0.183664 +v 0.941431 0.000000 -0.000000 +v 0.923341 0.000000 -0.183664 +v 0.869769 0.000000 -0.360270 +v 0.782771 0.000000 -0.523031 +v 0.665692 0.000000 -0.665692 +v 0.523031 0.000000 -0.782771 +v 0.360270 0.000000 -0.869769 +v 0.183664 0.000000 -0.923341 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +s off +f 19/1 50/2 51/3 +f 5/4 38/5 6/6 +f 32/7 33/8 1/9 +f 20/10 51/3 52/11 +f 6/6 39/12 7/13 +f 21/14 52/11 53/15 +f 7/13 40/16 8/17 +f 21/14 54/18 22/19 +f 8/17 41/20 9/21 +f 23/22 54/18 55/23 +f 10/24 41/20 42/25 +f 23/22 56/26 24/27 +f 10/24 43/28 11/29 +f 24/27 57/30 25/31 +f 11/29 44/32 12/33 +f 25/31 58/34 26/35 +f 12/33 45/36 13/37 +f 27/38 58/34 59/39 +f 13/37 46/40 14/41 +f 28/42 59/39 60/43 +f 14/41 47/44 15/45 +f 1/9 34/46 2/47 +f 28/42 61/48 29/49 +f 16/50 47/44 48/51 +f 2/47 35/52 3/53 +f 29/49 62/54 30/55 +f 16/50 49/56 17/57 +f 4/58 35/52 36/59 +f 30/55 63/60 31/61 +f 18/62 49/56 50/2 +f 4/58 37/63 5/4 +f 31/61 64/64 32/7 +f 19/1 18/62 50/2 +f 5/4 37/63 38/5 +f 32/7 64/64 33/8 +f 20/10 19/1 51/3 +f 6/6 38/5 39/12 +f 21/14 20/10 52/11 +f 7/13 39/12 40/16 +f 21/14 53/15 54/18 +f 8/17 40/16 41/20 +f 23/22 22/19 54/18 +f 10/24 9/21 41/20 +f 23/22 55/23 56/26 +f 10/24 42/25 43/28 +f 24/27 56/26 57/30 +f 11/29 43/28 44/32 +f 25/31 57/30 58/34 +f 12/33 44/32 45/36 +f 27/38 26/35 58/34 +f 13/37 45/36 46/40 +f 28/42 27/38 59/39 +f 14/41 46/40 47/44 +f 1/9 33/8 34/46 +f 28/42 60/43 61/48 +f 16/50 15/45 47/44 +f 2/47 34/46 35/52 +f 29/49 61/48 62/54 +f 16/50 48/51 49/56 +f 4/58 3/53 35/52 +f 30/55 62/54 63/60 +f 18/62 17/57 49/56 +f 4/58 36/59 37/63 +f 31/61 63/60 64/64 diff --git a/Rendr/res/green.png b/Rendr/res/green.png new file mode 100644 index 0000000000000000000000000000000000000000..13197db03b7b75ec6ad13c7a165c31bebbe174d5 GIT binary patch literal 4448 zcmeAS@N?(olHy`uVBq!ia0y~yV15C@985rwLkFEV11Zh|kH}&M20djEW~^9hUj`Ip zFY)wsWq-!a$zvqgaeARMP)M@GHKN2hKQ}iuuY|$5C^fMpHASI3vm`^o-P1Q9ypd0w zfkEJwr;B4q#jUs33>g_1I1X=UtUsU7I9Yk(h1F9z85lmqcmoYOCdOpBu>%4p?=t#%&@mD^~E zGFqZwX=xHJ7Ezi8gsb<_HVQRbnxiEO;SeC47D=q$3AdMrV^fo0u?PxhSUa7F4#Q}P l0v!oJX)O-Uq*$=I;5WzlkjcBw07ozwJYD@<);T3K0RUQi6`TM7 literal 0 HcmV?d00001 diff --git a/Rendr/res/red.png b/Rendr/res/red.png new file mode 100644 index 0000000000000000000000000000000000000000..128777dbf08a506338ac219b39757b25d4dd2fac GIT binary patch literal 4448 zcmeAS@N?(olHy`uVBq!ia0y~yV15C@985rwLkFEV11Zh|kH}&M20djEW~^9hUj`Ip zFY)wsWq-!a$zy2oFeK{@P)M@GHKN2hKQ}iuuY|$5C^fMpHASI3vm`^o-P1Q9ypd0w zfkEJwr;B4q#jUs33>g_1I1U>e*#A7SakBEn3#;b>t=uQg$-r=62Qvf1FP>D>IIt)vlng{$QtZ=K`w*Yi(N0w zFx;L1Sp(M#GYoeCV0Q@40HBH|PyzrHHSmDP8UTbnL1HWt;R)mbfQKe1tg!1P>MA1WGc-8WSXXVmKzn(GrF7Vi6@BW6w||rp3_`Wwdfbs#Iac@sOz8 zMoW~@5(Uw$fHe(>C>D{M21HcvuuOr{SVF0yNvz&S+bFOO1aeFag34{QL>VnnMoSdj oMH@zopr0HSpg4gdfE literal 0 HcmV?d00001 diff --git a/Rendr/res/yellow.png b/Rendr/res/yellow.png new file mode 100644 index 0000000000000000000000000000000000000000..f144b58cee279e626e35a4fc90eb3df89b856ee8 GIT binary patch literal 4448 zcmeAS@N?(olHy`uVBq!ia0y~yV15C@985rwLkFEV11Zh|kH}&M20djEW~^9hUj`Ip zFY)wsWq-!a$zx=tQXat!6p}1)jVN)>&&^HED`9XhN=+EaktaqI0hLq-M$j>85A_CHT-oUA`MYqUfm*wP$rqfn!zIa;C+4uR38CKWQ{Xs>4^cfboa|6Iy>ZfmRHT3~;J!PC{x JWt~$(69BGO6zTu~ literal 0 HcmV?d00001 diff --git a/Rendr/src/Engine2D/Circle.java b/Rendr/src/Engine2D/Circle.java new file mode 100644 index 0000000..37a06c7 --- /dev/null +++ b/Rendr/src/Engine2D/Circle.java @@ -0,0 +1,18 @@ +package Engine2D; + +public class Circle extends Shape +{ + public float radius; + + public Circle(float x, float y, float radius, Color color) + { + super(x, y, color); + this.radius = radius; + } + + @Override + public Type getType() + { + return Type.Circle; + } +} diff --git a/Rendr/src/Engine2D/Input.java b/Rendr/src/Engine2D/Input.java new file mode 100644 index 0000000..833027e --- /dev/null +++ b/Rendr/src/Engine2D/Input.java @@ -0,0 +1,114 @@ +package Engine2D; + +import RenderEngine.DisplayManager; + +public class Input +{ + public static boolean isKeyPressed(Key key) + { + switch(key) + { + case a: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_A); + case b: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_B); + case c: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_C); + case d: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_D); + case e: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_E); + case f: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_F); + case g: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_G); + case h: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_H); + case i: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_I); + case j: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_J); + case k: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_K); + case l: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_L); + case m: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_M); + case n: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_N); + case o: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_O); + case p: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_P); + case q: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_Q); + case r: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_R); + case s: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_S); + case t: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_T); + case u: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_U); + case v: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_V); + case w: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_W); + case x: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_X); + case y: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_Y); + case k1: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_1); + case k2: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_2); + case k3: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_3); + case k4: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_4); + case k5: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_5); + case k6: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_6); + case k7: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_7); + case k8: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_8); + case k9: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_9); + case k0: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_0); + case kUp: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_UP); + case kDown: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_DOWN); + case kLeft: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_LEFT); + case kRight: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_RIGHT); + case kSpace: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_SPACE); + case kShift: return org.lwjgl.input.Keyboard.isKeyDown(org.lwjgl.input.Keyboard.KEY_LSHIFT); + } + + return false; + } + + public static float horizontal() + { + if(isKeyPressed(Key.a) || isKeyPressed(Key.kLeft)) + { + return -1; + } + + if(isKeyPressed(Key.d) || isKeyPressed(Key.kRight)) + { + return 1; + } + + return 0; + } + + public static float vertical() + { + if(isKeyPressed(Key.w) || isKeyPressed(Key.kUp)) + { + return 1; + } + + if(isKeyPressed(Key.s) || isKeyPressed(Key.kDown)) + { + return -1; + } + + return 0; + } + + public static boolean isMouseDown(Mouse button) + { + if(button == Mouse.left) + { + return org.lwjgl.input.Mouse.isButtonDown(0); + } + else + { + return org.lwjgl.input.Mouse.isButtonDown(1); + } + } + + public static float mouseX() + { + double res = org.lwjgl.input.Mouse.getX() - DisplayManager.width / 2f; + res /= 5.714285714285714; + + return (float)res; + } + + public static float mouseY() + { + double res = org.lwjgl.input.Mouse.getY() - DisplayManager.height / 2f; + res /= 5.714285714285714; + + return (float)res; + } +} diff --git a/Rendr/src/Engine2D/Key.java b/Rendr/src/Engine2D/Key.java new file mode 100644 index 0000000..0cf5d60 --- /dev/null +++ b/Rendr/src/Engine2D/Key.java @@ -0,0 +1,7 @@ +package Engine2D; + +public enum Key +{ + a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, + k1, k2, k3, k4, k5, k6, k7, k8, k9, k0, kSpace, kEnter, kUp, kDown, kLeft, kRight, kShift +} \ No newline at end of file diff --git a/Rendr/src/Engine2D/Line.java b/Rendr/src/Engine2D/Line.java new file mode 100644 index 0000000..e5993c9 --- /dev/null +++ b/Rendr/src/Engine2D/Line.java @@ -0,0 +1,23 @@ +package Engine2D; + +public class Line extends Shape +{ + public float length; + + public float rotation; + public float thickness; + + public Line(float x, float y, float length, float rotation, float thickness, Color color) + { + super(x, y, color); + this.length = length; + this.rotation = rotation; + this.thickness = thickness; + } + + @Override + public Type getType() + { + return Type.Line; + } +} diff --git a/Rendr/src/Engine2D/Mouse.java b/Rendr/src/Engine2D/Mouse.java new file mode 100644 index 0000000..691a786 --- /dev/null +++ b/Rendr/src/Engine2D/Mouse.java @@ -0,0 +1,6 @@ +package Engine2D; + +public enum Mouse +{ + left, right +} diff --git a/Rendr/src/Engine2D/Rectangle.java b/Rendr/src/Engine2D/Rectangle.java new file mode 100644 index 0000000..37486bb --- /dev/null +++ b/Rendr/src/Engine2D/Rectangle.java @@ -0,0 +1,19 @@ +package Engine2D; + +public class Rectangle extends Shape +{ + public float width, height; + + public Rectangle(float x, float y, float width, float height, Color color) + { + super(x, y, color); + this.width = width; + this.height = height; + } + + @Override + public Type getType() + { + return Type.Rectangle; + } +} diff --git a/Rendr/src/Engine2D/Shape.java b/Rendr/src/Engine2D/Shape.java new file mode 100644 index 0000000..b9f870a --- /dev/null +++ b/Rendr/src/Engine2D/Shape.java @@ -0,0 +1,44 @@ +package Engine2D; + +public abstract class Shape +{ + public float x, y; + public Color color; + public Shape(float x, float y, Color color) + { + this.x = x; + this.y = y; + this.color = color; + } + + public void move(float x, float y) + { + this.x += x; + this.y += y; + } + + public void setPosition(float x, float y) + { + this.x = x; + this.y = y; + } + + public enum Type + { + Rectangle, Line, Circle + } + + public enum Color + { + red("red"), green("green"), blue("blue"), yellow("yellow"), white("white"); + + public String color; + + Color(String color) + { + this.color = color; + } + } + + public abstract Type getType(); +} diff --git a/Rendr/src/Engine2D/Window2D.java b/Rendr/src/Engine2D/Window2D.java new file mode 100644 index 0000000..1077a6a --- /dev/null +++ b/Rendr/src/Engine2D/Window2D.java @@ -0,0 +1,181 @@ +package Engine2D; + +import Entity.Entity; +import Models.RawModel; +import Models.TexturedModel; +import RenderEngine.DisplayManager; +import RenderEngine.Loader; +import RenderEngine.Renderer; +import Shaders.StaticShader; +import Textures.ModelTexture; +import Tools.OBJLoader; +import org.lwjgl.opengl.Display; +import org.lwjgl.util.vector.Vector3f; + +import java.util.ArrayList; +import java.util.List; +import Entity.Camera; + +public abstract class Window2D +{ + private Loader loader; + private StaticShader shader; + private Renderer renderer; + + public float gameWidth, gameHeight; + + private float rBackground = 0, gBackground = 0, bBackground = 0; + + List entities = new ArrayList<>(); + List shapes = new ArrayList<>(); + + private Camera cam = new Camera(); + + public void createWindow(String title, int width, int height) + { + DisplayManager.createDisplay(title, true, width, height, false); + loader = new Loader(); + shader = new StaticShader(); + renderer = new Renderer(shader); + + double res = DisplayManager.width / 2f; + res /= 5.714285714285714; + res *= 2; + gameWidth = (float)res; + + System.out.println("Window game width : " + res); + + res = DisplayManager.height / 2f; + res /= 5.714285714285714; + res *= 2; + gameHeight = (float)res; + + System.out.println("Window game height: " + res); + } + + public void addShape(Shape shape) + { + switch (shape.getType()) + { + case Rectangle: + Rectangle rect = (Rectangle) shape; + float[] vertices = + { + //links unten + 0 - rect.width / 2, 0 - rect.height / 2, 0, + //links oben + 0 - rect.width / 2, 0 + rect.height / 2, 0, + //rechts oben + 0 + rect.width / 2, 0 + rect.height / 2, 0, + //rechts unten + 0 + rect.width / 2, 0 - rect.height / 2, 0 + }; + int[] indices = + { + 3, 0, 1, 1, 3, 2 + }; + float[] uvs = + { + 0, 0, + 0, 0, + 0, 0, + 0, 0 + }; + + + RawModel model = loader.loadToVAO(vertices, indices, uvs); + ModelTexture texture = new ModelTexture(loader.loadTexture(shape.color.color)); + TexturedModel texturedModel = new TexturedModel(model, texture); + Entity entity = new Entity(texturedModel, new Vector3f(shape.x, shape.y, -100), 0, 0, 0, 1); + entities.add(entity); + shapes.add(shape); + break; + + case Line: + Line line = (Line) shape; + float[] vertices1 = + { + //links unten + 0 - line.length / 2, 0, 0, + //links oben + 0 - line.length / 2, 0 + line.thickness / 2, 0, + //rechts oben + 0 + line.length / 2, 0 + line.thickness / 2, 0, + //rechts unten + 0 + line.length / 2, 0, 0 + }; + int[] indices1 = + { + 3, 0, 1, 1, 3, 2 + }; + float[] uvs1 = + { + 0, 0, + 0, 0, + 0, 0, + 0, 0 + }; + + RawModel model1 = loader.loadToVAO(vertices1, indices1, uvs1); + ModelTexture texture1 = new ModelTexture(loader.loadTexture(shape.color.color)); + TexturedModel texturedModel1 = new TexturedModel(model1, texture1); + Entity entity1 = new Entity(texturedModel1, new Vector3f(shape.x, shape.y, -100), 0, 0, line.rotation, 1); + entities.add(entity1); + shapes.add(shape); + + break; + case Circle: + Circle circle = (Circle) shape; + OBJLoader objLoader = new OBJLoader("circle"); + float[] vertices2 = objLoader.getVertices(); + int[] indices2 = objLoader.getIndices(); + float[] uvs2 = objLoader.getUVs(); + + + RawModel model2 = loader.loadToVAO(vertices2, indices2, uvs2); + ModelTexture texture2 = new ModelTexture(loader.loadTexture(shape.color.color)); + TexturedModel texturedModel2 = new TexturedModel(model2, texture2); + Entity entity2 = new Entity(texturedModel2, new Vector3f(shape.x, shape.y, -100), -90, 0, 0, circle.radius); + entities.add(entity2); + shapes.add(shape); + break; + } + } + + public void setBackgroundColor(int r, int g, int b) + { + rBackground = r; + gBackground = g; + bBackground = b; + } + + public void update() + { + renderer.prepare(rBackground, gBackground, bBackground); + shader.start(); + shader.loadViewMatrix(cam); + for (int i = 0; i < entities.size(); i++) + { + Entity e = entities.get(i); + Shape s = shapes.get(i); + e.setPosition(new Vector3f(s.x, s.y, e.getPosition().z)); + + renderer.render(e, shader); + } + + shader.stop(); + DisplayManager.updateDisplay(); + } + + public void closeWindow() + { + shader.clear(); + loader.clear(); + DisplayManager.closeDisplay(); + } + + public boolean shouldClose() + { + return Display.isCloseRequested(); + } +} diff --git a/Rendr/src/EngineTest/MainGameLoop.java b/Rendr/src/EngineTest/MainGameLoop.java index 8365835..5f2008c 100644 --- a/Rendr/src/EngineTest/MainGameLoop.java +++ b/Rendr/src/EngineTest/MainGameLoop.java @@ -16,7 +16,6 @@ import org.lwjgl.input.Mouse; import org.lwjgl.opengl.Display; import Textures.ModelTexture; import org.lwjgl.util.vector.Vector3f; -import sun.awt.AWTAccessor; import java.util.Arrays; @@ -29,7 +28,7 @@ public class MainGameLoop public static void main(String[] args) { - DisplayManager.createDisplay("Rendr", true); + DisplayManager.createDisplay("Rendr", true, 800, 600, true); Loader loader = new Loader(); StaticShader shader = new StaticShader(); @@ -46,6 +45,7 @@ public class MainGameLoop TexturedModel texturedModel = new TexturedModel(model, texture); Entity entity = new Entity(texturedModel, new Vector3f(0, 0, -5), 0, 0, 0, 1); + entity.setPosition(new Vector3f(-0.5f, 0, 0)); Camera camera = new Camera(); diff --git a/Rendr/src/EngineTest/SwingTest.java b/Rendr/src/EngineTest/SwingTest.java index 0618480..ac5fd0f 100644 --- a/Rendr/src/EngineTest/SwingTest.java +++ b/Rendr/src/EngineTest/SwingTest.java @@ -69,7 +69,7 @@ public class SwingTest } private void includeGL(){ - DisplayManager.createDisplay("Redr", false); + DisplayManager.createDisplay("Redr", false, 800, 600, true); DisplayManager.setDisplayParent(canvas); Loader loader = new Loader(); @@ -78,13 +78,25 @@ public class SwingTest Camera camera = new Camera(); + OBJLoader objLoader = new OBJLoader("Object"); + float[] vertices = objLoader.getVertices(); + int[] indices = objLoader.getIndices(); + float[] uvs = objLoader.getUVs(); + + + RawModel model = loader.loadToVAO(vertices, indices, uvs); + ModelTexture texture = new ModelTexture(loader.loadTexture("mossyBricks")); + TexturedModel texturedModel = new TexturedModel(model, texture); + Entity entity = new Entity(texturedModel, new Vector3f(0, 0, -5), 0, 0, 0, 1); + while(!Display.isCloseRequested()) { renderer.prepare(0.2f, 0.2f, 0.2f); shader.start(); shader.loadViewMatrix(camera); -// renderer.render(entity, shader); + renderer.render(entity, shader); shader.stop(); + entity.rotate(0, 1, 0); DisplayManager.updateDisplay(); } diff --git a/Rendr/src/EngineTest/Test2D.java b/Rendr/src/EngineTest/Test2D.java new file mode 100644 index 0000000..decfc7c --- /dev/null +++ b/Rendr/src/EngineTest/Test2D.java @@ -0,0 +1,47 @@ +package EngineTest; + +import Engine2D.*; + +public class Test2D extends Window2D +{ + private static Test2D test; + private static Rectangle rect; + private static Line line; + + private static Circle circle; + + public static void main(String[] args) + { + test = new Test2D(); + test.createWindow("Test", 800, 600); + + test.setBackgroundColor(1, 1, 1); + + rect = new Rectangle(0, 0, 10, 10, Shape.Color.green); + test.addShape(rect); + +// line = new Line(0, 0, -10, 5, 10, -5, 1, Shape.Color.blue); + //test.addShape(line); + + circle = new Circle(0, 0, 10, Shape.Color.blue); + test.addShape(circle); + + test.update(); + + test.closeWindow(); + } + + @Override + public void update() + { + while(!test.shouldClose()) + { + super.update(); + + if(Input.isMouseDown(Mouse.left)) + { + circle.setPosition(Input.mouseX(), Input.mouseY()); + } + } + } +} diff --git a/Rendr/src/RenderEngine/DisplayManager.java b/Rendr/src/RenderEngine/DisplayManager.java index c547998..c91e458 100644 --- a/Rendr/src/RenderEngine/DisplayManager.java +++ b/Rendr/src/RenderEngine/DisplayManager.java @@ -8,12 +8,14 @@ import java.awt.*; public class DisplayManager { - private static final int width = 1280, height = 700; + public static int width = 1280, height = 700; private static final int fps = 60; //creates a new display with openGL viewport in it - public static void createDisplay(String title, boolean vSync) + public static void createDisplay(String title, boolean vSync, int _width, int _height, boolean resizable) { + width = _width; + height = _height; ContextAttribs attribs = new ContextAttribs(3, 2) .withForwardCompatible(true).withProfileCore(true); @@ -23,7 +25,7 @@ public class DisplayManager Display.create(new PixelFormat(), attribs); Display.setVSyncEnabled(vSync); Display.setTitle(title); - Display.setResizable(true); + Display.setResizable(resizable); } catch (LWJGLException e) { e.printStackTrace(); diff --git a/Rendr/src/Test2D/TicTacToe.java b/Rendr/src/Test2D/TicTacToe.java new file mode 100644 index 0000000..4e2725f --- /dev/null +++ b/Rendr/src/Test2D/TicTacToe.java @@ -0,0 +1,68 @@ +package Test2D; + +import Engine2D.*; + +public class TicTacToe extends Window2D +{ + public static void main(String[] args) + { + new TicTacToe(); + } + + private int[][] grid = new int[3][3]; + + private int currentPlayer = 1; + + public TicTacToe() + { + createWindow("TicTacToe", 800, 800); + setBackgroundColor(1, 1, 1); + + addShape(new Line(-25, 0, 120, 90, 1, Shape.Color.blue)); + addShape(new Line(25, 0, 120, 90, 1, Shape.Color.blue)); + + addShape(new Line(0, -25, 120, 0, 1, Shape.Color.blue)); + addShape(new Line(0, 25, 120, 0, 1, Shape.Color.blue)); + + update(); + closeWindow(); + } + + @Override + public void update() + { + while(!shouldClose()) + { + super.update(); + + if(Input.isMouseDown(Mouse.left)) + { + //get clicked cell + int xCell = (int)(Input.mouseX() / 25) + 1; + int yCell = (int)(Input.mouseY() / 25) + 1; + + if(xCell <= -1) xCell++; + if(xCell > 2) xCell--; + + if(yCell <= -1) yCell++; + if(yCell > 2) yCell--; + + if(grid[xCell][yCell] == 0) + { + if(currentPlayer == 2) + { + addShape(new Circle((xCell - 1) * 48, (yCell - 1) * 48, 15, Shape.Color.blue)); + } + else + { + addShape(new Line((xCell - 1) * 48, (yCell - 1) * 48, 30, 45, 2, Shape.Color.red)); + addShape(new Line((xCell - 1) * 48, (yCell - 1) * 48, 30, -45, 2, Shape.Color.red)); + } + + grid[xCell][yCell] = currentPlayer; + currentPlayer = currentPlayer == 1 ? 2 : 1; + } + } + } + } +}