Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Dec 11, 2024
1 parent f3b98fd commit df23a3e
Show file tree
Hide file tree
Showing 21 changed files with 39 additions and 42 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(OpenAL CONFIG REQUIRED)
find_package(chipmunk2d CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(Ogg CONFIG REQUIRED)
find_package(PhysFS CONFIG REQUIRED)
find_package(SDL2 CONFIG REQUIRED)
Expand Down Expand Up @@ -53,6 +52,7 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
"-s PTHREAD_POOL_SIZE=4"
"-s INITIAL_MEMORY=128MB"
"-l websocket.js"
"-s USE_SDL=2"
"-s WEBSOCKET_SUBPROTOCOL=text"
"-s EXPORTED_RUNTIME_METHODS=['callMain']"
# Debugging
Expand All @@ -72,7 +72,6 @@ endif()

target_link_libraries(${PROJECT_NAME} PRIVATE
chipmunk2d::chipmunk2d
fmt::fmt
Ogg::ogg
physfs-static
SDL2::SDL2-static
Expand Down
1 change: 0 additions & 1 deletion conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[requires]
chipmunk2d/7.0.3
fmt/11.0.2
libspng/0.7.4
nlohmann_json/3.11.3
ogg/1.3.5
Expand Down
4 changes: 2 additions & 2 deletions src/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ color::color(const SDL_Color &scolor) noexcept
color::color(const std::string &hex)
: _r(0), _g(0), _b(0), _a(255) {
if (hex.length() != 7 && hex.length() != 9) [[unlikely]] {
throw std::invalid_argument(fmt::format("Invalid hex code format: '{}'. Use #RRGGBB or #RRGGBBAA.", hex));
throw std::invalid_argument(std::format("Invalid hex code format: '{}'. Use #RRGGBB or #RRGGBBAA.", hex));
}

if (hex[0] != '#') [[unlikely]] {
throw std::invalid_argument(fmt::format("Hex code '{}' must start with '#'.", hex));
throw std::invalid_argument(std::format("Hex code '{}' must start with '#'.", hex));
}

_r = static_cast<uint8_t>(std::stoi(hex.substr(1, 2), nullptr, 16));
Expand Down
2 changes: 1 addition & 1 deletion src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <SDL2/SDL.h>
#include <chipmunk/chipmunk.h>
#include <chipmunk/chipmunk_structs.h>
#include <fmt/core.h>
#include <nlohmann/json.hpp>
#include <ogg/ogg.h>
#include <physfs.h>
Expand All @@ -38,6 +37,7 @@
#include <chrono>
#include <cmath>
#include <cstdint>
#include <format>
#include <functional>
#include <iostream>
#include <iterator>
Expand Down
3 changes: 1 addition & 2 deletions src/entity.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "entity.hpp"
#include <fmt/base.h>

using namespace framework;

entity::entity(entityprops &&props) noexcept
: _props(std::move(props)) {}

entity::~entity() noexcept {
fmt::print("entity destroyed {}\n", kind());
std::println("entity of type '{}' destroyed", kind());
}

std::shared_ptr<entity> entity::create(entityprops &&props) {
Expand Down
4 changes: 2 additions & 2 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ entitymanager::entitymanager(std::shared_ptr<world> world, std::shared_ptr<resou
_resourcemanager(std::move(resourcemanager)) {}

std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
const auto buffer = storage::io::read(fmt::format("entities/{}.json", kind));
const auto buffer = storage::io::read(std::format("entities/{}.json", kind));
const auto j = json::parse(buffer);

auto spritesheet = j.contains("spritesheet")
Expand Down Expand Up @@ -93,7 +93,7 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
};

auto e = entity::create(std::move(props));
fmt::println("[entitymanager] spawn {} kind {}", e->id(), kind);
std::println("[entitymanager] spawn {} kind {}", e->id(), kind);
_entities.emplace_back(e);
return e;
}
Expand Down
2 changes: 1 addition & 1 deletion src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ using namespace storage;

void filesystem::mount(std::string_view filename, std::string_view mountpoint) noexcept(false) {
if (PHYSFS_mount(filename.data(), mountpoint.data(), true) == 0) [[unlikely]] {
throw std::runtime_error(fmt::format("[PHYSFS_mount] failed to mount {} to {}. reason: {}", filename, mountpoint, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
throw std::runtime_error(std::format("[PHYSFS_mount] failed to mount {} to {}. reason: {}", filename, mountpoint, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
}
}
8 changes: 4 additions & 4 deletions src/fontfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ std::shared_ptr<font> fontfactory::get(const std::string &family) {
auto [it, added] = _pool.insert_or_assign(family, nullptr);

if (added) [[unlikely]] {
fmt::println("[fontfactory] cache miss {}", family);
std::println("[fontfactory] cache miss {}", family);

const auto &buffer = storage::io::read(fmt::format("fonts/{}.json", family));
const auto &buffer = storage::io::read(std::format("fonts/{}.json", family));
const auto &j = json::parse(buffer);
const auto &alphabet = j["alphabet"].get<std::string>();
const auto spacing = j["spacing"].get<int16_t>();
Expand All @@ -35,7 +35,7 @@ std::shared_ptr<font> fontfactory::get(const std::string &family) {
};

if (!surface) [[unlikely]] {
throw std::runtime_error(fmt::format("[SDL_CreateRGBSurfaceWithFormatFrom] Error: {}", SDL_GetError()));
throw std::runtime_error(std::format("[SDL_CreateRGBSurfaceWithFormatFrom] Error: {}", SDL_GetError()));
}

const auto pixels = static_cast<uint32_t *>(surface->pixels);
Expand All @@ -50,7 +50,7 @@ std::shared_ptr<font> fontfactory::get(const std::string &family) {
}

if (x >= size.width()) [[unlikely]] {
throw std::runtime_error(fmt::format("Error: Missing glyph for '{}'", letter));
throw std::runtime_error(std::format("Error: Missing glyph for '{}'", letter));
}

width = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/framerate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void framerate::loop(float_t delta) noexcept {
_start = now;

if (_elapsed >= 1000) {
fmt::println("{:.1f}", (_frames / (_elapsed / 1000.0f)));
std::println("{:.1f}", (_frames / (_elapsed / 1000.0f)));
_elapsed = 0;
_frames = 0;
}
Expand Down
8 changes: 4 additions & 4 deletions src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ std::pair<std::vector<uint8_t>, geometry::size> _load_png(std::string_view filen
auto ctx = std::unique_ptr<spng_ctx, decltype(&spng_ctx_free)>(spng_ctx_new(0), spng_ctx_free);

if (const auto error = spng_set_png_buffer(ctx.get(), buffer.data(), buffer.size()); error != SPNG_OK) [[unlikely]] {
throw std::runtime_error(fmt::format("[spng_set_png_buffer] error while parsing image: {}, error: {}", filename, spng_strerror(error)));
throw std::runtime_error(std::format("[spng_set_png_buffer] error while parsing image: {}, error: {}", filename, spng_strerror(error)));
}

spng_ihdr ihdr{};
if (const auto error = spng_get_ihdr(ctx.get(), &ihdr); error != SPNG_OK) [[unlikely]] {
throw std::runtime_error(fmt::format("[spng_get_ihdr] error while getting image information: {}, error: {}", filename, spng_strerror(error)));
throw std::runtime_error(std::format("[spng_get_ihdr] error while getting image information: {}, error: {}", filename, spng_strerror(error)));
}

const int format{SPNG_FMT_RGBA8};
size_t length{0};
if (const auto error = spng_decoded_image_size(ctx.get(), format, &length); error != SPNG_OK) [[unlikely]] {
throw std::runtime_error(fmt::format("[spng_decoded_image_size] error while getting image size: {}, error: {}", filename, spng_strerror(error)));
throw std::runtime_error(std::format("[spng_decoded_image_size] error while getting image size: {}, error: {}", filename, spng_strerror(error)));
}

std::vector<uint8_t> output(length);
if (const auto error = spng_decode_image(ctx.get(), output.data(), length, format, SPNG_DECODE_TRNS); error != SPNG_OK) [[unlikely]] {
throw std::runtime_error(fmt::format("[spng_decode_image] error while decoding image: {}, error: {}", filename, spng_strerror(error)));
throw std::runtime_error(std::format("[spng_decode_image] error while decoding image: {}, error: {}", filename, spng_strerror(error)));
}

const auto size = geometry::size{static_cast<int32_t>(ihdr.width), static_cast<int32_t>(ihdr.height)};
Expand Down
6 changes: 3 additions & 3 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ using namespace storage;
std::vector<uint8_t> io::read(std::string_view filename) noexcept(false) {
std::unique_ptr<PHYSFS_File, decltype(&PHYSFS_close)> ptr(PHYSFS_openRead(filename.data()), PHYSFS_close);
if (!ptr) [[unlikely]] {
throw std::runtime_error(fmt::format("[PHYSFS_openRead] error while opening file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
throw std::runtime_error(std::format("[PHYSFS_openRead] error while opening file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
}

PHYSFS_sint64 length = PHYSFS_fileLength(ptr.get());
if (length <= 0) [[unlikely]] {
throw std::runtime_error(fmt::format("[PHYSFS_fileLength] invalid file length, file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
throw std::runtime_error(std::format("[PHYSFS_fileLength] invalid file length, file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
}

std::vector<uint8_t> buffer(length);
auto bytesRead = PHYSFS_readBytes(ptr.get(), buffer.data(), length);
if (bytesRead != length) [[unlikely]] {
throw std::runtime_error(fmt::format("[PHYSFS_readBytes] error reading file: {}, expected {} bytes but read {}, error: {}", filename, length, bytesRead, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
throw std::runtime_error(std::format("[PHYSFS_readBytes] error reading file: {}, expected {} bytes but read {}, error: {}", filename, length, bytesRead, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
}

return buffer;
Expand Down
6 changes: 3 additions & 3 deletions src/pixmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ pixmap::pixmap(const std::shared_ptr<renderer> &renderer, std::string_view filen
SDL_FreeSurface
};
if (!surface) [[unlikely]] {
throw std::runtime_error(fmt::format("[SDL_CreateRGBSurfaceWithFormat] error while creating surface, file: {}, error: {}", filename, SDL_GetError()));
throw std::runtime_error(std::format("[SDL_CreateRGBSurfaceWithFormat] error while creating surface, file: {}, error: {}", filename, SDL_GetError()));
}

std::memcpy(surface->pixels, output.data(), output.size());

_texture = texture_ptr(SDL_CreateTextureFromSurface(*renderer, surface.get()), SDL_Deleter());
if (!_texture) [[unlikely]] {
throw std::runtime_error(fmt::format("[SDL_CreateTextureFromSurface] error while creating texture from surface, file: {}", filename));
throw std::runtime_error(std::format("[SDL_CreateTextureFromSurface] error while creating texture from surface, file: {}", filename));
}
}

Expand All @@ -34,7 +34,7 @@ pixmap::pixmap(const std::shared_ptr<renderer> &renderer, std::unique_ptr<SDL_Su

_texture = texture_ptr(SDL_CreateTextureFromSurface(*renderer, surface.get()), SDL_Deleter());
if (!_texture) [[unlikely]] {
throw std::runtime_error(fmt::format("[SDL_CreateTextureFromSurface] error while creating texture, SDL Error: {}", SDL_GetError()));
throw std::runtime_error(std::format("[SDL_CreateTextureFromSurface] error while creating texture, SDL Error: {}", SDL_GetError()));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/pixmappool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const std::shared_ptr<pixmap> pixmappool::get(const std::string &filename) {
auto [it, added] = _pool.insert_or_assign(filename, nullptr);

if (added) [[unlikely]] {
fmt::println("[pixmappool] cache miss {}", filename);
std::println("[pixmappool] cache miss {}", filename);

assert(_renderer);

Expand All @@ -20,10 +20,10 @@ const std::shared_ptr<pixmap> pixmappool::get(const std::string &filename) {
}

void pixmappool::flush() noexcept {
fmt::println("[pixmappool] actual size {}", _pool.size());
std::println("[pixmappool] actual size {}", _pool.size());

const auto count = std::erase_if(_pool, [](const auto &pair) { return pair.second.use_count() == MINIMAL_USE_COUNT; });
fmt::println("[pixmappool] {} objects have been flushed", count);
std::println("[pixmappool] {} objects have been flushed", count);
}

void pixmappool::update(float_t delta) noexcept {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using namespace graphics;
renderer::renderer(SDL_Window *window)
: _renderer(SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), SDL_Deleter()) {
if (!_renderer) [[unlikely]] {
throw std::runtime_error(fmt::format("[SDL_CreateRenderer] failed to create renderer: {}", SDL_GetError()));
throw std::runtime_error(std::format("[SDL_CreateRenderer] failed to create renderer: {}", SDL_GetError()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/scenemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ scenemanager::scenemanager(std::shared_ptr<graphics::pixmappool> pixmappool) noe
: _pixmappool(std::move(pixmappool)) {}

void scenemanager::set(const std::string_view name) noexcept {
const auto buffer = storage::io::read(fmt::format("scenes/{}.json", name));
const auto buffer = storage::io::read(std::format("scenes/{}.json", name));
const auto j = json::parse(buffer);
_background = _pixmappool->get(j["background"].get<std::string>());
_size = {j.at("width").get<int32_t>(), j.at("height").get<int32_t>()};
Expand Down
2 changes: 1 addition & 1 deletion src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace storage;
using namespace network;

sol::table require(sol::state &lua, std::string_view module) {
const auto data = io::read(fmt::format("scripts/{}.lua", module));
const auto data = io::read(std::format("scripts/{}.lua", module));
const auto script = std::string(data.begin(), data.end());
const auto result = lua.script(script);

Expand Down
8 changes: 4 additions & 4 deletions src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using json = nlohmann::json;
socket::socket() noexcept {
_queue.reserve(8);

const auto url = fmt::format("http://{}:3000/socket", emscripten_run_script_string("window.location.hostname"));
const auto url = std::format("http://{}:3000/socket", emscripten_run_script_string("window.location.hostname"));

EmscriptenWebSocketCreateAttributes attrs = {
url.c_str(),
Expand Down Expand Up @@ -116,16 +116,16 @@ socket::~socket() noexcept {
}

void socket::emit(const std::string &topic, const std::string &data) noexcept {
send(fmt::format(R"({{"event": {{"topic": "{}", "data": {}}}}})", topic, data));
send(std::format(R"({{"event": {{"topic": "{}", "data": {}}}}})", topic, data));
}

void socket::on(const std::string &topic, std::function<void(const std::string &)> callback) noexcept {
send(fmt::format(R"({{"subscribe": "{}"}})", topic));
send(std::format(R"({{"subscribe": "{}"}})", topic));
_callbacks[topic].push_back(std::move(callback));
}

void socket::rpc(const std::string &method, const std::string &arguments, std::function<void(const std::string &)> callback) noexcept {
send(fmt::format(R"({{"rpc": {{"request": {{"id": {}, "method": "{}", "arguments": {}}}}}}})", ++counter, method, arguments));
send(std::format(R"({{"rpc": {{"request": {{"id": {}, "method": "{}", "arguments": {}}}}}}})", ++counter, method, arguments));
_callbacks[std::to_string(counter)].push_back(std::move(callback));
}

Expand Down
4 changes: 2 additions & 2 deletions src/soundfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ soundfx::soundfx(std::shared_ptr<audiodevice> audiodevice, std::string_view file
: _audiodevice(std::move(audiodevice)) {
std::unique_ptr<PHYSFS_File, decltype(&PHYSFS_close)> fp{PHYSFS_openRead(filename.data()), PHYSFS_close};
if (!fp) [[unlikely]] {
throw std::runtime_error(fmt::format("[PHYSFS_openRead] error while opening file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
throw std::runtime_error(std::format("[PHYSFS_openRead] error while opening file: {}, error: {}", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
}

std::unique_ptr<OggVorbis_File, decltype(&ov_clear)> vf{new OggVorbis_File, ov_clear};
Expand Down Expand Up @@ -106,7 +106,7 @@ soundfx::soundfx(std::shared_ptr<audiodevice> audiodevice, std::string_view file
do {
offset = ov_read(vf.get(), reinterpret_cast<char *>(array.data()), length, bigendian, 2, 1, nullptr);
if (offset < 0) [[unlikely]] {
throw std::runtime_error(fmt::format("[ov_read] error while reading file: {}, error: {}", filename, ov_strerror(offset)));
throw std::runtime_error(std::format("[ov_read] error while reading file: {}, error: {}", filename, ov_strerror(offset)));
}
data.insert(data.end(), array.begin(), std::ranges::next(array.begin(), offset));
} while (offset > 0);
Expand Down
4 changes: 2 additions & 2 deletions src/soundmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ std::shared_ptr<soundfx> soundmanager::get(const std::string &filename) noexcept
auto [it, added] = _pool.insert_or_assign(filename, nullptr);

if (added) [[unlikely]] {
fmt::println("[soundmanager] cache miss {}", filename);
std::println("[soundmanager] cache miss {}", filename);

assert(_audiodevice);

Expand All @@ -20,7 +20,7 @@ std::shared_ptr<soundfx> soundmanager::get(const std::string &filename) noexcept
}

void soundmanager::play(const std::string &filename) noexcept {
if (const auto &sound = get(fmt::format("blobs/{}.ogg", filename)); sound) {
if (const auto &sound = get(std::format("blobs/{}.ogg", filename)); sound) {
sound->play();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/timermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void timermanager::add_timer(int32_t interval, std::function<void()> fn, bool re
const auto ptr = std::make_shared<std::function<void()>>(std::move(fn));
const auto id = SDL_AddTimer(interval, repeat ? wrapper : singleshot_wrapper, ptr.get());
if (!id) [[unlikely]] {
throw std::runtime_error(fmt::format("[SDL_AddTimer] failed to set timer. reason: {}", SDL_GetError()));
throw std::runtime_error(std::format("[SDL_AddTimer] failed to set timer. reason: {}", SDL_GetError()));
}

_timers.emplace(id, ptr);
Expand Down
2 changes: 1 addition & 1 deletion src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ window::window(std::string_view title, int32_t width, int32_t height, bool fulls
: _width(width), _height(height),
_window(SDL_CreateWindow(title.data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN : 0)), SDL_Deleter()) {
if (_window == nullptr) [[unlikely]] {
throw std::runtime_error(fmt::format("[SDL_CreateWindow] failed to create window: {}", SDL_GetError()));
throw std::runtime_error(std::format("[SDL_CreateWindow] failed to create window: {}", SDL_GetError()));
}
}

Expand Down

0 comments on commit df23a3e

Please sign in to comment.