Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_fluid.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27
28#define ALLEGRO_UNSTABLE 1
29
30#include <locale.h>
31
32#include "nilorea/n_common.h"
33#include "nilorea/n_log.h"
34#include "nilorea/n_list.h"
35#include "nilorea/n_str.h"
36#include "nilorea/n_time.h"
37#include "nilorea/n_fluids.h"
38
39#include <allegro5/allegro.h>
40#include <allegro5/allegro_ttf.h>
41#include "allegro5/allegro_audio.h"
42#include "allegro5/allegro_acodec.h"
43#include <allegro5/allegro_font.h>
44#include <allegro5/allegro_image.h>
45#include <allegro5/allegro_primitives.h>
46
47#include "ex_fluid_config.h"
48
50#define RESERVED_SAMPLES 16
52#define MAX_SAMPLE_DATA 10
53
54ALLEGRO_DISPLAY* display = NULL;
55
56/* VARIOUS DECLARATIONS * */
57
58int DONE = 0, /* Flag to check if we are always running */
59 getoptret = 0, /* launch parameter check */
60 log_level = LOG_ERR; /* default LOG_LEVEL */
61
62double drawFPS = 60.0;
63double logicFPS = 120.0;
64
65ALLEGRO_TIMER* fps_timer = NULL;
66ALLEGRO_TIMER* logic_timer = NULL;
69
71int WIDTH = 800,
72 HEIGHT = 600;
73bool fullscreen = 0;
74char* bgmusic = NULL;
75
77
78int main(int argc, char* argv[]) {
79 /* Set the locale to the POSIX C environment */
80 setlocale(LC_ALL, "POSIX");
81
83
84 N_STR* log_file = NULL;
85 nstrprintf(log_file, "%s.log", argv[0]);
86 /*set_log_file( _nstr( log_file ) );*/
88
89 char ver_str[128] = "";
90
91 while ((getoptret = getopt(argc, argv, "hvV:L:")) != EOF) {
92 switch (getoptret) {
93 case 'h':
94 n_log(LOG_NOTICE, "\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG)\n", argv[0]);
95 exit(TRUE);
96 case 'v':
97 sprintf(ver_str, "%s %s", __DATE__, __TIME__);
98 exit(TRUE);
99 break;
100 case 'V':
101 if (!strncmp("INFO", optarg, 6)) {
103 } else {
104 if (!strncmp("NOTICE", optarg, 6)) {
106 } else {
107 if (!strncmp("VERBOSE", optarg, 7)) {
109 } else {
110 if (!strncmp("ERROR", optarg, 5)) {
112 } else {
113 if (!strncmp("DEBUG", optarg, 5)) {
115 } else {
116 n_log(LOG_ERR, "%s is not a valid log level", optarg);
117 exit(FALSE);
118 }
119 }
120 }
121 }
122 }
123 n_log(LOG_NOTICE, "LOG LEVEL UP TO: %d", log_level);
125 break;
126 case 'L':
127 n_log(LOG_NOTICE, "LOG FILE: %s", optarg);
128 set_log_file(optarg);
129 break;
130 case '?': {
131 switch (optopt) {
132 case 'V':
133 n_log(LOG_ERR, "\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG");
134 break;
135 case 'L':
136 n_log(LOG_ERR, "\nPlease specify a log file after -L");
137 default:
138 break;
139 }
140 }
141 __attribute__((fallthrough));
142 default:
143 n_log(LOG_ERR, "\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile", argv[0]);
144 exit(FALSE);
145 }
146 }
147 /*
148 * INITIALISATION
149 */
150 /* allegro 5 + addons loading */
151 if (!al_init()) {
152 n_abort("Could not init Allegro.\n");
153 }
154
155 /* The data is in DATAS/ and CONFIGS/ beside the executable: found from
156 * wherever the example is started, not only from examples/. */
157 {
158 ALLEGRO_PATH* here = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
159
160 if (here) {
161 al_change_directory(al_path_cstr(here, ALLEGRO_NATIVE_PATH_SEP));
162 al_destroy_path(here);
163 }
164 }
165 /* After the change of directory above, so CONFIGS/ is found beside the
166 * executable like DATAS/. */
167 int threadedProcessing = 0;
168 N_FLUID* fluid_data = NULL;
169 Malloc(fluid_data, N_FLUID, 1);
170 if (load_app_state("CONFIGS/ex_fluid.cfg", &WIDTH, &HEIGHT, &fullscreen, &bgmusic, &drawFPS, &logicFPS, fluid_data, &threadedProcessing) != TRUE) {
171 n_log(LOG_ERR, "couldn't load CONFIGS/ex_fluid.cfg !");
172 exit(1);
173 }
174 double fluid_factor = fluid_data->cScale;
175 n_log(LOG_DEBUG, "%s starting with params: %dx%d fullscreen(%d), music: %s", argv[0], WIDTH, HEIGHT, fullscreen, _str(bgmusic));
176
177 if (!al_init_acodec_addon()) {
178 n_abort("Could not register addons.\n");
179 }
180 if (!al_install_audio()) {
181 n_log(LOG_ERR, "Unable to initialize audio addon, disabling bgmusic\n");
182 Free(bgmusic);
183 }
184 if (!al_init_acodec_addon()) {
185 n_abort("Unable to initialize acoded addon\n");
186 }
187 if (!al_init_image_addon()) {
188 n_abort("Unable to initialize image addon\n");
189 }
190 if (!al_init_primitives_addon()) {
191 n_abort("Unable to initialize primitives addon\n");
192 }
193 if (!al_init_font_addon()) {
194 n_abort("Unable to initialize font addon\n");
195 }
196 if (!al_init_ttf_addon()) {
197 n_abort("Unable to initialize ttf_font addon\n");
198 }
199 if (!al_install_keyboard()) {
200 n_abort("Unable to initialize keyboard handler\n");
201 }
202 if (!al_install_mouse()) {
203 n_abort("Unable to initialize mouse handler\n");
204 }
205
206 if (bgmusic && !al_reserve_samples(RESERVED_SAMPLES)) {
207 n_abort("Could not set up voice and mixer.\n");
208 }
209
210 ALLEGRO_SAMPLE* sample_data[MAX_SAMPLE_DATA] = {NULL};
211 memset(sample_data, 0, sizeof(sample_data));
212
213 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
214
215 event_queue = al_create_event_queue();
216 if (!event_queue) {
217 fprintf(stderr, "failed to create event_queue!\n");
218 return -1;
219 }
220
221 if (fullscreen) {
222 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_FULLSCREEN_WINDOW);
223 } else {
224 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
225 }
226
227#ifdef __windows__
228 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP | ALLEGRO_NO_PRESERVE_TEXTURE);
229#endif
230
231 /* Before any window exists: a missing font used to end the example with
232 * its window open, which crashed on some OpenGL drivers on the way out. */
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);
236 return 1;
237 }
238
239 display = al_create_display(WIDTH, HEIGHT);
240 if (!display) {
241 n_abort("Unable to create display\n");
242 }
243
244 al_set_window_title(display, argv[0]);
245
246 ALLEGRO_FONT* font = al_load_font("DATAS/2Dumb.ttf", 18, 0);
247 if (!font) {
248 n_abort("Unable to load font DATAS/2Dumb.ttf\n");
249 }
250
251 DONE = 0;
252 fps_timer = al_create_timer(1.0 / drawFPS);
253 logic_timer = al_create_timer(1.0 / logicFPS);
254
255 al_register_event_source(event_queue, al_get_display_event_source(display));
256 al_start_timer(fps_timer);
257 al_start_timer(logic_timer);
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));
260
261 al_register_event_source(event_queue, al_get_keyboard_event_source());
262 al_register_event_source(event_queue, al_get_mouse_event_source());
263
264 bool backbuffer = 1;
265 ALLEGRO_BITMAP* scrbuf = NULL;
266 ALLEGRO_BITMAP* bitmap = NULL;
267
268 al_hide_mouse_cursor(display);
269
270 enum APP_KEYS {
271 KEY_UP,
272 KEY_DOWN,
273 KEY_LEFT,
274 KEY_RIGHT,
275 KEY_ESC,
276 KEY_SPACE,
277 KEY_CTRL,
278 KEY_SHIFT,
279 KEY_PAD_MINUS,
280 KEY_PAD_PLUS,
281 KEY_PAD_ENTER,
282 KEY_M,
283 KEY_W,
284 KEY_F1,
285 KEY_F2,
286 KEY_F3,
287 KEY_F4,
288 KEY_F5,
289 KEY_F6
290 };
291 int key[19] = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
292
293 if (bgmusic) {
294 if (!(sample_data[0] = al_load_sample(bgmusic))) {
295 n_log(LOG_ERR, "Could not load %s", bgmusic);
296 exit(1);
297 }
298 al_play_sample(sample_data[0], 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);
299 }
300
301 long int nb_cores = get_nb_cpu_cores();
302 if (nb_cores <= 0) {
303 nb_cores = 1;
304 }
305 thread_pool = new_thread_pool((size_t)nb_cores, 0);
306
307 /* set fluid */
308 fluid_sim = new_n_fluid(fluid_data->density, fluid_data->gravity, fluid_data->numIters, fluid_data->fluid_production_percentage, fluid_data->overRelaxation, (size_t)(WIDTH / fluid_factor), (size_t)(HEIGHT / fluid_factor));
309
310 fluid_sim->density = fluid_data->density;
311 fluid_sim->gravity = fluid_data->gravity;
312 fluid_sim->numIters = fluid_data->numIters;
313 fluid_sim->fluid_production_percentage = fluid_data->fluid_production_percentage;
314 fluid_sim->overRelaxation = fluid_data->overRelaxation;
315 fluid_sim->cScale = fluid_data->cScale;
316 Free(fluid_data);
317
318 fluid_sim->dt = 1.0 / logicFPS;
319
320 size_t n = fluid_sim->numY;
321 double inVel = 2.0;
322 for (size_t i = 0; i < fluid_sim->numX; i++) {
323 for (size_t j = 0; j < fluid_sim->numY; j++) {
324 double s = 1.0; // fluid
325 if (i == 0 || j == 0 || j == fluid_sim->numY - 1)
326 s = 0.0; // solid
327 //
328 fluid_sim->s[i * n + j] = s;
329
330 if (i == 1) {
331 fluid_sim->u[i * n + j] = inVel;
332 }
333 }
334 }
335 double pipeH = fluid_sim->fluid_production_percentage * (double)fluid_sim->numY;
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++)
339 fluid_sim->m[j] = 1.0;
340 for (size_t j = minJ; j < maxJ; j++)
341 fluid_sim->m[j] = 0.0;
342 for (size_t j = maxJ; j < fluid_sim->numY; j++)
343 fluid_sim->m[j] = 1.0;
344
345 bool do_draw = 1, do_logic = 1;
346 int mx = WIDTH / 3, my = HEIGHT / 2, mouse_button = 0, mouse_b1 = 0, mouse_b2 = 0;
347
349
350 al_flush_event_queue(event_queue);
351 al_set_mouse_xy(display, WIDTH / 3, HEIGHT / 2);
352
353 int w = al_get_display_width(display);
354 int h = al_get_display_height(display);
355
356 bitmap = al_create_bitmap(WIDTH, HEIGHT);
357
358 size_t logic_duration = 0;
359 size_t drawing_duration = 0;
360 do {
361 do {
362 ALLEGRO_EVENT ev;
363
364 al_wait_for_event(event_queue, &ev);
365
366 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
367 switch (ev.keyboard.keycode) {
368 case ALLEGRO_KEY_UP:
369 key[KEY_UP] = 1;
370 break;
371 case ALLEGRO_KEY_DOWN:
372 key[KEY_DOWN] = 1;
373 break;
374 case ALLEGRO_KEY_LEFT:
375 key[KEY_LEFT] = 1;
376 break;
377 case ALLEGRO_KEY_RIGHT:
378 key[KEY_RIGHT] = 1;
379 break;
380 case ALLEGRO_KEY_ESCAPE:
381 key[KEY_ESC] = 1;
382 break;
383 case ALLEGRO_KEY_SPACE:
384 key[KEY_SPACE] = 1;
385 break;
386 case ALLEGRO_KEY_LSHIFT:
387 case ALLEGRO_KEY_RSHIFT:
388 key[KEY_SHIFT] = 1;
389 break;
390 case ALLEGRO_KEY_PAD_MINUS:
391 key[KEY_PAD_MINUS] = 1;
392 break;
393 case ALLEGRO_KEY_PAD_PLUS:
394 key[KEY_PAD_PLUS] = 1;
395 break;
396 case ALLEGRO_KEY_PAD_ENTER:
397 key[KEY_PAD_ENTER] = 1;
398 break;
399 case ALLEGRO_KEY_M:
400 key[KEY_M] = 1;
401 break;
402 case ALLEGRO_KEY_W:
403 key[KEY_W] = 1;
404 break;
405 case ALLEGRO_KEY_LCTRL:
406 case ALLEGRO_KEY_RCTRL:
407 key[KEY_CTRL] = 1;
408 break;
409 case ALLEGRO_KEY_F1:
410 key[KEY_F1] = 1;
411 break;
412 case ALLEGRO_KEY_F2:
413 key[KEY_F2] = 1;
414 break;
415 case ALLEGRO_KEY_F3:
416 key[KEY_F3] = 1;
417 break;
418 case ALLEGRO_KEY_F4:
419 key[KEY_F4] = 1;
420 break;
421 case ALLEGRO_KEY_F5:
422 key[KEY_F5] = 1;
423 break;
424 case ALLEGRO_KEY_F6:
425 key[KEY_F6] = 1;
426 break;
427 default:
428 break;
429 }
430 } else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
431 switch (ev.keyboard.keycode) {
432 case ALLEGRO_KEY_UP:
433 key[KEY_UP] = 0;
434 break;
435 case ALLEGRO_KEY_DOWN:
436 key[KEY_DOWN] = 0;
437 break;
438 case ALLEGRO_KEY_LEFT:
439 key[KEY_LEFT] = 0;
440 break;
441 case ALLEGRO_KEY_RIGHT:
442 key[KEY_RIGHT] = 0;
443 break;
444 case ALLEGRO_KEY_ESCAPE:
445 key[KEY_ESC] = 0;
446 break;
447 case ALLEGRO_KEY_SPACE:
448 key[KEY_SPACE] = 0;
449 break;
450 case ALLEGRO_KEY_LSHIFT:
451 case ALLEGRO_KEY_RSHIFT:
452 key[KEY_SHIFT] = 0;
453 break;
454 case ALLEGRO_KEY_PAD_MINUS:
455 key[KEY_PAD_MINUS] = 0;
456 break;
457 case ALLEGRO_KEY_PAD_PLUS:
458 key[KEY_PAD_PLUS] = 0;
459 break;
460 case ALLEGRO_KEY_PAD_ENTER:
461 key[KEY_PAD_ENTER] = 0;
462 break;
463 case ALLEGRO_KEY_M:
464 key[KEY_M] = 0;
465 break;
466 case ALLEGRO_KEY_W:
467 key[KEY_W] = 0;
468 break;
469 case ALLEGRO_KEY_LCTRL:
470 case ALLEGRO_KEY_RCTRL:
471 key[KEY_CTRL] = 0;
472 break;
473 case ALLEGRO_KEY_F1:
474 key[KEY_F1] = 0;
475 break;
476 case ALLEGRO_KEY_F2:
477 key[KEY_F2] = 0;
478 break;
479 case ALLEGRO_KEY_F3:
480 key[KEY_F3] = 0;
481 break;
482 case ALLEGRO_KEY_F4:
483 key[KEY_F4] = 0;
484 break;
485 case ALLEGRO_KEY_F5:
486 key[KEY_F5] = 0;
487 break;
488 case ALLEGRO_KEY_F6:
489 key[KEY_F6] = 0;
490 break;
491
492 default:
493 break;
494 }
495 } else if (ev.type == ALLEGRO_EVENT_TIMER) {
496 if (al_get_timer_event_source(fps_timer) == ev.any.source) {
497 do_draw = 1;
498 } else if (al_get_timer_event_source(logic_timer) == ev.any.source) {
499 do_logic = 1;
500 }
501 } else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
502 mx = ev.mouse.x;
503 my = ev.mouse.y;
504 } else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
505 if (ev.mouse.button == 1)
506 mouse_b1 = 1;
507 if (ev.mouse.button == 2)
508 mouse_b2 = 1;
509 } else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
510 if (ev.mouse.button == 1)
511 mouse_b1 = 0;
512 if (ev.mouse.button == 2)
513 mouse_b2 = 0;
514 }
515
516 /* Processing inputs */
517 mouse_button = -1;
518 if (mouse_b1 == 1)
519 mouse_button = 1;
520 if (mouse_b2 == 1)
521 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);
525 } else {
526 // Processing inputs
527 // get_keyboard( chat_line , ev );
528 if (key[KEY_F1]) {
529 fluid_sim->showSmoke = 1;
530 }
531 if (key[KEY_F2]) {
532 fluid_sim->showSmoke = 0;
533 }
534 if (key[KEY_F3]) {
535 fluid_sim->showPressure = 1;
536 }
537 if (key[KEY_F4]) {
538 fluid_sim->showPressure = 0;
539 }
540 if (key[KEY_F5]) {
541 fluid_sim->showPaint = 1;
542 }
543 if (key[KEY_F6]) {
544 fluid_sim->showPaint = 0;
545 }
546 if (key[KEY_UP]) {
547 fluid_sim->density += 100.0;
548 }
549 if (key[KEY_DOWN]) {
550 fluid_sim->density -= 100.0;
551 if (fluid_sim->density < 1.0) fluid_sim->density = 1.0;
552 }
553 if (key[KEY_LEFT]) {
554 fluid_sim->numIters++;
555 }
556 if (key[KEY_RIGHT]) {
557 fluid_sim->numIters--;
558 if (fluid_sim->numIters < 1.0) fluid_sim->numIters = 1.0;
559 }
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;
563 }
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;
567 }
568 if (mouse_button != -1) {
569 n_log(LOG_DEBUG, "mouse button: %d", mouse_button);
570 }
571 }
572 } while (!al_is_event_queue_empty(event_queue));
573
574 if (do_logic == 1) {
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) {
580 vx = (old_mx - mx) / logicFPS;
581 vy = (old_my - my) / logicFPS;
582 }
583 old_mx = mx;
584 old_my = my;
586 n_fluid_setObstacle(fluid_sim, mx / fluid_factor, (my / fluid_factor) - 20.0, vx, vy, fluid_factor / 2);
587 n_fluid_setObstacle(fluid_sim, (mx / fluid_factor) - 15, my / fluid_factor, vx, vy, fluid_factor / 2 + fluid_factor / 3);
588 n_fluid_setObstacle(fluid_sim, (mx / fluid_factor) + 15, my / fluid_factor - 10.0, vx, vy, fluid_factor / 2 + fluid_factor / 2);
589 n_fluid_setObstacle(fluid_sim, (mx / fluid_factor) + 15, my / fluid_factor + 10.0, vx, vy, fluid_factor / 2 + fluid_factor / 2);
590 n_fluid_setObstacle(fluid_sim, mx / fluid_factor, (my / fluid_factor) + 20.0, vx, vy, fluid_factor / 2);
591 }
592
593 pipeH = fluid_sim->fluid_production_percentage * (double)fluid_sim->numY;
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++)
597 fluid_sim->m[j] = 1.0;
598 for (size_t j = minJ; j < maxJ; j++)
599 fluid_sim->m[j] = 0.0;
600 for (size_t j = maxJ; j < fluid_sim->numY; j++)
601 fluid_sim->m[j] = 1.0;
602 for (size_t j = minJ; j < maxJ; j++)
603 fluid_sim->m[j] = 0.0;
604
605 if (threadedProcessing == 1)
607 else
609
610 logic_duration = (logic_duration + (size_t)get_usec(&logic_chrono)) / 2;
611 do_logic = 0;
612 }
613 if (do_draw == 1) {
615
616 if (backbuffer)
617 scrbuf = al_get_backbuffer(display);
618 else
619 scrbuf = bitmap;
620
621 al_set_target_bitmap(scrbuf);
622
623 if (!backbuffer)
624 al_lock_bitmap(scrbuf, al_get_bitmap_format(scrbuf), ALLEGRO_LOCK_READWRITE);
625
627
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);
633
634 static N_STR* textout = NULL;
635 nstrprintf(textout, "[F1/F2]:showSmoke:%d [F3/F4]:showPressure:%d [F5/F6]:showPaint:%d", fluid_sim->showSmoke, fluid_sim->showPressure, fluid_sim->showPaint);
636 al_draw_text(font, al_map_rgb(0, 0, 255), (float)WIDTH, 10, ALLEGRO_ALIGN_RIGHT, _nstr(textout));
637
638 nstrprintf(textout, "numIters: %zd production: %3.3g density: %3.3g", fluid_sim->numIters, fluid_sim->fluid_production_percentage, fluid_sim->density);
639 al_draw_text(font, al_map_rgb(0, 0, 255), (float)WIDTH, 25, ALLEGRO_ALIGN_RIGHT, _nstr(textout));
640
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));
643
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));
646
647 if (!backbuffer) {
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);
651 }
652
653 drawing_duration = (drawing_duration + (size_t)get_usec(&drawing_chrono)) / 2;
654
655 al_flip_display();
656
657 do_draw = 0;
658 }
659
660 } while (!key[KEY_ESC] && !DONE);
661
662 al_uninstall_system();
663
664 return 0;
665}
int main(void)
Definition ex_atlas.c:117
static int log_level
ALLEGRO_TIMER * fps_timer
Definition ex_fluid.c:65
THREAD_POOL * thread_pool
Definition ex_fluid.c:76
#define MAX_SAMPLE_DATA
audio MAX_SAMPLE_DATA for bgmusic
Definition ex_fluid.c:52
int getoptret
Definition ex_fluid.c:59
double drawFPS
Definition ex_fluid.c:62
int DONE
Definition ex_fluid.c:58
bool fullscreen
Definition ex_fluid.c:73
N_TIME drawing_chrono
Definition ex_fluid.c:68
N_FLUID * fluid_sim
Definition ex_fluid.c:70
N_TIME logic_chrono
Definition ex_fluid.c:67
char * bgmusic
Definition ex_fluid.c:74
#define RESERVED_SAMPLES
audio RESERVED_SAMPLES for bgmusic
Definition ex_fluid.c:50
ALLEGRO_TIMER * logic_timer
Definition ex_fluid.c:66
double logicFPS
Definition ex_fluid.c:63
ALLEGRO_DISPLAY * display
Definition ex_fluid.c:54
int load_app_state(char *state_filename, int *WIDTH, int *HEIGHT, bool *fullscreen, char **bgmusic, double *drawFPS, double *logicFPS, N_FLUID *fluid, int *threaded)
Load application state from a config file.
#define WIDTH
Definition ex_gui.c:42
#define HEIGHT
Definition ex_gui.c:43
static void mouse_button(N_GUI_CTX *gui, int down, float x, float y)
static int h(void *ctx, const N_ROUTE_MATCH *m)
a handler that does nothing (the test only routes)
#define KEY_RIGHT
key code
#define KEY_DOWN
key code
#define KEY_F1
key code
#define KEY_F5
key code
volatile char key[227]
TRUE while the key is held.
#define KEY_F3
key code
FONT * font
the default 8x8 font, valid after allegro_init()
#define KEY_ESC
key code
#define KEY_SPACE
key code
#define KEY_F4
key code
#define KEY_F6
key code
#define KEY_M
key code
#define KEY_F2
key code
#define KEY_LEFT
key code
#define KEY_UP
key code
#define KEY_W
key code
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:204
#define _str(__PTR)
define true
Definition n_common.h:193
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:58
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:199
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_DEBUG
debug-level messages
Definition n_log.h:108
#define LOG_ERR
error conditions
Definition n_log.h:100
int set_log_file(char *file)
Set the logging to a file instead of stderr.
Definition n_log.c:170
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_INFO
informational
Definition n_log.h:106
double fluid_production_percentage
size of the produced fluid
Definition n_fluids.h:104
double overRelaxation
over relaxation
Definition n_fluids.h:90
size_t numIters
number of fluid processing iterations for each frame
Definition n_fluids.h:80
double density
density of the fluid (not working ?)
Definition n_fluids.h:82
double cScale
scale used to deduce cellX and cellY from screen/window width and height
Definition n_fluids.h:106
double gravity
gravity on Y
Definition n_fluids.h:88
int n_fluid_draw(N_FLUID *fluid)
draw a N_FLUID on screen / targert bitmap
Definition n_fluids.c:843
int n_fluid_simulate_threaded(N_FLUID *fluid, THREAD_POOL *thread_pool)
a threaded version of N_FLUID global processing function
Definition n_fluids.c:641
int n_fluid_simulate(N_FLUID *fluid)
non threaded version of N_FLUID global processing function
Definition n_fluids.c:624
int n_fluid_resetObstacles(N_FLUID *fluid)
reset the obstacles set in a N_FLUID
Definition n_fluids.c:740
int n_fluid_setObstacle(N_FLUID *fluid, double x, double y, double vx, double vy, double r)
set an obstacle in the fluid grid
Definition n_fluids.c:713
N_FLUID * new_n_fluid(double density, double gravity, size_t numIters, double dt, double overRelaxation, size_t sx, size_t sy)
return a newly allocated fluid
Definition n_fluids.c:96
structure of a fluid
Definition n_fluids.h:70
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:117
A box including a string and his lenght.
Definition n_str.h:61
int start_HiTimer(N_TIME *timer)
Initialize or restart from zero any N_TIME HiTimer.
Definition n_time.c:86
time_t get_usec(N_TIME *timer)
Poll any N_TIME HiTimer, returning usec, and moving currentTime to startTime.
Definition n_time.c:108
Timing Structure.
Definition n_time.h:49
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
long int get_nb_cpu_cores()
get number of core of current system
Structure of a thread pool.
Common headers and low-level functions & define.
Fluid management port from "How to write an Eulerian fluid simulator with 200 lines of code",...
List structures and definitions.
static FILE * log_file
static FILE handling if logging to file is enabled
Definition n_log.c:84
Generic log system.
__attribute__((unused))
Definition n_network.c:1288
N_STR and string function declaration.
Timing utilities.