I'm try to create a TileMap system... for this, I get that sample:
http://wiki.allegro.cc/index.php?title=Tilemap_Example
But, that sample is very confusing for me... so, How i can make for the allegro recognize the matrix in screen... for example:
sample.map
##############################################
##############################################
##############################################
#########$$###################################
##############################################
######$$###$$#################################
#2############################################
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
So, the system to read that file is very very very simple... but, How I can make to game put the correct imagens in correct screen matrix...
What I have thinked...
The game load all the matrix in memory, because, I whant to player walk freely around the map...
So, i think for that, I need to create a "camera" system... beacause at time, my player is fixied on the midle of scren... and I whant he can walk freely on the map...
>-----------------------------------------<
Drag And Drop
How I can make a system for drag and drop imagens inside of game ?
For example, I have one image loaded in game, and, I need to put that image in correct position of screen, how I can make this ?
Or, I can make this using the matrix ideia, using the keyboard to put the image in correct local ?
You're asking very broad questions that don't necessarily have a "right" answer. The question isn't how is it done, it's how do you want to do it? Personally, I think the easiest way is to use the .map file and iterate through each character and use if-else to find out which object to create.
So let's say you load sample.map into a vector<string> where string[0] = first line, string[1] = second line, and so on until you reach eof. You could just go
//load sample.map into vector<string> map_string; for( int i = 0; i != map_string.end(); ++i){ //iterate through each string for( int j = 0; j != map_string[i].length(); ++j){ //iterate through each character //Assumes 32x32 tileset int x_loc = j*32; //x location to create this tile int y_loc = i*32; //y location to create this tile if( c == '#'){ //Probably actually requires doing nothing create_tile( Background_Tile, x_loc, y_loc); }else if( c == '$'){ create_tile( Block_Tile, x_loc, y_loc); }else if( c == '2'){ create_tile( Character, x_loc, y_loc); } } }
If you decide to do a drag and drop thing, it will require you to save the maps to a .map file like that anyway so I'd just stick to doing a tilemap like that for now
As part of your question you asked how to represent the different images; your map should define this, so instead of:
#######$#########$##
####################
#$##################
##############$#####
You would have this:
1 1 1 1 4 1 1 3 1 1 1
1 1 1 1 1 1 3 3 1 1 1
1 1 1 1 1 1 1 3 1 1 1
1 1 1 1 4 1 1 1 1 1 1
The in your game loop you would say:
if(tileNumber[x][y] == 1) draw_grass(); if(tileNumber[x][y] == 3) draw_water(); if(tileNumber[x][y] == 4) draw_tree();
If you want a program to build the maps (drag & drop) look at something like Tilestudio or Mappy.
The only limit to using numbers to represent tiles is that you have no more than 10 tiles. Comparing single characters works the same way but gives you way more options.
The only limit to using numbers to represent tiles is that you have no more than 10 tiles. Comparing single characters works the same way but gives you way more options.
If you leave a space between them, you have as many different tiles as the size of whatever integer type you use to read them in allows.
OR, you can save your maps as BINARY... write your own editor, which I always do first, they're not difficult to create, the just write your tile numbers and other level data (level number, name, description, player start x & Y, enemies etc) to it in binary and load it in as binary.
You'll much happier once you get an editor created that does things exactly as YOU want them done and end up with a smaller level file as well.
My little Deluxe Pacman 2 levels I am working on (I posted the editor in another thread) come out at 1.16K each in size.
I don't know how to use binary files, I've been trying to figure it out but I can't find any resources that make sense to me.
If you include a delimiter obviously you can make the numbers as big as you want but I don't see why you'd do that when using a character set you have a uniform width for each object representation and just make it easier to read and write overall.
I don't know how to use binary files, I've been trying to figure it out but I can't find any resources that make sense to me.
I think thats the funny part. I had the same issues.. But on a lower level, binary files are much simpler. Instead of thinking that you're saving "text" of some kind, you're storing raw numbers in the file. If you write an 8 bit number, you can read that 8 bit number back. Same goes for 16bit and larger numbers (if you ignore endianess).
Reading/writing raw data means you also don't have to parse the file's contents. Like if you chose to store ascii numbers separated by spaces, you have to specially handle that. With binary, its just one integer after another. very simple.
append: at a low level, a binary file is just an array of bytes. Or if you wish, any larger multiple of 8bits (so long as you don't go for a really strange non multiple of 8bits format, that would make things significantly more complicated for no good reason).
Binary file types are fairly easy once you get used to them, but they can be a bit of a pain until you've written an editor as they're not as easy to hand edit as text files. I usually stick with text files until the program is finished and then convert to a binary format.
edit:
I did start writing a tutorial on using Allegro 4 to read and write multi-platform binary files, but stopped halfway when I decided to switch to A5. I'm pretty certain I still have the A4.9 code I wrote for the tutorial around somewhere, so maybe I'll try to update/finish the tutorial.
I have a bit of a predilection for plaintext formats, which I justify like so:
1) Even with binary save/resource formats, anyone who wants to cheat by editing the files can.
2) Unless my software is so unpopular that no one uses it.
I'd stick with plaintext even for something with an online component - all the popular MMOs have (multiple) communities devoted to extracting and unpacking their datafiles and mining them for interesting information. Security shouldn't be clientside, and online or offline using binary formats only serves to increase debugging complexity while excluding a portion of my users.
Here's my save and load functions for my Deluxe Pacman 2 level editor (Pace2) which I posted in another thread. I don't know if it's the best way to do it, but it's the way I do it and it works perfectly for me.
My loadmap() function uses Allegro 5 functions like al_fgetc() but not my save function for some odd reason, I may simply not have gotten around to it.
Edit: Just quickly rewrote it to use Allegro 5 functions, don't know how I missed this the first time around. See, it pays to help people!
As you can see, you simply write your data to the file, then read in back in the same order. When you write a 2, a 2 goes into the file for example (not a text "2" but an actual number 2, not readable by text editors. Open up the file in a hex editor and you'll see a 2 where you wrote a 2 etc... pure binary, no waste of space and no time wasted parsing text. It is most definitely easier in my opinion.
I commented the loadmap() function to give an idea what is going on, it's quite simple as you can see. No parsing, just load and go.
This is also all in pure C (I use -std=c11, the 2011 C standard)...