30#define ALLEGRO_UNSTABLE 1
38#include <allegro5/allegro_ttf.h>
109 if (!selected)
return;
142 char* entry_key = (
char*)node->ptr;
151 char buf[16384] =
"";
155 int written = snprintf(buf + offset,
sizeof(buf) - offset,
"%s : %s\n", def->
type, def->
definition);
156 if (written > 0) offset += (size_t)written;
157 if (offset >=
sizeof(buf) - 1)
break;
165int main(
int argc,
char* argv[]) {
172 n_abort(
"Could not init Allegro.\n");
174 if (!al_init_image_addon()) {
175 n_abort(
"Unable to initialize image addon\n");
177 if (!al_init_primitives_addon()) {
178 n_abort(
"Unable to initialize primitives addon\n");
180 if (!al_init_font_addon()) {
181 n_abort(
"Unable to initialize font addon\n");
183 if (!al_init_ttf_addon()) {
184 n_abort(
"Unable to initialize ttf_font addon\n");
186 if (!al_install_keyboard()) {
187 n_abort(
"Unable to initialize keyboard handler\n");
189 if (!al_install_mouse()) {
190 n_abort(
"Unable to initialize mouse handler\n");
192 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
194 event_queue = al_create_event_queue();
196 fprintf(stderr,
"failed to create event_queue!\n");
201 char ver_str[128] =
"";
202 while ((
getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
205 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]);
208 sprintf(ver_str,
"%s %s", __DATE__, __TIME__);
212 if (!strncmp(
"INFO", optarg, 6)) {
215 if (!strncmp(
"NOTICE", optarg, 7)) {
218 if (!strncmp(
"ERR", optarg, 5)) {
221 if (!strncmp(
"DEBUG", optarg, 5)) {
224 n_log(
LOG_ERR,
"%s is not a valid log level\n", optarg);
240 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n");
243 n_log(
LOG_ERR,
"\nPlease specify a log file after -L\n");
250 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[0]);
256 double fps_delta_time = 1.0 / fps;
258 fps_timer = al_create_timer(fps_delta_time);
260 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
263 n_abort(
"Unable to create display\n");
265 al_set_window_title(
display, argv[0]);
267 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
274 int key[1] = {
false};
276 al_register_event_source(event_queue, al_get_display_event_source(
display));
278 al_register_event_source(event_queue, al_get_timer_event_source(
fps_timer));
280 al_register_event_source(event_queue, al_get_keyboard_event_source());
281 al_register_event_source(event_queue, al_get_mouse_event_source());
283 ALLEGRO_FONT* font = NULL;
284 font = al_load_font(
"DATAS/2Dumb.ttf", 18, 0);
286 n_log(
LOG_ERR,
"Unable to load DATAS/2Dumb.ttf, using builtin font");
287 font = al_create_builtin_font();
294 FILE* dict_file = fopen(
"DATAS/dictionary.csv",
"r");
296 n_abort(
"Unable to open DATAS/dictionary.csv\n");
298 char read_buf[16384] =
"";
299 char* entry_key = NULL;
301 char* definition = NULL;
304 while (fgets(read_buf, 16384, dict_file)) {
317 entry_def->
type = strdup(type);
324 entry->
key = strdup(entry_key);
328 entry_def->
type = strdup(type);
381 al_clear_keyboard_state(NULL);
382 al_flush_event_queue(event_queue);
386 al_wait_for_event(event_queue, &ev);
394 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
395 switch (ev.keyboard.keycode) {
396 case ALLEGRO_KEY_ESCAPE:
402 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
404 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
405 al_acknowledge_resize(
display);
407 (
float)al_get_display_height(
display));
408 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
409 al_clear_keyboard_state(
display);
410 al_flush_event_queue(event_queue);
413 if (ev.type == ALLEGRO_EVENT_TIMER) {
417 if (do_draw && al_is_event_queue_empty(event_queue)) {
418 al_set_target_bitmap(al_get_backbuffer(
display));
419 al_clear_to_color(al_map_rgba(20, 20, 25, 255));
428 }
while (!
key[KEY_ESC] && !
DONE);
435 al_destroy_event_queue(event_queue);
436 al_destroy_font(font);
438 al_uninstall_system();
ALLEGRO_TIMER * fps_timer
ALLEGRO_TIMER * logic_timer
ALLEGRO_DISPLAY * display
char * definition
content of definition
static int search_textarea_id
void free_entry(void *entry_ptr)
LIST * definitions
list of DICTIONARY_DEFINITION for that entry
void on_search_change(int widget_id, const char *text, void *user_data)
static int definition_win_id
void on_clear_click(int widget_id, void *user_data)
void on_completion_select(int widget_id, int index, int selected, void *user_data)
static int status_label_id
static int definition_label_id
char * key
key of the entry
char * type
type of definition (verb, noun,...)
static void update_completion(N_GUI_CTX *gui, const char *text)
static int completion_listbox_id
void free_entry_def(void *entry_def)
static size_t max_results
static void update_definitions(N_GUI_CTX *gui, const char *word)
static HASH_TABLE * dictionary
static char last_search[4096]
dictionary definition for DICTIONARY_ENTRY
#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
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
Set the display (viewport) size for global scrollbar computation.
#define N_GUI_ALIGN_LEFT
left aligned text
void n_gui_textarea_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text content of a textarea widget
void n_gui_label_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text of a label widget
int n_gui_add_listbox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int selection_mode, void(*on_select)(int, int, int, void *), void *user_data)
Add a listbox widget.
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
#define N_GUI_WIN_FIXED_POSITION
disable window dragging (default:enable)
int n_gui_listbox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a listbox widget
void n_gui_set_virtual_size(N_GUI_CTX *ctx, float w, float h)
Set the virtual canvas size for resolution-independent scaling.
#define N_GUI_SELECT_SINGLE
single item selection
void n_gui_draw(N_GUI_CTX *ctx)
Draw all visible windows and their widgets.
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
const char * n_gui_listbox_get_item_text(N_GUI_CTX *ctx, int widget_id, int index)
get the text of a listbox item
void n_gui_listbox_clear(N_GUI_CTX *ctx, int widget_id)
remove all items from a listbox widget
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
#define N_GUI_ALIGN_JUSTIFIED
justified text (spread words to fill width)
int n_gui_add_textarea(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int multiline, size_t char_limit, void(*on_change)(int, const char *, void *), void *user_data)
Add a text area widget.
int n_gui_add_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, void(*on_click)(int, void *), void *user_data)
Add a button widget to a window.
The top-level GUI context that holds all windows.
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
int ht_put_ptr(HASH_TABLE *table, const char *key, void *ptr, void(*destructor)(void *ptr), void *(*duplicator)(void *ptr))
put an arbitrary pointer value with given key in the targeted hash table
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
structure of a hash table
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, safe for node removal during iteration.
int list_destroy(LIST **list)
Empty and Free a list container.
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
#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
PCRE2_UCHAR8 ** match_list
populated match list (NULL-terminated) after npcre_match_capture() Allocated by PCRE2 via pcre2_subst...
N_PCRE * npcre_new(char *str, int flags)
From pcre doc, the flag bits are: PCRE_ANCHORED Force pattern anchoring PCRE_AUTO_CALLOUT Compile aut...
int npcre_match_capture(char *str, N_PCRE *pcre)
Return TRUE if str matches regexp, and make captures.
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.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Hash functions and table.
List structures and definitions.
PCRE helpers for regex matching.
N_STR and string function declaration.