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