bool FileToBuffer(MPQHANDLE &hStarDat, MPQHANDLE &hBrooDat, MPQHANDLE &hPatchRt, MPQHANDLE &hPriority, const std::string &fileName, buffer &buf) { return ( hPriority != nullptr && FileToBuffer(hPriority, fileName, buf) ) || FileToBuffer(hPatchRt, fileName, buf) || FileToBuffer(hBrooDat, fileName, buf) || FileToBuffer(hStarDat, fileName, buf); }
unsigned int ShaderLoader::LoadShader(const char* _strName, unsigned int _uiType) { int success = GL_FALSE; unsigned int handle = glCreateShader(_uiType); unsigned char* source = FileToBuffer(_strName); glShaderSource(handle, 1, (const char**)&source, 0); glCompileShader(handle); glGetShaderiv(handle, GL_COMPILE_STATUS, &success); if (success == GL_FALSE) { int infoLogLength = 0; glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength); char* infoLog = new char[infoLogLength]; glGetShaderInfoLog(handle, infoLogLength, 0, infoLog); printf("Error: Failed to compile shader!\n"); printf("%s", infoLog); printf("\n"); delete[] infoLog; return 0; } delete[] source; return handle; }
bool FileToBuffer(MPQHANDLE &hStarDat, MPQHANDLE &hBrooDat, MPQHANDLE &hPatchRt, const std::string &fileName, buffer &buf) { return FileToBuffer(hPatchRt, fileName, buf) || FileToBuffer(hBrooDat, fileName, buf) || FileToBuffer(hStarDat, fileName, buf); }
////////////////////////////////////////////////////////////////////////// // loads only a vertex and pixel shader from text files unsigned int AIE::LoadShader(unsigned int a_uiInputAttributeCount, const char** a_aszInputAttributes, unsigned int a_uiOutputAttributeCount, const char** a_aszOutputAttributes, const char* a_szVertexShader, const char* a_szPixelShader, const char* a_szGeometryShader /* = nullptr */, const char* a_szTessellationControlShader /* = nullptr */, const char* a_szTessellationEvaluationShader /* = nullptr */) { GLint iSuccess; GLchar acLog[256]; // load files into char buffers char* vsSource = FileToBuffer(a_szVertexShader); char* fsSource = FileToBuffer(a_szPixelShader); char* gsSource = a_szGeometryShader == nullptr ? nullptr : FileToBuffer(a_szGeometryShader); char* tcsSource = a_szTessellationControlShader == nullptr ? nullptr : FileToBuffer(a_szTessellationControlShader); char* tesSource = a_szTessellationEvaluationShader == nullptr ? nullptr : FileToBuffer(a_szTessellationEvaluationShader); // must have vertex and pixel if (vsSource == nullptr || fsSource == nullptr) { return 0; } // create 2 shader handles GLuint vsHandle = glCreateShader(GL_VERTEX_SHADER); GLuint fsHandle = glCreateShader(GL_FRAGMENT_SHADER); GLuint gsHandle = 0; GLuint tcsHandle = 0; GLuint tesHandle = 0; // compile vertex shader and log errors glShaderSource(vsHandle, 1, (const char**)&vsSource, 0); glCompileShader(vsHandle); glGetShaderiv(vsHandle, GL_COMPILE_STATUS, &iSuccess); glGetShaderInfoLog(vsHandle, sizeof(acLog), 0, acLog); if (iSuccess == GL_FALSE) { printf("Error: Failed to compile vertex shader!\n"); printf(acLog); printf("\n"); return 0; } // compile pixel shader and log errors glShaderSource(fsHandle, 1, (const char**)&fsSource, 0); glCompileShader(fsHandle); glGetShaderiv(fsHandle, GL_COMPILE_STATUS, &iSuccess); glGetShaderInfoLog(fsHandle, sizeof(acLog), 0, acLog); if (iSuccess == GL_FALSE) { printf("Error: Failed to compile pixel shader!\n"); printf(acLog); printf("\n"); return 0; } if (gsSource != nullptr) { gsHandle = glCreateShader(GL_GEOMETRY_SHADER); // compile geometry shader and log errors glShaderSource(gsHandle, 1, (const char**)&gsSource, 0); glCompileShader(gsHandle); glGetShaderiv(gsHandle, GL_COMPILE_STATUS, &iSuccess); glGetShaderInfoLog(gsHandle, sizeof(acLog), 0, acLog); if (iSuccess == GL_FALSE) { printf("Error: Failed to compile geometry shader!\n"); printf(acLog); printf("\n"); return 0; } } if (tesSource != nullptr && tcsSource != nullptr) { tesHandle = glCreateShader(GL_TESS_EVALUATION_SHADER); tcsHandle = glCreateShader(GL_TESS_CONTROL_SHADER); // compile tessellation control shader and log errors glShaderSource(tcsHandle, 1, (const char**)&tcsSource, 0); glCompileShader(tcsHandle); glGetShaderiv(tcsHandle, GL_COMPILE_STATUS, &iSuccess); glGetShaderInfoLog(tcsHandle, sizeof(acLog), 0, acLog); if (iSuccess == GL_FALSE) { printf("Error: Failed to compile tessellation control shader!\n"); printf(acLog); printf("\n"); return 0; } // compile tessellation evaluation shader and log errors glShaderSource(tesHandle, 1, (const char**)&tesSource, 0); glCompileShader(tesHandle); glGetShaderiv(tesHandle, GL_COMPILE_STATUS, &iSuccess); glGetShaderInfoLog(tesHandle, sizeof(acLog), 0, acLog); if (iSuccess == GL_FALSE) { printf("Error: Failed to compile tessellation evaluation shader!\n"); printf(acLog); printf("\n"); return 0; } } // create a shader program and attach the shaders to it GLuint uiProgramHandle = glCreateProgram(); glAttachShader(uiProgramHandle, vsHandle); glAttachShader(uiProgramHandle, fsHandle); if (gsHandle != 0) glAttachShader(uiProgramHandle, gsHandle); if (tcsHandle != 0) glAttachShader(uiProgramHandle, tcsHandle); if (tesHandle != 0) glAttachShader(uiProgramHandle, tesHandle); // specify vertex input attributes for ( unsigned int i = 0 ; i < a_uiInputAttributeCount ; ++i ) glBindAttribLocation(uiProgramHandle, i, a_aszInputAttributes[i]); // specify pixel shader outputs for ( unsigned int i = 0 ; i < a_uiOutputAttributeCount ; ++i ) glBindFragDataLocation(uiProgramHandle, i, a_aszOutputAttributes[i]); // link the program together and log errors glLinkProgram(uiProgramHandle); glGetProgramiv(uiProgramHandle, GL_LINK_STATUS, &iSuccess); glGetProgramInfoLog(uiProgramHandle, sizeof(acLog), 0, acLog); if (iSuccess == GL_FALSE) { printf("Error: Failed to link shader program!\n"); printf(acLog); printf("\n"); return 0; } glUseProgram(uiProgramHandle); delete vsSource; delete fsSource; if (gsSource != nullptr) delete gsSource; if (tcsSource != nullptr) delete tcsSource; if (tesSource != nullptr) delete tesSource; return uiProgramHandle; }
bool MapFile::OpenFile() { if ( filePath != nullptr ) { buffer chk("oMAP"); char* ext = strrchr(filePath, '.'); // Find the last occurrence of '.' if ( ext != nullptr ) { if ( strcmp(ext, ".scm") == 0 || strcmp(ext, ".scx") == 0 ) { HANDLE hMpq = MpqOpenArchiveForUpdate(filePath, MOAU_OPEN_EXISTING, 1000); if ( hMpq != NULL && hMpq != INVALID_HANDLE_VALUE ) { FileToBuffer(hMpq, "staredit\\scenario.chk", chk); MpqCloseUpdatedArchive(hMpq, 0); if ( chk.size() > 0 && ParseScenario(chk) ) { if ( VER().exists() ) { if ( VER().get<u16>(0) < 63 ) SaveType = 1; // Vanilla else if ( VER().get<u16>(0) < 205 ) SaveType = 2; // Hybrid else SaveType = 3; // Expansion } else if ( strcmp(ext, ".scx") == 0 ) SaveType = 3; // Expansion else if ( true ) // Could search for clues to map version here SaveType = 6; // Otherwise set to expansion to prevent data loss return true; } } else if ( GetLastError() == ERROR_FILE_NOT_FOUND ) CHKD_ERR("File Not Found"); else if ( GetLastError() == MPQ_ERROR_MPQ_INVALID ) CHKD_ERR("Invalid or corrupt MPQ file"); else CHKD_ERR("%d", GetLastError()); } else if ( strcmp(ext, ".chk") == 0 ) { if ( FileToBuffer(filePath, chk) ) { if ( ParseScenario(chk) ) { if ( VER().exists() ) { if ( VER().get<u16>(0) < 63 ) SaveType = 4; // Vanilla chk else if ( VER().get<u16>(0) < 205 ) SaveType = 5; // Hybrid chk else SaveType = 6; // Expansion chk } else if ( true ) // Could search for clues to map version here SaveType = 6; // Otherwise set to expansion to prevent data loss return true; } else CHKD_ERR("Parsing Failed!"); } else CHKD_ERR("Error Reading CHK File"); } } } return false; }