Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  [SOLVED] Texture is flickering, help?  (Read 1035 times)
0 Members and 1 Guest are viewing this topic.
Offline tdegroot96

Junior Member


Projects: 1



« Posted 2012-10-31 19:52:52 »

My texture is flickering, it keeps showing up normal and after 5 seconds it starts to flicker again and then it shows up normal again. Is it my game loop or what is it?

Source code:
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  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Main {

   public boolean finished = false;
   Texture texture;
   float x = 50.0f;
   float y = 50.0f;

   public Main() {

   }

   private void init(boolean fullscreen) {
      try {

         Display.setTitle("Test LWJGL");
         Display.setFullscreen(fullscreen);
         Display.setDisplayMode(new DisplayMode(480, 600));
         Display.setVSyncEnabled(true);
         Display.create();

         texture = getTexture("texture");

         GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
         GL11.glMatrixMode(GL11.GL_MODELVIEW);

         GL11.glMatrixMode(GL11.GL_PROJECTION);
         GL11.glLoadIdentity();
         GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
         GL11.glMatrixMode(GL11.GL_MODELVIEW);
         GL11.glEnable(GL11.GL_TEXTURE_2D);
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
   }

   private Texture getTexture(String key) {
      try {
         return TextureLoader.getTexture("PNG", new FileInputStream(
               new File("res/img/" + key + ".png")));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return null;
   }

   public void start() {
      init(false);
      run();
   }

   public void run() {
      while (!finished) {
         Display.update();
         if (Display.isCloseRequested()) {
            finished = true;
         } else {
            tick();
            render();
         }
      }
   }

   private void render() {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

      draw();

      Display.update();
      Display.sync(60);
   }

   private void draw() {
      texture.bind();

      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();
   }

   private void tick() {

   }

   public static void main(String args[]) {
      Main main = new Main();
      main.start();
   }

}
Offline KittenKoder

Senior Member


Medals: 7



« Reply #1 - Posted 2012-10-31 20:13:51 »

For one thing, you should use tris and not quads, graphics cards prefer those. Also, if I remember correctly, you only bind a texture once, though I could be wrong, but I think that's your problem.

I am no one else.
Offline tdegroot96

Junior Member


Projects: 1



« Reply #2 - Posted 2012-10-31 20:19:52 »

This is the only texture I use. And Triangles just split it up. Or did you mean something else with tri?
<a href="http://www.youtube.com/v/pxiv6mqfrqs?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/pxiv6mqfrqs?version=3&amp;hl=en_US&amp;start=</a>
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KittenKoder

Senior Member


Medals: 7



« Reply #3 - Posted 2012-10-31 20:28:43 »

Triangle strip, basically.

I think I just realized what could be wrong, it's not the texture flickering but the entire scene. It's been so long since I coded raw GL that I forgot how to fix this, but I do remember having that same problem. There's a wait method for something that will fix it.

I am no one else.
Offline davedes
« Reply #4 - Posted 2012-10-31 20:33:29 »

Read the tutorials and wiki. There are a few things wrong:

- You call Display.update at the beginning of your loop, and then again during render(). You should only call this once at the end of your loop (before sync), and probably not inside your render() method. This is likely the cause of the flickering.
- You aren't using proper texcoords. See here.
- You are loading from a file, which won't work if you try packaging your resources into a JAR. You should be using MyGame.class.getResource().

Regarding triangles; true that the GPU will prefer triangles (since that's what it will be in the end), but that's the least of your worries. Right now you're using old-school immediate mode and fixed function; ideally you should be learning more modern OpenGL through shaders and VBOs. Understandably though, the "hello world" quad is a good starting point for newbies.

If you find these topics difficult to grasp, then you would be better off using a library like LibGDX which will cover the lower-level stuff for you.

Offline tdegroot96

Junior Member


Projects: 1



« Reply #5 - Posted 2012-10-31 20:43:17 »

Read the tutorials and wiki. There are a few things wrong:

- You call Display.update at the beginning of your loop, and then again during render(). You should call this at the end of your loop (before sync), and probably not inside your render() method.
- You aren't using proper texcoords. See here.
- You are loading from a file, which won't work if you try packaging your resources into a JAR. You should be using MyGame.class.getResource().

Regarding triangles; true that the GPU will prefer triangles (since that's what it will be in the end), but that's the least of your worries. Right now you're using old-school immediate mode and fixed function; ideally you should be learning more modern OpenGL through shaders and VBOs. Understandably though, the "hello world" quad is a good starting point for newbies.

If you find these topics difficult to grasp, then you would be better off using a library like LibGDX which will cover the lower-level stuff for you.
Thank you! It was the Display.update() which made this fault. I just started with OpenGL, but I have alot of experience in making 2D and 3D games with the built-in graphics class. Thanks for this file tip as well, I just made this messy piece of code in about 15 mins. And I know, it's messy.
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.

CogWheelz (15 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!