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 * 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
28#include "ex_fluid_config.h"
29#include "nilorea/n_str.h"
30#include "nilorea/n_fluids.h"
32
46int load_app_state(char* state_filename, int* WIDTH, int* HEIGHT, bool* fullscreen, char** bgmusic, double* drawFPS, double* logicFPS, N_FLUID* fluid, int* threaded) {
47 __n_assert(state_filename, return FALSE);
48 __n_assert(fluid, return FALSE);
49
50 if (access(state_filename, F_OK) != 0) {
51 n_log(LOG_INFO, "no app state %s to load !", state_filename);
52 return FALSE;
53 }
54
55 /* N_STR *data = NULL ;
56 data = file_to_nstr( state_filename );
57 if( !data )
58 {
59 n_log( LOG_ERR , "Error reading file %s, defaults will be used" , state_filename );
60 return FALSE;
61 }
62
63 cJSON *monitor_json = cJSON_Parse( _nstr( data ) );
64 if (monitor_json == NULL)
65 {
66 const char *error_ptr = cJSON_GetErrorPtr();
67 n_log( LOG_ERR , "%s: Error before: %s, defaults will be used", state_filename , _str( error_ptr ) );
68 cJSON_Delete( monitor_json );
69 free_nstr( &data );
70 return FALSE ;
71 }
72
73 cJSON *value = NULL ;
74 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "width" ); if( cJSON_IsNumber( value ) ){ (*WIDTH) = value -> valueint ; } else { n_log( LOG_ERR , "width is not a number"); }
75 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "height" ); if( cJSON_IsNumber( value ) ){ (*HEIGHT) = value -> valueint ; } else { n_log( LOG_ERR , "height is not a number"); }
76 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "fullscreen" ); if( cJSON_IsNumber( value ) ){ (*fullscreen) = value -> valueint ; } else { n_log( LOG_ERR , "fullscreen is not a number"); }
77 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"); }
78 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "drawFPS" ); if( cJSON_IsNumber( value ) ){ (*drawFPS) = value -> valuedouble ; } else { n_log( LOG_ERR , "drawFPS is not a number"); }
79 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "logicFPS" ); if( cJSON_IsNumber( value ) ){ (*logicFPS) = value -> valuedouble ; } else { n_log( LOG_ERR , "logicFPS is not a number"); }
80
81 cJSON_Delete(monitor_json);
82 free_nstr( &data );
83 */
84 int errors = 0;
85 CONFIG_FILE* config = load_config_file(state_filename, &errors);
86 if (!config) {
87 n_log(LOG_ERR, "Unable to load config file from %s", state_filename);
88 return FALSE;
89 }
90 if (errors != 0) {
91 n_log(LOG_ERR, "There were %d errors in %s. Check the logs !", errors, state_filename);
92 return FALSE;
93 }
94 /* default section, only one should be allocated. Let's test it ! */
95 size_t nb = get_nb_config_file_sections(config, "__DEFAULT__");
96 for (size_t it = 0; it < nb; it++) {
97 char* value = NULL;
98 value = get_config_section_value(config, "__DEFAULT__", it, "width", 0);
99 if (value) (*WIDTH) = atoi(value);
100 value = get_config_section_value(config, "__DEFAULT__", it, "height", 0);
101 if (value) (*HEIGHT) = atoi(value);
102 value = get_config_section_value(config, "__DEFAULT__", it, "fullscreen", 0);
103 if (value) (*fullscreen) = atoi(value);
104 value = get_config_section_value(config, "__DEFAULT__", it, "bg-music", 0);
105 if (value) (*bgmusic) = strdup(value);
106 value = get_config_section_value(config, "__DEFAULT__", it, "drawFPS", 0);
107 if (value) (*drawFPS) = strtod(value, NULL);
108 value = get_config_section_value(config, "__DEFAULT__", it, "logicFPS", 0);
109 if (value) (*logicFPS) = strtod(value, NULL);
110 value = get_config_section_value(config, "__DEFAULT__", it, "numIters", 0);
111 if (value) fluid->numIters = (size_t)atoi(value);
112 value = get_config_section_value(config, "__DEFAULT__", it, "density", 0);
113 if (value) fluid->density = strtod(value, NULL);
114 value = get_config_section_value(config, "__DEFAULT__", it, "gravity", 0);
115 if (value) fluid->gravity = strtod(value, NULL);
116 value = get_config_section_value(config, "__DEFAULT__", it, "overRelaxation", 0);
117 if (value) fluid->overRelaxation = strtod(value, NULL);
118 value = get_config_section_value(config, "__DEFAULT__", it, "fluid_production_percentage", 0);
119 if (value) fluid->fluid_production_percentage = strtod(value, NULL);
120 value = get_config_section_value(config, "__DEFAULT__", it, "cScale", 0);
121 if (value) fluid->cScale = strtod(value, NULL);
122 value = get_config_section_value(config, "__DEFAULT__", it, "threadedProcessing", 0);
123 if (value) (*threaded) = atoi(value);
124 }
125
126 /*data = file_to_nstr( state_filename );
127 if( !data )
128 {
129 n_log( LOG_ERR , "Error reading file %s, defaults will be used" , state_filename );
130 return FALSE;
131 }
132
133 cJSON *monitor_json = cJSON_Parse( _nstr( data ) );
134 if (monitor_json == NULL)
135 {
136 const char *error_ptr = cJSON_GetErrorPtr();
137 n_log( LOG_ERR , "%s: Error before: %s, defaults will be used", state_filename , _str( error_ptr ) );
138 cJSON_Delete( monitor_json );
139 free_nstr( &data );
140 return FALSE ;
141 }
142
143 cJSON *value = NULL ;
144 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "numIters" ); if( cJSON_IsNumber( value ) ){ fluid -> numIters = value -> valueint ; } else { n_log( LOG_ERR , "numIters is not a number"); }
145 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "density" ); if( cJSON_IsNumber( value ) ){ fluid -> density = value -> valuedouble ; } else { n_log( LOG_ERR , "density is not a number"); }
146 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "gravity" ); if( cJSON_IsNumber( value ) ){ fluid -> gravity = value -> valuedouble ; } else { n_log( LOG_ERR , "gravity is not a number"); }
147 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "overRelaxation" ); if( cJSON_IsNumber( value ) ){ fluid -> overRelaxation = value -> valuedouble ; } else { n_log( LOG_ERR , "overRelaxation is not a number"); }
148 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"); }
149 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "cScale" ); if( cJSON_IsNumber( value ) ){ fluid -> cScale = value -> valuedouble ; } else { n_log( LOG_ERR , "cScale is not a number"); }
150 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "threadedProcessing" ); if( cJSON_IsNumber( value ) ){ (*threaded) = value -> valueint ; } else { n_log( LOG_ERR , "threadedProcessing is not a number"); }
151
152 cJSON_Delete(monitor_json);
153 free_nstr( &data ); */
154
155 destroy_config_file(&config);
156
157 return TRUE;
158}
double drawFPS
Definition ex_fluid.c:62
bool fullscreen
Definition ex_fluid.c:73
char * bgmusic
Definition ex_fluid.c:74
double logicFPS
Definition ex_fluid.c:63
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
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
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:89
#define LOG_ERR
error conditions
Definition n_log.h:76
#define LOG_INFO
informational
Definition n_log.h:82
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
structure of a fluid
Definition n_fluids.h:70
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.