Exploring raycasters and possibly make a little "doom like" game based on it.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
raycaster/main.cpp

139 lines
3.8 KiB

#include "raycaster.hpp"
#include <iostream>
#include <chrono>
#include <numeric>
#include <functional>
#include "constants.hpp"
#include "stats.hpp"
#include "worldbuilder.hpp"
void draw_gui(sf::RenderWindow &window, sf::Text &text, Stats &stats) {
sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT});
rect.setPosition({0,0});
rect.setFillColor({50, 50, 50});
window.draw(rect);
text.setString(
fmt::format("FPS\nmean:{:>8.5}\nsdev: {:>8.5}\nmin: {:>8.5}\nmax: {:>8.5}\ncount:{:<10}\n\nVSync? {}\nFR Limit: {}\nDebug? {}\n\nHit R to reset.",
stats.mean(), stats.stddev(), stats.min, stats.max, stats.n, VSYNC, FRAME_LIMIT, DEBUG_BUILD));
window.draw(text);
}
Matrix generate_map(Map& map, Point &player) {
// generate the world and make the map
WorldBuilder builder(map);
builder.generate_map();
bool can_place = map.place_entity(1, player);
dbc::check(can_place, "couldn't place the player");
auto &tiles = map.tiles();
tiles.dump(player.x, player.y);
auto bad_map = matrix::make(tiles.width(), tiles.height());
for(matrix::each_cell it(tiles.$tile_ids); it.next();) {
switch(tiles.$tile_ids[it.y][it.x]) {
case 0x289e:
bad_map[it.y][it.x] = 0; break;
case 0xa5b8:
bad_map[it.y][it.x] = 1; break;
case 0x19f0:
bad_map[it.y][it.x] = 2; break;
case 0x16de:
bad_map[it.y][it.x] = 3; break;
default:
bad_map[it.y][it.x] = 0;
}
}
matrix::dump("CONVERTED MAP", bad_map);
return bad_map;
}
void draw_weapon(sf::RenderWindow &window, sf::Sprite &weapon, float rotation) {
weapon.setPosition({SCREEN_WIDTH/2,SCREEN_HEIGHT/2});
weapon.setRotation(sf::degrees(rotation));
window.draw(weapon);
}
int main() {
sf::RenderWindow window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Ray Caster Game Thing");
sf::Font font{"./assets/text.otf"};
sf::Text text{font};
text.setFillColor({255,255,255});
text.setPosition({10,10});
Map map(30, 30);
Point player{0, 0};
auto MAP = generate_map(map, player);
Raycaster rayview(window, MAP, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y);
rayview.position_camera(player.x, player.y);
rayview.init_shaders();
double moveSpeed = 0.1;
double rotSpeed = 0.1;
const auto onClose = [&window](const sf::Event::Closed&)
{
window.close();
};
float rotation = -30.0f;
Stats stats;
window.setVerticalSyncEnabled(VSYNC);
window.setFramerateLimit(FRAME_LIMIT);
while(window.isOpen()) {
auto start = std::chrono::high_resolution_clock::now();
rayview.render();
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration<double>(end - start);
stats.sample(1/elapsed.count());
auto weapon_sprite_ptr = rayview.$textures.sword.sprite;
draw_gui(window, text, stats);
draw_weapon(window, *weapon_sprite_ptr, rotation);
window.display();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
rayview.run(moveSpeed, 1);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) {
rayview.run(moveSpeed, -1);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
rayview.rotate(rotSpeed, -1);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
rayview.rotate(rotSpeed, 1);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) {
stats.reset();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::P)) {
if(rayview.$active_shader == nullptr) {
rayview.$active_shader = &rayview.$paused;
} else {
rayview.$active_shader = nullptr;
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
rayview.$anim.play();
rotation = -30.0f;
} else {
rotation = -10.0f;
}
window.handleEvents(onClose);
}
return 0;
}