News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Files

Started by Bustaballs, February 22, 2006, 04:25:29 PM

Previous topic - Next topic

Bustaballs

Ok I promise I looked through the help files, attempted many things to get it to work, etc, and stilled failed and need some help.

What I'm trying to do is access the data in map files for my Sibertekk game.  They are very simple.  I created a array in an array to save the location of each image row by row. So if you open up my test.hax file in notepad you'll see this for the first row:

"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\walkway2.bmp"
"\GFX\street2.bmp"
"\GFX\street.bmp"
"\GFX\street2.bmp"
"\GFX\walkway2.bmp"


I've tried many things and looked at all of the help docs under the "Files" catagory but have yet to be able to access my files past the first line. I can easily access the first line of code with a simple:

 File$=CurrentDir$()+"\maps\test.hax"
 
ReadFile File$, 1
Print ReadString$(1)
Sync
WaitKey


but I can't figure out how to access anything past that.  Please help me out.
QuoteOh please

Ian Price

#1
I've got an example of a map editor that loads and saves data that I'll post here later.

What I did though was use values that represent the tiles, rather than strings.

What I basically did is use this formula to load


; Create map array 40x30
Dim map(1200)

; Load all your images
LoadImage.....


load_data()
main()



; Load data
Function load_data()

file$="data\BYTE"+Str$(lvl)+".dat"

ReadFile file$,1

For n=0 To 1200
map(n)=ReadByte(1)
Next

CloseFile(1)

EndFunction



; Main function
Function main()

Do

  n=1

  For y=0 To 29
  For x=0 To 39

   If map(n)>0 then DrawImage map(n),x*32+distx,y*32+disty,1; 32x32 tiles

   n=n+1

  Next
  Next

Sync

Cls RGB(0,0,0)

Loop

EndFunction


What this does is loads the data into the map array, then uses the main function to redraw the map using that data. You could easily change that so that it draws all the tiles onto a single image, or use the data to place sprites (eg. just before the "If map(n)>0 Then Draw" bit you could use

If map(n)=X Then Createsprite(X)


Creating a map editor is simple once you understand the basics. Using the above example to redraw your images, why don't you have a go at saving a load of random numbers to a .dat file (make sure they are Bytes (less than 255)).

BTW I used bytes for my project as I use less than 255 tiles and it saves disk space.
I came. I saw. I played some Nintendo.

Bustaballs

Damn.. I have a hard time reading pure code that isn't extremely simple or something I can play around with.  There are a few things that I don't understand at all about it though.


; Create map array 40x30
Dim map(1200)

is that intended to create 40x30 tiles?



file$="data\BYTE"+Str$(lvl)+".dat"

what does the "lvl" part mean?




 If map(n)>0 then DrawImage map(n),x*32+distx,y*32+disty,1; 32x32 tiles


distx and disty are for what?


Thanks for trying to help.
QuoteOh please

Ian Price

#3
Right firstly Dim map(1200) creates a space in memory that will allow 1200 bits of information to be stored - if you like, imagine 1200 matchboxes each capable of holding one item. This item doesn't have to be a match, it can be a nail, a dog hair, a penny, a pebble. An array is much like those boxes, except it holds one pieceof data. This data can be a number or a word/sentence (this is a "string" and you would have to define your array with Dim word$(x) - notice the $).

So Dim map(1200) will allow you to store 1200 pieces of data/information - this is where the data for your game map will be stored. Once you have loaded your data once, it will stay in memory until you clear that map() array. The good thing about this is that you can change the array at any time during your game.

The Dim command does not create the tiles - it merely holds values.

Eg. say you created a map on screen made up of 6 different tile images (1-6) you could place them onto a map eg. (the numbers represent different graphic tiles)

2 4 3 4
6 6 1 2
4 4 1 4
6 1 5 6

You could then save this information into an array. The array would start at top left then work right, then down so you map array would look like this (in memory)
BTW array data always starts from 0

Array    tile
map(0)=2
map(1)=4
map(2)=3
map(3)=4

map(4)=6
map(5)=6
map(6)=1
map(7)=2
etc.

If you were to then save this data and be able to view it it would look like 2 4 3 4 6 6 1 2 etc.

You can then reload the data and the map would appear exactly as you drew it originally. Despite you drawing in images/tiles, the computer uses the numbers of the tiles to create an array in memory made up only of numbers - this is what a level editor does for you. Writing level editors is simple once you understand what it is supposed to do for you and what you want to get out of it.


As for the other code, that is actually related to my game - a conversion of BoulderDash, and perhaps not relevant to your game, but the principles will be the same.

"lvl" is just a shortened variable for "level" this is the number of the current game level. Eg lvl=1 would mean load level 1 data etc. Basically I'm asking the computer to load my data file that is called "Data\BYTE1.dat" for level 1 or "Data\BYTE2.dat" for level 2 etc.

"distx" and "disty" are scrolling offset values - you wouldn't need these if your level map didn't need to scroll.

For instance if distx=0 and disty=0 then the very first tile would be placed at 0,0 on the map. If you increase the distx variable, then the first tile would be drawn further to the right, decrease the value and it would be drawn further to the left.

eg if dist=10 then the first tile would be drawn at position 10,0 and so on.


I hope this has made it a bit clearer - if not, just ask :)
I came. I saw. I played some Nintendo.

Alex777

#4
Another approach: create a test file in MS Excel with 3 rows of data, each containing 5 integers of your choice.  Save it to the project folder as a comma-delimited text file (*.csv file extension).  Then run this code, which uses SplitToArray:


dim t(15)

test = GetFreeFile()
ReadFile CurrentDir$() + "/test.csv", test

While Not(EndOfFile(test))
; read 1 line of data
x$ = ReadString$(test)
; parse it, convert each token to an integer, and store it in the array
; (all in 1 line of code!)
count = SplitToArray(x$, ",", t(), pos)
pos = pos + count
EndWhile
CloseFile test

For n = 0 To 14
Print t(n)
Next n

Sync
WaitKey


Ian Price

That would work, but honestly, how many games are written with their maps created in excel?

Most maps are created in map editors, which is both quicker and visually clearer - in a map editor you can actually see what a map will look like, rather than having to visualise a bunch of numbers. Besides not everyone has Excel.

I suspect that his maps will use less than 255 tiles, so bytes would be a lot more efficient memory-wise.

I've just tried your example - using 10 numbers, Excel created a file 23 bytes long, a 20 integer file is 55 bytes long - my method would create a file 10 bytes long and 20 bytes long respectively. Excel creates files more than twice the size of a standard byte file.

Admittedly this could be more useful if there are more than 255 tiles, but still visually that'd be a nightmare  - who can realisticly remember each of 255+ tiles?
I came. I saw. I played some Nintendo.

Digital Awakening

Nobody typed their maps in a txt file? :)

Anyway, smaler maps that are linked together or placed next to each other are a good idea. That is if you walk into a certain coordinate there's info there to take you to another map or simply when you are next to the edge of one map you start reading from the next one.
Wisit my site at: DigitalAwakening.net

Alex777

<< but honestly, how many games are written with their maps created in excel?  >>

Yah, I use a map editor also (my own) but it produces *.csv files so I can edit them quickly in Excel if I want.  The extra size is mostly the commas.

I really just wanted to point out that SplitToArray is a powerful command for loading text map files.

Digital Awakening

It works just like explode in PHP and I also use that a lot because I often stroe arrays in MySQL that way. Implode works the other way so an array can be "glued" together into a string.
Wisit my site at: DigitalAwakening.net

Alex777

Hmmm - I like the sound of Implode (bad pun intended).  Kevin, can we get something similar some time?

Bustaballs

QuoteNobody typed their maps in a txt file? :)

Anyway, smaler maps that are linked together or placed next to each other are a good idea. That is if you walk into a certain coordinate there's info there to take you to another map or simply when you are next to the edge of one map you start reading from the next one.


I just had some simple code do that for me in my Sibertekk game.  If xxx map then xxx map for each direction.
QuoteOh please