79int main(
int argc,
char* argv[]) {
81 setlocale(LC_ALL,
"POSIX");
90 char ver_str[128] =
"";
92 while ((
getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
95 n_log(
LOG_NOTICE,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)\n", argv[0]);
98 sprintf(ver_str,
"%s %s", __DATE__, __TIME__);
102 if (!strncmp(
"INFO", optarg, 6)) {
105 if (!strncmp(
"NOTICE", optarg, 6)) {
108 if (!strncmp(
"VERBOSE", optarg, 7)) {
111 if (!strncmp(
"ERROR", optarg, 5)) {
114 if (!strncmp(
"DEBUG", optarg, 5)) {
134 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG");
144 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile", argv[0]);
151 int threadedProcessing = 0;
158 double fluid_factor = fluid_data->
cScale;
163 n_abort(
"Could not init Allegro.\n");
165 if (!al_init_acodec_addon()) {
166 n_abort(
"Could not register addons.\n");
168 if (!al_install_audio()) {
169 n_log(
LOG_ERR,
"Unable to initialize audio addon, disabling bgmusic\n");
172 if (!al_init_acodec_addon()) {
173 n_abort(
"Unable to initialize acoded addon\n");
175 if (!al_init_image_addon()) {
176 n_abort(
"Unable to initialize image addon\n");
178 if (!al_init_primitives_addon()) {
179 n_abort(
"Unable to initialize primitives addon\n");
181 if (!al_init_font_addon()) {
182 n_abort(
"Unable to initialize font addon\n");
184 if (!al_init_ttf_addon()) {
185 n_abort(
"Unable to initialize ttf_font addon\n");
187 if (!al_install_keyboard()) {
188 n_abort(
"Unable to initialize keyboard handler\n");
190 if (!al_install_mouse()) {
191 n_abort(
"Unable to initialize mouse handler\n");
195 n_abort(
"Could not set up voice and mixer.\n");
199 memset(sample_data, 0,
sizeof(sample_data));
201 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
203 event_queue = al_create_event_queue();
205 fprintf(stderr,
"failed to create event_queue!\n");
210 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_FULLSCREEN_WINDOW);
212 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
216 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP | ALLEGRO_NO_PRESERVE_TEXTURE);
221 n_abort(
"Unable to create display\n");
224 al_set_window_title(
display, argv[0]);
226 ALLEGRO_FONT* font = al_load_font(
"DATAS/2Dumb.ttf", 18, 0);
228 n_abort(
"Unable to load font DATAS/2Dumb.ttf\n");
235 al_register_event_source(event_queue, al_get_display_event_source(
display));
238 al_register_event_source(event_queue, al_get_timer_event_source(
fps_timer));
239 al_register_event_source(event_queue, al_get_timer_event_source(
logic_timer));
241 al_register_event_source(event_queue, al_get_keyboard_event_source());
242 al_register_event_source(event_queue, al_get_mouse_event_source());
245 ALLEGRO_BITMAP* scrbuf = NULL;
246 ALLEGRO_BITMAP* bitmap = NULL;
271 int key[19] = {
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false};
274 if (!(sample_data[0] = al_load_sample(
bgmusic))) {
278 al_play_sample(sample_data[0], 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);
316 size_t minJ = (size_t)floor(0.5 * (
double)
fluid_sim->
numY - 0.5 * pipeH);
317 size_t maxJ = (size_t)floor(0.5 * (
double)
fluid_sim->
numY + 0.5 * pipeH);
318 for (
size_t j = 0; j < minJ; j++)
320 for (
size_t j = minJ; j < maxJ; j++)
325 bool do_draw = 1, do_logic = 1;
326 int mx =
WIDTH / 3, my =
HEIGHT / 2, mouse_button = 0, mouse_b1 = 0, mouse_b2 = 0;
330 al_flush_event_queue(event_queue);
333 int w = al_get_display_width(
display);
334 int h = al_get_display_height(
display);
338 size_t logic_duration = 0;
339 size_t drawing_duration = 0;
344 al_wait_for_event(event_queue, &ev);
346 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
347 switch (ev.keyboard.keycode) {
351 case ALLEGRO_KEY_DOWN:
354 case ALLEGRO_KEY_LEFT:
357 case ALLEGRO_KEY_RIGHT:
360 case ALLEGRO_KEY_ESCAPE:
363 case ALLEGRO_KEY_SPACE:
366 case ALLEGRO_KEY_LSHIFT:
367 case ALLEGRO_KEY_RSHIFT:
370 case ALLEGRO_KEY_PAD_MINUS:
371 key[KEY_PAD_MINUS] = 1;
373 case ALLEGRO_KEY_PAD_PLUS:
374 key[KEY_PAD_PLUS] = 1;
376 case ALLEGRO_KEY_PAD_ENTER:
377 key[KEY_PAD_ENTER] = 1;
385 case ALLEGRO_KEY_LCTRL:
386 case ALLEGRO_KEY_RCTRL:
410 }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
411 switch (ev.keyboard.keycode) {
415 case ALLEGRO_KEY_DOWN:
418 case ALLEGRO_KEY_LEFT:
421 case ALLEGRO_KEY_RIGHT:
424 case ALLEGRO_KEY_ESCAPE:
427 case ALLEGRO_KEY_SPACE:
430 case ALLEGRO_KEY_LSHIFT:
431 case ALLEGRO_KEY_RSHIFT:
434 case ALLEGRO_KEY_PAD_MINUS:
435 key[KEY_PAD_MINUS] = 0;
437 case ALLEGRO_KEY_PAD_PLUS:
438 key[KEY_PAD_PLUS] = 0;
440 case ALLEGRO_KEY_PAD_ENTER:
441 key[KEY_PAD_ENTER] = 0;
449 case ALLEGRO_KEY_LCTRL:
450 case ALLEGRO_KEY_RCTRL:
475 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
476 if (al_get_timer_event_source(
fps_timer) == ev.any.source) {
478 }
else if (al_get_timer_event_source(
logic_timer) == ev.any.source) {
481 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
484 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
485 if (ev.mouse.button == 1)
487 if (ev.mouse.button == 2)
489 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
490 if (ev.mouse.button == 1)
492 if (ev.mouse.button == 2)
502 else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
503 al_clear_keyboard_state(
display);
504 al_flush_event_queue(event_queue);
536 if (
key[KEY_RIGHT]) {
540 if (
key[KEY_PAD_PLUS]) {
544 if (
key[KEY_PAD_MINUS]) {
548 if (mouse_button != -1) {
552 }
while (!al_is_event_queue_empty(event_queue));
556 static int old_mx = -1, old_my = -1;
557 double vx = 0.0, vy = 0.0;
558 if (old_mx != mx || old_my != my) {
559 if (old_mx != -1 && old_my != -1) {
574 minJ = (size_t)floor(0.5 * (
double)
fluid_sim->
numY - 0.5 * pipeH);
575 maxJ = (size_t)floor(0.5 * (
double)
fluid_sim->
numY + 0.5 * pipeH);
576 for (
size_t j = 0; j < minJ; j++)
578 for (
size_t j = minJ; j < maxJ; j++)
582 for (
size_t j = minJ; j < maxJ; j++)
585 if (threadedProcessing == 1)
597 scrbuf = al_get_backbuffer(
display);
601 al_set_target_bitmap(scrbuf);
604 al_lock_bitmap(scrbuf, al_get_bitmap_format(scrbuf), ALLEGRO_LOCK_READWRITE);
608 al_draw_circle((
float)mx, (
float)(my - 20 * fluid_factor), (
float)(fluid_factor * fluid_factor / 2), al_map_rgb(255, 0, 0), 2.0);
609 al_draw_circle((
float)(mx - 15 * fluid_factor), (
float)my, (
float)(fluid_factor * fluid_factor / 2 + (fluid_factor * fluid_factor) / 3), al_map_rgb(255, 0, 0), 2.0);
610 al_draw_circle((
float)(mx + 15 * fluid_factor), (
float)(my + 10.0 * fluid_factor), (
float)(fluid_factor * fluid_factor / 2 + (fluid_factor * fluid_factor) / 2), al_map_rgb(255, 0, 0), 2.0);
611 al_draw_circle((
float)(mx + 15 * fluid_factor), (
float)(my - 10.0 * fluid_factor), (
float)(fluid_factor * fluid_factor / 2 + (fluid_factor * fluid_factor) / 2), al_map_rgb(255, 0, 0), 2.0);
612 al_draw_circle((
float)mx, (
float)(my + 20 * fluid_factor), (
float)(fluid_factor * fluid_factor / 2), al_map_rgb(255, 0, 0), 2.0);
614 static N_STR* textout = NULL;
616 al_draw_text(font, al_map_rgb(0, 0, 255), (
float)
WIDTH, 10, ALLEGRO_ALIGN_RIGHT,
_nstr(textout));
619 al_draw_text(font, al_map_rgb(0, 0, 255), (
float)
WIDTH, 25, ALLEGRO_ALIGN_RIGHT,
_nstr(textout));
621 nstrprintf(textout,
"logic(max %zd): %zd usecs", (
size_t)(1000000.0 /
logicFPS), logic_duration);
622 al_draw_text(font, al_map_rgb(0, 0, 255), 5, 10, ALLEGRO_ALIGN_LEFT,
_nstr(textout));
624 nstrprintf(textout,
"drawing(max %zd): %zd usecs", (
size_t)(1000000.0 /
drawFPS), drawing_duration);
625 al_draw_text(font, al_map_rgb(0, 0, 255), 5, 30, ALLEGRO_ALIGN_LEFT,
_nstr(textout));
628 al_unlock_bitmap(scrbuf);
629 al_set_target_bitmap(al_get_backbuffer(
display));
630 al_draw_bitmap(scrbuf, (
float)(w / 2 - al_get_bitmap_width(scrbuf) / 2), (
float)(h / 2 - al_get_bitmap_height(scrbuf) / 2), 0);
640 }
while (!
key[KEY_ESC] && !
DONE);
642 al_uninstall_system();