///////////////////////////////////////////////////////////////////// /// Create level length data structure and write a place holder into stream. /// It might throw an IOException. /// @param levelLength A reference to an integer array, large enough to save the relative file positions of all PGF levels /// @return number of bytes written into stream UINT32 CEncoder::WriteLevelLength(UINT32*& levelLength) { // renew levelLength delete[] levelLength; levelLength = new(std::nothrow) UINT32[m_nLevels]; if (!levelLength) ReturnWithError(InsufficientMemory); for (UINT8 l = 0; l < m_nLevels; l++) levelLength[l] = 0; m_levelLength = levelLength; // save level length file position m_levelLengthPos = m_stream->GetPos(); // write dummy levelLength int count = m_nLevels*WordBytes; m_stream->Write(&count, m_levelLength); // save current file position SetBufferStartPos(); return count; }
int XyEngine::CreateWindow(int width, int height, char* title, float frameRate) { double StartTime = Time::GetTime(); Log("XyEngine is Loading..."); this->m_windowWidth = width; this->m_windowHeight = height; this->m_middleWidth = m_windowWidth / 2; this->m_middleHeight = m_windowHeight / 2; this->m_FrameRate = frameRate; this->m_frameTime = 1.0 / this->m_FrameRate; if (SDL_Init(SDL_INIT_EVERYTHING) != 0) return ReturnWithError("SDL Failed To Init!"); m_Window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, m_windowWidth, m_windowHeight, SDL_WINDOW_OPENGL); if (m_Window == NULL) { SDL_Quit(); return ReturnWithError("SDL Failed To Create the Window!"); } glcontext = SDL_GL_CreateContext(m_Window); if (glewInit() != GLEW_OK) return ReturnWithError("GLEW Failed to Init!"); if (TTF_Init() != 0) return ReturnWithError("TTF Failed to Init!"); if (!IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG)) return ReturnWithError("IMG Failed to Init!"); m_running = true; double lastTime = Time::GetTime(); double unprocessedTime = 0; double frameCounter = 0; frames = 0; glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_MULTISAMPLE_ARB); m_InitFunc(); m_pPhysicsThread = SDL_CreateThread(StaticPhysicsThread, "PhysicsThread", this); if (NULL == m_pPhysicsThread) { ReturnWithError("Physics Thread Failed to create"); } else { Log("Physics Thread Created"); } double EndTime = Time::GetTime() - StartTime; std::cout << "Time Taken to load XyEngine: " << EndTime << std::endl; SDL_Event event; while (m_running) { bool render = false; double startTime = Time::GetTime(); double passedTime = startTime - lastTime; lastTime = startTime; unprocessedTime += passedTime; frameCounter += passedTime; if (frameCounter >= 1.0) { fpsRate = frames; frames = 0; frameCounter = 0; } while (unprocessedTime > m_frameTime) { render = true; while (SDL_PollEvent(&event)) { m_InputFunc(event); switch (event.type) { case SDL_QUIT: m_running = false; break; case SDL_MOUSEBUTTONDOWN: mousein = true; SDL_ShowCursor(SDL_DISABLE); break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_ESCAPE: mousein = false; SDL_ShowCursor(SDL_ENABLE); break; } } } unprocessedTime -= m_frameTime; } if (render) { if (mousein) SDL_WarpMouseInWindow(m_Window, m_middleWidth, m_middleHeight); m_RenderFunc(); SDL_GL_SwapWindow(m_Window); frames++; } else { SDL_Delay(1); } } return EXIT_SUCCESS; }
bool Compiler::CompileShaderPrimary( const ShaderInput& inputDesc, const ShaderOutput& outputDesc, Reflection::ReflectionData* reflectionData) { /* Validate arguments */ ValidateArguments(inputDesc, outputDesc); /* ----- Pre-processing ----- */ timePoints_.preprocessor = Time::now(); std::unique_ptr<IncludeHandler> stdIncludeHandler; if (!inputDesc.includeHandler) stdIncludeHandler = std::unique_ptr<IncludeHandler>(new IncludeHandler()); auto includeHandler = (inputDesc.includeHandler != nullptr ? inputDesc.includeHandler : stdIncludeHandler.get()); std::unique_ptr<PreProcessor> preProcessor; if (IsLanguageHLSL(inputDesc.shaderVersion)) preProcessor = MakeUnique<PreProcessor>(*includeHandler, log_); else if (IsLanguageGLSL(inputDesc.shaderVersion)) preProcessor = MakeUnique<GLSLPreProcessor>(*includeHandler, log_); const bool writeLineMarksInPP = (!outputDesc.options.preprocessOnly || outputDesc.formatting.lineMarks); const bool writeLineMarkFilenamesInPP = (!outputDesc.options.preprocessOnly || IsLanguageHLSL(inputDesc.shaderVersion)); auto processedInput = preProcessor->Process( std::make_shared<SourceCode>(inputDesc.sourceCode), inputDesc.filename, writeLineMarksInPP, writeLineMarkFilenamesInPP, ((inputDesc.warnings & Warnings::PreProcessor) != 0) ); if (reflectionData) reflectionData->macros = preProcessor->ListDefinedMacroIdents(); if (!processedInput) return ReturnWithError(R_PreProcessingSourceFailed); if (outputDesc.options.preprocessOnly) { (*outputDesc.sourceCode) << processedInput->rdbuf(); return true; } /* ----- Parsing ----- */ timePoints_.parser = Time::now(); std::unique_ptr<IntrinsicAdept> intrinsicAdpet; ProgramPtr program; if (IsLanguageHLSL(inputDesc.shaderVersion)) { /* Establish intrinsic adept */ intrinsicAdpet = MakeUnique<HLSLIntrinsicAdept>(); /* Parse HLSL input code */ HLSLParser parser(log_); program = parser.ParseSource( std::make_shared<SourceCode>(std::move(processedInput)), outputDesc.nameMangling, inputDesc.shaderVersion, outputDesc.options.rowMajorAlignment, ((inputDesc.warnings & Warnings::Syntax) != 0) ); } else if (IsLanguageGLSL(inputDesc.shaderVersion)) { /* Establish intrinsic adept */ #if 0 intrinsicAdpet = MakeUnique<GLSLIntrinsicAdept>(); #else //!!! intrinsicAdpet = MakeUnique<HLSLIntrinsicAdept>(); #endif /* Parse GLSL input code */ GLSLParser parser(log_); program = parser.ParseSource( std::make_shared<SourceCode>(std::move(processedInput)), outputDesc.nameMangling, inputDesc.shaderVersion, ((inputDesc.warnings & Warnings::Syntax) != 0) ); } if (!program) return ReturnWithError(R_ParsingSourceFailed); /* ----- Context analysis ----- */ timePoints_.analyzer = Time::now(); bool analyzerResult = false; if (IsLanguageHLSL(inputDesc.shaderVersion)) { /* Analyse HLSL program */ HLSLAnalyzer analyzer(log_); analyzerResult = analyzer.DecorateAST(*program, inputDesc, outputDesc); } /* Print AST */ if (outputDesc.options.showAST) { ASTPrinter printer; printer.PrintAST(program.get()); } if (!analyzerResult) return ReturnWithError(R_AnalyzingSourceFailed); /* Optimize AST */ timePoints_.optimizer = Time::now(); if (outputDesc.options.optimize) { Optimizer optimizer; optimizer.Optimize(*program); } /* ----- Code generation ----- */ timePoints_.generation = Time::now(); bool generatorResult = false; if (IsLanguageGLSL(outputDesc.shaderVersion) || IsLanguageESSL(outputDesc.shaderVersion) || IsLanguageVKSL(outputDesc.shaderVersion)) { /* Generate GLSL output code */ GLSLGenerator generator(log_); generatorResult = generator.GenerateCode(*program, inputDesc, outputDesc, log_); } if (!generatorResult) return ReturnWithError(R_GeneratingOutputCodeFailed); /* ----- Code reflection ----- */ timePoints_.reflection = Time::now(); if (reflectionData) { ReflectionAnalyzer reflectAnalyzer(log_); reflectAnalyzer.Reflect( *program, inputDesc.shaderTarget, *reflectionData, ((inputDesc.warnings & Warnings::CodeReflection) != 0) ); } return true; }
////////////////////////////////////////////////////// /// Write pre-header, header, postHeader, and levelLength. /// It might throw an IOException. /// @param stream A PGF stream /// @param preHeader A already filled in PGF pre-header /// @param header An already filled in PGF header /// @param postHeader [in] An already filled in PGF post-header (containing color table, user data, ...) /// @param userDataPos [out] File position of user data /// @param useOMP If true, then the encoder will use multi-threading based on openMP CEncoder::CEncoder(CPGFStream* stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader& postHeader, UINT64& userDataPos, bool useOMP) : m_stream(stream) , m_bufferStartPos(0) , m_currLevelIndex(0) , m_nLevels(header.nLevels) , m_favorSpeed(false) , m_forceWriting(false) #ifdef __PGFROISUPPORT__ , m_roi(false) #endif { ASSERT(m_stream); int count; m_lastMacroBlock = 0; m_levelLength = nullptr; // set number of threads #ifdef LIBPGF_USE_OPENMP m_macroBlockLen = omp_get_num_procs(); #else m_macroBlockLen = 1; #endif if (useOMP && m_macroBlockLen > 1) { #ifdef LIBPGF_USE_OPENMP omp_set_num_threads(m_macroBlockLen); #endif // create macro block array m_macroBlocks = new(std::nothrow) CMacroBlock*[m_macroBlockLen]; if (!m_macroBlocks) ReturnWithError(InsufficientMemory); for (int i=0; i < m_macroBlockLen; i++) m_macroBlocks[i] = new CMacroBlock(this); m_currentBlock = m_macroBlocks[m_lastMacroBlock++]; } else { m_macroBlocks = 0; m_macroBlockLen = 1; m_currentBlock = new CMacroBlock(this); } // save file position m_startPosition = m_stream->GetPos(); // write preHeader preHeader.hSize = __VAL(preHeader.hSize); count = PreHeaderSize; m_stream->Write(&count, &preHeader); // write file header header.height = __VAL(header.height); header.width = __VAL(header.width); count = HeaderSize; m_stream->Write(&count, &header); // write postHeader if (header.mode == ImageModeIndexedColor) { // write color table count = ColorTableSize; m_stream->Write(&count, (void *)postHeader.clut); } // save user data file position userDataPos = m_stream->GetPos(); if (postHeader.userDataLen) { if (postHeader.userData) { // write user data count = postHeader.userDataLen; m_stream->Write(&count, postHeader.userData); } else { m_stream->SetPos(FSFromCurrent, count); } } // save level length file position m_levelLengthPos = m_stream->GetPos(); }