Save: SAVE: I CAN'T SAVE!
Taiko Keiji

does anyone know how you can take the current game and save it into the disc for the player to continue later???
I'm a new programmer and these things are very confusing to me.???

GameCreator

What are you coding in?

Taiko Keiji

I'm coding in allegro.
I havent learned anything else yet. I want something similar to the zelda save type.

HoHo
FILE* f=open_file_for_writing();
write_gamestate_to_file(f);
close(f);

Now you simply have to fill in the first two functions :)
First should open a file for writing. You have to generate a filename. How you do it is up to you.
Second function writes all the important information to that file. Important information is stuff like player and enemy positions, their actions, score and other statistics.

Loading goes exactly the same, only instead of writing to file you read from it :)

Taiko Keiji

is there any source code i could look at as a resource, it seems to be the best way for me to learn.

Onewing

Keep in mind there is no save_game() function, just like there isn't any play_game() or make_my_game_the_most_popular_game(). You've setup your game, you know what needs to be set to make things occur, you just need to write functions to store it and load it from an external file. Maybe you could start with your initilization functions to give you some direction. Perhaps you set "score" to 0 in the initilization function, well, you probably don't have 0 in the saved game. ;)

Taiko Keiji

Wow, I don't get it.

...It wont write to file??
what am I doing wrong?

Edit:
FILE* save_1;
it says that there should be a constructor or destuctor before the * symbol.

HoHo

^- click on those links and read the documentation

Taiko Keiji

Sweet, got it.

Thanx alot.;D

EDIT:

Can anyone show me an example using these because when I tried to write to the file and then read from it it only says 0.
here's the code.

1#include <allegro.h>
2#include <stdio.h>
3 
4int w=4;
5void save_1()
6{
7 FILE* save1=fopen("test","wt");
8 fwrite(save1,sizeof(int),w,stdin);
9}END_OF_FUNCTION(save_1);
10 
11int load_1()
12{
13 FILE* load1= fopen("test","r");
14 return fread(load1,sizeof(int),w,stdin);
15}END_OF_FUNCTION(load_1);
16 
17volatile long speed_counter=0;
18 void increment_speed_counter()
19 {
20 speed_counter++;
21 }
22END_OF_FUNCTION(increment_speed_counter);
23
24int main(int argc, char*argv[])
25{
26 //Global variables
27 BITMAP* buffer;
28
29 //initialize and install
30 allegro_init();
31 install_keyboard();
32 install_sound( DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
33 install_timer();
34 LOCK_VARIABLE(speed_counter);
35 LOCK_FUNCTION(increment_speed_counter);
36 install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));
37 if (install_mouse()==-1)
38 {
39 allegro_message("Unable to locate mouse! %s",allegro_error);
40 return -1;
41 }
42
43 //Set graphic mode
44 set_color_depth(16);
45 if (set_gfx_mode(GFX_AUTODETECT,640,480,0,0)!=0)
46 {
47 set_gfx_mode(GFX_TEXT,0,0,0,0);
48 allegro_message("Cannot initialize graphics mode! %s", allegro_error);
49 }
50
51 //Create bitmaps and other objects
52 buffer=create_bitmap(SCREEN_W,SCREEN_H);
53 if ((!buffer))
54 {
55 set_gfx_mode(GFX_TEXT,0,0,0,0);
56 allegro_message("failed to initialize bitmaps!");
57 return 1;
58 }
59
60 //Main loop
61 text_mode(-1);
62 while (!key[KEY_ESC])
63 {
64 clear_bitmap(buffer);
65 while (speed_counter>0)
66 {
67 speed_counter--;
68 }
69 save_1();
70 textprintf(buffer,font,0,0,makecol(0,255,255),"%d",load_1());
71
72 blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
73 }
74
75 //Clean up
76 destroy_bitmap(buffer);
77 return 0;
78}
79END_OF_MAIN();

Kauhiz

Use the code tags! And why do you have END_OF_FUNCTION after everything?

Audric

ouch...
You should check the return value of fopen to see if it even succeeded.
You should decide if you want text files or binary files:
- text: save with fprintf(), read with...for example: fscanf() or gets()
- binary:
. open the file with "rb" on reading and "wb" on writing,
. save with fwrite((address of an integer),sizeof(int),1, save1)
. load with fread((address of an integer), sizeof(int), 1, load1)

Matthew Leverton

What you need to do is learn how to read and understand manuals.

For example, see fread:

size_t fread(void *buffer, size_t size, size_t number, FILE *file);

Description

This function reads size*number characters from file to buffer.

Return Value

The number of items of size size read, or less if there was an error.

------

What you're trying to do is making absolutely no sense. Programming isn't like scrap booking. You cannot just copy and paste from this and that example, rename a few variables, and think that anything will work like what you are imagining.

return fread(load1,sizeof(int),w,stdin);

Examine what you are doing with what the manual says to do.

Jeff Bernard

Here's somewhat of an example that may help you...

1#include <allegro.h>
2#include <stdio.h>
3 
4int w=4;
5void save_1()
6{
7 FILE* save1=fopen("test","w");
8
9 //You aren't actually writing to the file that you opened.
10 //fwrite(save1,sizeof(int),w,stdin);
11 
12 //fwrite() works like this:
13 int data[4] = {0, 1, 2, 3};
14 fwrite(data, // the first argument is a pointer to the buffer what is
15 // written to the opened FILE*. An array is a pointer.
16 
17 sizeof(int), // you had this one correct, good job.
18 
19 4, // I'm not sure you had this correct in your attempt. I get
20 // the impression that you tried to write the value stored in w,
21 // which is far from what you were doing.
22 
23 save1); // lastly, the file to which to write
24 
25 fclose(save1); // don't forget to close the file that you opened!
26 
27}// you do not need END_OF_FUNCTION() here.
28 
29int load_1()
30{
31 FILE* load1= fopen("test","r");
32 //return fread(load1,sizeof(int),w,stdin);
33 
34 int data[4];
35
36 if (load1) // only try to read if the file actually opened
37 {
38 //here's how to use fread()
39 fread(data, // the first argument is to what to write the read data
40 
41 sizeof(int), // again you seem to have grasped this concept
42 
43 4, // same message as above
44 
45 load1); // read from the opened file, not stdin
46 
47 // I normally do not check the return value of fread(), but you can
48 // if you wish.
49 
50 fclose(load1);
51 }
52 
53 else // the file did not open, so let's make sure to have something
54 // for the function to return:
55 data[0] = 99;
56 
57 return data[0]; // I suppose for testing purposes you can make sure it
58 // loaded the correct first value or something
59}// you do not need END_OF_FUNCTION() here
60 
61volatile long speed_counter=0;
62void increment_speed_counter()
63{
64 speed_counter++;
65}
66END_OF_FUNCTION(increment_speed_counter)
67// this is swhere you would use END_OF_FUNCTION(), but you do not need a
68// semi-colon
69
70int main(int argc, char*argv[])
71{
72
73 // this isn't actually a global variable, so you know
74 //Global variables
75 BITMAP* buffer;
76 
77 //your main() probably works. I did not look at it and do not want a
78 //large post filled with unneccessary code
79}
80END_OF_MAIN()
81// you do not need a semi-colon

You also probably do not know what stdin is. stdin is a special variable (of type FILE* or _iobuf*, although those are basically the same thing, I think) that reads user input. Similarly, stdout is a special variable that outputs stuff, such as writing to the console screen.

Taiko Keiji

Thanks alot, I understand now.

Thread #589971. Printed from Allegro.cc