Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Default filename for al_create_native_file_dialog()?

This thread is locked; no one can reply to it. rss feed Print
[A5] Default filename for al_create_native_file_dialog()?
Neil Roy
Member #2,229
April 2002
avatar

I was doing more work on my editor, actually editing a level, testing new graphics when I forgot the name of the level that I had loaded. I didn't want to overwrite the wrong filename. It occurred to me that I needed Allegro 5's dialog to remember the file that was loaded so when I went to save it again, it would be already there in the selection box by default under Windows 7.

I added in code which basically, should do this from what I read in the docs.

Something like this...

const char *filename = "c:\develop\projects\deluxe pacman 2\level02.dp2";
file_dialog = al_create_native_file_dialog(filename, "Load Level", "*.dp2",
                                          ALLEGRO_FILECHOOSER_FILE_MUST_EXIST);

My code is actually slightly different, but this is essentially it. The dialog works, but doesn't have the filename by default. It will navigate to the folder but the file doesn't show up. It shows all files ending in *.dp2 okay and I can select and save as usual. Is there any way I can do this? Do I need to learn how to do this myself? I am just about ready to start reading on how to get windows dialogs up to do this and abandon all attempts at portability, but I would rather not. I also thought of making my own interface, which is starting to seem easier, but I have to re-invent the wheel.

I tried some other things but then ran into problems with the different file types used, ALLEGRO_PATH, const char * as opposed to char * etc... so very annoying. I miss the simplicity of Allegro 4.

Something like: file_select_ex() which worked perfectly this way.

Matthew Leverton
Supreme Loser
January 1999
avatar

I'm not sure if the Windows version supports that properly. The Windows part of that add-on hasn't been given much love.

But that said, using single back slashes in a C-string is a no-no, so you should at least double them up or use forward slashes.

Neil Roy
Member #2,229
April 2002
avatar

But that said, using single back slashes in a C-string is a no-no, so you should at least double them up or use forward slashes.

Yeah, actually, I just quickly typed that part in by hand, I don't normally type out backslashes. I guess I should have copied/pasted code instead. ;)

My actual full save function is:

#SelectExpand
1// returns true if saved properly 2bool savemap(ALLEGRO_DISPLAY *display, LEVEL *level) 3{ 4 const char *filename = NULL; 5 ALLEGRO_FILECHOOSER *file_dialog = NULL; 6 7 /// TODO: Search folder for levels and set the filename to the next available level # 8 file_dialog = al_create_native_file_dialog(".//", "Save Level", "*.dp2", 9 ALLEGRO_FILECHOOSER_SAVE); 10 if(!file_dialog) return false; 11 12 if(!al_show_native_file_dialog(display, file_dialog)) { 13 if(file_dialog) al_destroy_native_file_dialog(file_dialog); 14 return false; 15 } 16 17 filename = al_get_native_file_dialog_path(file_dialog, 0); 18 19 FILE *file = NULL; 20 21 file = fopen(filename, "wb"); 22 23 fwrite(level->map_id, sizeof(char), strlen(level->map_id), file); 24 25 fputc(level->map_ver, file); 26 fputc(level->validated, file); 27 fputc(level->line_set, file); 28 fputc(level->player.x, file); 29 fputc(level->player.y, file); 30 for(int i = 0; i < 4; i++) { 31 fputc(level->ghost[i].x, file); 32 fputc(level->ghost[i].y, file); 33 } 34 fputc(level->pickup.x, file); 35 fputc(level->pickup.y, file); 36 fputc(level->background, file); 37 for(unsigned char y = 0; y < MAPY; y++) { 38 for(unsigned char x = 0; x < MAPX; x++) { 39 fputc(level->map[y][x].tile, file); 40 fputc(level->map[y][x].is_pill, file); 41 fputc(level->map[y][x].is_powerpill, file); 42 } 43 } 44 fwrite(&level->pills, sizeof(unsigned short), 1, file); 45 46 // never forget to close the file 47 fclose(file); 48 49 if(file_dialog) al_destroy_native_file_dialog(file_dialog); 50 51 return true; 52}

Currently it just goes to the default local folder to save, works well. I was thinking of changing the ".//" part (I copied that from someone on here, seems to work) so that it uses the filename variable, one that would be passed to this function, and it would contain the full path to the last loaded level (or default level name, eventually the function will scan for the next best name if there are none yet) and then the dialog would show the filename in it.

I have seen this in programs like Winamp, if you save your playlist, at first you have to select a filename, but once it is selected the next time you save it, the filename is there already set. I don't know how they do it. I'll have to look around and see what I can come up with I guess. I just figured it wouldn't do any harm to ask. ;)

This is the dialog that Winamp uses with the previously selected filename already in place. I'm not sure how to get the same thing up, I've not programmed the Windows API much, but I'll look into it.
{"name":"606625","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/a\/ba6f8bfa67feb076b4c5859cead7c36d.png","w":624,"h":474,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/a\/ba6f8bfa67feb076b4c5859cead7c36d"}606625

Go to: