//-------------------------------------------------------------------------- // load and compile a shader // =-= source code modified from a version in the OpenGL SuperBible BOOL GL21compileShaderPair( const char* shaderDir, GLuint hVertexShader, const char* vertexFileName, GLuint hFragmentShader, const char* fragmentFileName) { GLint testVal; // Load them. If fail clean up and return null mgString fileName; fileName.format("%s%s", (const char*) shaderDir, (const char*) vertexFileName); mgOSFixFileName(fileName); if (!GL21loadShaderFile(fileName, hVertexShader)) { mgDebug("shader %s not found", (const char*) fileName); return false; } mgString log; // Compile vertex shader glCompileShader(hVertexShader); GL21getShaderLog(hVertexShader, log); log.trim(); if (log.isEmpty()) mgDebug("shader %s compiled", (const char*) fileName); else mgDebug("shader %s log:\n%s", (const char*) fileName, (const char*) log); // Check for errors glGetShaderiv(hVertexShader, GL_COMPILE_STATUS, &testVal); if (testVal == GL_FALSE) { mgDebug("shader %s compilation failed.", (const char*) fileName); return false; } fileName.format("%s%s", (const char*) shaderDir, (const char*) fragmentFileName); mgOSFixFileName(fileName); if (!GL21loadShaderFile(fileName, hFragmentShader)) { mgDebug("shader %s not found", (const char*) fileName); return false; } glCompileShader(hFragmentShader); GL21getShaderLog(hFragmentShader, log); log.trim(); if (log.isEmpty()) mgDebug("shader %s compiled", (const char*) fileName); else mgDebug("shader %s log:\n%s", (const char*) fileName, (const char*) log); glGetShaderiv(hFragmentShader, GL_COMPILE_STATUS, &testVal); if (testVal == GL_FALSE) { mgDebug("shader %s compilation failed.", (const char*) fileName); return false; } return true; }
//-------------------------------------------------------------- // constructor MinecraftRegion::MinecraftRegion( const char* regionDir, int regionX, int regionZ) { memset(m_header, 0, sizeof(m_header)); m_lock = mgOSLock::create(); // create the region file name m_fileName.format("%s/r.%d.%d.mcr", (const char*) regionDir, regionX, regionZ); mgOSFixFileName(m_fileName); // open the file m_regionFile = mgOSFileOpen(m_fileName, "rb"); if (m_regionFile == NULL) { mgDebug("could not open region file %s", (const char*) m_fileName); m_exists = false; return; } else m_exists = true; // read the header int count = fread(m_header, 1, sizeof(m_header), m_regionFile); if (count != sizeof(m_header)) { mgDebug("Could not read region (%d, %d) header -- %d bytes read, %d expected", regionX, regionZ, count, sizeof(m_header)); m_exists = false; } }
//-------------------------------------------------------------- // parse a file void mgXMLScanner::parseFile( const char* fileName) { m_sourceFileName = fileName; mgOSFixFileName(m_sourceFileName); FILE* inFile = mgOSFileOpen(m_sourceFileName, "rb"); if (inFile == NULL) throw new mgErrorMsg("xmlFileNotFound", "filename", "%s", (const char*) m_sourceFileName); char* buffer = new char[PARSE_BUFFER_SIZE]; while (true) { size_t len = fread(buffer, 1, PARSE_BUFFER_SIZE, inFile); if (len <= 0) break; // parse buffer of file data parse((int) len, buffer); } delete buffer; fclose(inFile); // end of input parseEnd(); }
//-------------------------------------------------------------- // get filename valued option void mgOptionsFile::getFileName( const char* name, const char* relativeTo, const char* defaultValue, mgString& fileName) const { mgString key(name); key.makeLower(); mgString value(defaultValue); m_options.lookup(key, value); mgString relativeFileName(relativeTo); mgOSFixFileName(relativeFileName); // handle ;-delimeted pairs of filenames we use for RGB+alpha images int semi = value.find(0, ";"); if (semi != -1) { mgString rgbName; value.substring(rgbName, 0, semi); value.deleteAt(0, semi+1); mgOSResolveRelativeName(relativeFileName, rgbName, fileName); fileName += ';'; mgString alphaName; mgOSResolveRelativeName(relativeFileName, value, alphaName); fileName += alphaName; } else { mgOSResolveRelativeName(relativeFileName, value, fileName); } }
//-------------------------------------------------------------- // load texture cube from file list mgTextureCube* mgGL33Services::loadTextureCube( const char* xminImage, const char* xmaxImage, const char* yminImage, const char* ymaxImage, const char* zminImage, const char* zmaxImage) { mgGL33TextureCube* texture = new mgGL33TextureCube(); memset(texture->m_imgTransparent, 0, sizeof(texture->m_imgTransparent)); // copy the file list into the texture texture->m_xminImage = xminImage; mgOSFixFileName(texture->m_xminImage); texture->m_xmaxImage = xmaxImage; mgOSFixFileName(texture->m_xminImage); texture->m_yminImage = yminImage; mgOSFixFileName(texture->m_yminImage); texture->m_ymaxImage = ymaxImage; mgOSFixFileName(texture->m_ymaxImage); texture->m_zminImage = zminImage; mgOSFixFileName(texture->m_zminImage); texture->m_zmaxImage = zmaxImage; mgOSFixFileName(texture->m_zmaxImage); reloadTextureCube(texture); m_textureCubes.add(texture); return texture; }
//-------------------------------------------------------------- // load texture from image file mgTextureImage* mgGL33Services::loadTexture( const char* fileName) { const void *value; // if we've already created the texture if (m_textureImages.lookup(fileName, value)) return (mgTextureImage*) value; mgGL33TextureImage* texture = new mgGL33TextureImage(); texture->m_fileName = fileName; mgOSFixFileName(texture->m_fileName); reloadTextureImage(texture); // save the texture id (under original name. copy in texture is fixed) m_textureImages.setAt(fileName, texture); return texture; }
//-------------------------------------------------------------- // load texture array from file list mgTextureArray* mgGL33Services::loadTextureArray( const mgStringArray& fileList) { mgGL33TextureArray* texture = new mgGL33TextureArray(); // flag for each transparent texture texture->m_imgTransparent = new BOOL[fileList.length()]; memset(texture->m_imgTransparent, 0, sizeof(BOOL)*fileList.length()); // copy the file list into the texture mgString fileName; for (int i = 0; i < fileList.length(); i++) { fileName = fileList[i]; mgOSFixFileName(fileName); texture->m_fileList.add(fileName); } reloadTextureArray(texture); m_textureArrays.add(texture); return texture; }
//-------------------------------------------------------------- // resolve possibly relative file name. void mgOSResolveRelativeName( const char* sourceName, const char* relName, mgString& absName) { mgString name(relName); mgOSFixFileName(name); // if relative name starts with backslash, we're done if (name.startsWith("\\")) { absName = name; return; } // if relative name starts with X: (drive letter), we're done char letter[MG_MAX_LETTER]; int posn = name.nextLetter(0, letter); if (posn < name.length()) { posn = name.nextLetter(posn, letter); if (strcmp(letter, ":") == 0) { absName = name; return; } } // assume it's really relative. strip last directory in source file mgString sourceFile(sourceName); int lastSlash = sourceFile.reverseFind(sourceFile.length(), '\\'); if (lastSlash != -1) sourceFile.substring(absName, 0, lastSlash+1); else absName.empty(); // source file has no dir absName += name; }
//-------------------------------------------------------------- // resolve possibly relative file name. void mgOSResolveRelativeName( const char* sourceFile, const char* relName, mgString& absName) { mgString name(relName); mgOSFixFileName(name); // if relative name starts with /, we're done if (name.startsWith("/")) { absName = name; return; } // assume it's really relative. strip last directory in source file absName = sourceFile; int lastSlash = absName.reverseFind(absName.length(), "/"); if (lastSlash != -1) absName.deleteAt(lastSlash+1, absName.length()-(lastSlash+1)); else absName.empty(); // source file has no dir absName += name; }