30#define ALLEGRO_UNSTABLE 1
36#include <allegro5/allegro_ttf.h>
64int main(
int argc,
char* argv[]) {
76 n_abort(
"Could not init Allegro.\n");
78 if (!al_install_audio()) {
79 n_abort(
"Unable to initialize audio addon\n");
81 if (!al_init_acodec_addon()) {
82 n_abort(
"Unable to initialize acoded addon\n");
84 if (!al_init_image_addon()) {
85 n_abort(
"Unable to initialize image addon\n");
87 if (!al_init_primitives_addon()) {
88 n_abort(
"Unable to initialize primitives addon\n");
90 if (!al_init_font_addon()) {
91 n_abort(
"Unable to initialize font addon\n");
93 if (!al_init_ttf_addon()) {
94 n_abort(
"Unable to initialize ttf_font addon\n");
96 if (!al_install_keyboard()) {
97 n_abort(
"Unable to initialize keyboard handler\n");
99 if (!al_install_mouse()) {
100 n_abort(
"Unable to initialize mouse handler\n");
102 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
104 event_queue = al_create_event_queue();
106 fprintf(stderr,
"failed to create event_queue!\n");
111 char ver_str[128] =
"";
112 while ((
getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
115 n_log(
LOG_NOTICE,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)", argv[0]);
118 sprintf(ver_str,
"%s %s", __DATE__, __TIME__);
122 if (!strncmp(
"NOTICE", optarg, 6)) {
125 if (!strncmp(
"VERBOSE", optarg, 7)) {
128 if (!strncmp(
"ERROR", optarg, 5)) {
131 if (!strncmp(
"DEBUG", optarg, 5)) {
150 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG");
160 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile", argv[0]);
168 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
171 n_abort(
"Unable to create display\n");
173 al_set_window_title(
display, argv[0]);
175 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
190 int key[7] = {
false,
false,
false,
false,
false,
false,
false};
192 al_register_event_source(event_queue, al_get_display_event_source(
display));
196 al_register_event_source(event_queue, al_get_timer_event_source(
fps_timer));
197 al_register_event_source(event_queue, al_get_timer_event_source(
logic_timer));
199 al_register_event_source(event_queue, al_get_keyboard_event_source());
200 al_register_event_source(event_queue, al_get_mouse_event_source());
202 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(
WIDTH,
HEIGHT);
208 int mx = 0, my = 0, mouse_b1 = 0, mouse_b2 = 0;
209 int do_draw = 0, do_logic = 0;
212 ALLEGRO_FONT* gui_font = al_create_builtin_font();
225 al_clear_keyboard_state(NULL);
226 al_flush_event_queue(event_queue);
228 double last_fps_time = al_get_time();
230 int w = al_get_display_width(
display);
231 int h = al_get_display_height(
display);
237 al_wait_for_event(event_queue, &ev);
239 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
240 switch (ev.keyboard.keycode) {
244 case ALLEGRO_KEY_DOWN:
247 case ALLEGRO_KEY_LEFT:
250 case ALLEGRO_KEY_RIGHT:
253 case ALLEGRO_KEY_ESCAPE:
256 case ALLEGRO_KEY_SPACE:
259 case ALLEGRO_KEY_LCTRL:
260 case ALLEGRO_KEY_RCTRL:
265 }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
266 switch (ev.keyboard.keycode) {
270 case ALLEGRO_KEY_DOWN:
273 case ALLEGRO_KEY_LEFT:
276 case ALLEGRO_KEY_RIGHT:
279 case ALLEGRO_KEY_ESCAPE:
282 case ALLEGRO_KEY_SPACE:
285 case ALLEGRO_KEY_LCTRL:
286 case ALLEGRO_KEY_RCTRL:
291 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
292 if (al_get_timer_event_source(
fps_timer) == ev.any.source) {
294 }
else if (al_get_timer_event_source(
logic_timer) == ev.any.source) {
297 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
300 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
301 if (ev.mouse.button == 1)
303 if (ev.mouse.button == 2)
305 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
306 if (ev.mouse.button == 1)
308 if (ev.mouse.button == 2)
310 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
311 al_acknowledge_resize(
display);
312 w = al_get_display_width(
display);
313 h = al_get_display_height(
display);
315 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
316 al_clear_keyboard_state(
display);
317 al_flush_event_queue(event_queue);
324 }
while (!al_is_event_queue_empty(event_queue));
328 int mouse_button = -1;
339 for (
int it = 0; it < 100; it++) {
348 al_map_rgba((
unsigned char)(55 + rand() % 200), (
unsigned char)(55 + rand() % 200), (
unsigned char)(55 + rand() % 200), (
unsigned char)(10 + rand() % 245)), tmp_part) == TRUE) {
357 al_set_target_bitmap(scrbuf);
358 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
361 al_set_target_bitmap(al_get_backbuffer(
display));
363 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
364 al_draw_bitmap(scrbuf, 0, 0, 0);
368 double now = al_get_time();
378 size_t active_count = 0;
379 size_t max_count = 0;
385 snprintf(buf,
sizeof(buf),
"Active: %zu", active_count);
388 snprintf(buf,
sizeof(buf),
"Max: %zu", max_count);
394 snprintf(buf,
sizeof(buf),
"Mouse: %d, %d", mx, my);
397 snprintf(buf,
sizeof(buf),
"FPS: %.1f",
fps_display);
404 al_draw_line((
float)(mx - 5), (
float)my, (
float)(mx + 5), (
float)my, al_map_rgb(255, 0, 0), 1);
405 al_draw_line((
float)mx, (
float)(my + 5), (
float)mx, (
float)(my - 5), al_map_rgb(255, 0, 0), 1);
411 }
while (!
key[KEY_ESC] && !
DONE);
415 al_destroy_font(gui_font);
416 al_destroy_bitmap(scrbuf);
419 al_uninstall_system();
ALLEGRO_TIMER * fps_timer
ALLEGRO_TIMER * logic_timer
ALLEGRO_DISPLAY * display
static int lbl_particle_count
static double fps_display
static int lbl_spawn_info
static int lbl_particle_max
PARTICLE_SYSTEM * particle_system_effects
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
int n_gui_wants_mouse(N_GUI_CTX *ctx)
Check if the mouse is currently over any open GUI window.
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_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.
void n_gui_set_virtual_size(N_GUI_CTX *ctx, float w, float h)
Set the virtual canvas size for resolution-independent scaling.
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.
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
The top-level GUI context that holds all windows.
size_t nb_max_items
Maximum number of items in the list.
size_t nb_items
number of item currently in the list
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
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
A box including a string and his lenght.
LIST * list
list of PARTICLE pointers
int draw_particle(PARTICLE_SYSTEM *psys, double xpos, double ypos, int w, int h, double range)
draw particles of a particle system
#define PIXEL_PART
pixel particle
int manage_particle(PARTICLE_SYSTEM *psys)
update particles positions using particle system internal timer
int add_particle(PARTICLE_SYSTEM *psys, int spr, int mode, int lifetime, int size, ALLEGRO_COLOR color, PHYSICS object)
add a particle to a particle system
int init_particle_system(PARTICLE_SYSTEM **psys, int max, double x, double y, double z, int max_sprites)
initialize a particle system
Structure of a particle system.
VECTOR3D speed
vx,vy,vz actual speed
VECTOR3D orientation
ax,ay,az actual rotation position
VECTOR3D position
x,y,z actual position
VECTOR3D acceleration
ax,ay,az actual acceleration
#define VECTOR3D_SET(VECTOR, X, Y, Z)
helper to set a VECTOR3D position
structure of the physics of an object
Animations graphics and animations parameters.
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
static FILE * log_file
static FILE handling if logging to file is enabled