void ResourceManager::loadResources(std::vector<std::string> fileNames) { for (auto file : fileNames) { if (file.find(".ttf") != -1) { TTF_Font * font = TTF_OpenFont(file.c_str(), 36); // hardcode font size ? if (nullptr == font) throw EngineException(TTF_GetError(), file); fonts[file] = font; } else if (file.find(".wav") != -1) { Mix_Chunk * sound = Mix_LoadWAV(file.c_str()); if (nullptr == sound) throw EngineException(Mix_GetError(), file); sounds[file] = sound; } else if (file.find(".mp3") != -1) { Mix_Music * mp3 = Mix_LoadMUS(file.c_str()); if (nullptr == mp3) throw EngineException(Mix_GetError(), file); mp3files[file] = mp3; } else if (file.find(".png") != -1) { SDL_Surface * surf = IMG_Load(file.c_str()); if (nullptr == surf) throw EngineException(IMG_GetError(), file); textures[file] = GFX::createGLTextureFromSurface(surf); SDL_FreeSurface(surf); if (0 == textures[file]) throw EngineException("Failed to create GL texture from surface:", file); } } }
void pip::Shader::setSampler(const string &uniformName, Texture *texture) { if(this->samplersToUnitMap.count(uniformName) == 0) { this->samplersToUnitMap[uniformName] = currentTextureUnit; currentTextureUnit++; } if(this->samplersToLocationsMap.count(uniformName) == 0) { GLint tex_sampler = glGetUniformLocationARB(this->prog, uniformName.c_str()); std::cout << "location: " <<tex_sampler << std::endl; string err; getOGLErrors(err); if(!err.empty()) throw EngineException("Errors while getting texture sampler:\n" + err); this->samplersToLocationsMap[uniformName] = tex_sampler; } glActiveTexture(GL_TEXTURE0 + this->samplersToUnitMap[uniformName]); // Activate texture unit. texture->bind(); glUniform1iARB(this->samplersToLocationsMap[uniformName], this->samplersToUnitMap[uniformName]); // TODO But what, if referring to inexistent texture unit? glActiveTexture(GL_TEXTURE0); string err; getOGLErrors(err); if(!err.empty()) throw EngineException("Errors while setting texture sampler:\n" + err); }
void Engine::CreateWindow(const std::size_t Length, const std::size_t Width, const std::string& Title) throw(EngineException){ mWindowWidth = Width; mWindowLength = Length; mWindow.create(sf::VideoMode(mWindowLength, mWindowLength), Title); if (!mWindow.isOpen()) throw EngineException("Engine::CreateWindow: Can't create Window"); }
void Engines::setEngineDefault(Engine &engine, Engine::Algorithm algorithm) throw (EngineException) { unsigned int flag; ENGINE *e; e = engine.getEngine(); if (!e) { throw EngineException(EngineException::INVALID_ENGINE, "Engines::setEngineDefault"); } flag = Engines::getAlgorithmFlags(algorithm); if (!(ENGINE_set_default(e, flag))) { throw EngineException(EngineException::INTERNAL_ERROR, "Engines::setEngineDefault"); } }
void Engine::CreateSplashScreen(const std::size_t Length, const std::size_t Width, const std::string& Title) throw(EngineException){ mWindowWidth = Width; mWindowLength = Length; mWindow.create(sf::VideoMode(mWindowLength, mWindowLength), Title, sf::Style::None); if (!mWindow.isOpen()) throw EngineException("Engine::CreateWindow: Can't create Window"); mWindow.setPosition(sf::Vector2i(400, 100)); }
void Engine::Init(){ if((direct3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL){ // Створення IDirect3D9 інтерфейсу throw EngineException(TEXT("Помилка створення IDirect3D9 інтерфейсу")); } D3DDISPLAYMODE DisplayMode; if(FAILED( direct3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode) ) ){ // Отримання параметрів режиму throw EngineException(TEXT("Помилка отримання параметрів режиму")); } ZeroMemory(&direct3DParameter, sizeof(direct3DParameter)); // Перевірка на підтримку зглажування if(SUCCEEDED(direct3D->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, FALSE, D3DMULTISAMPLE_8_SAMPLES, NULL))){ direct3DParameter.MultiSampleType = D3DMULTISAMPLE_8_SAMPLES; }else if(SUCCEEDED(direct3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, FALSE, D3DMULTISAMPLE_4_SAMPLES, NULL))){ direct3DParameter.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES; }else if(SUCCEEDED(direct3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, DisplayMode.Format, FALSE, D3DMULTISAMPLE_2_SAMPLES, NULL))){ direct3DParameter.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES; }else { direct3DParameter.MultiSampleType = D3DMULTISAMPLE_NONE; } direct3DParameter.Windowed = true; // Встановлення віконого режиму direct3DParameter.SwapEffect = D3DSWAPEFFECT_DISCARD; direct3DParameter.BackBufferFormat = DisplayMode.Format; direct3DParameter.EnableAutoDepthStencil = TRUE; direct3DParameter.AutoDepthStencilFormat = D3DFMT_D16; direct3DParameter.PresentationInterval = D3DPRESENT_INTERVAL_ONE; RECT windowClient; GetClientRect(hWnd, &windowClient); direct3DParameter.BackBufferWidth = windowClient.right; direct3DParameter.BackBufferHeight = windowClient.bottom; // Створення пристрою Direct3DDevice if(FAILED(direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &direct3DParameter, &direct3DDevice))){ throw EngineException(TEXT("Помилка створення пристрою Direct3D")); } SetParams(); };
Engine* Engines::getEngineById(std::string id) throw (EngineException) { ENGINE *eng; eng = ENGINE_by_id(id.c_str()); if (!eng) { throw EngineException(EngineException::ENGINE_NOT_FOUND, "Engines::getEngineById", true); } return new Engine(eng); }
GraphicsEngine::GraphicsEngine() : fpsAverage(0), fpsPrevious(0), fpsStart(0), fpsEnd(0) { window = SDL_CreateWindow(DEFAULT_WINDOW_TITLE.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); if (nullptr == window) throw EngineException("Failed to create window", SDL_GetError()); glContext = SDL_GL_CreateContext(window); if (nullptr == glContext) throw EngineException("Failed to create OpenGL context", SDL_GetError()); // although not necessary, SDL doc says to prevent hiccups load it before using if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG) throw EngineException("Failed to init SDL_image - PNG", IMG_GetError()); if (TTF_Init() < 0) throw EngineException("Failed to init SDL_ttf", TTF_GetError()); if (glewInit() != GLEW_OK) throw EngineException("Failed to init GLEW"); initGL(); textureBackground = IMG_Load("res/trans128.png"); if (nullptr == textureBackground) throw EngineException("Failed to load texture background", IMG_GetError()); }
void Engine::ResizeScene(int width, int height){ BeforeResetDevice(); // Встановлюємо нові розміри вікна виведення і попереджуємо встановлення 0 значень direct3DParameter.BackBufferWidth = (width == 0)? 1 : width; direct3DParameter.BackBufferHeight = (height == 0)? 1 : height; if(FAILED(direct3DDevice->Reset(&direct3DParameter))){ throw EngineException(TEXT("Неможливо переініціалізувати Direct3D")); } SetParams(); AfterResetDevice(); };
std::vector<std::string> Engines::getEnginesList() throw (EngineException) { ENGINE *e; const char *name; std::vector<std::string> ret; e = ENGINE_get_first(); if (!e) { throw EngineException(EngineException::ENGINE_NOT_FOUND, "Engines::getEnginesList"); } name = ENGINE_get_name(e); ret.push_back(name); while ((e = ENGINE_get_next(e)) != NULL) { name = ENGINE_get_name(e); ret.push_back(name); } return ret; }
Engine* Engines::getEngineDefault(Engine::Algorithm algorithm) throw (EngineException) { ENGINE *eng = NULL; switch (algorithm) { case Engine::RSA: eng = ENGINE_get_default_RSA(); break; case Engine::DSA: eng = ENGINE_get_default_DSA(); break; // case Engines::DH: // e = ENGINE_get_default_DH(); // break; case Engine::RAND: eng = ENGINE_get_default_RAND(); break; // case Engine::ECDH: // e = ENGINE_get_default_ECDH(); // break; case Engine::ECDSA: eng = ENGINE_get_default_ECDSA(); break; // case Engine::CIPHERS: // e = ENGINE_get_default_CIPHERS(); // break; // case Engine::DIGESTS: // e = ENGINE_get_default_DIGESTS(); // break; // case Engine::STORE: case Engine::ALL: case Engine::NONE: default: break; } if (!eng) { throw EngineException(EngineException::ENGINE_NOT_FOUND, "Engines::getEngineDefault"); } return new Engine(eng); }
void pip::Shader::loadShader(const string &vertexName, const string &fragmentName) { //printLog(this->prog); // Loading vertex shader. if(this->vertexShadersMap.count(vertexName) == 0) // TODO Error checking? { this->vertexShadersMap[vertexName] = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); //printLog(this->vertexShadersMap[vertexName]); string program; if(loadFile(vertexName, program)) { const char * vv = STR_CCHR(program); std::cout << "Program: " << vv << std::endl; glShaderSourceARB(this->vertexShadersMap[vertexName], 1, &vv, NULL); /*GLenum glErr; glErr = glGetError(); while (glErr != GL_NO_ERROR) { printf("%s\n", gluErrorString(glErr)); glErr = glGetError(); }*/ //delete vv; glCompileShaderARB(this->vertexShadersMap[vertexName]); //printLog(this->vertexShadersMap[vertexName]); GLint result = GL_FALSE; glGetObjectParameterivARB(this->vertexShadersMap[vertexName],GL_COMPILE_STATUS,&result); if(result == GL_FALSE) { //printLog(this->vertexShadersMap[vertexName]); throw EngineException("Error occurred when compiling '" + vertexName + "' vertex shader."); } }else throw EngineException("Can't load '" + vertexName + "' shader!"); } // Loading fragment shader. if(this->fragmentShadersMap.count(fragmentName) == 0) { this->fragmentShadersMap[fragmentName] = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); string program; if(loadFile(fragmentName, program)) { const char * ff = STR_CCHR(program); glShaderSourceARB(this->fragmentShadersMap[fragmentName], 1, &ff,NULL); //delete ff; glCompileShaderARB(this->fragmentShadersMap[fragmentName]); GLint result = GL_FALSE; glGetObjectParameterivARB(this->fragmentShadersMap[fragmentName],GL_COMPILE_STATUS,&result); if(result == GL_FALSE) throw EngineException("Error occurred when compiling '" + fragmentName + "' fragment shader."); }else throw EngineException("Can't load '" + fragmentName + "' shader!"); } this->prog = glCreateProgramObjectARB(); // Attaching vertex shader to program object. glAttachObjectARB(this->prog, this->vertexShadersMap[vertexName]); // Attaching fragment shader to program object. glAttachObjectARB(this->prog, this->fragmentShadersMap[fragmentName]); glLinkProgramARB(this->prog); GLint result = GL_FALSE; glGetObjectParameterivARB(this->prog,GL_OBJECT_LINK_STATUS_ARB,&result); if(result == GL_FALSE) throw EngineException("Error occurred when linking shaders."); }