78int main(
int argc,
char* argv[]) {
80 setlocale(LC_ALL,
"POSIX");
89 char ver_str[128] =
"";
91 while ((
getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
94 n_log(
LOG_NOTICE,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)\n", argv[0]);
97 sprintf(ver_str,
"%s %s", __DATE__, __TIME__);
101 if (!strncmp(
"INFO", optarg, 6)) {
104 if (!strncmp(
"NOTICE", optarg, 6)) {
107 if (!strncmp(
"VERBOSE", optarg, 7)) {
110 if (!strncmp(
"ERROR", optarg, 5)) {
113 if (!strncmp(
"DEBUG", optarg, 5)) {
133 n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG");
143 n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile", argv[0]);
152 n_abort(
"Could not init Allegro.\n");
158 ALLEGRO_PATH* here = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
161 al_change_directory(al_path_cstr(here, ALLEGRO_NATIVE_PATH_SEP));
162 al_destroy_path(here);
167 int threadedProcessing = 0;
174 double fluid_factor = fluid_data->
cScale;
177 if (!al_init_acodec_addon()) {
178 n_abort(
"Could not register addons.\n");
180 if (!al_install_audio()) {
181 n_log(
LOG_ERR,
"Unable to initialize audio addon, disabling bgmusic\n");
184 if (!al_init_acodec_addon()) {
185 n_abort(
"Unable to initialize acoded addon\n");
187 if (!al_init_image_addon()) {
188 n_abort(
"Unable to initialize image addon\n");
190 if (!al_init_primitives_addon()) {
191 n_abort(
"Unable to initialize primitives addon\n");
193 if (!al_init_font_addon()) {
194 n_abort(
"Unable to initialize font addon\n");
196 if (!al_init_ttf_addon()) {
197 n_abort(
"Unable to initialize ttf_font addon\n");
199 if (!al_install_keyboard()) {
200 n_abort(
"Unable to initialize keyboard handler\n");
202 if (!al_install_mouse()) {
203 n_abort(
"Unable to initialize mouse handler\n");
207 n_abort(
"Could not set up voice and mixer.\n");
211 memset(sample_data, 0,
sizeof(sample_data));
213 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
215 event_queue = al_create_event_queue();
217 fprintf(stderr,
"failed to create event_queue!\n");
222 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_FULLSCREEN_WINDOW);
224 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
228 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP | ALLEGRO_NO_PRESERVE_TEXTURE);
233 if (!al_filename_exists(
"DATAS/2Dumb.ttf")) {
234 n_log(
LOG_ERR,
"Missing DATAS/2Dumb.ttf beside the executable");
235 al_destroy_event_queue(event_queue);
241 n_abort(
"Unable to create display\n");
244 al_set_window_title(
display, argv[0]);
246 ALLEGRO_FONT*
font = al_load_font(
"DATAS/2Dumb.ttf", 18, 0);
248 n_abort(
"Unable to load font DATAS/2Dumb.ttf\n");
255 al_register_event_source(event_queue, al_get_display_event_source(
display));
258 al_register_event_source(event_queue, al_get_timer_event_source(
fps_timer));
259 al_register_event_source(event_queue, al_get_timer_event_source(
logic_timer));
261 al_register_event_source(event_queue, al_get_keyboard_event_source());
262 al_register_event_source(event_queue, al_get_mouse_event_source());
265 ALLEGRO_BITMAP* scrbuf = NULL;
266 ALLEGRO_BITMAP* bitmap = NULL;
291 int key[19] = {
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false};
294 if (!(sample_data[0] = al_load_sample(
bgmusic))) {
298 al_play_sample(sample_data[0], 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);
322 for (
size_t i = 0; i <
fluid_sim->numX; i++) {
323 for (
size_t j = 0; j <
fluid_sim->numY; j++) {
325 if (i == 0 || j == 0 || j ==
fluid_sim->numY - 1)
336 size_t minJ = (size_t)floor(0.5 * (
double)
fluid_sim->numY - 0.5 * pipeH);
337 size_t maxJ = (size_t)floor(0.5 * (
double)
fluid_sim->numY + 0.5 * pipeH);
338 for (
size_t j = 0; j < minJ; j++)
340 for (
size_t j = minJ; j < maxJ; j++)
342 for (
size_t j = maxJ; j <
fluid_sim->numY; j++)
345 bool do_draw = 1, do_logic = 1;
350 al_flush_event_queue(event_queue);
353 int w = al_get_display_width(
display);
354 int h = al_get_display_height(
display);
358 size_t logic_duration = 0;
359 size_t drawing_duration = 0;
364 al_wait_for_event(event_queue, &ev);
366 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
367 switch (ev.keyboard.keycode) {
371 case ALLEGRO_KEY_DOWN:
374 case ALLEGRO_KEY_LEFT:
377 case ALLEGRO_KEY_RIGHT:
380 case ALLEGRO_KEY_ESCAPE:
383 case ALLEGRO_KEY_SPACE:
386 case ALLEGRO_KEY_LSHIFT:
387 case ALLEGRO_KEY_RSHIFT:
390 case ALLEGRO_KEY_PAD_MINUS:
391 key[KEY_PAD_MINUS] = 1;
393 case ALLEGRO_KEY_PAD_PLUS:
394 key[KEY_PAD_PLUS] = 1;
396 case ALLEGRO_KEY_PAD_ENTER:
397 key[KEY_PAD_ENTER] = 1;
405 case ALLEGRO_KEY_LCTRL:
406 case ALLEGRO_KEY_RCTRL:
430 }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
431 switch (ev.keyboard.keycode) {
435 case ALLEGRO_KEY_DOWN:
438 case ALLEGRO_KEY_LEFT:
441 case ALLEGRO_KEY_RIGHT:
444 case ALLEGRO_KEY_ESCAPE:
447 case ALLEGRO_KEY_SPACE:
450 case ALLEGRO_KEY_LSHIFT:
451 case ALLEGRO_KEY_RSHIFT:
454 case ALLEGRO_KEY_PAD_MINUS:
455 key[KEY_PAD_MINUS] = 0;
457 case ALLEGRO_KEY_PAD_PLUS:
458 key[KEY_PAD_PLUS] = 0;
460 case ALLEGRO_KEY_PAD_ENTER:
461 key[KEY_PAD_ENTER] = 0;
469 case ALLEGRO_KEY_LCTRL:
470 case ALLEGRO_KEY_RCTRL:
495 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
496 if (al_get_timer_event_source(
fps_timer) == ev.any.source) {
498 }
else if (al_get_timer_event_source(
logic_timer) == ev.any.source) {
501 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
504 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
505 if (ev.mouse.button == 1)
507 if (ev.mouse.button == 2)
509 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
510 if (ev.mouse.button == 1)
512 if (ev.mouse.button == 2)
522 else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
523 al_clear_keyboard_state(
display);
524 al_flush_event_queue(event_queue);
560 if (
key[KEY_PAD_PLUS]) {
561 fluid_sim->fluid_production_percentage += 0.01;
562 if (
fluid_sim->fluid_production_percentage > 1.0)
fluid_sim->fluid_production_percentage = 1.0;
564 if (
key[KEY_PAD_MINUS]) {
565 fluid_sim->fluid_production_percentage -= 0.01;
566 if (
fluid_sim->fluid_production_percentage < 0.1)
fluid_sim->fluid_production_percentage = 0.1;
572 }
while (!al_is_event_queue_empty(event_queue));
576 static int old_mx = -1, old_my = -1;
577 double vx = 0.0, vy = 0.0;
578 if (old_mx != mx || old_my != my) {
579 if (old_mx != -1 && old_my != -1) {
594 minJ = (size_t)floor(0.5 * (
double)
fluid_sim->numY - 0.5 * pipeH);
595 maxJ = (size_t)floor(0.5 * (
double)
fluid_sim->numY + 0.5 * pipeH);
596 for (
size_t j = 0; j < minJ; j++)
598 for (
size_t j = minJ; j < maxJ; j++)
600 for (
size_t j = maxJ; j <
fluid_sim->numY; j++)
602 for (
size_t j = minJ; j < maxJ; j++)
605 if (threadedProcessing == 1)
617 scrbuf = al_get_backbuffer(
display);
621 al_set_target_bitmap(scrbuf);
624 al_lock_bitmap(scrbuf, al_get_bitmap_format(scrbuf), ALLEGRO_LOCK_READWRITE);
628 al_draw_circle((
float)mx, (
float)(my - 20 * fluid_factor), (
float)(fluid_factor * fluid_factor / 2), al_map_rgb(255, 0, 0), 2.0);
629 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);
630 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);
631 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);
632 al_draw_circle((
float)mx, (
float)(my + 20 * fluid_factor), (
float)(fluid_factor * fluid_factor / 2), al_map_rgb(255, 0, 0), 2.0);
634 static N_STR* textout = NULL;
636 al_draw_text(
font, al_map_rgb(0, 0, 255), (
float)
WIDTH, 10, ALLEGRO_ALIGN_RIGHT,
_nstr(textout));
639 al_draw_text(
font, al_map_rgb(0, 0, 255), (
float)
WIDTH, 25, ALLEGRO_ALIGN_RIGHT,
_nstr(textout));
641 nstrprintf(textout,
"logic(max %zd): %zd usecs", (
size_t)(1000000.0 /
logicFPS), logic_duration);
642 al_draw_text(
font, al_map_rgb(0, 0, 255), 5, 10, ALLEGRO_ALIGN_LEFT,
_nstr(textout));
644 nstrprintf(textout,
"drawing(max %zd): %zd usecs", (
size_t)(1000000.0 /
drawFPS), drawing_duration);
645 al_draw_text(
font, al_map_rgb(0, 0, 255), 5, 30, ALLEGRO_ALIGN_LEFT,
_nstr(textout));
648 al_unlock_bitmap(scrbuf);
649 al_set_target_bitmap(al_get_backbuffer(
display));
650 al_draw_bitmap(scrbuf, (
float)(w / 2 - al_get_bitmap_width(scrbuf) / 2), (
float)(
h / 2 - al_get_bitmap_height(scrbuf) / 2), 0);
662 al_uninstall_system();