[Add] Moving stars.

[Change] optimized opengl.c a little.
This commit is contained in:
Allanis 2013-02-02 15:12:39 +00:00
parent b5a7ba8919
commit c84a11e4a7
5 changed files with 71 additions and 191 deletions

View File

@ -18,6 +18,7 @@
#include "ai.h"
#include "pilot.h"
#define WINDOW_CAPTION "Lephisto"
#define CONF_FILE "conf"
static gl_font fdefault;
@ -112,6 +113,9 @@ int main(int argc, char** argv) {
exit(EXIT_FAILURE);
}
// Window.
SDL_WM_SetCaption(WINDOW_CAPTION, NULL);
// Input.
if(indjoystick >= 0 || namjoystick != NULL) {
if(joystick_init())
@ -191,7 +195,7 @@ static void display_fps(const double dt) {
fps_dt += dt;
fps_cur += 1.;
if(fps_dt > 1.) {
fps = fps_cur;
fps = fps_cur / fps_dt;
fps_dt = fps_cur = 0.;
}
Vec2 pos = { .x = 10., .y = (double)(gl_screen.h-20) };

View File

@ -77,12 +77,15 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) {
SDL_Surface* tmp;
Uint32 saved_flags;
Uint8 saved_alpha;
int potw, poth;
// Make size power of two.
if(rw)*rw = pot(surface->w);
if(rh)*rh = pot(surface->h);
potw = pot(surface->w);
poth = pot(surface->h);
if(rw)*rw = potw;
if(rh)*rh = poth;
if(surface->w != *rw || surface->h != *rh) {
if(surface->w != potw || surface->h != poth) {
// Size isn't original.
SDL_Rect rtemp;
rtemp.x = rtemp.y = 0;
@ -97,7 +100,7 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) {
// Create the temp POT surface.
tmp = SDL_CreateRGBSurface(SDL_SRCCOLORKEY,
*rw, *rh, surface->format->BytesPerPixel*8, RGBMASK);
potw, poth, surface->format->BytesPerPixel*8, RGBMASK);
if(tmp == NULL) {
WARN("Unable to create POT surface %s", SDL_GetError());
return 0;
@ -118,7 +121,7 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) {
// Create the temp POT surface.
tmp = SDL_CreateRGBSurface(SDL_SRCCOLORKEY,
*rw, *rh, surface->format->BytesPerPixel*8, RGBMASK);
potw, poth, surface->format->BytesPerPixel*8, RGBMASK);
if(tmp == NULL) {
WARN("Unable to create POT surface %s", SDL_GetError());
return 0;
@ -230,6 +233,8 @@ void gl_blitSprite(const gl_texture* sprite, const Vec2* pos, const int sx, cons
if(fabs(pos->x - gl_camera->x) > gl_screen.w / 2 + sprite->sw / 2 ||
fabs(pos->y-gl_camera->y) > gl_screen.h / 2 + sprite->sh / 2)
return;
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glTranslated(sprite->sw * (double)(sx)/sprite->rw,
@ -243,8 +248,8 @@ void gl_blitSprite(const gl_texture* sprite, const Vec2* pos, const int sx, cons
// Actual blitting....
glBindTexture(GL_TEXTURE_2D, sprite->texture);
glColor4ub(255, 255, 255, 255);
glBegin(GL_TRIANGLE_STRIP);
glColor4d(1., 1., 1., 1.);
glTexCoord2d(0., 0.);
glVertex2d(0., 0.);
glTexCoord2d(sprite->sw/sprite->rw, 0.);
@ -259,10 +264,13 @@ void gl_blitSprite(const gl_texture* sprite, const Vec2* pos, const int sx, cons
glMatrixMode(GL_TEXTURE);
glPopMatrix(); // Sprite translation matrix.
glDisable(GL_TEXTURE_2D);
}
// Just straight out blit the thing at position.
void gl_blitStatic(const gl_texture* texture, const Vec2* pos) {
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glPushMatrix(); // Set up translation matrix.
glTranslated(pos->x - (double)gl_screen.w/2., pos->y - (double)gl_screen.h/2., 0);
@ -270,8 +278,8 @@ void gl_blitStatic(const gl_texture* texture, const Vec2* pos) {
// Actual blitting..
glBindTexture(GL_TEXTURE_2D, texture->texture);
glColor4ub(255, 255, 255, 255);
glBegin(GL_TRIANGLE_STRIP);
glColor4ub(1., 1., 1., 1.);
glTexCoord2d(0., 0.);
glVertex2d(0., 0.);
glTexCoord2d(texture->w/texture->rw, 0.);
@ -283,6 +291,8 @@ void gl_blitStatic(const gl_texture* texture, const Vec2* pos) {
glEnd();
glPopMatrix(); // Pop the translation matrix.
glDisable(GL_TEXTURE_2D);
}
// Bind our precious camera to a vector.
@ -291,14 +301,12 @@ void gl_bindCamera(const Vec2* pos) {
}
// Print text on screen! YES!!!! Just like printf! But different!
void gl_print(const gl_font* ft_font, Vec2* pos, const char* fmt, ...) {
void gl_print(const gl_font* ft_font, const Vec2* pos, const char* fmt, ...) {
//float h = ft_font->h / .63; // Slightly increases font size.
char text[256];
va_list ap;
//int i;
if(fmt == NULL)
*text = 0;
if(fmt == NULL) return;
else {
// convert the symbols to text.
va_start(ap, fmt);
@ -306,16 +314,20 @@ void gl_print(const gl_font* ft_font, Vec2* pos, const char* fmt, ...) {
va_end(ap);
}
glEnable(GL_TEXTURE_2D);
glListBase(ft_font->list_base);
glMatrixMode(GL_PROJECTION);
//for(i = 0; i < strlen(text); i++) {
glPushMatrix();
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();
//}
glPushMatrix(); // Translation matrix.
glTranslated(pos->x - (double)gl_screen.w/2., pos->y - (double)gl_screen.h/2., 0);
glColor4d(1., 1., 1., 1.);
glCallLists(strlen(text), GL_UNSIGNED_BYTE, &text);
glPopMatrix(); // Translation matrix.
glDisable(GL_TEXTURE_2D);
}
// ================
@ -364,30 +376,26 @@ static void gl_fontMakeDList(FT_Face face, char ch, GLuint list_base, GLuint* te
// Create the display lists.
glNewList(list_base+ch, GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, tex_base[(int)ch]);
// Corrects a spacing flaw between letters and
// downwards correction for letters like g or y.
glPushMatrix();
// Corrects a spacing flaw between letters.
glTranslated(bitmap_glyph->left, 0,0);
// Downwards correction for letters like g or y.
glTranslated(0, bitmap_glyph->top-bitmap.rows,0);
glTranslated(bitmap_glyph->left, bitmap_glyph->top-bitmap.rows, 0);
// Take the opengl POT wrapping into account.
double x = (double)bitmap.width/(double)w;
double y = (double)bitmap.rows/(double)h;
// Draw the texture mapped quad.
glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, tex_base[(int)ch]);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2d(0, 0);
glVertex2d(0, bitmap.rows);
glTexCoord2d(x, 0);
glVertex2d(bitmap.width, bitmap.rows);
glTexCoord2d(0, y);
glVertex2d(0, 0);
glTexCoord2d(x, y);
glVertex2d(bitmap.width, 0);
glTexCoord2d(x, 0);
glVertex2d(bitmap.width, bitmap.rows);
glEnd();
glPopMatrix();
@ -515,9 +523,11 @@ int gl_init(void) {
DEBUG("Renderer: %s", glGetString(GL_RENDERER));
// Some openGL options.
glClearColor(0., 0., 0., 0.);
glClearColor(0., 0., 0., 1.);
glDisable(GL_DEPTH_TEST); // Set for doing 2D shidazles.
glEnable(GL_TEXTURE_2D);
//glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING); // No lighting, it is done when rendered.
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-SCREEN_W /2, // Left edge.
@ -529,10 +539,9 @@ int gl_init(void) {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Alpha.
glEnable(GL_BLEND);
glPointSize(1.); // Default is 1.
glClear(GL_COLOR_BUFFER_BIT);
SDL_WM_SetCaption(WINDOW_CAPTION, NULL);
return 0;
}

View File

@ -17,8 +17,6 @@
#endif
#define RGBMASK RMASK,GMASK,BMASK,AMASK
#define WINDOW_CAPTION "Lephisto"
// Info about opengl screen.
typedef struct {
int w, h; // Window dimensions.
@ -60,7 +58,7 @@ void gl_freeTexture(gl_texture* texture);
void gl_blitSprite(const gl_texture* sprite, const Vec2* pos, const int sx, const int sy);
void gl_blitStatic(const gl_texture* texture, const Vec2* pos);
void gl_bindCamera(const Vec2* pos);
void gl_print(const gl_font* ft_font, Vec2* pos, const char* fmt, ...);
void gl_print(const gl_font* ft_font, const Vec2* pos, const char* fmt, ...);
// Initialize/cleanup.
int gl_init(void);

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);

View File

@ -1,3 +1,5 @@
#include <SDL.h>
#include <SDL_opengl.h>
#include "log.h"
#include "physics.h"
#include "opengl.h"
@ -5,183 +7,50 @@
#include "pilot.h"
#include "space.h"
#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;
double 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);
// 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, const Uint8 A) {
Uint32 pixel = SDL_MapRGBA(surface->format, R, G, B, A);
int bpp = surface->format->BytesPerPixel;
// p is the address to the pixel we want to set.
Uint8* p = (Uint8*)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16*)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32*)p = pixel;
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, d;
w = (int)((float)gl_screen.w * 1.5);
if((w & (w-1)) != 0) {
w = 1;
while(w < (int)((float)gl_screen.w*1.5))
w <<= 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;
}
SDL_LockSurface(surface);
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));
SDL_UnlockSurface(surface);
return gl_loadImage(surface);
}
void space_init(void) {
int i;
nstars = 2000;
nstars = (500*gl_screen.w*gl_screen.h + STAR_BUF*STAR_BUF)/(800*640);;
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);
stars[i].brightness = (float)RNG(50, 200)/256.;
stars[i].pos.x = (float)RNG(-STAR_BUF, gl_screen.w + STAR_BUF);
stars[i].pos.y = (float)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(double dt) {
int i;
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glPushMatrix(); // Projection translation matrix.
glTranslated(-(double)gl_screen.w/2., -(double)gl_screen.h/2., 0.);
glBegin(GL_POINTS);
glColor4ub(255, 255, 255, stars[i].brightness);
for(i = 0; i < nstars; i++) {
// Update the position.
stars[i].pos.x -= player->solid->vel.x/(15.-10.*stars[i].brightness)*dt;
stars[i].pos.y -= player->solid->vel.y/(15.-10.*stars[i].brightness)*dt;
// Scroll those stars bitch!
if(stars[i].pos.x > gl_screen.w + STAR_BUF) stars[i].pos.x = -STAR_BUF;
else if(stars[i].pos.x < -STAR_BUF) stars[i].pos.x = gl_screen.w + STAR_BUF;
if(stars[i].pos.y > gl_screen.h + STAR_BUF) stars[i].pos.y = -STAR_BUF;
else if(stars[i].pos.y < -STAR_BUF) stars[i].pos.y = gl_screen.h + STAR_BUF;
// Render.
glColor4d(1., 1., 1., stars[i].brightness);
glVertex2d(stars[i].pos.x, stars[i].pos.y);
glEnd();
glPopMatrix();
#if 0
Vec2 tmp;
FP f;
for(i = 0; i < STAR_LAYERS; i++) {
// Movement.
starPos[i].x -= player->solid->vel.x / (FP)(2*i+10)*dt;
starPos[i].y -= player->solid->vel.y / (FP)(2*i+104)*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;
// More blits if part of the screen is blank.
if(starPos[i].x < starBG[i]->w/4.)
tmp.x += starBG[i]->w;
else if(starPos[i].x < starBG[i]->w*3./4.)
tmp.y -= starBG[i]->w;
if(starPos[i].y < starBG[i]->h/4.)
tmp.y += starBG[i]->h;
else if(starPos[i].y < starBG[i]->h*3./4.)
tmp.y -= starBG[i]->h;
if(tmp.x != starPos[i].x && tmp.y != starPos[i].y) {
gl_blitStatic(starBG[i], &tmp);
f = tmp.x;
tmp.x = starPos[i].x;
gl_blitStatic(starBG[i], &tmp);
tmp.x = f;
tmp.y = starPos[i].y;
gl_blitStatic(starBG[i], &tmp);
}
else if(tmp.x != starPos[i].x || tmp.y != starPos[i].y)
gl_blitStatic(starBG[i], &tmp);
}
#endif
glEnd();
glPopMatrix(); // Projection translation matrix.
}
void space_exit(void) {
free(stars);
#if 0
int i;
for(i = 0; i < STAR_LAYERS; i++)
gl_freeTexture(starBG[i]);
#endif
}