From 6b99c7aee539e35b8e67520f36adeca9007641cb Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 17 Feb 2015 13:28:50 -0500 Subject: Refactored map loader and added a second map Also tweaked the font for apostrophe, p, and q --- src/main.cpp | 91 +++++++++++++++--------------------------------------------- 1 file changed, 22 insertions(+), 69 deletions(-) (limited to 'src/main.cpp') 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 @@ #include #include #include "map.h" +#include "state.h" +#include "mapview.h" #include "renderer.h" using namespace::std; -const int FRAMES_PER_SECOND = 60; -bool holding_left = false; -bool holding_right = false; bool quit = false; -mob_t* player; -// Initialize jump physics -double jump_height = TILE_HEIGHT*3; -double jump_length = 0.25 * FRAMES_PER_SECOND; -double jump_velocity = -2 * jump_height / jump_length; -double jump_gravity = -1 * jump_velocity / jump_length; +State* curGameState; void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { - if (action == GLFW_PRESS) + if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS)) { - switch (key) - { - case GLFW_KEY_LEFT: holding_left = true; break; - case GLFW_KEY_RIGHT: holding_right = true; break; - case GLFW_KEY_UP: player->y_vel = jump_velocity; break; - case GLFW_KEY_ESCAPE: quit = true; break; - } - } else if (action == GLFW_RELEASE) + quit = true; + } + + if (curGameState != NULL) { - switch (key) - { - case GLFW_KEY_LEFT: holding_left = false; break; - case GLFW_KEY_RIGHT: holding_right = false; break; - } + curGameState->input(key, action); } } @@ -44,63 +30,30 @@ int main() glfwSwapInterval(1); glfwSetKeyCallback(window, key_callback); - Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT); + Map* m = new Map("../maps/bigmap.txt"); + Map* m2 = new Map("../maps/cozy.txt"); - // Initialize player data - player = new mob_t(); - player->x = 100; - player->y = 100; - player->x_vel = 0; - player->y_vel = 0; - player->x_accel = 0; - player->y_accel = jump_gravity; - player->w = 10; - player->h = 14; + m->setLeftMap(m2); + m2->setRightMap(m); - Map* map = new Map(); - - Texture* tiles = loadTextureFromBMP("../res/tiles.bmp"); + curGameState = new MapView(m, 100, 100); + + Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT); - while (!quit) + while (!(quit || glfwWindowShouldClose(window))) { - if (holding_left && player->x_vel >= 0) - { - player->x_vel = -2; - } else if (holding_right && player->x_vel <= 0) - { - player->x_vel = 2; - } else if (!holding_left && !holding_right) { - player->x_vel = 0; - } - - player->x_vel += player->x_accel; - if (player->x_vel < -16) player->x_vel = -16; - if (player->x_vel > 16) player->x_vel = 16; - int playerx_next = player->x + player->x_vel; - - player->y_vel += player->y_accel; - if (player->y_vel > 16) player->y_vel = 16; // Terminal velocity - if (player->y_vel < -16) player->y_vel = -16; - int playery_next = player->y + player->y_vel; - - map->check_collisions(player, playerx_next, playery_next); + // Tick! + curGameState->tick(); // Do rendering - map->render(buffer); - - //Rectangle src_rect(96, 0, 8, 8); - Rectangle dst_rect(player->x, player->y, player->w, player->h); - - //blitTexture(tiles, buffer, &src_rect, &dst_rect); - fillTexture(buffer, &dst_rect, 255, 255, 255); - + curGameState->render(buffer); renderScreen(buffer); glfwPollEvents(); } - delete map; - delete player; + delete curGameState; + delete m; destroyRenderer(); -- cgit 1.4.1