void reloadParallaxTextures () { parallaxLayer * nP = parallaxStuff; if (! nP) return; while (nP) { //fprintf (stderr, "Reloading parallax. (%d, %d) ", nP->width, nP->height); nP->textureName = 0; 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); } if (! NPOT_textures) { texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, getNextPOT(nP->width), getNextPOT(nP->height), 0, GL_RGBA, GL_UNSIGNED_BYTE, nP->texture, nP->textureName); } else { texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, nP->width, nP->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nP->texture, nP->textureName); } nP = nP->next; } }
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 getRGBIntoStack (unsigned int x, unsigned int y, stackHandler * sH) { if (x >= sceneWidth || y >= sceneHeight) { return fatal ("Co-ordinates are outside current scene!"); } variable newValue; newValue.varType = SVT_NULL; saveTexture (backdropTextureName, backdropTexture); GLubyte * target; if (! NPOT_textures) { target = backdropTexture + 4*getNextPOT(sceneWidth)*y + x*4; } else { target = backdropTexture + 4*sceneWidth*y + x*4; } setVariable (newValue, SVT_INT, target[2]); if (! addVarToStackQuick (newValue, sH -> first)) return false; sH -> last = sH -> first; setVariable (newValue, SVT_INT, target[1]); if (! addVarToStackQuick (newValue, sH -> first)) return false; setVariable (newValue, SVT_INT, target[0]); if (! addVarToStackQuick (newValue, sH -> first)) return false; return true; }
bool snapshot () { nosnapshot (); if (! freeze ()) return false; setPixelCoords (true); glGenTextures (1, &snapshotTextureName); int w = winWidth; int h = winHeight; if (! NPOT_textures) { w = getNextPOT(winWidth); h = getNextPOT(winHeight); snapTexW = ((double)winWidth) / w; snapTexH = ((double)winHeight) / h; } 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, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0, snapshotTextureName); // Render scene glDepthMask (GL_TRUE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen glDepthMask (GL_FALSE); drawBackDrop (); // Draw the room drawZBuffer(cameraX, cameraY, false); glEnable(GL_DEPTH_TEST); drawPeople (); // Then add any moving characters... glDisable(GL_DEPTH_TEST); viewSpeech (); // ...and anything being said drawStatusBar (); // Copy Our ViewPort To The Texture copyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, viewportOffsetX, viewportOffsetY, winWidth, winHeight, snapshotTextureName); setPixelCoords (false); unfreeze (false); return true; }
bool RenderManagerGL2D::setBackground(const std::string& filename) { try { SDL_Surface* newSurface = loadSurface(filename); glDeleteTextures(1, &mBackground); delete mImageMap["background"]; BufferedImage *imgBuffer = new BufferedImage; imgBuffer->w = getNextPOT(newSurface->w); imgBuffer->h = getNextPOT(newSurface->h); imgBuffer->glHandle = loadTexture(newSurface, false); mBackground = imgBuffer->glHandle; mImageMap["background"] = imgBuffer; } catch (FileLoadException) { return false; } return true; }
bool reserveBackdrop () { cameraX = 0; cameraY = 0; input.mouseX = (int)((float)input.mouseX * cameraZoom); input.mouseY = (int)((float)input.mouseY * cameraZoom); cameraZoom = 1.0; input.mouseX = (int)((float)input.mouseX / cameraZoom); input.mouseY = (int)((float)input.mouseY / cameraZoom); setPixelCoords(false); int picWidth = sceneWidth; int picHeight = sceneHeight; glPixelStorei (GL_UNPACK_ALIGNMENT, 1); if (backdropTexture) delete backdropTexture; if (! NPOT_textures) { picWidth = getNextPOT(sceneWidth); picHeight = getNextPOT(sceneHeight); backdropTexW = ((double)sceneWidth) / picWidth; backdropTexH = ((double)sceneHeight) / picHeight; } backdropTexture = new GLubyte [picWidth*picHeight*4]; if (! checkNew (backdropTexture)) return false; if (! backdropTextureName) glGenTextures (1, &backdropTextureName); glBindTexture (GL_TEXTURE_2D, backdropTextureName); 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, backdropTextureName); return true; }
void RenderManagerGL2D::drawImage(const std::string& filename, Vector2 position) { glEnable(GL_TEXTURE_2D); glEnable(GL_ALPHA_TEST); glDisable(GL_BLEND); BufferedImage* imageBuffer = mImageMap[filename]; if (!imageBuffer) { imageBuffer = new BufferedImage; SDL_Surface* newSurface = loadSurface(filename); imageBuffer->w = getNextPOT(newSurface->w); imageBuffer->h = getNextPOT(newSurface->h); imageBuffer->glHandle = loadTexture(newSurface, false); mImageMap[filename] = imageBuffer; } glColor4f(1.0, 1.0, 1.0, 1.0); glDisable(GL_BLEND); //glLoadIdentity(); //glTranslatef(position.x , position.y, 0.0); glBindTexture(imageBuffer->glHandle); drawQuad2(position.x, position.y, imageBuffer->w, imageBuffer->h); }
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 drawBackDrop () { setPrimaryColor(1.0, 1.0, 1.0, 1.0); //glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_BLEND); glUseProgram(shader.smartScaler); GLuint uniform = glGetUniformLocation(shader.smartScaler, "useLightTexture"); if (uniform >= 0) glUniform1i(uniform, 0); setPMVMatrix(shader.smartScaler); if (gameSettings.antiAlias == 1) { glUniform1i(glGetUniformLocation(shader.smartScaler, "antialias"), 1); } else { glUniform1i(glGetUniformLocation(shader.smartScaler, "antialias"), 0); } if (parallaxStuff) { parallaxLayer * ps = parallaxStuff; while (ps->next) ps = ps->next; while (ps) { ps -> cameraX = sortOutPCamera (cameraX, ps -> fractionX, (int)(sceneWidth - (float)winWidth/cameraZoom), (int)(ps -> width - (float)winWidth/cameraZoom)); ps -> cameraY = sortOutPCamera (cameraY, ps -> fractionY, (int)(sceneHeight - (float)winHeight/cameraZoom), (int)(ps -> height - (float)winHeight/cameraZoom)); glBindTexture (GL_TEXTURE_2D, ps->textureName); float w = (ps->wrapS) ? sceneWidth : ps->width; float h = (ps->wrapT) ? sceneHeight : ps->height; float texw; float texh; if (! NPOT_textures) { texw = (ps->wrapS) ? (float) sceneWidth / ps->width: (float) ps->width / getNextPOT(ps->width); texh = (ps->wrapT) ? (float) sceneHeight / ps->height: (float) ps->height / getNextPOT(ps->height); } else { texw = (ps->wrapS) ? (float) sceneWidth / ps->width: 1.0; texh = (ps->wrapT) ? (float) sceneHeight / ps->height: 1.0; } const GLfloat vertices[] = { (GLfloat)-ps -> cameraX, (GLfloat)-ps -> cameraY, 0.1f, w -ps -> cameraX, (GLfloat)-ps -> cameraY, 0.1f, (GLfloat)-ps -> cameraX, h -ps -> cameraY, 0.1f, w -ps -> cameraX, h -ps -> cameraY, 0.1f }; const GLfloat texCoords[] = { 0.0f, 0.0f, texw, 0.0f, 0.0f, texh, texw, texh }; drawQuad(shader.smartScaler, vertices, 1, texCoords); ps = ps -> prev; } } glBindTexture (GL_TEXTURE_2D, backdropTextureName); const GLfloat backdropTexCoords[] = { 0.0f, 0.0f, backdropTexW, 0.0f, 0.0f, backdropTexH, backdropTexW, backdropTexH }; const GLfloat vertices[] = { (GLfloat)-cameraX, (GLfloat)-cameraY, 0., (GLfloat)sceneWidth- (GLfloat)cameraX, (GLfloat)-cameraY, 0., (GLfloat)-cameraX, (GLfloat)sceneHeight- (GLfloat)cameraY, 0., (GLfloat)sceneWidth- (GLfloat)cameraX, (GLfloat)sceneHeight- (GLfloat)cameraY, 0. }; drawQuad(shader.smartScaler, vertices, 1, backdropTexCoords); glDisable(GL_BLEND); glUseProgram(0); }
bool loadSpriteBank (int fileNum, spriteBank & loadhere, bool isFont) { int i, tex_num, total, picwidth, picheight, spriteBankVersion = 0, howmany=0, startIndex=0; int * totalwidth, * maxheight; int numTextures = 0; byte * data; setResourceForFatal (fileNum); if (! openFileFromNum (fileNum)) return fatal ("Can't open sprite bank / font"); loadhere.isFont = isFont; total = get2bytes (bigDataFile); if (! total) { spriteBankVersion = fgetc (bigDataFile); if (spriteBankVersion == 1) { total = 0; } else { total = get2bytes (bigDataFile); } } if (total <= 0) return fatal ("No sprites in bank or invalid sprite bank file"); if (spriteBankVersion > 3) return fatal ("Unsupported sprite bank file format"); loadhere.total = total; loadhere.sprites = new sprite [total]; if (! checkNew (loadhere.sprites)) return false; byte ** spriteData = new byte * [total]; if (! checkNew (spriteData)) return false; totalwidth = new int[total]; if (! checkNew (totalwidth)) return false; maxheight = new int[total]; if (! checkNew (maxheight)) return false; loadhere.myPalette.tex_names = new GLuint [total]; if (! checkNew (loadhere.myPalette.tex_names)) return false; if (isFont) { loadhere.myPalette.burnTex_names = new GLuint [total]; if (! checkNew (loadhere.myPalette.burnTex_names)) return false; } loadhere.myPalette.tex_w = new int [total]; if (! checkNew (loadhere.myPalette.tex_w)) return false; loadhere.myPalette.tex_h = new int [total]; if (! checkNew (loadhere.myPalette.tex_h)) return false; if (spriteBankVersion && spriteBankVersion < 3) { howmany = fgetc (bigDataFile); startIndex = 1; } totalwidth[0] = maxheight[0] = 1; for (i = 0; i < total; i ++) { switch (spriteBankVersion) { case 3: { loadhere.sprites[i].xhot = getSigned (bigDataFile); loadhere.sprites[i].yhot = getSigned (bigDataFile); png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { return fatal ("Can't open sprite bank / font."); } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); return fatal ("Can't open sprite bank / font."); } png_infop end_info = png_create_info_struct(png_ptr); if (!end_info) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); return fatal ("Can't open sprite bank / font."); } png_init_io(png_ptr, bigDataFile); // Tell libpng which file to read png_set_sig_bytes(png_ptr, 8); // No sig 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); int rowbytes = png_get_rowbytes(png_ptr, info_ptr); unsigned char * row_pointers[height]; spriteData[i] = new unsigned char [rowbytes*height]; if (! checkNew (spriteData[i])) return false; for (unsigned int row = 0; row<height; row++) row_pointers[row] = spriteData[i] + row*rowbytes; 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); picwidth = loadhere.sprites[i].width = width; picheight = loadhere.sprites[i].height = height; break; } case 2: picwidth = get2bytes (bigDataFile); picheight = get2bytes (bigDataFile); loadhere.sprites[i].xhot = getSigned (bigDataFile); loadhere.sprites[i].yhot = getSigned (bigDataFile); break; default: picwidth = (byte) fgetc (bigDataFile); picheight = (byte) fgetc (bigDataFile); loadhere.sprites[i].xhot = fgetc (bigDataFile); loadhere.sprites[i].yhot = fgetc (bigDataFile); break; } if (totalwidth[numTextures] + picwidth < 2047) { loadhere.sprites[i].tex_x = totalwidth[numTextures]; totalwidth[numTextures] += (loadhere.sprites[i].width = picwidth) + 1; if ((loadhere.sprites[i].height = picheight)+2 > maxheight[numTextures]) maxheight[numTextures] = picheight+2; } else { numTextures++; if (numTextures > 255) return fatal ("Can't open sprite bank / font - it's too big."); loadhere.sprites[i].tex_x = 0; totalwidth[numTextures] = (loadhere.sprites[i].width = picwidth); maxheight[numTextures] = loadhere.sprites[i].height = picheight; } loadhere.sprites[i].texNum = numTextures; if (spriteBankVersion < 3) { data = (byte *) new byte [picwidth * (picheight + 1)]; if (! checkNew (data)) return false; int ooo = picwidth * picheight; for (int tt = 0; tt < picwidth; tt ++) { data[ooo ++] = 0; } spriteData[i] = data; switch (spriteBankVersion) { case 2: // RUN LENGTH COMPRESSED DATA { unsigned size = picwidth * picheight; unsigned pip = 0; while (pip < size) { byte col = fgetc (bigDataFile); int looper; if (col > howmany) { col -= howmany + 1; looper = fgetc (bigDataFile) + 1; } else looper = 1; while (looper --) { data[pip ++] = col; } } } break; default: // RAW DATA size_t bytes_read = fread (data, picwidth, picheight, bigDataFile); if (bytes_read != picwidth * picheight && ferror (bigDataFile)) { debugOut("Reading error in loadSpriteBank.\n"); } break; } } } numTextures++; if (! spriteBankVersion) { howmany = fgetc (bigDataFile); startIndex = fgetc (bigDataFile); } if (spriteBankVersion < 3) { if (! reserveSpritePal (loadhere.myPalette, howmany + startIndex)) return false; for (i = 0; i < howmany; i ++) { loadhere.myPalette.r[i + startIndex] = (byte) fgetc (bigDataFile); loadhere.myPalette.g[i + startIndex] = (byte) fgetc (bigDataFile); loadhere.myPalette.b[i + startIndex] = (byte) fgetc (bigDataFile); loadhere.myPalette.pal[i + startIndex] = makeColour (loadhere.myPalette.r[i + startIndex], loadhere.myPalette.g[i + startIndex], loadhere.myPalette.b[i + startIndex]); } } loadhere.myPalette.originalRed = loadhere.myPalette.originalGreen = loadhere.myPalette.originalBlue = 255; loadhere.myPalette.numTextures = numTextures; GLubyte * tmp[numTextures]; GLubyte * tmp2[numTextures]; for (tex_num = 0; tex_num < numTextures; tex_num++) { if (! NPOT_textures) { totalwidth[tex_num] = getNextPOT(totalwidth[tex_num]); maxheight[tex_num] = getNextPOT(maxheight[tex_num]); } tmp[tex_num] = new GLubyte [(maxheight[tex_num]+1)*totalwidth[tex_num]*4]; if (! checkNew (tmp[tex_num])) return false; memset (tmp[tex_num], 0, maxheight[tex_num]*totalwidth[tex_num]*4); if (isFont) { tmp2[tex_num] = new GLubyte [(maxheight[tex_num]+1)*totalwidth[tex_num]*4]; if (! checkNew (tmp2[tex_num])) return false; memset (tmp2[tex_num], 0, maxheight[tex_num]*totalwidth[tex_num]*4); } loadhere.myPalette.tex_w[tex_num] = totalwidth[tex_num]; loadhere.myPalette.tex_h[tex_num] = maxheight[tex_num]; } int fromhere; unsigned char s; for (i = 0; i < total; i ++) { fromhere = 0; int transColour = -1; if (spriteBankVersion < 3) { int size = loadhere.sprites[i].height * loadhere.sprites[i].width; while (fromhere < size) { s = spriteData[i][fromhere++]; if (s) { transColour = s; break; } } fromhere = 0; } for (int y = 1; y < 1 + loadhere.sprites[i].height; y ++) { for (int x = loadhere.sprites[i].tex_x; x < loadhere.sprites[i].tex_x+loadhere.sprites[i].width; x ++) { GLubyte * target = tmp[loadhere.sprites[i].texNum] + 4*totalwidth[loadhere.sprites[i].texNum]*y + x*4; if (spriteBankVersion < 3) { s = spriteData[i][fromhere++]; if (s) { target[0] = (GLubyte) loadhere.myPalette.r[s]; target[1] = (GLubyte) loadhere.myPalette.g[s]; target[2] = (GLubyte) loadhere.myPalette.b[s]; target[3] = (GLubyte) 255; transColour = s; } else if (transColour >=0) { target[0] = (GLubyte) loadhere.myPalette.r[transColour]; target[1] = (GLubyte) loadhere.myPalette.g[transColour]; target[2] = (GLubyte) loadhere.myPalette.b[transColour]; target[3] = (GLubyte) 0; } if (isFont) { target = tmp2[loadhere.sprites[i].texNum] + 4*totalwidth[loadhere.sprites[i].texNum]*y + x*4; target[0] = (GLubyte) 255; target[1] = (GLubyte) 255; target[2] = (GLubyte) 255; if (s) target[3] = (GLubyte) loadhere.myPalette.r[s]; /*else target[3] = (GLubyte) 0;*/ } } else { target[0] = (GLubyte) spriteData[i][fromhere++]; target[1] = (GLubyte) spriteData[i][fromhere++]; target[2] = (GLubyte) spriteData[i][fromhere++]; target[3] = (GLubyte) spriteData[i][fromhere++]; } } } delete spriteData[i]; } delete spriteData; spriteData = NULL; glPixelStorei (GL_UNPACK_ALIGNMENT, 1); glGenTextures (numTextures, loadhere.myPalette.tex_names); if (isFont) glGenTextures (numTextures, loadhere.myPalette.burnTex_names); for (tex_num = 0; tex_num < numTextures; tex_num++) { glBindTexture (GL_TEXTURE_2D, loadhere.myPalette.tex_names[tex_num]); 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, totalwidth[tex_num], maxheight[tex_num], 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp[tex_num], loadhere.myPalette.tex_names[tex_num]); delete tmp[tex_num]; tmp[tex_num] = NULL; if (isFont) { glBindTexture (GL_TEXTURE_2D, loadhere.myPalette.burnTex_names[tex_num]); 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, totalwidth[tex_num], maxheight[tex_num], 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp2[tex_num], loadhere.myPalette.burnTex_names[tex_num]); delete tmp2[tex_num]; tmp2[tex_num] = NULL; } } finishAccess (); setResourceForFatal (-1); return true; }
bool scaleSprite (sprite & single, const spritePalette & fontPal, onScreenPerson * thisPerson, bool mirror) { float x = thisPerson->x; float y = thisPerson->y; float scale = thisPerson-> scale; bool light = ! (thisPerson->extra & EXTRA_NOLITE); if (scale <= 0.05) return false; float tx1 = (float)(single.tex_x) / fontPal.tex_w[single.texNum]; float ty1 = (float) 1.0/fontPal.tex_h[single.texNum]; float tx2 = (float)(single.tex_x + single.width) / fontPal.tex_w[single.texNum]; float ty2 = (float)(single.height+1)/fontPal.tex_h[single.texNum]; int diffX = (int)(((float)single.width) * scale); int diffY = (int)(((float)single.height) * scale); GLfloat x1, y1, x2, y2; if (thisPerson -> extra & EXTRA_FIXTOSCREEN) { x = x / cameraZoom; y = y / cameraZoom; if (single.xhot < 0) x1 = x - (int)((mirror ? (float) (single.width - single.xhot) : (float)(single.xhot+1) ) * scale/cameraZoom); else x1 = x - (int)((mirror ? (float) (single.width - (single.xhot+1)) : (float)single.xhot ) * scale / cameraZoom); y1 = y - (int)((single.yhot - thisPerson->floaty) * scale / cameraZoom); x2 = x1 + (int)(diffX / cameraZoom); y2 = y1 + (int)(diffY / cameraZoom); } else { x -= cameraX; y -= cameraY; if (single.xhot < 0) x1 = x - (int)((mirror ? (float) (single.width - single.xhot) : (float)(single.xhot+1) ) * scale); else x1 = x - (int)((mirror ? (float) (single.width - (single.xhot+1)) : (float)single.xhot ) * scale); y1 = y - (int)((single.yhot - thisPerson->floaty) * scale); x2 = x1 + diffX; y2 = y1 + diffY; } GLfloat z; if ((! (thisPerson->extra & EXTRA_NOZB)) && zBuffer.numPanels) { int i; for (i = 1; i<zBuffer.numPanels; i++) { if (zBuffer.panel[i] >= y + cameraY) { i--; break; } } z = 0.999 - (double) i * (1.0 / 128.0); } else { z = -0.5; } float ltx1, ltx2, lty1, lty2; if (! NPOT_textures) { ltx1 = lightMap.texW * (x1+cameraX) / sceneWidth; ltx2 = lightMap.texW * (x2+cameraX) / sceneWidth; lty1 = lightMap.texH * (y1+cameraY) / sceneHeight; lty2 = lightMap.texH * (y2+cameraY) / sceneHeight; } else { ltx1 = (float) (x1+cameraX) / sceneWidth; ltx2 = (float) (x2+cameraX) / sceneWidth; lty1 = (float) (y1+cameraY) / sceneHeight; lty2 = (float) (y2+cameraY) / sceneHeight; } const GLfloat ltexCoords[] = { ltx1, lty1, ltx2, lty1, ltx1, lty2, ltx2, lty2 }; if (light && lightMap.data) { if (lightMapMode == LIGHTMAPMODE_HOTSPOT) { int lx=(int)(x+cameraX); int ly=(int)(y+cameraY); if (lx<0) lx = 0; else if (lx>=sceneWidth) lx = sceneWidth-1; if (ly<0) ly = 0; else if (ly>=sceneHeight) ly = sceneHeight-1; GLubyte *target; if (! NPOT_textures) { target = lightMap.data + (ly*getNextPOT(sceneWidth) + lx)*4; } else { target = lightMap.data + (ly*sceneWidth + lx)*4; } curLight[0] = target[0]; curLight[1] = target[1]; curLight[2] = target[2]; } else if (lightMapMode == LIGHTMAPMODE_PIXEL) { curLight[0] = curLight[1] = curLight[2] = 255; glActiveTexture(GL_TEXTURE1); glBindTexture (GL_TEXTURE_2D, lightMap.name); glActiveTexture(GL_TEXTURE0); } } else { curLight[0] = curLight[1] = curLight[2] = 255; } if (! (thisPerson->extra & EXTRA_RECTANGULAR)) checkColourChange (true); setDrawMode (thisPerson); glBindTexture (GL_TEXTURE_2D, fontPal.tex_names[single.texNum]); glEnable(GL_BLEND); glUseProgram(shader.smartScaler); GLuint uniform = glGetUniformLocation(shader.smartScaler, "useLightTexture"); if (uniform >= 0) glUniform1i(uniform, light && lightMapMode == LIGHTMAPMODE_PIXEL && lightMap.data); setPMVMatrix(shader.smartScaler); if (gameSettings.antiAlias == 1) { glUniform1i(glGetUniformLocation(shader.smartScaler, "antialias"), 1); } else { glUniform1i(glGetUniformLocation(shader.smartScaler, "antialias"), 0); } const GLfloat vertices[] = { x1, y1, z, x2, y1, z, x1, y2, z, x2, y2, z }; if (! mirror) { GLfloat tx3 = tx1; tx1 = tx2; tx2 = tx3; } const GLfloat texCoords[] = { tx2, ty1, tx1, ty1, tx2, ty2, tx1, ty2 }; drawQuad(shader.smartScaler, vertices, 2, texCoords, ltexCoords); glDisable(GL_BLEND); glUseProgram(0); if (light && lightMapMode == LIGHTMAPMODE_PIXEL) { glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE0); } setSecondaryColor(0., 0., 0.,1.); //glDisable(GL_COLOR_SUM); FIXME: replace line? // Are we pointing at the sprite? if (input.mouseX >= x1 && input.mouseX <= x2 && input.mouseY >= y1 && input.mouseY <= y2) { if (thisPerson->extra & EXTRA_RECTANGULAR) return true; return checkColourChange (false); } return false; }
GLuint RenderManagerGL2D::loadTexture(SDL_Surface *surface, bool specular) { SDL_Surface* textureSurface; SDL_Surface* convertedTexture; textureSurface = surface; // Determine size of padding for 2^n format int oldX = textureSurface->w; int oldY = textureSurface->h; int paddedX = getNextPOT(textureSurface->w); int paddedY = getNextPOT(textureSurface->h); SDL_Rect targetRect; targetRect.w = oldX; targetRect.h = oldY; targetRect.x = (paddedX - oldX) / 2; targetRect.y = (paddedY - oldY) / 2; SDL_SetColorKey(textureSurface, SDL_TRUE, SDL_MapRGB(textureSurface->format, 0, 0, 0)); convertedTexture = SDL_CreateRGBSurface(SDL_SWSURFACE, paddedX, paddedY, 32, #if SDL_BYTEORDER == SDL_BIG_ENDIAN 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff); #else 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); #endif SDL_BlitSurface(textureSurface, 0, convertedTexture, &targetRect); if (specular) { for (int y = 0; y < convertedTexture->h; ++y) { for (int x = 0; x < convertedTexture->w; ++x) { SDL_Color* pixel = &(((SDL_Color*)convertedTexture->pixels) [y * convertedTexture->w +x]); int luminance = int(pixel->r) * 5 - 4 * 256 - 138; luminance = luminance > 0 ? luminance : 0; luminance = luminance < 255 ? luminance : 255; pixel->r = luminance; pixel->g = luminance; pixel->b = luminance; } } } GLuint texture; glGenTextures(1, &texture); glBindTexture(texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, convertedTexture->w, convertedTexture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, convertedTexture->pixels); SDL_FreeSurface(textureSurface); SDL_FreeSurface(convertedTexture); return texture; }
void RenderManagerGL2D::init(int xResolution, int yResolution, bool fullscreen) { glDisable(GL_DEPTH_TEST); mCurrentFlags.insert(GL_MULTISAMPLE); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Set modesetting Uint32 screenFlags = SDL_WINDOW_OPENGL; if (fullscreen) screenFlags |= SDL_WINDOW_FULLSCREEN; // Create window mWindow = SDL_CreateWindow(AppTitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, xResolution, yResolution, screenFlags); // Set icon SDL_Surface* icon = loadSurface("Icon.bmp"); SDL_SetColorKey(icon, SDL_TRUE, SDL_MapRGB(icon->format, 0, 0, 0)); SDL_SetWindowIcon(mWindow, icon); SDL_FreeSurface(icon); // Create gl context mGlContext = SDL_GL_CreateContext(mWindow); SDL_ShowCursor(0); glDisable(GL_MULTISAMPLE); mLeftBlobColor = Color(255, 0, 0); mRightBlobColor = Color(0, 255, 0); glEnable(GL_TEXTURE_2D); // Load background SDL_Surface* bgSurface = loadSurface("backgrounds/strand2.bmp"); BufferedImage* bgBufImage = new BufferedImage; bgBufImage->w = getNextPOT(bgSurface->w); bgBufImage->h = getNextPOT(bgSurface->h); bgBufImage->glHandle = loadTexture(bgSurface, false); mBackground = bgBufImage->glHandle; mImageMap["background"] = bgBufImage; mBallShadow = loadTexture(loadSurface("gfx/schball.bmp"), false); for (int i = 1; i <= 16; ++i) { char filename[64]; sprintf(filename, "gfx/ball%02d.bmp", i); GLuint ballImage = loadTexture(loadSurface(filename), false); mBall.push_back(ballImage); } for (int i = 1; i <= 5; ++i) { char filename[64]; sprintf(filename, "gfx/blobbym%d.bmp", i); GLuint blobImage = loadTexture(loadSurface(filename), false); mBlob.push_back(blobImage); sprintf(filename, "gfx/blobbym%d.bmp", i); GLuint blobSpecular = loadTexture(loadSurface(filename), true); mBlobSpecular.push_back(blobSpecular); sprintf(filename, "gfx/sch1%d.bmp", i); GLuint blobShadow = loadTexture(loadSurface(filename), false); mBlobShadow.push_back(blobShadow); } // create text base textures SDL_Surface* textbase = createEmptySurface(2048, 32); SDL_Surface* hltextbase = createEmptySurface(2048, 32); int x = 0; int sx = 0; for (int i = 0; i <= 54; ++i) { char filename[64]; sprintf(filename, "gfx/font%02d.bmp", i); SDL_Surface* fontSurface = loadSurface(filename); SDL_Surface* highlight = highlightSurface(fontSurface, 60); SDL_Rect r = {(Uint16)x, 0, (Uint16)fontSurface->w, (Uint16)fontSurface->h}; SDL_BlitSurface(fontSurface, 0, textbase, &r); SDL_BlitSurface(highlight, 0, hltextbase, &r); r.x = sx; r.y = 0; Texture s = Texture(0, x, 0, fontSurface->w, fontSurface->h, 2048, 32); mFont.push_back(s); mHighlightFont.push_back(s); x += fontSurface->w; SDL_FreeSurface(fontSurface); } GLuint texture = loadTexture(textbase, false); GLuint hltexture = loadTexture(hltextbase, false); for (unsigned int i = 0; i < mFont.size(); ++i) { mFont[i].texture = texture; mHighlightFont[i].texture = hltexture; } mParticle = loadTexture(loadSurface("gfx/blood.bmp"), false); glViewport(0, 0, xResolution, yResolution); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, 800, 600, 0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_TEXTURE_2D); glAlphaFunc(GL_GREATER, 0.5); glEnable(GL_ALPHA_TEST); }
bool loadSpriteTexture (spriteBank *loadhere, int index) { int fromhere; unsigned char s; NPOT_check(); glPixelStorei (GL_UNPACK_ALIGNMENT, 1); if (loadhere->type < 2) { loadhere->myPalette.tex_w[index] = loadhere->sprites[index].width; loadhere->myPalette.tex_h[index] = loadhere->sprites[index].height; loadhere->sprites[index].tex_x = 0.0; if (! NPOT_textures) { loadhere->myPalette.tex_w[index] = getNextPOT(loadhere->myPalette.tex_w[index]); loadhere->myPalette.tex_h[index] = getNextPOT(loadhere->myPalette.tex_h[index]); } int h = loadhere->myPalette.tex_h[index]; int w = loadhere->myPalette.tex_w[index]; GLubyte *tmp = new GLubyte[w*h*4]; memset (tmp, 0, w*h*4); fromhere = 0; for (int y = 0; y < loadhere->sprites[index].height; y ++) { for (int x = 0; x < loadhere->sprites[index].width; x ++) { GLubyte * target = tmp + 4*w*y + x*4; s = loadhere->sprites[index].data[fromhere++]; if (s) { target[0] = (GLubyte) loadhere->myPalette.r[s]; target[1] = (GLubyte) loadhere->myPalette.g[s]; target[2] = (GLubyte) loadhere->myPalette.b[s]; target[3] = (GLubyte) 255; } else { target[0] = (GLubyte) 0; target[1] = (GLubyte) 0; target[2] = (GLubyte) 0; target[3] = (GLubyte) 0; } } } glBindTexture (GL_TEXTURE_2D, loadhere->myPalette.tex_names[loadhere->sprites[index].texNum]); 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_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp); delete[] tmp; } else { loadhere->myPalette.tex_w[index] = loadhere->sprites[index].width; loadhere->myPalette.tex_h[index] = loadhere->sprites[index].height; loadhere->sprites[index].tex_x = 0.0; if (! NPOT_textures) { loadhere->myPalette.tex_w[index] = getNextPOT(loadhere->myPalette.tex_w[index]); loadhere->myPalette.tex_h[index] = getNextPOT(loadhere->myPalette.tex_h[index]); } int h = loadhere->myPalette.tex_h[index]; int w = loadhere->myPalette.tex_w[index]; GLubyte *tmp = new GLubyte[w*h*4]; memset (tmp, 0, w*h*4); fromhere = 0; for (int y = 0; y < loadhere->sprites[index].height; y ++) { for (int x = 0; x < loadhere->sprites[index].width; x ++) { GLubyte * target = tmp + 4*w*y + x*4; target[0] = loadhere->sprites[index].data[fromhere++]; target[1] = loadhere->sprites[index].data[fromhere++]; target[2] = loadhere->sprites[index].data[fromhere++]; target[3] = loadhere->sprites[index].data[fromhere++]; } } glBindTexture (GL_TEXTURE_2D, loadhere->myPalette.tex_names[loadhere->sprites[index].texNum]); 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_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp); delete[] tmp; } 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 setZBuffer (int y) { int x, n; uint32_t stillToGo = 0; int yPalette[16], sorted[16], sortback[16]; killZBuffer (); setResourceForFatal (y); zBuffer.originalNum = y; if (! openFileFromNum (y)) return false; if (fgetc (bigDataFile) != 'S') return fatal ("Not a Z-buffer file"); if (fgetc (bigDataFile) != 'z') return fatal ("Not a Z-buffer file"); if (fgetc (bigDataFile) != 'b') return fatal ("Not a Z-buffer file"); switch (fgetc (bigDataFile)) { case 0: zBuffer.width = 640; zBuffer.height = 480; break; case 1: zBuffer.width = get2bytes (bigDataFile); zBuffer.height = get2bytes (bigDataFile); break; default: return fatal ("Extended Z-buffer format not supported in this version of the SLUDGE engine"); } if (zBuffer.width != sceneWidth || zBuffer.height != sceneHeight) { char tmp[256]; sprintf (tmp, "Z-w: %d Z-h:%d w: %d, h:%d", zBuffer.width, zBuffer.height, sceneWidth, sceneHeight); return fatal ("Z-buffer width and height don't match scene width and height", tmp); } zBuffer.numPanels = fgetc (bigDataFile); for (y = 0; y < zBuffer.numPanels; y ++) { yPalette[y] = get2bytes (bigDataFile); } sortZPal (yPalette, sorted, zBuffer.numPanels); for (y = 0; y < zBuffer.numPanels; y ++) { zBuffer.panel[y] = yPalette[sorted[y]]; sortback[sorted[y]] = y; } int picWidth = sceneWidth; int picHeight = sceneHeight; if (! NPOT_textures) { picWidth = getNextPOT(picWidth); picHeight = getNextPOT(picHeight); } zBuffer.tex = new GLubyte [picHeight*picWidth]; if (! checkNew (zBuffer.tex)) return false; for (y = 0; y < sceneHeight; y ++) { for (x = 0; x < sceneWidth; x ++) { if (stillToGo == 0) { n = fgetc (bigDataFile); stillToGo = n >> 4; if (stillToGo == 15) stillToGo = get2bytes (bigDataFile) + 16l; else stillToGo ++; n &= 15; } zBuffer.tex[y*picWidth + x] = sortback[n]*16; stillToGo --; } }
void RenderManagerGL2D::init(int xResolution, int yResolution, bool fullscreen) { mCurrentFlags.insert(GL_DEPTH_TEST); mCurrentFlags.insert(GL_MULTISAMPLE); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); Uint32 screenFlags = SDL_OPENGL; if (fullscreen) screenFlags |= SDL_FULLSCREEN; SDL_WM_SetCaption(AppTitle, ""); SDL_WM_SetIcon(SDL_LoadBMP("data/Icon.bmp"), NULL); SDL_SetVideoMode(xResolution, yResolution, 0, screenFlags); SDL_ShowCursor(0); glDisable(GL_MULTISAMPLE); mLeftBlobColor = Color(255, 0, 0); mRightBlobColor = Color(0, 255, 0); SDL_Surface* bgSurface = loadSurface("backgrounds/strand2.bmp"); BufferedImage* bgBufImage = new BufferedImage; bgBufImage->w = getNextPOT(bgSurface->w); bgBufImage->h = getNextPOT(bgSurface->h); bgBufImage->glHandle = loadTexture(bgSurface, false); mBackground = bgBufImage->glHandle; mImageMap["background"] = bgBufImage; mBallShadow = loadTexture(loadSurface("gfx/schball.bmp"), false); mScroll = loadTexture(loadSurface("gfx/scrollbar.bmp"), false); for (int i = 1; i <= 16; ++i) { char filename[64]; sprintf(filename, "gfx/ball%02d.bmp", i); GLuint ballImage = loadTexture(loadSurface(filename), false); mBall.push_back(ballImage); } for (int i = 1; i <= 5; ++i) { char filename[64]; sprintf(filename, "gfx/blobbym%d.bmp", i); GLuint blobImage = loadTexture(loadSurface(filename), false); mBlob.push_back(blobImage); sprintf(filename, "gfx/blobbym%d.bmp", i); GLuint blobSpecular = loadTexture(loadSurface(filename), true); mBlobSpecular.push_back(blobSpecular); sprintf(filename, "gfx/sch1%d.bmp", i); GLuint blobShadow = loadTexture(loadSurface(filename), false); mBlobShadow.push_back(blobShadow); } // create text base textures SDL_Surface* textbase = createEmptySurface(2048, 32); SDL_Surface* hltextbase = createEmptySurface(2048, 32); SDL_Surface* smalltextbase = createEmptySurface(1024, 16); SDL_Surface* hlsmalltextbase = createEmptySurface(1024, 16); int x = 0; int sx = 0; for (int i = 0; i <= 53; ++i) { char filename[64], filename2[64]; sprintf(filename, "gfx/font%02d.bmp", i); sprintf(filename2, "gfx/font_small/font%02d.bmp", i); SDL_Surface* fontSurface = loadSurface(filename); SDL_Surface* fontSurface2 = loadSurface(filename2); GLuint newFont = loadTexture(loadSurface(filename), false); GLuint newFont2 = loadTexture(loadSurface(filename2), false); SDL_Surface* highlight = highlightSurface(fontSurface, 60); SDL_Surface* highlight2 = highlightSurface(fontSurface2, 60); SDL_FreeSurface(fontSurface); SDL_FreeSurface(fontSurface2); fontSurface = loadSurface(filename); fontSurface2 = loadSurface(filename2); SDL_Rect r = {x, 0, fontSurface->w, fontSurface->h}; SDL_BlitSurface(fontSurface, 0, textbase, &r); SDL_BlitSurface(highlight, 0, hltextbase, &r); r = {sx, 0, fontSurface2->w, fontSurface2->h}; SDL_BlitSurface(fontSurface2, 0, smalltextbase, &r); SDL_BlitSurface(highlight2, 0, hlsmalltextbase, &r); //GLuint ballImage = loadTexture(sf, false); //mBall.push_back(ballImage); Texture s = Texture(0, x, 0, fontSurface->w, fontSurface->h, 2048, 32); mFont.push_back(s); mHighlightFont.push_back(s); s = Texture(0, sx, 0, fontSurface2->w, fontSurface2->h, 1024, 16); //mFont.push_back(newFont); //mHighlightFont.push_back(loadTexture(highlight, false)); mSmallFont.push_back( s ); mHighlightSmallFont.push_back( s ); x += fontSurface->w; sx += fontSurface2->w; SDL_FreeSurface(fontSurface); SDL_FreeSurface(fontSurface2); } GLuint texture = loadTexture(textbase, false); GLuint hltexture = loadTexture(hltextbase, false); GLuint smalltexture = loadTexture(smalltextbase, false); GLuint hlsmalltexture = loadTexture(hlsmalltextbase, false); for (int i = 0; i < mFont.size(); ++i) { mFont[i].texture = texture; mHighlightFont[i].texture = hltexture; mSmallFont[i].texture = smalltexture; mHighlightSmallFont[i].texture = hlsmalltexture; } mParticle = loadTexture(loadSurface("gfx/blood.bmp"), false); glViewport(0, 0, xResolution, yResolution); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, 800, 600, 0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearDepth(1.0); glDepthFunc(GL_LEQUAL); glDisable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); glAlphaFunc(GL_GREATER, 0.5); glEnable(GL_ALPHA_TEST); }