summary refs log tree commit diff stats
path: root/src/main.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2015-02-17 13:28:50 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2015-02-17 13:28:50 -0500
commit6b99c7aee539e35b8e67520f36adeca9007641cb (patch)
tree7e265900e63512889109048047ee6a1b103c4339 /src/main.cpp
parent783b308990e7c4ef0837a102a138778f73e4d2b7 (diff)
downloadtherapy-6b99c7aee539e35b8e67520f36adeca9007641cb.tar.gz
therapy-6b99c7aee539e35b8e67520f36adeca9007641cb.tar.bz2
therapy-6b99c7aee539e35b8e67520f36adeca9007641cb.zip
Refactored map loader and added a second map
Also tweaked the font for apostrophe, p, and q
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp91
1 files changed, 22 insertions, 69 deletions
diff --git a/src/main.cpp b/src/main.cpp index e1e1aa2..2a1d9ea 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -1,40 +1,26 @@
1#include <ctime> 1#include <ctime>
2#include <list> 2#include <list>
3#include "map.h" 3#include "map.h"
4#include "state.h"
5#include "mapview.h"
4#include "renderer.h" 6#include "renderer.h"
5 7
6using namespace::std; 8using namespace::std;
7 9
8const int FRAMES_PER_SECOND = 60;
9bool holding_left = false;
10bool holding_right = false;
11bool quit = false; 10bool quit = false;
12mob_t* player;
13 11
14// Initialize jump physics 12State* curGameState;
15double jump_height = TILE_HEIGHT*3;
16double jump_length = 0.25 * FRAMES_PER_SECOND;
17double jump_velocity = -2 * jump_height / jump_length;
18double jump_gravity = -1 * jump_velocity / jump_length;
19 13
20void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 14void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
21{ 15{
22 if (action == GLFW_PRESS) 16 if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS))
23 { 17 {
24 switch (key) 18 quit = true;
25 { 19 }
26 case GLFW_KEY_LEFT: holding_left = true; break; 20
27 case GLFW_KEY_RIGHT: holding_right = true; break; 21 if (curGameState != NULL)
28 case GLFW_KEY_UP: player->y_vel = jump_velocity; break;
29 case GLFW_KEY_ESCAPE: quit = true; break;
30 }
31 } else if (action == GLFW_RELEASE)
32 { 22 {
33 switch (key) 23 curGameState->input(key, action);
34 {
35 case GLFW_KEY_LEFT: holding_left = false; break;
36 case GLFW_KEY_RIGHT: holding_right = false; break;
37 }
38 } 24 }
39} 25}
40 26
@@ -44,63 +30,30 @@ int main()
44 glfwSwapInterval(1); 30 glfwSwapInterval(1);
45 glfwSetKeyCallback(window, key_callback); 31 glfwSetKeyCallback(window, key_callback);
46 32
47 Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT); 33 Map* m = new Map("../maps/bigmap.txt");
34 Map* m2 = new Map("../maps/cozy.txt");
48 35
49 // Initialize player data 36 m->setLeftMap(m2);
50 player = new mob_t(); 37 m2->setRightMap(m);
51 player->x = 100;
52 player->y = 100;
53 player->x_vel = 0;
54 player->y_vel = 0;
55 player->x_accel = 0;
56 player->y_accel = jump_gravity;
57 player->w = 10;
58 player->h = 14;
59 38
60 Map* map = new Map(); 39 curGameState = new MapView(m, 100, 100);
61 40
62 Texture* tiles = loadTextureFromBMP("../res/tiles.bmp"); 41 Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT);
63 42
64 while (!quit) 43 while (!(quit || glfwWindowShouldClose(window)))
65 { 44 {
66 if (holding_left && player->x_vel >= 0) 45 // Tick!
67 { 46 curGameState->tick();
68 player->x_vel = -2;
69 } else if (holding_right && player->x_vel <= 0)
70 {
71 player->x_vel = 2;
72 } else if (!holding_left && !holding_right) {
73 player->x_vel = 0;
74 }
75
76 player->x_vel += player->x_accel;
77 if (player->x_vel < -16) player->x_vel = -16;
78 if (player->x_vel > 16) player->x_vel = 16;
79 int playerx_next = player->x + player->x_vel;
80
81 player->y_vel += player->y_accel;
82 if (player->y_vel > 16) player->y_vel = 16; // Terminal velocity
83 if (player->y_vel < -16) player->y_vel = -16;
84 int playery_next = player->y + player->y_vel;
85
86 map->check_collisions(player, playerx_next, playery_next);
87 47
88 // Do rendering 48 // Do rendering
89 map->render(buffer); 49 curGameState->render(buffer);
90
91 //Rectangle src_rect(96, 0, 8, 8);
92 Rectangle dst_rect(player->x, player->y, player->w, player->h);
93
94 //blitTexture(tiles, buffer, &src_rect, &dst_rect);
95 fillTexture(buffer, &dst_rect, 255, 255, 255);
96
97 renderScreen(buffer); 50 renderScreen(buffer);
98 51
99 glfwPollEvents(); 52 glfwPollEvents();
100 } 53 }
101 54
102 delete map; 55 delete curGameState;
103 delete player; 56 delete m;
104 57
105 destroyRenderer(); 58 destroyRenderer();
106 59