Permalink
Browse files

GLVideoDriver: few bugfixes (thanks fuzzie). Add an error handling du…

…ring shader programs creation.
  • Loading branch information...
BehoIder authored and lynxlynxlynx committed Jan 9, 2014
1 parent 5e1e455 commit b14069549adacbafe0dcf43e360b6cb1aecab37e
@@ -125,7 +125,7 @@ void GLTextureSprite2D::createGlTexture()
if(Bpp == 32) // true color textures
{
int* buffer = new int[Width * Height];
- for(unsigned int i = 0; i < Width*Height; i++)
+ for(int i = 0; i < Width*Height; i++)
{
Uint32 src = ((Uint32*) pixels)[i];
Uint8 r = (src & rMask) >> GetShiftValue(rMask);
@@ -137,12 +137,14 @@ void GLTextureSprite2D::createGlTexture()
buffer[i] = r | (g << 8) | (b << 16) | (a << 24);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
- delete buffer;
+ delete[] buffer;
}
else if(Bpp == 8) // indexed
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, Width, Height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, (GLvoid*) pixels);
}
}
@@ -158,7 +160,7 @@ void GLTextureSprite2D::createGlTextureForPalette()
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Color* colors = new Color[256];
memcpy(colors, currentPalette->col, sizeof(Color)*256);
- for(int i=0; i<256; i++)
+ for(unsigned int i=0; i<256; i++)
{
if(colors[i].a == 0)
{
@@ -168,8 +170,9 @@ void GLTextureSprite2D::createGlTextureForPalette()
colors[i].a = 0;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) colors);
- delete colors;
+ delete[] colors;
}
void GLTextureSprite2D::createGLMaskTexture()
@@ -188,8 +191,9 @@ void GLTextureSprite2D::createGLMaskTexture()
else mask[i] = 0x00;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, Width, Height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, (GLvoid*) mask);
- delete mask;
+ delete[] mask;
}
GLuint GLTextureSprite2D::GetPaletteTexture()
@@ -1,11 +1,15 @@
#ifdef USE_GL
#include <GL/glew.h>
-#pragma comment(lib, "glew32")
-#pragma comment(lib, "opengl32")
+#ifdef _MSC_VER
+ #pragma comment(lib, "glew32")
+ #pragma comment(lib, "opengl32")
+#endif
#else
#include <GLES2/GL2.h>
#include <GLES2/GL2ext.h>
-#pragma comment(lib, "libGLESv2")
+#ifdef _MSC_VER
+ #pragma comment(lib, "libGLESv2")
+#endif
#endif
#include "SDL20GLVideo.h"
#include "Interface.h"
@@ -225,7 +229,7 @@ int GLVideoDriver::CreateDisplay(int w, int h, int bpp, bool fs, const char* tit
#ifdef USE_GL
glewInit();
#endif
- createPrograms();
+ if (!createPrograms()) return GEM_ERROR;
glViewport(0, 0, width, height);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -241,10 +245,16 @@ void GLVideoDriver::useProgram(GLuint program)
lastUsedProgram = program;
}
-void GLVideoDriver::createPrograms()
+bool GLVideoDriver::createPrograms()
{
ShaderOperationResult* result = Shader::BuildProgram(vertex, fragment);
- if(result->Id != 0) program32 = result->Id;
+ if(result->Id == 0)
+ {
+ Log(ERROR, "SDL 2 GL Driver", "can't build shader program:%s", result->Message);
+ delete result;
+ return false;
+ }
+ program32 = result->Id;
delete result;
glUseProgram(program32);
glUniform1i(glGetUniformLocation(program32, "s_texture"), 0);
@@ -254,7 +264,13 @@ void GLVideoDriver::createPrograms()
glUseProgram(0);
result = Shader::BuildProgram(vertex, fragmentPal);
- if(result->Id != 0) programPal = result->Id;
+ if(result->Id == 0)
+ {
+ Log(ERROR, "SDL 2 GL Driver", "can't build shader program:%s", result->Message);
+ delete result;
+ return false;
+ }
+ programPal = result->Id;
delete result;
glUseProgram(programPal);
glUniform1i(glGetUniformLocation(programPal, "s_texture"), 0);
@@ -264,7 +280,13 @@ void GLVideoDriver::createPrograms()
glUseProgram(0);
result = Shader::BuildProgram(vertex, fragmentPalGrayed);
- if(result->Id != 0) programPalGrayed = result->Id;
+ if(result->Id == 0)
+ {
+ Log(ERROR, "SDL 2 GL Driver", "can't build shader program:%s", result->Message);
+ delete result;
+ return false;
+ }
+ programPalGrayed = result->Id;
delete result;
glUseProgram(programPalGrayed);
glUniform1i(glGetUniformLocation(programPalGrayed, "s_texture"), 0);
@@ -274,7 +296,13 @@ void GLVideoDriver::createPrograms()
glUseProgram(0);
result = Shader::BuildProgram(vertex, fragmentPalSepia);
- if(result->Id != 0) programPalSepia = result->Id;
+ if(result->Id == 0)
+ {
+ Log(ERROR, "SDL 2 GL Driver", "can't build shader program:%s", result->Message);
+ delete result;
+ return false;
+ }
+ programPalSepia = result->Id;
delete result;
glUseProgram(programPalSepia);
glUniform1i(glGetUniformLocation(programPalSepia, "s_texture"), 0);
@@ -284,13 +312,20 @@ void GLVideoDriver::createPrograms()
glUseProgram(0);
result = Shader::BuildProgram(vertexRect, fragmentRect);
- if(result->Id != 0) programRect = result->Id;
+ if(result->Id == 0)
+ {
+ Log(ERROR, "SDL 2 GL Driver", "can't build shader program:%s", result->Message);
+ delete result;
+ return false;
+ }
+ programRect = result->Id;
delete result;
glUseProgram(programRect);
glUniformMatrix4fv(glGetUniformLocation(programRect, "u_matrix"), 1, GL_FALSE, matrix);
glUseProgram(0);
lastUsedProgram = 0;
+ return true;
}
Sprite2D* GLVideoDriver::CreateSprite(int w, int h, int bpp, ieDword rMask, ieDword gMask, ieDword bMask, ieDword aMask, void* pixels, bool cK, int index)
@@ -361,26 +396,14 @@ void GLVideoDriver::blitSprite(GLTextureSprite2D* spr, int x, int y, const Regio
if (flags & BLIT_MIRRORX) hflip = !hflip;
if (flags & BLIT_MIRRORY) vflip = !vflip;
GLfloat* textureCoords;
- if (hflip && vflip)
- {
- GLfloat coords[] = { 1.0f,1.0f, 0.0f,1.0f, 1.0f,0.0f, 0.0f,0.0f };
- textureCoords = coords;
- }
- else if (hflip)
- {
- GLfloat coords[] = { 1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f, 0.0f,1.0f };
- textureCoords = coords;
- }
- else if (vflip)
- {
- GLfloat coords[] = { 0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f, 1.0f,0.0f };
- textureCoords = coords;
- }
- else
- {
- GLfloat coords[] = { 0.0f,0.0f, 1.0f,0.0f, 0.0f,1.0f, 1.0f,1.0f };
- textureCoords = coords;
- }
+ GLfloat coordsHV[] = { 1.0f,1.0f, 0.0f,1.0f, 1.0f,0.0f, 0.0f,0.0f };
+ GLfloat coordsH[] = { 1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f, 0.0f,1.0f };
+ GLfloat coordsV[] = { 0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f, 1.0f,0.0f };
+ GLfloat coordsN[] = { 0.0f,0.0f, 1.0f,0.0f, 0.0f,1.0f, 1.0f,1.0f };
+ if (hflip && vflip) textureCoords = coordsHV;
+ else if (hflip) textureCoords = coordsH;
+ else if (vflip) textureCoords = coordsV;
+ else textureCoords = coordsN;
// alpha modifier
GLfloat alphaModifier = flags & BLIT_HALFTRANS ? 0.5f : 1.0f;
@@ -428,6 +451,7 @@ void GLVideoDriver::blitSprite(GLTextureSprite2D* spr, int x, int y, const Regio
if(flags & BLIT_EXTERNAL_MASK) {} // used with external mask
else
{
+ // disable 3rd texture
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, 0);
}
@@ -540,8 +564,9 @@ void GLVideoDriver::BlitGameSprite(const Sprite2D* spr, int x, int y, unsigned i
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, glSprite->Width, glSprite->Height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, (GLvoid*) data);
- delete data;
+ delete[] data;
flags |= BLIT_EXTERNAL_MASK;
}
@@ -591,7 +616,7 @@ void GLVideoDriver::DrawRect(const Region& rgn, const Color& color, bool fill, b
}
}
-void GLVideoDriver::DrawHLine(short x1, short y, short x2, const Color& color, bool clipped)
+void GLVideoDriver::DrawHLine(short x1, short y, short x2, const Color& color, bool /*clipped*/)
{
Region rgn;
rgn.x = x1;
@@ -601,7 +626,7 @@ void GLVideoDriver::DrawHLine(short x1, short y, short x2, const Color& color, b
return drawColoredRect(rgn, color);
}
-void GLVideoDriver::DrawVLine(short x, short y1, short y2, const Color& color, bool clipped)
+void GLVideoDriver::DrawVLine(short x, short y1, short y2, const Color& color, bool /*clipped*/)
{
Region rgn;
rgn.x = x;
@@ -664,6 +689,13 @@ int GLVideoDriver::SwapBuffers()
return val;
}
+void GLVideoDriver::DestroyMovieScreen()
+{
+ SDL20VideoDriver::DestroyMovieScreen();
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+}
+
#include "plugindef.h"
GEMRB_PLUGIN(0xDBAAB50, "SDL Video Driver")
@@ -30,7 +30,7 @@ namespace GemRB
GLuint lastUsedProgram; // stores last used program to prevent switching if possible (switching may cause performance lack)
void useProgram(GLuint program); // use this instead glUseProgram
- void createPrograms();
+ bool createPrograms();
void blitSprite(GLTextureSprite2D* spr, int x, int y, const Region* clip, unsigned int flags = 0, const Color* tint = NULL, GLTextureSprite2D* mask = NULL);
void drawColoredRect(const Region& rgn, const Color& color);
@@ -48,6 +48,7 @@ namespace GemRB
void DrawRect(const Region& rgn, const Color& color, bool fill = true, bool clipped = false);
void DrawHLine(short x1, short y, short x2, const Color& color, bool clipped = false);
void DrawVLine(short x, short y1, short y2, const Color& color, bool clipped = false);
+ void DestroyMovieScreen();
};
}
@@ -24,8 +24,8 @@ ShaderOperationResult* Shader::BuildShader(GLenum type, const char* source)
char tmp[2048];
glGetShaderInfoLog(id, sizeof(tmp), 0, tmp);
opResult->Id = 0;
- //sprintf(tmp, "%s shader compile error: %s", (type == GL_VERTEX_SHADER) ? "Vertex" : "Fragment", tmp);
- opResult->Message = tmp;
+ tmp[strlen(tmp)]='\0';
+ opResult->Message = strdup(tmp);
}
return opResult;
}
@@ -54,7 +54,7 @@ ShaderOperationResult* Shader::BuildProgram(const char* vertexSource, const char
opResult->Id = 0;
glGetProgramInfoLog(id, sizeof(tmp), 0, tmp);
tmp[strlen(tmp)]='\0';
- opResult->Message = tmp;
+ opResult->Message = strdup(tmp);
}
glDeleteShader(vertexShader->Id);

0 comments on commit b140695

Please sign in to comment.