diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..a2743d1 --- /dev/null +++ b/src/main.cpp | |||
@@ -0,0 +1,149 @@ | |||
1 | #include <ctime> | ||
2 | #include <list> | ||
3 | #include "map.h" | ||
4 | #include "renderer.h" | ||
5 | |||
6 | using namespace::std; | ||
7 | |||
8 | #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__WINDOWS__) || defined(__TOS_WIN__) | ||
9 | |||
10 | #include <windows.h> | ||
11 | |||
12 | inline void delay( unsigned long ms ) | ||
13 | { | ||
14 | Sleep( ms ); | ||
15 | } | ||
16 | |||
17 | #else /* presume POSIX */ | ||
18 | |||
19 | #include <unistd.h> | ||
20 | |||
21 | inline void delay( unsigned long ms ) | ||
22 | { | ||
23 | usleep( ms * 1000 ); | ||
24 | } | ||
25 | |||
26 | #endif | ||
27 | |||
28 | const int FRAMES_PER_SECOND = 60; | ||
29 | bool holding_left = false; | ||
30 | bool holding_right = false; | ||
31 | bool quit = false; | ||
32 | mob_t* player; | ||
33 | |||
34 | // Initialize jump physics | ||
35 | double jump_height = TILE_HEIGHT*3; | ||
36 | double jump_length = 0.25 * FRAMES_PER_SECOND; | ||
37 | double jump_velocity = -2 * jump_height / jump_length; | ||
38 | double jump_gravity = -1 * jump_velocity / jump_length; | ||
39 | |||
40 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) | ||
41 | { | ||
42 | if (action == GLFW_PRESS) | ||
43 | { | ||
44 | switch (key) | ||
45 | { | ||
46 | case GLFW_KEY_LEFT: holding_left = true; break; | ||
47 | case GLFW_KEY_RIGHT: holding_right = true; break; | ||
48 | case GLFW_KEY_UP: player->y_vel = jump_velocity; break; | ||
49 | case GLFW_KEY_ESCAPE: quit = true; break; | ||
50 | } | ||
51 | } else if (action == GLFW_RELEASE) | ||
52 | { | ||
53 | switch (key) | ||
54 | { | ||
55 | case GLFW_KEY_LEFT: holding_left = false; break; | ||
56 | case GLFW_KEY_RIGHT: holding_right = false; break; | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | |||
61 | int main() | ||
62 | { | ||
63 | GLFWwindow* window = initRenderer(); | ||
64 | glfwSwapInterval(1); | ||
65 | glfwSetKeyCallback(window, key_callback); | ||
66 | |||
67 | Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT); | ||
68 | |||
69 | // Initialize player data | ||
70 | player = new mob_t(); | ||
71 | player->x = 100; | ||
72 | player->y = 100; | ||
73 | player->x_vel = 0; | ||
74 | player->y_vel = 0; | ||
75 | player->x_accel = 0; | ||
76 | player->y_accel = jump_gravity; | ||
77 | player->w = 10; | ||
78 | player->h = 14; | ||
79 | |||
80 | Map* map = new Map(); | ||
81 | |||
82 | Texture* tiles = loadTextureFromBMP("../res/tiles.bmp"); | ||
83 | |||
84 | double lastTime = glfwGetTime(); | ||
85 | int nbFrames = 0; | ||
86 | |||
87 | while (!quit) | ||
88 | { | ||
89 | double currentTime = glfwGetTime(); | ||
90 | nbFrames++; | ||
91 | if ( currentTime - lastTime >= 1.0 ){ // If last prinf() was more than 1 sec ago | ||
92 | // printf and reset timer | ||
93 | printf("%f ms/frame\n", 1000.0/double(nbFrames)); | ||
94 | nbFrames = 0; | ||
95 | lastTime += 1.0; | ||
96 | } | ||
97 | |||
98 | if (holding_left && player->x_vel >= 0) | ||
99 | { | ||
100 | player->x_vel = -2; | ||
101 | } else if (holding_right && player->x_vel <= 0) | ||
102 | { | ||
103 | player->x_vel = 2; | ||
104 | } else if (!holding_left && !holding_right) { | ||
105 | player->x_vel = 0; | ||
106 | } | ||
107 | |||
108 | player->x_vel += player->x_accel; | ||
109 | if (player->x_vel < -16) player->x_vel = -16; | ||
110 | if (player->x_vel > 16) player->x_vel = 16; | ||
111 | int playerx_next = player->x + player->x_vel; | ||
112 | |||
113 | player->y_vel += player->y_accel; | ||
114 | if (player->y_vel > 16) player->y_vel = 16; // Terminal velocity | ||
115 | if (player->y_vel < -16) player->y_vel = -16; | ||
116 | int playery_next = player->y + player->y_vel; | ||
117 | |||
118 | map->check_collisions(player, playerx_next, playery_next); | ||
119 | |||
120 | // Do rendering | ||
121 | map->render(buffer); | ||
122 | |||
123 | //Rectangle src_rect(96, 0, 8, 8); | ||
124 | Rectangle dst_rect(player->x, player->y, player->w, player->h); | ||
125 | |||
126 | //blitTexture(tiles, buffer, &src_rect, &dst_rect); | ||
127 | fillTexture(buffer, &dst_rect, 85, 85, 255); | ||
128 | //fillTexture(buffer, NULL, 85, 85, 0); | ||
129 | |||
130 | renderScreen(buffer); | ||
131 | |||
132 | //fuckThePolice(buffer); | ||
133 | |||
134 | glfwPollEvents(); | ||
135 | |||
136 | // Regulate frame rate | ||
137 | /*if ((clock() - frame_start) < CLOCKS_PER_SEC / FRAMES_PER_SECOND) | ||
138 | { | ||
139 | //delay(((CLOCKS_PER_SEC / FRAMES_PER_SECOND) - clock() + frame_start) * CLOCKS_PER_SEC / 1000); | ||
140 | }*/ | ||
141 | } | ||
142 | |||
143 | delete map; | ||
144 | delete player; | ||
145 | |||
146 | destroyRenderer(); | ||
147 | |||
148 | return 0; | ||
149 | } | ||