Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_dictionary.c
1
7#define WIDTH 1280
8#define HEIGHT 800
9
10#define ALLEGRO_UNSTABLE 1
11
12#include "nilorea/n_common.h"
13#include "nilorea/n_list.h"
14#include "nilorea/n_hash.h"
15#include "nilorea/n_pcre.h"
16#include "nilorea/n_str.h"
17#include "nilorea/n_allegro5.h"
18#include <allegro5/allegro_ttf.h>
19
20/* dictionnaries are from https://www.bragitoff.com/2016/03/english-dictionary-in-csv-format/ */
21
22ALLEGRO_DISPLAY* display = NULL;
23
24int DONE = 0, /* Flag to check if we are always running */
25 getoptret = 0, /* launch parameter check */
26 log_level = LOG_INFO; /* default LOG_LEVEL */
27
28ALLEGRO_BITMAP* scr_buf = NULL;
29
30ALLEGRO_TIMER* fps_timer = NULL;
31ALLEGRO_TIMER* logic_timer = NULL;
32
33int main(int argc, char* argv[]) {
34 set_log_level(log_level);
35
36 n_log(LOG_NOTICE, "%s is starting ...", argv[0]);
37
38 /* allegro 5 + addons loading */
39 if (!al_init()) {
40 n_abort("Could not init Allegro.\n");
41 }
42 if (!al_init_image_addon()) {
43 n_abort("Unable to initialize image addon\n");
44 }
45 if (!al_init_primitives_addon()) {
46 n_abort("Unable to initialize primitives addon\n");
47 }
48 if (!al_init_font_addon()) {
49 n_abort("Unable to initialize font addon\n");
50 }
51 if (!al_init_ttf_addon()) {
52 n_abort("Unable to initialize ttf_font addon\n");
53 }
54 if (!al_install_keyboard()) {
55 n_abort("Unable to initialize keyboard handler\n");
56 }
57 if (!al_install_mouse()) {
58 n_abort("Unable to initialize mouse handler\n");
59 }
60 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
61
62 event_queue = al_create_event_queue();
63 if (!event_queue) {
64 fprintf(stderr, "failed to create event_queue!\n");
65 al_destroy_display(display);
66 return -1;
67 }
68
69 char ver_str[128] = "";
70 while ((getoptret = getopt(argc, argv, "hvV:L:")) != EOF) {
71 switch (getoptret) {
72 case 'h':
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]);
74 exit(TRUE);
75 case 'v':
76 sprintf(ver_str, "%s %s", __DATE__, __TIME__);
77 exit(TRUE);
78 break;
79 case 'V':
80 if (!strncmp("INFO", optarg, 6)) {
81 log_level = LOG_INFO;
82 } else {
83 if (!strncmp("NOTICE", optarg, 7)) {
84 log_level = LOG_NOTICE;
85 } else {
86 if (!strncmp("ERR", optarg, 5)) {
87 log_level = LOG_ERR;
88 } else {
89 if (!strncmp("DEBUG", optarg, 5)) {
90 log_level = LOG_DEBUG;
91 } else {
92 n_log(LOG_ERR, "%s is not a valid log level\n", optarg);
93 exit(FALSE);
94 }
95 }
96 }
97 }
98 n_log(LOG_NOTICE, "LOG LEVEL UP TO: %d\n", log_level);
99 set_log_level(log_level);
100 break;
101 case 'L':
102 n_log(LOG_NOTICE, "LOG FILE: %s\n", optarg);
103 set_log_file(optarg);
104 break;
105 case '?': {
106 switch (optopt) {
107 case 'V':
108 n_log(LOG_ERR, "\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n");
109 break;
110 case 'L':
111 n_log(LOG_ERR, "\nPlease specify a log file after -L\n");
112 default:
113 break;
114 }
115 }
116 __attribute__((fallthrough));
117 default:
118 n_log(LOG_ERR, "\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[0]);
119 exit(FALSE);
120 }
121 }
122
123 double fps = 60.0;
124 double fps_delta_time = 1.0 / fps;
125 double logic_delta_time = 1.0 / (5.0 * fps);
126
127 fps_timer = al_create_timer(fps_delta_time);
128 logic_timer = al_create_timer(logic_delta_time);
129
130 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
131 display = al_create_display(WIDTH, HEIGHT);
132 if (!display) {
133 n_abort("Unable to create display\n");
134 }
135 al_set_window_title(display, argv[0]);
136
137 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
138
139 DONE = 0;
140
141 enum APP_KEYS {
142 KEY_ESC
143 };
144 int key[1] = {false};
145
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));
151
152 al_register_event_source(event_queue, al_get_keyboard_event_source());
153 al_register_event_source(event_queue, al_get_mouse_event_source());
154
155 ALLEGRO_FONT* font = NULL;
156 font = al_load_font("DATAS/2Dumb.ttf", 24, 0);
157 if (!font) {
158 n_log(LOG_ERR, "Unable to load DATAS/2Dumb.ttf");
159 exit(1);
160 }
161
162 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(WIDTH, HEIGHT);
163
164 al_hide_mouse_cursor(display);
165
166 int mx = 0, my = 0, mz = 0;
167 int mouse_b1 = 0, mouse_b2 = 0;
168 int do_draw = 0, do_logic = 0;
169
171 typedef struct DICTIONARY_DEFINITION {
173 char* type;
175 char* definition;
176 } DICTIONARY_DEFINITION;
177
179 typedef struct DICTIONARY_ENTRY {
181 char* key;
183 LIST* definitions;
184 } DICTIONARY_ENTRY;
185
186 void free_entry_def(void* entry_def) {
187 DICTIONARY_DEFINITION* def = (DICTIONARY_DEFINITION*)entry_def;
188 FreeNoLog(def->type);
189 FreeNoLog(def->definition);
190 FreeNoLog(def);
191 }
192 void free_entry(void* entry_ptr) {
193 DICTIONARY_ENTRY* entry = (DICTIONARY_ENTRY*)entry_ptr;
194 list_destroy(&entry->definitions);
195 FreeNoLog(entry->key);
196 FreeNoLog(entry);
197 }
198
199 /* Load dictionaries */
200 HASH_TABLE* dictionary = new_ht_trie(256, 32);
201 FILE* dict_file = fopen("DATAS/dictionary.csv", "r");
202 char read_buf[16384] = "";
203 char* entry_key = NULL;
204 char* type = NULL;
205 char* definition = NULL;
206 N_PCRE* dico_regexp = npcre_new("\"(.*)\",\"(.*)\",\"(.*)\"", 99, 0);
207
208 while (fgets(read_buf, 16384, dict_file)) {
209 if (npcre_match(read_buf, dico_regexp)) {
210 entry_key = strdup(_str(dico_regexp->match_list[1]));
211 type = strdup(_str(dico_regexp->match_list[2]));
212 definition = strdup(_str(dico_regexp->match_list[3]));
213
214 n_log(LOG_DEBUG, "matched %s , %s , %s", entry_key, type, definition);
215
216 DICTIONARY_ENTRY* entry = NULL;
217 DICTIONARY_DEFINITION* entry_def = NULL;
218
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);
224 } else {
225 Malloc(entry, DICTIONARY_ENTRY, 1);
226
227 entry->definitions = new_generic_list(MAX_LIST_ITEMS);
228 entry->key = strdup(entry_key);
229
230 Malloc(entry_def, DICTIONARY_DEFINITION, 1);
231
232 entry_def->type = strdup(type);
233 entry_def->definition = strdup(definition);
234
235 list_push(entry->definitions, entry_def, &free_entry_def);
236
237 ht_put_ptr(dictionary, entry_key, entry, &free_entry);
238 }
239 FreeNoLog(entry_key);
240 FreeNoLog(type);
241 FreeNoLog(definition);
242
243 npcre_clean_match(dico_regexp);
244 }
245 }
246 fclose(dict_file);
247 npcre_delete(&dico_regexp);
248
249 ALLEGRO_USTR* keyboard_buffer = al_ustr_new("");
250 int key_pressed = 0;
251 n_log(LOG_NOTICE, "Starting main loop");
252
253 al_clear_keyboard_state(NULL);
254 al_flush_event_queue(event_queue);
255
256 int max_results = 100;
257 LIST* completion = ht_get_completion_list(dictionary, al_cstr(keyboard_buffer), max_results);
258
259 do {
260 do {
261 ALLEGRO_EVENT ev;
262
263 al_wait_for_event(event_queue, &ev);
264
265 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
266 switch (ev.keyboard.keycode) {
267 case ALLEGRO_KEY_ESCAPE:
268 key[KEY_ESC] = 1;
269 break;
270 default:
271 break;
272 }
273 } else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
274 switch (ev.keyboard.keycode) {
275 case ALLEGRO_KEY_ESCAPE:
276 key[KEY_ESC] = 0;
277 break;
278 default:
279 break;
280 }
281 } else if (ev.type == ALLEGRO_EVENT_TIMER) {
282 if (al_get_timer_event_source(fps_timer) == ev.any.source) {
283 do_draw = 1;
284 } else if (al_get_timer_event_source(logic_timer) == ev.any.source) {
285 do_logic = 1;
286 }
287 } else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
288 mx = ev.mouse.x;
289 my = ev.mouse.y;
290 mz -= ev.mouse.dz;
291 if (mz < 0) mz = 0;
292 } else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
293 if (ev.mouse.button == 1)
294 mouse_b1 = 1;
295 if (ev.mouse.button == 2)
296 mouse_b2 = 1;
297 } else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
298 if (ev.mouse.button == 1)
299 mouse_b1 = 0;
300 if (ev.mouse.button == 2)
301 mouse_b2 = 0;
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);
305 } else {
306 /* Processing inputs */
307 if (get_keyboard(keyboard_buffer, ev) == TRUE)
308 key_pressed = 1;
309 }
310 } while (!al_is_event_queue_empty(event_queue));
311
312 if (do_logic == 1) {
313 if (key_pressed == 1) {
314 key_pressed = 0;
315 mz = 0;
316 if (completion) {
317 list_destroy(&completion);
318 }
319 completion = ht_get_completion_list(dictionary, al_cstr(keyboard_buffer), max_results);
320 }
321
322 if (mouse_b1 == 1) {
323 if (completion) {
324 size_t skip_entry = mz;
325 if (skip_entry >= completion->nb_items) {
326 skip_entry = completion->nb_items - 1;
327 mz = skip_entry;
328 }
329 list_foreach(node, completion) {
330 entry_key = (char*)node->ptr;
331 if (skip_entry == 0) {
332 break;
333 }
334 skip_entry--;
335 }
336 al_ustr_free(keyboard_buffer);
337 keyboard_buffer = al_ustr_new(entry_key);
338 }
339 if (completion) {
340 list_destroy(&completion);
341 }
342 completion = ht_get_completion_list(dictionary, al_cstr(keyboard_buffer), max_results);
343 mz = 0;
344 mouse_b1 = 0;
345 }
346 if (mouse_b2 == 1) {
347 int pos = (int)al_ustr_size(keyboard_buffer);
348 if (al_ustr_prev(keyboard_buffer, &pos)) {
349 al_ustr_truncate(keyboard_buffer, pos);
350 }
351 if (completion) {
352 list_destroy(&completion);
353 }
354 completion = ht_get_completion_list(dictionary, al_cstr(keyboard_buffer), max_results);
355 mouse_b2 = 0;
356 }
357
358 do_logic = 0;
359 }
360
361 if (do_draw == 1) {
362 static int last_time = 0;
363
364 al_acknowledge_resize(display);
365 // int w = al_get_display_width( display );
366 // int h = al_get_display_height( display );
367
368 al_set_target_bitmap(scrbuf);
369 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
370
371 last_time -= 1000000 / 30.0;
372 static int length = 0;
373 if (last_time < 0) {
374 last_time = 250000;
375 } else {
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));
380 }
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));
383
384 DICTIONARY_ENTRY* entry = NULL;
385
386 if (completion) {
387 int pos_x = (WIDTH / 2) + (input_size / 2) + 50;
388 int pos_y = 50;
389 size_t skip_entry = mz;
390 if (skip_entry >= completion->nb_items) {
391 skip_entry = completion->nb_items - 1;
392 mz = skip_entry;
393 }
394
395 list_foreach(node, completion) {
396 if (skip_entry > 0) {
397 skip_entry--;
398 continue;
399 }
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);
402 pos_y += 35;
403 }
404 }
405
406 if (ht_get_ptr(dictionary, al_cstr(keyboard_buffer), (void**)&entry) == TRUE) {
407 int vertical_it = 0;
408 list_foreach(node, entry->definitions) {
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);
411 vertical_it += 50;
412 }
413 }
414
415 al_set_target_bitmap(al_get_backbuffer(display));
416 al_draw_bitmap(scrbuf, 0, 0, 0);
417
418 /* mouse pointer */
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);
421
422 al_flip_display();
423 do_draw = 0;
424 }
425
426 } while (!key[KEY_ESC] && !DONE);
427
428 destroy_ht(&dictionary);
429 if (completion)
430 list_destroy(&completion);
431
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();
439
440 return 0;
441}
int get_keyboard(ALLEGRO_USTR *str, ALLEGRO_EVENT event)
update a keyboard buffer from an event
Definition n_allegro5.c:17
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:251
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:187
#define _str(__PTR)
define true
Definition n_common.h:176
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:38
int ht_get_ptr(HASH_TABLE *table, const char *key, void **val)
get pointer at 'key' from 'table'
Definition n_hash.c:2047
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
Definition n_hash.c:2180
LIST * ht_get_completion_list(HASH_TABLE *table, const char *keybud, size_t max_results)
get next matching keys in table tree
Definition n_hash.c:2335
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
Definition n_hash.c:1902
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
Definition n_hash.c:2100
structure of a hash table
Definition n_hash.h:114
size_t nb_items
number of item currently in the list
Definition n_list.h:41
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:199
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper.
Definition n_list.h:65
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:518
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Definition n_list.h:55
Structure of a generic LIST container.
Definition n_list.h:39
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_DEBUG
debug-level messages
Definition n_log.h:64
#define LOG_ERR
error conditions
Definition n_log.h:56
int set_log_file(char *file)
Set the logging to a file instead of stderr.
Definition n_log.c:120
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:91
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:60
#define LOG_INFO
informational
Definition n_log.h:62
const char ** match_list
populated match list if nPcreCapMatch is called
Definition n_pcre.h:40
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...
Definition n_pcre.c:58
int npcre_match(char *str, N_PCRE *pcre)
Return TRUE if str matches regexp, and make captures up to max_cap.
Definition n_pcre.c:149
int npcre_clean_match(N_PCRE *pcre)
clean the match list of the last capture, if any
Definition n_pcre.c:131
int npcre_delete(N_PCRE **pcre)
Free a N_PCRE pointer.
Definition n_pcre.c:101
N_PCRE structure.
Definition n_pcre.h:27
Allegro5 helpers.
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.