Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_fluid_config.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include "ex_fluid_config.h"
28#include "nilorea/n_str.h"
29#include "nilorea/n_fluids.h"
31
45int load_app_state(char* state_filename, int* WIDTH, int* HEIGHT, bool* fullscreen, char** bgmusic, double* drawFPS, double* logicFPS, N_FLUID* fluid, int* threaded) {
46 __n_assert(state_filename, return FALSE);
47 __n_assert(fluid, return FALSE);
48
49 if (access(state_filename, F_OK) != 0) {
50 n_log(LOG_INFO, "no app state %s to load !", state_filename);
51 return FALSE;
52 }
53
54 /* N_STR *data = NULL ;
55 data = file_to_nstr( state_filename );
56 if( !data )
57 {
58 n_log( LOG_ERR , "Error reading file %s, defaults will be used" , state_filename );
59 return FALSE;
60 }
61
62 cJSON *monitor_json = cJSON_Parse( _nstr( data ) );
63 if (monitor_json == NULL)
64 {
65 const char *error_ptr = cJSON_GetErrorPtr();
66 n_log( LOG_ERR , "%s: Error before: %s, defaults will be used", state_filename , _str( error_ptr ) );
67 cJSON_Delete( monitor_json );
68 free_nstr( &data );
69 return FALSE ;
70 }
71
72 cJSON *value = NULL ;
73 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "width" ); if( cJSON_IsNumber( value ) ){ (*WIDTH) = value -> valueint ; } else { n_log( LOG_ERR , "width is not a number"); }
74 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "height" ); if( cJSON_IsNumber( value ) ){ (*HEIGHT) = value -> valueint ; } else { n_log( LOG_ERR , "height is not a number"); }
75 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "fullscreen" ); if( cJSON_IsNumber( value ) ){ (*fullscreen) = value -> valueint ; } else { n_log( LOG_ERR , "fullscreen is not a number"); }
76 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "bg-music" ); if( cJSON_IsString( value ) ){ (*bgmusic) = strdup( value -> valuestring ); } else { n_log( LOG_ERR , "bg-music is not a string"); }
77 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "drawFPS" ); if( cJSON_IsNumber( value ) ){ (*drawFPS) = value -> valuedouble ; } else { n_log( LOG_ERR , "drawFPS is not a number"); }
78 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "logicFPS" ); if( cJSON_IsNumber( value ) ){ (*logicFPS) = value -> valuedouble ; } else { n_log( LOG_ERR , "logicFPS is not a number"); }
79
80 cJSON_Delete(monitor_json);
81 free_nstr( &data );
82 */
83 int errors = 0;
84 CONFIG_FILE* config = load_config_file(state_filename, &errors);
85 if (!config) {
86 n_log(LOG_ERR, "Unable to load config file from %s", state_filename);
87 return FALSE;
88 }
89 if (errors != 0) {
90 n_log(LOG_ERR, "There were %d errors in %s. Check the logs !", errors, state_filename);
91 return FALSE;
92 }
93 /* default section, only one should be allocated. Let's test it ! */
94 size_t nb = get_nb_config_file_sections(config, "__DEFAULT__");
95 for (size_t it = 0; it < nb; it++) {
96 char* value = NULL;
97 value = get_config_section_value(config, "__DEFAULT__", it, "width", 0);
98 if (value) (*WIDTH) = atoi(value);
99 value = get_config_section_value(config, "__DEFAULT__", it, "height", 0);
100 if (value) (*HEIGHT) = atoi(value);
101 value = get_config_section_value(config, "__DEFAULT__", it, "fullscreen", 0);
102 if (value) (*fullscreen) = atoi(value);
103 value = get_config_section_value(config, "__DEFAULT__", it, "bg-music", 0);
104 if (value) (*bgmusic) = strdup(value);
105 value = get_config_section_value(config, "__DEFAULT__", it, "drawFPS", 0);
106 if (value) (*drawFPS) = strtod(value, NULL);
107 value = get_config_section_value(config, "__DEFAULT__", it, "logicFPS", 0);
108 if (value) (*logicFPS) = strtod(value, NULL);
109 value = get_config_section_value(config, "__DEFAULT__", it, "numIters", 0);
110 if (value) fluid->numIters = (size_t)atoi(value);
111 value = get_config_section_value(config, "__DEFAULT__", it, "density", 0);
112 if (value) fluid->density = strtod(value, NULL);
113 value = get_config_section_value(config, "__DEFAULT__", it, "gravity", 0);
114 if (value) fluid->gravity = strtod(value, NULL);
115 value = get_config_section_value(config, "__DEFAULT__", it, "overRelaxation", 0);
116 if (value) fluid->overRelaxation = strtod(value, NULL);
117 value = get_config_section_value(config, "__DEFAULT__", it, "fluid_production_percentage", 0);
118 if (value) fluid->fluid_production_percentage = strtod(value, NULL);
119 value = get_config_section_value(config, "__DEFAULT__", it, "cScale", 0);
120 if (value) fluid->cScale = strtod(value, NULL);
121 value = get_config_section_value(config, "__DEFAULT__", it, "threadedProcessing", 0);
122 if (value) (*threaded) = atoi(value);
123 }
124
125 /*data = file_to_nstr( state_filename );
126 if( !data )
127 {
128 n_log( LOG_ERR , "Error reading file %s, defaults will be used" , state_filename );
129 return FALSE;
130 }
131
132 cJSON *monitor_json = cJSON_Parse( _nstr( data ) );
133 if (monitor_json == NULL)
134 {
135 const char *error_ptr = cJSON_GetErrorPtr();
136 n_log( LOG_ERR , "%s: Error before: %s, defaults will be used", state_filename , _str( error_ptr ) );
137 cJSON_Delete( monitor_json );
138 free_nstr( &data );
139 return FALSE ;
140 }
141
142 cJSON *value = NULL ;
143 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "numIters" ); if( cJSON_IsNumber( value ) ){ fluid -> numIters = value -> valueint ; } else { n_log( LOG_ERR , "numIters is not a number"); }
144 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "density" ); if( cJSON_IsNumber( value ) ){ fluid -> density = value -> valuedouble ; } else { n_log( LOG_ERR , "density is not a number"); }
145 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "gravity" ); if( cJSON_IsNumber( value ) ){ fluid -> gravity = value -> valuedouble ; } else { n_log( LOG_ERR , "gravity is not a number"); }
146 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "overRelaxation" ); if( cJSON_IsNumber( value ) ){ fluid -> overRelaxation = value -> valuedouble ; } else { n_log( LOG_ERR , "overRelaxation is not a number"); }
147 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "fluid_production_percentage" ); if( cJSON_IsNumber( value ) ){ fluid -> fluid_production_percentage = value -> valuedouble ; }else { n_log( LOG_ERR , "fluid_production_percentage is not a number"); }
148 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "cScale" ); if( cJSON_IsNumber( value ) ){ fluid -> cScale = value -> valuedouble ; } else { n_log( LOG_ERR , "cScale is not a number"); }
149 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "threadedProcessing" ); if( cJSON_IsNumber( value ) ){ (*threaded) = value -> valueint ; } else { n_log( LOG_ERR , "threadedProcessing is not a number"); }
150
151 cJSON_Delete(monitor_json);
152 free_nstr( &data ); */
153
154 destroy_config_file(&config);
155
156 return TRUE;
157}
double drawFPS
Definition ex_fluid.c:63
bool fullscreen
Definition ex_fluid.c:74
char * bgmusic
Definition ex_fluid.c:75
double logicFPS
Definition ex_fluid.c:64
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:36
#define HEIGHT
Definition ex_gui.c:37
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
CONFIG_FILE * load_config_file(char *filename, int *errors)
load a config file
int destroy_config_file(CONFIG_FILE **cfg_file)
Destroy a loaded config file.
char * get_config_section_value(CONFIG_FILE *cfg_file, char *section_name, size_t section_position, char *entry, size_t entry_position)
Function to parse sections and get entries values.
size_t get_nb_config_file_sections(CONFIG_FILE *cfg_file, const char *section_name)
Get the number of config file with section_name.
Structure of a config file.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_ERR
error conditions
Definition n_log.h:75
#define LOG_INFO
informational
Definition n_log.h:81
double fluid_production_percentage
size of the produced fluid
Definition n_fluids.h:103
double overRelaxation
over relaxation
Definition n_fluids.h:89
size_t numIters
number of fluid processing iterations for each frame
Definition n_fluids.h:79
double density
density of the fluid (not working ?)
Definition n_fluids.h:81
double cScale
scale used to deduce cellX and cellY from screen/window width and height
Definition n_fluids.h:105
double gravity
gravity on Y
Definition n_fluids.h:87
structure of a fluid
Definition n_fluids.h:69
Config file reading and writing.
Fluid management port from "How to write an Eulerian fluid simulator with 200 lines of code",...
N_STR and string function declaration.