diff options
-rw-r--r-- | maps/bigmap.txt | 3 | ||||
-rw-r--r-- | maps/cozy.txt | 25 | ||||
-rw-r--r-- | maps/testmap.txt | 16 | ||||
-rw-r--r-- | src/main.cpp | 91 | ||||
-rw-r--r-- | src/map.cpp | 257 | ||||
-rw-r--r-- | src/map.h | 57 | ||||
-rw-r--r-- | src/mapview.cpp | 319 | ||||
-rw-r--r-- | src/mapview.h | 58 | ||||
-rw-r--r-- | src/renderer.cpp | 46 | ||||
-rw-r--r-- | src/state.h | 13 |
10 files changed, 515 insertions, 370 deletions
diff --git a/maps/bigmap.txt b/maps/bigmap.txt index 00ab68e..ebc021d 100644 --- a/maps/bigmap.txt +++ b/maps/bigmap.txt | |||
@@ -21,4 +21,5 @@ XXX XXXXXXXXXXXX | |||
21 | XXXXXXXXXXXXXXXXXXXX | 21 | XXXXXXXXXXXXXXXXXXXX |
22 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | 22 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
23 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | 23 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
24 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \ No newline at end of file | 24 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
25 | Everything Is Embarrassing \ No newline at end of file | ||
diff --git a/maps/cozy.txt b/maps/cozy.txt new file mode 100644 index 0000000..ab4c6f8 --- /dev/null +++ b/maps/cozy.txt | |||
@@ -0,0 +1,25 @@ | |||
1 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | ||
2 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | ||
3 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | ||
4 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | ||
5 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | ||
6 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | ||
7 | XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXX | ||
8 | XXXXXXXXXXXXXXXXXXX XXXXXXX | ||
9 | XXXXXXXXXXXXXXX XX | ||
10 | XXXXXXXXXXX | ||
11 | XXXXXXXXXX | ||
12 | XXXXXXXX | ||
13 | XXXXXXXX | ||
14 | XXXXXX | ||
15 | XXXX | ||
16 | XXXX | ||
17 | XXX | ||
18 | XXX | ||
19 | XXX | ||
20 | XXXX | ||
21 | XXXXXXXXX | ||
22 | XXXXXXXXXXX | ||
23 | XXXXXXXXXXXXXXXXX XXXXXXXXXXXX | ||
24 | XXXXXXXXXXXXXXXXX XXXXXXXXXXXX | ||
25 | It's A Trap! \ No newline at end of file | ||
diff --git a/maps/testmap.txt b/maps/testmap.txt deleted file mode 100644 index 7fc55ab..0000000 --- a/maps/testmap.txt +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
1 | XXXXXXXXXXXXXXXX | ||
2 | XXXXXX XXXXXX | ||
3 | XX X | ||
4 | X X | ||
5 | X XXXX | ||
6 | PPPP | ||
7 | |||
8 | PPPP | ||
9 | |||
10 | PPPP | ||
11 | |||
12 | |||
13 | XXXXXXXX X | ||
14 | XXXP XX | ||
15 | XXXX XXX | ||
16 | XXXXXXXXXXXXXXXX | ||
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 | ||
6 | using namespace::std; | 8 | using namespace::std; |
7 | 9 | ||
8 | const int FRAMES_PER_SECOND = 60; | ||
9 | bool holding_left = false; | ||
10 | bool holding_right = false; | ||
11 | bool quit = false; | 10 | bool quit = false; |
12 | mob_t* player; | ||
13 | 11 | ||
14 | // Initialize jump physics | 12 | State* curGameState; |
15 | double jump_height = TILE_HEIGHT*3; | ||
16 | double jump_length = 0.25 * FRAMES_PER_SECOND; | ||
17 | double jump_velocity = -2 * jump_height / jump_length; | ||
18 | double jump_gravity = -1 * jump_velocity / jump_length; | ||
19 | 13 | ||
20 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) | 14 | void 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 | ||
diff --git a/src/map.cpp b/src/map.cpp index 0dd3e04..cb1ce72 100644 --- a/src/map.cpp +++ b/src/map.cpp | |||
@@ -1,252 +1,55 @@ | |||
1 | #include "map.h" | 1 | #include "map.h" |
2 | #include "mapview.h" | ||
2 | 3 | ||
3 | struct platform_t { | 4 | Map::Map(char* filename) |
4 | int x; | ||
5 | int y; | ||
6 | int w; | ||
7 | int h; | ||
8 | bool enter_from_left; | ||
9 | bool enter_from_right; | ||
10 | bool enter_from_top; | ||
11 | bool enter_from_bottom; | ||
12 | int r; | ||
13 | int g; | ||
14 | int b; | ||
15 | int a; | ||
16 | }; | ||
17 | |||
18 | Map::Map() | ||
19 | { | 5 | { |
20 | add_collision(-6, 0, GAME_WIDTH, left, 1); | 6 | FILE* f = fopen(filename, "r"); |
21 | add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, 1); | 7 | m_mapdata = (char*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(char)); |
22 | |||
23 | FILE* f = fopen("../maps/bigmap.txt", "r"); | ||
24 | char* mapbuf = (char*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(char)); | ||
25 | 8 | ||
26 | for (int i=0; i<MAP_HEIGHT-1; i++) | 9 | for (int i=0; i<MAP_HEIGHT-1; i++) |
27 | { | 10 | { |
28 | fread(mapbuf + i*MAP_WIDTH, sizeof(char), MAP_WIDTH, f); | 11 | fread(m_mapdata + i*MAP_WIDTH, sizeof(char), MAP_WIDTH, f); |
29 | fgetc(f); | 12 | fgetc(f); |
30 | } | 13 | } |
31 | 14 | ||
32 | fclose(f); | 15 | m_title = (char*) calloc(41, sizeof(char)); |
33 | 16 | fgets(m_title, 41, f); | |
34 | //Texture* tiles = loadTextureFromBMP("../res/tiles.bmp"); | ||
35 | bg = createTexture(GAME_WIDTH, GAME_HEIGHT); | ||
36 | fillTexture(bg, NULL, 0, 0, 0); | ||
37 | |||
38 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) | ||
39 | { | ||
40 | int x = i % MAP_WIDTH; | ||
41 | int y = i / MAP_WIDTH; | ||
42 | Rectangle dst(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | ||
43 | //Rectangle src; | ||
44 | |||
45 | switch (mapbuf[i]) | ||
46 | { | ||
47 | case ' ': break; | ||
48 | case 'X': fillTexture(bg, &dst, 255, 85, 85); break; | ||
49 | case 'P': fillTexture(bg, &dst, 85, 255, 255); break; | ||
50 | } | ||
51 | |||
52 | //blitTexture(tiles, bg, &src, &dst); | ||
53 | |||
54 | if (mapbuf[i] == 'X') | ||
55 | { | ||
56 | if ((x != 0) && (mapbuf[i-1] != 'X')) | ||
57 | { | ||
58 | add_collision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, right, 0); | ||
59 | } | ||
60 | |||
61 | if ((x != 15) && (mapbuf[i+1] != 'X')) | ||
62 | { | ||
63 | add_collision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, left, 0); | ||
64 | } | ||
65 | |||
66 | if ((y != 0) && (mapbuf[i-MAP_WIDTH] != 'X')) | ||
67 | { | ||
68 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); | ||
69 | } | ||
70 | |||
71 | if ((y != 15) && (mapbuf[i+MAP_WIDTH] != 'X')) | ||
72 | { | ||
73 | add_collision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, up, 0); | ||
74 | } | ||
75 | } else if (mapbuf[i] == 'P') | ||
76 | { | ||
77 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | Texture* font = loadTextureFromBMP("../res/font.bmp"); | ||
82 | const char* map_name = "Everything Is Embarassing"; | ||
83 | int start_x = (40/2) - (strlen(map_name)/2); | ||
84 | for (int i=0; i<strlen(map_name); i++) | ||
85 | { | ||
86 | Rectangle srcRect(map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8); | ||
87 | Rectangle dstRect((start_x + i)*8, 24*8, 8, 8); | ||
88 | blitTexture(font, bg, &srcRect, &dstRect); | ||
89 | } | ||
90 | 17 | ||
91 | destroyTexture(font); | 18 | fclose(f); |
92 | //destroyTexture(tiles); | ||
93 | } | 19 | } |
94 | 20 | ||
95 | Map::~Map() | 21 | Map::~Map() |
96 | { | 22 | { |
97 | destroyTexture(bg); | 23 | free(m_mapdata); |
24 | free(m_title); | ||
98 | } | 25 | } |
99 | 26 | ||
100 | void Map::add_collision(int axis, int lower, int upper, direction_t dir, int type) | 27 | const char* Map::mapdata() |
101 | { | 28 | { |
102 | //printf("added collision\n"); | 29 | return m_mapdata; |
103 | list<collision_t>::iterator it; | ||
104 | |||
105 | switch (dir) | ||
106 | { | ||
107 | case up: | ||
108 | it = up_collisions.begin(); | ||
109 | for (; it!=up_collisions.end(); it++) | ||
110 | { | ||
111 | if (it->axis < axis) break; | ||
112 | } | ||
113 | |||
114 | up_collisions.insert(it, {axis, lower, upper, type}); | ||
115 | |||
116 | break; | ||
117 | case down: | ||
118 | it = down_collisions.begin(); | ||
119 | for (; it!=down_collisions.end(); it++) | ||
120 | { | ||
121 | if (it->axis > axis) break; | ||
122 | } | ||
123 | |||
124 | down_collisions.insert(it, {axis, lower, upper, type}); | ||
125 | |||
126 | break; | ||
127 | case left: | ||
128 | it = left_collisions.begin(); | ||
129 | for (; it!=left_collisions.end(); it++) | ||
130 | { | ||
131 | if (it->axis < axis) break; | ||
132 | } | ||
133 | |||
134 | left_collisions.insert(it, {axis, lower, upper, type}); | ||
135 | |||
136 | break; | ||
137 | case right: | ||
138 | it = right_collisions.begin(); | ||
139 | for (; it!=right_collisions.end(); it++) | ||
140 | { | ||
141 | if (it->axis > axis) break; | ||
142 | } | ||
143 | |||
144 | right_collisions.insert(it, {axis, lower, upper, type}); | ||
145 | |||
146 | break; | ||
147 | } | ||
148 | } | 30 | } |
149 | 31 | ||
150 | void Map::check_collisions(mob_t* mob, int x_next, int y_next) | 32 | const char* Map::title() |
151 | { | 33 | { |
152 | if (x_next < mob->x) | 34 | return m_title; |
153 | { | 35 | } |
154 | for (list<collision_t>::iterator it=left_collisions.begin(); it!=left_collisions.end(); it++) | ||
155 | { | ||
156 | if (it->axis > mob->x) continue; | ||
157 | if (it->axis < x_next) break; | ||
158 | |||
159 | if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) | ||
160 | { | ||
161 | // We have a collision! | ||
162 | if (it->type == 0) | ||
163 | { | ||
164 | x_next = it->axis; | ||
165 | mob->x_vel = 0; | ||
166 | } else if (it->type == 1) | ||
167 | { | ||
168 | x_next = GAME_WIDTH-mob->w/2; | ||
169 | } | ||
170 | |||
171 | break; | ||
172 | } | ||
173 | } | ||
174 | } else if (x_next > mob->x) | ||
175 | { | ||
176 | for (list<collision_t>::iterator it=right_collisions.begin(); it!=right_collisions.end(); it++) | ||
177 | { | ||
178 | if (it->axis < mob->x+mob->w) continue; | ||
179 | if (it->axis > x_next+mob->w) break; | ||
180 | |||
181 | if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) | ||
182 | { | ||
183 | // We have a collision! | ||
184 | if (it->type == 0) | ||
185 | { | ||
186 | x_next = it->axis - mob->w; | ||
187 | mob->x_vel = 0; | ||
188 | } else if (it->type == 1) | ||
189 | { | ||
190 | x_next = -mob->w/2; | ||
191 | } | ||
192 | |||
193 | break; | ||
194 | } | ||
195 | } | ||
196 | } | ||
197 | |||
198 | mob->x = x_next; | ||
199 | |||
200 | if (y_next < mob->y) | ||
201 | { | ||
202 | for (list<collision_t>::iterator it=up_collisions.begin(); it!=up_collisions.end(); it++) | ||
203 | { | ||
204 | if (it->axis > mob->y) continue; | ||
205 | if (it->axis < y_next) break; | ||
206 | |||
207 | if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) | ||
208 | { | ||
209 | // We have a collision! | ||
210 | if (it->type == 0) | ||
211 | { | ||
212 | y_next = it->axis; | ||
213 | mob->y_vel = 0; | ||
214 | } else if (it->type == 1) | ||
215 | { | ||
216 | y_next = GAME_HEIGHT-mob->h/2-1; | ||
217 | } | ||
218 | |||
219 | break; | ||
220 | } | ||
221 | } | ||
222 | } else if (y_next > mob->y) | ||
223 | { | ||
224 | for (list<collision_t>::iterator it=down_collisions.begin(); it!=down_collisions.end(); it++) | ||
225 | { | ||
226 | if (it->axis < mob->y+mob->h) continue; | ||
227 | if (it->axis > y_next+mob->h) break; | ||
228 | 36 | ||
229 | if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) | 37 | Map* Map::getLeftMap() |
230 | { | 38 | { |
231 | // We have a collision! | 39 | return m_leftMap; |
232 | if (it->type == 0) | 40 | } |
233 | { | 41 | |
234 | y_next = it->axis - mob->h; | 42 | Map* Map::getRightMap() |
235 | mob->y_vel = 0; | 43 | { |
236 | } else if (it->type == 1) | 44 | return m_rightMap; |
237 | { | 45 | } |
238 | y_next = 1 - mob->h/2; | 46 | |
239 | } | 47 | void Map::setLeftMap(Map* m) |
240 | 48 | { | |
241 | break; | 49 | m_leftMap = m; |
242 | } | ||
243 | } | ||
244 | } | ||
245 | |||
246 | mob->y = y_next; | ||
247 | } | 50 | } |
248 | 51 | ||
249 | void Map::render(Texture* buffer) | 52 | void Map::setRightMap(Map* m) |
250 | { | 53 | { |
251 | blitTexture(bg, buffer, NULL, NULL); | 54 | m_rightMap = m; |
252 | } | 55 | } |
diff --git a/src/map.h b/src/map.h index 7986e0d..3c09238 100644 --- a/src/map.h +++ b/src/map.h | |||
@@ -1,42 +1,21 @@ | |||
1 | #include <list> | 1 | #ifndef MAP_H |
2 | #include <vector> | 2 | #define MAP_H |
3 | #include "mob.h" | ||
4 | #include "renderer.h" | ||
5 | 3 | ||
6 | using namespace::std; | 4 | class Map { |
7 | 5 | public: | |
8 | const int TILE_WIDTH = 8; | 6 | Map(char* filename); |
9 | const int TILE_HEIGHT = 8; | 7 | ~Map(); |
10 | const int GAME_WIDTH = 320; | 8 | const char* mapdata(); |
11 | const int GAME_HEIGHT = 200; | 9 | const char* title(); |
12 | const int MAP_WIDTH = GAME_WIDTH/TILE_WIDTH; | 10 | Map* getLeftMap(); |
13 | const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT; | 11 | Map* getRightMap(); |
14 | 12 | void setLeftMap(Map* m); | |
15 | enum direction_t { | 13 | void setRightMap(Map* m); |
16 | up, left, down, right | 14 | private: |
15 | char* m_mapdata; | ||
16 | char* m_title; | ||
17 | Map* m_leftMap = 0; | ||
18 | Map* m_rightMap = 0; | ||
17 | }; | 19 | }; |
18 | 20 | ||
19 | typedef struct { | 21 | #endif |
20 | int axis; | ||
21 | int lower; | ||
22 | int upper; | ||
23 | int type; | ||
24 | } collision_t; | ||
25 | |||
26 | class Map { | ||
27 | public: | ||
28 | Map(); | ||
29 | ~Map(); | ||
30 | void render(Texture* buffer); | ||
31 | void check_collisions(mob_t* mob, int x_next, int y_next); | ||
32 | |||
33 | private: | ||
34 | void add_collision(int axis, int lower, int upper, direction_t dir, int type); | ||
35 | |||
36 | list<collision_t> left_collisions; | ||
37 | list<collision_t> right_collisions; | ||
38 | list<collision_t> up_collisions; | ||
39 | list<collision_t> down_collisions; | ||
40 | |||
41 | Texture* bg; | ||
42 | }; \ No newline at end of file | ||
diff --git a/src/mapview.cpp b/src/mapview.cpp new file mode 100644 index 0000000..f524d46 --- /dev/null +++ b/src/mapview.cpp | |||
@@ -0,0 +1,319 @@ | |||
1 | #include "mapview.h" | ||
2 | |||
3 | // Initialize jump physics | ||
4 | double jump_height = TILE_HEIGHT*3; | ||
5 | double jump_length = 0.25 * FRAMES_PER_SECOND; | ||
6 | double jump_velocity = -2 * jump_height / jump_length; | ||
7 | double jump_gravity = -1 * jump_velocity / jump_length; | ||
8 | |||
9 | MapView::MapView(Map* first, int x, int y) | ||
10 | { | ||
11 | // Initialize player data | ||
12 | player = new mob_t(); | ||
13 | player->x = x; | ||
14 | player->y = y; | ||
15 | player->x_vel = 0; | ||
16 | player->y_vel = 0; | ||
17 | player->x_accel = 0; | ||
18 | player->y_accel = jump_gravity; | ||
19 | player->w = 10; | ||
20 | player->h = 14; | ||
21 | |||
22 | loadMap(first); | ||
23 | } | ||
24 | |||
25 | MapView::~MapView() | ||
26 | { | ||
27 | destroyTexture(bg); | ||
28 | |||
29 | delete player; | ||
30 | } | ||
31 | |||
32 | void MapView::loadMap(Map* m) | ||
33 | { | ||
34 | curMap = m; | ||
35 | |||
36 | left_collisions.clear(); | ||
37 | right_collisions.clear(); | ||
38 | up_collisions.clear(); | ||
39 | down_collisions.clear(); | ||
40 | |||
41 | add_collision(-6, 0, GAME_WIDTH, left, (m->getLeftMap() == NULL) ? 1 : 2); | ||
42 | add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, (m->getRightMap() == NULL) ? 1 : 2); | ||
43 | |||
44 | if (bg == NULL) | ||
45 | { | ||
46 | bg = createTexture(GAME_WIDTH, GAME_HEIGHT); | ||
47 | } | ||
48 | |||
49 | fillTexture(bg, NULL, 0, 0, 0); | ||
50 | |||
51 | const char* mapbuf = m->mapdata(); | ||
52 | |||
53 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) | ||
54 | { | ||
55 | int x = i % MAP_WIDTH; | ||
56 | int y = i / MAP_WIDTH; | ||
57 | Rectangle dst(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | ||
58 | //Rectangle src; | ||
59 | |||
60 | switch (mapbuf[i]) | ||
61 | { | ||
62 | case ' ': break; | ||
63 | case 'X': fillTexture(bg, &dst, 255, 85, 85); break; | ||
64 | case 'P': fillTexture(bg, &dst, 85, 255, 255); break; | ||
65 | } | ||
66 | |||
67 | //blitTexture(tiles, bg, &src, &dst); | ||
68 | |||
69 | if (mapbuf[i] == 'X') | ||
70 | { | ||
71 | if ((x != 0) && (mapbuf[i-1] != 'X')) | ||
72 | { | ||
73 | add_collision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, right, 0); | ||
74 | } | ||
75 | |||
76 | if ((x != 39) && (mapbuf[i+1] != 'X')) | ||
77 | { | ||
78 | add_collision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, left, 0); | ||
79 | } | ||
80 | |||
81 | if ((y != 0) && (mapbuf[i-MAP_WIDTH] != 'X')) | ||
82 | { | ||
83 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); | ||
84 | } | ||
85 | |||
86 | if ((y != 23) && (mapbuf[i+MAP_WIDTH] != 'X')) | ||
87 | { | ||
88 | add_collision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, up, 0); | ||
89 | } | ||
90 | } else if (mapbuf[i] == 'P') | ||
91 | { | ||
92 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | Texture* font = loadTextureFromBMP("../res/font.bmp"); | ||
97 | const char* map_name = m->title(); | ||
98 | int start_x = (40/2) - (strlen(map_name)/2); | ||
99 | for (size_t i=0; i<strlen(map_name); i++) | ||
100 | { | ||
101 | Rectangle srcRect(map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8); | ||
102 | Rectangle dstRect((start_x + i)*8, 24*8, 8, 8); | ||
103 | blitTexture(font, bg, &srcRect, &dstRect); | ||
104 | } | ||
105 | |||
106 | destroyTexture(font); | ||
107 | } | ||
108 | |||
109 | void MapView::input(int key, int action) | ||
110 | { | ||
111 | if (action == GLFW_PRESS) | ||
112 | { | ||
113 | switch (key) | ||
114 | { | ||
115 | case GLFW_KEY_LEFT: holding_left = true; break; | ||
116 | case GLFW_KEY_RIGHT: holding_right = true; break; | ||
117 | case GLFW_KEY_UP: player->y_vel = jump_velocity; break; | ||
118 | } | ||
119 | } else if (action == GLFW_RELEASE) | ||
120 | { | ||
121 | switch (key) | ||
122 | { | ||
123 | case GLFW_KEY_LEFT: holding_left = false; break; | ||
124 | case GLFW_KEY_RIGHT: holding_right = false; break; | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | void MapView::tick() | ||
130 | { | ||
131 | if (holding_left && player->x_vel >= 0) | ||
132 | { | ||
133 | player->x_vel = -2; | ||
134 | } else if (holding_right && player->x_vel <= 0) | ||
135 | { | ||
136 | player->x_vel = 2; | ||
137 | } else if (!holding_left && !holding_right) { | ||
138 | player->x_vel = 0; | ||
139 | } | ||
140 | |||
141 | player->x_vel += player->x_accel; | ||
142 | if (player->x_vel < -16) player->x_vel = -16; | ||
143 | if (player->x_vel > 16) player->x_vel = 16; | ||
144 | int playerx_next = player->x + player->x_vel; | ||
145 | |||
146 | player->y_vel += player->y_accel; | ||
147 | if (player->y_vel > 16) player->y_vel = 16; // Terminal velocity | ||
148 | if (player->y_vel < -16) player->y_vel = -16; | ||
149 | int playery_next = player->y + player->y_vel; | ||
150 | |||
151 | check_collisions(player, playerx_next, playery_next); | ||
152 | } | ||
153 | |||
154 | void MapView::render(Texture* tex) | ||
155 | { | ||
156 | // Draw the background | ||
157 | blitTexture(bg, tex, NULL, NULL); | ||
158 | |||
159 | // Draw the player | ||
160 | Rectangle dst_rect(player->x, player->y, player->w, player->h); | ||
161 | fillTexture(tex, &dst_rect, 255, 255, 255); | ||
162 | } | ||
163 | |||
164 | void MapView::add_collision(int axis, int lower, int upper, direction_t dir, int type) | ||
165 | { | ||
166 | //printf("added collision\n"); | ||
167 | list<collision_t>::iterator it; | ||
168 | |||
169 | switch (dir) | ||
170 | { | ||
171 | case up: | ||
172 | it = up_collisions.begin(); | ||
173 | for (; it!=up_collisions.end(); it++) | ||
174 | { | ||
175 | if (it->axis < axis) break; | ||
176 | } | ||
177 | |||
178 | up_collisions.insert(it, {axis, lower, upper, type}); | ||
179 | |||
180 | break; | ||
181 | case down: | ||
182 | it = down_collisions.begin(); | ||
183 | for (; it!=down_collisions.end(); it++) | ||
184 | { | ||
185 | if (it->axis > axis) break; | ||
186 | } | ||
187 | |||
188 | down_collisions.insert(it, {axis, lower, upper, type}); | ||
189 | |||
190 | break; | ||
191 | case left: | ||
192 | it = left_collisions.begin(); | ||
193 | for (; it!=left_collisions.end(); it++) | ||
194 | { | ||
195 | if (it->axis < axis) break; | ||
196 | } | ||
197 | |||
198 | left_collisions.insert(it, {axis, lower, upper, type}); | ||
199 | |||
200 | break; | ||
201 | case right: | ||
202 | it = right_collisions.begin(); | ||
203 | for (; it!=right_collisions.end(); it++) | ||
204 | { | ||
205 | if (it->axis > axis) break; | ||
206 | } | ||
207 | |||
208 | right_collisions.insert(it, {axis, lower, upper, type}); | ||
209 | |||
210 | break; | ||
211 | } | ||
212 | } | ||
213 | |||
214 | void MapView::check_collisions(mob_t* mob, int x_next, int y_next) | ||
215 | { | ||
216 | if (x_next < mob->x) | ||
217 | { | ||
218 | for (list<collision_t>::iterator it=left_collisions.begin(); it!=left_collisions.end(); it++) | ||
219 | { | ||
220 | if (it->axis > mob->x) continue; | ||
221 | if (it->axis < x_next) break; | ||
222 | |||
223 | if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) | ||
224 | { | ||
225 | // We have a collision! | ||
226 | if (it->type == 0) | ||
227 | { | ||
228 | x_next = it->axis; | ||
229 | mob->x_vel = 0; | ||
230 | } else if (it->type == 1) | ||
231 | { | ||
232 | x_next = GAME_WIDTH-mob->w/2; | ||
233 | } else if (it->type == 2) | ||
234 | { | ||
235 | x_next = GAME_WIDTH-mob->w/2; | ||
236 | loadMap(curMap->getLeftMap()); | ||
237 | } | ||
238 | |||
239 | break; | ||
240 | } | ||
241 | } | ||
242 | } else if (x_next > mob->x) | ||
243 | { | ||
244 | for (list<collision_t>::iterator it=right_collisions.begin(); it!=right_collisions.end(); it++) | ||
245 | { | ||
246 | if (it->axis < mob->x+mob->w) continue; | ||
247 | if (it->axis > x_next+mob->w) break; | ||
248 | |||
249 | if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) | ||
250 | { | ||
251 | // We have a collision! | ||
252 | if (it->type == 0) | ||
253 | { | ||
254 | x_next = it->axis - mob->w; | ||
255 | mob->x_vel = 0; | ||
256 | } else if (it->type == 1) | ||
257 | { | ||
258 | x_next = -mob->w/2; | ||
259 | } else if (it->type == 2) | ||
260 | { | ||
261 | x_next = -mob->w/2; | ||
262 | loadMap(curMap->getRightMap()); | ||
263 | } | ||
264 | |||
265 | break; | ||
266 | } | ||
267 | } | ||
268 | } | ||
269 | |||
270 | mob->x = x_next; | ||
271 | |||
272 | if (y_next < mob->y) | ||
273 | { | ||
274 | for (list<collision_t>::iterator it=up_collisions.begin(); it!=up_collisions.end(); it++) | ||
275 | { | ||
276 | if (it->axis > mob->y) continue; | ||
277 | if (it->axis < y_next) break; | ||
278 | |||
279 | if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) | ||
280 | { | ||
281 | // We have a collision! | ||
282 | if (it->type == 0) | ||
283 | { | ||
284 | y_next = it->axis; | ||
285 | mob->y_vel = 0; | ||
286 | } else if (it->type == 1) | ||
287 | { | ||
288 | y_next = GAME_HEIGHT-mob->h/2-1; | ||
289 | } | ||
290 | |||
291 | break; | ||
292 | } | ||
293 | } | ||
294 | } else if (y_next > mob->y) | ||
295 | { | ||
296 | for (list<collision_t>::iterator it=down_collisions.begin(); it!=down_collisions.end(); it++) | ||
297 | { | ||
298 | if (it->axis < mob->y+mob->h) continue; | ||
299 | if (it->axis > y_next+mob->h) break; | ||
300 | |||
301 | if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) | ||
302 | { | ||
303 | // We have a collision! | ||
304 | if (it->type == 0) | ||
305 | { | ||
306 | y_next = it->axis - mob->h; | ||
307 | mob->y_vel = 0; | ||
308 | } else if (it->type == 1) | ||
309 | { | ||
310 | y_next = 1 - mob->h/2; | ||
311 | } | ||
312 | |||
313 | break; | ||
314 | } | ||
315 | } | ||
316 | } | ||
317 | |||
318 | mob->y = y_next; | ||
319 | } | ||
diff --git a/src/mapview.h b/src/mapview.h new file mode 100644 index 0000000..70ffb3b --- /dev/null +++ b/src/mapview.h | |||
@@ -0,0 +1,58 @@ | |||
1 | #ifndef MAPVIEW_H | ||
2 | #define MAPVIEW_H | ||
3 | |||
4 | #include <list> | ||
5 | #include "state.h" | ||
6 | #include "mob.h" | ||
7 | #include "map.h" | ||
8 | |||
9 | using namespace::std; | ||
10 | |||
11 | const int TILE_WIDTH = 8; | ||
12 | const int TILE_HEIGHT = 8; | ||
13 | const int GAME_WIDTH = 320; | ||
14 | const int GAME_HEIGHT = 200; | ||
15 | const int MAP_WIDTH = GAME_WIDTH/TILE_WIDTH; | ||
16 | const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT; | ||
17 | |||
18 | const int FRAMES_PER_SECOND = 60; | ||
19 | |||
20 | enum direction_t { | ||
21 | up, left, down, right | ||
22 | }; | ||
23 | |||
24 | typedef struct { | ||
25 | int axis; | ||
26 | int lower; | ||
27 | int upper; | ||
28 | int type; | ||
29 | } collision_t; | ||
30 | |||
31 | class MapView : public State { | ||
32 | public: | ||
33 | MapView(Map* start, int x, int y); | ||
34 | ~MapView(); | ||
35 | void loadMap(Map* m); | ||
36 | void input(int key, int action); | ||
37 | void tick(); | ||
38 | void render(Texture* tex); | ||
39 | |||
40 | private: | ||
41 | void add_collision(int axis, int lower, int upper, direction_t dir, int type); | ||
42 | void check_collisions(mob_t* mob, int x_next, int y_next); | ||
43 | |||
44 | list<collision_t> left_collisions; | ||
45 | list<collision_t> right_collisions; | ||
46 | list<collision_t> up_collisions; | ||
47 | list<collision_t> down_collisions; | ||
48 | |||
49 | Texture* bg = NULL; | ||
50 | |||
51 | bool holding_left = false; | ||
52 | bool holding_right = false; | ||
53 | mob_t* player; | ||
54 | |||
55 | Map* curMap; | ||
56 | }; | ||
57 | |||
58 | #endif | ||
diff --git a/src/renderer.cpp b/src/renderer.cpp index 3011e8f..2ee0642 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -3,7 +3,7 @@ | |||
3 | #include <fstream> | 3 | #include <fstream> |
4 | #include <vector> | 4 | #include <vector> |
5 | #include <cstdio> | 5 | #include <cstdio> |
6 | #include "map.h" | 6 | #include "mapview.h" |
7 | 7 | ||
8 | static bool rendererInitialized = false; | 8 | static bool rendererInitialized = false; |
9 | 9 | ||
@@ -27,6 +27,12 @@ static GLuint scanlinesTex; | |||
27 | static GLuint preBloomTex; | 27 | static GLuint preBloomTex; |
28 | static GLuint bloomPassTex; | 28 | static GLuint bloomPassTex; |
29 | 29 | ||
30 | // The VAO | ||
31 | static GLuint VertexArrayID; | ||
32 | |||
33 | // A plane that fills the renderbuffer | ||
34 | static GLuint quad_vertexbuffer; | ||
35 | |||
30 | GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) | 36 | GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) |
31 | { | 37 | { |
32 | // Create the shaders | 38 | // Create the shaders |
@@ -221,7 +227,6 @@ GLFWwindow* initRenderer() | |||
221 | } | 227 | } |
222 | 228 | ||
223 | // Set up vertex array object | 229 | // Set up vertex array object |
224 | GLuint VertexArrayID; | ||
225 | glGenVertexArrays(1, &VertexArrayID); | 230 | glGenVertexArrays(1, &VertexArrayID); |
226 | glBindVertexArray(VertexArrayID); | 231 | glBindVertexArray(VertexArrayID); |
227 | 232 | ||
@@ -269,6 +274,20 @@ GLFWwindow* initRenderer() | |||
269 | 274 | ||
270 | curBuf = 0; | 275 | curBuf = 0; |
271 | 276 | ||
277 | // Load the vertices of a flat surface | ||
278 | GLfloat g_quad_vertex_buffer_data[] = { | ||
279 | -1.0f, -1.0f, 0.0f, | ||
280 | 1.0f, -1.0f, 0.0f, | ||
281 | -1.0f, 1.0f, 0.0f, | ||
282 | -1.0f, 1.0f, 0.0f, | ||
283 | 1.0f, -1.0f, 0.0f, | ||
284 | 1.0f, 1.0f, 0.0f, | ||
285 | }; | ||
286 | |||
287 | glGenBuffers(1, &quad_vertexbuffer); | ||
288 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | ||
289 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW); | ||
290 | |||
272 | artifactsTex = loadBMP_custom("../res/artifacts.bmp"); | 291 | artifactsTex = loadBMP_custom("../res/artifacts.bmp"); |
273 | scanlinesTex = loadBMP_custom("../res/scanlines.bmp"); | 292 | scanlinesTex = loadBMP_custom("../res/scanlines.bmp"); |
274 | 293 | ||
@@ -293,6 +312,9 @@ void destroyRenderer() | |||
293 | exit(-1); | 312 | exit(-1); |
294 | } | 313 | } |
295 | 314 | ||
315 | // Delete the plane buffer | ||
316 | glDeleteBuffers(1, &quad_vertexbuffer); | ||
317 | |||
296 | // Delete the shaders | 318 | // Delete the shaders |
297 | glDeleteProgram(ntscShader); | 319 | glDeleteProgram(ntscShader); |
298 | glDeleteProgram(finalShader); | 320 | glDeleteProgram(finalShader); |
@@ -312,6 +334,9 @@ void destroyRenderer() | |||
312 | // Delete the framebuffer | 334 | // Delete the framebuffer |
313 | glDeleteFramebuffers(1, &FramebufferName); | 335 | glDeleteFramebuffers(1, &FramebufferName); |
314 | 336 | ||
337 | // Delete the VAO | ||
338 | glDeleteVertexArrays(1, &VertexArrayID); | ||
339 | |||
315 | // Kill the window | 340 | // Kill the window |
316 | glfwTerminate(); | 341 | glfwTerminate(); |
317 | 342 | ||
@@ -569,23 +594,9 @@ void renderScreen(Texture* tex) | |||
569 | glUniform1i(glGetUniformLocation(ntscShader, "NTSCArtifactSampler"), 2); | 594 | glUniform1i(glGetUniformLocation(ntscShader, "NTSCArtifactSampler"), 2); |
570 | glUniform1f(glGetUniformLocation(ntscShader, "NTSCLerp"), curBuf * 1.0); | 595 | glUniform1f(glGetUniformLocation(ntscShader, "NTSCLerp"), curBuf * 1.0); |
571 | 596 | ||
572 | // Load the vertices of a flat surface | ||
573 | GLfloat g_quad_vertex_buffer_data[] = { | ||
574 | -1.0f, -1.0f, 0.0f, | ||
575 | 1.0f, -1.0f, 0.0f, | ||
576 | -1.0f, 1.0f, 0.0f, | ||
577 | -1.0f, 1.0f, 0.0f, | ||
578 | 1.0f, -1.0f, 0.0f, | ||
579 | 1.0f, 1.0f, 0.0f, | ||
580 | }; | ||
581 | |||
582 | GLuint quad_vertexbuffer; | ||
583 | glGenBuffers(1, &quad_vertexbuffer); | ||
584 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | ||
585 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW); | ||
586 | |||
587 | // Render our composition | 597 | // Render our composition |
588 | glEnableVertexAttribArray(0); | 598 | glEnableVertexAttribArray(0); |
599 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | ||
589 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); | 600 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); |
590 | glDrawArrays(GL_TRIANGLES, 0, 6); | 601 | glDrawArrays(GL_TRIANGLES, 0, 6); |
591 | glDisableVertexAttribArray(0); | 602 | glDisableVertexAttribArray(0); |
@@ -694,7 +705,6 @@ void renderScreen(Texture* tex) | |||
694 | glfwSwapBuffers(window); | 705 | glfwSwapBuffers(window); |
695 | 706 | ||
696 | glDeleteBuffers(1, &g_norms); | 707 | glDeleteBuffers(1, &g_norms); |
697 | glDeleteBuffers(1, &quad_vertexbuffer); | ||
698 | 708 | ||
699 | curBuf = (curBuf + 1) % 2; | 709 | curBuf = (curBuf + 1) % 2; |
700 | } | 710 | } |
diff --git a/src/state.h b/src/state.h new file mode 100644 index 0000000..e7962ec --- /dev/null +++ b/src/state.h | |||
@@ -0,0 +1,13 @@ | |||
1 | #ifndef STATE_H | ||
2 | #define STATE_H | ||
3 | |||
4 | #include "renderer.h" | ||
5 | |||
6 | class State { | ||
7 | public: | ||
8 | virtual void input(int key, int action) = 0; | ||
9 | virtual void tick() = 0; | ||
10 | virtual void render(Texture* tex) = 0; | ||
11 | }; | ||
12 | |||
13 | #endif | ||