Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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 from Sprite Sheet LWJGL  (Read 3161 times)
0 Members and 1 Guest are viewing this topic.
Offline PixelDeBurner

Junior Member


Medals: 1



« Posted 2012-09-03 15:58:02 »

For example I have 128px wide sprite sheet, with 32x32 sprites on it. How do I get only one sprite ?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2012-09-03 16:04:00 »

http://www.glprogramming.com/red/chapter09.html

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Buzzyboy
« Reply #2 - Posted 2012-09-10 02:21:19 »

For example I have 128px wide sprite sheet, with 32x32 sprites on it. How do I get only one sprite ?

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  
   public static BufferedImage[][] divideImage2D(BufferedImage img,int divideWidth,int divideHeight) {
      try {
      int a=img.getWidth()/divideWidth,b=img.getHeight()/divideHeight;
      BufferedImage[][] images=new BufferedImage[b][a];
      for(int they=0;they<b*divideHeight;they+=divideHeight) {
      for(int thex=0;thex<a*divideWidth;thex+=divideWidth) {
         images[they/divideHeight][thex/divideWidth]=img.getSubimage(thex,they,divideWidth,divideHeight);
      }}
      return images;
      }catch(Exception e) {e.printStackTrace();return null;}
   }
   public static BufferedImage[] divideImage(BufferedImage img,int divideWidth,int divideHeight) {
      try {
      int a=img.getWidth()/divideWidth,b=img.getHeight()/divideHeight;
      BufferedImage[] images=new BufferedImage[a*b];
      int i=0,thex=0;
      for(int they=0;i<images.length;) {
         if(i<a*b+1){
         images[i]=img.getSubimage(thex,they,divideWidth,divideHeight);
         i++;thex+=divideWidth;
         if(thex==img.getWidth()) {
            they+=divideHeight;
            thex=0;
         }}
      }
      return images;
      }catch(Exception e) {e.printStackTrace();return null;}
   }

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #3 - Posted 2012-09-12 20:46:31 »

For example I have 128px wide sprite sheet, with 32x32 sprites on it. How do I get only one sprite ?

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  
   public static BufferedImage[][] divideImage2D(BufferedImage img,int divideWidth,int divideHeight) {
      try {
      int a=img.getWidth()/divideWidth,b=img.getHeight()/divideHeight;
      BufferedImage[][] images=new BufferedImage[b][a];
      for(int they=0;they<b*divideHeight;they+=divideHeight) {
      for(int thex=0;thex<a*divideWidth;thex+=divideWidth) {
         images[they/divideHeight][thex/divideWidth]=img.getSubimage(thex,they,divideWidth,divideHeight);
      }}
      return images;
      }catch(Exception e) {e.printStackTrace();return null;}
   }
   public static BufferedImage[] divideImage(BufferedImage img,int divideWidth,int divideHeight) {
      try {
      int a=img.getWidth()/divideWidth,b=img.getHeight()/divideHeight;
      BufferedImage[] images=new BufferedImage[a*b];
      int i=0,thex=0;
      for(int they=0;i<images.length;) {
         if(i<a*b+1){
         images[i]=img.getSubimage(thex,they,divideWidth,divideHeight);
         i++;thex+=divideWidth;
         if(thex==img.getWidth()) {
            they+=divideHeight;
            thex=0;
         }}
      }
      return images;
      }catch(Exception e) {e.printStackTrace();return null;}
   }


This would work for regular java2D, but how to use BufferedImage in LWJGL ?
Offline davedes
« Reply #4 - Posted 2012-09-12 23:28:17 »

See here

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 132
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2012-09-13 05:17:05 »

Not (really) related to LWJGL but the basic concept of sprite sheets is that you divide the image into each particular sprite (for example diving the sheet into 16x16 squares because your sprites are that size). Then you would load the texture regions into an array/list of some sort, and to access something you would just request an index of that array. Pretty intuitive, and it's always good to know what's going on in the background.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #6 - Posted 2012-09-13 06:14:06 »

In LibGDX it's super simple, goes something like this. 

1  
2  
3  
4  
TextureRegion[][] tiles = TextureRegion.split(new Texture(Gdx.files.internal("tiles/sheet.png")), tileWidth, tileHeight);
...
TextureRegion foo = tiles[row][col]; // notice it's row/col: y first, from upper left!
foo.draw(batch, x,y);


Offline R.D.

Senior Member


Medals: 2
Projects: 1


"For the last time, Hats ARE Awesome"


« Reply #7 - Posted 2012-09-13 08:36:27 »

In LibGDX it's super simple, goes something like this. 

1  
2  
3  
4  
TextureRegion[][] tiles = TextureRegion.split(new Texture(Gdx.files.internal("tiles/sheet.png")), tileWidth, tileHeight);
...
TextureRegion foo = tiles[row][col]; // notice it's row/col: y first, from upper left!
foo.draw(batch, x,y);




I made a wrapper class for that (mostly because I wanted to access tilesize, tilesacross and tilesdown without using the length parameter. also I can access it via
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #8 - Posted 2012-09-17 04:59:43 »

I've never used a spritesheet arranged in rows and columns, I use packed images. You can use TexturePacker for that (and optionally whitespace strips and/or rotates for better packing). This tool is run offline and produces a large image containing all the smaller ones and a text file describing each small image location in the large image. You can use this directly with libgdx via the TextureAtlas class or write something to parse the text file if not using libgdx. The bin packing TexturePacker does is pretty sophisticated, but it is very easy to use. It is as easy or easier than arranging things in rows and columns. I usually have sprites as Photoshop or Illustrator layers, which makes aligning frames easy, and then export them to individual images with a script. The packer can be run as part of your game during development, so the packed images are always up to date. Then your workflow is just: save images, run app.

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.

Grunnt (18 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (105 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!