1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-07 00:58:39 +02:00

Error handling in GL2D::makeShaderProgram,

Gfx::PlettedBitmap class correction
This commit is contained in:
paracelsus
2013-03-04 19:43:38 +00:00
parent 672cf5bcb5
commit d96e7cae4a
6 changed files with 75 additions and 62 deletions

View File

@ -232,13 +232,14 @@ void CBitmap32::putWithPlrColor(Point p, ColorRGBA c, float scale)
CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[]) : CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[]) :
CImage(w, h), CImage(w, h),
palette(pal) palette(pal),
realWidth((w + 3) & ~3),
realHeight(h)
{ {
const ui32 rowStride = (w + 3) & ~3; const ui32 size = realWidth * h;
const ui32 size = rowStride * h;
buffer = new ui8[size]; buffer = new ui8[size];
if (rowStride == w) if (realWidth == w)
{ {
memcpy(buffer, pixBuff, size); memcpy(buffer, pixBuff, size);
return; return;
@ -246,19 +247,19 @@ CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pi
for (ui32 y=0; y<h; ++y) for (ui32 y=0; y<h; ++y)
{ {
memset(&buffer[rowStride*(y+1)-4], 0, 4); memcpy(&buffer[realWidth*y], &pixBuff[w*y], w);
memcpy(&buffer[rowStride*y], &pixBuff[w*y], w);
} }
width = rowStride; realWidth = w;
} }
CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) : CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) :
CImage(w, h), CImage(w, h),
palette(pal) palette(pal),
realWidth((w + 3) & ~3),
realHeight(h)
{ {
const ui32 rowStride = (w + 3) & ~3; buffer = new ui8[realWidth * h];
buffer = new ui8[rowStride * h];
switch (format) switch (format)
{ {
@ -269,7 +270,7 @@ CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pi
for (ui32 y=0; y<h; ++y) for (ui32 y=0; y<h; ++y)
{ {
const ui8* srcRowPtr = pixBuff + SDL_SwapLE32(rowsOffsets[y]); const ui8* srcRowPtr = pixBuff + SDL_SwapLE32(rowsOffsets[y]);
ui8* dstRowPtr = buffer + (y * rowStride); ui8* dstRowPtr = buffer + (y * realWidth);
ui32 rowLength = 0; ui32 rowLength = 0;
do { do {
@ -290,6 +291,8 @@ CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pi
} }
while (rowLength < w); while (rowLength < w);
} }
realWidth = w;
return; return;
} }
default: default:
@ -307,7 +310,7 @@ CPalettedBitmap::~CPalettedBitmap()
void CPalettedBitmap::textureTransfer() void CPalettedBitmap::textureTransfer()
{ {
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R8UI, width, height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, buffer); glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R8UI, realWidth, realHeight, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, buffer);
palette.loadToVideoRAM(); palette.loadToVideoRAM();
} }
@ -318,7 +321,7 @@ void CPalettedBitmap::putAt(Point p)
GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle()); GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle); GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
GL2D::usePaletteBitmapShader(p.x, p.y); GL2D::usePaletteBitmapShader(p.x, p.y);
glRecti(p.x, p.y, p.x + width, p.y + height); glRecti(p.x, p.y, p.x + realWidth, p.y + realHeight);
} }
@ -334,7 +337,7 @@ void CPalettedBitmap::putAt(Point p, TransformFlags flags, float scale)
GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle()); GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle); GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
GL2D::usePaletteBitmapShader(p.x, p.y); GL2D::usePaletteBitmapShader(p.x, p.y);
glRecti(p.x, p.y, p.x + (ui32)(width*scale), p.y + (ui32)(height*scale)); glRecti(p.x, p.y, p.x + (ui32)(realWidth*scale), p.y + (ui32)(realHeight*scale));
} }
@ -371,26 +374,17 @@ void CPalettedBitmap::putWithPlrColor(Point p, ColorRGBA c, float scale)
CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih, CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
CPaletteRGBA& pal, const ui8 pixBuff[]) : CPaletteRGBA& pal, const ui8 pixBuff[]) :
CPalettedBitmap(iw, ih, pal, pixBuff), CPalettedBitmap(iw, ih, pal, pixBuff),
leftMargin(lm), topMargin(tm), leftMargin(lm), topMargin(tm)
intWidth(iw), intHeight(ih)
{ {
width = fw; width = fw;
height = fh; height = fh;
} }
void CPalBitmapWithMargin::textureTransfer()
{
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R8UI, intWidth, intHeight, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, buffer);
palette.loadToVideoRAM();
}
CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih, CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) : CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) :
CPalettedBitmap(iw, ih, pal, pixBuff, format), CPalettedBitmap(iw, ih, pal, pixBuff, format),
leftMargin(lm), topMargin(tm), leftMargin(lm), topMargin(tm)
intWidth(iw), intHeight(ih)
{ {
width = fw; width = fw;
height = fh; height = fh;
@ -402,8 +396,8 @@ void CPalBitmapWithMargin::putAt(Point p)
loadToVideoRAM(); loadToVideoRAM();
GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle()); GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle); GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
GL2D::usePaletteBitmapShader(p.x, p.y); GL2D::usePaletteBitmapShader(p.x + leftMargin, p.y + topMargin);
glRecti(p.x, p.y, p.x + intWidth, p.y + intHeight); glRecti(p.x + leftMargin, p.y + topMargin, p.x + realWidth, p.y + realHeight);
} }
@ -430,8 +424,8 @@ void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, const ColorMatri
loadToVideoRAM(); loadToVideoRAM();
GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle()); GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle); GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
GL2D::usePaletteBitmapShader(p.x, p.y, cm); GL2D::usePaletteBitmapShader(p.x + leftMargin, p.y + topMargin);
glRecti(p.x, p.y, p.x + width, p.y + height); glRecti(p.x + leftMargin, p.y + topMargin, p.x + realWidth, p.y + realHeight);
} }

View File

@ -96,8 +96,8 @@ class CBitmap32 : public CImage
}; };
protected: protected:
CBitmap32(ui32 w, ui32 h, const ColorRGB pixBuff[], bool bgra = true); // 24bit RGB source CBitmap32(ui32 w, ui32 h, const ColorRGB pixBuff[], bool bgra); // 24bit RGB source
CBitmap32(ui32 w, ui32 h, const ColorRGBA pixBuff[], bool bgra = true); // 32bit RGBA source CBitmap32(ui32 w, ui32 h, const ColorRGBA pixBuff[], bool bgra); // 32bit RGBA source
virtual void textureTransfer(); virtual void textureTransfer();
public: public:
@ -120,6 +120,8 @@ class CPalettedBitmap : public CImage
protected: protected:
CPaletteRGBA& palette; CPaletteRGBA& palette;
ui8* buffer; ui8* buffer;
ui32 realWidth;
ui32 realHeight;
CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[]); CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[]);
CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format); CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format);
@ -143,16 +145,12 @@ class CPalBitmapWithMargin : public CPalettedBitmap
friend class CImage; friend class CImage;
ui32 leftMargin; ui32 leftMargin;
ui32 topMargin; ui32 topMargin;
ui32 intWidth;
ui32 intHeight;
protected: protected:
CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih, CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
CPaletteRGBA& pal, const ui8 pixBuff[]); CPaletteRGBA& pal, const ui8 pixBuff[]);
CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih, CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format); CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format);
virtual void textureTransfer();
public: public:
virtual void putAt(Point p); virtual void putAt(Point p);
virtual void putAt(Point p, TransformFlags flags); virtual void putAt(Point p, TransformFlags flags);

View File

@ -67,7 +67,7 @@ CImage * CImage::makeFromPCX(const SH3PcxFile& pcx, size_t fileSize)
{ {
if (H3PCX_HEADER_SIZE + imgSize > fileSize) return nullptr; if (H3PCX_HEADER_SIZE + imgSize > fileSize) return nullptr;
return new CBitmap32(width, height, (ColorRGB*)pcx.data); return new CBitmap32(width, height, (ColorRGB*)pcx.data, true);
} }
return nullptr; return nullptr;

View File

@ -50,7 +50,7 @@ PImage CManager::getImage(const std::string& fname)
if (img_tmp == nullptr) if (img_tmp == nullptr)
{ {
tlog1 << "Image " << fname << " not loaded!\n"; tlog1 << "Gfx: Image " << fname << " not loaded!\n";
return nullptr; return nullptr;
} }
} }
@ -80,7 +80,7 @@ PAnimation CManager::getAnimation(const std::string& fname)
CAnimation* anim_tmp = CAnimation::makeFromDEF(*(SH3DefFile*)data.get(), (size_t)readSize); CAnimation* anim_tmp = CAnimation::makeFromDEF(*(SH3DefFile*)data.get(), (size_t)readSize);
if (anim_tmp == nullptr) if (anim_tmp == nullptr)
{ {
tlog1 << "Animation " << fname << " not loaded!\n"; tlog1 << "Gfx: Animation " << fname << " not loaded!\n";
return nullptr; return nullptr;
} }

View File

@ -18,10 +18,13 @@ PFNGLACTIVETEXTUREPROC glActiveTexture;
// OpenGL 2.0 functions pointers // OpenGL 2.0 functions pointers
PFNGLCREATEPROGRAMPROC glCreateProgram; PFNGLCREATEPROGRAMPROC glCreateProgram;
PFNGLCREATESHADERPROC glCreateShader; PFNGLCREATESHADERPROC glCreateShader;
PFNGLDELETESHADERPROC glDeleteShader;
PFNGLSHADERSOURCEPROC glShaderSource; PFNGLSHADERSOURCEPROC glShaderSource;
PFNGLCOMPILESHADERPROC glCompileShader; PFNGLCOMPILESHADERPROC glCompileShader;
PFNGLGETSHADERIVPROC glGetShaderiv;
PFNGLATTACHSHADERPROC glAttachShader; PFNGLATTACHSHADERPROC glAttachShader;
PFNGLLINKPROGRAMPROC glLinkProgram; PFNGLLINKPROGRAMPROC glLinkProgram;
PFNGLGETPROGRAMIVPROC glGetProgramiv;
PFNGLUSEPROGRAMPROC glUseProgram; PFNGLUSEPROGRAMPROC glUseProgram;
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation; PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
PFNGLUNIFORM1IPROC glUniform1i; PFNGLUNIFORM1IPROC glUniform1i;
@ -54,7 +57,7 @@ static GLint coord_uniform = -1;
// Print out the information log for a shader object // Print out the information log for a shader object
void printInfoLog(PFNGLGETPROGRAMINFOLOGPROC logFunc, GLuint object) void printInfoLog(PFNGLGETPROGRAMINFOLOGPROC logFunc, GLuint object, GLint status)
{ {
if (logFunc != nullptr) if (logFunc != nullptr)
{ {
@ -62,7 +65,7 @@ void printInfoLog(PFNGLGETPROGRAMINFOLOGPROC logFunc, GLuint object)
GLchar infoLog[1024]; GLchar infoLog[1024];
logFunc(object, sizeof(infoLog)-1, &infoLogLength, infoLog); logFunc(object, sizeof(infoLog)-1, &infoLogLength, infoLog);
if (infoLogLength > 0) tlog1 << infoLog; if (infoLogLength > 0) (status ? tlog0 : tlog1) << infoLog;
} }
} }
@ -71,21 +74,31 @@ GLuint makeShaderProgram(const char * frg_src)
{ {
// Creating a fragment shader object // Creating a fragment shader object
GLuint shader_object = glCreateShader(GL_FRAGMENT_SHADER); GLuint shader_object = glCreateShader(GL_FRAGMENT_SHADER);
if (shader_object == 0) return 0;
glShaderSource(shader_object, 1, &frg_src, nullptr); // assigning the shader source glShaderSource(shader_object, 1, &frg_src, nullptr); // assigning the shader source
// Compiling the shader glCompileShader(shader_object); // Compiling the shader
glCompileShader(shader_object);
printInfoLog(glGetShaderInfoLog, shader_object);
// Creating a program object GLint ret;
GLuint program_object = glCreateProgram(); glGetShaderiv(shader_object, GL_COMPILE_STATUS, &ret);
// Attaching the shader into program printInfoLog(glGetShaderInfoLog, shader_object, ret);
glAttachShader(program_object, shader_object);
// Link the shaders into a complete GLSL program. if (ret == GL_TRUE)
glLinkProgram(program_object); {
printInfoLog(glGetProgramInfoLog, program_object); GLuint program_object = glCreateProgram(); // Creating a program object
glAttachShader(program_object, shader_object); // Attaching the shader into program
return program_object; // Link the shaders into a complete GLSL program.
glLinkProgram(program_object);
glGetProgramiv(shader_object, GL_LINK_STATUS, &ret);
printInfoLog(glGetProgramInfoLog, program_object, ret);
if (ret == GL_TRUE) return program_object;
}
glDeleteShader(shader_object);
return 0;
} }
@ -104,17 +117,20 @@ void initVideo(ui32 w, ui32 h, bool fullscreen)
SDL_GL_SwapBuffers(); SDL_GL_SwapBuffers();
// Dynamic-linked OpenGL 1.3 and 2.0 functions - required to 2D rendeing // Dynamic-linked OpenGL 1.3 and 2.0 functions - required to 2D rendeing
if ( (glActiveTexture = (PFNGLACTIVETEXTUREPROC) SDL_GL_GetProcAddress("glActiveTexture")) == nullptr if ( (glActiveTexture = (PFNGLACTIVETEXTUREPROC) SDL_GL_GetProcAddress("glActiveTexture")) == nullptr
|| (glCreateProgram = (PFNGLCREATEPROGRAMPROC) SDL_GL_GetProcAddress("glCreateProgram")) == nullptr || (glCreateProgram = (PFNGLCREATEPROGRAMPROC) SDL_GL_GetProcAddress("glCreateProgram")) == nullptr
|| (glCreateShader = (PFNGLCREATESHADERPROC) SDL_GL_GetProcAddress("glCreateShader")) == nullptr || (glCreateShader = (PFNGLCREATESHADERPROC) SDL_GL_GetProcAddress("glCreateShader")) == nullptr
|| (glShaderSource = (PFNGLSHADERSOURCEPROC) SDL_GL_GetProcAddress("glShaderSource")) == nullptr || (glDeleteShader = (PFNGLDELETESHADERPROC) SDL_GL_GetProcAddress("glDeleteShader")) == nullptr
|| (glCompileShader = (PFNGLCOMPILESHADERPROC) SDL_GL_GetProcAddress("glCompileShader")) == nullptr || (glShaderSource = (PFNGLSHADERSOURCEPROC) SDL_GL_GetProcAddress("glShaderSource")) == nullptr
|| (glAttachShader = (PFNGLATTACHSHADERPROC) SDL_GL_GetProcAddress("glAttachShader")) == nullptr || (glCompileShader = (PFNGLCOMPILESHADERPROC) SDL_GL_GetProcAddress("glCompileShader")) == nullptr
|| (glLinkProgram = (PFNGLLINKPROGRAMPROC) SDL_GL_GetProcAddress("glLinkProgram")) == nullptr || (glGetShaderiv = (PFNGLGETSHADERIVPROC) SDL_GL_GetProcAddress("glGetShaderiv")) == nullptr
|| (glUseProgram = (PFNGLUSEPROGRAMPROC) SDL_GL_GetProcAddress("glUseProgram")) == nullptr || (glAttachShader = (PFNGLATTACHSHADERPROC) SDL_GL_GetProcAddress("glAttachShader")) == nullptr
|| (glLinkProgram = (PFNGLLINKPROGRAMPROC) SDL_GL_GetProcAddress("glLinkProgram")) == nullptr
|| (glGetProgramiv = (PFNGLGETPROGRAMIVPROC) SDL_GL_GetProcAddress("glGetProgramiv")) == nullptr
|| (glUseProgram = (PFNGLUSEPROGRAMPROC) SDL_GL_GetProcAddress("glUseProgram")) == nullptr
|| (glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) SDL_GL_GetProcAddress("glGetUniformLocation")) == nullptr || (glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) SDL_GL_GetProcAddress("glGetUniformLocation")) == nullptr
|| (glUniform1i = (PFNGLUNIFORM1IPROC) SDL_GL_GetProcAddress("glUniform1i")) == nullptr || (glUniform1i = (PFNGLUNIFORM1IPROC) SDL_GL_GetProcAddress("glUniform1i")) == nullptr
|| (glUniform2i = (PFNGLUNIFORM2IPROC) SDL_GL_GetProcAddress("glUniform2i")) == nullptr || (glUniform2i = (PFNGLUNIFORM2IPROC) SDL_GL_GetProcAddress("glUniform2i")) == nullptr
) )
{ {
tlog1 << "Error: OpenGL2 Extenstions are not available\n"; tlog1 << "Error: OpenGL2 Extenstions are not available\n";
@ -133,6 +149,11 @@ void initVideo(ui32 w, ui32 h, bool fullscreen)
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
paletteBitmapProgram = makeShaderProgram(frag_palette_bitmap); paletteBitmapProgram = makeShaderProgram(frag_palette_bitmap);
if (paletteBitmapProgram == 0)
{
throw std::runtime_error("OpenGL shader for palleted bitmaps is not compiled\n");
}
GLint bitmap_uniform = glGetUniformLocation(paletteBitmapProgram, "bitmap"); GLint bitmap_uniform = glGetUniformLocation(paletteBitmapProgram, "bitmap");
GLint palette_uniform = glGetUniformLocation(paletteBitmapProgram, "palette"); GLint palette_uniform = glGetUniformLocation(paletteBitmapProgram, "palette");
coord_uniform = glGetUniformLocation(paletteBitmapProgram, "coordOffs"); coord_uniform = glGetUniformLocation(paletteBitmapProgram, "coordOffs");

View File

@ -2159,7 +2159,7 @@
"everflowingCrystalCloak", "everflowingCrystalCloak",
"ringOfInfiniteGems", "ringOfInfiniteGems",
"everpouringVialOfMercury", "everpouringVialOfMercury",
"inexhaustibleCartOfOre" "eversmokingRingOfSulfur"
] ]
}, },
"magicWand": "magicWand":