Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Single Minecraft-like tileset  (Read 1461 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Posted 2012-06-07 17:32:25 »

I am making a game with a grid based world.
I already have the code for running around and whatnot working.
However, there's something I want to learn how to do.

Different maps of my world require different tiles
So a beach might require sand tiles and palm tree tiles, while a forest might require grass and maple tree tiles
The thing is, sand tiles are functionally identical to grass tiles as a "floor", and palm trees are the same as maple trees as a "wall"
So I was wondering if there were ways to "clean up" some of my code

I know that in Minecraft, all of the block textures are derived from one picture

I assume that in the code, this singular picture is broken up into many different smaller textures
I'd like to do that

My grand idea is that I'd have one tileset for a beach, one for a forest, one for a dungeon etc.
So... how can I do this? How have people done it in the past?
(I'm using the standard LWJGL and Slick-Util)

Thanks
Offline davedes
« Reply #1 - Posted 2012-06-07 19:05:33 »

This is very easy, it just takes a bit of understanding of how OpenGL textures and texture coordinates work.

In short, if you're rendering a textured quad (a pretty standard way of drawing sprites using Slick-Util) then you will probably be specifying texture coordinates [0,1] or something similar. If you only want to render a region of the whole texture, you have to specify different texture coordinates for each vertex of your quad.

I'd suggest making yourself an Image or TextureRegion type of class. Example here:
http://www.java-gaming.org/topics/2d-tile-render/26211/msg/228935/view.html#msg228935

This is done not just for the sake of organization, but also performance. Texture binds are relatively expensive in OpenGL, so the fewer binds you need per frame, the more performant your game will be. LibGDX has some good texture packing tools; either for use offline (i.e. pack separate images into a single PNG file) or in game, on the fly (i.e. at startup, load and pack multiple images into the same OpenGL texture).

EDIT: Regarding your question of different tilesets for different environments; if you organize your sprite types to be in the same spot between sheets (i.e. ground type in cell (0, 0), tree type in cell (1, 0), etc) then you can use the same texture coordinates between sprite sheets. The only thing that needs to change is the texture you bind before rendering.

Offline davidllanos22

Junior Member


Projects: 2


I'm just a learner!


« Reply #2 - Posted 2012-06-07 22:04:57 »

If you are using slick is very simple:

Load your image and make a spritesheet:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Resources{
//definde image
private Image img;
//define spritesheet
public static SpriteSheet tiles;

public static void load(){
//load image (file location,image flipped?,filter)
img=new Image("data/images/tiles.png",false,Image.FILTER_NEAREST);
//load Spritesheet (image ,width of the tile,height of the tile)
tiles= new SpriteSheet(img,16,16);
}
}


Call it in the main class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public class Main extends BasicGame{
//blablabalbala

public void init(GameContainer gc){
Resources.load();
}

//blabalbalbala
}


And for draw one image of the spritesheet is very simple. Once you have loaded it you can call it from wherever you want:

1  
2  
3  
4  
public void render(Graphics g,GameContainer gc){
//x=xtile of the sheet y= ytile of the sheet
g.drawImage(Resources.tiles.getSubImage(x,y),0,0);
}


I hope it's understandable Smiley

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

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #3 - Posted 2012-06-08 03:29:09 »

Thanks to both of you! I spent some time, and looking at your guys' example code, I got it to work!
It also simplified my code tremendously
Thanks again
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.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (27 views)
2014-09-07 01:10:19

mitcheeb (35 views)
2014-09-04 23:08:59
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!