summary refs log tree commit diff stats
path: root/src/game.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2015-03-10 00:41:59 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2015-03-10 00:41:59 -0400
commit7f0e8c7ef70c62814c274f110367db92f01cbb26 (patch)
tree750bcfec923a826609034ebe9185014521400c68 /src/game.cpp
parentb53826079429939cdfbda073608cb85be8ba0738 (diff)
downloadtherapy-7f0e8c7ef70c62814c274f110367db92f01cbb26.tar.gz
therapy-7f0e8c7ef70c62814c274f110367db92f01cbb26.tar.bz2
therapy-7f0e8c7ef70c62814c274f110367db92f01cbb26.zip
C++11'd everything!
Also moved location information from physics components into entity.
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp135
1 files changed, 62 insertions, 73 deletions
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 @@
1#include "game.h" 1#include "game.h"
2#include "renderer.h" 2#include "renderer.h"
3#include "components.h"
3 4
4Game::Game() 5Game::Game()
5{ 6{
6 window = initRenderer(); 7 m.setLeftMap(&m2);
7 glfwSwapInterval(1); 8 m2.setRightMap(&m);
8
9 m = new Map("../maps/embarass.txt");
10 m2 = new Map("../maps/second.txt");
11
12 m->setLeftMap(m2);
13 m2->setRightMap(m);
14
15 world = new World();
16 9
17 auto player = std::make_shared<Entity>(world); 10 player = std::make_shared<Entity>();
11 player->position = std::make_pair(100.0,100.0);
12 player->size = std::make_pair(10.0,12.0);
18 13
19 auto player_input = std::make_shared<UserMovementComponent>(*player); 14 auto player_input = std::make_shared<UserMovementComponent>();
20 player->addComponent(player_input); 15 player->addComponent(player_input);
21 16
22 auto player_physics = std::make_shared<PlayerPhysicsComponent>(*player); 17 auto player_physics = std::make_shared<PlayerPhysicsComponent>();
23 player_physics->position = std::make_pair(100.0,100.0);
24 player_physics->size = std::make_pair(10.0,12.0);
25 player->addComponent(player_physics); 18 player->addComponent(player_physics);
26 19
27 auto player_anim = std::make_shared<PlayerSpriteComponent>(*player, *player_physics); 20 auto player_anim = std::make_shared<PlayerSpriteComponent>();
28 player->addComponent(player_anim); 21 player->addComponent(player_anim);
29 22
30 world->addEntity(player);
31 world->player = player;
32
33 loadMap(m); 23 loadMap(m);
34} 24}
35 25
36Game::~Game() 26void key_callback(GLFWwindow* window, int key, int, int action, int)
37{ 27{
38 if (world != 0) 28 Game* game = (Game*) glfwGetWindowUserPointer(window);
39 {
40 delete world;
41 }
42 29
43 if (nextWorld != 0) 30 if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS))
44 { 31 {
45 delete nextWorld; 32 game->shouldQuit = true;
46 } 33 }
47 34
48 delete m; 35 for (auto entity : game->entities)
49 delete m2;
50
51 destroyRenderer();
52}
53
54void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
55{
56 (void)window;
57 (void)scancode;
58 (void)mods;
59
60 if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS))
61 { 36 {
62 Game::getInstance().shouldQuit = true; 37 entity->input(*game, key, action);
63 } 38 }
64
65 Game::getInstance().input(key, action);
66} 39}
67 40
68void Game::execute() 41void Game::execute(GLFWwindow* window)
69{ 42{
43 glfwSwapInterval(1);
44 glfwSetWindowUserPointer(window, this);
70 glfwSetKeyCallback(window, key_callback); 45 glfwSetKeyCallback(window, key_callback);
71 46
72 Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT); 47 Texture buffer(GAME_WIDTH, GAME_HEIGHT);
73 48
49 double lastTime = glfwGetTime();
50 int nbFrames = 0;
74 while (!(shouldQuit || glfwWindowShouldClose(window))) 51 while (!(shouldQuit || glfwWindowShouldClose(window)))
75 { 52 {
53 double currentTime = glfwGetTime();
54 nbFrames++;
55 if (currentTime - lastTime >= 1.0)
56 {
57 printf("%f ms/frame\n", 1000.0/double(nbFrames));
58 nbFrames = 0;
59 lastTime += 1.0;
60 }
61
76 // Should we load a new world? 62 // Should we load a new world?
77 if (nextWorld != 0) 63 if (newWorld)
78 { 64 {
79 delete world; 65 newWorld = false;
80 world = nextWorld; 66 entities.clear();
81 world->player->world = world; 67 entities = std::move(nextEntities);
82 nextWorld = 0;
83 } 68 }
84 69
85 // Handle input 70 // Handle input
86 glfwPollEvents(); 71 glfwPollEvents();
87 72
88 // Tick! 73 // Tick!
89 world->tick(); 74 for (auto entity : entities)
75 {
76 entity->tick(*this);
77 }
90 78
91 // Do rendering 79 // Do rendering
92 world->render(buffer); 80 buffer.fill(buffer.entirety(), 0, 0, 0);
93 renderScreen(buffer); 81 for (auto entity : entities)
94 } 82 {
95 83 entity->render(*this, buffer);
96 destroyTexture(buffer); 84 }
97}
98 85
99void Game::input(int key, int action) 86 buffer.renderScreen();
100{
101 if (world != NULL)
102 {
103 world->input(key, action);
104 } 87 }
105} 88}
106 89
107void Game::loadMap(Map* map) 90void Game::loadMap(Map& map)
108{ 91{
109 nextWorld = new World(); 92 auto mapEn = std::make_shared<Entity>();
110
111 nextWorld->player = world->player;
112
113 auto mapEn = std::make_shared<Entity>(nextWorld);
114 93
115 auto map_render = std::make_shared<MapRenderComponent>(*mapEn, map); 94 auto map_render = std::make_shared<MapRenderComponent>(map);
116 mapEn->addComponent(map_render); 95 mapEn->addComponent(map_render);
117 96
118 auto map_collision = std::make_shared<MapCollisionComponent>(*mapEn, map); 97 auto map_collision = std::make_shared<MapCollisionComponent>(map);
119 mapEn->addComponent(map_collision); 98 mapEn->addComponent(map_collision);
120 nextWorld->bodies.push_back(map_collision.get());
121 99
122 nextWorld->addEntity(mapEn); 100 nextEntities.clear();
123 nextWorld->addEntity(nextWorld->player); 101 nextEntities.push_back(mapEn);
102 nextEntities.push_back(player);
103
104 newWorld = true;
105}
106
107void Game::detectCollision(Entity& collider, std::pair<double, double> old_position)
108{
109 for (auto entity : entities)
110 {
111 entity->detectCollision(*this, collider, old_position);
112 }
124} 113}