Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
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  
  Easy LibGDX sprite creator  (Read 1545 times)
0 Members and 1 Guest are viewing this topic.
Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Posted 2013-10-14 12:32:55 »

I've used this class to death in the last couple of my LibGDX projects, and it's been extremely helpful! I've even used it in a pure LWJGL project also, with some reworking. Here's the class:
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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
package com.nishu.balancedEnergy.entity;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;

public class SpriteStore {
     
   public static type getType(String tileType) {
      String str = tileType;
      switch (str) {
      case "Grass":
         return type.Grass;
      case "Base":
         return type.Base;
      case "Stone":
         return type.Stone;
      case "light":
         return type.Light;
      case "bluePlayer":
         return type.bluePlayer;
      case "buttonDefault":
         return type.buttonDefault;
      case "buttonClicked":
         return type.buttonClicked;
      case "bullet-test":
         return type.Bullet;
      case "hotBar":
         return type.hotBar;
      case "hotBarSelection":
         return type.hotBarSelection;
      case "void":
         return type.voidTile;
      case "flowerGrass":
         return type.flowerGrass;
      case "water":
         return type.Water;
      case "lava":
         return type.lava;
      default:
         return type.Grass;
      }
   }

   public static Sprite createSprite(String type) {
      type tiletype = getType(type);
      if (tiletype != null) {
         Sprite s = new Sprite(getTexLoc(tiletype));
         return s;
      }
      return null;
   }

   private static Texture getTexLoc(type TileType) {
      switch (TileType) {
      case Base:
         return new Texture(Gdx.files.internal("res/tiles/base.png"));
      case Grass:
         return new Texture(Gdx.files.internal("res/tiles/grass.png"));
      case voidTile:
         return new Texture(Gdx.files.internal("res/tiles/void.png"));
      case flowerGrass:
         return new Texture(Gdx.files.internal("res/tiles/flowerGrass.png"));
      case Water:
         return new Texture(Gdx.files.internal("res/tiles/water.png"));
      case lava:
         return new Texture(Gdx.files.internal("res/tiles/lava.png"));
      case Stone:
         return new Texture(Gdx.files.internal("res/tiles/stone.png"));
      case bluePlayer:
         return new Texture(Gdx.files.internal("res/players/playerBlue.png"));
      case buttonClicked:
         return new Texture(Gdx.files.internal("res/gui/buttonClicked.png"));
      case buttonDefault:
         return new Texture(Gdx.files.internal("res/gui/buttonDefault.png"));
      case Light:
         return new Texture(Gdx.files.internal("res/tiles/light.png"));
      case Bullet:
         return new Texture(Gdx.files.internal("res/bullets/bullet-test.png"));
      case hotBar:
         return new Texture(Gdx.files.internal("res/ui/hotBar.png"));
      case hotBarSelection:
         return new Texture(Gdx.files.internal("res/ui/selectionSquare.png"));
      default:
         break;
      }
      return null;
   }

   public static enum type {
      Grass, Base, Stone, Light, Water, flowerGrass, lava, voidTile, bluePlayer, buttonDefault, buttonClicked, Bullet, hotBar, hotBarSelection;
   }
}

Basically I sort through a bunch of types in an enum, and then create a new sprite with a texture based off of the enum type. There can be some modifications, but for now, its quick and easy to create a new sprite! For instance, I can create a bunch of tiles like this:
1  
      Sprite tile = SpriteStore.createSprite(type);

And the spritestore will create a new sprite and send the texture back with it! I can definitely do more with it, like sorting textures or using texture atlases, but I like it Smiley

Offline Phased
« Reply #1 - Posted 2013-10-14 12:40:17 »

with OpenGL, you want less binding calls, so loading multiple textures is inefficient as you want a sprite sheet to contain all images allowing for 1 binding call.

you also would want a texture that is global and can be used to grab the texture from one image, this will allow for only one image to be loaded for all tiles.

what you would want to return is really just the texture location within the single image.

in OpenGL the max width is a value of 1, and the min width is 0 and the same for height.

EDIT: fixed a slight mistake
Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #2 - Posted 2013-10-14 12:46:40 »

I know, honestly I do. I've been meaning to get around to it, but this has been fine for me for a while, so I thought I'd share it for other people to change and use if they want. I'm going to sort my draw calls soon so that only one texture is ever bound once, and I'll be using a spritesheet. Thanks for the feedback!

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

JGO Knight


Medals: 22
Projects: 3
Exp: 10-12 months


Gamer's Helmet


« Reply #3 - Posted 2013-10-15 01:13:22 »

I like this, I use a simple id system that returns the block when the number is called, but this is way better  Grin. I don't really use Switch/Case statements a lot in my code though

Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #4 - Posted 2013-10-15 01:35:21 »

I absolutely love switch statements, especially the String comparison, it makes the code seem cleaner and its so much easier to code with them. No problem, by the way! It needs a serious rework though to optimize it.

Offline Grunnt

JGO Wizard


Medals: 55
Projects: 9
Exp: 5 years


Complex != complicated


« Reply #5 - Posted 2013-10-15 09:28:24 »

I'm probably missing something, but what is precisely the point of this class? How does this work better than:
1  
Sprite tile = new Sprite(new Texture(Gdx.files.internal("res/tiles/"+tile+".png"));
? Why use all the switch statements and the enums?

Also, LibGDX has pretty nice support for asset management through the AssetManager class. This has the added bonus of allowing you to easily preload all textures and reuse textures that were loaded already.

Offline Danny02
« Reply #6 - Posted 2013-10-15 10:08:08 »

yeah, I'm wondering if you posted on purpose to hear that your code is bad?

Some comments:
  • using enums in your code instead of strings is good of course, but you are doing string->enum->string
  • checking a nonnull value for null
  • returning null
  • using switch case for enum lookup(never heard of valueOf("blubb")?)
  • using switch for enum->, just add a string field to the enum.
  • you have to cache the textures
  • just use the things libgdx provides for you
Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #7 - Posted 2013-10-15 11:39:26 »

Alright, I'm sorry I just wanted to post something that I had been, ahem, WORKING, on. It wasn't finished, and I know its not the most efficient, but I've used it with other libraries other than LibGDX too, so it is useful. Everyone doesn't need to come bash on my code because its inefficient, I already know, and openly state it, so come on.

Thank you for the specific things that are wrong with it though, I can use those to make the class better.

Offline actual

JGO Coder


Medals: 24



« Reply #8 - Posted 2013-10-15 21:15:55 »

If you want to go with an enum type approach another option is to attach the methods directly to the enum itself. Something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public enum Sprites {

   Rock("res/tiles/","rock.png"),
   Paper("res/tiles/","paper.png"),
   VoidTile("res/tiles/","void.png");
   
   
   private String imageFilePath;
   
   SpriteType(String path, String fileName) {
      imageFilePath = path + fileName;
   }
   
   
   public Sprite getSprite() {
      return new Sprite(new Texture(imageFilePath));
   }
}



Then you could call it by saying Sprite myRock = Sprites.Rock.getSprite();. This doesn't negate some of the other criticisms people have with the approach (caching, using pre-built solutions) but it is easier to use and doesn't involve multiple expanding switch statements.


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 (48 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (207 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!