Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Create scrolling background with tilemap  (Read 1569 times)
0 Members and 1 Guest are viewing this topic.
Offline bauerr

Junior Devvie




Java!!!!!!!!!!! !!!


« Posted 2005-11-17 16:02:26 »

Hi,

I want create a scrolling background from a tilemap. What would be the best way of doing it:

- creating only the required section with size of the screen or
- creating the whole map as an Image and get the right position with g.drawImage(...)

I think the second option is the easiest to code, but I'm afraid to cause performance problems.  Undecided


Offline SluX

Junior Devvie





« Reply #1 - Posted 2005-11-17 16:25:19 »

U should do the first option. As u get into code, u ll see that it isnt difficult.

Second option would consume too much memory...

I could post some code if u need it.

"Intelligence is the most beautiful gift and the greatest temptation which one life can receive from the gods."Me Cheesy
Play strategic football
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #2 - Posted 2005-11-18 00:24:53 »

As these things go, a clean way to do this is to first design your interface of a TileMap and implement it the easiest way and later, if needed, you can always create a more optimized implementation and simply switch to your new implementation without otherwise changing your game code:

TileMap bgScroll = new SimpleTileMap();
can be then changed to
TileMap bgScroll = new OptimizedTileMap();
were TileMap is an abstract interface, and the SimpleTileMap and OptimizedTileMap are the implementations.

However, I agree with SluX that you'll probably find that your option 1) is not that difficult, but if you design things properly, optimizations like this can be integrated in your code cleanly.
I would just create an array of tile references, and only draw the visible tiles.

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

JGO Coder


Medals: 2


pixels! :x


« Reply #3 - Posted 2005-11-18 00:57:53 »

This code looks a bit different... well, you can use it as some kind of pseudo code reference Wink

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  
import com.kaioa.y3.game.*;
import com.kaioa.y3.ext.*;
import java.util.*;


public class TestModuleBsh extends Game {
   int MAP_W=10; //usually bigger + dynamic
   int MAP_H=10; //usually bigger + dynamic
   int SCREEN_W=256; //usually bigger
   int SCREEN_H=256; //usually bigger
   int TILE_W=32;
   int TILE_H=32;
   int TILES_X=SCREEN_W/TILE_W;
   int TILES_Y=SCREEN_H/TILE_H;
   int MAX_WX=MAP_W*TILE_W-TILES_X*TILE_W-1;
   int MAX_WY=MAP_H*TILE_H-TILES_Y*TILE_H-1;

   int [][]map=new int[MAP_W][MAP_H];
   int wx=0;
   int wy=0;

   TGATextureManager gfx=TGATextureManager.getInstance();
   Texture []set;

   public void init() {
      set=gfx.addSlicesWH("set2.tga",32,32);
      Random gen=new Random();
      for(int x=0;x<MAP_W;x++)
         for(int y=0;y<MAP_H;y++)
            map[x][y]=gen.nextInt(6); //usually this wouldnt be random
   }

   public void tick() {
      wx+=Controls.simpleX()*4;
      wy+=Controls.simpleY()*4;
   }

   public void render() {
      GTool.clear(); //dont clear the screen if you overdraw everything anyways
      GTool.setColor(1f,1f,1f);

      //clap world coords
      if(wx<0)wx=0;
      if(wy<0)wy=0;
      if(wx>MAX_WX)wx=MAX_WX;
      if(wy>MAX_WY)wy=MAX_WY;

      //find the array offsets
      int ox=wx/32;
      int oy=wy/32;

      for(int x=0;x<TILES_X+1;x++)
         for(int y=0;y<TILES_Y+1;y++)
            GTool.drawImage(set[map[ox+x][oy+y]],x*TILE_W-wx%TILE_W,y*TILE_H-wy%TILE_H);

      GTool.setColor(0f,0f,1f);
      GTool.va.print("wx:"+wx+" wy:"+wy,20,20);
   }
}


Thats basically everything there is to it. You load the tiles, you load the 2d map array, then you draw those tiles which are on the screen. Eg if the resolution is 640x480 and the tiles are 32x32 then 20x15 tiles are fitting on the screen... if you scroll a bit right/down you'll have a gap there. Therefore you would need to draw 21x16 instead to be sure that the whole screen is filled.

弾幕 ☆ @mahonnaiseblog
Offline bauerr

Junior Devvie




Java!!!!!!!!!!! !!!


« Reply #4 - Posted 2005-11-18 06:52:20 »

O.k.. I thought that it would be better to do it the first way.

Thank you all. Grin
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2005-11-22 04:47:59 »

This also dependson what the rest of your render logic is,

if this is a true tile-based game then you will need to re-render sub-sections at least on every frame as your characters move in order for the ordering to work out.  (You could theoretically spend a lot of logic clipping the characters v. other tiles but I doubt you'ld end up ahead of the game v. an efficient VRAM based pure tile engine.)

 

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
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.

rwatson462 (35 views)
2014-12-15 09:26:44

Mr.CodeIt (26 views)
2014-12-14 19:50:38

BurntPizza (59 views)
2014-12-09 22:41:13

BurntPizza (94 views)
2014-12-08 04:46:31

JscottyBieshaar (54 views)
2014-12-05 12:39:02

SHC (70 views)
2014-12-03 16:27:13

CopyableCougar4 (72 views)
2014-11-29 21:32:03

toopeicgaming1999 (132 views)
2014-11-26 15:22:04

toopeicgaming1999 (123 views)
2014-11-26 15:20:36

toopeicgaming1999 (34 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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