summary refs log tree commit diff stats
path: root/src/main.cpp
blob: e1e1aa270ea3511268c266d73fd3787b4239a155 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <ctime>
#include <list>
#include "map.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;

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
  if (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)
  {
    switch (key)
    {
      case GLFW_KEY_LEFT: holding_left = false; break;
      case GLFW_KEY_RIGHT: holding_right = false; break;
    }
  }
}

int main()
{
  GLFWwindow* window = initRenderer();
  glfwSwapInterval(1);
  glfwSetKeyCallback(window, key_callback);
  
  Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT);
  
  // 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;
  
  Map* map = new Map();

  Texture* tiles = loadTextureFromBMP("../res/tiles.bmp");
  
  while (!quit)
  {
    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);
    
    // 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);
    
    renderScreen(buffer);
    
    glfwPollEvents();
  }
  
  delete map;
  delete player;
  
  destroyRenderer();
  
  return 0;
}