bool restoreSnapshot (FILE * fp) { unsigned int picWidth = get2bytes (fp); unsigned int picHeight = get2bytes (fp); if ((picWidth != winWidth) || (picHeight != winHeight)) return false; unsigned int t1, t2, n; unsigned short c; GLubyte * target; if (! NPOT_textures) { picWidth = getNextPOT(picWidth); picHeight = getNextPOT(picHeight); snapTexW = ((double)winWidth) / picWidth; snapTexH = ((double)winHeight) / picHeight; } GLubyte * snapshotTexture = new GLubyte [picHeight*picWidth*4]; if (! snapshotTexture) return fatal("Out of memory while restoring snapshot."); for (t2 = 0; t2 < winHeight; t2 ++) { t1 = 0; while (t1 < winWidth) { c = (unsigned short) get2bytes (fp); if (c & 32) { n = fgetc (fp) + 1; c -= 32; } else { n = 1; } while (n --) { target = snapshotTexture + 4*picWidth*t2 + t1*4; target[0] = (GLubyte) redValue(c); target[1] = (GLubyte) greenValue(c); target[2] = (GLubyte) blueValue(c); target[3] = (GLubyte) 255; t1++; } } } if (! snapshotTextureName) glGenTextures (1, &snapshotTextureName); glBindTexture(GL_TEXTURE_2D, snapshotTextureName); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, picWidth, picHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, snapshotTexture, snapshotTextureName); delete snapshotTexture; snapshotTexture = NULL; return true; }
bool loadHSI (FILE * fp, int x, int y, bool reserve) { int t1, t2, n; unsigned short c; GLubyte * target; int32_t transCol = reserve ? -1 : 63519; int picWidth; int picHeight; int realPicWidth, realPicHeight; long file_pointer = ftell (fp); png_structp png_ptr; png_infop info_ptr, end_info; int fileIsPNG = true; // Is this a PNG file? char tmp[10]; size_t bytes_read = fread(tmp, 1, 8, fp); if (bytes_read != 8 && ferror (fp)) { debugOut("Reading error in loadHSI.\n"); } if (png_sig_cmp((png_byte *) tmp, 0, 8)) { // No, it's old-school HSI fileIsPNG = false; fseek(fp, file_pointer, SEEK_SET); picWidth = realPicWidth = get2bytes (fp); picHeight = realPicHeight = get2bytes (fp); } else { // Read the PNG header png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { return false; } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); return false; } end_info = png_create_info_struct(png_ptr); if (!end_info) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); return false; } png_init_io(png_ptr, fp); // Tell libpng which file to read png_set_sig_bytes(png_ptr, 8); // 8 bytes already read png_read_info(png_ptr, info_ptr); png_uint_32 width, height; int bit_depth, color_type, interlace_type, compression_type, filter_method; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); picWidth = realPicWidth = width; picHeight = realPicHeight = height; if (bit_depth < 8) png_set_packing(png_ptr); png_set_expand(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr); if (bit_depth == 16) png_set_strip_16(png_ptr); png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER); png_read_update_info(png_ptr, info_ptr); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); //int rowbytes = png_get_rowbytes(png_ptr, info_ptr); } GLfloat texCoordW = 1.0; GLfloat texCoordH = 1.0; if (! NPOT_textures) { picWidth = getNextPOT(picWidth); picHeight = getNextPOT(picHeight); texCoordW = ((double)realPicWidth) / picWidth; texCoordH = ((double)realPicHeight) / picHeight; } if (reserve) { if (! resizeBackdrop (realPicWidth, realPicHeight)) return false; } if (x == IN_THE_CENTRE) x = (sceneWidth - realPicWidth) >> 1; if (y == IN_THE_CENTRE) y = (sceneHeight - realPicHeight) >> 1; if (x < 0 || x + realPicWidth > sceneWidth || y < 0 || y + realPicHeight > sceneHeight) return false; if (fileIsPNG) { unsigned char * row_pointers[realPicHeight]; for (int i = 0; i<realPicHeight; i++) row_pointers[i] = backdropTexture + 4*i*picWidth; png_read_image(png_ptr, (png_byte **) row_pointers); png_read_end(png_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); } else { for (t2 = 0; t2 < realPicHeight; t2 ++) { t1 = 0; while (t1 < realPicWidth) { c = (unsigned short) get2bytes (fp); if (c & 32) { n = fgetc (fp) + 1; c -= 32; } else { n = 1; } while (n --) { target = backdropTexture + 4*picWidth*t2 + t1*4; if (c == transCol || c == 2015) { target[0] = (GLubyte) 0; target[1] = (GLubyte) 0; target[2] = (GLubyte) 0; target[3] = (GLubyte) 0; } else { target[0] = (GLubyte) redValue(c); target[1] = (GLubyte) greenValue(c); target[2] = (GLubyte) blueValue(c); target[3] = (GLubyte) 255; } t1++; } } } } GLuint tmpTex; glGenTextures (1, &tmpTex); glBindTexture(GL_TEXTURE_2D, tmpTex); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); if (gameSettings.antiAlias < 0) { glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } else { glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); } texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, picWidth, picHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, backdropTexture, tmpTex); //glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); float btx1; float btx2; float bty1; float bty2; if (! NPOT_textures) { btx1 = backdropTexW * x / sceneWidth; btx2 = backdropTexW * (x+realPicWidth) / sceneWidth; bty1 = backdropTexH * y / sceneHeight; bty2 = backdropTexH * (y+realPicHeight) / sceneHeight; } else { btx1 = (float) x / sceneWidth; btx2 = (float) (x+realPicWidth) / sceneWidth; bty1 = (float) y / sceneHeight; bty2 = (float) (y+realPicHeight) / sceneHeight; } const GLfloat btexCoords[] = { btx1, bty1, btx2, bty1, btx1, bty2, btx2, bty2 }; setPixelCoords (true); int xoffset = 0; while (xoffset < realPicWidth) { int w = (realPicWidth-xoffset < viewportWidth) ? realPicWidth-xoffset : viewportWidth; int yoffset = 0; while (yoffset < realPicHeight) { int h = (realPicHeight-yoffset < viewportHeight) ? realPicHeight-yoffset : viewportHeight; glClear(GL_COLOR_BUFFER_BIT); // Clear The Screen const GLfloat vertices[] = { (GLfloat)-xoffset, (GLfloat)-yoffset, 0., (GLfloat)realPicWidth-xoffset, (GLfloat)-yoffset, 0., (GLfloat)-xoffset, (GLfloat)-yoffset+realPicHeight, 0., (GLfloat)realPicWidth-xoffset, (GLfloat)-yoffset+realPicHeight, 0. }; const GLfloat texCoords[] = { 0.0f, 0.0f, texCoordW, 0.0f, 0.0f, texCoordH, texCoordW, texCoordH }; if (backdropExists) { // Render the sprite to the backdrop // (using mulitexturing, so the old backdrop is seen where alpha < 1.0) glActiveTexture(GL_TEXTURE2); glBindTexture (GL_TEXTURE_2D, backdropTextureName); glActiveTexture(GL_TEXTURE0); glUseProgram(shader.paste); GLint uniform = glGetUniformLocation(shader.paste, "useLightTexture"); if (uniform >= 0) glUniform1i(uniform, 0); // No lighting setPMVMatrix(shader.paste); setPrimaryColor(1.0, 1.0, 1.0, 1.0); glBindTexture(GL_TEXTURE_2D, tmpTex); //glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); drawQuad(shader.paste, vertices, 3, texCoords, NULL, btexCoords); glUseProgram(0); } else { // It's all new - nothing special to be done. glUseProgram(shader.texture); setPMVMatrix(shader.texture); glBindTexture(GL_TEXTURE_2D, tmpTex); setPrimaryColor(1.0, 0.0, 0.0, 0.0); drawQuad(shader.texture, vertices, 1, texCoords); glUseProgram(0); } // Copy Our ViewPort To The Texture copyTexSubImage2D(GL_TEXTURE_2D, 0, x+xoffset, y+yoffset, viewportOffsetX, viewportOffsetY, w, h, backdropTextureName); yoffset += viewportHeight; } xoffset += viewportWidth; } deleteTextures(1, &tmpTex); setPixelCoords (false); backdropExists = true; return true; }
bool loadParallax (unsigned short v, unsigned short fracX, unsigned short fracY) { setResourceForFatal (v); if (! openFileFromNum (v)) return fatal ("Can't open parallax image"); parallaxLayer * nP = new parallaxLayer; if (! checkNew (nP)) return false; nP -> next = parallaxStuff; parallaxStuff = nP; if (nP -> next) { nP -> next -> prev = nP; } nP -> prev = NULL; int picWidth; int picHeight; long file_pointer = ftell (bigDataFile); png_structp png_ptr; png_infop info_ptr, end_info; int fileIsPNG = true; // Is this a PNG file? char tmp[10]; size_t bytes_read = fread(tmp, 1, 8, bigDataFile); if (bytes_read != 8 && ferror (bigDataFile)) { debugOut("Reading error in loadParallax.\n"); } if (png_sig_cmp((png_byte *) tmp, 0, 8)) { // No, it's old-school HSI fileIsPNG = false; fseek(bigDataFile, file_pointer, SEEK_SET); picWidth = nP -> width = get2bytes (bigDataFile); picHeight = nP -> height = get2bytes (bigDataFile); } else { // Read the PNG header png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { return false; } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); return false; } end_info = png_create_info_struct(png_ptr); if (!end_info) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); return false; } png_init_io(png_ptr, bigDataFile); // Tell libpng which file to read png_set_sig_bytes(png_ptr, 8); // 8 bytes already read png_read_info(png_ptr, info_ptr); png_uint_32 width, height; int bit_depth, color_type, interlace_type, compression_type, filter_method; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); picWidth = nP -> width = width; picHeight = nP -> height = height; if (bit_depth < 8) png_set_packing(png_ptr); png_set_expand(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr); if (bit_depth == 16) png_set_strip_16(png_ptr); png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER); png_read_update_info(png_ptr, info_ptr); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); //int rowbytes = png_get_rowbytes(png_ptr, info_ptr); } if (! NPOT_textures) { picWidth = getNextPOT(picWidth); picHeight = getNextPOT(picHeight); } nP -> fileNum = v; nP -> fractionX = fracX; nP -> fractionY = fracY; if (fracX == 65535) { nP -> wrapS = false; if (nP -> width < winWidth) { fatal ("For AUTOFIT parallax backgrounds, the image must be at least as wide as the game window/screen."); return false; } } else { nP -> wrapS = true; } if (fracY == 65535) { nP -> wrapT = false; if (nP -> height < winHeight) { fatal ("For AUTOFIT parallax backgrounds, the image must be at least as tall as the game window/screen."); return false; } } else { nP -> wrapT = true; } nP -> texture = new GLubyte [picHeight * picWidth * 4]; if (! checkNew (nP -> texture)) return false; if (fileIsPNG) { unsigned char * row_pointers[nP -> height]; for (int i = 0; i < nP -> height; i++) row_pointers[i] = nP -> texture + 4*i*picWidth; png_read_image(png_ptr, (png_byte **) row_pointers); png_read_end(png_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); } else { int t1, t2, n; unsigned short c; GLubyte * target; for (t2 = 0; t2 < nP -> height; t2 ++) { t1 = 0; while (t1 < nP -> width) { c = (unsigned short) get2bytes (bigDataFile); if (c & 32) { n = fgetc (bigDataFile) + 1; c -= 32; } else { n = 1; } while (n--) { target = nP -> texture + 4*picWidth*t2 + t1*4; if (c == 63519 || c == 2015) { target[0] = (GLubyte) 0; target[1] = (GLubyte) 0; target[2] = (GLubyte) 0; target[3] = (GLubyte) 0; } else { target[0] = (GLubyte) redValue(c); target[1] = (GLubyte) greenValue(c); target[2] = (GLubyte) blueValue(c); target[3] = (GLubyte) 255; } t1 ++; } } } } glGenTextures (1, &nP->textureName); glBindTexture (GL_TEXTURE_2D, nP->textureName); if (nP -> wrapS) glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); else glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); if (nP -> wrapT) glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); else glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); if (gameSettings.antiAlias < 0) { glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } else { glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); } texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, picWidth, picHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nP->texture, nP->textureName); finishAccess (); setResourceForFatal (-1); return true; }
bool loadLightMap (int v) { int newPicWidth, newPicHeight; setResourceForFatal (v); if (! openFileFromNum (v)) return fatal ("Can't open light map."); long file_pointer = ftell (bigDataFile); png_structp png_ptr; png_infop info_ptr, end_info; int fileIsPNG = true; // Is this a PNG file? char tmp[10]; size_t bytes_read = fread(tmp, 1, 8, bigDataFile); if (bytes_read != 8 && ferror (bigDataFile)) { debugOut("Reading error in loadLightMap.\n"); } if (png_sig_cmp((png_byte *) tmp, 0, 8)) { // No, it's old-school HSI fileIsPNG = false; fseek(bigDataFile, file_pointer, SEEK_SET); newPicWidth = lightMap.w = get2bytes (bigDataFile); newPicHeight = lightMap.h = get2bytes (bigDataFile); } else { // Read the PNG header png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { return false; } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); return false; } end_info = png_create_info_struct(png_ptr); if (!end_info) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); return false; } png_init_io(png_ptr, bigDataFile); // Tell libpng which file to read png_set_sig_bytes(png_ptr, 8); // 8 bytes already read png_read_info(png_ptr, info_ptr); png_uint_32 width, height; int bit_depth, color_type, interlace_type, compression_type, filter_method; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); newPicWidth = lightMap.w = width; newPicHeight = lightMap.h = height; if (bit_depth < 8) png_set_packing(png_ptr); png_set_expand(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr); if (bit_depth == 16) png_set_strip_16(png_ptr); png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER); png_read_update_info(png_ptr, info_ptr); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); //int rowbytes = png_get_rowbytes(png_ptr, info_ptr); } if (lightMapMode == LIGHTMAPMODE_HOTSPOT) { if (lightMap.w != sceneWidth || lightMap.h != sceneHeight) { return fatal ("Light map width and height don't match scene width and height. That is required for lightmaps in HOTSPOT mode."); } } if (! NPOT_textures) { newPicWidth = getNextPOT(lightMap.w); newPicHeight = getNextPOT(lightMap.h); lightMap.texW = (double) lightMap.w / newPicWidth; lightMap.texH = (double) lightMap.h / newPicHeight; } else { lightMap.texW = 1.0; lightMap.texH = 1.0; } killLightMap (); lightMapNumber = v; glPixelStorei (GL_UNPACK_ALIGNMENT, 1); if (lightMap.data) delete [] lightMap.data; lightMap.data = new GLubyte [newPicWidth*newPicHeight*4]; if (! lightMap.data) { return fatal ("Out of memory loading light map."); } int t1, t2, n; unsigned short c; GLubyte * target; if (fileIsPNG) { unsigned char * row_pointers[lightMap.h]; for (int i = 0; i<lightMap.h; i++) row_pointers[i] = lightMap.data + 4*i*newPicWidth; png_read_image(png_ptr, (png_byte **) row_pointers); png_read_end(png_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); } else { for (t2 = 0; t2 < lightMap.h; t2 ++) { t1 = 0; while (t1 < lightMap.w) { c = (unsigned short) get2bytes (bigDataFile); if (c & 32) { n = fgetc (bigDataFile) + 1; c -= 32; } else { n = 1; } while (n --) { target = lightMap.data + 4*newPicWidth*t2 + t1*4; target[0] = (GLubyte) redValue(c); target[1] = (GLubyte) greenValue(c); target[2] = (GLubyte) blueValue(c); target[3] = (GLubyte) 255; t1++; } } } } if (! lightMap.name) glGenTextures (1, &lightMap.name); glBindTexture(GL_TEXTURE_2D, lightMap.name); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, newPicWidth, newPicHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightMap.data, lightMap.name); finishAccess (); setResourceForFatal (-1); return true; }
void blankScreen (int x1, int y1, int x2, int y2) { if (y1 < 0) y1 = 0; if (x1 < 0) x1 = 0; if (x2 > (int) sceneWidth) x2 = (int)sceneWidth; if (y2 > (int) sceneHeight) y2 = (int)sceneHeight; int picWidth = x2-x1; int picHeight = y2-y1; setPixelCoords (true); int xoffset = 0; while (xoffset < picWidth) { int w = (picWidth-xoffset < viewportWidth) ? picWidth-xoffset : viewportWidth; int yoffset = 0; while (yoffset < picHeight) { int h = (picHeight-yoffset < viewportHeight) ? picHeight-yoffset : viewportHeight; // Render the scene const GLfloat vertices[] = { -10.325f, -1.325f, 0.0f, w+1.325f, -1.325f, 0.0f, -10.325f, h+1.325f, 0.0f, w+1.325f, h+1.325f, 0.0f }; glUseProgram(shader.color); setPMVMatrix(shader.color); setPrimaryColor(redValue(currentBlankColour)/255.0f, greenValue(currentBlankColour)/255.0f, blueValue(currentBlankColour)/255.0f, 1.0f); drawQuad(shader.color, vertices, 0); glUseProgram(0); // Copy Our ViewPort To The Texture copyTexSubImage2D(GL_TEXTURE_2D, 0, x1+xoffset, y1+yoffset, viewportOffsetX, viewportOffsetY, w, h, backdropTextureName); yoffset += viewportHeight; } xoffset += viewportWidth; } setPixelCoords (false); }
bool initSludge (char * filename) { int a = 0; mouseCursorAnim = makeNullAnim (); FILE * fp = openAndVerify (filename, 'G', 'E', ERROR_BAD_HEADER, gameVersion); if (! fp) return false; if (fgetc (fp)) { numBIFNames = get2bytes (fp); allBIFNames = new char * [numBIFNames]; if (! checkNew (allBIFNames)) return false; for (int fn = 0; fn < numBIFNames; fn ++) { allBIFNames[fn] = readString (fp); } numUserFunc = get2bytes (fp); allUserFunc = new char * [numUserFunc]; if (! checkNew (allUserFunc)) return false; for (int fn = 0; fn < numUserFunc; fn ++) { allUserFunc[fn] = readString (fp); } if (gameVersion >= VERSION(1,3)) { numResourceNames = get2bytes (fp); allResourceNames = new char * [numResourceNames]; if (! checkNew (allResourceNames)) return false; for (int fn = 0; fn < numResourceNames; fn ++) { allResourceNames[fn] = readString (fp); } } } winWidth = get2bytes (fp); winHeight = get2bytes (fp); specialSettings = fgetc (fp); desiredfps = 1000/fgetc (fp); delete[] readString (fp); // Unused - was used for registration purposes. size_t bytes_read = fread (& fileTime, sizeof (FILETIME), 1, fp); if (bytes_read != sizeof (FILETIME) && ferror (fp)) { debugOut("Reading error in initSludge.\n"); } char * dataFol = (gameVersion >= VERSION(1,3)) ? readString(fp) : joinStrings ("", ""); gameSettings.numLanguages = (gameVersion >= VERSION(1,3)) ? (fgetc (fp)) : 0; makeLanguageTable (fp); if (gameVersion >= VERSION(1,6)) { fgetc(fp); // aaLoad fgetc (fp); getFloat (fp); getFloat (fp); } char * checker = readString (fp); if (strcmp (checker, "okSoFar")) return fatal (ERROR_BAD_HEADER, filename); delete checker; checker = NULL; unsigned char customIconLogo = fgetc (fp); if (customIconLogo & 1) { // There is an icon - read it! int n; long file_pointer = ftell (fp); png_structp png_ptr; png_infop info_ptr, end_info; int fileIsPNG = true; // Is this a PNG file? char tmp[10]; bytes_read = fread(tmp, 1, 8, fp); if (bytes_read != 8 && ferror (fp)) { debugOut("Reading error in initSludge.\n"); } if (png_sig_cmp((png_byte *) tmp, 0, 8)) { // No, it's old-school HSI fileIsPNG = false; fseek(fp, file_pointer, SEEK_SET); iconW = get2bytes (fp); iconH = get2bytes (fp); } else { // Read the PNG header png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { return false; } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); return false; } end_info = png_create_info_struct(png_ptr); if (!end_info) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); return false; } png_init_io(png_ptr, fp); // Tell libpng which file to read png_set_sig_bytes(png_ptr, 8); // 8 bytes already read png_read_info(png_ptr, info_ptr); png_uint_32 width, height; int bit_depth, color_type, interlace_type, compression_type, filter_method; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); iconW = width; iconH = height; if (bit_depth < 8) png_set_packing(png_ptr); png_set_expand(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr); if (bit_depth == 16) png_set_strip_16(png_ptr); png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER); png_read_update_info(png_ptr, info_ptr); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); } gameIcon = new unsigned char [iconW*iconH*4]; if (! gameIcon) return fatal ("Can't reserve memory for game icon."); int32_t transCol = 63519; Uint8 *p = (Uint8 *) gameIcon; if (fileIsPNG) { unsigned char * row_pointers[iconH]; for (int i = 0; i<iconH; i++) row_pointers[i] = p + 4*i*iconW; png_read_image(png_ptr, (png_byte **) row_pointers); png_read_end(png_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); } else { for (int t2 = 0; t2 < iconH; t2 ++) { int t1 = 0; while (t1 < iconW) { unsigned short c = (unsigned short) get2bytes (fp); if (c & 32) { n = fgetc (fp) + 1; c -= 32; } else { n = 1; } while (n --) { *p++ = (Uint8) redValue(c); *p++ = (Uint8) greenValue(c); *p++ = (Uint8) blueValue(c); *p++ = (Uint8) (c == transCol) ? 0 : 255; t1++; } } } } } if (customIconLogo & 2) { // There is an logo - read it! int n; long file_pointer = ftell (fp); png_structp png_ptr; png_infop info_ptr, end_info; int fileIsPNG = true; // Is this a PNG file? char tmp[10]; bytes_read = fread(tmp, 1, 8, fp); if (bytes_read != 8 && ferror (fp)) { debugOut("Reading error in initSludge.\n"); } if (png_sig_cmp((png_byte *) tmp, 0, 8)) { // No, it's old-school HSI fileIsPNG = false; fseek(fp, file_pointer, SEEK_SET); logoW = get2bytes (fp); logoH = get2bytes (fp); } else { // Read the PNG header png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { return false; } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); return false; } end_info = png_create_info_struct(png_ptr); if (!end_info) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); return false; } png_init_io(png_ptr, fp); // Tell libpng which file to read png_set_sig_bytes(png_ptr, 8); // 8 bytes already read png_read_info(png_ptr, info_ptr); png_uint_32 width, height; int bit_depth, color_type, interlace_type, compression_type, filter_method; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); logoW = width; logoH = height; if (bit_depth < 8) png_set_packing(png_ptr); png_set_expand(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr); if (bit_depth == 16) png_set_strip_16(png_ptr); #ifdef WIN32 // Windows wants a BGR bitmap if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) png_set_bgr(png_ptr); #endif png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER); png_read_update_info(png_ptr, info_ptr); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_method); } if ((logoW != 310) || (logoH != 88)) return fatal ("Game logo have wrong dimensions. (Should be 310x88)"); gameLogo = new unsigned char [logoW*logoH*4]; if (! gameLogo) return fatal ("Can't reserve memory for game logo."); // int32_t transCol = 63519; Uint8 *p = (Uint8 *) gameLogo; if (fileIsPNG) { unsigned char * row_pointers[logoH]; for (int i = 0; i<logoH; i++) row_pointers[i] = p + 4*i*logoW; png_read_image(png_ptr, (png_byte **) row_pointers); png_read_end(png_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); } else { for (int t2 = 0; t2 < logoH; t2 ++) { int t1 = 0; while (t1 < logoW) { unsigned short c = (unsigned short) get2bytes (fp); if (c & 32) { n = fgetc (fp) + 1; c -= 32; } else { n = 1; } while (n --) { #ifdef WIN32 // Windows wants a BGR bitmap *p++ = (Uint8) blueValue(c); *p++ = (Uint8) greenValue(c); *p++ = (Uint8) redValue(c); #else *p++ = (Uint8) redValue(c); *p++ = (Uint8) greenValue(c); *p++ = (Uint8) blueValue(c); #endif *p++ = (Uint8) /*(c == transCol) ? 0 :*/ 255; t1++; } } } } } numGlobals = get2bytes (fp); globalVars = new variable[numGlobals]; if (! checkNew (globalVars)) return false; for (a = 0; a < numGlobals; a ++) initVarNew (globalVars[a]); // Get the original (untranslated) name of the game and convert it to Unicode. // We use this to find saved preferences and saved games. setFileIndices (fp, gameSettings.numLanguages, 0); char * gameNameOrig = getNumberedString(1); char * gameName = encodeFilename (gameNameOrig); delete gameNameOrig; changeToUserDir (); #ifdef _WIN32 mkdir (gameName); #else mkdir (gameName, 0000777); #endif if (chdir (gameName)) return fatal ("This game's preference folder is inaccessible!\nI can't access the following directory (maybe there's a file with the same name, or maybe it's read-protected):", gameName); delete [] gameName; // Get user settings readIniFile (filename); // There's no startup window on Linux and respecting this // option from the ini file would disable commandline options. #if defined __unix__ && !(defined __APPLE__) if (! showSetupWindow()) return 0; saveIniFile (filename); #else if (! gameSettings.noStartWindow) { if (! showSetupWindow()) return 0; saveIniFile (filename); } #endif // Now set file indices properly to the chosen language. languageNum = getLanguageForFileB (); if (languageNum < 0) return fatal ("Can't find the translation data specified!"); setFileIndices (NULL, gameSettings.numLanguages, languageNum); if (dataFol[0]) { char *dataFolder = encodeFilename(dataFol); #ifdef _WIN32 mkdir (dataFolder); #else mkdir (dataFolder, 0000777); #endif if (chdir (dataFolder)) return fatal ("This game's data folder is inaccessible!\nI can't access the following directory (maybe there's a file with the same name, or maybe it's read-protected):", dataFolder); delete dataFolder; } positionStatus (10, winHeight - 15); return true; }