Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Why is it that when I make the object on my screen move, it leaves a trail?  (Read 610 times)
0 Members and 1 Guest are viewing this topic.
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Posted 2013-04-28 11:08:36 »

I'm just studying how to display images on a screen and make it move using a gamepad.

I've done that, but for some reason, when the object moves, it leaves a trail where it passed.

Anyone have any idea why? Here's my code. Thanks!

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  
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.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import net.java.games.input.*;

public class FuzzyMover {
   
   Controller[] ca;
   int gp=-1; // To store controller number of the gamepad
  private Texture texture; // The texture that will hold the image details
  float xMove=0, yMove=0, xPos=100,yPos=100;  
   
   public void start(){
     
      /* Get all controller instances attached to the machine */
      ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
     
      /* Get the name of each controller and pick the gamepad out of all controllers */
      for(int i =0;i<ca.length;i++){            
             System.out.println(ca[i].getName());
             System.out.println("Type: "+ca[i].getType().toString());                
             if(ca[i].getType().toString().indexOf("Gamepad")>-1){
                gp=i;
             }
       }
       
       initGL(800, 600); // initialize Open GL
      init(); // initialize other resources
     
       /* Do the following repeatedly until the app window is closed */
       while (!Display.isCloseRequested()) {      
           render();            
            pollInput();
            Display.update();
        }
       
        Display.destroy();      
   }
   
   private void initGL(int width, int height) {
      try {
         Display.setDisplayMode(new DisplayMode(width,height));
         Display.setLocation(43, 12);
         Display.create();
         Display.setVSyncEnabled(true);
      } catch (LWJGLException e) {
         e.printStackTrace();
         System.exit(0);
      }

      GL11.glEnable(GL11.GL_TEXTURE_2D);              
       
      GL11.glClearColor(255.0f, 255.0f, 255.0f, 255.0f);
       
      /* Enable alpha blending */
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);        
        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);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
   }
   
   public void render() {
      Color.white.bind();
      texture.bind();
           
      xPos=xPos+xMove;
      yPos=yPos+yMove;
     
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
      GL11.glBegin(GL11.GL_QUADS);
      GL11.glTexCoord2f(0,0);
      GL11.glVertex2f(xPos,yPos);
      GL11.glTexCoord2f(1,0);
      GL11.glVertex2f(xPos+texture.getImageWidth(),yPos);
      GL11.glTexCoord2f(1,1);
      GL11.glVertex2f(xPos+texture.getImageWidth(),yPos+texture.getImageHeight());
      GL11.glTexCoord2f(0,1);
      GL11.glVertex2f(xPos,yPos+texture.getImageHeight());
      GL11.glEnd();
   }
   
public void init() {      
      try {
         // load texture from PNG file
        texture = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("img/fuzzy.jpg"));

         System.out.println("Texture loaded: "+texture);
         System.out.println(">> Image width: "+texture.getImageWidth());
         System.out.println(">> Image height: "+texture.getImageHeight());
         System.out.println(">> Texture width: "+texture.getTextureWidth());
         System.out.println(">> Texture height: "+texture.getTextureHeight());
         System.out.println(">> Texture ID: "+texture.getTextureID());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   public void pollInput() {
      if(gp>-1){
            ca[gp].poll();
          EventQueue queue = ca[gp].getEventQueue();
             Event event = new Event();
   
             while(queue.getNextEvent(event)) {
                StringBuffer buffer = new StringBuffer(ca[gp].getName());
              buffer.append(" at ");
              buffer.append(event.getNanos()).append(", ");
              Component comp = event.getComponent();
              buffer.append(comp.getName()).append(comp.getIdentifier()).append(" changed to ");
              float value = event.getValue();
              if(comp.isAnalog()) {
                 buffer.append(value);
              } else {
                 if(value==1.0f) {
                    buffer.append("On");
                 } else {
                    buffer.append("Off");
                 }
              }
                         
              if(comp.getName().equals("X Axis")){
                 System.out.println(buffer.toString());
                 if(event.getValue()>0.1 || event.getValue()<-0.1)
                    xMove=event.getValue();
                 else
                    xMove=0;
              } else if(comp.getName().equals("Y Axis")){
                 if(event.getValue()>0.1 || event.getValue()<-0.1)
                    yMove=event.getValue();
                 else
                    yMove=0;
              }
             }
       }      
   }
   
   public static void main(String args[]){
      FuzzyMover fm = new FuzzyMover();
      fm.start();
   }
}

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #1 - Posted 2013-04-28 12:36:31 »

For what it's worth, this is what I'm talking about:

<a href="http://www.youtube.com/v/wkKDVMOttfM?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/wkKDVMOttfM?version=3&amp;hl=en_US&amp;start=</a>

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #2 - Posted 2013-04-28 13:19:19 »

Crap, sorry I just realized I should have posted this on the "Newbie & Debugging Questions" subforum... Sad

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-04-28 13:27:32 »

That's odd. The only thing I notice is that you're giving 0-255 values to glClearColor. OpenGL wants these in 0 - 1 range (always does if it asks for colours as floats). According to the docs however, this will just be clamped to 1 anyway so I'm really not sure what the problem could be.
Again from the docs:
Quote
The pixel ownership test, the scissor test, dithering, and the buffer writemasks affect the operation of glClear. The scissor box bounds the cleared region. Alpha function, blend function, logical operation, stenciling, texture mapping, and depth-buffering are ignored by glClear.
. But I can't see you doing any of that in the code.
Best to check if OpenGL is throwing any errors otherwise I am stumped.
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2013-04-28 13:34:48 »

That's odd. The only thing I notice is that you're giving 0-255 values to glClearColor. OpenGL wants these in 0 - 1 range (always does if it asks for colours as floats). According to the docs however, this will just be clamped to 1 anyway so I'm really not sure what the problem could be.

Even funnier... When it is clamped to 1, it should clear everything white, but it's not white... it's black...

Are you actually operating on the wrong framebuffer somehow? But I can't see anything like that happening... Huh

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #5 - Posted 2013-04-28 13:47:04 »

Sorry! I commented out this line for the video:

1  
//      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);


To better illustrate my problem with a black background.

With that line not commented out, the background is indeed white.

Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2013-04-28 13:48:13 »

Sorry! I commented out this line for the video:

1  
//      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);


To better illustrate my problem with a black background.

With that line not commented out, the background is indeed white.

OF COURSE your object leaves a trail when you don't clear the screen!!!
That is the purpose of glClear Cheesy

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #7 - Posted 2013-04-28 14:33:49 »

Good lord, what the hell was I thinking, I mixed and messed up things in my head.

Sorry for this thread, it was a total brain fart.

Emo

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.

ctomni231 (39 views)
2014-07-18 06:55:21

Zero Volt (36 views)
2014-07-17 23:47:54

danieldean (29 views)
2014-07-17 23:41:23

MustardPeter (32 views)
2014-07-16 23:30:00

Cero (47 views)
2014-07-16 00:42:17

Riven (48 views)
2014-07-14 18:02:53

OpenGLShaders (38 views)
2014-07-14 16:23:47

Riven (37 views)
2014-07-14 11:51:35

quew8 (33 views)
2014-07-13 13:57:52

SHC (70 views)
2014-07-12 17:50:04
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!