[Add] Stars!!!!

This commit is contained in:
Allanis 2013-02-01 22:20:57 +00:00
parent 223a6185fb
commit 246bdff2c8
4 changed files with 161 additions and 2 deletions

View File

@ -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();
@ -117,6 +120,7 @@ static void update_all(void) {
time = SDL_GetTicks();
glClear(GL_COLOR_BUFFER_BIT);
space_render(dt);
pilots_update(dt);
SDL_GL_SwapBuffers();

View File

@ -1,7 +1,7 @@
#pragma once
#include <stdlib.h>
#define RNG(L,H) rand()%(H-L_1)+L
#define RNG(L,H) rand()%(H-L+1)+L
void rng_init(void);

147
src/space.c Normal file
View File

@ -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]);
}

8
src/space.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include "def.h"
void space_init(void);
void space_exit(void);
void space_render(FP dt);