First cut of pulling out the relevant parts of my original game to make a little framework.
This commit is contained in:
commit
6a0c9e8d46
177 changed files with 18197 additions and 0 deletions
312
src/graphics/animation.cpp
Normal file
312
src/graphics/animation.cpp
Normal file
|
|
@ -0,0 +1,312 @@
|
|||
#include "graphics/animation.hpp"
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
#include "dbc.hpp"
|
||||
#include "algos/rand.hpp"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include "game/sound.hpp"
|
||||
#include "game/components.hpp"
|
||||
|
||||
constexpr float SUB_FRAME_SENSITIVITY = 0.999f;
|
||||
|
||||
namespace animation {
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
std::vector<sf::IntRect> Animation::calc_frames() {
|
||||
dbc::check(sequence.frames.size() == sequence.durations.size(), "sequence.frames.size() != sequence.durations.size()");
|
||||
|
||||
std::vector<sf::IntRect> frames;
|
||||
|
||||
for(int frame_i : sequence.frames) {
|
||||
dbc::check(frame_i < sheet.frames, "frame index greater than sheet frames");
|
||||
frames.emplace_back(
|
||||
sf::Vector2i{sheet.frame_width * frame_i, 0}, // NOTE: one row only for now
|
||||
sf::Vector2i{sheet.frame_width,
|
||||
sheet.frame_height});
|
||||
}
|
||||
|
||||
return frames;
|
||||
}
|
||||
|
||||
void Animation::play() {
|
||||
dbc::check(!playing, "can't call play while playing?");
|
||||
sequence.current = 0;
|
||||
sequence.subframe = 0.0f;
|
||||
sequence.loop_count = 0;
|
||||
playing = true;
|
||||
sequence.timer.start();
|
||||
sequence.INVARIANT();
|
||||
}
|
||||
|
||||
void Animation::stop() {
|
||||
playing = false;
|
||||
sequence.timer.reset();
|
||||
}
|
||||
|
||||
// need one for each kind of thing to animate
|
||||
// NOTE: possibly find a way to only run apply on frame change?
|
||||
void Animation::apply(sf::Sprite& sprite) {
|
||||
dbc::check(sequence.current < $frame_rects.size(), "current frame past $frame_rects");
|
||||
// NOTE: pos is not updated yet
|
||||
auto& rect = $frame_rects.at(sequence.current);
|
||||
sprite.setTextureRect(rect);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Alternative mostly used in raycaster.cpp that
|
||||
* DOES NOT setTextureRect() but just points
|
||||
* the rect_io at the correct frame, but leaves
|
||||
* it's size and base position alone.
|
||||
*/
|
||||
void Animation::apply(sf::Sprite& sprite, sf::IntRect& rect_io) {
|
||||
dbc::check(sequence.current < $frame_rects.size(), "current frame past $frame_rects");
|
||||
auto& rect = $frame_rects.at(sequence.current);
|
||||
rect_io.position.x += rect.position.x;
|
||||
rect_io.position.y += rect.position.y;
|
||||
}
|
||||
|
||||
void Animation::motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale) {
|
||||
sequence.INVARIANT();
|
||||
|
||||
transform.apply(sequence, pos, scale);
|
||||
|
||||
if(transform.flipped) {
|
||||
scale.x *= -1;
|
||||
}
|
||||
|
||||
sprite.setPosition(pos);
|
||||
|
||||
if(transform.scaled) {
|
||||
sprite.setScale(scale);
|
||||
}
|
||||
}
|
||||
|
||||
void Animation::motion(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
|
||||
dbc::check(size.x > 1.0f && size.y > 1.0f, "motion size must be above 1.0 since it's not a ratio");
|
||||
dbc::check(transform.flipped == false, "transform must be false, has no effect on View");
|
||||
|
||||
sf::Vector2f scale{transform.min_x, transform.min_y};
|
||||
|
||||
transform.apply(sequence, pos, scale);
|
||||
|
||||
view_out.setCenter(pos);
|
||||
|
||||
if(transform.scaled) {
|
||||
view_out.setSize({size.x * scale.x, size.y * scale.y});
|
||||
} else {
|
||||
view_out.setSize(size);
|
||||
}
|
||||
}
|
||||
|
||||
void Animation::apply_effect(std::shared_ptr<sf::Shader> effect) {
|
||||
dbc::check(effect != nullptr, "can't apply null effect");
|
||||
effect->setUniform("u_time", sequence.timer.getElapsedTime().asSeconds());
|
||||
sf::Vector2f u_resolution{float(sheet.frame_width), float(sheet.frame_height)};
|
||||
effect->setUniform("u_resolution", u_resolution);
|
||||
}
|
||||
|
||||
void Animation::play_sound() {
|
||||
// BUG: this can be optimized way better
|
||||
if(sounds.contains(form_name)) {
|
||||
for(auto& [at_frame, sound_name] : sounds.at(form_name)) {
|
||||
if(sequence.current == at_frame) {
|
||||
sound::play(sound_name);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt::println("Animation has not sound {}", form_name);
|
||||
}
|
||||
}
|
||||
|
||||
/* REFACTOR: I believe this is wrong still. If ::commit() determines number of ticks+alpha since last
|
||||
* render then update needs to be called 1/tick. The Timer will keep track of alpha as the error
|
||||
* between commit calls, so this function only really needs to care about ticks. But, I'm still
|
||||
* calling getElapsedTime() when I already did that in commit(), so should I just ignore that and assume
|
||||
* elapsed is DELTA, or use elapsed here?
|
||||
*/
|
||||
void Animation::update() {
|
||||
dbc::check(playing, "attempt to update animation that's not playing");
|
||||
sequence.INVARIANT();
|
||||
|
||||
auto [ticks, alpha] = sequence.timer.commit();
|
||||
int duration = sequence.durations.at(sequence.current);
|
||||
sequence.subframe += ticks;
|
||||
|
||||
sequence.easing_position += ticks;
|
||||
|
||||
bool frame_change = false;
|
||||
|
||||
if(sequence.subframe >= duration) {
|
||||
sequence.timer.restart();
|
||||
sequence.current++;
|
||||
sequence.subframe = 0;
|
||||
frame_change = true;
|
||||
}
|
||||
|
||||
if(sequence.current >= sequence.frame_count) {
|
||||
sequence.loop_count++;
|
||||
sequence.easing_position = 0;
|
||||
playing = onLoop(sequence, transform);
|
||||
sequence.INVARIANT();
|
||||
}
|
||||
|
||||
if(frame_change) play_sound();
|
||||
|
||||
if(frame_change && onFrame != nullptr) onFrame();
|
||||
}
|
||||
|
||||
void Timer::start() {
|
||||
clock.start();
|
||||
prev_time = clock.getElapsedTime().asSeconds();
|
||||
}
|
||||
|
||||
void Timer::reset() {
|
||||
elapsed_ticks = 0;
|
||||
clock.reset();
|
||||
}
|
||||
|
||||
void Timer::restart() {
|
||||
elapsed_ticks = 0;
|
||||
clock.restart();
|
||||
prev_time = clock.getElapsedTime().asSeconds();
|
||||
}
|
||||
|
||||
sf::Time Timer::getElapsedTime() {
|
||||
return clock.getElapsedTime();
|
||||
}
|
||||
|
||||
std::pair<int, double> Timer::commit() {
|
||||
// determine frame duration based on previous time
|
||||
current_time = clock.getElapsedTime().asSeconds();
|
||||
frame_duration = current_time - prev_time;
|
||||
|
||||
// update prev_time for the next call
|
||||
prev_time = current_time;
|
||||
|
||||
// update accumulator, retaining previous errors
|
||||
accumulator += frame_duration;
|
||||
|
||||
// find the tick count based on DELTA
|
||||
double tick_count = floor(accumulator / DELTA);
|
||||
|
||||
// reduce accumulator by the number of DELTAS
|
||||
accumulator -= tick_count * DELTA;
|
||||
// that leaves the remaining errors for next loop
|
||||
|
||||
elapsed_ticks += tick_count;
|
||||
|
||||
// alpha is then what we lerp...but WHY?!
|
||||
alpha = accumulator / DELTA;
|
||||
|
||||
// return the number of even DELTA ticks and the alpha
|
||||
return {int(tick_count), alpha};
|
||||
}
|
||||
|
||||
void Transform::apply(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out) {
|
||||
float tick = easing_func(seq.easing_position / seq.easing_duration);
|
||||
|
||||
motion_func(*this, pos_out, scale_out, tick, relative);
|
||||
}
|
||||
|
||||
bool Animation::has_form(const std::string& as_form) {
|
||||
return forms.contains(as_form);
|
||||
}
|
||||
|
||||
void Animation::set_form(const std::string& as_form) {
|
||||
dbc::check(forms.contains(as_form),
|
||||
$F("form {} does not exist in animation", as_form));
|
||||
stop();
|
||||
|
||||
const auto& [seq_name, tr_name] = forms.at(as_form);
|
||||
|
||||
dbc::check(sequences.contains(seq_name),
|
||||
$F("sequences do NOT have \"{}\" name", seq_name));
|
||||
|
||||
dbc::check(transforms.contains(tr_name),
|
||||
$F("transforms do NOT have \"{}\" name", tr_name));
|
||||
|
||||
// everything good, do the update
|
||||
form_name = as_form;
|
||||
sequence_name = seq_name;
|
||||
transform_name = tr_name;
|
||||
sequence = sequences.at(seq_name);
|
||||
transform = transforms.at(tr_name);
|
||||
|
||||
sequence.frame_count = sequence.frames.size();
|
||||
|
||||
// BUG: should this be configurable instead?
|
||||
for(auto duration : sequence.durations) {
|
||||
sequence.easing_duration += float(duration);
|
||||
}
|
||||
dbc::check(sequence.easing_duration > 0.0, "bad easing duration");
|
||||
|
||||
$frame_rects = calc_frames();
|
||||
transform.easing_func = ease2::get_easing(transform.easing);
|
||||
transform.motion_func = ease2::get_motion(transform.motion);
|
||||
|
||||
sequence.INVARIANT();
|
||||
}
|
||||
|
||||
Animation load(const std::string &file, const std::string &anim_name) {
|
||||
using nlohmann::json;
|
||||
std::ifstream infile(file);
|
||||
auto data = json::parse(infile);
|
||||
|
||||
dbc::check(data.contains(anim_name),
|
||||
$F("{} animation config does not have animation {}", file, anim_name));
|
||||
|
||||
Animation anim;
|
||||
animation::from_json(data[anim_name], anim);
|
||||
anim.name = anim_name;
|
||||
|
||||
dbc::check(anim.forms.contains("idle"),
|
||||
$F("animation {} must have 'idle' form", anim_name));
|
||||
|
||||
anim.set_form("idle");
|
||||
|
||||
return anim;
|
||||
}
|
||||
|
||||
void Sequence::INVARIANT(const std::source_location location) {
|
||||
dbc::check(frames.size() == durations.size(),
|
||||
$F("frames.size={} doesn't match durations.size={}",
|
||||
frames.size(), durations.size()), location);
|
||||
|
||||
dbc::check(easing_duration > 0.0, $F("bad easing duration: {}", easing_duration), location);
|
||||
|
||||
dbc::check(frame_count == frames.size(),
|
||||
$F("frame_count={} doesn't match frames.size={}", frame_count, frames.size()), location);
|
||||
|
||||
dbc::check(frame_count == durations.size(),
|
||||
$F("frame_count={} doesn't match durations.size={}", frame_count, durations.size()), location);
|
||||
|
||||
dbc::check(current < durations.size(),
|
||||
$F("current={} went past end of fame durations.size={}", current, durations.size()), location);
|
||||
}
|
||||
|
||||
// BUG: BAAADD REMOVE
|
||||
bool has(const std::string& name) {
|
||||
using nlohmann::json;
|
||||
std::ifstream infile("assets/animation.json");
|
||||
auto data = json::parse(infile);
|
||||
return data.contains(name);
|
||||
}
|
||||
|
||||
void configure(DinkyECS::World& world, DinkyECS::Entity entity) {
|
||||
auto sprite = world.get_if<components::Sprite>(entity);
|
||||
|
||||
if(sprite != nullptr && has(sprite->name)) {
|
||||
world.set<Animation>(entity, animation::load("assets/animation.json", sprite->name));
|
||||
}
|
||||
}
|
||||
|
||||
void animate_entity(DinkyECS::World &world, DinkyECS::Entity entity) {
|
||||
auto anim = world.get_if<Animation>(entity);
|
||||
|
||||
if(anim != nullptr && !anim->playing) {
|
||||
anim->play();
|
||||
}
|
||||
}
|
||||
}
|
||||
150
src/graphics/animation.hpp
Normal file
150
src/graphics/animation.hpp
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
#pragma once
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
#include <SFML/Graphics/Sprite.hpp>
|
||||
#include <SFML/Graphics/Shader.hpp>
|
||||
#include <SFML/Graphics/View.hpp>
|
||||
#include <SFML/System/Clock.hpp>
|
||||
#include <SFML/System/Time.hpp>
|
||||
#include <functional>
|
||||
#include "graphics/easing.hpp"
|
||||
#include <fmt/core.h>
|
||||
#include "game/json_mods.hpp"
|
||||
#include <source_location>
|
||||
#include "algos/dinkyecs.hpp"
|
||||
|
||||
namespace animation {
|
||||
|
||||
template <typename T> struct NameOf;
|
||||
|
||||
struct Sheet {
|
||||
int frames{0};
|
||||
int frame_width{0};
|
||||
int frame_height{0};
|
||||
};
|
||||
|
||||
struct Timer {
|
||||
double DELTA = 1.0/60.0;
|
||||
double accumulator = 0.0;
|
||||
double prev_time = 0.0;
|
||||
double current_time = 0.0;
|
||||
double frame_duration = 0.0;
|
||||
double alpha = 0.0;
|
||||
int elapsed_ticks = 0;
|
||||
sf::Clock clock{};
|
||||
|
||||
std::pair<int, double> commit();
|
||||
void start();
|
||||
void reset();
|
||||
void restart();
|
||||
sf::Time getElapsedTime();
|
||||
};
|
||||
|
||||
struct Sequence {
|
||||
std::vector<int> frames{};
|
||||
std::vector<int> durations{}; // in ticks
|
||||
size_t current{0};
|
||||
int loop_count{0};
|
||||
size_t frame_count{frames.size()};
|
||||
Timer timer{};
|
||||
int subframe{0};
|
||||
float easing_duration{0.0f};
|
||||
float easing_position{0.0f};
|
||||
|
||||
void INVARIANT(const std::source_location location = std::source_location::current());
|
||||
};
|
||||
|
||||
struct Transform {
|
||||
// how to know when a transform ends?
|
||||
float min_x{1.0f};
|
||||
float min_y{1.0f};
|
||||
float max_x{1.0f};
|
||||
float max_y{1.0f};
|
||||
bool flipped{false};
|
||||
bool scaled{false};
|
||||
bool relative{false};
|
||||
|
||||
// handled by onLoop
|
||||
bool toggled{false};
|
||||
bool looped{false};
|
||||
std::string easing{"in_out_back"};
|
||||
std::string motion{"move_rush"};
|
||||
|
||||
// change to using a callback function for these
|
||||
ease2::EaseFunc easing_func{ease2::get_easing(easing)};
|
||||
ease2::MotionFunc motion_func{ease2::get_motion(motion)};
|
||||
|
||||
std::shared_ptr<sf::Shader> shader{nullptr};
|
||||
|
||||
void apply(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out);
|
||||
};
|
||||
|
||||
/* Gets the number of times it looped, and returns if it should stop. */
|
||||
using OnLoopHandler = std::function<bool(Sequence& seq, Transform& tr)>;
|
||||
using OnFrameHandler = std::function<void()>;
|
||||
|
||||
inline bool DefaultOnLoop(Sequence& seq, Transform& tr) {
|
||||
if(tr.toggled) {
|
||||
seq.current = seq.frame_count - 1;
|
||||
} else {
|
||||
seq.current = 0;
|
||||
}
|
||||
|
||||
return tr.looped;
|
||||
}
|
||||
|
||||
using Form = std::pair<std::string, std::string>;
|
||||
using Sound = std::pair<size_t, std::string>;
|
||||
|
||||
class Animation {
|
||||
public:
|
||||
Sheet sheet;
|
||||
std::unordered_map<std::string, Sequence> sequences;
|
||||
std::unordered_map<std::string, Transform> transforms;
|
||||
std::unordered_map<std::string, Form> forms;
|
||||
std::unordered_map<std::string, std::vector<Sound>> sounds;
|
||||
OnFrameHandler onFrame = nullptr;
|
||||
|
||||
Sequence sequence{};
|
||||
Transform transform{};
|
||||
|
||||
std::vector<sf::IntRect> $frame_rects{calc_frames()};
|
||||
OnLoopHandler onLoop = DefaultOnLoop;
|
||||
bool playing = false;
|
||||
|
||||
// mostly for debugging purposes
|
||||
std::string form_name="idle";
|
||||
std::string sequence_name="";
|
||||
std::string transform_name="";
|
||||
std::string name="";
|
||||
|
||||
std::vector<sf::IntRect> calc_frames();
|
||||
void play();
|
||||
void play_sound();
|
||||
void stop();
|
||||
bool has_form(const std::string& as_form);
|
||||
void set_form(const std::string& form);
|
||||
void apply(sf::Sprite& sprite);
|
||||
void apply(sf::Sprite& sprite, sf::IntRect& rect_io);
|
||||
void apply_effect(std::shared_ptr<sf::Shader> effect);
|
||||
void update();
|
||||
void motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale);
|
||||
void motion(sf::View& view_out, sf::Vector2f pos, sf::Vector2f scale);
|
||||
};
|
||||
|
||||
Animation load(const std::string &file, const std::string &anim_name);
|
||||
|
||||
// BUG: brought over from animation to finish the refactor, but these may not be needed or maybe they go in system.cpp?
|
||||
bool has(const std::string& name);
|
||||
|
||||
void configure(DinkyECS::World& world, DinkyECS::Entity entity);
|
||||
|
||||
void animate_entity(DinkyECS::World &world, DinkyECS::Entity entity);
|
||||
|
||||
ENROLL_COMPONENT(Sheet, frames, frame_width, frame_height);
|
||||
ENROLL_COMPONENT(Sequence, frames, durations);
|
||||
ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y,
|
||||
flipped, scaled, relative, toggled, looped, easing, motion);
|
||||
ENROLL_COMPONENT(Animation, sheet, sequences, transforms, forms, sounds);
|
||||
}
|
||||
132
src/graphics/camera.cpp
Normal file
132
src/graphics/camera.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
#include "graphics/camera.hpp"
|
||||
#include <unordered_map>
|
||||
#include "game/components.hpp"
|
||||
#include "game/config.hpp"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace cinematic {
|
||||
using animation::Animation, std::string, std::min, std::clamp;
|
||||
|
||||
struct CameraManager {
|
||||
std::unordered_map<string, Animation> animations;
|
||||
};
|
||||
|
||||
static CameraManager MGR;
|
||||
static bool initialized = false;
|
||||
|
||||
void init() {
|
||||
if(!initialized) {
|
||||
// BUG: it should be that you give a camera to load by name, not just one for all cameras
|
||||
auto data = settings::get("cameras");
|
||||
|
||||
for(auto [key, value] : data.json().items()) {
|
||||
auto anim = components::convert<Animation>(value);
|
||||
MGR.animations.try_emplace(key, anim);
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
Camera::Camera(sf::Vector2f size, const std::string &name) :
|
||||
anim(MGR.animations.at(name)),
|
||||
size(size),
|
||||
base_size(size),
|
||||
aimed_at{size.x/2, size.y/2},
|
||||
going_to{size.x/2, size.y/2},
|
||||
camera_bounds{{0,0}, size},
|
||||
view{aimed_at, size}
|
||||
{
|
||||
anim.sheet.frame_width = base_size.x;
|
||||
anim.sheet.frame_height = base_size.y;
|
||||
}
|
||||
|
||||
void Camera::update_camera_bounds(sf::Vector2f size) {
|
||||
// camera bounds now constrains the x/y so that the mid-point
|
||||
// of the size won't go too far outside of the frame
|
||||
camera_bounds = {
|
||||
{size.x / 2.0f, size.y / 2.0f},
|
||||
{base_size.x - size.x / 2.0f, base_size.y - size.y / 2.0f}
|
||||
};
|
||||
}
|
||||
|
||||
void Camera::scale(float ratio) {
|
||||
size.x = base_size.x * ratio;
|
||||
size.y = base_size.y * ratio;
|
||||
update_camera_bounds(size);
|
||||
}
|
||||
|
||||
void Camera::resize(float width) {
|
||||
dbc::check(width <= base_size.x, "invalid width for camera");
|
||||
|
||||
size.x = width;
|
||||
size.y = base_size.y * (width / base_size.x);
|
||||
update_camera_bounds(size);
|
||||
}
|
||||
|
||||
void Camera::style(const std::string &name) {
|
||||
anim.set_form(name);
|
||||
}
|
||||
|
||||
void Camera::position(float x, float y) {
|
||||
aimed_at.x = clamp(x, camera_bounds.position.x, camera_bounds.size.x);
|
||||
aimed_at.y = clamp(y, camera_bounds.position.y, camera_bounds.size.y);
|
||||
}
|
||||
|
||||
void Camera::move(float x, float y) {
|
||||
going_to.x = clamp(x, camera_bounds.position.x, camera_bounds.size.x);
|
||||
going_to.y = clamp(y, camera_bounds.position.y, camera_bounds.size.y);
|
||||
|
||||
if(!anim.transform.relative) {
|
||||
anim.transform.min_x = aimed_at.x;
|
||||
anim.transform.min_y = aimed_at.y;
|
||||
anim.transform.max_x = going_to.x;
|
||||
anim.transform.max_y = going_to.y;
|
||||
}
|
||||
}
|
||||
|
||||
void Camera::reset(sf::RenderTexture& target) {
|
||||
size = {base_size.x, base_size.y};
|
||||
aimed_at = {base_size.x/2, base_size.y/2};
|
||||
going_to = {base_size.x/2, base_size.y/2};
|
||||
view = {aimed_at, size};
|
||||
camera_bounds = {{0,0}, base_size};
|
||||
|
||||
// BUG: is getDefaultView different from view?
|
||||
target.setView(target.getDefaultView());
|
||||
}
|
||||
|
||||
void Camera::render(sf::RenderTexture& target) {
|
||||
if(anim.playing) {
|
||||
anim.motion(view, going_to, size);
|
||||
target.setView(view);
|
||||
}
|
||||
}
|
||||
|
||||
void Camera::update() {
|
||||
if(anim.playing) anim.update();
|
||||
}
|
||||
|
||||
bool Camera::playing() {
|
||||
return anim.playing;
|
||||
}
|
||||
|
||||
void Camera::play() {
|
||||
anim.play();
|
||||
}
|
||||
|
||||
void Camera::from_story(components::Storyboard& story) {
|
||||
anim.sequences.clear();
|
||||
anim.forms.clear();
|
||||
|
||||
for(auto& [timecode, cell, transform, duration] : story.beats) {
|
||||
animation::Sequence seq{.frames={0}, .durations={std::stoi(duration)}};
|
||||
anim.sequences.try_emplace(timecode, seq);
|
||||
|
||||
animation::Form form{timecode, transform};
|
||||
anim.forms.try_emplace(timecode, form);
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/graphics/camera.hpp
Normal file
37
src/graphics/camera.hpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
#include "graphics/animation.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <SFML/Graphics/RenderTexture.hpp>
|
||||
|
||||
namespace components {
|
||||
struct Storyboard;
|
||||
}
|
||||
|
||||
namespace cinematic {
|
||||
struct Camera {
|
||||
animation::Animation anim;
|
||||
sf::Vector2f size{SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
sf::Vector2f base_size{SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
sf::Vector2f aimed_at{0,0};
|
||||
sf::Vector2f going_to{0,0};
|
||||
sf::FloatRect camera_bounds{{0,0},{SCREEN_WIDTH, SCREEN_HEIGHT}};
|
||||
sf::View view;
|
||||
|
||||
Camera(sf::Vector2f size, const std::string &name);
|
||||
|
||||
void resize(float width);
|
||||
void scale(float ratio);
|
||||
void position(float x, float y);
|
||||
void move(float x, float y);
|
||||
bool playing();
|
||||
void update();
|
||||
void render(sf::RenderTexture& target);
|
||||
void play();
|
||||
void style(const std::string &name);
|
||||
void reset(sf::RenderTexture& target);
|
||||
void update_camera_bounds(sf::Vector2f size);
|
||||
void from_story(components::Storyboard& story);
|
||||
};
|
||||
|
||||
void init();
|
||||
}
|
||||
141
src/graphics/easing.cpp
Normal file
141
src/graphics/easing.cpp
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
#include "algos/rand.hpp"
|
||||
#include "graphics/animation.hpp"
|
||||
#include <fmt/core.h>
|
||||
#include <unordered_map>
|
||||
#include "dbc.hpp"
|
||||
|
||||
namespace ease2 {
|
||||
using namespace animation;
|
||||
|
||||
double none(float tick) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
double linear(float tick) {
|
||||
return tick;
|
||||
}
|
||||
|
||||
double sine(double x) {
|
||||
// old one? return std::abs(std::sin(seq.subframe * ease_rate));
|
||||
return (std::sin(x) + 1.0) / 2.0;
|
||||
}
|
||||
|
||||
double out_circle(double x) {
|
||||
return std::sqrt(1.0f - ((x - 1.0f) * (x - 1.0f)));
|
||||
}
|
||||
|
||||
double out_bounce(double x) {
|
||||
constexpr const double n1 = 7.5625;
|
||||
constexpr const double d1 = 2.75;
|
||||
|
||||
if (x < 1 / d1) {
|
||||
return n1 * x * x;
|
||||
} else if (x < 2 / d1) {
|
||||
x -= 1.5;
|
||||
return n1 * (x / d1) * x + 0.75;
|
||||
} else if (x < 2.5 / d1) {
|
||||
x -= 2.25;
|
||||
return n1 * (x / d1) * x + 0.9375;
|
||||
} else {
|
||||
x -= 2.625;
|
||||
return n1 * (x / d1) * x + 0.984375;
|
||||
}
|
||||
}
|
||||
|
||||
double in_out_back(double x) {
|
||||
constexpr const double c1 = 1.70158;
|
||||
constexpr const double c2 = c1 * 1.525;
|
||||
|
||||
return x < 0.5
|
||||
? (std::pow(2.0 * x, 2.0) * ((c2 + 1.0) * 2.0 * x - c2)) / 2.0
|
||||
: (std::pow(2.0 * x - 2.0, 2.0) * ((c2 + 1.0) * (x * 2.0 - 2.0) + c2) + 2.0) / 2.0;
|
||||
}
|
||||
|
||||
double random(double tick) {
|
||||
return Random::uniform_real(0.0001f, 1.0f);
|
||||
}
|
||||
|
||||
double normal_dist(double tick) {
|
||||
return Random::normal(0.5f, 0.1f);
|
||||
}
|
||||
|
||||
void move_shake(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
pos_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (pos_out.x * relative);
|
||||
}
|
||||
|
||||
void move_bounce(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
pos_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (pos_out.y * relative);
|
||||
}
|
||||
|
||||
void move_rush(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (scale_out.x * relative);
|
||||
scale_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (scale_out.y * relative);
|
||||
pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y) + (pos_out.y * relative);
|
||||
}
|
||||
|
||||
void scale_squeeze(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (scale_out.x * relative);
|
||||
}
|
||||
|
||||
void scale_squash(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
scale_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (scale_out.y * relative);
|
||||
}
|
||||
|
||||
void scale_stretch(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (scale_out.x * relative);
|
||||
}
|
||||
|
||||
void scale_grow(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
scale_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (scale_out.y * relative);
|
||||
}
|
||||
|
||||
void move_slide(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
pos_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (pos_out.x * relative);
|
||||
pos_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (pos_out.y * relative);
|
||||
}
|
||||
|
||||
void move_none(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
}
|
||||
|
||||
void scale_both(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
|
||||
scale_out.x = std::lerp(scale_out.x * tr.min_x, scale_out.x * tr.max_x, tick) + (scale_out.x * relative);
|
||||
scale_out.y = std::lerp(scale_out.y * tr.min_y, scale_out.y * tr.max_y, tick) + (scale_out.y * relative);
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, EaseFunc> map_of_easings{
|
||||
{"sine", sine},
|
||||
{"out_circle", out_circle},
|
||||
{"out_bounce", out_bounce},
|
||||
{"in_out_back", in_out_back},
|
||||
{"random", random},
|
||||
{"normal_dist", normal_dist},
|
||||
{"none", none},
|
||||
{"linear", linear},
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, MotionFunc> map_of_motions{
|
||||
{"move_bounce", move_bounce},
|
||||
{"move_rush", move_rush},
|
||||
{"scale_squeeze", scale_squeeze},
|
||||
{"scale_squash", scale_squash},
|
||||
{"scale_stretch", scale_stretch},
|
||||
{"scale_grow", scale_grow},
|
||||
{"move_slide", move_slide},
|
||||
{"move_none", move_none},
|
||||
{"scale_both", scale_both},
|
||||
{"move_shake", move_shake},
|
||||
};
|
||||
|
||||
EaseFunc get_easing(const std::string& name) {
|
||||
dbc::check(map_of_easings.contains(name),
|
||||
$F("easing name {} does not exist", name));
|
||||
return map_of_easings.at(name);
|
||||
}
|
||||
|
||||
MotionFunc get_motion(const std::string& name) {
|
||||
dbc::check(map_of_motions.contains(name),
|
||||
$F("motion name {} does not exist", name));
|
||||
return map_of_motions.at(name);
|
||||
}
|
||||
|
||||
}
|
||||
32
src/graphics/easing.hpp
Normal file
32
src/graphics/easing.hpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <functional>
|
||||
#include "graphics/animation.hpp"
|
||||
|
||||
namespace animation {
|
||||
struct Transform;
|
||||
}
|
||||
|
||||
namespace ease2 {
|
||||
using EaseFunc = std::function<double(double)>;
|
||||
using MotionFunc = std::function<void(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative)>;
|
||||
|
||||
EaseFunc get_easing(const std::string& name);
|
||||
MotionFunc get_motion(const std::string& name);
|
||||
|
||||
double sine(double x);
|
||||
double out_circle(double x);
|
||||
double out_bounce(double x);
|
||||
double in_out_back(double x);
|
||||
double random(double tick);
|
||||
double normal_dist(double tick);
|
||||
|
||||
void move_bounce(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
void move_rush(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
void scale_squeeze(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
void scale_squash(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
void scale_stretch(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
void scale_grow(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
void move_slide(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
void move_none(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
void scale_both(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
void move_shake(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
|
||||
}
|
||||
87
src/graphics/lights.cpp
Normal file
87
src/graphics/lights.cpp
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
#include "graphics/lights.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "graphics/textures.hpp"
|
||||
#include <vector>
|
||||
|
||||
using std::vector;
|
||||
|
||||
namespace lighting {
|
||||
|
||||
LightRender::LightRender(Matrix& tiles) :
|
||||
$width(matrix::width(tiles)),
|
||||
$height(matrix::height(tiles)),
|
||||
$lightmap(matrix::make($width, $height)),
|
||||
$ambient(matrix::make($width, $height)),
|
||||
$paths($width, $height),
|
||||
$fow(matrix::make($width, $height))
|
||||
{
|
||||
auto& tile_ambient = textures::get_ambient_light();
|
||||
|
||||
for(matrix::each_cell it{tiles}; it.next();) {
|
||||
size_t tile_id = tiles[it.y][it.x];
|
||||
$ambient[it.y][it.x] = MIN + tile_ambient[tile_id];
|
||||
}
|
||||
}
|
||||
|
||||
void LightRender::render_square_light(LightSource source, Point at, PointList &has_light) {
|
||||
for(matrix::box it{$lightmap, at.x, at.y, (size_t)floor(source.radius)}; it.next();) {
|
||||
if($paths.$paths[it.y][it.x] != WALL_PATH_LIMIT) {
|
||||
$lightmap[it.y][it.x] = light_level(source.strength, it.distance(), it.x, it.y);
|
||||
has_light.emplace_back(it.x, it.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* NOTE: This really doesn't need to calculate light all the time. It doesn't
|
||||
* change around the light source until the lightsource is changed, so the
|
||||
* light levels could be placed in a Matrix inside LightSource, calculated once
|
||||
* and then simply "applied" to the area where the entity is located. The only
|
||||
* thing that would need to be calculated each time is the walls.
|
||||
*/
|
||||
void LightRender::render_light(LightSource source, Point at) {
|
||||
clear_light_target(at);
|
||||
PointList has_light;
|
||||
|
||||
render_square_light(source, at, has_light);
|
||||
|
||||
for(auto point : has_light) {
|
||||
for(matrix::compass it{$lightmap, point.x, point.y}; it.next();) {
|
||||
if($paths.$paths[it.y][it.x] == WALL_PATH_LIMIT) {
|
||||
$lightmap[it.y][it.x] = light_level(source.strength, 1.5f, point.x, point.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int LightRender::light_level(int strength, float distance, size_t x, size_t y) {
|
||||
int boosted = strength + BOOST;
|
||||
int new_level = distance <= 1.0f ? boosted : boosted / sqrt(distance);
|
||||
int cur_level = $lightmap[y][x];
|
||||
return cur_level < new_level ? new_level : cur_level;
|
||||
}
|
||||
|
||||
void LightRender::reset_light() {
|
||||
$lightmap = $ambient;
|
||||
}
|
||||
|
||||
void LightRender::clear_light_target(const Point &at) {
|
||||
$paths.clear_target(at);
|
||||
}
|
||||
|
||||
void LightRender::set_light_target(const Point &at, int value) {
|
||||
$paths.set_target(at, value);
|
||||
}
|
||||
|
||||
void LightRender::update_fow(Point at, LightSource source) {
|
||||
for(matrix::circle it{$lightmap, at, source.radius}; it.next();) {
|
||||
for(auto x = it.left; x < it.right; x++) {
|
||||
$fow[it.y][x] = $lightmap[it.y][x];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LightRender::path_light(Matrix &walls) {
|
||||
$paths.compute_paths(walls);
|
||||
}
|
||||
}
|
||||
40
src/graphics/lights.hpp
Normal file
40
src/graphics/lights.hpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
#include <array>
|
||||
#include "dbc.hpp"
|
||||
#include "algos/point.hpp"
|
||||
#include <algorithm>
|
||||
#include "algos/matrix.hpp"
|
||||
#include "algos/pathing.hpp"
|
||||
#include "game/components.hpp"
|
||||
|
||||
namespace lighting {
|
||||
using components::LightSource;
|
||||
|
||||
// THESE ARE PERCENTAGES!
|
||||
const int MIN = 20;
|
||||
const int BOOST = 10;
|
||||
|
||||
class LightRender {
|
||||
public:
|
||||
size_t $width;
|
||||
size_t $height;
|
||||
Matrix $lightmap;
|
||||
Matrix $ambient;
|
||||
Pathing $paths;
|
||||
matrix::Matrix $fow;
|
||||
|
||||
LightRender(Matrix& walls);
|
||||
|
||||
void reset_light();
|
||||
void set_light_target(const Point &at, int value=0);
|
||||
void clear_light_target(const Point &at);
|
||||
void path_light(Matrix &walls);
|
||||
void light_box(LightSource source, Point from, Point &min_out, Point &max_out);
|
||||
int light_level(int level, float distance, size_t x, size_t y);
|
||||
void render_light(LightSource source, Point at);
|
||||
void render_square_light(LightSource source, Point at, PointList &has_light);
|
||||
void update_fow(Point player_pos, LightSource source);
|
||||
Matrix &lighting() { return $lightmap; }
|
||||
Matrix &paths() { return $paths.paths(); }
|
||||
};
|
||||
}
|
||||
72
src/graphics/palette.cpp
Normal file
72
src/graphics/palette.cpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include <fmt/core.h>
|
||||
#include "graphics/palette.hpp"
|
||||
#include "game/config.hpp"
|
||||
#include "dbc.hpp"
|
||||
|
||||
namespace palette {
|
||||
using std::string;
|
||||
using nlohmann::json;
|
||||
|
||||
struct PaletteMgr {
|
||||
std::unordered_map<string, sf::Color> palettes;
|
||||
std::string config;
|
||||
std::unordered_map<string, string> pending_refs;
|
||||
bool initialized = false;
|
||||
};
|
||||
|
||||
static PaletteMgr COLOR;
|
||||
|
||||
bool initialized() {
|
||||
return COLOR.initialized;
|
||||
}
|
||||
|
||||
void init(const string &json_file) {
|
||||
if(!COLOR.initialized) {
|
||||
COLOR.initialized = true;
|
||||
|
||||
COLOR.config = json_file;
|
||||
auto config = settings::get(json_file);
|
||||
json& colors = config.json();
|
||||
|
||||
for(auto [key, value_specs] : colors.items()) {
|
||||
const string& base_key = key;
|
||||
|
||||
for(auto [value, rgba] : value_specs.items()) {
|
||||
auto color_path = base_key + ":" + value;
|
||||
dbc::check(!COLOR.palettes.contains(color_path),
|
||||
$F("PALLETES config {} already has a color path {}", COLOR.config, color_path));
|
||||
|
||||
if(rgba.type() == json::value_t::string) {
|
||||
COLOR.pending_refs.try_emplace(color_path, rgba);
|
||||
} else {
|
||||
uint8_t alpha = rgba.size() == 3 ? 255 : (uint8_t)rgba[3];
|
||||
sf::Color color{rgba[0], rgba[1], rgba[2], alpha};
|
||||
COLOR.palettes.try_emplace(color_path, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto [color_path, ref] : COLOR.pending_refs) {
|
||||
dbc::check(COLOR.palettes.contains(ref),
|
||||
$F("In {} you have {} referring to {} but {} doesn't exist.",
|
||||
COLOR.config, color_path, ref, ref));
|
||||
dbc::check(!COLOR.palettes.contains(color_path),
|
||||
$F("Color {} with ref {} is duplicated.", color_path, ref));
|
||||
|
||||
auto color = COLOR.palettes.at(ref);
|
||||
|
||||
COLOR.palettes.try_emplace(color_path, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sf::Color get(const string& key) {
|
||||
dbc::check(COLOR.palettes.contains(key),
|
||||
$F("COLOR {} is missing from {}", key, COLOR.config));
|
||||
return COLOR.palettes.at(key);
|
||||
}
|
||||
|
||||
sf::Color get(const string& key, const string& value) {
|
||||
return get(key + ":" + value);
|
||||
}
|
||||
}
|
||||
13
src/graphics/palette.hpp
Normal file
13
src/graphics/palette.hpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <string>
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
|
||||
namespace palette {
|
||||
using std::string;
|
||||
|
||||
bool initialized();
|
||||
void init(const std::string &config="palette");
|
||||
|
||||
sf::Color get(const string &key);
|
||||
|
||||
sf::Color get(const string &key, const string &value);
|
||||
}
|
||||
538
src/graphics/raycaster.cpp
Normal file
538
src/graphics/raycaster.cpp
Normal file
|
|
@ -0,0 +1,538 @@
|
|||
#include "dbc.hpp"
|
||||
#include "game/components.hpp"
|
||||
#include "game/systems.hpp"
|
||||
#include "graphics/animation.hpp"
|
||||
#include "graphics/raycaster.hpp"
|
||||
#include "graphics/shaders.hpp"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <fmt/core.h>
|
||||
#include <memory>
|
||||
#include <numbers>
|
||||
|
||||
using std::make_unique, std::shared_ptr;
|
||||
|
||||
union ColorConv {
|
||||
struct {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
uint8_t a;
|
||||
} as_color;
|
||||
RGBA as_int;
|
||||
};
|
||||
|
||||
// from: https://permadi.com/1996/05/ray-casting-tutorial-19/
|
||||
// Intensity = (kI/(d+do))*(N*L)
|
||||
// rcr says: kI = intensity coefficient, d = distance, d0 = fudge term to prevent division by zero, N is surface, L is direction to light from surface
|
||||
//
|
||||
// That formula is just "Inverse-square law" (except they don't square, which is physically dubious), and "Lambertian reflectance" ("Diffuse reflection") which sounds fancy but is super standard. All the quoted terms have wikipedia articles
|
||||
//
|
||||
// Distance means distance to surface from light.
|
||||
//
|
||||
// Intensity = Object Intensity/Distance * Multiplier
|
||||
//
|
||||
/* It's hard to believe, but this is faster than any bitfiddling
|
||||
* I could devise. Just use a union with a struct, do the math
|
||||
* and I guess the compiler can handle it better than shifting
|
||||
* bits around.
|
||||
*/
|
||||
inline RGBA lighting_calc(RGBA pixel, float dist, int level) {
|
||||
ColorConv conv{.as_int=pixel};
|
||||
|
||||
if(conv.as_color.b < GLOW_LIMIT
|
||||
&& conv.as_color.r < GLOW_LIMIT
|
||||
&& conv.as_color.g < GLOW_LIMIT)
|
||||
{
|
||||
float intensity = (float(level) * PERCENT) / (dist + 1) * LIGHT_MULTIPLIER;
|
||||
|
||||
conv.as_color.r *= intensity;
|
||||
conv.as_color.g *= intensity;
|
||||
conv.as_color.b *= intensity;
|
||||
}
|
||||
|
||||
return conv.as_int;
|
||||
}
|
||||
|
||||
Raycaster::Raycaster(int x, int y, int width, int height) :
|
||||
$view_texture(sf::Vector2u{(unsigned int)width, (unsigned int)height}),
|
||||
$view_sprite($view_texture),
|
||||
$width(width), $height(height),
|
||||
$screen_pos_x(x),
|
||||
$screen_pos_y(y),
|
||||
$zbuffer(width)
|
||||
{
|
||||
$view_sprite.setPosition({float($screen_pos_x), float($screen_pos_y)});
|
||||
$pixels = make_unique<RGBA[]>($width * $height);
|
||||
$view_texture.setSmooth(false);
|
||||
|
||||
$camera.target_x = $pos_x;
|
||||
$camera.target_y = $pos_y;
|
||||
update_camera_aiming();
|
||||
}
|
||||
|
||||
void Raycaster::position_camera(float player_x, float player_y) {
|
||||
// x and y start position
|
||||
$pos_x = player_x;
|
||||
$pos_y = player_y;
|
||||
$dir_x = 1;
|
||||
$dir_y = 0;
|
||||
$plane_x = 0;
|
||||
$plane_y = 0.66;
|
||||
|
||||
update_camera_aiming();
|
||||
}
|
||||
|
||||
void Raycaster::draw_pixel_buffer() {
|
||||
$view_texture.update((uint8_t *)$pixels.get(), {(unsigned int)$width, (unsigned int)$height}, {0, 0});
|
||||
}
|
||||
|
||||
void Raycaster::apply_sprite_effect(shared_ptr<sf::Shader> effect, float width, float height) {
|
||||
// BUG: should I use the clock in the animation?
|
||||
effect->setUniform("u_time", $clock.getElapsedTime().asSeconds());
|
||||
sf::Vector2f u_resolution{width, height};
|
||||
effect->setUniform("u_resolution", u_resolution);
|
||||
}
|
||||
|
||||
std::shared_ptr<sf::Shader> Raycaster::apply_lighting_effect(components::Position& sprite_pos, matrix::Matrix &lights) {
|
||||
// BUG: this is applying it to all sprites, put bottle far away then another close to see
|
||||
auto effect = $brightness;
|
||||
float level = lights[sprite_pos.location.y][sprite_pos.location.x] * PERCENT;
|
||||
// this boosts the brightness of anything we're aiming at
|
||||
level += (aiming_at == sprite_pos.location) * AIMED_AT_BRIGHTNESS;
|
||||
effect->setUniform("darkness", level);
|
||||
return effect;
|
||||
}
|
||||
|
||||
inline void step_animation(animation::Animation& anim, sf::Sprite& sprite, sf::Vector2f& position, sf::Vector2f& scale, sf::IntRect& in_texture, sf::Vector2f& origin) {
|
||||
anim.update();
|
||||
anim.apply(sprite, in_texture);
|
||||
anim.motion(sprite, position, scale);
|
||||
sprite.setOrigin(origin);
|
||||
sprite.setTextureRect(in_texture);
|
||||
}
|
||||
|
||||
inline void set_scale_position(sf::Sprite& sprite, sf::Vector2f& position, sf::Vector2f& scale, sf::IntRect& in_texture, sf::Vector2f& origin) {
|
||||
sprite.setScale(scale);
|
||||
sprite.setPosition(position);
|
||||
sprite.setOrigin(origin);
|
||||
sprite.setTextureRect(in_texture);
|
||||
}
|
||||
|
||||
|
||||
void Raycaster::sprite_casting() {
|
||||
auto& lights = $level.lights->lighting();
|
||||
auto world = $level.world;
|
||||
$level.collision->distance_sorted($sprite_order, {(size_t)$pos_x, (size_t)$pos_y}, RENDER_DISTANCE);
|
||||
$sprites_to_render.clear();
|
||||
|
||||
// BUG: shaders are shared between sprites
|
||||
// BUG: sprites seem to be shared too? Put a torch on the floor in a room then another to its left and only one is visible at a time.
|
||||
// after sorting the sprites, do the projection
|
||||
for(auto& rec : $sprite_order) {
|
||||
if(!$sprites.contains(rec.entity)) continue;
|
||||
|
||||
auto& sprite_texture = $sprites.at(rec.entity);
|
||||
|
||||
int texture_width = float(sprite_texture.frame_size.x);
|
||||
int texture_height = float(sprite_texture.frame_size.y);
|
||||
int half_height = texture_height / 2;
|
||||
|
||||
auto sf_sprite = sprite_texture.sprite;
|
||||
auto sprite_pos = world->get<components::Position>(rec.entity);
|
||||
|
||||
double sprite_x = double(sprite_pos.location.x) - rec.wiggle - $pos_x + 0.5;
|
||||
double sprite_y = double(sprite_pos.location.y) - rec.wiggle - $pos_y + 0.5;
|
||||
|
||||
double inv_det = 1.0 / ($plane_x * $dir_y - $dir_x * $plane_y); // required for correct matrix multiplication
|
||||
|
||||
double transform_x = inv_det * ($dir_y * sprite_x - $dir_x * sprite_y);
|
||||
|
||||
//this is actually the depth inside the screen, that what Z is in 3D, the distance of sprite to player, matching sqrt(spriteDistance[i])
|
||||
double transform_y = inv_det * (-$plane_y * sprite_x + $plane_x * sprite_y);
|
||||
|
||||
int sprite_screen_x = int(($width / 2) * (1 + transform_x / transform_y));
|
||||
|
||||
// calculate the height of the sprite on screen
|
||||
//using "transform_y" instead of the real distance prevents fisheye
|
||||
int sprite_height = abs(int($height / transform_y));
|
||||
if(sprite_height == 0) continue;
|
||||
|
||||
// calculate width the the sprite
|
||||
// same as height of sprite, given that it's square
|
||||
int sprite_width = abs(int($height / transform_y));
|
||||
if(sprite_width == 0) continue;
|
||||
|
||||
int draw_start_x = -sprite_width / 2 + sprite_screen_x;
|
||||
if(draw_start_x < 0) draw_start_x = 0;
|
||||
int draw_end_x = sprite_width / 2 + sprite_screen_x;
|
||||
if(draw_end_x > $width) draw_end_x = $width;
|
||||
|
||||
int stripe = draw_start_x;
|
||||
for(; stripe < draw_end_x; stripe++) {
|
||||
//the conditions in the if are:
|
||||
//1) it's in front of camera plane so you don't see things behind you
|
||||
//2) $zbuffer, with perpendicular distance
|
||||
if(!(transform_y > 0 && transform_y < $zbuffer[stripe])) break;
|
||||
}
|
||||
|
||||
int tex_x_end = int(texture_width * (stripe - (-sprite_width / 2 + sprite_screen_x)) * texture_width / sprite_width) / texture_width;
|
||||
|
||||
if(draw_start_x < draw_end_x && transform_y > 0 && transform_y < $zbuffer[draw_start_x]) {
|
||||
//calculate lowest and highest pixel to fill in current stripe
|
||||
int draw_start_y = -sprite_height / 2 + $height / 2;
|
||||
if(draw_start_y < 0) draw_start_y = 0;
|
||||
|
||||
int tex_x = int(texture_width * (draw_start_x - (-sprite_width / 2 + sprite_screen_x)) * texture_width / sprite_width) / texture_width;
|
||||
int tex_render_width = tex_x_end - tex_x;
|
||||
|
||||
// avoid drawing sprites that are not visible (width < 0)
|
||||
if(tex_render_width <= 0) continue;
|
||||
|
||||
float x = float(draw_start_x + $screen_pos_x);
|
||||
float y = float(draw_start_y + $screen_pos_y);
|
||||
|
||||
if(x < $screen_pos_x) dbc::log("X < rayview left bounds");
|
||||
if(y < $screen_pos_y) dbc::log("Y < rayview top bounds");
|
||||
if(x >= SCREEN_WIDTH) dbc::log("OUT OF BOUNDS X");
|
||||
if(y >= $height) dbc::log("OUT OF BOUNDS Y");
|
||||
|
||||
float sprite_scale_w = float(sprite_width) / float(texture_width);
|
||||
float sprite_scale_h = float(sprite_height) / float(texture_height);
|
||||
|
||||
int d = y * texture_height - $height * half_height + sprite_height * half_height;
|
||||
int tex_y = ((d * texture_height) / sprite_height) / texture_height;
|
||||
|
||||
// BUG: this data could be put into the world
|
||||
// as frame data, then just have a system that
|
||||
// constantly applies this to any sprite that
|
||||
// has an animation and is visible
|
||||
sf::Vector2f origin{texture_width / 2.0f, texture_height / 2.0f};
|
||||
sf::Vector2f scale{sprite_scale_w, sprite_scale_h};
|
||||
sf::Vector2f position{x + origin.x * scale.x, y + origin.y * scale.y};
|
||||
sf::IntRect in_texture{ {tex_x, tex_y}, {tex_render_width, texture_height}};
|
||||
|
||||
shared_ptr<sf::Shader> effect = System::sprite_effect(rec.entity);
|
||||
|
||||
if(effect) {
|
||||
// has an effect, use that instead of lighting
|
||||
apply_sprite_effect(effect, sprite_width, sprite_height);
|
||||
} else {
|
||||
// no effect applied in the system so apply lighting
|
||||
effect = apply_lighting_effect(sprite_pos, lights);
|
||||
}
|
||||
|
||||
auto anim = world->get_if<animation::Animation>(rec.entity);
|
||||
|
||||
if(anim != nullptr && anim->playing) {
|
||||
step_animation(*anim, *sf_sprite, position, scale, in_texture, origin);
|
||||
} else {
|
||||
set_scale_position(*sf_sprite, position, scale, in_texture, origin);
|
||||
}
|
||||
|
||||
$sprites_to_render.emplace_back(sf_sprite, effect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Raycaster::cast_rays() {
|
||||
constexpr static const int texture_width = TEXTURE_WIDTH;
|
||||
constexpr static const int texture_height = TEXTURE_HEIGHT;
|
||||
double perp_wall_dist;
|
||||
auto& lights = $level.lights->lighting();
|
||||
|
||||
// WALL CASTING
|
||||
for(int x = 0; x < $width; x++) {
|
||||
// calculate ray position and direction
|
||||
double cameraX = 2 * x / double($width) - 1; // x-coord in camera space
|
||||
double ray_dir_x = $dir_x + $plane_x * cameraX;
|
||||
double ray_dir_y = $dir_y + $plane_y * cameraX;
|
||||
|
||||
// which box of the map we're in
|
||||
int map_x = int($pos_x);
|
||||
int map_y = int($pos_y);
|
||||
|
||||
// length of ray from one x or y-side to next x or y-side
|
||||
double delta_dist_x = std::abs(1.0 / ray_dir_x);
|
||||
double delta_dist_y = std::abs(1.0 / ray_dir_y);
|
||||
|
||||
int step_x = 0;
|
||||
int step_y = 0;
|
||||
int hit = 0;
|
||||
int side = 0;
|
||||
|
||||
// length of ray from current pos to next x or y-side
|
||||
double side_dist_x;
|
||||
double side_dist_y;
|
||||
|
||||
if(ray_dir_x < 0) {
|
||||
step_x = -1;
|
||||
side_dist_x = ($pos_x - map_x) * delta_dist_x;
|
||||
} else {
|
||||
step_x = 1;
|
||||
side_dist_x = (map_x + 1.0 - $pos_x) * delta_dist_x;
|
||||
}
|
||||
|
||||
if(ray_dir_y < 0) {
|
||||
step_y = -1;
|
||||
side_dist_y = ($pos_y - map_y) * delta_dist_y;
|
||||
} else {
|
||||
step_y = 1;
|
||||
side_dist_y = (map_y + 1.0 - $pos_y) * delta_dist_y;
|
||||
}
|
||||
|
||||
// perform DDA
|
||||
while(hit == 0) {
|
||||
if(side_dist_x < side_dist_y) {
|
||||
side_dist_x += delta_dist_x;
|
||||
map_x += step_x;
|
||||
side = 0;
|
||||
} else {
|
||||
side_dist_y += delta_dist_y;
|
||||
map_y += step_y;
|
||||
side = 1;
|
||||
}
|
||||
|
||||
if($walls[map_y][map_x] == 1) hit = 1;
|
||||
}
|
||||
|
||||
if(side == 0) {
|
||||
perp_wall_dist = (side_dist_x - delta_dist_x);
|
||||
} else {
|
||||
perp_wall_dist = (side_dist_y - delta_dist_y);
|
||||
}
|
||||
|
||||
int line_height = int($height / perp_wall_dist);
|
||||
|
||||
int draw_start = -line_height / 2 + $height / 2 + $pitch;
|
||||
if(draw_start < 0) draw_start = 0;
|
||||
|
||||
int draw_end = line_height / 2 + $height / 2 + $pitch;
|
||||
if(draw_end >= $height) draw_end = $height - 1;
|
||||
|
||||
// BUG: I thought I got rid of this
|
||||
auto texture = textures::get_surface($tiles[map_y][map_x]);
|
||||
|
||||
// calculate value of wall_x
|
||||
double wall_x; // where exactly the wall was hit
|
||||
if(side == 0) {
|
||||
wall_x = $pos_y + perp_wall_dist * ray_dir_y;
|
||||
} else {
|
||||
wall_x = $pos_x + perp_wall_dist * ray_dir_x;
|
||||
}
|
||||
wall_x -= floor(wall_x);
|
||||
|
||||
// x coorindate on the texture
|
||||
int tex_x = int(wall_x * double(texture_width));
|
||||
if(side == 0 && ray_dir_x > 0) tex_x = texture_width - tex_x - 1;
|
||||
if(side == 1 && ray_dir_y < 0) tex_x = texture_width - tex_x - 1;
|
||||
|
||||
// LODE: an integer-only bresenham or DDA like algorithm could make the texture coordinate stepping faster
|
||||
|
||||
// How much to increase the texture coordinate per screen pixel
|
||||
double step = 1.0 * texture_height / line_height;
|
||||
// Starting texture coordinate
|
||||
double tex_pos = (draw_start - $pitch - $height / 2 + line_height / 2) * step;
|
||||
|
||||
for(int y = draw_start; y < draw_end; y++) {
|
||||
int tex_y = (int)tex_pos & (texture_height - 1);
|
||||
tex_pos += step;
|
||||
RGBA pixel = texture[texture_height * tex_y + tex_x];
|
||||
int light_level = lights[map_y][map_x];
|
||||
$pixels[pixcoord(x, y)] = lighting_calc(pixel, perp_wall_dist, light_level);
|
||||
}
|
||||
|
||||
// SET THE ZBUFFER FOR THE SPRITE CASTING
|
||||
$zbuffer[x] = perp_wall_dist;
|
||||
}
|
||||
}
|
||||
|
||||
void Raycaster::draw_ceiling_floor() {
|
||||
// BUG: this should come from the texture's config
|
||||
constexpr const int texture_width = TEXTURE_WIDTH;
|
||||
constexpr const int texture_height = TEXTURE_HEIGHT;
|
||||
|
||||
auto &lights = $level.lights->lighting();
|
||||
size_t surface_i = 0;
|
||||
const RGBA *floor_texture = textures::get_surface(surface_i);
|
||||
const RGBA *ceiling_texture = textures::get_ceiling(surface_i);
|
||||
|
||||
for(int y = $height / 2 + 1; y < $height; ++y) {
|
||||
// rayDir for leftmost ray (x=0) and rightmost (x = w)
|
||||
float ray_dir_x0 = $dir_x - $plane_x;
|
||||
float ray_dir_y0 = $dir_y - $plane_y;
|
||||
float ray_dir_x1 = $dir_x + $plane_x;
|
||||
float ray_dir_y1 = $dir_y + $plane_y;
|
||||
|
||||
// current y position compared to the horizon
|
||||
int p = y - $height / 2;
|
||||
|
||||
// vertical position of the camera
|
||||
// 0.5 will the camera at the center horizon. For a
|
||||
// different value you need a separate loop for ceiling
|
||||
// and floor since they're no longer symmetrical.
|
||||
float pos_z = 0.5 * $height;
|
||||
|
||||
// horizontal distance from the camera to the floor for the current row
|
||||
// 0.5 is the z position exactly in the middle between floor and ceiling
|
||||
// See NOTE in Lode's code for more.
|
||||
float row_distance = pos_z / p;
|
||||
|
||||
// calculate the real world step vector we have to add for each x (parallel to camera plane)
|
||||
// adding step by step avoids multiplications with a wight in the inner loop
|
||||
float floor_step_x = row_distance * (ray_dir_x1 - ray_dir_x0) / $width;
|
||||
float floor_step_y = row_distance * (ray_dir_y1 - ray_dir_y0) / $width;
|
||||
|
||||
// real world coordinates of the leftmost column.
|
||||
// This will be updated as we step to the right
|
||||
float floor_x = $pos_x + row_distance * ray_dir_x0;
|
||||
float floor_y = $pos_y + row_distance * ray_dir_y0;
|
||||
|
||||
for(int x = 0; x < $width; ++x) {
|
||||
// the cell coord is simply taken from the int parts of
|
||||
// floor_x and floor_y.
|
||||
int cell_x = int(floor_x);
|
||||
int cell_y = int(floor_y);
|
||||
|
||||
// get the texture coordinate from the fractional part
|
||||
int tx = int(texture_width * (floor_x - cell_x)) & (texture_width - 1);
|
||||
int ty = int(texture_width * (floor_y - cell_y)) & (texture_height - 1);
|
||||
|
||||
floor_x += floor_step_x;
|
||||
floor_y += floor_step_y;
|
||||
|
||||
// now get the pixel from the texture
|
||||
RGBA color;
|
||||
// this uses the previous ty/tx fractional parts of
|
||||
// floor_x cell_x to find the texture x/y. How?
|
||||
int map_x = int(floor_x);
|
||||
int map_y = int(floor_y);
|
||||
|
||||
if(!matrix::inbounds(lights, map_x, map_y)) continue;
|
||||
|
||||
int light_level = lights[map_y][map_x];
|
||||
size_t new_surface_i = $tiles[map_y][map_x];
|
||||
|
||||
if(new_surface_i != surface_i) {
|
||||
surface_i = new_surface_i;
|
||||
floor_texture = textures::get_surface(surface_i);
|
||||
ceiling_texture = textures::get_ceiling(surface_i);
|
||||
}
|
||||
|
||||
// NOTE: use map_x/y to get the floor, ceiling texture.
|
||||
|
||||
// FLOOR
|
||||
color = floor_texture[texture_width * ty + tx];
|
||||
$pixels[pixcoord(x, y)] = lighting_calc(color, row_distance, light_level);
|
||||
|
||||
// CEILING
|
||||
color = ceiling_texture[texture_width * ty + tx];
|
||||
$pixels[pixcoord(x, $height - y - 1)] = lighting_calc(color, row_distance, light_level);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Raycaster::update() {
|
||||
draw_ceiling_floor();
|
||||
cast_rays();
|
||||
draw_pixel_buffer();
|
||||
sprite_casting();
|
||||
}
|
||||
|
||||
// BUG: if target is a rendertarget then I can say it has to be the viewport only, then I can get rid of $screen_pos_x/y
|
||||
void Raycaster::render(sf::RenderTarget& target) {
|
||||
target.draw($view_sprite);
|
||||
|
||||
for(auto [sprite, effect] : $sprites_to_render) {
|
||||
target.draw(*sprite, effect.get());
|
||||
}
|
||||
}
|
||||
|
||||
void Raycaster::update_sprite(DinkyECS::Entity ent, components::Sprite& sprite) {
|
||||
auto sprite_txt = textures::get_sprite(sprite.name);
|
||||
$sprites.insert_or_assign(ent, sprite_txt);
|
||||
}
|
||||
|
||||
void Raycaster::update_level(GameDB::Level& level) {
|
||||
$sprites.clear();
|
||||
$sprite_order.clear();
|
||||
|
||||
$level = level;
|
||||
|
||||
$tiles = $level.map->tiles();
|
||||
$walls = $level.map->walls();
|
||||
|
||||
$level.world->query<components::Sprite>([&](const auto ent, auto& sprite) {
|
||||
// player doesn't need a sprite
|
||||
if($level.player != ent) {
|
||||
update_sprite(ent, sprite);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void Raycaster::init_shaders() {
|
||||
$brightness = shaders::get("rayview_sprites");
|
||||
}
|
||||
|
||||
|
||||
Point Raycaster::plan_move(int dir, bool strafe) {
|
||||
$camera.t = 0.0;
|
||||
if(strafe) {
|
||||
$camera.target_x = $pos_x + int(-$dir_y * 1.5 * dir);
|
||||
$camera.target_y = $pos_y + int($dir_x * 1.5 * dir);
|
||||
} else {
|
||||
$camera.target_x = $pos_x + int($dir_x * 1.5 * dir);
|
||||
$camera.target_y = $pos_y + int($dir_y * 1.5 * dir);
|
||||
}
|
||||
|
||||
return {size_t($camera.target_x), size_t($camera.target_y)};
|
||||
}
|
||||
|
||||
void Raycaster::plan_rotate(int dir, float amount) {
|
||||
$camera.t = 0.0;
|
||||
double angle_dir = std::numbers::pi * amount * float(dir);
|
||||
|
||||
$camera.target_dir_x = $dir_x * cos(angle_dir) - $dir_y * sin(angle_dir);
|
||||
$camera.target_dir_y = $dir_x * sin(angle_dir) + $dir_y * cos(angle_dir);
|
||||
|
||||
$camera.target_plane_x = $plane_x * cos(angle_dir) - $plane_y * sin(angle_dir);
|
||||
$camera.target_plane_y = $plane_x * sin(angle_dir) + $plane_y * cos(angle_dir);
|
||||
}
|
||||
|
||||
bool Raycaster::play_rotate() {
|
||||
$camera.t += $camera.rot_speed;
|
||||
$dir_x = std::lerp($dir_x, $camera.target_dir_x, $camera.t);
|
||||
$dir_y = std::lerp($dir_y, $camera.target_dir_y, $camera.t);
|
||||
$plane_x = std::lerp($plane_x, $camera.target_plane_x, $camera.t);
|
||||
$plane_y = std::lerp($plane_y, $camera.target_plane_y, $camera.t);
|
||||
update_camera_aiming();
|
||||
|
||||
return $camera.t >= 1.0;
|
||||
}
|
||||
|
||||
bool Raycaster::play_move() {
|
||||
$camera.t += $camera.move_speed;
|
||||
$pos_x = std::lerp($pos_x, $camera.target_x, $camera.t);
|
||||
$pos_y = std::lerp($pos_y, $camera.target_y, $camera.t);
|
||||
update_camera_aiming();
|
||||
|
||||
return $camera.t >= 1.0;
|
||||
}
|
||||
|
||||
void Raycaster::abort_plan() {
|
||||
$camera.target_x = $pos_x;
|
||||
$camera.target_y = $pos_y;
|
||||
update_camera_aiming();
|
||||
}
|
||||
|
||||
bool Raycaster::is_target(DinkyECS::Entity entity) {
|
||||
(void)entity;
|
||||
return false;
|
||||
}
|
||||
|
||||
void Raycaster::update_camera_aiming() {
|
||||
aiming_at = { size_t($pos_x + $dir_x), size_t($pos_y + $dir_y) };
|
||||
camera_at = { size_t($camera.target_x), size_t($camera.target_y) };
|
||||
}
|
||||
96
src/graphics/raycaster.hpp
Normal file
96
src/graphics/raycaster.hpp
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
#pragma once
|
||||
|
||||
#include "algos/matrix.hpp"
|
||||
#include "algos/spatialmap.hpp"
|
||||
#include "game/level.hpp"
|
||||
#include "graphics/textures.hpp"
|
||||
#include <SFML/System/Clock.hpp>
|
||||
|
||||
using matrix::Matrix;
|
||||
using RGBA = uint32_t;
|
||||
|
||||
struct CameraLOL {
|
||||
double t = 0.0;
|
||||
double move_speed = 0.1;
|
||||
double rot_speed = 0.06;
|
||||
double target_x = 0.0;
|
||||
double target_y = 0.0;
|
||||
double target_dir_x = 0.0;
|
||||
double target_dir_y = 0.0;
|
||||
double target_plane_x = 0.0;
|
||||
double target_plane_y = 0.0;
|
||||
};
|
||||
|
||||
using SpriteRender = std::pair<std::shared_ptr<sf::Sprite>, std::shared_ptr<sf::Shader>>;
|
||||
|
||||
struct Raycaster {
|
||||
sf::Texture $view_texture;
|
||||
sf::Sprite $view_sprite;
|
||||
int $width;
|
||||
int $height;
|
||||
int $screen_pos_x;
|
||||
int $screen_pos_y;
|
||||
std::vector<double> $zbuffer; // width
|
||||
|
||||
int $pitch=0;
|
||||
sf::Clock $clock;
|
||||
std::shared_ptr<sf::Shader> $brightness = nullptr;
|
||||
double $pos_x = 0;
|
||||
double $pos_y = 0;
|
||||
|
||||
// initial direction vector
|
||||
double $dir_x = 1;
|
||||
double $dir_y = 0;
|
||||
|
||||
// the 2d raycaster version of camera plane
|
||||
double $plane_x = 0.0;
|
||||
double $plane_y = 0.66;
|
||||
Point aiming_at{0,0};
|
||||
Point camera_at{0,0};
|
||||
CameraLOL $camera;
|
||||
std::unique_ptr<RGBA[]> $pixels = nullptr;
|
||||
|
||||
std::unordered_map<DinkyECS::Entity, textures::SpriteTexture> $sprites;
|
||||
// BUG: this can be way better I think
|
||||
std::vector<SpriteRender> $sprites_to_render;
|
||||
SortedEntities $sprite_order;
|
||||
|
||||
GameDB::Level $level;
|
||||
Matrix $tiles;
|
||||
Matrix $walls;
|
||||
|
||||
Raycaster(int x, int y, int width, int height);
|
||||
|
||||
void cast_rays();
|
||||
void draw_ceiling_floor();
|
||||
void draw_pixel_buffer();
|
||||
void sprite_casting();
|
||||
void update();
|
||||
void render(sf::RenderTarget& target);
|
||||
|
||||
void sort_sprites(std::vector<int>& order, std::vector<double>& dist, int amount);
|
||||
void set_position(int x, int y);
|
||||
|
||||
inline size_t pixcoord(int x, int y) {
|
||||
return ((y) * $width) + (x);
|
||||
}
|
||||
|
||||
void update_level(GameDB::Level& level);
|
||||
void update_sprite(DinkyECS::Entity ent, components::Sprite& sprite);
|
||||
void init_shaders();
|
||||
|
||||
void position_camera(float player_x, float player_y);
|
||||
Point plan_move(int dir, bool strafe);
|
||||
void plan_rotate(int dir, float amount);
|
||||
|
||||
bool play_rotate();
|
||||
bool play_move();
|
||||
|
||||
void abort_plan();
|
||||
bool is_target(DinkyECS::Entity entity);
|
||||
void update_camera_aiming();
|
||||
|
||||
// BUG: these should go away when Bug #42 is solved
|
||||
void apply_sprite_effect(std::shared_ptr<sf::Shader> effect, float width, float height);
|
||||
std::shared_ptr<sf::Shader> apply_lighting_effect(components::Position& sprite_pos, matrix::Matrix &lights);
|
||||
};
|
||||
189
src/graphics/scene.cpp
Normal file
189
src/graphics/scene.cpp
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
#include "graphics/scene.hpp"
|
||||
#include "graphics/animation.hpp"
|
||||
#include "graphics/shaders.hpp"
|
||||
#include <fmt/core.h>
|
||||
#include "dbc.hpp"
|
||||
|
||||
const bool DEBUG=false;
|
||||
|
||||
namespace scene {
|
||||
Element Engine::config_scene_element(nlohmann::json& config, bool duped) {
|
||||
std::string sprite_name = config["sprite"];
|
||||
auto st = textures::get_sprite(sprite_name, duped);
|
||||
|
||||
float scale_x = config["scale_x"];
|
||||
float scale_y = config["scale_y"];
|
||||
float x = config["x"];
|
||||
float y = config["y"];
|
||||
bool flipped = config["flipped"];
|
||||
|
||||
// BUG: put the .json file to load as a default/optional arg
|
||||
auto anim = animation::load("./assets/animation.json", sprite_name);
|
||||
anim.play();
|
||||
|
||||
anim.transform.flipped = flipped;
|
||||
|
||||
std::string cell = config["cell"];
|
||||
std::string name = config["name"];
|
||||
|
||||
bool at_mid = config["at_mid"];
|
||||
|
||||
sf::Text text(*$ui.$font, "", 60);
|
||||
|
||||
return {name, st, anim, cell, {scale_x, scale_y}, {x, y}, at_mid, flipped, nullptr, text};
|
||||
}
|
||||
|
||||
Engine::Engine(components::AnimatedScene& scene) :
|
||||
$scene(scene)
|
||||
{
|
||||
for(auto& config : $scene.actors) {
|
||||
auto element = config_scene_element(config, false);
|
||||
dbc::check(!$actor_name_ids.contains(element.name),
|
||||
$F("actors key {} already exists", element.name));
|
||||
$actors.push_back(element);
|
||||
$actor_name_ids.try_emplace(element.name, $actors.size() - 1);
|
||||
}
|
||||
|
||||
for(auto& fixture : $scene.fixtures) {
|
||||
auto element = config_scene_element(fixture, true);
|
||||
$fixtures.push_back(element);
|
||||
}
|
||||
|
||||
for(auto& line : $scene.layout) {
|
||||
$layout.append(line);
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::init() {
|
||||
$ui.position(0,0, BOSS_VIEW_WIDTH, BOSS_VIEW_HEIGHT);
|
||||
$ui.set<guecs::Background>($ui.MAIN, {$ui.$parser, guecs::THEME.TRANSPARENT});
|
||||
auto& background = $ui.get<guecs::Background>($ui.MAIN);
|
||||
background.set_sprite($scene.background, true);
|
||||
|
||||
$ui.layout($layout);
|
||||
|
||||
for(auto& actor : $actors) {
|
||||
actor.pos = position_sprite(actor.st, actor.cell,
|
||||
actor.scale, actor.at_mid, actor.pos.x, actor.pos.y);
|
||||
}
|
||||
|
||||
for(auto& fixture : $fixtures) {
|
||||
fixture.pos = position_sprite(fixture.st, fixture.cell,
|
||||
fixture.scale, fixture.at_mid, fixture.pos.x, fixture.pos.y);
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::apply_effect(const std::string& actor, const std::string& shader) {
|
||||
auto& element = actor_config(actor);
|
||||
element.effect = shaders::get(shader);
|
||||
}
|
||||
|
||||
void Engine::attach_text(const std::string& actor, const std::string& text) {
|
||||
auto& element = actor_config(actor);
|
||||
element.text.setPosition(element.pos);
|
||||
element.text.setScale(element.scale);
|
||||
element.text.setFillColor(sf::Color::Red);
|
||||
element.text.setOutlineThickness(2.0f);
|
||||
element.text.setOutlineColor(sf::Color::Black);
|
||||
element.text.setString(text);
|
||||
}
|
||||
|
||||
bool Engine::mouse(float x, float y, guecs::Modifiers mods) {
|
||||
return $ui.mouse(x, y, mods);
|
||||
}
|
||||
|
||||
void Engine::render(sf::RenderTexture& view) {
|
||||
$ui.render(view);
|
||||
|
||||
for(auto& fixture : $fixtures) {
|
||||
view.draw(*fixture.st.sprite, fixture.effect.get());
|
||||
}
|
||||
|
||||
for(auto& actor : $actors) {
|
||||
view.draw(*actor.st.sprite, actor.effect.get());
|
||||
if(actor.anim.playing) view.draw(actor.text);
|
||||
}
|
||||
|
||||
$camera.render(view);
|
||||
if(DEBUG) $ui.debug_layout(view);
|
||||
}
|
||||
|
||||
Element& Engine::actor_config(const std::string& actor) {
|
||||
dbc::check($actor_name_ids.contains(actor), $F("scene does not contain actor {}", actor));
|
||||
return $actors.at($actor_name_ids.at(actor));
|
||||
}
|
||||
|
||||
void Engine::move_actor(const std::string& actor, const std::string& cell_name) {
|
||||
auto& config = actor_config(actor);
|
||||
config.cell = cell_name;
|
||||
config.pos = position_sprite(config.st, config.cell, config.scale, config.at_mid);
|
||||
}
|
||||
|
||||
void Engine::animate_actor(const std::string& actor, const std::string& form) {
|
||||
auto& config = actor_config(actor);
|
||||
config.anim.set_form(form);
|
||||
|
||||
if(!config.anim.playing) {
|
||||
config.anim.play();
|
||||
}
|
||||
}
|
||||
|
||||
inline void this_is_stupid_refactor(std::vector<Element>& elements) {
|
||||
for(auto& element : elements) {
|
||||
if(element.anim.playing) {
|
||||
element.anim.update();
|
||||
element.anim.motion(*element.st.sprite, element.pos, element.scale);
|
||||
element.anim.apply(*element.st.sprite);
|
||||
if(element.effect != nullptr) element.anim.apply_effect(element.effect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::update() {
|
||||
this_is_stupid_refactor($fixtures);
|
||||
this_is_stupid_refactor($actors);
|
||||
}
|
||||
|
||||
sf::Vector2f Engine::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, sf::Vector2f scale, bool at_mid, float x_diff, float y_diff) {
|
||||
auto& cell = $ui.cell_for(cell_name);
|
||||
float x = float(at_mid ? cell.mid_x : cell.x);
|
||||
float y = float(at_mid ? cell.mid_y : cell.y);
|
||||
|
||||
sf::Vector2f pos{x + x_diff, y + y_diff};
|
||||
st.sprite->setPosition(pos);
|
||||
st.sprite->setScale(scale);
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
void Engine::zoom(float mid_x, float mid_y, const std::string& style, float scale) {
|
||||
$camera.style(style);
|
||||
$camera.scale(scale);
|
||||
$camera.move(mid_x, mid_y);
|
||||
$camera.play();
|
||||
}
|
||||
|
||||
void Engine::zoom(const std::string &actor, const std::string& style, float scale) {
|
||||
auto& config = actor_config(actor);
|
||||
auto bounds = config.st.sprite->getGlobalBounds();
|
||||
float mid_x = config.pos.x + bounds.size.x / 2.0f;
|
||||
float mid_y = config.pos.y + bounds.size.y / 2.0f;
|
||||
|
||||
zoom(mid_x, mid_y, style, scale);
|
||||
}
|
||||
|
||||
void Engine::set_end_cb(std::function<void()> cb) {
|
||||
for(auto& actor : $actors) {
|
||||
actor.anim.onLoop = [&,cb](auto& seq, auto& tr) -> bool {
|
||||
seq.current = tr.toggled ? seq.frame_count - 1 : 0;
|
||||
cb();
|
||||
actor.effect = nullptr;
|
||||
return tr.looped;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::reset(sf::RenderTexture& view) {
|
||||
$camera.reset(view);
|
||||
}
|
||||
}
|
||||
60
src/graphics/scene.hpp
Normal file
60
src/graphics/scene.hpp
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include "graphics/textures.hpp"
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <guecs/ui.hpp>
|
||||
#include "graphics/camera.hpp"
|
||||
#include <functional>
|
||||
#include "graphics/animation.hpp"
|
||||
#include "game/components.hpp"
|
||||
|
||||
namespace scene {
|
||||
using std::shared_ptr;
|
||||
using namespace textures;
|
||||
|
||||
struct Element {
|
||||
std::string name;
|
||||
textures::SpriteTexture st;
|
||||
animation::Animation anim;
|
||||
std::string cell;
|
||||
sf::Vector2f scale{1.0f, 1.0f};
|
||||
sf::Vector2f pos{0.0f, 0.0f};
|
||||
bool at_mid=false;
|
||||
bool flipped=false;
|
||||
std::shared_ptr<sf::Shader> effect = nullptr;
|
||||
sf::Text text;
|
||||
};
|
||||
|
||||
struct Engine {
|
||||
sf::Clock $clock;
|
||||
guecs::UI $ui;
|
||||
components::AnimatedScene& $scene;
|
||||
std::string $layout;
|
||||
std::unordered_map<std::string, int> $actor_name_ids;
|
||||
std::vector<Element> $fixtures;
|
||||
std::vector<Element> $actors;
|
||||
cinematic::Camera $camera{{BOSS_VIEW_WIDTH, BOSS_VIEW_HEIGHT}, "scene"};
|
||||
|
||||
Engine(components::AnimatedScene& scene);
|
||||
|
||||
void init();
|
||||
void render(sf::RenderTexture& view);
|
||||
void update();
|
||||
bool mouse(float x, float y, guecs::Modifiers mods);
|
||||
void attach_text(const std::string& actor, const std::string& text);
|
||||
Element config_scene_element(nlohmann::json& config, bool duped);
|
||||
|
||||
sf::Vector2f position_sprite(textures::SpriteTexture& st, const std::string& cell_name, sf::Vector2f scale, bool at_mid, float x_diff=0.0f, float y_diff=0.0f);
|
||||
|
||||
void move_actor(const std::string& actor, const std::string& cell_name);
|
||||
void animate_actor(const std::string& actor, const std::string& form);
|
||||
void apply_effect(const std::string& actor, const std::string& shader);
|
||||
Element& actor_config(const std::string& actor);
|
||||
void zoom(const std::string& actor, const std::string& style, float scale=0.9f);
|
||||
void zoom(float mid_x, float mid_y, const std::string& style, float scale);
|
||||
void reset(sf::RenderTexture& view);
|
||||
void set_end_cb(std::function<void()> cb);
|
||||
};
|
||||
}
|
||||
78
src/graphics/shaders.cpp
Normal file
78
src/graphics/shaders.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#include "graphics/shaders.hpp"
|
||||
#include <SFML/Graphics/Image.hpp>
|
||||
#include "dbc.hpp"
|
||||
#include <fmt/core.h>
|
||||
#include "game/config.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace shaders {
|
||||
using std::shared_ptr, std::make_shared;
|
||||
|
||||
static ShaderManager SMGR;
|
||||
static bool INITIALIZED = false;
|
||||
static int VERSION = 0;
|
||||
|
||||
inline void configure_shader_defaults(std::shared_ptr<sf::Shader> ptr) {
|
||||
ptr->setUniform("source", sf::Shader::CurrentTexture);
|
||||
}
|
||||
|
||||
bool load_shader(std::string name, nlohmann::json& settings) {
|
||||
std::string file_name = settings["file_name"];
|
||||
auto ptr = std::make_shared<sf::Shader>();
|
||||
bool good = ptr->loadFromFile(file_name, sf::Shader::Type::Fragment);
|
||||
|
||||
if(good) {
|
||||
configure_shader_defaults(ptr);
|
||||
SMGR.shaders.try_emplace(name, name, file_name, ptr);
|
||||
}
|
||||
return good;
|
||||
}
|
||||
|
||||
void init() {
|
||||
if(!INITIALIZED) {
|
||||
dbc::check(sf::Shader::isAvailable(), "no shaders?!");
|
||||
INITIALIZED = true;
|
||||
auto config = settings::get("shaders");
|
||||
bool good = load_shader("ERROR", config["ERROR"]);
|
||||
dbc::check(good, "Failed to load ERROR shader. Look in assets/shaders.json");
|
||||
|
||||
for(auto& [name, settings] : config.json().items()) {
|
||||
if(name == "ERROR") continue;
|
||||
|
||||
dbc::check(!SMGR.shaders.contains(name),
|
||||
$F("shader name '{}' duplicated in assets/shaders.json", name));
|
||||
good = load_shader(name, settings);
|
||||
|
||||
if(!good) {
|
||||
dbc::log($F("failed to load shader {}", name));
|
||||
SMGR.shaders.insert_or_assign(name, SMGR.shaders.at("ERROR"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<sf::Shader> get(const std::string& name) {
|
||||
dbc::check(INITIALIZED, "you forgot to shaders::init()");
|
||||
dbc::check(SMGR.shaders.contains(name),
|
||||
$F("shader name '{}' not in assets/shaders.json", name));
|
||||
auto& rec = SMGR.shaders.at(name);
|
||||
return rec.ptr;
|
||||
}
|
||||
|
||||
int reload() {
|
||||
VERSION++;
|
||||
INITIALIZED = false;
|
||||
SMGR.shaders.clear();
|
||||
init();
|
||||
return VERSION;
|
||||
}
|
||||
|
||||
bool updated(int my_version) {
|
||||
return my_version != VERSION;
|
||||
}
|
||||
|
||||
int version() {
|
||||
return VERSION;
|
||||
}
|
||||
};
|
||||
28
src/graphics/shaders.hpp
Normal file
28
src/graphics/shaders.hpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include "algos/matrix.hpp"
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace shaders {
|
||||
struct Record {
|
||||
std::string name;
|
||||
std::string file_name;
|
||||
std::shared_ptr<sf::Shader> ptr = nullptr;
|
||||
};
|
||||
|
||||
struct ShaderManager {
|
||||
std::unordered_map<std::string, Record> shaders;
|
||||
};
|
||||
|
||||
std::shared_ptr<sf::Shader> get(const std::string& name);
|
||||
void init();
|
||||
bool load_shader(std::string& name, nlohmann::json& settings);
|
||||
bool updated(int my_version);
|
||||
int reload();
|
||||
int version();
|
||||
}
|
||||
209
src/graphics/textures.cpp
Normal file
209
src/graphics/textures.cpp
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
#include "graphics/textures.hpp"
|
||||
#include <SFML/Graphics/Image.hpp>
|
||||
#include "dbc.hpp"
|
||||
#include <fmt/core.h>
|
||||
#include "game/config.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
|
||||
namespace textures {
|
||||
using std::shared_ptr, std::make_shared, nlohmann::json, std::string;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static TextureManager TMGR;
|
||||
static bool initialized = false;
|
||||
static bool failure = false;
|
||||
|
||||
void load_sprite_textures(SpriteTextureMap &mapping, json &config, bool smooth) {
|
||||
for(auto& [name, settings] : config.items()) {
|
||||
const string& path = settings["path"];
|
||||
dbc::check(fs::exists(path), $F("texture at {} doesn't exist", path));
|
||||
auto texture = make_shared<sf::Texture>(path);
|
||||
|
||||
texture->setSmooth(smooth);
|
||||
auto sprite = make_shared<sf::Sprite>(*texture);
|
||||
|
||||
int width = settings["frame_width"];
|
||||
int height = settings["frame_height"];
|
||||
|
||||
dbc::check(width % 2 == 0 && height % 2 == 0,
|
||||
$F("sprite {}:{} has invalid frame size {}:{}",
|
||||
path, name, width, height));
|
||||
|
||||
sf::Vector2i frame_size{width, height};
|
||||
|
||||
sprite->setTextureRect({{0,0}, frame_size});
|
||||
|
||||
dbc::check(!mapping.contains(name),
|
||||
$F("duplicate sprite/icon name {}", (string)name));
|
||||
mapping.try_emplace(name, sprite, texture, frame_size);
|
||||
}
|
||||
}
|
||||
|
||||
void load_sprites() {
|
||||
auto sprites = settings::get("config");
|
||||
bool smooth = sprites["graphics"]["smooth_textures"];
|
||||
|
||||
load_sprite_textures(TMGR.sprite_textures, sprites["sprites"], smooth);
|
||||
|
||||
auto icons = settings::get("assets/icons.json");
|
||||
load_sprite_textures(TMGR.icon_textures, icons.json(), smooth);
|
||||
}
|
||||
|
||||
inline void resize_shit(size_t size) {
|
||||
TMGR.surfaces.resize(size);
|
||||
TMGR.ceilings.resize(size);
|
||||
TMGR.map_tile_set.resize(size);
|
||||
TMGR.ambient_light.resize(size);
|
||||
}
|
||||
|
||||
void load_tiles() {
|
||||
auto assets = settings::get("tiles");
|
||||
auto &tiles = assets.json();
|
||||
|
||||
resize_shit(tiles.size());
|
||||
|
||||
for(auto &el : tiles.items()) {
|
||||
auto &config = el.value();
|
||||
const string& texture_fname = config["texture"];
|
||||
// BUG: if the tiles.json ids aren't exactly in order this fails, but do I need this?
|
||||
size_t surface_i = config["id"];
|
||||
|
||||
dbc::check(!TMGR.name_to_id.contains(el.key()),
|
||||
$F("duplicate key in textures {}",
|
||||
(string)el.key()));
|
||||
|
||||
TMGR.name_to_id.insert_or_assign(el.key(), surface_i);
|
||||
|
||||
if(surface_i >= tiles.size()) {
|
||||
resize_shit(surface_i + 1);
|
||||
}
|
||||
|
||||
TMGR.map_tile_set[surface_i] = config["display"];
|
||||
TMGR.ambient_light[surface_i] = config["light"];
|
||||
TMGR.surfaces[surface_i] = load_image(texture_fname);
|
||||
|
||||
// NOTE: ceilings defaults to 0 which is floor texture so only need to update
|
||||
if(config.contains("ceiling")) {
|
||||
const string& name = config["ceiling"];
|
||||
|
||||
dbc::check(tiles.contains(name), $F("invalid ceiling name {} in tile config {}", name, (string)el.key()));
|
||||
|
||||
auto& ceiling = tiles[name];
|
||||
TMGR.ceilings[surface_i] = ceiling["id"];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void load_map_tiles() {
|
||||
auto config = settings::get("map_tiles");
|
||||
json& tiles = config.json();
|
||||
|
||||
for(auto tile : tiles) {
|
||||
sf::Vector2i coords{tile["x"], tile["y"]};
|
||||
dbc::check(coords.x % ICONGEN_MAP_TILE_DIM == 0, "x coordinates wrong in map");
|
||||
dbc::check(coords.y % ICONGEN_MAP_TILE_DIM == 0, "y coordinates wrong in map");
|
||||
|
||||
sf::IntRect square{coords, {ICONGEN_MAP_TILE_DIM, ICONGEN_MAP_TILE_DIM}};
|
||||
sf::Sprite sprite{TMGR.map_sprite_sheet, square};
|
||||
wchar_t display = tile["display"];
|
||||
|
||||
dbc::check(!TMGR.map_sprites.contains(display),
|
||||
$F("duplicate tile display {} in map_tiles.json", int(display)));
|
||||
|
||||
TMGR.map_sprites.try_emplace(display, sprite);
|
||||
}
|
||||
}
|
||||
|
||||
void init() {
|
||||
dbc::check(!failure, "YOU HAD A CATASTROPHIC TEXTURES FAILURE, FIX IT");
|
||||
|
||||
try {
|
||||
if(!initialized) {
|
||||
load_tiles();
|
||||
load_sprites();
|
||||
load_map_tiles();
|
||||
initialized = true;
|
||||
}
|
||||
} catch(...) {
|
||||
failure = true;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
SpriteTexture& get(const string& name, SpriteTextureMap& mapping) {
|
||||
dbc::check(initialized, "you forgot to call textures::init()");
|
||||
dbc::check(mapping.contains(name),
|
||||
$F("!!!!! textures do not contain {} sprite", name));
|
||||
|
||||
auto& result = mapping.at(name);
|
||||
|
||||
dbc::check(result.sprite != nullptr,
|
||||
$F("bad sprite from textures::get named {}", name));
|
||||
dbc::check(result.texture != nullptr,
|
||||
$F("bad texture from textures::get named {}", name));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
SpriteTexture get_sprite(const string& name, bool duped) {
|
||||
auto& st = get(name, TMGR.sprite_textures);
|
||||
|
||||
if(duped) {
|
||||
st.sprite = make_shared<sf::Sprite>(*st.sprite);
|
||||
}
|
||||
|
||||
return st;
|
||||
}
|
||||
|
||||
SpriteTexture get_icon(const string& name) {
|
||||
return get(name, TMGR.icon_textures);
|
||||
}
|
||||
|
||||
sf::Image load_image(const string& filename) {
|
||||
sf::Image texture;
|
||||
bool good = texture.loadFromFile(filename);
|
||||
dbc::check(good, $F("failed to load {}", filename));
|
||||
return texture;
|
||||
}
|
||||
|
||||
std::vector<int>& get_ambient_light() {
|
||||
return TMGR.ambient_light;
|
||||
}
|
||||
|
||||
std::vector<wchar_t>& get_map_tile_set() {
|
||||
return TMGR.map_tile_set;
|
||||
}
|
||||
|
||||
const uint32_t* get_surface(size_t num) {
|
||||
return (const uint32_t *)TMGR.surfaces[num].getPixelsPtr();
|
||||
}
|
||||
|
||||
sf::Image& get_surface_img(size_t num) {
|
||||
return TMGR.surfaces[num];
|
||||
}
|
||||
|
||||
const uint32_t* get_ceiling(size_t num) {
|
||||
size_t ceiling_num = TMGR.ceilings[num];
|
||||
return (const uint32_t *)TMGR.surfaces[ceiling_num].getPixelsPtr();
|
||||
}
|
||||
|
||||
size_t get_id(const string& name) {
|
||||
dbc::check(TMGR.name_to_id.contains(name),
|
||||
$F("there is no texture named {} in tiles.json", name));
|
||||
return TMGR.name_to_id.at(name);
|
||||
}
|
||||
|
||||
sf::Sprite& get_map_sprite(wchar_t display) {
|
||||
dbc::check(TMGR.map_sprites.contains(display),
|
||||
$F("map_sprites.json doesn't have {} sprite", int(display)));
|
||||
|
||||
return TMGR.map_sprites.at(display);
|
||||
}
|
||||
|
||||
size_t door_for_wall(size_t wall_id) {
|
||||
size_t plain_door_id = textures::get_id("door_plain");
|
||||
return plain_door_id;
|
||||
}
|
||||
};
|
||||
56
src/graphics/textures.hpp
Normal file
56
src/graphics/textures.hpp
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <SFML/Graphics/Sprite.hpp>
|
||||
#include <SFML/Graphics/Image.hpp>
|
||||
#include <SFML/Graphics/Texture.hpp>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include "algos/matrix.hpp"
|
||||
|
||||
namespace textures {
|
||||
|
||||
struct SpriteTexture {
|
||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
||||
std::shared_ptr<sf::Texture> texture = nullptr;
|
||||
sf::Vector2i frame_size;
|
||||
};
|
||||
|
||||
using SpriteTextureMap = std::unordered_map<std::string, SpriteTexture>;
|
||||
|
||||
struct TextureManager {
|
||||
std::vector<sf::Image> surfaces;
|
||||
std::vector<size_t> ceilings;
|
||||
std::vector<wchar_t> map_tile_set;
|
||||
std::vector<int> ambient_light;
|
||||
SpriteTextureMap sprite_textures;
|
||||
SpriteTextureMap icon_textures;
|
||||
std::unordered_map<std::string, size_t> name_to_id;
|
||||
std::unordered_map<wchar_t, sf::Sprite> map_sprites;
|
||||
sf::Texture map_sprite_sheet{"./assets/map_tiles.png"};
|
||||
};
|
||||
|
||||
void init();
|
||||
|
||||
SpriteTexture get_sprite(const std::string& name, bool duped=false);
|
||||
SpriteTexture get_icon(const std::string& name);
|
||||
|
||||
sf::Image load_image(const std::string& filename);
|
||||
|
||||
std::vector<int>& get_ambient_light();
|
||||
|
||||
std::vector<wchar_t>& get_map_tile_set();
|
||||
|
||||
const uint32_t* get_surface(size_t num);
|
||||
|
||||
sf::Image& get_surface_img(size_t num);
|
||||
|
||||
const uint32_t* get_ceiling(size_t num);
|
||||
|
||||
sf::Sprite& get_map_sprite(wchar_t display);
|
||||
|
||||
size_t get_id(const std::string& name);
|
||||
|
||||
size_t door_for_wall(size_t wall_id);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue