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]);
150 int threadedProcessing = 0;
157 double fluid_factor = fluid_data->
cScale;
162 n_abort(
"Could not init Allegro.\n");
164 if (!al_init_acodec_addon()) {
165 n_abort(
"Could not register addons.\n");
167 if (!al_install_audio()) {
168 n_log(
LOG_ERR,
"Unable to initialize audio addon, disabling bgmusic\n");
171 if (!al_init_acodec_addon()) {
172 n_abort(
"Unable to initialize acoded addon\n");
174 if (!al_init_image_addon()) {
175 n_abort(
"Unable to initialize image addon\n");
177 if (!al_init_primitives_addon()) {
178 n_abort(
"Unable to initialize primitives addon\n");
180 if (!al_init_font_addon()) {
181 n_abort(
"Unable to initialize font addon\n");
183 if (!al_init_ttf_addon()) {
184 n_abort(
"Unable to initialize ttf_font addon\n");
186 if (!al_install_keyboard()) {
187 n_abort(
"Unable to initialize keyboard handler\n");
189 if (!al_install_mouse()) {
190 n_abort(
"Unable to initialize mouse handler\n");
194 n_abort(
"Could not set up voice and mixer.\n");
198 memset(sample_data, 0,
sizeof(sample_data));
200 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
202 event_queue = al_create_event_queue();
204 fprintf(stderr,
"failed to create event_queue!\n");
209 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_FULLSCREEN_WINDOW);
211 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
215 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP | ALLEGRO_NO_PRESERVE_TEXTURE);
220 n_abort(
"Unable to create display\n");
223 al_set_window_title(
display, argv[0]);
225 ALLEGRO_FONT* font = al_load_font(
"DATAS/2Dumb.ttf", 18, 0);
227 n_abort(
"Unable to load font DATAS/2Dumb.ttf\n");
234 al_register_event_source(event_queue, al_get_display_event_source(
display));
237 al_register_event_source(event_queue, al_get_timer_event_source(
fps_timer));
238 al_register_event_source(event_queue, al_get_timer_event_source(
logic_timer));
240 al_register_event_source(event_queue, al_get_keyboard_event_source());
241 al_register_event_source(event_queue, al_get_mouse_event_source());
244 ALLEGRO_BITMAP* scrbuf = NULL;
245 ALLEGRO_BITMAP* bitmap = NULL;
270 int key[19] = {
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false};
273 if (!(sample_data[0] = al_load_sample(
bgmusic))) {
277 al_play_sample(sample_data[0], 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);
315 size_t minJ = (size_t)floor(0.5 * (
double)
fluid_sim->
numY - 0.5 * pipeH);
316 size_t maxJ = (size_t)floor(0.5 * (
double)
fluid_sim->
numY + 0.5 * pipeH);
317 for (
size_t j = 0; j < minJ; j++)
319 for (
size_t j = minJ; j < maxJ; j++)
324 bool do_draw = 1, do_logic = 1;
325 int mx =
WIDTH / 3, my =
HEIGHT / 2, mouse_button = 0, mouse_b1 = 0, mouse_b2 = 0;
329 al_flush_event_queue(event_queue);
332 int w = al_get_display_width(
display);
333 int h = al_get_display_height(
display);
337 size_t logic_duration = 0;
338 size_t drawing_duration = 0;
343 al_wait_for_event(event_queue, &ev);
345 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
346 switch (ev.keyboard.keycode) {
350 case ALLEGRO_KEY_DOWN:
353 case ALLEGRO_KEY_LEFT:
356 case ALLEGRO_KEY_RIGHT:
359 case ALLEGRO_KEY_ESCAPE:
362 case ALLEGRO_KEY_SPACE:
365 case ALLEGRO_KEY_LSHIFT:
366 case ALLEGRO_KEY_RSHIFT:
369 case ALLEGRO_KEY_PAD_MINUS:
370 key[KEY_PAD_MINUS] = 1;
372 case ALLEGRO_KEY_PAD_PLUS:
373 key[KEY_PAD_PLUS] = 1;
375 case ALLEGRO_KEY_PAD_ENTER:
376 key[KEY_PAD_ENTER] = 1;
384 case ALLEGRO_KEY_LCTRL:
385 case ALLEGRO_KEY_RCTRL:
409 }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
410 switch (ev.keyboard.keycode) {
414 case ALLEGRO_KEY_DOWN:
417 case ALLEGRO_KEY_LEFT:
420 case ALLEGRO_KEY_RIGHT:
423 case ALLEGRO_KEY_ESCAPE:
426 case ALLEGRO_KEY_SPACE:
429 case ALLEGRO_KEY_LSHIFT:
430 case ALLEGRO_KEY_RSHIFT:
433 case ALLEGRO_KEY_PAD_MINUS:
434 key[KEY_PAD_MINUS] = 0;
436 case ALLEGRO_KEY_PAD_PLUS:
437 key[KEY_PAD_PLUS] = 0;
439 case ALLEGRO_KEY_PAD_ENTER:
440 key[KEY_PAD_ENTER] = 0;
448 case ALLEGRO_KEY_LCTRL:
449 case ALLEGRO_KEY_RCTRL:
474 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
475 if (al_get_timer_event_source(
fps_timer) == ev.any.source) {
477 }
else if (al_get_timer_event_source(
logic_timer) == ev.any.source) {
480 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
483 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
484 if (ev.mouse.button == 1)
486 if (ev.mouse.button == 2)
488 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
489 if (ev.mouse.button == 1)
491 if (ev.mouse.button == 2)
501 else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
502 al_clear_keyboard_state(
display);
503 al_flush_event_queue(event_queue);
535 if (
key[KEY_RIGHT]) {
539 if (
key[KEY_PAD_PLUS]) {
543 if (
key[KEY_PAD_MINUS]) {
547 if (mouse_button != -1) {
551 }
while (!al_is_event_queue_empty(event_queue));
555 static int old_mx = -1, old_my = -1;
556 double vx = 0.0, vy = 0.0;
557 if (old_mx != mx || old_my != my) {
558 if (old_mx != -1 && old_my != -1) {
573 minJ = (size_t)floor(0.5 * (
double)
fluid_sim->
numY - 0.5 * pipeH);
574 maxJ = (size_t)floor(0.5 * (
double)
fluid_sim->
numY + 0.5 * pipeH);
575 for (
size_t j = 0; j < minJ; j++)
577 for (
size_t j = minJ; j < maxJ; j++)
581 for (
size_t j = minJ; j < maxJ; j++)
584 if (threadedProcessing == 1)
596 scrbuf = al_get_backbuffer(
display);
600 al_set_target_bitmap(scrbuf);
603 al_lock_bitmap(scrbuf, al_get_bitmap_format(scrbuf), ALLEGRO_LOCK_READWRITE);
607 al_draw_circle((
float)mx, (
float)(my - 20 * fluid_factor), (
float)(fluid_factor * fluid_factor / 2), al_map_rgb(255, 0, 0), 2.0);
608 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);
609 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);
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, (
float)(my + 20 * fluid_factor), (
float)(fluid_factor * fluid_factor / 2), al_map_rgb(255, 0, 0), 2.0);
613 static N_STR* textout = NULL;
615 al_draw_text(font, al_map_rgb(0, 0, 255), (
float)
WIDTH, 10, ALLEGRO_ALIGN_RIGHT,
_nstr(textout));
618 al_draw_text(font, al_map_rgb(0, 0, 255), (
float)
WIDTH, 25, ALLEGRO_ALIGN_RIGHT,
_nstr(textout));
620 nstrprintf(textout,
"logic(max %zd): %zd usecs", (
size_t)(1000000.0 /
logicFPS), logic_duration);
621 al_draw_text(font, al_map_rgb(0, 0, 255), 5, 10, ALLEGRO_ALIGN_LEFT,
_nstr(textout));
623 nstrprintf(textout,
"drawing(max %zd): %zd usecs", (
size_t)(1000000.0 /
drawFPS), drawing_duration);
624 al_draw_text(font, al_map_rgb(0, 0, 255), 5, 30, ALLEGRO_ALIGN_LEFT,
_nstr(textout));
627 al_unlock_bitmap(scrbuf);
628 al_set_target_bitmap(al_get_backbuffer(
display));
629 al_draw_bitmap(scrbuf, (
float)(w / 2 - al_get_bitmap_width(scrbuf) / 2), (
float)(h / 2 - al_get_bitmap_height(scrbuf) / 2), 0);
639 }
while (!
key[KEY_ESC] && !
DONE);
641 al_uninstall_system();