From b53826079429939cdfbda073608cb85be8ba0738 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 7 Mar 2015 11:29:57 -0500 Subject: Created entity-component system Also tweaked the bloom flicker, tweaked the scanline texture, created a second test map, and created some currently unused sound effects. --- src/game.cpp | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/game.cpp (limited to 'src/game.cpp') diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..2db0a2c --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,124 @@ +#include "game.h" +#include "renderer.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(); + + auto player = std::make_shared(world); + + auto player_input = std::make_shared(*player); + 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); + player->addComponent(player_physics); + + auto player_anim = std::make_shared(*player, *player_physics); + player->addComponent(player_anim); + + world->addEntity(player); + world->player = player; + + loadMap(m); +} + +Game::~Game() +{ + if (world != 0) + { + delete world; + } + + if (nextWorld != 0) + { + delete nextWorld; + } + + 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)) + { + Game::getInstance().shouldQuit = true; + } + + Game::getInstance().input(key, action); +} + +void Game::execute() +{ + glfwSetKeyCallback(window, key_callback); + + Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT); + + while (!(shouldQuit || glfwWindowShouldClose(window))) + { + // Should we load a new world? + if (nextWorld != 0) + { + delete world; + world = nextWorld; + world->player->world = world; + nextWorld = 0; + } + + // Handle input + glfwPollEvents(); + + // Tick! + world->tick(); + + // Do rendering + world->render(buffer); + renderScreen(buffer); + } + + destroyTexture(buffer); +} + +void Game::input(int key, int action) +{ + if (world != NULL) + { + world->input(key, action); + } +} + +void Game::loadMap(Map* map) +{ + nextWorld = new World(); + + nextWorld->player = world->player; + + auto mapEn = std::make_shared(nextWorld); + + auto map_render = std::make_shared(*mapEn, map); + mapEn->addComponent(map_render); + + auto map_collision = std::make_shared(*mapEn, map); + mapEn->addComponent(map_collision); + nextWorld->bodies.push_back(map_collision.get()); + + nextWorld->addEntity(mapEn); + nextWorld->addEntity(nextWorld->player); +} -- cgit 1.4.1