[Fix] Fixed fuckeries in last commit. Documented a bit more too.
This commit is contained in:
parent
b75ec61d10
commit
d736e9a6a1
134
src/opengl.c
134
src/opengl.c
@ -58,7 +58,11 @@ static GLboolean gl_hasExt(char* name);
|
|||||||
* MISC!
|
* MISC!
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Get the closest power of two. */
|
/**
|
||||||
|
* @brief Get the closest power of two.
|
||||||
|
* @param n Number to get closest power of two to.
|
||||||
|
* @return Closest power of two to the number.
|
||||||
|
*/
|
||||||
int gl_pot(int n) {
|
int gl_pot(int n) {
|
||||||
int i = 1;
|
int i = 1;
|
||||||
while(i < n)
|
while(i < n)
|
||||||
@ -66,7 +70,11 @@ int gl_pot(int n) {
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Flips the surface vertically. Return 0 on success. */
|
/**
|
||||||
|
* @brief Flips the surface vertically.
|
||||||
|
* @param surface Surface to flip.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
static int SDL_VFlipSurface(SDL_Surface* surface) {
|
static int SDL_VFlipSurface(SDL_Surface* surface) {
|
||||||
/* Flip the image. */
|
/* Flip the image. */
|
||||||
Uint8* rowhi, *rowlo, *tmpbuf;
|
Uint8* rowhi, *rowlo, *tmpbuf;
|
||||||
@ -92,7 +100,13 @@ static int SDL_VFlipSurface(SDL_Surface* surface) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return true if position (x,y) of s is transparent. */
|
/**
|
||||||
|
* @brief Check to see if a position of the surface is transparent.
|
||||||
|
* @param s Surface to check for transparency.
|
||||||
|
* @param x X position of the pixel to check.
|
||||||
|
* @param y Y position of the pixel to check.
|
||||||
|
* @return 0 if the pixel isn't transparent, 0 if it is.
|
||||||
|
*/
|
||||||
static int SDL_IsTrans(SDL_Surface* s, int x, int y) {
|
static int SDL_IsTrans(SDL_Surface* s, int x, int y) {
|
||||||
int bpp = s->format->BytesPerPixel;
|
int bpp = s->format->BytesPerPixel;
|
||||||
/* p is the address to the pixel we want to retrieve. */
|
/* p is the address to the pixel we want to retrieve. */
|
||||||
@ -122,8 +136,14 @@ static int SDL_IsTrans(SDL_Surface* s, int x, int y) {
|
|||||||
return (pixelcolour == s->format->colorkey);
|
return (pixelcolour == s->format->colorkey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Map the surface transparancy. */
|
/**
|
||||||
/* Return 0 on success. */
|
* @brief Maps the surface transparency.
|
||||||
|
*
|
||||||
|
* Basically generates a map of what pixels are transparent. Good for pixel
|
||||||
|
* perfect collision routines.
|
||||||
|
* @param s Surface to map it's transparency.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
static uint8_t* SDL_MapTrans(SDL_Surface* s) {
|
static uint8_t* SDL_MapTrans(SDL_Surface* s) {
|
||||||
/* Allocate memory for just enough bits to hold all the data we need. */
|
/* Allocate memory for just enough bits to hold all the data we need. */
|
||||||
int size = s->w*s->h/8 + ((s->w*s->h%8)?1:0);
|
int size = s->w*s->h/8 + ((s->w*s->h%8)?1:0);
|
||||||
@ -143,7 +163,10 @@ static uint8_t* SDL_MapTrans(SDL_Surface* s) {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Take a screenshot. */
|
/**
|
||||||
|
* @brief Takes a screenshot.
|
||||||
|
* @param filename Name of the file to save screenshot as.
|
||||||
|
*/
|
||||||
void gl_screenshot(const char* filename) {
|
void gl_screenshot(const char* filename) {
|
||||||
SDL_Surface* screen = SDL_GetVideoSurface();
|
SDL_Surface* screen = SDL_GetVideoSurface();
|
||||||
unsigned rowbytes = screen->w * 3;
|
unsigned rowbytes = screen->w * 3;
|
||||||
@ -158,23 +181,28 @@ void gl_screenshot(const char* filename) {
|
|||||||
gl_checkErr();
|
gl_checkErr();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Save a surface to a file as a png. */
|
/**
|
||||||
|
* @brief Saves a surface to a file as a png.
|
||||||
|
* @param surface Surface to save.
|
||||||
|
* @param file Path to save surface to.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
int SDL_savePNG(SDL_Surface* surface, const char* file) {
|
int SDL_savePNG(SDL_Surface* surface, const char* file) {
|
||||||
static unsigned char** ss_rows;
|
unsigned char** ss_rows;
|
||||||
static int ss_size;
|
int ss_size;
|
||||||
static int ss_w, ss_h;
|
int ss_w, ss_h;
|
||||||
SDL_Surface* ss_surface;
|
SDL_Surface* ss_surface;
|
||||||
SDL_Rect ss_rect;
|
SDL_Rect ss_rect;
|
||||||
int r, i;
|
int r, i;
|
||||||
int alpha = 0;
|
|
||||||
int pixel_bits = 32;
|
|
||||||
|
|
||||||
unsigned surf_flags;
|
int alpha;
|
||||||
unsigned surf_alpha;
|
int pixel_bits;
|
||||||
|
unsigned int surf_flags;
|
||||||
|
unsigned int surf_alpha;
|
||||||
|
|
||||||
ss_rows = 0;
|
ss_rows = NULL;
|
||||||
ss_size = 0;
|
ss_size = 0;
|
||||||
ss_surface = 0;
|
ss_surface = NULL;
|
||||||
|
|
||||||
ss_w = surface->w;
|
ss_w = surface->w;
|
||||||
ss_h = surface->h;
|
ss_h = surface->h;
|
||||||
@ -183,13 +211,14 @@ int SDL_savePNG(SDL_Surface* surface, const char* file) {
|
|||||||
alpha = 1;
|
alpha = 1;
|
||||||
pixel_bits = 32;
|
pixel_bits = 32;
|
||||||
} else {
|
} else {
|
||||||
|
alpha = 0;
|
||||||
pixel_bits = 24;
|
pixel_bits = 24;
|
||||||
}
|
}
|
||||||
|
|
||||||
ss_surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, ss_w, ss_h,
|
ss_surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, ss_w, ss_h,
|
||||||
pixel_bits, RGBAMASK);
|
pixel_bits, RGBAMASK);
|
||||||
|
|
||||||
if(ss_surface == 0) {
|
if(ss_surface == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,7 +238,7 @@ int SDL_savePNG(SDL_Surface* surface, const char* file) {
|
|||||||
if(ss_size == 0) {
|
if(ss_size == 0) {
|
||||||
ss_size = ss_h;
|
ss_size = ss_h;
|
||||||
ss_rows = (unsigned char**)malloc(sizeof(unsigned char*)*ss_size);
|
ss_rows = (unsigned char**)malloc(sizeof(unsigned char*)*ss_size);
|
||||||
if(ss_rows == 0) {
|
if(ss_rows == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -218,19 +247,16 @@ int SDL_savePNG(SDL_Surface* surface, const char* file) {
|
|||||||
if(surf_flags & SDL_SRCCOLORKEY)
|
if(surf_flags & SDL_SRCCOLORKEY)
|
||||||
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, surface->format->colorkey);
|
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, surface->format->colorkey);
|
||||||
|
|
||||||
for(i = 0; i < ss_h; i++) {
|
for(i = 0; i < ss_h; i++)
|
||||||
ss_rows[i] = ((unsigned char*)ss_surface->pixels) + i * ss_surface->pitch;
|
ss_rows[i] = ((unsigned char*)ss_surface->pixels) + i * ss_surface->pitch;
|
||||||
}
|
|
||||||
|
|
||||||
if(alpha) {
|
if(alpha)
|
||||||
r = write_png(file, ss_rows, surface->w, surface->h, PNG_COLOR_TYPE_RGB_ALPHA, 8);
|
r = write_png(file, ss_rows, surface->w, surface->h, PNG_COLOR_TYPE_RGB_ALPHA, 8);
|
||||||
} else {
|
else
|
||||||
r = write_png(file, ss_rows, surface->w, surface->h, PNG_COLOR_TYPE_RGB, 8);
|
r = write_png(file, ss_rows, surface->w, surface->h, PNG_COLOR_TYPE_RGB, 8);
|
||||||
}
|
|
||||||
|
|
||||||
free(ss_rows);
|
free(ss_rows);
|
||||||
SDL_FreeSurface(ss_surface);
|
SDL_FreeSurface(ss_surface);
|
||||||
ss_surface = NULL;
|
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -241,8 +267,10 @@ int SDL_savePNG(SDL_Surface* surface, const char* file) {
|
|||||||
* ================
|
* ================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Returns the texture ID.
|
/**
|
||||||
* Stores real sizes in rw/rh (from POT padding).
|
* @brief Prepares the surface to be loaded as a texture.
|
||||||
|
* @param surface to load that is freed in the process.
|
||||||
|
* @return New surface that is prepared for texture loading.
|
||||||
*/
|
*/
|
||||||
SDL_Surface* gl_prepareSurface(SDL_Surface* surface) {
|
SDL_Surface* gl_prepareSurface(SDL_Surface* surface) {
|
||||||
SDL_Surface* tmp;
|
SDL_Surface* tmp;
|
||||||
@ -788,7 +816,11 @@ void gl_drawCircleInRect(const double cx, const double cy, const double r,
|
|||||||
/* GLOBAL. */
|
/* GLOBAL. */
|
||||||
/* ================ */
|
/* ================ */
|
||||||
|
|
||||||
/* Check for extensions. */
|
/**
|
||||||
|
* @brief Check for an opengl extension.
|
||||||
|
* @param name Extension to check for.
|
||||||
|
* @return GL_TRUE if found, GL_FALSE if it isn't.
|
||||||
|
*/
|
||||||
static GLboolean gl_hasExt(char* name) {
|
static GLboolean gl_hasExt(char* name) {
|
||||||
/* ====================================================== */
|
/* ====================================================== */
|
||||||
/* Search for name in the extensions string. Use of strstr() */
|
/* Search for name in the extensions string. Use of strstr() */
|
||||||
@ -814,10 +846,11 @@ static GLboolean gl_hasExt(char* name) {
|
|||||||
return GL_FALSE;
|
return GL_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check and report if there's been an error. */
|
#ifndef gl_checkErr // I know, I know, it's a little hackish.
|
||||||
/*#ifndef gl_checkErr // I know, I know, it's a little hackish. */
|
/**
|
||||||
|
* @brief Check and report if there's been an error.
|
||||||
|
*/
|
||||||
void gl_checkErr(void) {
|
void gl_checkErr(void) {
|
||||||
#ifdef DEBUG
|
|
||||||
GLenum err;
|
GLenum err;
|
||||||
char* errstr;
|
char* errstr;
|
||||||
|
|
||||||
@ -852,13 +885,14 @@ void gl_checkErr(void) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
WARN("OpenGL error: %s", errstr);
|
WARN("OpenGL error: %s", errstr);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
/*#endif */
|
#endif
|
||||||
|
|
||||||
/* Initialize SDL/OpenGL etc. */
|
/**
|
||||||
|
* @brief Initializes SDL/OpenGL and the works.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
int gl_init(void) {
|
int gl_init(void) {
|
||||||
double dmin;
|
|
||||||
int doublebuf, depth, i, j, off, toff, supported, fsaa;
|
int doublebuf, depth, i, j, off, toff, supported, fsaa;
|
||||||
SDL_Rect** modes;
|
SDL_Rect** modes;
|
||||||
int flags = SDL_OPENGL;
|
int flags = SDL_OPENGL;
|
||||||
@ -959,10 +993,6 @@ int gl_init(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Save real window width/height. */
|
|
||||||
gl_screen.rw = SCREEN_W;
|
|
||||||
gl_screen.rh = SCREEN_H;
|
|
||||||
|
|
||||||
/* Get info about the OpenGL window. */
|
/* Get info about the OpenGL window. */
|
||||||
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &gl_screen.r);
|
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &gl_screen.r);
|
||||||
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &gl_screen.g);
|
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &gl_screen.g);
|
||||||
@ -1018,19 +1048,19 @@ int gl_init(void) {
|
|||||||
glShadeModel(GL_FLAT); /* Default shade model. Functions should keep this when done.. */
|
glShadeModel(GL_FLAT); /* Default shade model. Functions should keep this when done.. */
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); /* Good blend model. */
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); /* Good blend model. */
|
||||||
|
|
||||||
/* Set up the matrix. */
|
/* Set up the proper viewport to use. */
|
||||||
dmin = 1.;
|
gl_screen.rw = SCREEN_W;
|
||||||
|
gl_screen.rh = SCREEN_H;
|
||||||
if((SCREEN_W < 640) && (SCREEN_W <= SCREEN_H)) {
|
if((SCREEN_W < 640) && (SCREEN_W <= SCREEN_H)) {
|
||||||
gl_screen.w = (gl_screen.w * 640) / SCREEN_H;
|
gl_screen.w = (gl_screen.w * 640) / SCREEN_H;
|
||||||
gl_screen.rw = (gl_screen.rw * SCREEN_H) / 640;
|
gl_screen.rw = (gl_screen.rw * SCREEN_H) / 640;
|
||||||
gl_screen.h = 640;
|
gl_screen.h = 640;
|
||||||
}
|
}
|
||||||
else if((SCREEN_W >= 640) && (SCREEN_W >= SCREEN_H)) {
|
else if((SCREEN_W < 640) && (SCREEN_W >= SCREEN_H)) {
|
||||||
gl_screen.w = (gl_screen.w * 640) / SCREEN_H;
|
gl_screen.w = (gl_screen.w * 640) / SCREEN_H;
|
||||||
gl_screen.rw = (gl_screen.rw * SCREEN_H) / 640;
|
gl_screen.rw = (gl_screen.rw * SCREEN_H) / 640;
|
||||||
gl_screen.h = 640;
|
gl_screen.h = 640;
|
||||||
}
|
}
|
||||||
DEBUG("%dx%d on %dx%d", gl_screen.w, gl_screen.h, gl_screen.rw, gl_screen.rh);
|
|
||||||
gl_defViewport();
|
gl_defViewport();
|
||||||
|
|
||||||
/* Finishing touches. */
|
/* Finishing touches. */
|
||||||
@ -1044,17 +1074,20 @@ int gl_init(void) {
|
|||||||
void gl_defViewport(void) {
|
void gl_defViewport(void) {
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
/*glViewport(0, 0, (GLsizei)SCREEN_W, (GLsizei)SCREEN_H);*/
|
|
||||||
glOrtho(-SCREEN_W /2, /* Left edge. */
|
glOrtho(-SCREEN_W /2, /* Left edge. */
|
||||||
SCREEN_W /2, /* Right edge. */
|
SCREEN_W /2, /* Right edge. */
|
||||||
-SCREEN_H /2, /* Bottom edge. */
|
-SCREEN_H /2, /* Bottom edge. */
|
||||||
SCREEN_H /2, /* Top edge. */
|
SCREEN_H /2, /* Top edge. */
|
||||||
-1., /* Near. */
|
-1., /* Near. */
|
||||||
1.); /* Far. */
|
1.); /* Far. */
|
||||||
glScaled(gl_screen.w / gl_screen.rw, gl_screen.h / gl_screen.rh, 1.);
|
/* Take into account possible scaling. */
|
||||||
|
glScaled((double)gl_screen.w / (double)gl_screen.rw,
|
||||||
|
(double)gl_screen.h / (double)gl_screen.rh, 1.);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Clean up our mess. */
|
/**
|
||||||
|
* @brief Cleans up OpenGL, the works.
|
||||||
|
*/
|
||||||
void gl_exit(void) {
|
void gl_exit(void) {
|
||||||
glTexList* tex;
|
glTexList* tex;
|
||||||
|
|
||||||
@ -1069,7 +1102,16 @@ void gl_exit(void) {
|
|||||||
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Saves a png. */
|
/**
|
||||||
|
* @brief Saves a png.
|
||||||
|
* @param file_name Name of the file to save the png as.
|
||||||
|
* @param rows Rows containing the data.
|
||||||
|
* @param w Width of the png.
|
||||||
|
* @param h Height of the png.
|
||||||
|
* @param colourtype Colour type of the png.
|
||||||
|
* @param bitdepth But depth of the png.
|
||||||
|
* @return 0 on success.
|
||||||
|
*/
|
||||||
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,
|
||||||
int colourtype, int bitdepth) {
|
int colourtype, int bitdepth) {
|
||||||
png_structp png_ptr;
|
png_structp png_ptr;
|
||||||
|
Loading…
Reference in New Issue
Block a user