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  
  Sprite Sheets in lwjgl, unable to move?  (Read 566 times)
0 Members and 1 Guest are viewing this topic.
Offline kynian

Junior Newbie





« Posted 2012-12-16 03:27:04 »

Hello I'm learning and playing around with LWJGL, and I was following the tutorial by the guy from thecodinguniverse on youtube (specifically episode 29 about how to load from a sprite sheet) and it worked, I got my sprite loaded, and even found out how to get it facing the right way and switching sprites when you press the A key. I'm trying to figure out how I would go about making the sprite move as well. I was looking at other tutorials that made sprites move around (such as the LWGJL rpg tutorial on youtube) and I could easily get the little "player" from that to move, but I cannot seem to get this one to move at all. Here is the code I have so far:

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  
package com.dipndawtz.ld25;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import com.dipndawtz.ld25.Util.ImageTools;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.lwjgl.opengl.ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB;
import static org.lwjgl.opengl.GL11.*;

public class Main {
   private int x;
   private int y;
   private static int posX;
   private static int posY;

   private static final String WINDOW_TITLE = "NecroLock";
   private static final int[] WINDOW_DIMENSIONS = { 640, 480 };

   private static int spritesheet;
   private static Map<String, Sprite> spriteMap = new HashMap<String, Sprite>();
   private static Sprite currentSprite;
   private static final String SPRITESHEET_IMAGE_LOCATION = "res/spritesheet.png";
   private static final String SPRITESHEET_XML_LOCATION = "res/spritesheet.xml";

   public Main() {
      setUpDisplay();
      setUpSpriteSheets();
      setUpStates();
      setUpMatrices();
      enterGameLoop();
      cleanUp(false);
   }

   private static void render() {
      glClear(GL_COLOR_BUFFER_BIT);

      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, spritesheet);

      int x = currentSprite.getX();
      int y = currentSprite.getY();
      int x2 = currentSprite.getX() + currentSprite.getWidth();
      int y2 = currentSprite.getY() + currentSprite.getHeight();
      posX = Display.getWidth() / 2;
      posY = Display.getHeight() / 2;
      int width = currentSprite.getWidth();
      int height = currentSprite.getHeight();
      System.out.println(posX + ", " + posY);
      // 28x42
     glBegin(GL_QUADS);
      glTexCoord2f(x2, y2);
      // glVertex2f(-0.1f, 0.1f);
     glVertex2f(posX, posY);
      glTexCoord2f(x, y2);
      // glVertex2f(-0.1f, -0.1f);
     glVertex2f(posX + width, posY);
      glTexCoord2f(x, y);
      // glVertex2f(0.1f, -0.1f);
     glVertex2f(posX + width, posY + height);
      glTexCoord2f(x2, y);
      // glVertex2f(0.1f, 0.1f);
     glVertex2f(posX, posY + height);
      glEnd();

      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
   }

   private static void logic() {

   }

   private void input() {

      if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
         currentSprite = spriteMap.get("fl.png");

      }

   }

   private static void cleanUp(boolean asCrash) {
      glDeleteTextures(spritesheet);
      Display.destroy();
      System.exit(asCrash ? 1 : 0);
   }

   private static void setUpMatrices() {
      glMatrixMode(GL_PROJECTION);
      glOrtho(0, 0, 640, 640, -1, 1);
      glMatrixMode(GL_MODELVIEW);
   }

   private static void setUpSpriteSheets() {
      spritesheet = ImageTools.glLoadLinearPNG(SPRITESHEET_IMAGE_LOCATION);
      SAXBuilder builder = new SAXBuilder();
      try {
         Document document = builder.build(new File(SPRITESHEET_XML_LOCATION));
         Element root = document.getRootElement();
         for (Object spriteObject : root.getChildren()) {
            Element spriteElement = (Element) spriteObject;
            String name = spriteElement.getAttributeValue("n");
            int x = spriteElement.getAttribute("x").getIntValue();
            int y = spriteElement.getAttribute("y").getIntValue();
            int w = spriteElement.getAttribute("w").getIntValue();
            int h = spriteElement.getAttribute("h").getIntValue();
            Sprite sprite = new Sprite(name, x, y, w, h);
            spriteMap.put(sprite.getName(), sprite);
         }
      } catch (JDOMException e) {
         e.printStackTrace();
         cleanUp(true);
      } catch (IOException e) {
         e.printStackTrace();
         cleanUp(true);
      }
      currentSprite = spriteMap.get("fc.png");
   }

   private static void setUpStates() {
      glEnable(GL_TEXTURE_RECTANGLE_ARB);
      glEnable(GL_CULL_FACE);
      glCullFace(GL_BACK);
      currentSprite = spriteMap.get("fc.png");
   }

   private static void update() {
      Display.update();
      Display.sync(60);
   }

   private void enterGameLoop() {
      while (!Display.isCloseRequested()) {
         render();
         logic();
         input();
         update();
      }
   }

   private static void setUpDisplay() {
      try {
         Display.setDisplayMode(new DisplayMode(WINDOW_DIMENSIONS[0], WINDOW_DIMENSIONS[1]));
         Display.setTitle(WINDOW_TITLE);
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
         cleanUp(true);
      }
   }

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

}

The imageTools class,
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  
package com.dipndawtz.ld25.Util;

import de.matthiasmann.twl.utils.PNGDecoder;
import org.lwjgl.BufferUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import static org.lwjgl.opengl.ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB;
import static org.lwjgl.opengl.GL11.*;

public class ImageTools {
   public static int glLoadLinearPNG(String location) {
      int texture = glGenTextures();
      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
      InputStream in = null;
      try {
         in = new FileInputStream(location);
         PNGDecoder decoder = new PNGDecoder(in);
         ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
         decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
         buffer.flip();
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
         glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
      } catch (FileNotFoundException e) {
         System.err.println("Texture file could not be found.");
         return -1;
      } catch (IOException e) {
         System.err.print("Failed to load the texture file.");
         return -1;
      } finally {
         if (in != null) {
            try {
               in.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
      return texture;
   }

   public static int glLoadPNG(String location) {
      int texture = glGenTextures();
      glBindTexture(GL_TEXTURE_2D, texture);
      InputStream in = null;
      try {
         in = new FileInputStream(location);
         PNGDecoder decoder = new PNGDecoder(in);
         ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
         decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
         buffer.flip();
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
      } catch (FileNotFoundException e) {
         e.printStackTrace();
         return -1;
      } catch (IOException e) {
         e.printStackTrace();
         return -1;
      } finally {
         if (in != null) {
            try {
               in.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
      glBindTexture(GL_TEXTURE_2D, 0);
      return texture;
   }
}

and the sprite class
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  
package com.dipndawtz.ld25;

public final class Sprite {

   private final String name;
   private final int x;
   private final int y;
   private final int w;
   private final int h;

   public Sprite(String name, int x, int y, int w, int h) {
      this.name = name;
      this.x = x;
      this.y = y;
      this.w = w;
      this.h = h;
   }

   @Override
   public boolean equals(Object o) {
      if (this == o)
         return true;
      if (o == null || getClass() != o.getClass())
         return false;

      Sprite sprite = (Sprite) o;

      if (h != sprite.h)
         return false;
      if (w != sprite.w)
         return false;
      if (x != sprite.x)
         return false;
      if (y != sprite.y)
         return false;
      if (name != null ? !name.equals(sprite.name) : sprite.name != null)
         return false;

      return true;
   }

   @Override
   public int hashCode() {
      int result = name != null ? name.hashCode() : 0;
      result = 31 * result + x;
      result = 31 * result + y;
      result = 31 * result + w;
      result = 31 * result + h;
      return result;
   }

   @Override
   public String toString() {
      return "Sprite{" + "name='" + name + '\'' + ", x=" + x + ", y=" + y + ", w=" + w + ", h=" + h + '}';
   }

   public String getName() {
      return new String(name);
   }

   public int getX() {
      return x;
   }

   public int getY() {
      return y;
   }

   public int getWidth() {
      return w;
   }

   public int getHeight() {
      return h;
   }
}


in the commented out lines of Main you can see what code originally got it showing the sprite in the first place, but when I tried to give it a new variable instead of those constants it wouldn't draw at all. Any help would be appreciated.
Offline SkyAphid
« Reply #1 - Posted 2012-12-18 04:48:11 »

That code looks pretty long and I don't have the time to read it all, but if you need it to move, you can just push the matrix, use loadIdentity and center the sprite at 0,0; then translate to where you want it. Something like this:

1  
2  
3  
4  
5  
glPushMatrix();
glLoadIdentity();
glTranslatef(x,y,1f);
drawSprite(x,y)
glPopMatrix();


Have the actual camera in the render/update method move to where you want it and you'll see the player where he's been placed. I suggest always doing it like this, because if you translate the matrix to the player so that he's the center, you'll have more control over him later with rotations and such.

If it's in the case of controls:

1  
if (Keyboard.keyDown(Keyboard.KEY_RIGHT) x++;


If that's not the problem, and you don't know how to go about animating the sprite itself, try to do something like this:

1  
2  
if (animationNext < 100) animationNext++; else animation++; animationNext = 0;
drawSprite(x,y,animation);


If you elaborate on your problem more I'll try to be more help, I know this is all basic stuff but I don't really understand what your problem is.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
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 (36 views)
2014-07-18 06:55:21

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

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

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

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

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

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

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

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

SHC (67 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!