[Change] Some openGL cleanup.
This commit is contained in:
parent
8f7ec2fa09
commit
6552fc2cf2
45
src/opengl.c
45
src/opengl.c
@ -355,17 +355,18 @@ void gl_getSpriteFromDir(int* x, int* y, const glTexture* t, const double dir) {
|
|||||||
// 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.
|
// Don't bother drawing if offscreen -- waste of cycles.
|
||||||
if(fabs(bx-VX(*gl_camera)+gui_xoff) > gl_screen.w / 2 + sprite->sw / 2 ||
|
if(fabs(bx-VX(*gl_camera)+gui_xoff) > SCREEN_W / 2 + sprite->sw / 2 ||
|
||||||
fabs(by-VY(*gl_camera)+gui_yoff) > gl_screen.h / 2 + sprite->sh / 2)
|
fabs(by-VY(*gl_camera)+gui_yoff) > SCREEN_H / 2 + sprite->sh / 2)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double x, y, tx, ty;
|
double x, y, tx, ty;
|
||||||
|
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
x = bx - VX(*gl_camera) - sprite->sw/2. + gui_xoff;
|
x = bx - (double)SCREEN_W/2.;
|
||||||
y = by - VY(*gl_camera) - sprite->sh/2. + gui_yoff;
|
y = by - (double)SCREEN_H/2.;
|
||||||
|
|
||||||
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;
|
||||||
@ -578,8 +579,8 @@ static GLboolean gl_hasExt(char* name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check and report if there's been an error.
|
// Check and report if there's been an error.
|
||||||
|
#ifndef gl_checkErr // I know, I know, it's a little hackish.
|
||||||
void gl_checkErr(void) {
|
void gl_checkErr(void) {
|
||||||
#ifdef DEBUG
|
|
||||||
GLenum err;
|
GLenum err;
|
||||||
char* errstr;
|
char* errstr;
|
||||||
|
|
||||||
@ -614,8 +615,8 @@ void gl_checkErr(void) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
WARN("OpenGL error: %s", errstr);
|
WARN("OpenGL error: %s", errstr);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Initialize SDL/OpenGL etc.
|
// Initialize SDL/OpenGL etc.
|
||||||
int gl_init(void) {
|
int gl_init(void) {
|
||||||
@ -648,8 +649,8 @@ int gl_init(void) {
|
|||||||
DEBUG("Available fullscreen modes:");
|
DEBUG("Available fullscreen modes:");
|
||||||
for(i = 0; modes[i]; i++) {
|
for(i = 0; modes[i]; i++) {
|
||||||
DEBUG("\t%dx%d", modes[i]->w, modes[i]->h);
|
DEBUG("\t%dx%d", modes[i]->w, modes[i]->h);
|
||||||
if((flags & SDL_FULLSCREEN) && (modes[i]->w == gl_screen.w) &&
|
if((flags & SDL_FULLSCREEN) && (modes[i]->w == SCREEN_W) &&
|
||||||
(modes[i]->h == gl_screen.h))
|
(modes[i]->h == SCREEN_H))
|
||||||
supported = 1; // Mode we asked for is supported.
|
supported = 1; // Mode we asked for is supported.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -660,14 +661,15 @@ int gl_init(void) {
|
|||||||
off = -1;
|
off = -1;
|
||||||
j = 0;
|
j = 0;
|
||||||
for(i = 0; modes[i]; i++) {
|
for(i = 0; modes[i]; i++) {
|
||||||
toff = ABS(gl_screen.w-modes[i]->w) + ABS(gl_screen.h-modes[i]->h);
|
toff = ABS(SCREEN_W-modes[i]->w) + ABS(SCREEN_H-modes[i]->h);
|
||||||
if((off == -1) || (toff < off)) {
|
if((off == -1) || (toff < off)) {
|
||||||
j = i;
|
j = i;
|
||||||
off = toff;
|
off = toff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WARN("Fullscreen mode %dx%d is not supported by your setup\n"
|
WARN("Fullscreen mode %dx%d is not supported by your setup\n"
|
||||||
" Switching to %dx%d", gl_screen.w, gl_screen.h,
|
" Switching to %dx%d",
|
||||||
|
SCREEN_W, SCREEN_H,
|
||||||
modes[j]->w, modes[j]->h);
|
modes[j]->w, modes[j]->h);
|
||||||
|
|
||||||
gl_screen.w = modes[j]->w;
|
gl_screen.w = modes[j]->w;
|
||||||
@ -676,18 +678,18 @@ int gl_init(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test the setup - aim for 32.
|
// Test the setup - aim for 32.
|
||||||
depth = SDL_VideoModeOK(gl_screen.w, gl_screen.h, gl_screen.depth, flags);
|
depth = SDL_VideoModeOK(SCREEN_W, SCREEN_H, gl_screen.depth, flags);
|
||||||
if(depth == 0)
|
if(depth == 0)
|
||||||
WARN("Video mode %dx%d @ %d bpp not supported"
|
WARN("Video mode %dx%d @ %d bpp not supported"
|
||||||
" going to try to create it anyway...",
|
" going to try to create it anyway...",
|
||||||
gl_screen.w, gl_screen.h, gl_screen.depth);
|
SCREEN_W, SCREEN_H, gl_screen.depth);
|
||||||
if(depth != gl_screen.depth)
|
if(depth != gl_screen.depth)
|
||||||
LOG("Depth: %d bpp unavailable, will use %d bpp", gl_screen.depth, depth);
|
LOG("Depth: %d bpp unavailable, will use %d bpp", gl_screen.depth, depth);
|
||||||
|
|
||||||
gl_screen.depth = depth;
|
gl_screen.depth = depth;
|
||||||
|
|
||||||
// Actually creating the screen.
|
// Actually creating the screen.
|
||||||
if(SDL_SetVideoMode(gl_screen.w, gl_screen.h, gl_screen.depth, flags) == NULL) {
|
if(SDL_SetVideoMode(SCREEN_W, SCREEN_H, gl_screen.depth, flags) == NULL) {
|
||||||
ERR("Unable to create OpenGL window: %s", SDL_GetError());
|
ERR("Unable to create OpenGL window: %s", SDL_GetError());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -707,24 +709,31 @@ int gl_init(void) {
|
|||||||
if(gl_hasExt("GL_ARB_fragment_shader")==GL_TRUE)
|
if(gl_hasExt("GL_ARB_fragment_shader")==GL_TRUE)
|
||||||
gl_screen.flags |= OPENGL_FRAG_SHADER;
|
gl_screen.flags |= OPENGL_FRAG_SHADER;
|
||||||
|
|
||||||
|
// Max texture size.
|
||||||
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_screen.tex_max);
|
||||||
|
|
||||||
// Debug heaven.
|
// Debug heaven.
|
||||||
DEBUG("OpenGL Window Created: %dx%d@%dbpp %s", gl_screen.w, gl_screen.h,
|
DEBUG("OpenGL Window Created: %dx%d@%dbpp %s", SCREEN_W, SCREEN_H,
|
||||||
gl_screen.depth, (gl_has(OPENGL_FULLSCREEN)) ? "fullscreen" : "window");
|
gl_screen.depth, (gl_has(OPENGL_FULLSCREEN)) ? "fullscreen" : "window");
|
||||||
|
|
||||||
DEBUG("r: %d, g: %d, b: %d, a: %d, doublebuffer: %s",
|
DEBUG("r: %d, g: %d, b: %d, a: %d, db: %s, tex: %d",
|
||||||
gl_screen.r, gl_screen.g, gl_screen.b, gl_screen.a,
|
gl_screen.r, gl_screen.g, gl_screen.b, gl_screen.a,
|
||||||
(gl_has(OPENGL_DOUBLEBUF)) ? "yes" : "no");
|
gl_has(OPENGL_DOUBLEBUF) ? "yes" : "no",
|
||||||
|
gl_screen.tex_max);
|
||||||
|
|
||||||
DEBUG("Renderer: %s", glGetString(GL_RENDERER));
|
DEBUG("Renderer: %s", glGetString(GL_RENDERER));
|
||||||
|
if(!gl_has(OPENGL_FRAG_SHADER))
|
||||||
|
DEBUG("No fragment shader extension detected");
|
||||||
|
DEBUG("");
|
||||||
|
|
||||||
// Some openGL options.
|
// Some openGL options.
|
||||||
glClearColor(0., 0., 0., 1.);
|
glClearColor(0., 0., 0., 1.);
|
||||||
glDisable(GL_DEPTH_TEST); // Set for doing 2D shidazles.
|
glDisable(GL_DEPTH_TEST); // Set for doing 2D shidazles.
|
||||||
//glEnable(GL_TEXTURE_2D); // Don't enable globally, it will break non-texture blits.
|
//glEnable(GL_TEXTURE_2D); // Don't enable globally, it will break non-texture blits.
|
||||||
glDisable(GL_LIGHTING); // No lighting, it is done when rendered.
|
glDisable(GL_LIGHTING); // No lighting, it is done when rendered.
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND); // Alpha blending ftw.
|
||||||
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);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Good blend model.
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glOrtho(-SCREEN_W /2, // Left edge.
|
glOrtho(-SCREEN_W /2, // Left edge.
|
||||||
|
@ -32,6 +32,7 @@ typedef struct glInfo_ {
|
|||||||
int depth; // Depth in bpp.
|
int depth; // Depth in bpp.
|
||||||
int r, g, b, a; // Framebuffer values in bits.
|
int r, g, b, a; // Framebuffer values in bits.
|
||||||
int flags; // Store different properties.
|
int flags; // Store different properties.
|
||||||
|
int tex_max; // Max texture size.
|
||||||
} glInfo;
|
} glInfo;
|
||||||
extern glInfo gl_screen; // Local structure set with gl_init etc.
|
extern glInfo gl_screen; // Local structure set with gl_init etc.
|
||||||
|
|
||||||
@ -79,5 +80,9 @@ void gl_exit(void);
|
|||||||
int gl_isTrans(const glTexture* t, const int x, const int y);
|
int gl_isTrans(const glTexture* t, const int x, const int y);
|
||||||
void gl_getSpriteFromDir(int* x, int* y, const glTexture* t, const double dir);
|
void gl_getSpriteFromDir(int* x, int* y, const glTexture* t, const double dir);
|
||||||
void gl_screenshot(const char* filename);
|
void gl_screenshot(const char* filename);
|
||||||
|
#if DEBUG == 1
|
||||||
void gl_checkErr(void);
|
void gl_checkErr(void);
|
||||||
|
#else
|
||||||
|
#define gl_checkErr()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user