Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 1121 times)
0 Members and 1 Guest are viewing this topic.
Offline RylandAlmanza

Junior Member


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 Member


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 Member


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  
 
 
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.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (31 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
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!