35int main(
int argc,
char* argv[]) {
42 n_abort(
"Could not init Allegro.\n");
44 if (!al_init_image_addon()) {
45 n_abort(
"Unable to initialize image addon\n");
47 if (!al_init_primitives_addon()) {
48 n_abort(
"Unable to initialize primitives addon\n");
50 if (!al_init_font_addon()) {
51 n_abort(
"Unable to initialize font addon\n");
53 if (!al_init_ttf_addon()) {
54 n_abort(
"Unable to initialize ttf_font addon\n");
56 if (!al_install_keyboard()) {
57 n_abort(
"Unable to initialize keyboard handler\n");
59 if (!al_install_mouse()) {
60 n_abort(
"Unable to initialize mouse handler\n");
62 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
64 event_queue = al_create_event_queue();
66 fprintf(stderr,
"failed to create event_queue!\n");
71 char ver_str[128] =
"";
72 while ((
getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
75 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]);
78 sprintf(ver_str,
"%s %s", __DATE__, __TIME__);
82 if (!strncmp(
"INFO", optarg, 6)) {
85 if (!strncmp(
"NOTICE", optarg, 7)) {
88 if (!strncmp(
"ERR", optarg, 5)) {
91 if (!strncmp(
"DEBUG", optarg, 5)) {
110 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n");
113 n_log(
LOG_ERR,
"\nPlease specify a log file after -L\n");
120 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[0]);
126 double fps_delta_time = 1.0 / fps;
127 double logic_delta_time = 1.0 / (5.0 * fps);
129 fps_timer = al_create_timer(fps_delta_time);
132 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
135 n_abort(
"Unable to create display\n");
137 al_set_window_title(
display, argv[0]);
139 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
146 int key[1] = {
false};
148 al_register_event_source(event_queue, al_get_display_event_source(
display));
151 al_register_event_source(event_queue, al_get_timer_event_source(
fps_timer));
152 al_register_event_source(event_queue, al_get_timer_event_source(
logic_timer));
154 al_register_event_source(event_queue, al_get_keyboard_event_source());
155 al_register_event_source(event_queue, al_get_mouse_event_source());
157 ALLEGRO_FONT* font = NULL;
158 font = al_load_font(
"DATAS/2Dumb.ttf", 24, 0);
164 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(
WIDTH,
HEIGHT);
168 int mx = 0, my = 0, mz = 0;
169 int mouse_b1 = 0, mouse_b2 = 0;
170 int do_draw = 0, do_logic = 0;
173 typedef struct DICTIONARY_DEFINITION {
178 } DICTIONARY_DEFINITION;
181 typedef struct DICTIONARY_ENTRY {
188 void free_entry_def(
void* entry_def) {
189 DICTIONARY_DEFINITION* def = (DICTIONARY_DEFINITION*)entry_def;
194 void free_entry(
void* entry_ptr) {
195 DICTIONARY_ENTRY* entry = (DICTIONARY_ENTRY*)entry_ptr;
203 FILE* dict_file = fopen(
"DATAS/dictionary.csv",
"r");
204 char read_buf[16384] =
"";
205 char* entry_key = NULL;
207 char* definition = NULL;
210 while (fgets(read_buf, 16384, dict_file)) {
216 n_log(
LOG_DEBUG,
"matched %s , %s , %s", entry_key, type, definition);
218 DICTIONARY_ENTRY* entry = NULL;
219 DICTIONARY_DEFINITION* entry_def = NULL;
221 if (
ht_get_ptr(dictionary, entry_key, (
void**)&entry) == TRUE) {
222 Malloc(entry_def, DICTIONARY_DEFINITION, 1);
223 entry_def->type = strdup(type);
224 entry_def->definition = strdup(definition);
225 list_push(entry->definitions, entry_def, &free_entry_def);
227 Malloc(entry, DICTIONARY_ENTRY, 1);
230 entry->key = strdup(entry_key);
232 Malloc(entry_def, DICTIONARY_DEFINITION, 1);
234 entry_def->type = strdup(type);
235 entry_def->definition = strdup(definition);
237 list_push(entry->definitions, entry_def, &free_entry_def);
239 ht_put_ptr(dictionary, entry_key, entry, &free_entry);
251 ALLEGRO_USTR* keyboard_buffer = al_ustr_new(
"");
255 al_clear_keyboard_state(NULL);
256 al_flush_event_queue(event_queue);
258 int max_results = 100;
265 al_wait_for_event(event_queue, &ev);
267 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
268 switch (ev.keyboard.keycode) {
269 case ALLEGRO_KEY_ESCAPE:
275 }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
276 switch (ev.keyboard.keycode) {
277 case ALLEGRO_KEY_ESCAPE:
283 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
284 if (al_get_timer_event_source(
fps_timer) == ev.any.source) {
286 }
else if (al_get_timer_event_source(
logic_timer) == ev.any.source) {
289 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
294 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
295 if (ev.mouse.button == 1)
297 if (ev.mouse.button == 2)
299 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
300 if (ev.mouse.button == 1)
302 if (ev.mouse.button == 2)
304 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
305 al_clear_keyboard_state(
display);
306 al_flush_event_queue(event_queue);
312 }
while (!al_is_event_queue_empty(event_queue));
315 if (key_pressed == 1) {
326 size_t skip_entry = mz;
327 if (skip_entry >= completion->
nb_items) {
328 skip_entry = completion->
nb_items - 1;
332 entry_key = (
char*)node->ptr;
333 if (skip_entry == 0) {
338 al_ustr_free(keyboard_buffer);
339 keyboard_buffer = al_ustr_new(entry_key);
349 int pos = (int)al_ustr_size(keyboard_buffer);
350 if (al_ustr_prev(keyboard_buffer, &pos)) {
351 al_ustr_truncate(keyboard_buffer, pos);
364 static int last_time = 0;
366 al_acknowledge_resize(
display);
370 al_set_target_bitmap(scrbuf);
371 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
373 last_time -= 1000000 / 30.0;
374 static int length = 0;
378 length = al_get_text_width(font, al_cstr(keyboard_buffer));
379 al_draw_filled_rectangle(
WIDTH / 2 + (length + 6) / 2, 50,
380 WIDTH / 2 + (length + 6) / 2 + 15, 70,
381 al_map_rgb(0, 128, 0));
383 int input_size = al_get_text_width(font, al_cstr(keyboard_buffer));
384 al_draw_text(font, al_map_rgb(0, 255, 0), (
WIDTH / 2) - (input_size / 2), 50, ALLEGRO_ALIGN_LEFT, al_cstr(keyboard_buffer));
386 DICTIONARY_ENTRY* entry = NULL;
389 int pos_x = (
WIDTH / 2) + (input_size / 2) + 50;
391 size_t skip_entry = mz;
392 if (skip_entry >= completion->
nb_items) {
393 skip_entry = completion->
nb_items - 1;
398 if (skip_entry > 0) {
402 entry_key = (
char*)node->ptr;
403 al_draw_text(font, al_map_rgb(230, 230, 0), pos_x, pos_y, ALLEGRO_ALIGN_LEFT, entry_key);
408 if (
ht_get_ptr(dictionary, al_cstr(keyboard_buffer), (
void**)&entry) == TRUE) {
411 DICTIONARY_DEFINITION* dictionary_definition = (DICTIONARY_DEFINITION*)node->ptr;
412 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);
417 al_set_target_bitmap(al_get_backbuffer(
display));
418 al_draw_bitmap(scrbuf, 0, 0, 0);
421 al_draw_line(mx - 5, my, mx + 5, my, al_map_rgb(255, 0, 0), 1);
422 al_draw_line(mx, my + 5, mx, my - 5, al_map_rgb(255, 0, 0), 1);
428 }
while (!
key[KEY_ESC] && !
DONE);
434 al_ustr_free(keyboard_buffer);
435 al_destroy_event_queue(event_queue);
436 al_destroy_font(font);
440 al_uninstall_system();