[Add] Some error checking in nebulae subsystem to avoid infinite loop.

This commit is contained in:
Allanis 2013-08-10 17:36:08 +01:00
parent 430fe0bb1f
commit 51db82aa25
3 changed files with 28 additions and 13 deletions

View File

@ -160,8 +160,12 @@ int main(int argc, char** argv) {
if(ai_init()) if(ai_init())
WARN("Error initializing AI"); WARN("Error initializing AI");
/* Misc openGL init stuff. */ /* Misc graphics init stuff. */
nebu_init(); /* Init the nebulae. */ if(nebu_init() != 0) { /* Init the nebulae. */
/* An error happened. */
ERR("Unable to init the Nebulae subsystem!");
/* Weirdness will accur. */
}
gl_fontInit(NULL, NULL, FONT_SIZE); /* Init default font size. */ gl_fontInit(NULL, NULL, FONT_SIZE); /* Init default font size. */
gl_fontInit(&gl_smallFont, NULL, FONT_SIZE_SMALL); /* Small font. */ gl_fontInit(&gl_smallFont, NULL, FONT_SIZE_SMALL); /* Small font. */
gui_init(); /* Init the GUI crap. */ gui_init(); /* Init the GUI crap. */

View File

@ -51,18 +51,19 @@ static int nebu_npuffs = 0;
static int nebu_checkCompat(const char* file); static int nebu_checkCompat(const char* file);
static void nebu_loadTexture(SDL_Surface* sur, int w, int h, GLuint tex); static void nebu_loadTexture(SDL_Surface* sur, int w, int h, GLuint tex);
static void nebu_generate(void); static int nebu_generate(void);
static void nebu_generatePuffs(void); static void nebu_generatePuffs(void);
static void saveNebulae(float* map, const uint32_t w, const uint32_t h, static int saveNebulae(float* map, const uint32_t w, const uint32_t h,
const char* file); const char* file);
static SDL_Surface* loadNebulae(const char* file); static SDL_Surface* loadNebulae(const char* file);
static SDL_Surface* nebu_surfaceFromNebulaeMap(float* map, const int w, const int h); static SDL_Surface* nebu_surfaceFromNebulaeMap(float* map, const int w, const int h);
/* Initialize the nebulae. */ /* Initialize the nebulae. */
void nebu_init(void) { int nebu_init(void) {
int i; int i;
char nebu_file[PATH_MAX]; char nebu_file[PATH_MAX];
SDL_Surface* nebu_sur; SDL_Surface* nebu_sur;
int ret;
/* Special code to regenerate the nebulae. */ /* Special code to regenerate the nebulae. */
if((nebu_w == -9) && (nebu_h == -9)) if((nebu_w == -9) && (nebu_h == -9))
@ -85,9 +86,11 @@ void nebu_init(void) {
LOG("No nebulae found, generating (this may take a while)."); LOG("No nebulae found, generating (this may take a while).");
/* So we generate and reload. */ /* So we generate and reload. */
nebu_generate(); ret = nebu_generate();
nebu_init(); if(ret != 0) /* An error has happened - break recursivity. */
return; return ret;
return nebu_init();
} }
/* Load the file. */ /* Load the file. */
@ -101,6 +104,7 @@ void nebu_init(void) {
} }
DEBUG("Loaded %d Nebulae Layers", NEBULAE_Z); DEBUG("Loaded %d Nebulae Layers", NEBULAE_Z);
return 0;
} }
/* Load sur into tex, check for expected size of w and h. */ /* Load sur into tex, check for expected size of w and h. */
@ -412,11 +416,12 @@ void nebu_forceGenerate(void) {
} }
/* Generate the nebulae. */ /* Generate the nebulae. */
static void nebu_generate(void) { static int nebu_generate(void) {
int i; int i;
float* nebu; float* nebu;
char nebu_file[PATH_MAX]; char nebu_file[PATH_MAX];
int w, h; int w, h;
int ret;
w = SCREEN_W; w = SCREEN_W;
h = SCREEN_H; h = SCREEN_H;
@ -428,11 +433,13 @@ static void nebu_generate(void) {
/* Save each nebulae as an image. */ /* Save each nebulae as an image. */
for(i = 0; i < NEBULAE_Z; i++) { for(i = 0; i < NEBULAE_Z; i++) {
snprintf(nebu_file, PATH_MAX, NEBULAE_PATH_BG, w, h, i); snprintf(nebu_file, PATH_MAX, NEBULAE_PATH_BG, w, h, i);
saveNebulae(&nebu[i*w*h], w, h, nebu_file); ret = saveNebulae(&nebu[i*w*h], w, h, nebu_file);
if(ret != 0) break; /* An error has happened. */
} }
/* Cleanup. */ /* Cleanup. */
free(nebu); free(nebu);
return ret;
} }
/* Generate the nebulae puffs. */ /* Generate the nebulae puffs. */
@ -463,21 +470,25 @@ static int nebu_checkCompat(const char* file) {
} }
/* Save a nebulae. */ /* Save a nebulae. */
static void saveNebulae(float* map, const uint32_t w, const uint32_t h, static int saveNebulae(float* map, const uint32_t w, const uint32_t h,
const char* file) { const char* file) {
char file_path[PATH_MAX]; char file_path[PATH_MAX];
SDL_Surface* sur; SDL_Surface* sur;
int ret;
/* Fix surface. */ /* Fix surface. */
sur = nebu_surfaceFromNebulaeMap(map, w, h); sur = nebu_surfaceFromNebulaeMap(map, w, h);
/* Save. */ /* Save. */
ret = 0;
snprintf(file_path, PATH_MAX, "%s%s", lfile_basePath(), file); snprintf(file_path, PATH_MAX, "%s%s", lfile_basePath(), file);
SDL_savePNG(sur, file_path); ret = SDL_savePNG(sur, file_path);
/* Cleanup. */ /* Cleanup. */
SDL_FreeSurface(sur); SDL_FreeSurface(sur);
return ret;
} }
/* Load the nebulae from file. */ /* Load the nebulae from file. */

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
/* Init/Exit. */ /* Init/Exit. */
void nebu_init(void); int nebu_init(void);
void nebu_exit(void); void nebu_exit(void);
/* Render. */ /* Render. */