Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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 1421 times)
0 Members and 1 Guest are viewing this topic.
Offline bauerr

Junior Member




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


« Posted 2005-11-17 17: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 Member





« Reply #1 - Posted 2005-11-17 17: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: 15
Projects: 4
Exp: 14 years


Maximumisness


« Reply #2 - Posted 2005-11-18 01: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: 1


pixels! :x


« Reply #3 - Posted 2005-11-18 01: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 Member




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


« Reply #4 - Posted 2005-11-18 07: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 05: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.

xsi3rr4x (75 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

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