From 246bdff2c8f03d31b927c66d57d9306d20925a10 Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 1 Feb 2013 22:20:57 +0000 Subject: [PATCH] [Add] Stars!!!! --- src/main.c | 6 ++- src/rng.h | 2 +- src/space.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/space.h | 8 +++ 4 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 src/space.c create mode 100644 src/space.h diff --git a/src/main.c b/src/main.c index 06551b6..be9f5b6 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ #include "pilot.h" #include "player.h" #include "joystick.h" +#include "space.h" #include "rng.h" #include "pilot.h" @@ -85,6 +86,7 @@ int main(int argc, char** argv) { unsigned int player_id; player_id = pilot_create(get_ship("Ship"), "Player", NULL, NULL, PILOT_PLAYER); gl_bindCamera(&get_pilot(player_id)->solid->pos); + space_init(); pilot_create(get_ship("Miss. Test"), NULL, NULL, NULL, 0); @@ -99,6 +101,7 @@ int main(int argc, char** argv) { } update_all(); } + space_exit(); // Unload data. pilots_free(); ships_free(); @@ -116,7 +119,8 @@ static void update_all(void) { FP dt = (FP)(SDL_GetTicks() - time) / 1000.0; time = SDL_GetTicks(); glClear(GL_COLOR_BUFFER_BIT); - + + space_render(dt); pilots_update(dt); SDL_GL_SwapBuffers(); diff --git a/src/rng.h b/src/rng.h index 308350f..cda598c 100644 --- a/src/rng.h +++ b/src/rng.h @@ -1,7 +1,7 @@ #pragma once #include -#define RNG(L,H) rand()%(H-L_1)+L +#define RNG(L,H) rand()%(H-L+1)+L void rng_init(void); diff --git a/src/space.c b/src/space.c new file mode 100644 index 0000000..7351a66 --- /dev/null +++ b/src/space.c @@ -0,0 +1,147 @@ +#include "log.h" +#include "physics.h" +#include "opengl.h" +#include "rng.h" +#include "pilot.h" +#include "space.h" + +#define STAR_LAYERS 3 + +static gl_texture* starBG[STAR_LAYERS]; +static Vec2 starPos[STAR_LAYERS]; + +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); + +// Modify the pixel at (x,y) of the surface to be of color RGB. +static void put_pixel(SDL_Surface* surface, const int x, const int y, const Uint8 R, const Uint8 G, const Uint8 B) { + Uint32 color = SDL_MapRGB(surface->format, R, G, B); + + Uint8* bufp8; + Uint16* bufp16; + Uint32* bufp32; + + switch(surface->format->BytesPerPixel) { + case 1: // 8bpp. + bufp8 = (Uint8)surface->pixels + y*surface->pitch + x; + *bufp8 = color; + break; + case 2: // 15 or 16bpp. + bufp16 = (Uint16*)surface->pixels + y*surface->pitch/2 + x; + *bufp16 = color; + break; + case 3: // 24bpp, Slow as hell. + bufp8 = (Uint8*)surface->pixels + y*surface->pitch + x; + *(bufp8 + surface->format->Rshift/8) = R; + *(bufp8 + surface->format->Gshift/8) = G; + *(bufp8 + surface->format->Bshift/8) = B; + break; + case 4: // 32bpp. + bufp32 = (Uint32*)surface->pixels + y*surface->pitch/4 + x; + *bufp32 = color; + break; + } +} + +// Create a background of stars to use. +// Background consists of four tiles together. +static gl_texture* starBG_create(const int density) { + SDL_Surface* surface; + int w, h; + int i, b, d, x, y; + + w = (int)((float)gl_screen.w * 1.5); + if((w & (w-1)) != 0) { + w = 1; + while(h < (int)((float)gl_screen.h*1.5)) + h <<= 1; + } + h = (int)((float)gl_screen.h * 1.5); + if((h & (h-1)) != 0) { + h = 1; + while(h < (int)((float)gl_screen.h * 1.5)) + h <<= 1; + } + + surface = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, w, h, + SDL_GetVideoSurface()->format->BytesPerPixel*8, RGBMASK); + + if(surface == NULL) { + WARN("Unable to create RGB surface"); + return NULL; + } + + d = (int)((FP)(density)*(FP)(gl_screen.w)*(FP)(gl_screen.h)/1000./1000.); + for(i = 0; i < d; i++) { + b = RNG(50, 255); + x = RNG(0, w-1); + y = RNG(0, h-1); + put_pixel(surface, x, y, b, b, b); + } + + return gl_loadImage(surface); +} + +void space_init(void) { + int i; + for(i = 0; i < STAR_LAYERS; i++) { + starBG[i] = starBG_create(1000); + starPos[i].x = 0.; + starPos[i].y = 0.; + } +} + +void space_render(FP dt) { + int i; + Vec2 tmp = { .x = starPos[0].x }; + + for(i = 0; i < STAR_LAYERS; i++) { + // Movement. + starPos[i].x -= player->solid->vel.x / (FP)(2*i+4)*dt; + starPos[i].y -= player->solid->vel.y / (FP)(2*i+4)*dt; + + // Displaces x if reaches edge. + if(starPos[i].x > 0) + starPos[i].x -= starBG[i]->w; + else if(starPos[i].x < -starBG[i]->w) + starPos[i].x += starBG[i]->w; + + // Displaces y if reaches edge. + if(starPos[i].y > 0) + starPos[i].y -= starBG[i]->h; + else if(starPos[i].y < -starBG[i]->h) + starPos[i].y += starBG[i]->h; + + // Primary blit. + gl_blitStatic(starBG[i], &starPos[i]); + + tmp.x = starPos[i].x; + tmp.y = starPos[i].y; + + // TODO: This needs optimizing. + tmp.x -= starBG[i]->w; + gl_blitStatic(starBG[i], &tmp); + tmp.y += starBG[i]->h; + gl_blitStatic(starBG[i], &tmp); + tmp.x += starBG[i]->w; + gl_blitStatic(starBG[i], &tmp); + tmp.x += starBG[i]->w; + gl_blitStatic(starBG[i], &tmp); + tmp.y -= starBG[i]->h; + gl_blitStatic(starBG[i], &tmp); + tmp.y -= starBG[i]->h; + gl_blitStatic(starBG[i], &tmp); + tmp.x -= starBG[i]->w; + gl_blitStatic(starBG[i], &tmp); + tmp.x -= starBG[i]->w; + gl_blitStatic(starBG[i], &tmp); + } +} + +void space_exit(void) { + int i; + for(i = 0; i < STAR_LAYERS; i++) + gl_free(starBG[i]); +} + diff --git a/src/space.h b/src/space.h new file mode 100644 index 0000000..d01f740 --- /dev/null +++ b/src/space.h @@ -0,0 +1,8 @@ +#pragma once +#include "def.h" + +void space_init(void); +void space_exit(void); + +void space_render(FP dt); +