Making a map loading sub

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Making a map loading sub

Post by DaveUnit »

I'm trying to make an efficient sub that loads multi-layered maps that can use multiple tilesets.

I've had no luck finding any tutorials that really focus on loading maps like that.

Here's my map format so far...

Code: Select all

map name

LAYER (number)
width
height
tileset used
0,1,4,2,3 (map data and whatnot)

LAYER (number)
etc etc etc
If anyone can kinda show me the right path to take to load something like this efficiently I would really appreciate it. I'm not asking you to code it for me, I'd just like some tips to load and display the map quickly.

Thanks to anyone that can be of any help.
I reeeeeaaaaally appreciate it. I'm trying to code as much of a game engine as I can before apathy sets in. :P

Dave
User avatar
Zamaster
Veteran
Posts: 174
Joined: Wed Jun 15, 2005 1:51 pm

Post by Zamaster »

It depends on how the map got there, did you just type it up in notepad? Is it something you made in QBASIC? I have the feelling its a file created with APPEND as the OPEN file type. Assuming it is, just use INPUT #(file number), (variable to receive data). This is very simple and easy to use.
I guess you might do something kinda like this(Ill assume you have a maximum of 4 layers and a maximum map size of 16x16):

Code: Select all

TYPE MapType
Width AS INTEGER
Height AS INTEGER
Tileset AS INTEGER
END TYPE
DIM MapProp(1 TO 4) AS MapType
DIM MapDat(1 TO 16, 1 TO 16) AS INTEGER
DIM MF AS INTEGER
MF = FREEFILE
OPEN "MAPFILE.MAP" FOR INPUT AS MF
INPUT MF, CMapName$
DO
INPUT MF, CML%
INPUT MF, MapProp(CML%).Width
INPUT MF, MapProp(CML%).Height
INPUT MF, MapProp(CML%).Tileset
FOR y% = 1 TO MapProp(CML%).Height
FOR x% = 1 TO MapProp(CML%).Width
INPUT MF, MapDat(x%, y%)
NEXT x%
NEXT y%
LOOP UNTIL EOF(MF)
This expects that the tiles are layed out from left to right in rows. You could switch the FOR NEXT statements if your map was the other way around. There are of course a million ways to do this. You could read the map on a first pass, record the widths and heights and set the map data array to the highest of the two values or you could create 4(or however many layers there are) seperate arrays that were dimensioned according to the widths and heights in the original file. The possibilities are endless!
I didnt test this code so it may or may not work. Let me know if you need more help-

~Chris
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

Thanks for some code, Zamaster. I appreciate the help.

I forgot to mention that I'm using Freebasic, btw.

Also, I need my drawmap routine to be as fast as I can get it. When I load the map I want that data to be put in an array because of the fact that reading from ram is a hell of a lot faster than accessing a hard drive.

What would be the fastest way to store data like this?
Again, not looking for code, just some tips on a good method to store and retrieve this data in the fastest possible way.

And one last little question, the way the map format is now, a layer can only use 1 tileset. I was wondering if I wanted to use multiple tilesets on a layer, should I insert a tileset name when I want to switch to a different tileset...
like so:

Code: Select all

map data:
$tileset_1,0,0,0,0,0,0,$tileset_4,0,0,0,0,0,0
...Or should I just have sub-layers in each layer. So every sub-layer can only use one tileset but all together they make a one layer that uses multiple tilesets?

I'm not very good at trying to describe my ideas so if you want me to elaborate and try to explain things better I'll give it a shot.

Thanks so much to any of you that help.

Dave
User avatar
Zamaster
Veteran
Posts: 174
Joined: Wed Jun 15, 2005 1:51 pm

Post by Zamaster »

Well if you looked, my code stores the map data in ram. So it should work fine that way. A few questions, are you talking about accessing the map data or the tile images? To make the map data load faster, you should compress the data array to one index, just multiply together all the original indexs,(My example would look like 4x16x16), actually, my code is wrong, lemme fix it:

Code: Select all

TYPE MapType 
Width AS INTEGER 
Height AS INTEGER 
Tileset AS INTEGER 
END TYPE 
DIM MapProp(1 TO 4) AS MapType 
DIM MapDat(1 TO 4, 1 TO 16, 1 TO 16) AS INTEGER 
DIM MF AS INTEGER 
MF = FREEFILE 
OPEN "MAPFILE.MAP" FOR INPUT AS MF 
INPUT MF, CMapName$ 
DO 
INPUT MF, CML% 
INPUT MF, MapProp(CML%).Width 
INPUT MF, MapProp(CML%).Height 
INPUT MF, MapProp(CML%).Tileset 
FOR y% = 1 TO MapProp(CML%).Height 
FOR x% = 1 TO MapProp(CML%).Width 
INPUT MF, MapDat(CML%, x%, y%) 
NEXT x% 
NEXT y% 
LOOP UNTIL EOF(MF) 
Okay, much better. Anyway, so you want a layer to have multiple tilesets? Truthfully, the fastest way to do this is just to create a larger tileset consisting of the ones you want to use. But if you absolutely must, you could do that. Id suggest having the program that creates a new tileset at the 'map load' that holds only the tiles YOU NEED. This would save space and move at a decent speed. So yeah, anything else?
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

I know you stored your map in ram, but I was just wondering about any little optimizations that could be done. I just don't want a slow drawing routine, but I really doubt I hafta worry about that since I'm not coming even remotely close to pushing any boundaries. :P

I was talking about the actual map data btw.

And I think I'll try your idea of putting all tiles needed into an array, or if I'm lazy just put all the tiles I need in one file like you suggested.

I think I'm good now. I just needed to make sure I know exactly what I'm doing before I make something like this so I don't get frustrated and let the old apathy set in.

Thanks for all the help, Zamaster.
User avatar
Zamaster
Veteran
Posts: 174
Joined: Wed Jun 15, 2005 1:51 pm

Post by Zamaster »

no problem!
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN
Post Reply