void Application::DestroyEnemy(Enemy * e) { if (e->GetActive()) { // Spawn explosion Explosion* ex = FetchExplosion(); if (ex) { ex->Init(e->GetX(), e->GetY()); // Reset enemy e->Reset(); } } }
int main() { // Init GLFW glfwInit(); // Create a GLFWwindow object that we can use for GLFW's functions Window window = Window(WIDTH, HEIGHT, TITLE); window.DefineViewport(); //glfwSetInputMode(window.getWindowPtr(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); /// ^(Maybe use this later) // Callback functions glfwSetKeyCallback(window.getWindowPtr() , key_callback); // Init GLEW glewExperimental = GL_TRUE; glewInit(); // Enable alpha channel transparency glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Load and compile shaders to a program Shader ShaderProgram = Shader("../deps/shaders/shadervert.vs", "../deps/shaders/shaderfrag.fs"); // Load explosion graphics extern_Explosion.Init(ShaderProgram.GetProgramID()); // Load texture/Game objects Texture2D texture_background1 = Texture2D("../deps/textures/backgroundSpace_01.1.png", PNG_RGB); SpriteMap Background = SpriteMap(texture_background1, 1.0f, 1.0f, glm::vec3(0.0f, 0.0f, 0.0f), 1.0f, BACKGROUND); Player PlayerShip; PlayerShip.Init(moveSpeed); Enemy Enemies; Enemies.Init(); // Projection matrix: ortho for 2D glm::mat4 proj = glm::ortho(0, window.getWidth(), 0, window.getHeight()); // Game loop while (!window.ShouldClose()) { double startFrame = glfwGetTime(); ///< for FPS limiting // Check if any events have been activiated and call callback function (via GLFW) glfwPollEvents(); // Clear the colorbuffer glClearColor(0.6f, 0.8f, 0.8f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Drawing */ ShaderProgram.Use(); // Background position and drawing calculations - just identity matrix glm::mat4 model; GLint modelLoc = glGetUniformLocation(ShaderProgram.GetProgramID(), "model"); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); Background.BackgroundScroll(scrollSpeed); Background.Draw(); Collision::EnemyHit(&PlayerShip, &Enemies); Collision::PlayerHit(&PlayerShip, &Enemies); Collision::ShipsCollide(&PlayerShip, &Enemies); PlayerShip.Move(keys); PlayerShip.AddShots(keys); PlayerShip.Draw(ShaderProgram.GetProgramID()); Enemies.Move(); Enemies.Shoot(PlayerShip.GetPosition()); Enemies.Draw(ShaderProgram.GetProgramID()); extern_Explosion.Draw(); // FPS Calculation/Limiting float fps = CalculateFPS(); static int printFPS = 0; if (printFPS == 100) { Enemies.Add(EN_0, PlayerShip.GetPosition()); Enemies.Add(EN_1, PlayerShip.GetPosition()); std::cout << fps << std::endl; printFPS = 0; } else { printFPS++; } LimitFPS(FPS, startFrame); if (PlayerShip.GetLives() <= 0) { window.Close(); } // Swap the screen buffers window.SwapBuffers(); } Background.Delete(); PlayerShip.Delete(); Enemies.DeleteAll(); extern_Explosion.DeleteAll(); // Close GLFW glfwTerminate(); return 0; }