Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
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  
  [Request] "full" lwjgl example  (Read 1296 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-05-21 06:32:24 »

Hello,
please can anyone post a full working example of a basic lwjgl class (drawing simple graphic,loading image, loading font)
cause every time i follow an example alone it works, but when i try to use them all i always face a problem.
thank you very much

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline quew8

JGO Coder


Medals: 29



« Reply #1 - Posted 2013-05-21 08:29:41 »

http://www.lwjgl.org/wiki/index.php?title=Space_Invaders_Example_Game
The wiki is always there, watching over us.
Offline alaslipknot
« Reply #2 - Posted 2013-05-21 08:40:54 »


i saw it, it's kinda too complicated and hard to follow  Undecided
am looking for a simple single class that render textures,fonts and simple graphic 

thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Coder


Medals: 29



« Reply #3 - Posted 2013-05-21 08:56:18 »

I am afraid that that is about as simple as a whole game can get. Texture loading see TextureLoader and Texture. Texture rendering see Sprite.

Frankly we could probably help you more by solving the problems you have when putting it all together since, as you said, these can be hard to follow.
Offline relminator
« Reply #4 - Posted 2013-05-21 09:06:34 »

Hello,
please can anyone post a full working example of a basic lwjgl class (drawing simple graphic,loading image, loading font)
cause every time i follow an example alone it works, but when i try to use them all i always face a problem.
thank you very much

Dunno if this is simple enough...

rel.phatcode.net/junk.php?id=140

Offline alaslipknot
« Reply #5 - Posted 2013-05-21 09:08:32 »

I am afraid that that is about as simple as a whole game can get. Texture loading see TextureLoader and Texture. Texture rendering see Sprite.

Frankly we could probably help you more by solving the problems you have when putting it all together since, as you said, these can be hard to follow.
my main problem was here and today i tried to apply the texture example on the game am currently working on and i faced more than one problem...

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline quew8

JGO Coder


Medals: 29



« Reply #6 - Posted 2013-05-21 09:22:32 »

Ah - I should have recognized the Pikachu - I recon the best ways to do text rendering is to use textures (or bitmaps) so get that sorted before you do anything else.

Basically for textures:

1)Load the texture (for now just use the example in LWJGL wiki)

2)Enable textures (glEnable(GL_TEXTURE_2D)) If you do any rendering without textures you must do this in render method and disable afterwords.

In your draw / render method.
2)bind the texture (glBindTexture)
3)give texture coordinates before giving vertex coords (glTexCoord2f before glVertex2f)

4)Disable textures if you intend to do some colour only rendering later.

Example
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  
//Imagine you texture id is texId, your square's position is (sx, sy) and its size is (sizeX, sizeY)

//This draws a single textured quad.
public void draw() {
     glEnable(GL_TEXTURE_2D);
     glBindTexture(texId);

     glBegin(GL_QUADS);

     glTexCoord2f(0, 0);
     glVertex2f(sx, sy);

     glTexCoord2f(1, 0);
     glVertex2f(sx+sizeX, sy);

     glTexCoord2f(1, 1);
     glVertex2f(sx+sizeX, sy+sizeY);

     glTexCoord2f(0, 1);
     glVertex2f(sx, sy+sizeY);

     glEnd();

     glDisable(GL_TEXTURE_2D);
}
Offline alaslipknot
« Reply #7 - Posted 2013-05-21 09:46:32 »

@quew8
i think i should reorder all what i know about openGL, i didn't know that i can enable/disable during drawing (glDisable(GL_TEXTURE_2D)Wink i thought all that must happens in the beginning once and for all and it could never be changed,
i'll try to implement this now and tell you if this fixed my problem, thank's a lot 

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline davedes
« Reply #8 - Posted 2013-05-21 12:32:46 »

Look through the minimal lwjgl-basics library:
https://github.com/mattdesl/lwjgl-basics

It renders sprites using vertex arrays and a basic sprite batcher. It uses object-oriented patterns to wrap basic OpenGL and LWJGL concepts:
Display
Texture
Shader Program

Using glVertex is deprecated and part of the old fixed-function pipeline. Shaders are much more powerful to use and not too difficult to learn. The hard part is putting all the OpenGL boilerplate together -- this is why most people would suggest using LibGDX or another solution.

EDIT: For example, you could learn how shaders work with a higher-level framework (lwjgl-basics, LibGDX). Then you will have a much greater understanding of the OpenGL pipeline. When it comes time that you want to create your own sprite batch (with LWJGL or LibGDX), you will know what you are doing, rather than just copy-pasting GL calls blindly. Wink

Offline quew8

JGO Coder


Medals: 29



« Reply #9 - Posted 2013-05-21 15:38:16 »

OpenGl is a state machine (at least so I keep getting told). You can't do anything state changing between glBegin / glEnd but any other time is fine.

BTW @davedes +1. Shaders and vbos are the way to go but I just think one step at a time. First lets get a textured quad on the screen before we destroy everything by using vbos and shaders (I kid they are very useful and fast)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #10 - Posted 2013-05-21 16:22:48 »

Shouldn't have chosen lwjgl if you're not willing to do the extra work, go low level and get your hands dirty. Copy+pasting isn't going to get you anywhere. Don't try to implement everything at once - do it one by one. If you try to implement everything at once there could be problems with any feature you're trying to implement. One by one, it's easier to find the problem.
Offline alaslipknot
« Reply #11 - Posted 2013-05-22 18:55:29 »

@davedes & quew8
thank you i'll try that
@Jimmt
the goal isn't copy-pasting and just reusing written codes by other people, i tried the one by one example and used them in other examples by my own, but the thing is am facing a problem everytime i try to use them all in the same time which is very weird or maybe that's how it looks to a very noob lwjgl learner like me, and thanks for the advise  Wink

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline Vermeer

JGO Coder


Medals: 16



« Reply #12 - Posted 2013-05-22 19:37:00 »

Hi, this may or may not help, but a lots of topics where cover in my post as I got to grips with lwjgl. On page 3 there is a link to source code. It's not great code, but you can take bits, or compare bits, however you want to use it. I have use bitmap fonts in there, and vbo rendering.

http://www.java-gaming.org/topics/lwjgl-blockworld/28866/view.html

Offline alaslipknot
« Reply #13 - Posted 2013-05-24 22:58:48 »

thank you guys, i will follow those advise, for now i want to say that my problem is solved (thanx to quew8 )
what i learned (please correct me if am wrong)
-we don't initialize openGL once for all the game life
-each rendering may or must have it's own initialization (enable/disable)
finally here is the code of an all-in-one example (like i requested)
again thank you all and if you found any mistake or a better way please tell me to correct it
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  
import java.awt.Font;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class AllInOneExample {

   /** The texture that will hold the image details */
   private Texture texture;
   /** The fonts to draw to the screen */
   private TrueTypeFont font;
   /**the coordinates of the drawn elements  */
   int x = 500, y = 100, w = 50, h = 50;
   

   /**
    * Start the example
    */

   public void start() {
      initGL(800, 600);
      init();

      while (true) {
         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
         render();

         Display.update();
         Display.sync(100);

         if (Display.isCloseRequested()) {
            Display.destroy();
            System.exit(0);
         }
      }
   }

   private void initGL(int width, int height) {
      try {
         Display.setDisplayMode(new DisplayMode(width, height));
         Display.create();
         Display.setVSyncEnabled(true);
      } catch (LWJGLException e) {
         e.printStackTrace();
         System.exit(0);
      }

      GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      GL11.glViewport(0, 0, width, height);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GL11.glOrtho(0, width, height, 0, 1, -1);

   }

   /**
    * Initialise resources
    */

   public void init() {

      try {
         // load texture from PNG file
        texture = TextureLoader.getTexture("PNG",
               ResourceLoader.getResourceAsStream("res/cube.png"));

      } catch (IOException e) {
         e.printStackTrace();
      }

      Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
      font = new TrueTypeFont(awtFont, false);

   }

   /**
    * draw a quad with the image on it
    */

   public void render() {

      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

      // draw quad
     x = 10;y = 10;w = 32;h = 32;
      GL11.glColor3d(250, 250, 250);
      GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex2f(x, y); // up left corner
     GL11.glVertex2f(x + w, y);// up right corner
     GL11.glVertex2f(x + w, y + h);// down right corner
     GL11.glVertex2f(x, y + h);// down left corner
     GL11.glEnd();
      // end of Quad

      // Texture
     x = 50;
      GL11.glEnable(GL11.GL_TEXTURE_2D);
      texture.bind(); // or GL11.glBind(texture.getTextureID());
     GL11.glBegin(GL11.GL_QUADS);
      GL11.glTexCoord2f(0, 0);
      GL11.glVertex2f(x, y);
      GL11.glTexCoord2f(1, 0);
      GL11.glVertex2f(x + texture.getTextureWidth(), y);
      GL11.glTexCoord2f(1, 1);
      GL11.glVertex2f(x + texture.getTextureWidth(),
            y + texture.getTextureHeight());
      GL11.glTexCoord2f(0, 1);
      GL11.glVertex2f(x, y + texture.getTextureHeight());
      GL11.glEnd();
      GL11.glDisable(GL11.GL_TEXTURE_2D);
      // end of texture

      // Font
     x = 90;
      GL11.glEnable(GL11.GL_BLEND);
      GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
      font.drawString(x, y, "FINALLY !! ", Color.white);
      GL11.glDisable(GL11.GL_BLEND);
      // end of Font
  }

   /**
    * Main Class
    */

   public static void main(String[] argv) {
      AllInOneExample all = new AllInOneExample();
      all.start();
   }
}

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
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.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
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!