diff --git a/src/def.h b/src/def.h index 3b93acc..d027c8b 100644 --- a/src/def.h +++ b/src/def.h @@ -6,7 +6,5 @@ #define ABS(X) ((X<0)?-X:X) #define FABS(X) ((X<0.)?-X:X) -typedef float FP; - #define DATA "data" diff --git a/src/main.c b/src/main.c index b74f273..4d574ab 100644 --- a/src/main.c +++ b/src/main.c @@ -172,7 +172,7 @@ int main(int argc, char** argv) { // -- Think (ai). // -- Solid. static void update_all(void) { - FP dt = (FP)(SDL_GetTicks() - time) / 1000.0; + double dt = (double)(SDL_GetTicks() - time) / 1000.0; time = SDL_GetTicks(); glClear(GL_COLOR_BUFFER_BIT); diff --git a/src/opengl.c b/src/opengl.c index 0b77408..e5a99d2 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -162,15 +162,15 @@ gl_texture* gl_loadImage(SDL_Surface* surface) { // Set up the texture defaults. gl_texture* texture = MALLOC_L(gl_texture); - texture->w = (FP)surface->w; - texture->h = (FP)surface->h; + texture->w = (double)surface->w; + texture->h = (double)surface->h; texture->sx = 1.; texture->sy = 1.; texture->texture = gl_loadSurface(surface, &rw, &rh); - texture->rw = (FP)rw; - texture->rh = (FP)rh; + texture->rw = (double)rw; + texture->rh = (double)rh; texture->sw = texture->w; texture->sh = texture->h; @@ -207,8 +207,8 @@ gl_texture* gl_newSprite(const char* path, const int sx, const int sy) { gl_texture* texture; if((texture = gl_newImage(path)) == NULL) return NULL; - texture->sx = (FP)sx; - texture->sy = (FP)sy; + texture->sx = (double)sx; + texture->sy = (double)sy; texture->sw = texture->w/texture->sx; texture->sh = texture->h/texture->sy; return texture; @@ -232,26 +232,27 @@ void gl_blitSprite(const gl_texture* sprite, const Vec2* pos, const int sx, cons return; glMatrixMode(GL_TEXTURE); glPushMatrix(); - glTranslatef(sprite->sw * (FP)(sx)/sprite->rw, - sprite->sh*(sprite->sy-(FP)sy-1)/sprite->rh, 0.); + glTranslated(sprite->sw * (double)(sx)/sprite->rw, + sprite->sh*(sprite->sy-(double)sy-1)/sprite->rh, 0.); glMatrixMode(GL_PROJECTION); glPushMatrix(); // Projection translation matrix. - glTranslatef(pos->x - gl_camera->x - sprite->sw/2., + glTranslated(pos->x - gl_camera->x - sprite->sw/2., pos->y - gl_camera->y - sprite->sh/2., 0.); - glScalef((FP)gl_screen.w/SCREEN_W, (FP)gl_screen.h/SCREEN_H, 0.); + glScalef((double)gl_screen.w/SCREEN_W, (double)gl_screen.h/SCREEN_H, 0.); // Actual blitting.... glBindTexture(GL_TEXTURE_2D, sprite->texture); + glColor4ub(255, 255, 255, 255); glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0., 0.); - glVertex2f(0., 0.); - glTexCoord2f(sprite->sw/sprite->rw, 0.); - glVertex2f(sprite->sw, 0.); - glTexCoord2f(0., sprite->sh/sprite->rh); - glVertex2f(0., sprite->sh); - glTexCoord2f(sprite->sw/sprite->rw, sprite->sh/sprite->rh); - glVertex2f(sprite->sw, sprite->sh); + glTexCoord2d(0., 0.); + glVertex2d(0., 0.); + glTexCoord2d(sprite->sw/sprite->rw, 0.); + glVertex2d(sprite->sw, 0.); + glTexCoord2d(0., sprite->sh/sprite->rh); + glVertex2d(0., sprite->sh); + glTexCoord2d(sprite->sw/sprite->rw, sprite->sh/sprite->rh); + glVertex2d(sprite->sw, sprite->sh); glEnd(); glPopMatrix(); // Projection translation matrix. @@ -264,20 +265,21 @@ void gl_blitSprite(const gl_texture* sprite, const Vec2* pos, const int sx, cons void gl_blitStatic(const gl_texture* texture, const Vec2* pos) { glMatrixMode(GL_PROJECTION); glPushMatrix(); // Set up translation matrix. - glTranslatef(pos->x - (FP)gl_screen.w/2., pos->y - (FP)gl_screen.h/2., 0); - glScalef((FP)gl_screen.w/SCREEN_W, (FP)gl_screen.h/SCREEN_H, 0.); + glTranslated(pos->x - (double)gl_screen.w/2., pos->y - (double)gl_screen.h/2., 0); + glScaled((double)gl_screen.w/SCREEN_W, (double)gl_screen.h/SCREEN_H, 0.); // Actual blitting.. glBindTexture(GL_TEXTURE_2D, texture->texture); + glColor4ub(255, 255, 255, 255); glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0., 0.); - glVertex2f(0., 0.); - glTexCoord2f(texture->w/texture->rw, 0.); - glVertex2f(texture->w, 0.); - glTexCoord2f(0., texture->h/texture->rh); - glVertex2f(0., texture->h); - glTexCoord2f(texture->w/texture->rw, texture->h/texture->rh); - glVertex2f(texture->w, texture->h); + glTexCoord2d(0., 0.); + glVertex2d(0., 0.); + glTexCoord2d(texture->w/texture->rw, 0.); + glVertex2d(texture->w, 0.); + glTexCoord2d(0., texture->h/texture->rh); + glVertex2d(0., texture->h); + glTexCoord2d(texture->w/texture->rw, texture->h/texture->rh); + glVertex2d(texture->w, texture->h); glEnd(); glPopMatrix(); // Pop the translation matrix. @@ -309,7 +311,8 @@ void gl_print(const gl_font* ft_font, Vec2* pos, const char* fmt, ...) { glMatrixMode(GL_PROJECTION); //for(i = 0; i < strlen(text); i++) { glPushMatrix(); - glTranslatef(pos->x - (FP)gl_screen.w/2., pos->y - (FP)gl_screen.h/2., 0); + glTranslated(pos->x - (double)gl_screen.w/2., pos->y - (double)gl_screen.h/2., 0); + glColor4ub(255, 255, 255, 255); glCallLists(strlen(text), GL_UNSIGNED_BYTE, &text); glPopMatrix(); //} @@ -366,29 +369,29 @@ static void gl_fontMakeDList(FT_Face face, char ch, GLuint list_base, GLuint* te glPushMatrix(); // Corrects a spacing flaw between letters. - glTranslatef(bitmap_glyph->left, 0,0); + glTranslated(bitmap_glyph->left, 0,0); // Downwards correction for letters like g or y. - glTranslatef(0, bitmap_glyph->top-bitmap.rows,0); + glTranslated(0, bitmap_glyph->top-bitmap.rows,0); // Take the opengl POT wrapping into account. - FP x = (FP)bitmap.width/(FP)w; - FP y = (FP)bitmap.rows/(FP)h; + double x = (double)bitmap.width/(double)w; + double y = (double)bitmap.rows/(double)h; // Draw the texture mapped quad. glBegin(GL_QUADS); glTexCoord2d(0, 0); - glVertex2f(0, bitmap.rows); + glVertex2d(0, bitmap.rows); glTexCoord2d(0, y); - glVertex2f(0, 0); + glVertex2d(0, 0); glTexCoord2d(x, y); - glVertex2f(bitmap.width, 0); + glVertex2d(bitmap.width, 0); glTexCoord2d(x, 0); - glVertex2f(bitmap.width, bitmap.rows); + glVertex2d(bitmap.width, bitmap.rows); glEnd(); glPopMatrix(); - glTranslatef(face->glyph->advance.x >> 6, 0,0); + glTranslated(face->glyph->advance.x >> 6, 0,0); // End of the display list. glEndList(); diff --git a/src/opengl.h b/src/opengl.h index 7141a01..e845363 100644 --- a/src/opengl.h +++ b/src/opengl.h @@ -32,10 +32,10 @@ extern gl_info gl_screen; // Local structure set with gl_init etc. // Spritesheet info. typedef struct { - FP w, h; // Real size of the image (excluding POT buffer. - FP rw, rh; // Size of POT surface. - FP sx, sy; // Number of sprites on x and y axes. - FP sw, sh; // Size of each sprite. + double w, h; // Real size of the image (excluding POT buffer. + double rw, rh; // Size of POT surface. + double sx, sy; // Number of sprites on x and y axes. + double sw, sh; // Size of each sprite. GLuint texture; // The opengl texture itself. } gl_texture; diff --git a/src/physics.c b/src/physics.c index d5cf44f..a32fa3c 100644 --- a/src/physics.c +++ b/src/physics.c @@ -23,7 +23,7 @@ // error, so watch out with big values for dt. // ======================================================== #if 0 -static void simple_update(Solid* obj, const FP dt) { +static void simple_update(Solid* obj, const double dt) { // Make sure angle doesn't flip. obj->dir += obj->dir_vel/360.0*dt; if(obj->dir > 2*M_PI) obj->dir -= 2*M_PI; @@ -61,13 +61,13 @@ static void simple_update(Solid* obj, const FP dt) { // ======================================================== #define RK4_N 4 -static void rk4_update(Solid* obj, const FP dt) { +static void rk4_update(Solid* obj, const double dt) { // Make sure angle doesn't flip. obj->dir += obj->dir_vel/360.0*dt; if(obj->dir > 2*M_PI) obj->dir -= 2*M_PI; if(obj->dir < 0.0) obj->dir += 2*M_PI; - FP h = dt / RK4_N; // Step. + double h = dt / RK4_N; // Step. if(obj->force) { // Force applied on object. int i; @@ -105,7 +105,7 @@ static void rk4_update(Solid* obj, const FP dt) { } // Initialize a new solid. -void solid_init(Solid* dest, const FP mass, const Vec2* vel, const Vec2* pos) { +void solid_init(Solid* dest, const double mass, const Vec2* vel, const Vec2* pos) { dest->mass = mass; dest->force = 0; @@ -129,7 +129,7 @@ void solid_init(Solid* dest, const FP mass, const Vec2* vel, const Vec2* pos) { } // Create a new solid. -Solid* solid_create(const FP mass, const Vec2* vel, const Vec2* pos) { +Solid* solid_create(const double mass, const Vec2* vel, const Vec2* pos) { Solid* dyn = MALLOC_L(Solid); assert(dyn != NULL); solid_init(dyn, mass, vel, pos); diff --git a/src/physics.h b/src/physics.h index 032ee4e..fab782a 100644 --- a/src/physics.h +++ b/src/physics.h @@ -3,19 +3,19 @@ // Base of 2D vectors. typedef struct { - FP x, y; // Basic 2D vector components. + double x, y; // Basic 2D vector components. } Vec2; // Describe any solid in 2D space. struct Solid { - FP mass, force, dir, dir_vel; // Properties. + double mass, force, dir, dir_vel; // Properties. Vec2 vel, pos; // Position/velocity vectors. - void(*update)(struct Solid*, const FP); // Update method. + void(*update)(struct Solid*, const double); // Update method. }; typedef struct Solid Solid; -void solid_init(Solid* dest, const FP mass, const Vec2* vel, const Vec2* pos); -Solid* solid_create(const FP mass, const Vec2* vel, const Vec2* pos); +void solid_init(Solid* dest, const double mass, const Vec2* vel, const Vec2* pos); +Solid* solid_create(const double mass, const Vec2* vel, const Vec2* pos); void solid_free(Solid* src); diff --git a/src/pilot.c b/src/pilot.c index da2fd53..ec63763 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -14,9 +14,9 @@ static unsigned int pilot_id = 0; static Pilot** pilot_stack; static int pilots = 0; -extern void player_think(Pilot* pilot, const FP dt); // Player.c +extern void player_think(Pilot* pilot, const double dt); // Player.c extern void ai_think(Pilot* pilot); // Ai.c -static void pilot_update(Pilot* pilot, const FP dt); +static void pilot_update(Pilot* pilot, const double dt); static void pilot_render(Pilot* pilot); // Pull a pilot out of the pilot_stack based on id. @@ -47,7 +47,7 @@ static void pilot_render(Pilot* pilot) { } // Update the pilot. -static void pilot_update(Pilot* pilot, const FP dt) { +static void pilot_update(Pilot* pilot, const double dt) { if(pilot->solid->dir > 2*M_PI) pilot->solid->dir -= 2*M_PI; if(pilot->solid->dir < 0.0) pilot->solid->dir += 2*M_PI; @@ -118,7 +118,7 @@ void pilots_free(void) { } // Update all pilots. -void pilots_update(FP dt) { +void pilots_update(double dt) { int i; for(i = pilots-1; i >= 0; i--) { if(pilot_stack[i]->think != NULL) diff --git a/src/pilot.h b/src/pilot.h index 8794e1d..f8d3cb4 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -30,9 +30,9 @@ struct Pilot { Solid* solid; // Associated solid (physics). // Current health. - FP armor, shield, energy; + double armor, shield, energy; - void (*update)(struct Pilot*, const FP); // Update the pilot. + void (*update)(struct Pilot*, const double); // Update the pilot. unsigned int properties; // Used for AI etc. @@ -56,5 +56,5 @@ unsigned int pilot_create(Ship* ship, char* name, const Vec2* vel, void pilots_free(void); // Update. -void pilots_update(FP dt); +void pilots_update(double dt); diff --git a/src/player.c b/src/player.c index d3fb603..550b758 100644 --- a/src/player.c +++ b/src/player.c @@ -6,14 +6,14 @@ Pilot* player = NULL; static unsigned int player_flags = PLAYER_FLAG_NULL; -static FP player_turn = 0.; -static FP player_acc = 0.; +static double player_turn = 0.; +static double player_acc = 0.; // To be used in pilot.c -void player_think(Pilot* player, const FP dt) { +void player_think(Pilot* player, const double dt) { player->solid->dir_vel = 0.; if(player_turn) - player->solid->dir_vel -= player->ship->turn*player_turn/(FP)(1<<15); + player->solid->dir_vel -= player->ship->turn*player_turn/(double)(1<<15); #if 0 if(player_isFlag(PLAYER_FLAG_MOV_LEFT)) player->solid->dir_vel += player->ship->turn; @@ -21,7 +21,7 @@ void player_think(Pilot* player, const FP dt) { player->solid->dir_vel -= player->ship->turn; #endif - player->solid->force = player->ship->thrust*player_acc/(FP)(1<<15); + player->solid->force = player->ship->thrust*player_acc/(double)(1<<15); } // Flag manipulationz. @@ -45,11 +45,11 @@ void player_rmFlag(unsigned int flag) { static void handle_joyaxis(int axis, int value) { switch(axis) { case 0: - player_turn = (FP)value; + player_turn = (double)value; break; case 1: if(value <= 0) - player_acc = (FP)-value; + player_acc = (double)-value; break; } } @@ -57,7 +57,7 @@ static void handle_joyaxis(int axis, int value) { static void handle_joydown(int button) { switch(button) { case 0: - player_acc += (FP)(1<<15); + player_acc += (double)(1<<15); break; case 1: break; @@ -67,7 +67,7 @@ static void handle_joydown(int button) { static void handle_joyup(int button) { switch(button) { case 0: - player_acc -=(FP)(1<<15); + player_acc -=(double)(1<<15); break; case 1: break; @@ -84,15 +84,15 @@ static void handle_keydown(SDLKey key) { break; case SDLK_LEFT: case SDLK_a: - player_turn -= (FP)(1<<15); + player_turn -= (double)(1<<15); break; case SDLK_RIGHT: case SDLK_d: - player_turn += (FP)(1<<15); + player_turn += (double)(1<<15); break; case SDLK_UP: case SDLK_w: - player_acc += (FP)(1<<15); + player_acc += (double)(1<<15); break; default: break; @@ -103,15 +103,15 @@ static void handle_keyup(SDLKey key) { switch(key) { case SDLK_LEFT: case SDLK_a: - player_turn += (FP)(1<<15); + player_turn += (double)(1<<15); break; case SDLK_RIGHT: case SDLK_d: - player_turn -= (FP)(1<<15); + player_turn -= (double)(1<<15); break; case SDLK_UP: case SDLK_w: - player_acc -= (FP)(1<<15); + player_acc -= (double)(1<<15); //break; default: break; diff --git a/src/ship.c b/src/ship.c index 056dba7..f2dc9e4 100644 --- a/src/ship.c +++ b/src/ship.c @@ -69,17 +69,17 @@ Ship* ship_parse(xmlNodePtr node) { cur = node->children; while((cur = cur->next)) { if(strcmp((char*)cur->name, "armor")==0) - tmp->armor = (FP)atoi((char*)cur->children->content); + tmp->armor = (double)atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "shield")==0) - tmp->shield = (FP)atoi((char*)cur->children->content); + tmp->shield = (double)atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "energy")==0) - tmp->energy = (FP)atoi((char*)cur->children->content); + tmp->energy = (double)atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "armor_regen")==0) - tmp->armor_regen = (FP)(atoi((char*)cur->children->content))/60.0; + tmp->armor_regen = (double)(atoi((char*)cur->children->content))/60.0; else if(strcmp((char*)cur->name, "shield_regen")==0) - tmp->shield_regen = (FP)(atoi((char*)cur->children->content))/60.0; + tmp->shield_regen = (double)(atoi((char*)cur->children->content))/60.0; else if(strcmp((char*)cur->name, "energy_regen")==0) - tmp->energy_regen = (FP)(atoi((char*)cur->children->content))/60.0; + tmp->energy_regen = (double)(atoi((char*)cur->children->content))/60.0; } } else if(strcmp((char*)node->name, "characteristics")==0) { @@ -88,7 +88,7 @@ Ship* ship_parse(xmlNodePtr node) { if(strcmp((char*)cur->name, "crew")==0) tmp->crew = atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "mass")==0) - tmp->mass = (FP)atoi((char*)cur->children->content); + tmp->mass = (double)atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "cap_weapon")==0) tmp->cap_weapon = atoi((char*)cur->children->content); else if(strcmp((char*)cur->name, "cap_cargo")==0) diff --git a/src/ship.h b/src/ship.h index ce1600d..c6a7119 100644 --- a/src/ship.h +++ b/src/ship.h @@ -10,7 +10,7 @@ typedef struct { ship_class class; // Ship class. // Movement. - FP thrust, turn, speed; + double thrust, turn, speed; // Graphics. gl_texture* gfx_ship, *gfx_target; @@ -20,9 +20,9 @@ typedef struct { int mass; // Health. - FP armor, armor_regen; - FP shield, shield_regen; - FP energy, energy_regen; + double armor, armor_regen; + double shield, shield_regen; + double energy, energy_regen; // Capacity. int cap_cargo, cap_weapon; diff --git a/src/space.c b/src/space.c index e7f70ec..d1ef5ae 100644 --- a/src/space.c +++ b/src/space.c @@ -5,11 +5,20 @@ #include "pilot.h" #include "space.h" -#define STAR_LAYERS 3 +#define STAR_LAYERS 1 static gl_texture* starBG[STAR_LAYERS]; static Vec2 starPos[STAR_LAYERS]; +#define STAR_BUF 100 // Area to leave around screen. +typedef struct { + Vec2 pos; + Uint8 brightness; +} Star; + +Star* stars; +int nstars; + static gl_texture* starBG_create(const int density); static void put_pixel(SDL_Surface* surface, const int x, const int y, const Uint8 R, const Uint8 G, const Uint8 B, const Uint8 A); @@ -77,7 +86,7 @@ static gl_texture* starBG_create(const int density) { SDL_LockSurface(surface); - d = (int)((FP)(density)*(FP)(gl_screen.w)*(FP)(gl_screen.h)/1000./1000.); + d = (int)((double)(density)*(double)(gl_screen.w)*(double)(gl_screen.h)/1000./1000.); for(i = 0; i < d; i++) put_pixel(surface, RNG(0,w-1), RNG(0,h-1), 255, 255, 255, RNG(50, 255)); @@ -88,15 +97,33 @@ static gl_texture* starBG_create(const int density) { void space_init(void) { int i; + nstars = 2000; + stars = malloc(sizeof(Star)*nstars); + for(i = 0; i < nstars; i++) { + stars[i].brightness = RNG(50, 255); + stars[i].pos.x = RNG(STAR_BUF, gl_screen.w + STAR_BUF); + stars[i].pos.y = RNG(-STAR_BUF, gl_screen.h + STAR_BUF); + } +#if 0 for(i = 0; i < STAR_LAYERS; i++) { starBG[i] = starBG_create(1000); starPos[i].x = 0.; starPos[i].y = 0.; } +#endif } -void space_render(FP dt) { +void space_render(double dt) { int i; + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glBegin(GL_POINTS); + glColor4ub(255, 255, 255, stars[i].brightness); + glVertex2d(stars[i].pos.x, stars[i].pos.y); + glEnd(); + glPopMatrix(); + +#if 0 Vec2 tmp; FP f; @@ -146,11 +173,15 @@ void space_render(FP dt) { else if(tmp.x != starPos[i].x || tmp.y != starPos[i].y) gl_blitStatic(starBG[i], &tmp); } +#endif } void space_exit(void) { + free(stars); +#if 0 int i; for(i = 0; i < STAR_LAYERS; i++) gl_freeTexture(starBG[i]); +#endif } diff --git a/src/space.h b/src/space.h index d01f740..1314f72 100644 --- a/src/space.h +++ b/src/space.h @@ -4,5 +4,5 @@ void space_init(void); void space_exit(void); -void space_render(FP dt); +void space_render(double dt);