Updated Amit's code to run in sfml 3.0
This commit is contained in:
parent
105c974f1c
commit
7fb2d5cf26
3 changed files with 58 additions and 28 deletions
35
amt/main.cpp
35
amt/main.cpp
|
@ -24,10 +24,15 @@ Matrix MAP{
|
|||
{1,1,1,1,1,1,1,1,1}
|
||||
};
|
||||
|
||||
int main() {
|
||||
using KB = sf::Keyboard;
|
||||
void draw_gui(sf::RenderWindow &window) {
|
||||
sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, 300});
|
||||
rect.setPosition({0,0});
|
||||
rect.setFillColor({100, 100, 100});
|
||||
window.draw(rect);
|
||||
}
|
||||
|
||||
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Zed's Ray Caster Game Thing");
|
||||
int main() {
|
||||
sf::RenderWindow window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Ray Caster Game Thing");
|
||||
|
||||
//ZED this should set with a function
|
||||
float player_x = MAP.rows() / 2;
|
||||
|
@ -40,6 +45,12 @@ int main() {
|
|||
double moveSpeed = 0.1;
|
||||
double rotSpeed = 0.1;
|
||||
|
||||
const auto onClose = [&window](const sf::Event::Closed&)
|
||||
{
|
||||
window.close();
|
||||
};
|
||||
|
||||
|
||||
std::size_t const max_count = 100;
|
||||
std::vector<double> frames(max_count);
|
||||
std::size_t it = 1;
|
||||
|
@ -57,27 +68,23 @@ int main() {
|
|||
it = 1;
|
||||
}
|
||||
++it;
|
||||
// DRAW GUI
|
||||
|
||||
draw_gui(window);
|
||||
window.display();
|
||||
|
||||
if(KB::isKeyPressed(KB::W)) {
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
|
||||
rayview.run(moveSpeed, 1);
|
||||
} else if(KB::isKeyPressed(KB::S)) {
|
||||
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) {
|
||||
rayview.run(moveSpeed, -1);
|
||||
}
|
||||
|
||||
if(KB::isKeyPressed(KB::D)) {
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
|
||||
rayview.rotate(rotSpeed, -1);
|
||||
} else if(KB::isKeyPressed(KB::A)) {
|
||||
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
|
||||
rayview.rotate(rotSpeed, 1);
|
||||
}
|
||||
|
||||
sf::Event event;
|
||||
while(window.pollEvent(event)) {
|
||||
if(event.type == sf::Event::Closed) {
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
window.handleEvents(onClose);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -5,7 +5,31 @@
|
|||
using namespace fmt;
|
||||
using std::make_unique;
|
||||
|
||||
union ColorConv {
|
||||
struct {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
uint8_t a;
|
||||
} as_color;
|
||||
uint32_t as_int;
|
||||
};
|
||||
|
||||
inline uint32_t dumb_lighting(uint32_t pixel, double distance) {
|
||||
if(distance < 0.9) return pixel;
|
||||
|
||||
ColorConv conv{.as_int=pixel};
|
||||
conv.as_color.r /= distance;
|
||||
conv.as_color.g /= distance;
|
||||
conv.as_color.b /= distance;
|
||||
|
||||
return conv.as_int;
|
||||
}
|
||||
|
||||
|
||||
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) :
|
||||
view_texture({(unsigned int)width, (unsigned int)height}),
|
||||
view_sprite(view_texture),
|
||||
$width(width), $height(height),
|
||||
pixels(static_cast<size_t>(height), static_cast<std::size_t>(width)),
|
||||
$window(window),
|
||||
|
@ -15,14 +39,12 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh
|
|||
ZBuffer(width)
|
||||
{
|
||||
$window.setVerticalSyncEnabled(true);
|
||||
view_texture.create($width, $height);
|
||||
view_sprite.setTexture(view_texture);
|
||||
view_sprite.setPosition(0, 0);
|
||||
view_sprite.setPosition({0, 0});
|
||||
textures.load_textures();
|
||||
}
|
||||
|
||||
void Raycaster::set_position(int x, int y) {
|
||||
view_sprite.setPosition(x, y);
|
||||
view_sprite.setPosition({(float)x, (float)y});
|
||||
}
|
||||
|
||||
void Raycaster::position_camera(float player_x, float player_y) {
|
||||
|
@ -32,7 +54,7 @@ void Raycaster::position_camera(float player_x, float player_y) {
|
|||
}
|
||||
|
||||
void Raycaster::draw_pixel_buffer() {
|
||||
view_texture.update(pixels.to_raw_buf(), $width, $height, 0, 0);
|
||||
view_texture.update(pixels.to_raw_buf(), {(unsigned int)$width, (unsigned int)$height}, {0, 0});
|
||||
// BUG: can I do this once and just update it?
|
||||
$window.draw(view_sprite);
|
||||
}
|
||||
|
@ -64,6 +86,7 @@ void Raycaster::sprite_casting() {
|
|||
int sprite_index = spriteOrder[i];
|
||||
Sprite& sprite_rec = textures.get_sprite(sprite_index);
|
||||
auto& sprite_texture = textures.get_texture(sprite_rec.texture);
|
||||
|
||||
double spriteX = sprite_rec.x - posX;
|
||||
double spriteY = sprite_rec.y - posY;
|
||||
|
||||
|
|
18
meson.build
18
meson.build
|
@ -40,12 +40,12 @@ executable('zedcaster', [
|
|||
],
|
||||
dependencies: dependencies)
|
||||
|
||||
# executable('amtcaster', [
|
||||
# 'dbc.cpp',
|
||||
# 'config.cpp',
|
||||
# 'amt/texture.cpp',
|
||||
# 'amt/raycaster.cpp',
|
||||
# 'amt/main.cpp'
|
||||
# ],
|
||||
# cpp_args: ['-std=c++23'],
|
||||
# dependencies: dependencies)
|
||||
executable('amtcaster', [
|
||||
'dbc.cpp',
|
||||
'config.cpp',
|
||||
'amt/texture.cpp',
|
||||
'amt/raycaster.cpp',
|
||||
'amt/main.cpp'
|
||||
],
|
||||
cpp_args: ['-std=c++23'],
|
||||
dependencies: dependencies)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue