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