[Fix] OpenGL blitting stuff.

This commit is contained in:
Allanis 2013-06-01 21:59:08 +01:00
parent 61cb4a3f67
commit fee98162a6
2 changed files with 76 additions and 48 deletions

View File

@ -32,6 +32,9 @@ static uint8_t* SDL_MapTrans(SDL_Surface* s);
static int pot(int n); static int pot(int n);
// glTexture. // glTexture.
static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh); static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh);
static void gl_blitTexture(const glTexture* texture,
const double x, const double y,
const double tx, const double ty, const glColour* c);
// PNG. // PNG.
int write_png(const char* file_name, png_bytep* rows, int w, int h, int write_png(const char* file_name, png_bytep* rows, int w, int h,
@ -350,82 +353,102 @@ void gl_getSpriteFromDir(int* x, int* y, const glTexture* t, const double dir) {
// BLITTING! // BLITTING!
// ================ // ================
static void gl_blitTexture(const glTexture* texture,
const double x, const double y,
const double tx, const double ty, const glColour* c) {
double tw, th;
// Texture dimensions.
tw = texture->sw / texture->rw;
th = texture->sh / texture->rh;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture->texture);
glBegin(GL_QUADS);
// Set colour or default if not set.
if(c == NULL) glColor4d(1., 1., 1., 1.);
else COLOUR(*c);
glTexCoord2d(tx, ty);
glVertex2d(x, y);
glTexCoord2d(tx + tw, ty);
glVertex2d(x + texture->sw, y);
glTexCoord2d(tx + tw, ty + th);
glVertex2d(x + texture->sw, y + texture->sh);
glTexCoord2d(tx, ty + th);
glVertex2d(x, y + texture->sh);
glEnd();
glDisable(GL_TEXTURE_2D);
// Did anything fail?
gl_checkErr();
}
// Blit the sprite at given position. // Blit the sprite at given position.
void gl_blitSprite(const glTexture* sprite, const double bx, const double by, void gl_blitSprite(const glTexture* sprite, const double bx, const double by,
const int sx, const int sy, const glColour* c) { const int sx, const int sy, const glColour* c) {
// Don't bother drawing if offscreen -- waste of cycles.
if(fabs(bx-VX(*gl_camera)+gui_xoff) > SCREEN_W / 2 + sprite->sw / 2 ||
fabs(by-VY(*gl_camera)+gui_yoff) > SCREEN_H / 2 + sprite->sh / 2)
return;
double x, y, tx, ty; double x, y, tx, ty;
glEnable(GL_TEXTURE_2D); // Calculate position - we'll use relative coords to player.
x = bx - VX(*gl_camera) - sprite->sw/2. + gui_xoff; x = bx - VX(*gl_camera) - sprite->sw/2. + gui_xoff;
y = by - VY(*gl_camera) - sprite->sh/2. + gui_yoff; y = by - VY(*gl_camera) - sprite->sh/2. + gui_yoff;
// Don't draw if offscreen.
if((fabs(x) > SCREEN_W/2 + sprite->sw) ||
(fabs(y) > SCREEN_H/2 + sprite->sh))
return;
// Texture coords.
tx = sprite->sw * (double)(sx)/sprite->rw; tx = sprite->sw * (double)(sx)/sprite->rw;
ty = sprite->sh * (sprite->sy-(double)sy-1)/sprite->rh; ty = sprite->sh * (sprite->sy-(double)sy-1)/sprite->rh;
// Actual blitting.... // Actual blitting.
glBindTexture(GL_TEXTURE_2D, sprite->texture); gl_blitTexture(sprite, x, y, tx, ty, c);
glBegin(GL_QUADS); }
if(c == NULL) glColor4d(1., 1., 1., 1.);
else COLOUR(*c);
glTexCoord2d(tx, ty); // Like gl_blitSprite but will use the actual direction, for things that
glVertex2d(x, y); // can just rotate around.
void gl_blitRotate(const glTexture* texture,
const double bx, const double by,
const double dir, const glColour* c) {
glTexCoord2d(tx + sprite->sw/sprite->rw, ty); double x, y;
glVertex2d(x + sprite->sw, y);
glTexCoord2d(tx + sprite->sw/sprite->rw, ty + sprite->sh/sprite->rh); // Calculate position - we'll use relative coords to player.
glVertex2d(x + sprite->sw, y + sprite->sh); x = bx - VX(*gl_camera) - texture->sw/2. + gui_xoff;
y = by - VY(*gl_camera) - texture->sh/2. + gui_yoff;
glTexCoord2d(tx, ty + sprite->sh/sprite->rh); // Don't draw if offscreen.
glVertex2d(x, y + sprite->sh); if((fabs(x) > SCREEN_W/2 + texture->sw) ||
(fabs(y) > SCREEN_H/2 + texture->sh))
return;
glEnd(); glMatrixMode(GL_PROJECTION);
glPushMatrix();
glRotated(dir, 0., 0., 1.);
glDisable(GL_TEXTURE_2D); // Blit.
gl_blitTexture(texture, x, y, 0, 0, c);
gl_checkErr(); glPopMatrix(); // GL_PROJECTION.
} }
// Just straight out blit the thing at position. // Just straight out blit the thing at position.
void gl_blitStatic(const glTexture* texture, const double bx, const double by, void gl_blitStatic(const glTexture* texture, const double bx, const double by,
const glColour* c) { const glColour* c) {
double x, y; double x, y;
glEnable(GL_TEXTURE_2D);
// Here we use absolute coords.
x = bx - (double)SCREEN_W/2.; x = bx - (double)SCREEN_W/2.;
y = by - (double)SCREEN_H/2.; y = by - (double)SCREEN_H/2.;
// Actual blitting.. // Actual blitting..
glBindTexture(GL_TEXTURE_2D, texture->texture); gl_blitTexture(texture, x, y, 0, 0, c);
glBegin(GL_QUADS);
if(c == NULL) glColor4d(1., 1., 1., 1.);
else COLOUR(*c);
glTexCoord2d(0., 0.);
glVertex2d(x, y);
glTexCoord2d(texture->sw/texture->rw, 0.);
glVertex2d(x + texture->sw, y);
glTexCoord2d(texture->sw/texture->rw, texture->sh/texture->rh);
glVertex2d(x + texture->sw, y + texture->sh);
glTexCoord2d(0., texture->sh/texture->rh);
glVertex2d(x, y + texture->sh);
glEnd();
glDisable(GL_TEXTURE_2D);
gl_checkErr();
} }
// Bind our precious camera to a vector. // Bind our precious camera to a vector.

View File

@ -60,11 +60,16 @@ glTexture* gl_newSprite(const char* path, const int sx, const int sy);
void gl_freeTexture(glTexture* texture); void gl_freeTexture(glTexture* texture);
// Rendering. // Rendering.
// Blits a sprite. // Blits a sprite, relative pos.
void gl_blitSprite(const glTexture* sprite, const double bx, const double by, void gl_blitSprite(const glTexture* sprite, const double bx, const double by,
const int sx, const int sy, const glColour* c); const int sx, const int sy, const glColour* c);
// Blit the entire image. // Blits a texture rotated, relative pos.
void gl_blitRotate(const glTexture* texture,
const double bx, const double by,
const double dir, const glColour* c);
// Blit the entire image, absolute pos.
void gl_blitStatic(const glTexture* texture, const double bx, const double by, void gl_blitStatic(const glTexture* texture, const double bx, const double by,
const glColour* c); const glColour* c);