Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 2806 times)
0 Members and 1 Guest are viewing this topic.
Offline PixelDeBurner

Junior Member


Medals: 1



« Posted 2012-09-03 17: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: 51
Projects: 11


Monkey for a head


« Reply #1 - Posted 2012-09-03 18: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

Senior Newbie


Medals: 2
Exp: 6 years



« Reply #2 - Posted 2012-09-10 04: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 22: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-13 01:28:17 »

See here

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 117
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2012-09-13 07: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
« Reply #6 - Posted 2012-09-13 08: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 10: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: 129
Projects: 3
Exp: 14 years


Esoteric Software


« Reply #8 - Posted 2012-09-17 06: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.

xsi3rr4x (62 views)
2014-04-15 18:08:23

BurntPizza (60 views)
2014-04-15 03:46:01

UprightPath (73 views)
2014-04-14 17:39:50

UprightPath (56 views)
2014-04-14 17:35:47

Porlus (73 views)
2014-04-14 15:48:38

tom_mai78101 (99 views)
2014-04-10 04:04:31

BurntPizza (159 views)
2014-04-08 23:06:04

tom_mai78101 (254 views)
2014-04-05 13:34:39

trollwarrior1 (208 views)
2014-04-04 12:06:45

CJLetsGame (215 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!