10#define ALLEGRO_UNSTABLE 1
18#include <allegro5/allegro_ttf.h>
22ALLEGRO_DISPLAY* display = NULL;
28ALLEGRO_BITMAP* scr_buf = NULL;
30ALLEGRO_TIMER* fps_timer = NULL;
31ALLEGRO_TIMER* logic_timer = NULL;
33int main(
int argc,
char* argv[]) {
40 n_abort(
"Could not init Allegro.\n");
42 if (!al_init_image_addon()) {
43 n_abort(
"Unable to initialize image addon\n");
45 if (!al_init_primitives_addon()) {
46 n_abort(
"Unable to initialize primitives addon\n");
48 if (!al_init_font_addon()) {
49 n_abort(
"Unable to initialize font addon\n");
51 if (!al_init_ttf_addon()) {
52 n_abort(
"Unable to initialize ttf_font addon\n");
54 if (!al_install_keyboard()) {
55 n_abort(
"Unable to initialize keyboard handler\n");
57 if (!al_install_mouse()) {
58 n_abort(
"Unable to initialize mouse handler\n");
60 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
62 event_queue = al_create_event_queue();
64 fprintf(stderr,
"failed to create event_queue!\n");
65 al_destroy_display(display);
69 char ver_str[128] =
"";
70 while ((getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
73 n_log(
LOG_NOTICE,
"\n %s -h help -v version -V LOG_LEVEL (NOLOG,INFO,NOTICE,ERR,DEBUG) -L OPT_LOG_FILE\n", argv[0]);
76 sprintf(ver_str,
"%s %s", __DATE__, __TIME__);
80 if (!strncmp(
"INFO", optarg, 6)) {
83 if (!strncmp(
"NOTICE", optarg, 7)) {
86 if (!strncmp(
"ERR", optarg, 5)) {
89 if (!strncmp(
"DEBUG", optarg, 5)) {
108 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n");
111 n_log(
LOG_ERR,
"\nPlease specify a log file after -L\n");
116 __attribute__((fallthrough));
118 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[0]);
124 double fps_delta_time = 1.0 / fps;
125 double logic_delta_time = 1.0 / (5.0 * fps);
127 fps_timer = al_create_timer(fps_delta_time);
128 logic_timer = al_create_timer(logic_delta_time);
130 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
131 display = al_create_display(WIDTH, HEIGHT);
133 n_abort(
"Unable to create display\n");
135 al_set_window_title(display, argv[0]);
137 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
144 int key[1] = {
false};
146 al_register_event_source(event_queue, al_get_display_event_source(display));
147 al_start_timer(fps_timer);
148 al_start_timer(logic_timer);
149 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer));
150 al_register_event_source(event_queue, al_get_timer_event_source(logic_timer));
152 al_register_event_source(event_queue, al_get_keyboard_event_source());
153 al_register_event_source(event_queue, al_get_mouse_event_source());
155 ALLEGRO_FONT* font = NULL;
156 font = al_load_font(
"DATAS/2Dumb.ttf", 24, 0);
162 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(WIDTH, HEIGHT);
164 al_hide_mouse_cursor(display);
166 int mx = 0, my = 0, mz = 0;
167 int mouse_b1 = 0, mouse_b2 = 0;
168 int do_draw = 0, do_logic = 0;
171 typedef struct DICTIONARY_DEFINITION {
176 } DICTIONARY_DEFINITION;
179 typedef struct DICTIONARY_ENTRY {
186 void free_entry_def(
void* entry_def) {
187 DICTIONARY_DEFINITION* def = (DICTIONARY_DEFINITION*)entry_def;
192 void free_entry(
void* entry_ptr) {
193 DICTIONARY_ENTRY* entry = (DICTIONARY_ENTRY*)entry_ptr;
201 FILE* dict_file = fopen(
"DATAS/dictionary.csv",
"r");
202 char read_buf[16384] =
"";
203 char* entry_key = NULL;
205 char* definition = NULL;
208 while (fgets(read_buf, 16384, dict_file)) {
214 n_log(
LOG_DEBUG,
"matched %s , %s , %s", entry_key, type, definition);
216 DICTIONARY_ENTRY* entry = NULL;
217 DICTIONARY_DEFINITION* entry_def = NULL;
219 if (
ht_get_ptr(dictionary, entry_key, (
void**)&entry) == TRUE) {
220 Malloc(entry_def, DICTIONARY_DEFINITION, 1);
221 entry_def->type = strdup(type);
222 entry_def->definition = strdup(definition);
223 list_push(entry->definitions, entry_def, &free_entry_def);
225 Malloc(entry, DICTIONARY_ENTRY, 1);
228 entry->key = strdup(entry_key);
230 Malloc(entry_def, DICTIONARY_DEFINITION, 1);
232 entry_def->type = strdup(type);
233 entry_def->definition = strdup(definition);
235 list_push(entry->definitions, entry_def, &free_entry_def);
237 ht_put_ptr(dictionary, entry_key, entry, &free_entry);
249 ALLEGRO_USTR* keyboard_buffer = al_ustr_new(
"");
253 al_clear_keyboard_state(NULL);
254 al_flush_event_queue(event_queue);
256 int max_results = 100;
263 al_wait_for_event(event_queue, &ev);
265 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
266 switch (ev.keyboard.keycode) {
267 case ALLEGRO_KEY_ESCAPE:
273 }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
274 switch (ev.keyboard.keycode) {
275 case ALLEGRO_KEY_ESCAPE:
281 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
282 if (al_get_timer_event_source(fps_timer) == ev.any.source) {
284 }
else if (al_get_timer_event_source(logic_timer) == ev.any.source) {
287 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
292 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
293 if (ev.mouse.button == 1)
295 if (ev.mouse.button == 2)
297 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
298 if (ev.mouse.button == 1)
300 if (ev.mouse.button == 2)
302 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
303 al_clear_keyboard_state(display);
304 al_flush_event_queue(event_queue);
310 }
while (!al_is_event_queue_empty(event_queue));
313 if (key_pressed == 1) {
324 size_t skip_entry = mz;
325 if (skip_entry >= completion->
nb_items) {
326 skip_entry = completion->
nb_items - 1;
330 entry_key = (
char*)node->ptr;
331 if (skip_entry == 0) {
336 al_ustr_free(keyboard_buffer);
337 keyboard_buffer = al_ustr_new(entry_key);
347 int pos = (int)al_ustr_size(keyboard_buffer);
348 if (al_ustr_prev(keyboard_buffer, &pos)) {
349 al_ustr_truncate(keyboard_buffer, pos);
362 static int last_time = 0;
364 al_acknowledge_resize(display);
368 al_set_target_bitmap(scrbuf);
369 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
371 last_time -= 1000000 / 30.0;
372 static int length = 0;
376 length = al_get_text_width(font, al_cstr(keyboard_buffer));
377 al_draw_filled_rectangle(WIDTH / 2 + (length + 6) / 2, 50,
378 WIDTH / 2 + (length + 6) / 2 + 15, 70,
379 al_map_rgb(0, 128, 0));
381 int input_size = al_get_text_width(font, al_cstr(keyboard_buffer));
382 al_draw_text(font, al_map_rgb(0, 255, 0), (WIDTH / 2) - (input_size / 2), 50, ALLEGRO_ALIGN_LEFT, al_cstr(keyboard_buffer));
384 DICTIONARY_ENTRY* entry = NULL;
387 int pos_x = (WIDTH / 2) + (input_size / 2) + 50;
389 size_t skip_entry = mz;
390 if (skip_entry >= completion->
nb_items) {
391 skip_entry = completion->
nb_items - 1;
396 if (skip_entry > 0) {
400 entry_key = (
char*)node->ptr;
401 al_draw_text(font, al_map_rgb(230, 230, 0), pos_x, pos_y, ALLEGRO_ALIGN_LEFT, entry_key);
406 if (
ht_get_ptr(dictionary, al_cstr(keyboard_buffer), (
void**)&entry) == TRUE) {
409 DICTIONARY_DEFINITION* dictionary_definition = (DICTIONARY_DEFINITION*)node->ptr;
410 al_draw_multiline_textf(font, al_map_rgb(0, 255, 0), 10, 100 + vertical_it, WIDTH - 10, 20, ALLEGRO_ALIGN_LEFT,
"%s : %s", dictionary_definition->type, dictionary_definition->definition);
415 al_set_target_bitmap(al_get_backbuffer(display));
416 al_draw_bitmap(scrbuf, 0, 0, 0);
419 al_draw_line(mx - 5, my, mx + 5, my, al_map_rgb(255, 0, 0), 1);
420 al_draw_line(mx, my + 5, mx, my - 5, al_map_rgb(255, 0, 0), 1);
426 }
while (!key[KEY_ESC] && !DONE);
432 al_ustr_free(keyboard_buffer);
433 al_destroy_event_queue(event_queue);
434 al_destroy_font(font);
435 al_destroy_timer(fps_timer);
436 al_destroy_timer(logic_timer);
437 al_destroy_bitmap(scr_buf);
438 al_uninstall_system();
int get_keyboard(ALLEGRO_USTR *str, ALLEGRO_EVENT event)
update a keyboard buffer from an event
#define FreeNoLog(__ptr)
Free Handler without log.
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define _str(__PTR)
define true
void n_abort(char const *format,...)
abort program with a text
int ht_get_ptr(HASH_TABLE *table, const char *key, void **val)
get pointer at 'key' from 'table'
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
LIST * ht_get_completion_list(HASH_TABLE *table, const char *keybud, size_t max_results)
get next matching keys in table tree
HASH_TABLE * new_ht_trie(size_t alphabet_length, size_t alphabet_offset)
create a TRIE hash table with the alphabet_size, each key value beeing decreased by alphabet_offset
int ht_put_ptr(HASH_TABLE *table, const char *key, void *ptr, void(*destructor)(void *ptr))
put a pointer to the string value with given key in the targeted hash table
structure of a hash table
size_t nb_items
number of item currently in the list
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper.
int list_destroy(LIST **list)
Empty and Free a list container.
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Structure of a generic LIST container.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
int set_log_file(char *file)
Set the logging to a file instead of stderr.
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_INFO
informational
const char ** match_list
populated match list if nPcreCapMatch is called
N_PCRE * npcre_new(char *str, int max_cap, int flags)
From pcre doc, the flag bits are: PCRE_ANCHORED Force pattern anchoring PCRE_AUTO_CALLOUT Compile aut...
int npcre_match(char *str, N_PCRE *pcre)
Return TRUE if str matches regexp, and make captures up to max_cap.
int npcre_clean_match(N_PCRE *pcre)
clean the match list of the last capture, if any
int npcre_delete(N_PCRE **pcre)
Free a N_PCRE pointer.
Common headers and low-level functions & define.
Hash functions and table.
List structures and definitions.
PCRE helpers for regex matching.
N_STR and string function declaration.