Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to draw GUI  (Read 2046 times)
0 Members and 1 Guest are viewing this topic.
Offline ernest210

Senior Newbie





« Posted 2012-02-23 22:19:27 »

I almost ashamed to ask Wink
Usually I'm doing all in 3D (and all looks like FPS shooter) but now I want to draw GUI and in fact I don't know how, how to again drawing in 2D.
Mostly my class looks like this one
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
public class MainCanvas extends Canvas {
   private static MainCanvas canvas;
   private Thread gameThread;
   boolean running = false;
   
   private GameState gameState;
   
   public static MainCanvas get(){
      if(canvas == null) {
         canvas = new MainCanvas();
      }
      return canvas;
   }
   
   public final void addNotify(){
      super.addNotify();
      startLWJGL();
   }
   
   public final void removeNotify(){
      stopLWJGL();
      super.removeNotify();
   }
   
   public void startLWJGL(){
      gameThread = new Thread (){
         
         public void run(){
            running = true;
            try {
               Display.setParent(canvas);
               Display.create();
               Display.setVSyncEnabled(true);
               initGL();
               gameState = GameState.prepare();
            } catch (LWJGLException e) {
               e.printStackTrace();
            }
            gameLoop();
         }

      };
      gameThread.start();
   }
   
   public void stopLWJGL(){
      running = false;
      try {
         gameThread.join();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }

   private void initGL() {
      glViewport(0, 0,  Conf.W,Conf.H); // Reset The Current Viewport
     glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
     
      GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
     glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
     GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Black Background
     glClearDepth(1.0f); // Depth Buffer Setup
     glEnable(GL_DEPTH_TEST); // Enables Depth Testing
     glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
     GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
  }
   
   public void gameLoop(){
     
      while(running) {
         gameState.update();
         gameState.render();
         Display.update();
      }
      Display.destroy();
   }
   
}
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2012-02-24 01:10:07 »

Turn off depth testing, turn on ortho mode, and then just draw using screen coordinates. You can easily switch modes and depth testing in between draw calls.

See my work:
OTC Software
Offline UprightPath
« Reply #2 - Posted 2012-02-24 04:50:04 »

An alternative is to use something like TWL ( http://twl.l33tlabs.org/ ) and allow it to do some of the work.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ernest210

Senior Newbie





« Reply #3 - Posted 2012-02-24 09:16:11 »

Turn off depth testing, turn on ortho mode, and then just draw using screen coordinates. You can easily switch modes and depth testing in between draw calls.
It looks like it's working but until I'm not trying to load texture after this my GUI square turns black (should be blue) and loaded texture gets more blue.
I'm using SLICK TextureLoader.
Can you help me with this?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
public class TexturedCubeVA extends JApplet {
   
   private Canvas canvas;
   private Thread gameThread;
   boolean running = false;
   
   private Texture texture;
   
   public void startLWJGL(){
      gameThread = new Thread (){
         public void run(){
            running = true;
            try {
               Display.setParent(canvas);
               Display.create();
               Display.setVSyncEnabled(true);
               initGL();
            } catch (LWJGLException e) {
               e.printStackTrace();
            }
            gameLoop();
         }
      };
      gameThread.start();
   }
   
   public void stopLWJGL(){
      running = false;
      try {
         gameThread.join();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }

   public void destroy(){
      remove(canvas);
      super.destroy();
   }
   
   public void init(){
      setLayout(new BorderLayout());
      canvas = new Canvas(){
         public final void addNotify(){
            super.addNotify();
            startLWJGL();
         }
         public final void removeNotify(){
            stopLWJGL();
            super.removeNotify();
         }
      };
      canvas.setSize(Conf.W, Conf.H);
      setSize(Conf.W, Conf.H);
      canvas.setBackground(Color.BLACK);
      canvas.setFocusable(true);
      canvas.requestFocus();
      canvas.setIgnoreRepaint(false);
      add(canvas);
   }
   
   private void initGL() {
      glViewport(0, 0,  Conf.W,Conf.H); // Reset The Current Viewport
     glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(45.0f, ((float)Conf.W / (float)Conf.H) , 0.1f, 100f);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
     
      GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
     glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
     GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Black Background
     glClearDepth(1.0f); // Depth Buffer Setup
     glEnable(GL_DEPTH_TEST); // Enables Depth Testing
     glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
     GL11.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
     
      try {
         texture = TextureLoader.getTexture("png", ResourceLoader.getResourceAsStream("res/caution.png"));
      } catch (IOException e) {
         e.printStackTrace();
      }
     
   }
   float rquad; // Angle For The Quad
  public void gameLoop(){
      final int amountOfVertices = 24;
        final int vertexSize = 3;
        final int textureVertexSize = 2;
     
        FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(CubeUtil.getCube(1f, 1f, 1f));
        vertexData.flip();
       
        FloatBuffer textureData = BufferUtils.createFloatBuffer(amountOfVertices * textureVertexSize);
        textureData.put(CubeUtil.getCubeTexture(1.0f,1.0f));
        textureData.flip();
       
      while(running) {
         update();
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
         texture.bind();
         
         glLoadIdentity();//Matrix reseting.
        GL11.glTranslatef(-1.0f, 0.0f, -8.0f); // Move Right And Into The Screen
        GL11.glRotatef(rquad, 1.0f, 1.0f, 1.0f); // Rotate The Cube On X, Y & Z
       
         glEnableClientState(GL_VERTEX_ARRAY);// Enable vertex
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
         
            glVertexPointer(vertexSize, 0, vertexData);//First cube
           glTexCoordPointer(textureVertexSize, 0, textureData);
            glDrawArrays(GL_QUADS, 0, amountOfVertices);//Draw cube
           
            glDisableClientState(GL_VERTEX_ARRAY);//Disable Vertex
           glDisableClientState(GL_TEXTURE_COORD_ARRAY);//Disable Vertex
       
            drawGUI();
         Display.sync(60);
         Display.update();
      }
     
      Display.destroy();
   }
   
   private void update(){
      rquad -= 0.80f; // Decrease The Rotation Variable For The Quad
  }

   /**
    * Draw the overlay for score and lifes
    *
    * @param window The window in which the GUI is displayed
    */

   private void drawGUI() {
      enterOrtho();
      guiRenderer();
      leaveOrtho();
   }
   
   /**
    * Drawing square on screen
    */

   private void guiRenderer() {
      GL11.glColor3f(0.5f, 0.5f, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex2f(10, 410);
      GL11.glVertex2f(70, 410);
      GL11.glVertex2f(70, 470);
      GL11.glVertex2f(10, 470);
      GL11.glEnd();
   }
   
   /**
    * Enter the orthographic mode by first recording the current state,
    * next changing us into orthographic projection.
    */

   public void enterOrtho() {
      // store the current state of the renderer
     GL11.glPushAttrib(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_ENABLE_BIT);
      GL11.glPushMatrix();
      GL11.glLoadIdentity();
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glPushMatrix();  
     
      // now enter orthographic projection
     GL11.glLoadIdentity();      
      GL11.glOrtho(0, Conf.W, Conf.H, 0, 1, -1);
      GL11.glDisable(GL11.GL_DEPTH_TEST);
      GL11.glDisable(GL11.GL_LIGHTING);  
   }
   
   /**
    * Leave the orthographic mode by restoring the state we store
    * in enterOrtho()
    *
    * @see enterOrtho()
    */

   public void leaveOrtho() {
      // restore the state of the renderer
     GL11.glPopMatrix();
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      GL11.glPopMatrix();
      GL11.glPopAttrib();
   }
}
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2012-02-29 00:42:33 »

I don't know Slick's texture loader, unfortunately.

See my work:
OTC Software
Offline zngga
« Reply #5 - Posted 2012-03-11 01:24:11 »

Correct me if I am wrong, but couldn't you just draw a 2D gui via the normal awt Graphics, directing its output to an image of some kind (BufferedImage perhaps) and simply paint it to the canvas after each render...?

My code never has bugs... it just develops unexpected features!
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-03-11 01:25:14 »

Yup, but this is not asking about Java2D Wink

Offline zngga
« Reply #7 - Posted 2012-03-11 02:07:46 »

Yup, but this is not asking about Java2D Wink
Well what I meant was do the gl rendering as normal, but simply create the gui this way... just to clarify.

My code never has bugs... it just develops unexpected features!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (21 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!