X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fsdlemu_opengl.c;h=7b4e01e61bb419bf3876a367602738e35b557045;hb=d239de704f276a75d927900e3d413a44cc87116c;hp=348982297f1c2f2c787919d584f0033ca114db8a;hpb=f3e5bb2807319c210d124d6150e019130c54867b;p=virtualjaguar diff --git a/src/sdlemu_opengl.c b/src/sdlemu_opengl.c index 3489822..7b4e01e 100644 --- a/src/sdlemu_opengl.c +++ b/src/sdlemu_opengl.c @@ -41,6 +41,8 @@ #include "sdlemu_opengl.h" +#include "log.h" + // We want alpha on our OpenGL contexts...! // Or do we? Seems to kill performance on X... // Or does it? Could it be bad blitter performance? @@ -71,20 +73,20 @@ static inline int power_of_two(int input) void sdlemu_init_opengl(SDL_Surface * src, SDL_Surface * dst, int texturetype, int filter, int src_bpp) { - printf("\nOpenGL driver information :\n"); - printf("\n"); - printf("Vendor: %s\n", glGetString(GL_VENDOR)); - printf("Renderer: %s\n", glGetString(GL_RENDERER)); - printf("Version: %s\n", glGetString(GL_VERSION)); - printf("OpenGL drawmethod: "); + WriteLog("\nOpenGL driver information :\n"); + WriteLog("\n"); + WriteLog("Vendor: %s\n", glGetString(GL_VENDOR)); + WriteLog("Renderer: %s\n", glGetString(GL_RENDERER)); + WriteLog("Version: %s\n", glGetString(GL_VERSION)); + WriteLog("OpenGL drawmethod: "); switch (texturetype) { case 1: - printf("GL_QUAD rendering\n\n"); + WriteLog("GL_QUAD rendering\n\n"); break; default: - printf("GL_TRIANGLE rendering\n\n"); + WriteLog("GL_TRIANGLE rendering\n\n"); break; } @@ -207,7 +209,6 @@ glDrawPixels(5, 5, GL_RGBA, GL_UNSIGNED_INT, map);//*/ // glFlush(); SDL_GL_SwapBuffers(); // glFinish(); - } void sdlemu_close_opengl(void) @@ -219,33 +220,35 @@ void sdlemu_close_opengl(void) SDL_FreeSurface(overlay); } -void sdlemu_create_overlay(SDL_Surface * dst, int src_bpp) +void sdlemu_create_texture(SDL_Surface * src, SDL_Surface * dst, int filter, int src_bpp) { // Local variables. + int w , h; // w and h contain the width and height of the OpenGL texture. Uint32 rmask, gmask, bmask, amask; // Needed for creating RGBA masks. + int bpp; // Delete old texture (if allocated). Useful when there is a screen resize. - if (overlay) - SDL_FreeSurface(overlay); + if (texture) + SDL_FreeSurface(texture); // Texture width/height should be power of 2 of the SDL_Surface *src when using OpenGL. // So, find the largest power of two that will contain both the width and height - int w = power_of_two(dst->w); - int h = power_of_two(dst->h); + w = power_of_two(src->w); + h = power_of_two(src->h); - printf("OpenGL - Overlay size : %d x %d\n", w, h); + WriteLog("OpenGL - Texture size : %d x %d\n", w, h); // Setting bpp based upon src_bpp. - int bpp = src_bpp; + bpp = src_bpp; // We allow the developer to set its own texture bpp. But if the value is NULL or // not equal to 16, 24 or 32, we make the texturebpp the same as the BPP from src. if (bpp == 16 || bpp == 24 || bpp == 32) texturebpp = bpp; else - texturebpp = dst->format->BitsPerPixel; + texturebpp = src->format->BitsPerPixel; - printf("OpenGL - Overlay depth : %d bpp\n", texturebpp); + WriteLog("OpenGL - Texture depth : %d bpp\n", texturebpp); // Now were are going to create a SDL_Surface named texture. This will be our surface // which will function as a buffer between the SDL_Surface *src and SDL_Surface *dst. @@ -306,20 +309,65 @@ void sdlemu_create_overlay(SDL_Surface * dst, int src_bpp) } // Creating SDL_Surface texture based upon the above settings. - overlay = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, texturebpp, rmask, gmask, bmask, amask); + texture = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, texturebpp, rmask, gmask, bmask, amask); + + if (texture == NULL) + { + WriteLog("sdlemu_opengl: Could not create texture surface! (SDL: %s)\n", SDL_GetError()); + } + + // Setting up OpenGL + glDisable(GL_FOG); + glDisable(GL_LIGHTING); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); +// glDisable(GL_BLEND); + glEnable(GL_BLEND); + glDisable(GL_NORMALIZE); + glDisable(GL_ALPHA_TEST); + glEnable(GL_TEXTURE_2D); +// glBlendFunc(GL_SRC_ALPHA, GL_ONE); +// glBlendFunc(GL_ONE, GL_SRC_ALPHA); +//This works, but in a wrong way... +// glBlendFunc(GL_ONE, GL_ONE); + + // Definitely needed for screen resolution larger then the *src. + // This way we can have automatic scaling functionality. + glViewport(0, 0, dst->w, dst->h); + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho(0.0, (GLdouble)dst->w, (GLdouble)dst->h, 0.0, 0.0, 1.0); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); // Setting up the texture coordinates. - overlayCoord[0] = 0.0f; - overlayCoord[1] = 0.0f; - overlayCoord[2] = (GLfloat)(dst->w) / overlay->w; - overlayCoord[3] = (GLfloat)(dst->h) / overlay->h; + texcoord[0] = 0.0f; + texcoord[1] = 0.0f; + texcoord[2] = (GLfloat)(src->w) / texture->w; + texcoord[3] = (GLfloat)(src->h) / texture->h; // create a RGB(A) texture for the texture surface - glGenTextures(1, &overlayID); - glBindTexture(GL_TEXTURE_2D, overlayID); + glGenTextures(1, &texid); + glBindTexture(GL_TEXTURE_2D, texid); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + // Setting up the OpenGL Filters. These filters are important when we/you + // want to scale the texture. + if (filter) + { + // Textures are rendered in best quality. + WriteLog("OpenGL filters: enabled\n"); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + } + else + { + // Textures are rendered in normal quality. + WriteLog("OpenGL filters: disabled\n"); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + } // Setting texture mode. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); @@ -330,7 +378,7 @@ void sdlemu_create_overlay(SDL_Surface * dst, int src_bpp) { case 16: // Normal 16bpp depth based textures consist out of GL_RGB5 and doesn't have support for Alpha channels. - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5, overlay->w, overlay->h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); break; case 24: // The 24bpp depth based textures consist out of GL_RGB8 and doesn't have support for Alpha channels. @@ -338,65 +386,43 @@ void sdlemu_create_overlay(SDL_Surface * dst, int src_bpp) // IMPORTANT : If you don't use Alpha. Use textures with a depth of 16bpp. // If you use Alpha. Use textures with a depth of 32bpp. // 24bpp textures are SLOW and avoid them at all costs! - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, overlay->w, overlay->h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); break; case 32: default: // The 32bpp depth based textures consist out of GL_RGBA8 and has support for Alpha channels. - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, overlay->w, overlay->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); break; } } -void * sdlemuGetOverlayPixels(void) -{ - return overlay->pixels; -} - -Uint32 sdlemuGetOverlayWidthInPixels(void) -{ - return overlay->pitch / 4; -} - -void sdlemuEnableOverlay(void) -{ - showOverlay = 1; -} - -void sdlemuDisableOverlay(void) -{ - showOverlay = 0; -} - -void sdlemu_create_texture(SDL_Surface * src, SDL_Surface * dst, int filter, int src_bpp) +void sdlemu_create_overlay(SDL_Surface * dst, int src_bpp) { // Local variables. - int w , h; // w and h contain the width and height of the OpenGL texture. Uint32 rmask, gmask, bmask, amask; // Needed for creating RGBA masks. - int bpp; // Delete old texture (if allocated). Useful when there is a screen resize. - if (texture) - SDL_FreeSurface(texture); + if (overlay) + SDL_FreeSurface(overlay); // Texture width/height should be power of 2 of the SDL_Surface *src when using OpenGL. // So, find the largest power of two that will contain both the width and height - w = power_of_two(src->w); - h = power_of_two(src->h); + int w = power_of_two(dst->w); + int h = power_of_two(dst->h); - printf("OpenGL - Texture size : %d x %d\n", w, h); + WriteLog("OpenGL - Overlay size : %d x %d\n", w, h); // Setting bpp based upon src_bpp. - bpp = src_bpp; + int bpp = src_bpp; // We allow the developer to set its own texture bpp. But if the value is NULL or // not equal to 16, 24 or 32, we make the texturebpp the same as the BPP from src. if (bpp == 16 || bpp == 24 || bpp == 32) texturebpp = bpp; else - texturebpp = src->format->BitsPerPixel; + texturebpp = dst->format->BitsPerPixel; - printf("OpenGL - Texture depth : %d bpp\n", texturebpp); + WriteLog("OpenGL - Overlay depth : %d bpp\n", texturebpp); // Now were are going to create a SDL_Surface named texture. This will be our surface // which will function as a buffer between the SDL_Surface *src and SDL_Surface *dst. @@ -457,60 +483,25 @@ void sdlemu_create_texture(SDL_Surface * src, SDL_Surface * dst, int filter, int } // Creating SDL_Surface texture based upon the above settings. - texture = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, texturebpp, rmask, gmask, bmask, amask); - - // Setting up OpenGL - glDisable(GL_FOG); - glDisable(GL_LIGHTING); - glDisable(GL_CULL_FACE); - glDisable(GL_DEPTH_TEST); -// glDisable(GL_BLEND); - glEnable(GL_BLEND); - glDisable(GL_NORMALIZE); - glDisable(GL_ALPHA_TEST); - glEnable(GL_TEXTURE_2D); -// glBlendFunc(GL_SRC_ALPHA, GL_ONE); -// glBlendFunc(GL_ONE, GL_SRC_ALPHA); -//This works, but in a wrong way... -// glBlendFunc(GL_ONE, GL_ONE); + overlay = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, texturebpp, rmask, gmask, bmask, amask); - // Definitely needed for screen resolution larger then the *src. - // This way we can have automatic scaling functionality. - glViewport(0, 0, dst->w, dst->h); - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glOrtho(0.0, (GLdouble)dst->w, (GLdouble)dst->h, 0.0, 0.0, 1.0); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); + if (overlay == NULL) + { + WriteLog("sdlemu_opengl: Could not create overlay surface! (SDL: %s)\n", SDL_GetError()); + } // Setting up the texture coordinates. - texcoord[0] = 0.0f; - texcoord[1] = 0.0f; - texcoord[2] = (GLfloat)(src->w) / texture->w; - texcoord[3] = (GLfloat)(src->h) / texture->h; + overlayCoord[0] = 0.0f; + overlayCoord[1] = 0.0f; + overlayCoord[2] = (GLfloat)(dst->w) / overlay->w; + overlayCoord[3] = (GLfloat)(dst->h) / overlay->h; // create a RGB(A) texture for the texture surface - glGenTextures(1, &texid); - glBindTexture(GL_TEXTURE_2D, texid); + glGenTextures(1, &overlayID); + glBindTexture(GL_TEXTURE_2D, overlayID); - // Setting up the OpenGL Filters. These filters are important when we/you - // want to scale the texture. - if (filter) - { - // Textures are rendered in best quality. - printf("OpenGL filters: enabled\n"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - } - else - { - // Textures are rendered in normal quality. - printf("OpenGL filters: disabled\n"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - } + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Setting texture mode. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); @@ -521,7 +512,7 @@ void sdlemu_create_texture(SDL_Surface * src, SDL_Surface * dst, int filter, int { case 16: // Normal 16bpp depth based textures consist out of GL_RGB5 and doesn't have support for Alpha channels. - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5, overlay->w, overlay->h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); break; case 24: // The 24bpp depth based textures consist out of GL_RGB8 and doesn't have support for Alpha channels. @@ -529,12 +520,32 @@ void sdlemu_create_texture(SDL_Surface * src, SDL_Surface * dst, int filter, int // IMPORTANT : If you don't use Alpha. Use textures with a depth of 16bpp. // If you use Alpha. Use textures with a depth of 32bpp. // 24bpp textures are SLOW and avoid them at all costs! - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, overlay->w, overlay->h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); break; case 32: default: // The 32bpp depth based textures consist out of GL_RGBA8 and has support for Alpha channels. - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, overlay->w, overlay->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); break; } } + +void * sdlemuGetOverlayPixels(void) +{ + return overlay->pixels; +} + +Uint32 sdlemuGetOverlayWidthInPixels(void) +{ + return overlay->pitch / 4; +} + +void sdlemuEnableOverlay(void) +{ + showOverlay = 1; +} + +void sdlemuDisableOverlay(void) +{ + showOverlay = 0; +}