
-----------------------------------
romantic_programmer
Wed Oct 04, 2006 12:44 pm

Tile Mapping Question
-----------------------------------
OK, I have 260 tiles... 

var mapTile : array 0 .. 249 of int

%% Null Tile
mapTile (0) := Pic.FileNew ("resources/tilesets/null.bmp")

%%Main Tiles
mapTile (1) := Pic.FileNew ("resources/tilesets/land/land/main/1.bmp")
mapTile (2) := Pic.FileNew ("resources/tilesets/land/land/main/2.bmp")

Etc..


Now How would i Go about getting this to be displayed. so it will show the mapTiles...


here is an ex of my .txt file   

0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0


**I jsut need this so i can start designing levels...

-----------------------------------
romantic_programmer
Wed Oct 04, 2006 12:45 pm


-----------------------------------
sry for the double repost....  i have 250 tiles...  and i still cant figure how to go about drawing the tiles to produce a map...

-----------------------------------
NikG
Wed Oct 04, 2006 7:16 pm


-----------------------------------
Use the code /code tags when posting... er... code.

Not 100% sure what you're asking, but my guess is that you have a txt file (map) and you have the different tiles (250 of them), and the #s in the txt file correspond to the tile you want.  Am I right?

-Load your map file into a 2d array
-Go through each row and column in the array and draw the tile depending on what the number is:for y:1..6
  for x:1..10
    Pic.Draw(Map(x,y),x*15,y*15,picCopy)
  end for
end for(UNTESTED)

Replace the *15 with however big your tiles are.

-----------------------------------
romantic_programmer
Thu Oct 05, 2006 9:53 am


-----------------------------------
The only problem with


for y:1..6 
  for x:1..10 
    Pic.Draw(Map(x,y),x*15,y*15,picCopy) 
  end for 
end for

is that it will repeat the same image. Which lets me have no control over my map... i somw how need to to read the txt files.  

for y:1..15 
  for x:1..25 
    Pic.Draw(Get it to read the txt file here,x*32,y*32,picCopy) 
  end for 
end for


and like you said your code was untested... but then again so you know... it will only show one tile and display it repeatedly according to what the for statement is set to...

-----------------------------------
do_pete
Thu Oct 05, 2006 9:56 am


-----------------------------------
You should start your for loop from 0 so that the bottom left of the screen gets drawn. And this will not draw the same image on the screen because you have your pictures in a 2d array and are drawing the picture Map(x,y)[/code]

-----------------------------------
romantic_programmer
Thu Oct 05, 2006 10:15 am


-----------------------------------

    for y:0..20
  for x:0..25
    Pic.Draw (mapTile (1),x*32,y*32,picCopy) 
  end for 
end for



That is what i have......

But it is still drawing the same image. OVer and over....    I tried to do it with and (x,y) but says their is too many subscripts. For mapTile

-----------------------------------
romantic_programmer
Thu Oct 05, 2006 10:20 am


-----------------------------------
can some one just dleate this topic...   i will figure it out on my own... no point to keep the topic

-----------------------------------
Mazer
Thu Oct 05, 2006 10:37 am


-----------------------------------
I could easily delete it, but we generally prefer to not delete topic in the help sections. There's always something for someone (if not the original poster) to learn.

Are you sure you don't want our help? It seemed to me you were pretty close already.
(If not, don't read the rest of my post)



for y:0..20
  for x:0..25
    Pic.Draw (mapTile (1),x*32,y*32,picCopy)
  end for
end for 

It's drawing just one tile repeatedly because that's all you're telling it to do. Ideally, mapTile would be a one dimensional array of integers in which you loaded your pictures. You should also have a two dimensional array (of integers) that represents the map just like the text file is doing. Then you could instead do this:

for y : 0 .. 20
    for x : 0 .. 25
        Pic.Draw (mapTile(map(x, y), x * 32, y * 32, picCopy)
    end for
end for


As for reading in the text file, sorry, but Turing file I/O is a bit fuzzy for me. I think you'd have something like the previous for loop (assuming all maps will be 21x26 tiles), but instead of the Pic.Draw you'd be getting an integer from the text file.

-----------------------------------
Clayton
Thu Oct 05, 2006 11:57 am


-----------------------------------
%% Null Tile 
mapTile (0) := Pic.FileNew ("resources/tilesets/null.bmp") 

%%Main Tiles 
mapTile (1) := Pic.FileNew ("resources/tilesets/land/land/main/1.bmp") 
mapTile (2) := Pic.FileNew ("resources/tilesets/land/land/main/2.bmp") 

Etc.. 


things like this make my heart break, instead of doing that, have a loop read from the file instead eg.


var mapTile : array 0..249 of int
for i : 0..249
    mapTile(i) := Pic.FileNew ("resources/tilesets/land/land/main/"+intstr(i)+".bmp")
end for


This makes it much simpler to read, and takes and uber less amount of time to code :D

-----------------------------------
TokenHerbz
Thu Oct 05, 2006 2:53 pm


-----------------------------------
Try somthing along these lines:


[code]
var TileSize := 20      %%sets the tiles size
var TileRow : int := 30     %%the tiles column and row sizes
var TileColumn : int := 30
var file : int      %%to call my maps
var TileLoc : flexible array 1 .. TileRow, 1 .. TileColumn of int    %%to get the locations of the tiles
var ScreenShot : int        %%used to make the picture more effective
var map_number: int := 1  %%can use to show you which map your on

proc draw_map
    cls        %%this is to clear the map that may be present befor
    open : file, "Maps/Map" + intstr (map_number) + ".txt", get   %%txt file
    for x : 1 .. upper (TileLoc, 1)
        for y : 1 .. upper (TileLoc, 2)
            get : file, TileLoc (x, y)

-----------------------------------
TokenHerbz
Thu Oct 05, 2006 3:00 pm


-----------------------------------
Err i some how posted my message trying to write my code


Tabbing and all, note my fault:

The rest of it:


after you can draw certain tiles that you have in your txt:


if TileLoc (x, y) = 1 then
                    drawfillbox (x * TileSize, y * TileSize, (x + 1) * TileSize, (y + 1) * TileSize, brightgreen) %%ground
                end if


we should take a screen shot of our level, so we dont have to use the loops always:

%%end of proc
ScreenShot := Pic.New (0, 0, 620, 620)
    close : file
end draw_map



And to draw the map, simply have a proc like this:


proc map
    Pic.Draw (ScreenShot, 0, 0, picCopy)   %%draws the map
end map


and call it:)


OverView, the first proc is to change the map, you change the file and vars needed:  Ex: 


map_number := 2
%%OR
map_number := 3

%%will make this a different txt file, if you name them map1, map2, etc:

 open : file, "Maps/Map" + intstr (map_number) + ".txt", get %%txt file




hahaha hows that better?  i tend to over do things and the wrong way at that, but it works, so enjoy.

-----------------------------------
romantic_programmer
Thu Oct 05, 2006 7:07 pm


-----------------------------------
ok, right now i am at home... lol... I need to some how get turing here too... but tommorw will put this stuff to work and give ya a heads up....

-----------------------------------
romantic_programmer
Fri Oct 06, 2006 11:52 am


-----------------------------------
Ok i am making progress.. here is what i got so far...  


var tileID : int
var tileNM : string
var shrCut : string := "resources/tilesets"
var mapTile : array 1 .. 250 of string
var tileFile : string := "MapTile_Declarations.txt"
var mapId : array 1 .. 250 of int
open : tileID, tileFile, get
assert tileID > 0

var ctrLines : int := 0

loop
    exit when eof (tileID)
    get : tileID, tileNM : *
    if tileNM not= "" then
        ctrLines += 1
        mapTile (ctrLines) := shrCut + tileNM + ".bmp"
        mapId (ctrLines) := Pic.FileNew (mapTile (ctrLines))
        put ctrLines : 3, " ", mapTile (ctrLines)
        delay (10)
    end if
end loop



I know this isn't much but it is a big breakthrough... Now i just need to get the actual maping system to work and i will be set.... i think...

-----------------------------------
NikG
Fri Oct 06, 2006 1:48 pm


-----------------------------------
I knew I'd made a mistake.  What Mazer said was right: mapTile(map(x, y)), not just map(x,y) in the Pic.Draw.

Freakman, that big loop might not be possible because what are the chances that all 250 tiles are in the resources/tilesets/land/land/main folder?  Most probably, they are in different folders.
However, romantic_programmer, the lesson is that if you do have many tiles in a certain folder, using several for loop is better that typing each line individually.

About your code, it didn't really make sense to me, so let me give you a step by step of what you need.
-Declare your tiles array (0 or 1..250) and populate it (as you did before)
-Declare a 2 dimensional array (check the [url=http://www.compsci.ca/v2/viewtopic.php?t=8808]Turing Walkthrough for a tutorial) and populate it with the map file
-Go through the 2d array element by element, and depending on what it contains (presumably a # between 0 and 250), draw it using Pic.Draw and your tiles array (something along the lines of: Pic.Draw(mapTile(map(x, y))...)

do you see what's happening? you loaded the map into an 2d array (hence the map(x,y)), and whatever value it contained is being used to decide which tile to draw in the mapTile array).

hope that helps.

-----------------------------------
SNIPERDUDE
Fri Oct 13, 2006 2:34 pm


-----------------------------------
related...

What if you want to load a larger map, but display only a portion of it.  So when I move left or right the displayed portion of the map moves.  How would you accomplish this?

I hope this made sense.

-----------------------------------
TheOneTrueGod
Sat Oct 14, 2006 12:44 pm


-----------------------------------
omg... quad post... allright, basically, you just draw what is on-screen.  I.E.

if SquareX > (x - maxx div 2) and SquareX < (x + maxx div 2) and SquareY ...Etc... then
   DrawThisSquare
end if

of course, it all depends on how your co-ordinate system works... so you'll have to adjust accordingly.

-----------------------------------
NikG
Sun Oct 15, 2006 10:59 pm


-----------------------------------
Well how I handled it in my rts game was to have ViewX and ViewY variables that kept track of where you're looking.  Then all I did was check which of my objects were between ViewX +/- 250 and ViewY +/- 200 and drew those only.

Let me know if you want an example.

-----------------------------------
SNIPERDUDE
Mon Oct 16, 2006 6:46 am


-----------------------------------
An example would be great, thanks.

-----------------------------------
NikG
Mon Feb 05, 2007 8:54 pm

Re: Tile Mapping Question
-----------------------------------
Well I had two people asking for a demo of this, so I finally prepared one.
(Sorry for the delay guys, I haven't been on CompSci for a while)

var x, y, b : int
var MaxX := 1000
var MaxY := 800
var ViewX := 250
var ViewY := 200

loop
    locate (1, 1)
    put "Currently looking at:"
    put "X: ", ViewX - 250, " - ", ViewX + 250
    put "Y: ", ViewY - 200, " - ", ViewY + 200

    Mouse.Where (x, y, b)

    if x >= 0 and x = 0 and y  maxx - 15 then
            ViewX := min (ViewX + 10, MaxX - 250)
        end if
        if y < 15 then
            ViewY := max (ViewY - 10, 200)
        elsif y > maxy - 15 then
            ViewY := min (ViewY + 10, MaxY - 200)
        end if
    end if

    %Draw objects on screen

    delay (10)
end loop

That's pretty much it! All you need is absolute positions of your objects and you draw them based on what ViewX and ViewY are.

For example, in this case our map size is 1000 by 800 and the view size is 500 (ViewX-250 to ViewX+250) by 400 (ViewY-200 to ViewY+200).
So if we want to show an object at location Obj1.x,Obj1.y just check if it's within the ViewX and ViewY ranges:if Obj1.x>ViewX-250 and Obj1.xViewY-200 and Obj1.y