[Add] Nebulae render function.

This commit is contained in:
Allanis 2013-07-14 15:09:39 +01:00
parent 02e136520e
commit fefc7e793c
2 changed files with 53 additions and 5 deletions

View File

@ -33,8 +33,9 @@ static void saveNebulae(float* map, const uint32_t w, const uint32_t h,
const char* file);
static unsigned char* loadNebulae(const char* file, int* w, int* h);
/* Initialize the nebulae. */
void nebu_init(void) {
int i;
int i, y;
char nebu_file[PATH_MAX];
unsigned char* nebu_padded;
int w, h;
@ -64,7 +65,14 @@ void nebu_init(void) {
/* Load the file. */
nebu_data = loadNebulae(nebu_file, &w, &h);
memcpy(nebu_padded, nebu_data, w*h);
for(y = 0; y < nebu_h; y++) { /* Copy lines over. */
/* nebu_padded = [ nebu_data 0000000000000 ] */
memmove(&nebu_padded[y*nebu_pw], &nebu_data[y*nebu_w], nebu_w);
memset(&nebu_padded[y*nebu_pw+nebu_w], 0, nebu_pw-nebu_w); /* Pad the end. */
}
/* End it with 0's. */
memset(&nebu_padded[nebu_h*nebu_pw+nebu_w], 0,
nebu_ph*nebu_w - nebu_h*nebu_pw);
/* Load the textures. */
glBindTexture(GL_TEXTURE_2D, nebu_textures[i]);
@ -77,6 +85,8 @@ void nebu_init(void) {
free(nebu_data); /* No longer need the data. */
}
free(nebu_padded);
DEBUG("Loaded %d Nebulae Layers", NEBULAE_Z);
}
/* Clean up the nebu subsystem. */
@ -84,6 +94,39 @@ void nebu_exit(void) {
glDeleteTextures(NEBULAE_Z, nebu_textures);
}
/* Render the nebulae. */
void nebu_render(void) {
int n;
double tw, th;
n = 0;
tw = nebu_w / nebu_pw;
th = nebu_h / nebu_ph;
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, nebu_textures[n]);
glColor4d(1., 1., 1., 1.);
glBegin(GL_QUADS);
glTexCoord2d(0., 0.);
glVertex2d(-SCREEN_W/2., -SCREEN_H/2.);
glTexCoord2d(tw, th);
glVertex2d(SCREEN_W/2., -SCREEN_H/2.);
glTexCoord2d(tw, th);
glVertex2d(SCREEN_W/2., SCREEN_H/2.);
glTexCoord2d(0., th);
glVertex2d(-SCREEN_W/2., SCREEN_H/2.);
glEnd();
glDisable(GL_TEXTURE_2D);
/* Did anything fail? */
gl_checkErr();
}
/* Force generation of new nebulae. */
void nebu_generate(const int w, const int h) {
int i;
@ -130,8 +173,9 @@ static void saveNebulae(float* map, const uint32_t w, const uint32_t h,
snprintf(buf, NEBU_FORMAT_HEADER, "LEPHISTO NEBU v" NEBU_VERSION);
cur = 16;
memcpy(&buf[cur], &w, 4);
cur += 4;
memcpy(&buf[cur], &h, 4);
cur += 8;
cur += 4;
/* The Body. */
for(y = 0; y < (int)h; y++)
@ -178,8 +222,9 @@ static unsigned char* loadNebulae(const char* file, int* w, int* h) {
}
int fd;
int cur;
snprintf(file_path, PATH_MAX, "%s%s", lfile_basePath(), file);
fd = open(file, O_RDONLY);
fd = open(file_path, O_RDONLY);
if(fd < 0) {
ERR("Unable to open file %s: %s", file_path, strerror(errno));
return NULL;
@ -188,7 +233,9 @@ static unsigned char* loadNebulae(const char* file, int* w, int* h) {
READ(&tw, 4);
READ(&th, 4);
buf = malloc(tw*th);
READ(buf, tw*th);
cur = 0;
while((len = read(fd, &buf[cur], tw*th - cur)) != 0)
cur += len;
#else
#error "Needs implementation."
#endif

View File

@ -6,5 +6,6 @@ void nebu_init(void);
void nebu_exit(void);
void nebu_render(void);
void nebu_generate(const int w, const int h);