int32_t WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int32_t nCmdShow) { Timer globalTimer; auto& window = Win_Window::GetInstance(); window.Createwindow(); window.SetUpOpenGL(); auto& game = Game::GetInstance(); game.Init("../Assets/GameInit.txt"); auto oldTime = globalTimer.GetGlobalTime(); MSG msg; int32_t frames = 0; float frameTimer = 0.0f; int32_t framesLastSecond = 0; while (window.IsRunning()) { if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessageW(&msg); } globalTimer.ToggleTimer(); auto timeStamp = globalTimer.GetGlobalTime(); if ((timeStamp - oldTime) > TARGET_TIME) { float dt = (timeStamp - oldTime) * Timer::AreTimersRunning(); game.ProcessInput(dt); oldTime = timeStamp; game.Render(); if (frameTimer > 1.0f) { framesLastSecond = frames; frameTimer = 0.0f; frames = 0; } game.RenderText(std::to_string(framesLastSecond) + " FPS", glm::vec2(-WIDTH / 2.0f, -HEIGHT / 2.0f), 0.4f, glm::vec3(1.0f, 0.0f, 1.0f)); window.Swapwindow(); ++frames; } frameTimer += globalTimer.GetDeltaTime(); } return EXIT_SUCCESS; }
void GameSceneManager::Run() { isRunning = Initialize(); Timer timer; timer.Start(); while (isRunning) { timer.UpdateFrameTicks(); HandleEvents(); Update(timer.GetDeltaTime()); Render(); SDL_Delay(timer.GetSleepTime(fps)); } }
int main() { try { ImageLoaders::PNG loader; if (!loader.CanLoad("resources/logo.png")) { throw FatalException("Cannot load PNG images."); } Image* image = loader.Load("resources/logo.png"); WindowStyle style; style.title = U8("logo.png (") + image->GetWidth() + U8(", ") + image->GetHeight() + U8(")"); style.width = image->GetWidth(); style.height = image->GetHeight(); style.backgroundColor = Colors::black; style.resizable = false; Window window; window.Create(style); OpenGL::Context context; context.Create(&window); GL::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GL::Enable(GL_BLEND); GL::Enable(GL_ALPHA_TEST); GL::Enable(GL_TEXTURE_2D); UInt program = LoadShader(U8("resources/shader.vert"), U8("resources/shader.frag")); UInt box = MakeBox(); UInt texture; GL::GenTextures(1, &texture); GL::BindTexture(GL_TEXTURE_2D, texture); GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); GL::TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); GL::PixelStorei(GL_UNPACK_ALIGNMENT, 1); GL::TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->GetWidth(), image->GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image->GetData()); GL::BindTexture(GL_TEXTURE_2D, 0); Timer timer; Float32 hue = 0.0f; while (window.IsOpen()) { Timer::Sleep(1); hue = Math::Mod(hue + timer.GetDeltaTime() * 45, 360.0f); context.ClearColor(Color::MakeHSL(hue, 40, 255)); context.Viewport(0, 0, window.GetWidth(), window.GetHeight()); context.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT); GL::UseProgram(program); GL::UniformMatrix4fv(GL::GetUniformLocation(program, "orthoMatrix"), 1, false, Matrix::MakeOrtho(0, 1, 1, 0).GetData()); GL::UniformMatrix4fv(GL::GetUniformLocation(program, "modelMatrix"), 1, false, Matrix::Identity().GetData()); GL::Uniform1i(GL::GetUniformLocation(program, "texture"), 0); GL::ActiveTexture(GL_TEXTURE0); GL::BindTexture(GL_TEXTURE_2D, texture); GL::BindVertexArray(box); GL::DrawArrays(GL_QUADS, 0, 4); GL::BindVertexArray(0); GL::UseProgram(0); context.SwapBuffers(); Window::Update(); } } catch (Exception& e) { std::cout << e.what() << std::endl; std::cout << std::hex << "Error code: 0x" << e.unique << std::dec << " (decimal " << e.unique << ")" << std::endl; } }
int main() { try { Image image; image.Load("resources/jattabox.png"); WindowStyle style; style.title = U8("CGUL - 3D World"); style.size = UCoord32(640, 480); style.backgroundColor = Colors::black; style.resizable = true; Window window; window.Create(style); OpenGL::Context context; context.Create(&window); GL::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GL::Enable(GL_BLEND); GL::Enable(GL_ALPHA_TEST); GL::Enable(GL_TEXTURE_2D); GL::Enable(GL_DEPTH_TEST); GL::Enable(GL_CULL_FACE); glCullFace(GL_BACK); UIntN program = LoadShader(U8("resources/shader.vert"), U8("resources/shader.frag")); UIntN box = MakeBox(); UIntN texture; GL::GenTextures(1, &texture); GL::BindTexture(GL_TEXTURE_2D, texture); GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); GL::TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); GL::PixelStorei(GL_UNPACK_ALIGNMENT, 1); GL::TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.GetWidth(), image.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.GetData< void >()); GL::BindTexture(GL_TEXTURE_2D, 0); Timer timer; Float32 hue = 0.0f; while (window.IsOpen()) { Float32 deltaTime = timer.GetDeltaTime(); Timer::Sleep(1); hue = Math::Mod(hue + deltaTime * 0.125f, 1.0f); context.ClearColor(Color::MakeHSV(hue, 0.156862745098039f, 1.0f)); Float32 movement = 1 + Math::Cos(hue / 10.0f); static Float32 rot = 0; rot += deltaTime / 1.0f; context.Viewport(0, 0, window.GetWidth(), window.GetHeight()); context.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT); GL::UseProgram(program); //GL::UniformMatrix4fv(GL::GetUniformLocation(program, "orthoMatrix"), 1, false, Matrix::MakeOrtho2D(0, 1280, 768, 0).GetData()); MatrixF view = MatrixF::MakeLookAt(Vector3F(Math::Cos(rot) * 50, Math::Cos(rot) * 50, Math::Sin(rot) * 50), Vector3F(0, 0, 0), Vector3F(0, -1, 0)); MatrixF projection = MatrixF::MakePerspective(45.0, window.GetWidth() / (Float32)window.GetHeight(), 1, 1000); MatrixF vp = view * projection; MatrixF model; model = MatrixF::MakeScaling(Vector3F(20, 20, 20)) * model; //Matrix wvp = model * Matrix::MakeOrtho2D(0, 1280, 768, 0); MatrixF wvp = model * vp; GL::UniformMatrix4fv(GL::GetUniformLocation(program, "matrix"), 1, false, wvp.GetData()); GL::Uniform1i(GL::GetUniformLocation(program, "texture"), 0); GL::ActiveTexture(GL_TEXTURE0); GL::BindTexture(GL_TEXTURE_2D, texture); GL::BindVertexArray(box); GL::DrawArrays(GL_QUADS, 0, 24); GL::BindVertexArray(0); GL::UseProgram(0); context.SwapBuffers(); Window::Update(); } } catch (Exception& e) { std::cout << e.what() << std::endl; std::cout << std::hex << "Error code: 0x" << e.unique << std::dec << " (decimal " << e.unique << ")" << std::endl; } }
int main(int _argc, char** _argv) { bool fail = false; if (SDL_Init(SDL_INIT_VIDEO) < 0) { LOG_ERR("SDL_Init() failed"); return 0; } 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_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); SDL_WM_SetCaption("Drash", nullptr); if (SDL_SetVideoMode(gWindowWidth, gWindowHeight, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_GL_DOUBLEBUFFER) == nullptr) { LOG_ERR("SDL_SetVideoMode() failed"); fail = true; } glViewport(0, 0, gWindowWidth, gWindowHeight); greng::Greng greng; greng.GetViewport().SetSize({ gWindowWidth, gWindowHeight }); if (glewInit() != GLEW_OK) { LOG_ERR("glewInit() failed"); fail = true; } int img_flags = IMG_INIT_PNG; if (IMG_Init(img_flags) != img_flags) { LOG_ERR("IMG_Init() failed"); fail = true; } LOG_INFO("OpenGL version: " << (const char*)glGetString(GL_VERSION)); LOG_INFO("Vendor: " << (const char*)glGetString(GL_VENDOR)); LOG_INFO("GLSL version: " << (const char*)glGetString( GL_SHADING_LANGUAGE_VERSION)); App* app = nullptr; if (fail == false) { for (int i = 0; i < _argc; i++) { if (strcmp("--test", _argv[i]) == 0) { if (++i < _argc) { app = test::StartApp(greng, _argv[i]); if (app == nullptr) { LOG_ERR("drash::test::StartApp() failed"); } break; } } } } Timer timer; timer.Reset(true); if (fail == false && app != nullptr) { glViewport(0, 0, gWindowWidth, gWindowHeight); app->GetGreng().GetCameraManager().SetAspectRatio(gWindowWidth / gWindowHeight); greng.GetViewport().SetSize({ gWindowWidth, gWindowHeight }); app->GetUISystem().SetAspectRatio(gWindowWidth / gWindowHeight); app->GetUISystem().SetWidth(gWindowWidth); bool exit = false; SDL_Event e; app->SetQuitHandler([&exit]() { exit = true; }); auto update_cursor = [&app](int _x, int _y) { Vec2f pos(_x, _y); WindowSpaceToScreenSpace(pos); app->SetCursorPos(pos); int x; int y; app->GetUISystem().ScreenSpaceToUISpace(pos, x, y); app->GetUISystem().SetCursorPos(x, y); }; for (;;) { while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { exit = true; break; } else if (e.type == SDL_KEYDOWN) { app->GetEventSystem().BeginEvent( ConvertKey(e.key.keysym.sym)); } else if (e.type == SDL_KEYUP) { app->GetEventSystem().EndEvent( ConvertKey(e.key.keysym.sym)); } else if (e.type == SDL_MOUSEBUTTONDOWN) { update_cursor(e.button.x, e.button.y); app->GetUISystem().BeginEvent(); app->GetEventSystem().BeginEvent( ConvertButton(e.button.button)); } else if (e.type == SDL_MOUSEBUTTONUP) { update_cursor(e.button.x, e.button.y); app->GetUISystem().EndEvent(); app->GetEventSystem().EndEvent( ConvertButton(e.button.button)); } else if (e.type == SDL_MOUSEMOTION) { update_cursor(e.motion.x, e.motion.y); } else if (e.type == SDL_VIDEORESIZE) { gWindowWidth = e.resize.w; gWindowHeight = e.resize.h; if (SDL_SetVideoMode(gWindowWidth, gWindowHeight, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_GL_DOUBLEBUFFER) == nullptr) { LOG_ERR("SDL_SetVideoMode() failed"); exit = true; } glViewport(0, 0, gWindowWidth, gWindowHeight); app->GetGreng().GetCameraManager().SetAspectRatio( gWindowWidth / gWindowHeight); greng.GetViewport().SetSize( { gWindowWidth, gWindowHeight }); app->GetUISystem().SetAspectRatio(gWindowWidth / gWindowHeight); app->GetUISystem().SetWidth(gWindowWidth); } } timer.Tick(); app->Step(timer.GetDeltaTime()); glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); app->Render(); SDL_GL_SwapBuffers(); if (exit == true) { break; } } delete app; app = nullptr; } IMG_Quit(); SDL_Quit(); return 0; }