Browse Source

frame cap shit

master
Arc0re 5 years ago
parent
commit
55da49a960
  1. 68
      game.cpp
  2. BIN
      game.exe

68
game.cpp

@ -18,13 +18,16 @@ @@ -18,13 +18,16 @@
#define MAX_MAP_TILES_Y 11 /*16*/
#define TILE_WIDTH 16
#define TILE_HEIGHT 16
#define PLAYER_SPEED 1.0f
#define UNUSED(x) (void)(x)
//#define for_map for (int col = 0; col < MAX_MAP_TILES_Y; ++col) for (int row = 0; row < MAX_MAP_TILES_X; ++row)
#define debug_print_rect(r) printf("x: %d, y: %d, w: %d, h: %d\n", r.x, r.y, r.w, r.h);
#define USE_VSYNC 0
#define USE_LOGICAL_RENDER_VIEW 1
#define FPS 60
//#define USE_STATIC_ARRAY_LVL
using namespace std;
@ -89,6 +92,18 @@ struct R { @@ -89,6 +92,18 @@ struct R {
}
};
/*struct Keys {
static bool down_pressed;
static bool up_pressed;
static bool left_pressed;
static bool right_pressed;
};
bool Keys::down_pressed = false;
bool Keys::up_pressed = false;
bool Keys::left_pressed = false;
bool Keys::right_pressed = false;
*/
// Global variables
vector<R> rooms;
SDL_Window* window;
@ -100,11 +115,26 @@ struct Player { @@ -100,11 +115,26 @@ struct Player {
Rect src_rect;
Rect body_hitbox; // player is a 16*16 tile + 8 pixels in height for the head, we ignore those
int x, y;
Player() : src_rect(0, 39, TILE_WIDTH, TILE_HEIGHT + 10/*head size*/), body_hitbox(0, 48, TILE_WIDTH, TILE_HEIGHT), x(0), y(0) {}
float speed;
int x_vel, y_vel;
Player() : src_rect(0, 39, TILE_WIDTH, TILE_HEIGHT + 10/*head size*/), body_hitbox(0, 48, TILE_WIDTH, TILE_HEIGHT), x(0), y(0), speed(PLAYER_SPEED)
,x_vel(0), y_vel(0) {}
void draw() {
Rect dst(x, y, src_rect()->w, src_rect()->h);
SDL_RenderCopy(renderer, tiles, src_rect(), dst());
}
}
void update() {
// TODO: time wtf, tick
/*if (Keys::down_pressed) y += speed;
if (Keys::up_pressed) y -= speed;
if (Keys::left_pressed) x -= speed;
if (Keys::right_pressed) x += speed;*/
x += x_vel;
y += y_vel;
}
} player;
// A room is a screen in the sense of a static screen, aka the player doesnt move a camera,
@ -149,7 +179,12 @@ extern "C" int main(int argc, char** argv) { @@ -149,7 +179,12 @@ extern "C" int main(int argc, char** argv) {
window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (!window) die("failed to create window");
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED
#if USE_VSYNC
| SDL_RENDERER_PRESENTVSYNC);
#else
);
#endif
if (!renderer) die("failed to create renderer");
#if USE_LOGICAL_RENDER_VIEW
@ -167,10 +202,11 @@ extern "C" int main(int argc, char** argv) { @@ -167,10 +202,11 @@ extern "C" int main(int argc, char** argv) {
int next_room(0);
quit = false;
//float now(0), secs(0), last_time(0);
while (!quit) {
SDL_Event e;
if (SDL_PollEvent(&e)) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) quit = true;
if (e.type == SDL_KEYDOWN) {
@ -181,16 +217,40 @@ extern "C" int main(int argc, char** argv) { @@ -181,16 +217,40 @@ extern "C" int main(int argc, char** argv) {
case SDLK_1: next_room = 1; break;
case SDLK_2: next_room = 2; break;
// fin debug
case SDLK_DOWN: player.y_vel = PLAYER_SPEED; break;
case SDLK_UP: player.y_vel = -PLAYER_SPEED; break;
case SDLK_RIGHT: player.x_vel = PLAYER_SPEED; break;
case SDLK_LEFT: player.x_vel = -PLAYER_SPEED; break;
}
} else if (e.type == SDL_KEYUP) {
switch (e.key.keysym.sym) {
case SDLK_DOWN: if (player.y_vel > 0) player.y_vel = 0; break;
case SDLK_UP: if (player.y_vel < 0) player.y_vel = 0; break;
case SDLK_RIGHT: if (player.x_vel > 0) player.x_vel = 0; break;
case SDLK_LEFT: if (player.x_vel < 0) player.x_vel = 0; break;
}
}
}
player.update(); //TODO: time and shit
SDL_RenderClear(renderer);
// Current room rendering
draw_room(&next_room);
player.draw();
/*
float freq = SDL_GetPerformanceFrequency();
now = SDL_GetPerformanceCounter();
secs = (now - last_time) / freq;
SDL_Delay(FPS - secs * 1000);
*/
SDL_Delay(16);
SDL_RenderPresent(renderer);
}

BIN
game.exe

Binary file not shown.
Loading…
Cancel
Save