10#define ALLEGRO_UNSTABLE 1
17#include <allegro5/allegro_ttf.h>
19ALLEGRO_DISPLAY* display = NULL;
25ALLEGRO_BITMAP* scr_buf = NULL;
27ALLEGRO_TIMER* fps_timer = NULL;
28ALLEGRO_TIMER* logic_timer = NULL;
29LIST* active_object = NULL;
34int main(
int argc,
char* argv[]) {
46 n_abort(
"Could not init Allegro.\n");
48 if (!al_install_audio()) {
49 n_abort(
"Unable to initialize audio addon\n");
51 if (!al_init_acodec_addon()) {
52 n_abort(
"Unable to initialize acoded addon\n");
54 if (!al_init_image_addon()) {
55 n_abort(
"Unable to initialize image addon\n");
57 if (!al_init_primitives_addon()) {
58 n_abort(
"Unable to initialize primitives addon\n");
60 if (!al_init_font_addon()) {
61 n_abort(
"Unable to initialize font addon\n");
63 if (!al_init_ttf_addon()) {
64 n_abort(
"Unable to initialize ttf_font addon\n");
66 if (!al_install_keyboard()) {
67 n_abort(
"Unable to initialize keyboard handler\n");
69 if (!al_install_mouse()) {
70 n_abort(
"Unable to initialize mouse handler\n");
72 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
74 event_queue = al_create_event_queue();
76 fprintf(stderr,
"failed to create event_queue!\n");
77 al_destroy_display(display);
81 char ver_str[128] =
"";
82 while ((getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
85 n_log(
LOG_NOTICE,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)\n", argv[0]);
88 sprintf(ver_str,
"%s %s", __DATE__, __TIME__);
92 if (!strncmp(
"NOTICE", optarg, 6)) {
95 if (!strncmp(
"VERBOSE", optarg, 7)) {
98 if (!strncmp(
"ERROR", optarg, 5)) {
101 if (!strncmp(
"DEBUG", optarg, 5)) {
104 n_log(
LOG_ERR,
"%s is not a valid log level\n", optarg);
120 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n");
123 n_log(
LOG_ERR,
"\nPlease specify a log file after -L\n");
128 __attribute__((fallthrough));
130 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[0]);
135 fps_timer = al_create_timer(1.0 / 30.0);
136 logic_timer = al_create_timer(1.0 / 50.0);
138 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
139 display = al_create_display(WIDTH, HEIGHT);
141 n_abort(
"Unable to create display\n");
143 al_set_window_title(display, argv[0]);
145 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
160 int key[7] = {
false,
false,
false,
false,
false,
false,
false};
162 al_register_event_source(event_queue, al_get_display_event_source(display));
164 al_start_timer(fps_timer);
165 al_start_timer(logic_timer);
166 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer));
167 al_register_event_source(event_queue, al_get_timer_event_source(logic_timer));
169 al_register_event_source(event_queue, al_get_keyboard_event_source());
170 al_register_event_source(event_queue, al_get_mouse_event_source());
172 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(WIDTH, HEIGHT);
174 al_hide_mouse_cursor(display);
178 int mx = 0, my = 0, mouse_b1 = 0, mouse_b2 = 0;
179 int do_draw = 0, do_logic = 0;
181 al_clear_keyboard_state(NULL);
182 al_flush_event_queue(event_queue);
187 al_wait_for_event(event_queue, &ev);
189 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
190 switch (ev.keyboard.keycode) {
194 case ALLEGRO_KEY_DOWN:
197 case ALLEGRO_KEY_LEFT:
200 case ALLEGRO_KEY_RIGHT:
203 case ALLEGRO_KEY_ESCAPE:
206 case ALLEGRO_KEY_SPACE:
209 case ALLEGRO_KEY_LCTRL:
210 case ALLEGRO_KEY_RCTRL:
215 }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
216 switch (ev.keyboard.keycode) {
220 case ALLEGRO_KEY_DOWN:
223 case ALLEGRO_KEY_LEFT:
226 case ALLEGRO_KEY_RIGHT:
229 case ALLEGRO_KEY_ESCAPE:
232 case ALLEGRO_KEY_SPACE:
235 case ALLEGRO_KEY_LCTRL:
236 case ALLEGRO_KEY_RCTRL:
241 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
242 if (al_get_timer_event_source(fps_timer) == ev.any.source) {
244 }
else if (al_get_timer_event_source(logic_timer) == ev.any.source) {
247 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
250 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
251 if (ev.mouse.button == 1)
253 if (ev.mouse.button == 2)
255 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
256 if (ev.mouse.button == 1)
258 if (ev.mouse.button == 2)
260 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
261 al_clear_keyboard_state(display);
262 al_flush_event_queue(event_queue);
267 }
while (!al_is_event_queue_empty(event_queue));
271 int mouse_button = -1;
279 if (mouse_button == 1) {
282 for (
int it = 0; it < 100; it++) {
291 al_map_rgba(55 + rand() % 200, 55 + rand() % 200, 55 + rand() % 200, 10 + rand() % 245), tmp_part);
298 al_acknowledge_resize(display);
299 int w = al_get_display_width(display);
300 int h = al_get_display_height(display);
302 al_set_target_bitmap(scrbuf);
303 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
306 al_set_target_bitmap(al_get_backbuffer(display));
308 al_clear_to_color(al_map_rgba(0, 0, 0, 255));
309 al_draw_bitmap(scrbuf, 0, 0, 0);
312 al_draw_line(mx - 5, my, mx + 5, my, al_map_rgb(255, 0, 0), 1);
313 al_draw_line(mx, my + 5, mx, my - 5, al_map_rgb(255, 0, 0), 1);
319 }
while (!key[KEY_ESC] && !DONE);
323 al_destroy_bitmap(scr_buf);
325 al_uninstall_system();
void n_abort(char const *format,...)
abort program with a text
int list_destroy(LIST **list)
Empty and Free a list container.
#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.
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 usting 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.
static FILE * log_file
static FILE handling if logging to file is enabled