Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » making a datafile, but it won't load..?

This thread is locked; no one can reply to it. rss feed Print
making a datafile, but it won't load..?
red-dragon
Member #7,722
August 2006

Ok, yeah, I know I'm taking over the forums :) But I'm trying to learn how to make data files and uhh I followed this tutorial but it didn't work. A zip file is attached, please check it out.

I found Grabber, opened it up, Right-clicked in white side, clicked on New, for a bitmap, names it linksawakening. Then I gave it the header, linkawakes.h. The file with no name is the .dat file.

The original linksawakening.bmp was 24-bit. Could the problem be in here somewhere:

set_color_conversion(COLORCONV_24_TO_32);//??
set_color_depth(32); // Set the color depth
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480,0,0);

If so, what am I doing wrong?

Lastly, is there a tutorial on how to make a Mappy level into a .dat file? If you've done it, can you give me clear, precise instructions? I'd REALLY appreciate it. REALLLLLY! :o

gnolam
Member #2,030
March 2002
avatar

Quote:

But I'm trying to learn how to make data files and uhh I followed this tutorial but it didn't work.

Define "didn't work". Did the program crash? Did load_datafile() return NULL? Was the bitmap full of garbage? Did your computer explode? Did you spontaneously burst into flames? Did Timmy fall down the well?

Quote:

DATAFILE *my_datafile = load_datafile("linksawakening.dat");

And the file is actually just called ".dat"? :P

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

red-dragon
Member #7,722
August 2006

haha, hey gnolam,
Ok, it said it couldn't load the datafile. I forgot to include that.
And you're right... I renamed the nameless .dat file to linksawakening.dat and now it works. How come.. it doesn't name the .dat file automatically?

It is very important that I find out how to do this for a map created in mappy, so I can follow a tutorial and finish the scrolling part of the project. SO, I beg someone.. please. I'll give you a $1 :P

EDIT: I need to mainly know what type to make it as.. obviously it's not a bitmap or sprite..

Edward Sheets
Member #4,734
June 2004
avatar

red-dragon, may I suggest that you simply start from scratch and play with Allegro for a while. You will learn more that way than any of us can teach you by giving you lines of code to plug into your program. There is a program in the example folder called 'exdata' that shows you the basics of using datafiles. Adding your own user-defined maps to a datafile is a more complex subject and you will need to have a good understanding of the grabber utility and datafiles in order to do that. The tutorial will still be there waiting for you when you're ready for it. :)

---

Note: carving a pentagram on the top of a container of spoiled yogurt does not summon a yogurt demon. -Kikaru

red-dragon
Member #7,722
August 2006

hey Edward,
thanks for the input. THe way I'm putting up questions, it makes me look retarded. I had my mind set to solve a problem in a specific way, when you can do the same thing a lot easier.

i.e. why don't i just use store my maps as a 2d array and work on scrolling from there? I need to get some sleep! I Just got pulled in too many directions.

And now I'm gonna sound even moreso retarded... after I export a mappy file as "map array as comma values only (.csv)", I rename it as .txt and open it up in Notepad. Now, I'm trying to save it as "Map Array (.MAR)" and it saves it as a MS Access shortcut? What do I do with that? I swear I've searched and can't find an answer.

Game programming as a whole is frustrating! Lessons learned though ;)

Edward Sheets
Member #4,734
June 2004
avatar

red-dragon, you are not retarded at all (and we would like you even if you were ;)). Programming is a complex thing and game programming is one of the most challenging programming tasks there is.

I think this is the perfect time to "reinvent the wheel" a little bit. Mappy is a great tool but you could write a simple, elegant solution to this problem in half the time it would take to figure out the inner-workings of Mappy. And in the process of writing your own simple map loading and saving functions, you would get a much better understanding of how Mappy works under the hood.

Take a look at how simple it can be:

1//we'll use simple stdio functions for handling file input and output
2#include <stdio.h>
3//define your map and tile dimensions here
4#define MAP_W 100
5#define MAP_H 100
6#define TILE_W 32
7#define TILE_H 32
8 
9//create a simple 2d array to hold your map - works like a charm :P
10int my_map[MAP_W][MAP_H];
11//don't forget to initialize the map or use the load_map() function to put meaningful data in the map array before using it.
12 
13//map x and y position
14int map_x = 0;
15int map_y = 0;
16 
17//this function will load a saved map into your 2d array
18int load_map(char file[], int tile_map[MAP_W][MAP_H]){
19 
20 FILE *p_file;
21 
22 p_file = fopen(file, "rb");
23 if(p_file == NULL){
24 printf("Unable to load specified map file...\n");
25 return 1;
26 }
27 
28 fread(tile_map, sizeof(int), MAP_W * MAP_H, p_file);
29 
30 fclose(p_file);
31 printf("Map successfully loaded...\n");
32 return 0;
33}
34 
35//this function will save your map to file using the filename you pass to it. You can use this to create your own simple, but very useful map editor.
36int save_map(char file[], int tile_map[MAP_W][MAP_H]){
37
38 FILE *p_file;
39 
40 p_file = fopen(file, "wb");
41 if(p_file == NULL){
42 printf("Unable to open file for writing...\n");
43 return 1;
44 }
45 
46 fwrite(tile_map, sizeof(int), MAP_W * MAP_H, p_file);
47 
48 fclose(p_file);
49 printf("Map saved...\n");
50 return 0;
51}
52 
53//you'll want to add features you need to the drawing function to suit your game, but this will get you started:
54void draw_map(int tile_map[MAP_W][MAP_H]){
55 
56 int x, y, bmp;
57 for (x = map_x; x < SCREEN_W / TILE_W; x++){
58 for (y = map_y; y < SCREEN_H / TILE_H; y++){
59 bmp = tile_map[x][y];
60 blit((BITMAP *)data[bmp].dat, buffer, 0, 0, x * TILE_W, y * TILE_H, TILE_W, TILE_H);
61 }
62 }
63 
64}
65 
66// then you can use these simple functions like this:
67 
68save_map("level3", my_map); //this will create a file named level3 if it does not already exist. If it exists, it will be overwritten.
69
70load_map("level3", my_map); //this will load your saved map right into your array and you're good to go :)
71 
72draw_map(my_map); //simply draws the map to your buffer
73//

I'd play with something like that and maybe add some features to this simple tile engine and in no time you'll know exactly how Mappy works and you might even decide that your own simple version of Mappy works good enough :)

Also, you can get rid of that set_color_conversion() call. Just make sure you set the color depth with set_color_depth(32) before loading any graphics. That way when you call load_datafile() it will automatically load everything in the color depth you passed to set_color_depth(). Here's a handy way to set color depth based on the local color depth of the computer your game is running on:

1int local_color_depth;
2 
3local_color_depth = desktop_color_depth();
4set_color_depth(local_color_depth);
5 
6if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0){
7 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
8 allegro_message("Unable to set graphics mode.\n%s\n", allegro_error);
9}
10 
11data = load_datafile("data.dat");
12if(!data){
13 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
14 allegro_message("Error loading data. Try reinstalling the game.\n%s\n", allegro_error);
15 }

---

Note: carving a pentagram on the top of a container of spoiled yogurt does not summon a yogurt demon. -Kikaru

red-dragon
Member #7,722
August 2006

Edward,
Thanks for the kind words. I feel better today and more focused.
That's cool how you showed me how simple map saving/loading functions can be. However, I'm on a very tight schedule and have to turn these assignments in. This is my first attempt at some form of game programming, and I'd rather have my focus on that than the "tools".

I wanted to save my map into a 2d array. So I was trying the .MAR option. It saves it as a MS Access REPORT shortcut. I open it and and it opens up the Access application and that's it. Nothing else. What am I suppose to do that I'm not doing?

Go to: