Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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  
  Global Texture  (Read 603 times)
0 Members and 1 Guest are viewing this topic.
Offline RylandAlmanza

Junior Member


Medals: 3



« Posted 2012-02-28 22:25:48 »

So I've got a large image of several sprites. Instead of loading that whole texture in every single class that uses a sprite from the image, I'd like to load it once at the beginning of my program, and have it accessible by all classes. How might I go about doing this? (I'm using the slick-util library for textures.)
Offline davedes
« Reply #1 - Posted 2012-02-29 01:33:48 »

Here are two common/easy ways. One would be to rely on static getters or singleton instances. You can extend this however you see fit; e.g. storing your textures by a String identifier in a hash map.
1  
2  
3  
4  
5  
6  
7  
8  
MyEntity {
    Texture texture;

    public MyEntity() {
        //gets the already-loaded texture
       this.texture = MyResources.getInstance().getMyTexture();
    }
}


Another common solution is to pass the single texture to the entity when you create it:
1  
2  
3  
4  
5  
6  
7  
MyEntity {
    Texture texture;

    public MyEntity(Texture texture) {
        this.texture = texture;
    }
}


To draw a "sub image" of a texture (i.e. render a particular sprite in a sheet), you need to define your texcoords properly. With slick-util, you might define an Image2D/Sprite class that holds the normalized texture offsets and width/height that will be passed to glTexCoord2f:
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  
   public Image2D(Texture texture) {
      this.texture = texture;
      this.normalizedWidth = texture.getWidth();
      this.normalizedHeight = texture.getHeight();
      this.width = texture.getImageWidth();
      this.height = texture.getImageHeight();
      this.centerX = width / 2f;
      this.centerY = height / 2f;
   }

   public Image2D getSubImage(float x, float y, float width, float height) {
      float tx = ( x / this.width * normalizedWidth ) + textureOffsetX;
      float ty = ( y / this.height * normalizedHeight ) + textureOffsetY;
      float tw = width / this.width * normalizedWidth;
      float th = height / this.height * normalizedHeight;
     
      Image2D img = copy();
      img.textureOffsetX = tx;
      img.textureOffsetY = ty;
      img.width = width;
      img.height = height;
      img.normalizedWidth = tw;
      img.normalizedHeight = th;
      img.centerX = width / 2f;
      img.centerY = height / 2f;
      return img;
   }

Offline RylandAlmanza

Junior Member


Medals: 3



« Reply #2 - Posted 2012-02-29 02:15:04 »

I already knew how to divide the texture, I just needed to know how to pass it between classes. Anyways, you answered my question. Much appreciated!
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline pitbuller
« Reply #3 - Posted 2012-02-29 09:59:49 »

It might heresy for some but just slap those on public static fields on some place.
Work everytime without any effort.
Offline RylandAlmanza

Junior Member


Medals: 3



« Reply #4 - Posted 2012-02-29 10:03:00 »

It might heresy for some but just slap those on public static fields on some place.
Work everytime without any effort.
That's what I tried initially. I can't remember why it didn't work, and I'm too tired to try it again (It's 1:00 am here.) I'll try it when I wake up, and let you know how it goes. Smiley Thanks!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (100 views)
2013-05-17 21:29:12

alaslipknot (108 views)
2013-05-16 21:24:48

gouessej (138 views)
2013-05-16 00:53:38

gouessej (133 views)
2013-05-16 00:17:58

theagentd (145 views)
2013-05-15 15:01:13

theagentd (131 views)
2013-05-15 15:00:54

StreetDoggy (174 views)
2013-05-14 15:56:26

kutucuk (196 views)
2013-05-12 17:10:36

kutucuk (198 views)
2013-05-12 15:36:09

UnluckyDevil (204 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.146 seconds with 20 queries.