Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (761)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (845)
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 sheet in LWJGL  (Read 3108 times)
0 Members and 1 Guest are viewing this topic.
Offline masteryoom

JGO Coder

Medals: 5
Projects: 2

If you look closely, you might see it turning...

« Posted 2012-11-23 06:16:36 »

How would you select different pictures in a sprite sheet to put on the screen with LWJGL?

Offline Grunnt

JGO Kernel

Medals: 143
Projects: 8
Exp: 5 years

Complex != complicated

« Reply #1 - Posted 2012-11-23 10:28:24 »

Your question is a bit generic: do you have code examples of what you are trying to do?

You may consider using Slick2D or LibGDX, which both support easy-to-use sprite sheets out of the box.

Offline Varkas
« Reply #2 - Posted 2012-11-23 11:21:23 »

Maybe I misunderstood the question, but isn't that just done by setting the texture coordinates so that the right area from the sprite sheet is put as texture on the sprite?

if (error) throw new Brick(); // Blog (german):
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

« Reply #3 - Posted 2012-11-23 14:52:47 »

So ... actually it's pretty easy.

You've got a SpriteSheet, which has dimensions width and height (in pixels).
Usually for Texture coordinates you use these:
1st Vertex: x: 0; y: 0
1st Vertex: x: 1; y: 0
1st Vertex: x: 1; y: 1
1st Vertex: x: 0; y: 1

We could replace this by some variables: lowX, lowY, highX and highY:
1st Vertex: x: lowX; y: lowY
1st Vertex: x: highX; y: lowY
1st Vertex: x: highX; y: highY
1st Vertex: x: lowX; y: highY

To get the above example, we would use 1 for high and 0 for low.

Sooo, to draw "Sprites" from the SpriteSheet, you'd just change your high and low values.
If you've got a spritesheet which is 512 pixels of width and 512 pixels of height, and you want the Sprite in the SpriteSheet at position x: 32; y: 32 and width: 64; height: 64, then you'd easily use this:

sprite.lowX = sprite.pixelX / sheet.width;
sprite.lowY = sprite.pixelY / sheet.height;
sprite.highX = sprite.lowX + (sprite.pixelWidth / sheet.width);
sprite.highY = sprite.lowY + (sprite.pixelHeight / sheet.height);

It's pretty easy actually.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
  ignore  |  Print  

EgonOlsen (351 views)
2018-06-10 19:43:48

EgonOlsen (344 views)
2018-06-10 19:43:44

EgonOlsen (292 views)
2018-06-10 19:43:20

DesertCoockie (515 views)
2018-05-13 18:23:11

nelsongames (843 views)
2018-04-24 18:15:36

nelsongames (829 views)
2018-04-24 18:14:32

ivj94 (1251 views)
2018-03-24 14:47:39

ivj94 (395 views)
2018-03-24 14:46:31

ivj94 (1058 views)
2018-03-24 14:43:53

Solater (413 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!