mirror of
https://github.com/ssloy/tinyraycaster.git
synced 2025-08-31 17:41:56 +02:00
std::chrono for FPS control; smooth sliding along the walls
This commit is contained in:
BIN
doc/017.mkv
Normal file
BIN
doc/017.mkv
Normal file
Binary file not shown.
36
gui.cpp
36
gui.cpp
@@ -1,6 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include "SDL.h"
|
||||
|
||||
#include "utils.h"
|
||||
@@ -13,6 +15,7 @@ int main() {
|
||||
{ {3.523, 3.812, 2, 0}, // monsters lists
|
||||
{1.834, 8.765, 0, 0},
|
||||
{5.323, 5.365, 1, 0},
|
||||
{14.32, 13.36, 3, 0},
|
||||
{4.123, 10.76, 1, 0} },
|
||||
Texture("../walltext.png"), // textures for the walls
|
||||
Texture("../monsters.png") }; // textures for the monsters
|
||||
@@ -37,8 +40,17 @@ int main() {
|
||||
SDL_Texture *framebuffer_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, fb.w, fb.h);
|
||||
|
||||
SDL_Event event;
|
||||
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
while (1) {
|
||||
if (SDL_PollEvent(&event)) {
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double, std::milli> fp_ms = t2 - t1;
|
||||
if (fp_ms.count()<20) { // sleep if less than 20 ms since last re-rendering
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(3));
|
||||
t1 = t2;
|
||||
}
|
||||
|
||||
if (SDL_PollEvent(&event)) { // update player's state (walking/turning)
|
||||
if (SDL_QUIT==event.type || (SDL_KEYDOWN==event.type && SDLK_ESCAPE==event.key.keysym.sym)) break;
|
||||
if (SDL_KEYUP==event.type) {
|
||||
if ('a'==event.key.keysym.sym || 'd'==event.key.keysym.sym) gs.player.turn = 0;
|
||||
@@ -52,18 +64,20 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
gs.player.a += float(gs.player.turn)*.05;
|
||||
float nx = gs.player.x + gs.player.walk*cos(gs.player.a)*.1;
|
||||
float ny = gs.player.y + gs.player.walk*sin(gs.player.a)*.1;
|
||||
{ // update player's position
|
||||
gs.player.a += float(gs.player.turn)*.05; // TODO measure elapsed time and modify the speed accordingly
|
||||
float nx = gs.player.x + gs.player.walk*cos(gs.player.a)*.05;
|
||||
float ny = gs.player.y + gs.player.walk*sin(gs.player.a)*.05;
|
||||
|
||||
if (int(nx)>=0 && int(nx)<int(gs.map.w) && int(ny)>=0 && int(ny)<int(gs.map.h) && gs.map.is_empty(nx, ny)) {
|
||||
gs.player.x = nx;
|
||||
gs.player.y = ny;
|
||||
if (int(nx)>=0 && int(nx)<int(gs.map.w) && int(ny)>=0 && int(ny)<int(gs.map.h)) {
|
||||
if (gs.map.is_empty(nx, gs.player.y)) gs.player.x = nx;
|
||||
if (gs.map.is_empty(gs.player.x, ny)) gs.player.y = ny;
|
||||
}
|
||||
for (size_t i=0; i<gs.monsters.size(); i++) { // update the distances from the player to each sprite
|
||||
gs.monsters[i].player_dist = std::sqrt(pow(gs.player.x - gs.monsters[i].x, 2) + pow(gs.player.y - gs.monsters[i].y, 2));
|
||||
}
|
||||
std::sort(gs.monsters.begin(), gs.monsters.end()); // sort it from farthest to closest
|
||||
}
|
||||
for (size_t i=0; i<gs.monsters.size(); i++) { // update the distances from the player to each sprite
|
||||
gs.monsters[i].player_dist = std::sqrt(pow(gs.player.x - gs.monsters[i].x, 2) + pow(gs.player.y - gs.monsters[i].y, 2));
|
||||
}
|
||||
std::sort(gs.monsters.begin(), gs.monsters.end()); // sort it from farthest to closest
|
||||
|
||||
render(fb, gs);
|
||||
SDL_UpdateTexture(framebuffer_texture, NULL, reinterpret_cast<void *>(fb.img.data()), fb.w*4);
|
||||
|
30
map.cpp
30
map.cpp
@@ -2,21 +2,21 @@
|
||||
#include "map.h"
|
||||
|
||||
static const char map[] = "0000222222220000"\
|
||||
"1 0"\
|
||||
"1 11111 0"\
|
||||
"1 0 0"\
|
||||
"0 0 1110000"\
|
||||
"0 3 0"\
|
||||
"0 10000 0"\
|
||||
"0 3 11100 0"\
|
||||
"5 4 0 0"\
|
||||
"5 4 1 00000"\
|
||||
"0 1 0"\
|
||||
"2 1 0"\
|
||||
"0 0 0"\
|
||||
"0 0000000 0"\
|
||||
"0 0"\
|
||||
"0002222222200000";
|
||||
"1 5"\
|
||||
"1 5"\
|
||||
"1 01111 5"\
|
||||
"0 0 5"\
|
||||
"0 3 1155"\
|
||||
"0 1000 5"\
|
||||
"0 3 0 5"\
|
||||
"5 4 100011 5"\
|
||||
"5 4 1 4"\
|
||||
"0 1 4"\
|
||||
"2 1 44444"\
|
||||
"0 000 4"\
|
||||
"0 111 4"\
|
||||
"0 4"\
|
||||
"0002222244444444";
|
||||
|
||||
Map::Map() : w(16), h(16) {
|
||||
assert(sizeof(map) == w*h+1); // +1 for the null terminated string
|
||||
|
@@ -84,7 +84,7 @@ void render(FrameBuffer &fb, const GameState &gs) {
|
||||
assert(texid<tex_walls.count);
|
||||
float dist = t*cos(angle-player.a);
|
||||
depth_buffer[i] = dist;
|
||||
size_t column_height = fb.h/dist;
|
||||
size_t column_height = std::min(2000, int(fb.h/dist));
|
||||
int x_texcoord = wall_x_texcoord(x, y, tex_walls);
|
||||
std::vector<uint32_t> column = tex_walls.get_scaled_column(texid, x_texcoord, column_height);
|
||||
int pix_x = i + fb.w/2; // we are drawing at the right half of the screen, thus +fb.w/2
|
||||
|
Reference in New Issue
Block a user