on_ and file_dialog

Posted By: 3run

on_ and file_dialog - 04/28/18 23:53

I have two functions, let's call them 'save' and 'load'. In them I use 'file_dialog' and 'file_dialog_save' in order to save/load a file. To call those functions I use f4 (to call 'save') and f5 (to call 'load'). All is great, but I noticed that after first two calls, in order to call those functions I have to press f4 and f5 twice! Can anyone please explain why?

Best regards!

Edit: also noticed, that this results to not being able to close game's window by clicking on 'close' X at the upper right corner.. Could this be a memory issue? Pointers and such?

Edit2: thx God, this has nothing to do with my map editor in general, here is a small example, that has the same problem:
Code:
#include <acknex.h>
#include <default.c>
#include <windows.h>
#include <strio.c>

#define PRAGMA_POINTER

BMAP *temp_bmp = NULL;

void save_level(){
	
	char *map_file = file_dialog_save("Save level", "*.png");	
	if(map_file == NULL){ return(-1); }
	
	STRING *file_name_str = "";
	STRING *path_str = str_create(map_file);
	var e_pos = str_stri(path_str, ".png");
	
	if(e_pos == 0){
		
		var length = str_len(path_str);
		var n_pos = str_chr(path_str, -length, 92);
		str_cut(file_name_str, path_str, n_pos + 1, length);
		
	}
	else{
		
		var n_pos = str_chr(path_str, -e_pos, 92);
		str_cut(file_name_str, path_str, n_pos + 1, e_pos - 1);
		
	}
	
	str_cat(file_name_str, ".png");
	
	bmap_save(temp_bmp, file_name_str);
	
}

void load_level(){
	
	char *map_file = file_dialog("Load level", "*.png");
	if(map_file == NULL){ return; }	
	
	STRING *file_name_str = "";
	STRING *path_str = str_create(map_file);
	var e_pos = str_stri(path_str, ".png");
	
	if(e_pos == 0){
		
		var length = str_len(path_str);
		var n_pos = str_chr(path_str, -length, 92);
		str_cut(file_name_str, path_str, n_pos + 1, length);
		
	}
	else{
		
		var n_pos = str_chr(path_str, -e_pos, 92);
		str_cut(file_name_str, path_str, n_pos + 1, e_pos - 1);
		
	}
	
	str_cat(file_name_str, ".png");
	
	if(!file_exists(file_name_str)){ return; }
	temp_bmp = bmap_create(file_name_str);
	
}

void remove_level(){
	
	if(temp_bmp){ safe_remove(temp_bmp); }
	
}

void main(){
	
	on_f3 = remove_level;
	on_f4 = save_level;
	on_f5 = load_level;
	
	fps_max = 60;
	warn_level = 6;
	level_load("");
	wait(3);
	
	temp_bmp = bmap_createblack(256, 64, 32);
	bmap_fill(temp_bmp, COLOR_RED, 100);
	
	while(!key_esc){
		
		draw_text("F3 - remove\nF4 - save\nF5 - load", 10, 10, COLOR_WHITE);
		
		if(temp_bmp){
			
			draw_quad(temp_bmp, vector(256, 256, 0), NULL, NULL, NULL, NULL, 100, 0);
			
		}
		
		wait(1);
		
	}
	
	sys_exit("");
	
}

Posted By: Kartoffel

Re: on_ and file_dialog - 04/29/18 08:24

It's because acknex doesn't recognize the key being released when another window is in focus.

One cheap solution would be to wait until the key is released. Otherwise, using the windows API directly for input should work aswell.
Posted By: txesmi

Re: on_ and file_dialog - 04/29/18 09:34

Hi,
that happens when you open any new window (file explorer, printf, error, etc) with a pressed key. The engine does not reset its pilot variable (key_... and key_any aswell), so they remain true although the new window is closed. I guess that happens because the key releasing windows event is received by the new window instead of its parent so engines key_... variables manager is never executed for that event. I thought that calling the engines windows callback function with the proper parameters might work but no luck.

Code:
// global prototype
UINT CALLBACK myMsgHandler (HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam);
// save engines callback function
myMsgHandler = on_message;
// call it on_f5 event
myMsgHandler (hWnd, WM_KEYUP, VK_F5, 0);



Salud!

edited________
ups, late...
Posted By: 3run

Re: on_ and file_dialog - 04/29/18 09:46

Hi guys!

Thank you for suggestions! Does it mean that windows API won't help here?

Edit: yes, 'key_pressed' shows that f4 and f5 are remaining pressed when file dialog windows get's closed...

Edit2: I found this in manual for 'key_pressed'
Quote:
number - scancode of the key to check, 8.30 or -1 for resetting all keys to the nonpressed state.
So maybe I need to call 'key_pressed(-1)' to reset all keys, right after the file windows was closed ?

Edit3: grin yes, this helped. Just call 'key_pressed(-1)' right after 'file_dialog' function. As a prove, here is edited example from first post:
Code:
#include <acknex.h>
#include <default.c>
#include <windows.h>
#include <strio.c>

#define PRAGMA_POINTER

BMAP *temp_bmp = NULL;

void save_level(){
	
	char *map_file = file_dialog_save("Save level", "*.png");	
	key_pressed(-1);
	if(map_file == NULL){ return(-1); }
	
	STRING *file_name_str = "";
	STRING *path_str = str_create(map_file);
	var e_pos = str_stri(path_str, ".png");
	
	if(e_pos == 0){
		
		var length = str_len(path_str);
		var n_pos = str_chr(path_str, -length, 92);
		str_cut(file_name_str, path_str, n_pos + 1, length);
		
	}
	else{
		
		var n_pos = str_chr(path_str, -e_pos, 92);
		str_cut(file_name_str, path_str, n_pos + 1, e_pos - 1);
		
	}
	
	str_cat(file_name_str, ".png");
	
	bmap_save(temp_bmp, file_name_str);
	
}

void load_level(){
	
	char *map_file = file_dialog("Load level", "*.png");
	key_pressed(-1);
	if(map_file == NULL){ return; }	
	
	STRING *file_name_str = "";
	STRING *path_str = str_create(map_file);
	var e_pos = str_stri(path_str, ".png");
	
	if(e_pos == 0){
		
		var length = str_len(path_str);
		var n_pos = str_chr(path_str, -length, 92);
		str_cut(file_name_str, path_str, n_pos + 1, length);
		
	}
	else{
		
		var n_pos = str_chr(path_str, -e_pos, 92);
		str_cut(file_name_str, path_str, n_pos + 1, e_pos - 1);
		
	}
	
	str_cat(file_name_str, ".png");
	
	if(!file_exists(file_name_str)){ return; }
	temp_bmp = bmap_create(file_name_str);
	
}

void remove_level(){
	
	if(temp_bmp){ safe_remove(temp_bmp); }
	
}

void main(){
	
	on_f3 = remove_level;
	on_f4 = save_level;
	on_f5 = load_level;
	
	fps_max = 60;
	warn_level = 6;
	level_load("");
	wait(3);
	
	temp_bmp = bmap_createblack(256, 64, 32);
	bmap_fill(temp_bmp, COLOR_RED, 100);
	
	while(!key_esc){
		
		draw_text("F3 - removenF4 - savenF5 - load", 10, 10, COLOR_WHITE);
		DEBUG_VAR(key_pressed(62), 100);
		DEBUG_VAR(key_pressed(63), 130);
		
		if(temp_bmp){
			
			draw_quad(temp_bmp, vector(256, 256, 0), NULL, NULL, NULL, NULL, 100, 0);
			
		}
		
		wait(1);
		
	}
	
	sys_exit("");
	
}

Thank you for clearing things out guys! Without you I wouldn't even understand why this happened. Was thinking about memory/pointers related issues grin
Posted By: txesmi

Re: on_ and file_dialog - 04/29/18 10:00

you hit the clove! cool

I had found this problem last week and solved it by Kartoffels suggestion. Much better now, thanks to you!
Posted By: 3run

Re: on_ and file_dialog - 04/29/18 10:18

Originally Posted By: txesmi
you hit the clove! cool
at least once I did grin ahah, thank you guys!

My best regards!
© 2024 lite-C Forums