Show Posts
|
|
Pages: [1]
|
|
2
|
Game Development / Newbie & Debugging Questions / Re: using a spritesheet vs using separate sprite images?
|
on: 2012-12-20 04:29:46
|
Since Java2D tries to use hardware acceleration under the hood, it's also wise to use sprite sheets and BufferedImage.getSubImage where possible as it may allow for better performance. With LWJGL, sprite sheets are pretty much essential if you plan to render many sprites per frame. It should really be step 1 of optimizing any OpenGL game. Step 2 would be creating a sprite batcher, instead of using old-school glBegin/glEnd calls everywhere. If you're interested, you should check out LibGDX's sprite batcher or my own minimal sprite API here. If you've organized your Texture wrapper well enough, then creating a sprite sheet utility should be easy as pie. See the TextureRegion source in LibGDX, for example. If your'e still struggling with basic OpenGL Texture concepts (of which texture coordinates is an essential part) then see here. I'm still really new to LWJGL and java game design in the first place, so I'm not 100% sure what a lot of this means! xD I've been trying to follow tutorials, however there seems to be a lack of good ones out there for LWJGL. There are some limited ones from thecodinguniverse on youtube and some others, but none that go into depth on sprites. Thank you for the info however, I'll make sure to keep it in mind when I learn more.
|
|
|
|
|
4
|
Game Development / Newbie & Debugging Questions / using a spritesheet vs using separate sprite images?
|
on: 2012-12-20 02:57:49
|
|
Hello, I've been working with lwjgl for a while, and I've had luck making characters move around and attaching sprites to them so they look like the chracter is moving (I haven't figured out how to do animation yet though) but I was having a lot of trouble with spritesheets after following some tutorials about using them. Is there a major benefit in speed or other benefit if you use a spritesheet over just separate images of the sprites (EG: player sprites, terrain sprite, etc all in different files)?
|
|
|
|
|
5
|
Game Development / Newbie & Debugging Questions / Sprite Sheets in lwjgl, unable to move?
|
on: 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); glBegin(GL_QUADS); glTexCoord2f(x2, y2); glVertex2f(posX, posY); glTexCoord2f(x, y2); glVertex2f(posX + width, posY); glTexCoord2f(x, y); glVertex2f(posX + width, posY + height); glTexCoord2f(x2, y); 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.
|
|
|
|
|
6
|
Game Development / Newbie & Debugging Questions / Using lwjgl to make a zelda style dungeon crawler
|
on: 2012-12-15 08:06:50
|
|
I'm making a small dungeon crawler game that is going to have movement similar to the old zelda games where one room or one section is loaded on the screen, the character can move around that section and fight monsters/open chests/ etc and not worry about the map moving, just the character. Then if they hit a doorway or a wall the current area image kind of gets "pushed" off the screen by the next room/area to be loaded. How would I go about doing this with lwjgl? Also we are debating using static images for all of the background items like walls, floors, etc and just having individual sprites for things like monsters chests etc. Is this a good way to go about it, or would a tile set be better?
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|