From 7f0e8c7ef70c62814c274f110367db92f01cbb26 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 10 Mar 2015 00:41:59 -0400 Subject: C++11'd everything! Also moved location information from physics components into entity. --- src/game.cpp | 135 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 62 insertions(+), 73 deletions(-) (limited to 'src/game.cpp') diff --git a/src/game.cpp b/src/game.cpp index 2db0a2c..cbbae06 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,124 +1,113 @@ #include "game.h" #include "renderer.h" +#include "components.h" Game::Game() -{ - window = initRenderer(); - glfwSwapInterval(1); - - m = new Map("../maps/embarass.txt"); - m2 = new Map("../maps/second.txt"); - - m->setLeftMap(m2); - m2->setRightMap(m); - - world = new World(); +{ + m.setLeftMap(&m2); + m2.setRightMap(&m); - auto player = std::make_shared(world); + player = std::make_shared(); + player->position = std::make_pair(100.0,100.0); + player->size = std::make_pair(10.0,12.0); - auto player_input = std::make_shared(*player); + auto player_input = std::make_shared(); player->addComponent(player_input); - auto player_physics = std::make_shared(*player); - player_physics->position = std::make_pair(100.0,100.0); - player_physics->size = std::make_pair(10.0,12.0); + auto player_physics = std::make_shared(); player->addComponent(player_physics); - auto player_anim = std::make_shared(*player, *player_physics); + auto player_anim = std::make_shared(); player->addComponent(player_anim); - world->addEntity(player); - world->player = player; - loadMap(m); } -Game::~Game() +void key_callback(GLFWwindow* window, int key, int, int action, int) { - if (world != 0) - { - delete world; - } + Game* game = (Game*) glfwGetWindowUserPointer(window); - if (nextWorld != 0) + if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS)) { - delete nextWorld; + game->shouldQuit = true; } - delete m; - delete m2; - - destroyRenderer(); -} - -void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) -{ - (void)window; - (void)scancode; - (void)mods; - - if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS)) + for (auto entity : game->entities) { - Game::getInstance().shouldQuit = true; + entity->input(*game, key, action); } - - Game::getInstance().input(key, action); } -void Game::execute() +void Game::execute(GLFWwindow* window) { + glfwSwapInterval(1); + glfwSetWindowUserPointer(window, this); glfwSetKeyCallback(window, key_callback); - Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT); + Texture buffer(GAME_WIDTH, GAME_HEIGHT); + double lastTime = glfwGetTime(); + int nbFrames = 0; while (!(shouldQuit || glfwWindowShouldClose(window))) { + double currentTime = glfwGetTime(); + nbFrames++; + if (currentTime - lastTime >= 1.0) + { + printf("%f ms/frame\n", 1000.0/double(nbFrames)); + nbFrames = 0; + lastTime += 1.0; + } + // Should we load a new world? - if (nextWorld != 0) + if (newWorld) { - delete world; - world = nextWorld; - world->player->world = world; - nextWorld = 0; + newWorld = false; + entities.clear(); + entities = std::move(nextEntities); } // Handle input glfwPollEvents(); // Tick! - world->tick(); + for (auto entity : entities) + { + entity->tick(*this); + } // Do rendering - world->render(buffer); - renderScreen(buffer); - } - - destroyTexture(buffer); -} + buffer.fill(buffer.entirety(), 0, 0, 0); + for (auto entity : entities) + { + entity->render(*this, buffer); + } -void Game::input(int key, int action) -{ - if (world != NULL) - { - world->input(key, action); + buffer.renderScreen(); } } -void Game::loadMap(Map* map) +void Game::loadMap(Map& map) { - nextWorld = new World(); - - nextWorld->player = world->player; - - auto mapEn = std::make_shared(nextWorld); + auto mapEn = std::make_shared(); - auto map_render = std::make_shared(*mapEn, map); + auto map_render = std::make_shared(map); mapEn->addComponent(map_render); - auto map_collision = std::make_shared(*mapEn, map); + auto map_collision = std::make_shared(map); mapEn->addComponent(map_collision); - nextWorld->bodies.push_back(map_collision.get()); - nextWorld->addEntity(mapEn); - nextWorld->addEntity(nextWorld->player); + nextEntities.clear(); + nextEntities.push_back(mapEn); + nextEntities.push_back(player); + + newWorld = true; +} + +void Game::detectCollision(Entity& collider, std::pair old_position) +{ + for (auto entity : entities) + { + entity->detectCollision(*this, collider, old_position); + } } -- cgit 1.4.1