Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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 1999 times)
0 Members and 1 Guest are viewing this topic.
Offline RylandAlmanza

Junior Devvie


Medals: 3



« Posted 2012-02-28 21: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 00: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 Devvie


Medals: 3



« Reply #2 - Posted 2012-02-29 01: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!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2012-02-29 08: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 Devvie


Medals: 3



« Reply #4 - Posted 2012-02-29 09: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  
 
 

 
Ecumene (150 views)
2017-09-30 02:57:34

theagentd (225 views)
2017-09-26 18:23:31

cybrmynd (302 views)
2017-08-02 12:28:51

cybrmynd (290 views)
2017-08-02 12:19:43

cybrmynd (298 views)
2017-08-02 12:18:09

Sralse (291 views)
2017-07-25 17:13:48

Archive (979 views)
2017-04-27 17:45:51

buddyBro (1103 views)
2017-04-05 03:38:00

CopyableCougar4 (1680 views)
2017-03-24 15:39:42

theagentd (1430 views)
2017-03-24 15:32: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
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!