Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui.c
1
7#define WIDTH 640
8#define HEIGHT 480
9
10#define ALLEGRO_UNSTABLE 1
11
12#include "nilorea/n_gui.h"
13
14ALLEGRO_DISPLAY* display = NULL;
15
16int DONE = 0, /* Flag to check if we are always running */
17 getoptret = 0, /* launch parameter check */
18 log_level = LOG_ERR; /* default LOG_LEVEL */
19
20ALLEGRO_BITMAP* scr_buf = NULL;
21
22ALLEGRO_TIMER* fps_timer = NULL;
23ALLEGRO_TIMER* logic_timer = NULL;
24
25// RESOURCES *resources = NULL ;
26
27int main(int argc, char* argv[]) {
29
30 N_STR* log_file = NULL;
31 nstrprintf(log_file, "%s.log", argv[0]);
32 /*set_log_file( _nstr( log_file ) );*/
34
35 n_log(LOG_NOTICE, "%s is starting ...", argv[0]);
36
37 /* allegro 5 + addons loading */
38 if (!al_init()) {
39 n_abort("Could not init Allegro.\n");
40 }
41 if (!al_install_audio()) {
42 n_log(LOG_ERR, "Unable to initialize audio addon, audio will be disabled or crashing !");
43 }
44 if (!al_init_acodec_addon()) {
45 n_log(LOG_ERR, "Unable to initialize audio codec addon, audio will be disabled or crashing !");
46 }
47 if (!al_init_image_addon()) {
48 n_abort("Unable to initialize image addon\n");
49 }
50 if (!al_init_primitives_addon()) {
51 n_abort("Unable to initialize primitives addon\n");
52 }
53 if (!al_init_font_addon()) {
54 n_abort("Unable to initialize font addon\n");
55 }
56 if (!al_init_ttf_addon()) {
57 n_abort("Unable to initialize ttf_font addon\n");
58 }
59 if (!al_install_keyboard()) {
60 n_abort("Unable to initialize keyboard handler\n");
61 }
62 if (!al_install_mouse()) {
63 n_abort("Unable to initialize mouse handler\n");
64 }
65 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
66
67 event_queue = al_create_event_queue();
68 if (!event_queue) {
69 fprintf(stderr, "failed to create event_queue!\n");
70 al_destroy_display(display);
71 return -1;
72 }
73
74 char ver_str[128] = "";
75 while ((getoptret = getopt(argc, argv, "hvV:L:")) != EOF) {
76 switch (getoptret) {
77 case 'h':
78 n_log(LOG_NOTICE, "\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)\n", argv[0]);
79 exit(TRUE);
80 case 'v':
81 sprintf(ver_str, "%s %s", __DATE__, __TIME__);
82 exit(TRUE);
83 break;
84 case 'V':
85 if (!strncmp("NOTICE", optarg, 6)) {
86 log_level = LOG_INFO;
87 } else {
88 if (!strncmp("VERBOSE", optarg, 7)) {
89 log_level = LOG_NOTICE;
90 } else {
91 if (!strncmp("ERROR", optarg, 5)) {
92 log_level = LOG_ERR;
93 } else {
94 if (!strncmp("DEBUG", optarg, 5)) {
95 log_level = LOG_DEBUG;
96 } else {
97 n_log(LOG_ERR, "%s is not a valid log level\n", optarg);
98 exit(FALSE);
99 }
100 }
101 }
102 }
103 n_log(LOG_NOTICE, "LOG LEVEL UP TO: %d\n", log_level);
104 set_log_level(log_level);
105 break;
106 case 'L':
107 n_log(LOG_NOTICE, "LOG FILE: %s\n", optarg);
108 set_log_file(optarg);
109 break;
110 case '?': {
111 switch (optopt) {
112 case 'V':
113 n_log(LOG_ERR, "\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n");
114 break;
115 case 'L':
116 n_log(LOG_ERR, "\nPlease specify a log file after -L\n");
117 default:
118 break;
119 }
120 }
121 __attribute__((fallthrough));
122 default:
123 n_log(LOG_ERR, "\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[0]);
124 exit(FALSE);
125 }
126 }
127
128 fps_timer = al_create_timer(1.0 / 30.0);
129 logic_timer = al_create_timer(1.0 / 50.0);
130
131 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
132 display = al_create_display(WIDTH, HEIGHT);
133 if (!display) {
134 n_abort("Unable to create display\n");
135 }
136 al_set_window_title(display, argv[0]);
137
138 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
139
140 DONE = 0;
141
142 enum APP_KEYS {
143 KEY_UP,
144 KEY_DOWN,
145 KEY_LEFT,
146 KEY_RIGHT,
147 KEY_ESC,
148 KEY_SPACE,
149 KEY_CTRL
150 };
151 int key[7] = {false, false, false, false, false, false, false};
152
153 al_register_event_source(event_queue, al_get_display_event_source(display));
154
155 al_start_timer(fps_timer);
156 al_start_timer(logic_timer);
157 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer));
158 al_register_event_source(event_queue, al_get_timer_event_source(logic_timer));
159
160 al_register_event_source(event_queue, al_get_keyboard_event_source());
161 al_register_event_source(event_queue, al_get_mouse_event_source());
162
163 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(WIDTH, HEIGHT);
164
165 al_hide_mouse_cursor(display);
166
167 /* const char *html = "<div class=\"example\">Hello, <span>world!</span>__ this is\n"
168 "free<br>\n"
169 "text</br>\n"
170 "zone</div>";
171
172 N_GUI_DIALOG *dialog = NULL ;
173 dialog = n_gui_load_dialog( "index.html" , "styles.css" ); */
174
175 int mx = 0, my = 0, mouse_b1 = 0, mouse_b2 = 0;
176 int do_draw = 0, do_logic = 0;
177
178 al_clear_keyboard_state(NULL);
179 al_flush_event_queue(event_queue);
180 do {
181 do {
182 ALLEGRO_EVENT ev;
183
184 al_wait_for_event(event_queue, &ev);
185
186 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
187 switch (ev.keyboard.keycode) {
188 case ALLEGRO_KEY_UP:
189 key[KEY_UP] = 1;
190 break;
191 case ALLEGRO_KEY_DOWN:
192 key[KEY_DOWN] = 1;
193 break;
194 case ALLEGRO_KEY_LEFT:
195 key[KEY_LEFT] = 1;
196 break;
197 case ALLEGRO_KEY_RIGHT:
198 key[KEY_RIGHT] = 1;
199 break;
200 case ALLEGRO_KEY_ESCAPE:
201 key[KEY_ESC] = 1;
202 break;
203 case ALLEGRO_KEY_SPACE:
204 key[KEY_SPACE] = 1;
205 break;
206 case ALLEGRO_KEY_LCTRL:
207 case ALLEGRO_KEY_RCTRL:
208 key[KEY_CTRL] = 1;
209 default:
210 break;
211 }
212 } else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
213 switch (ev.keyboard.keycode) {
214 case ALLEGRO_KEY_UP:
215 key[KEY_UP] = 0;
216 break;
217 case ALLEGRO_KEY_DOWN:
218 key[KEY_DOWN] = 0;
219 break;
220 case ALLEGRO_KEY_LEFT:
221 key[KEY_LEFT] = 0;
222 break;
223 case ALLEGRO_KEY_RIGHT:
224 key[KEY_RIGHT] = 0;
225 break;
226 case ALLEGRO_KEY_ESCAPE:
227 key[KEY_ESC] = 0;
228 break;
229 case ALLEGRO_KEY_SPACE:
230 key[KEY_SPACE] = 0;
231 break;
232 case ALLEGRO_KEY_LCTRL:
233 case ALLEGRO_KEY_RCTRL:
234 key[KEY_CTRL] = 0;
235 default:
236 break;
237 }
238 } else if (ev.type == ALLEGRO_EVENT_TIMER) {
239 if (al_get_timer_event_source(fps_timer) == ev.any.source) {
240 do_draw = 1;
241 } else if (al_get_timer_event_source(logic_timer) == ev.any.source) {
242 do_logic = 1;
243 }
244 } else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
245 mx = ev.mouse.x;
246 my = ev.mouse.y;
247 } else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
248 if (ev.mouse.button == 1)
249 mouse_b1 = 1;
250 if (ev.mouse.button == 2)
251 mouse_b2 = 1;
252 } else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
253 if (ev.mouse.button == 1)
254 mouse_b1 = 0;
255 if (ev.mouse.button == 2)
256 mouse_b2 = 0;
257 } else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
258 al_clear_keyboard_state(display);
259 al_flush_event_queue(event_queue);
260 } else {
261 /* Processing inputs */
262 // get_keyboard( chat_line , ev );
263 }
264 } while (!al_is_event_queue_empty(event_queue));
265
266 /* dev mod: right click to temporary delete a block
267 left click to temporary add a block */
268 int mouse_button = -1;
269 if (mouse_b1 == 1)
270 mouse_button = 1;
271 if (mouse_b2 == 1)
272 mouse_button = 2;
273
274 if (do_logic == 1) {
275 if (mouse_button == 1) {
276 }
277 do_logic = 0;
278 }
279
280 if (do_draw == 1) {
281 al_acknowledge_resize(display);
282 /* int w = al_get_display_width( display );
283int h = al_get_display_height( display ); */
284
285 // clear buffer
286 al_set_target_bitmap(scrbuf);
287 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
288
289 // draw game
290
291 // draw gui
292
293 // draw buffer to screen
294 al_set_target_bitmap(al_get_backbuffer(display));
295 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
296 al_draw_bitmap(scrbuf, 0, 0, 0);
297
298 /* mouse pointer */
299 al_draw_line(mx - 5, my, mx + 5, my, al_map_rgb(255, 0, 0), 1);
300 al_draw_line(mx, my + 5, mx, my - 5, al_map_rgb(255, 0, 0), 1);
301
302 al_flip_display();
303 do_draw = 0;
304 }
305
306 } while (!key[KEY_ESC] && !DONE);
307
308 al_destroy_bitmap(scr_buf);
309
310 al_uninstall_system();
311
312 return 0;
313}
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:38
#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
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:94
A box including a string and his lenght.
Definition n_str.h:39
GUI headers.
static FILE * log_file
static FILE handling if logging to file is enabled
Definition n_log.c:54