diff --git a/.clang-format b/.clang-format index 9264f3bc0f..4191b164f7 100644 --- a/.clang-format +++ b/.clang-format @@ -81,7 +81,7 @@ IndentCaseBlocks: false IndentCaseLabels: false IndentExternBlock: NoIndent IndentGotoLabels: false -IndentPPDirectives: None +IndentPPDirectives: BeforeHash IndentWidth: 4 IndentWrappedFunctionNames: false # clang-format-16 InsertNewlineAtEOF: true diff --git a/copying.md b/copying.md index 1002c8321a..ec1ddd2a46 100644 --- a/copying.md +++ b/copying.md @@ -155,6 +155,7 @@ _the openage authors_ are: | Michael Seibt | RoboSchmied | github à roboschmie dawt de | | Nikhil Ghosh | NikhilGhosh75 | nghosh606 à gmail dawt com | | Edvin Lindholm | EdvinLndh | edvinlndh à gmail dawt com | +| Jeremiah Morgan | jere8184 | jeremiahmorgan dawt bham à outlook dawt com | If you're a first-time committer, add yourself to the above list. This is not just for legal reasons, but also to keep an overview of all those nicknames. diff --git a/libopenage/console/tests.cpp b/libopenage/console/tests.cpp index 445fb71b30..7da8dd02f4 100644 --- a/libopenage/console/tests.cpp +++ b/libopenage/console/tests.cpp @@ -1,12 +1,12 @@ -// Copyright 2014-2019 the openage authors. See copying.md for legal info. +// Copyright 2014-2024 the openage authors. See copying.md for legal info. -#include #include +#include #ifdef _MSC_VER -#define STDOUT_FILENO 1 + #define STDOUT_FILENO 1 #else -#include + #include #endif #include "../util/fds.h" #include "../util/pty.h" @@ -15,8 +15,8 @@ #include #include -#include "../log/log.h" #include "../error/error.h" +#include "../log/log.h" #include "buf.h" #include "console.h" @@ -51,7 +51,7 @@ void render() { void interactive() { - #ifndef _WIN32 +#ifndef _WIN32 console::Buf buf{{80, 25}, 1337, 80}; struct winsize ws; @@ -75,7 +75,7 @@ void interactive() { throw Error(MSG(err) << "execl(\"" << shell << "\", \"" << shell << "\", nullptr) failed: " << strerror(errno)); } default: - //we are the parent + // we are the parent break; } @@ -143,7 +143,7 @@ void interactive() { ssize_t retval = read(ptyin.fd, rdbuf, rdbuf_size); switch (retval) { case -1: - switch(errno) { + switch (errno) { case EIO: loop = false; break; @@ -175,7 +175,7 @@ void interactive() { // show cursor termout.puts("\x1b[?25h"); - #endif /* _WIN32 */ +#endif /* _WIN32 */ } diff --git a/libopenage/coord/coord.h.template b/libopenage/coord/coord.h.template index 8aeafadc5f..278b486ae4 100644 --- a/libopenage/coord/coord.h.template +++ b/libopenage/coord/coord.h.template @@ -74,12 +74,8 @@ struct Coord${camelcase}Absolute { return static_cast(*this); } - constexpr bool operator ==(const Absolute &other) const { - return ${formatted_members("(this->{0} == other.{0})", join_with=" && ")}; - } - - constexpr bool operator !=(const Absolute &other) const { - return !(*this == other); + friend constexpr bool operator ==(const Absolute &lhs, const Absolute &rhs) { + return ${formatted_members("(lhs.{0} == rhs.{0})", join_with=" && ")}; } }; @@ -167,12 +163,8 @@ struct Coord${camelcase}Relative { return static_cast(*this); } - constexpr bool operator ==(const Relative &other) const { - return ${formatted_members("(this->{0} == other.{0})", join_with=" && ")}; - } - - constexpr bool operator !=(const Relative &other) const { - return !(*this == other); + friend constexpr bool operator ==(const Relative &lhs, const Relative &rhs) { + return ${formatted_members("(lhs.{0} == rhs.{0})", join_with=" && ")}; } }; diff --git a/libopenage/datastructure/pairing_heap.h b/libopenage/datastructure/pairing_heap.h index dd5438091a..d03972e898 100644 --- a/libopenage/datastructure/pairing_heap.h +++ b/libopenage/datastructure/pairing_heap.h @@ -38,7 +38,7 @@ class PairingHeap; template > -class PairingHeapNode : public std::enable_shared_from_this> { +class PairingHeapNode{ public: using this_type = PairingHeapNode; @@ -47,7 +47,6 @@ class PairingHeapNode : public std::enable_shared_from_this &node) { - node->add_child(this->shared_from_this()); + void become_child_of(const this_type& node) { + node.add_child(this); } /** * Add the given node as a child to this one. */ - void add_child(const std::shared_ptr &new_child) { + void add_child(const this_type &new_child) { // first child is the most recently attached one // it must not have siblings as they will get lost. @@ -85,7 +88,7 @@ class PairingHeapNode : public std::enable_shared_from_thisfirst_child = new_child; - new_child->parent = this->shared_from_this(); + new_child->parent = this; } /** @@ -93,17 +96,17 @@ class PairingHeapNode : public std::enable_shared_from_this link_with(const std::shared_ptr &node) { - std::shared_ptr new_root; - std::shared_ptr new_child; + this_type* link_with(const this_type* node) { + this_type* new_root; + this_type* new_child; if (this->cmp(this->data, node->data)) { - new_root = this->shared_from_this(); + new_root = this; new_child = node; } else { new_root = node; - new_child = this->shared_from_this(); + new_child = this; } // children of new root become siblings of new new_child @@ -128,15 +131,15 @@ class PairingHeapNode : public std::enable_shared_from_this link_backwards() { + this_type* link_backwards() { if (this->next_sibling == nullptr) { // reached end, return this as current root, // the previous siblings will be linked to it. - return this->shared_from_this(); + return this; } // recurse to last sibling, - std::shared_ptr node = this->next_sibling->link_backwards(); + this_type* node = this->next_sibling->link_backwards(); // then link ourself to the new root. this->next_sibling = nullptr; @@ -153,7 +156,7 @@ class PairingHeapNode : public std::enable_shared_from_thisparent and this->parent->first_child == this->shared_from_this()) { + if (this->parent and this->parent->first_child == this) { // we are the first child // make the next sibling the first child this->parent->first_child = this->next_sibling; @@ -176,10 +179,10 @@ class PairingHeapNode : public std::enable_shared_from_this first_child; - std::shared_ptr prev_sibling; - std::shared_ptr next_sibling; - std::shared_ptr parent; // for decrease-key and delete + this_type* first_child; + this_type* prev_sibling; + this_type* next_sibling; + this_type* parent; // for decrease-key and delete }; @@ -191,10 +194,8 @@ template > class PairingHeap final { public: - using node_t = heapnode_t; - using element_t = std::shared_ptr; - using this_type = PairingHeap; - using cmp_t = compare; + using element_t = heapnode_t*; + using this_type = PairingHeap; /** * create a empty heap. @@ -211,7 +212,7 @@ class PairingHeap final { * O(1) */ element_t push(const T &item) { - element_t new_node = std::make_shared(item); + element_t new_node = new heapnode_t(item); this->push_node(new_node); return new_node; } @@ -221,7 +222,7 @@ class PairingHeap final { * O(1) */ element_t push(T &&item) { - element_t new_node = std::make_shared(std::move(item)); + element_t new_node = new heapnode_t(std::move(item)); this->push_node(new_node); return new_node; } @@ -230,7 +231,10 @@ class PairingHeap final { * returns and removes the smallest item on the heap. */ T pop() { - return std::move(this->pop_node()->data); + element_t poped_node = this->pop_node(); + T data = std::move(poped_node->data); + delete poped_node; + return data; } /** @@ -583,7 +587,7 @@ class PairingHeap final { this->walk_tree(this->root_node, func); } -protected: +private: void walk_tree(const element_t &root, const std::function &func) const { func(root); @@ -631,7 +635,6 @@ class PairingHeap final { } } -protected: compare cmp; size_t node_count; element_t root_node; diff --git a/libopenage/engine/engine.h b/libopenage/engine/engine.h index 4f96ae74bf..1fe4298061 100644 --- a/libopenage/engine/engine.h +++ b/libopenage/engine/engine.h @@ -11,7 +11,7 @@ // TODO: Remove custom jthread definition when clang/libc++ finally supports it #if __llvm__ -#if !__cpp_lib_jthread + #if !__cpp_lib_jthread namespace std { class jthread : public thread { public: @@ -27,9 +27,9 @@ class jthread : public thread { } }; } // namespace std -#endif + #endif #else -#include + #include #endif diff --git a/libopenage/error/handlers.cpp b/libopenage/error/handlers.cpp index f608b0e78f..b3e61450b2 100644 --- a/libopenage/error/handlers.cpp +++ b/libopenage/error/handlers.cpp @@ -1,4 +1,4 @@ -// Copyright 2015-2023 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. /* * This file holds handlers for std::terminate and SIGSEGV. @@ -17,9 +17,9 @@ #include #ifdef _MSC_VER -#include + #include #else -#include + #include #endif #include "util/init.h" diff --git a/libopenage/error/stackanalyzer.cpp b/libopenage/error/stackanalyzer.cpp index 878cb01025..cc04dae05d 100644 --- a/libopenage/error/stackanalyzer.cpp +++ b/libopenage/error/stackanalyzer.cpp @@ -1,4 +1,4 @@ -// Copyright 2015-2023 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. #include "stackanalyzer.h" @@ -30,8 +30,8 @@ constexpr uint64_t base_skip_frames = 1; #if WITH_BACKTRACE -// use modern -#include + // use modern + #include namespace openage { namespace error { @@ -59,7 +59,7 @@ struct backtrace_state *bt_state; util::OnInit init_backtrace_state([]() { bt_state = backtrace_create_state( nullptr, // auto-determine filename - 1, // threaded + 1, // threaded backtrace_error_callback, nullptr // passed to the callback ); @@ -189,8 +189,8 @@ void StackAnalyzer::get_symbols(std::function cb #else // WITHOUT_BACKTRACE -#ifdef _WIN32 -#include + #ifdef _WIN32 + #include namespace openage { namespace error { @@ -208,10 +208,10 @@ void StackAnalyzer::analyze() { } // namespace error } // namespace openage -#else // not _MSC_VER + #else // not _MSC_VER -// use GNU's -#include + // use GNU's + #include namespace openage::error { @@ -256,7 +256,7 @@ void StackAnalyzer::analyze() { } // namespace openage::error -#endif // for _MSC_VER or GNU execinfo + #endif // for _MSC_VER or GNU execinfo namespace openage::error { diff --git a/libopenage/event/demo/gui.cpp b/libopenage/event/demo/gui.cpp index 100509c0f4..66d0bbf79d 100644 --- a/libopenage/event/demo/gui.cpp +++ b/libopenage/event/demo/gui.cpp @@ -1,25 +1,25 @@ -// Copyright 2016-2023 the openage authors. See copying.md for legal info. +// Copyright 2016-2024 the openage authors. See copying.md for legal info. #include "gui.h" // the gui requires ncurses. #if WITH_NCURSES -#include -#include -#include -#include -#ifdef __MINGW32__ -#include -#else -#include -#endif // __MINGW32__ -#include - -#include "curve/continuous.h" -#include "curve/discrete.h" -#include "event/demo/gamestate.h" -#include "util/fixed_point.h" + #include + #include + #include + #include + #ifdef __MINGW32__ + #include + #else + #include + #endif // __MINGW32__ + #include + + #include "curve/continuous.h" + #include "curve/discrete.h" + #include "event/demo/gamestate.h" + #include "util/fixed_point.h" namespace openage::event::demo { diff --git a/libopenage/event/demo/gui.h b/libopenage/event/demo/gui.h index de21a99d02..8e447d808f 100644 --- a/libopenage/event/demo/gui.h +++ b/libopenage/event/demo/gui.h @@ -1,18 +1,18 @@ -// Copyright 2015-2023 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. #pragma once #include "config.h" #if WITH_NCURSES -#include -#include -#include -#include - -#include "event/demo/gamestate.h" -#include "time/time.h" -#include "util/vector.h" + #include + #include + #include + #include + + #include "event/demo/gamestate.h" + #include "time/time.h" + #include "util/vector.h" namespace openage::event::demo { diff --git a/libopenage/event/demo/main.cpp b/libopenage/event/demo/main.cpp index 3ccbed5ac6..f5100b2e82 100644 --- a/libopenage/event/demo/main.cpp +++ b/libopenage/event/demo/main.cpp @@ -1,4 +1,4 @@ -// Copyright 2016-2023 the openage authors. See copying.md for legal info. +// Copyright 2016-2024 the openage authors. See copying.md for legal info. #include "main.h" @@ -16,11 +16,11 @@ #include "renderer/gui/integration/public/gui_application_with_logger.h" #if WITH_NCURSES -#ifdef __MINGW32__ -#include -#else -#include -#endif // __MINGW32__ + #ifdef __MINGW32__ + #include + #else + #include + #endif // __MINGW32__ #endif diff --git a/libopenage/event/demo/physics.cpp b/libopenage/event/demo/physics.cpp index e27a51d962..e7e969a533 100644 --- a/libopenage/event/demo/physics.cpp +++ b/libopenage/event/demo/physics.cpp @@ -1,4 +1,4 @@ -// Copyright 2017-2023 the openage authors. See copying.md for legal info. +// Copyright 2017-2024 the openage authors. See copying.md for legal info. #include "physics.h" @@ -9,13 +9,13 @@ #include #if WITH_NCURSES -#ifdef __MINGW32__ -#include -#else -#include -#endif // __MINGW32__ + #ifdef __MINGW32__ + #include + #else + #include + #endif // __MINGW32__ -#include "gui.h" + #include "gui.h" #endif #include "error/error.h" @@ -377,7 +377,7 @@ void Physics::init(const std::shared_ptr &gstate, loop->create_event("demo.ball.reflect_panel", state->ball->position, state, now); // FIXME once "reset": deregister - //reset(state, mgr, now); + // reset(state, mgr, now); } void Physics::process_input(const std::shared_ptr &state, @@ -466,11 +466,11 @@ void Physics::process_input(const std::shared_ptr &state, Physics::reset(state, *mgr, now); break; - //if (player->state->get(now).state == PongEvent::LOST) { + // if (player->state->get(now).state == PongEvent::LOST) { // state->ball.position->set_last(now, state.display_boundary * 0.5); - //} - //update_ball(state, now, init_recursion_limit); - //break; + // } + // update_ball(state, now, init_recursion_limit); + // break; default: break; diff --git a/libopenage/log/level.h b/libopenage/log/level.h index ef7322ec63..0d35f163c5 100644 --- a/libopenage/log/level.h +++ b/libopenage/log/level.h @@ -1,4 +1,4 @@ -// Copyright 2015-2023 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. #pragma once @@ -50,11 +50,11 @@ struct OAAPI level : util::Enum { level(); #ifdef __MINGW32__ -// Do not try to optimize these out even if it seems they are not used. -// Namely MIN that is not used within the library. -#define NOOPTIMIZE __attribute__((__used__)) + // Do not try to optimize these out even if it seems they are not used. + // Namely MIN that is not used within the library. + #define NOOPTIMIZE __attribute__((__used__)) #else -#define NOOPTIMIZE + #define NOOPTIMIZE #endif // _win32 static constexpr level_value MIN NOOPTIMIZE{{"min loglevel", -1000}, "5"}; diff --git a/libopenage/log/message.h b/libopenage/log/message.h index d46ed18c7d..ca534e2efe 100644 --- a/libopenage/log/message.h +++ b/libopenage/log/message.h @@ -21,11 +21,11 @@ #if defined(__GNUC__) -#define OPENAGE_FUNC_NAME __PRETTY_FUNCTION__ + #define OPENAGE_FUNC_NAME __PRETTY_FUNCTION__ #elif defined(_MSC_VER) -#define OPENAGE_FUNC_NAME __FUNCSIG__ + #define OPENAGE_FUNC_NAME __FUNCSIG__ #else -#define OPENAGE_FUNC_NAME __FUNCTION__ + #define OPENAGE_FUNC_NAME __FUNCTION__ #endif namespace openage { diff --git a/libopenage/log/stdout_logsink.cpp b/libopenage/log/stdout_logsink.cpp index 647af51c49..99daecf5cd 100644 --- a/libopenage/log/stdout_logsink.cpp +++ b/libopenage/log/stdout_logsink.cpp @@ -1,4 +1,4 @@ -// Copyright 2015-2023 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. #include "stdout_logsink.h" @@ -7,8 +7,8 @@ #include #ifdef _MSC_VER -#define WIN32_LEAN_AND_MEAN -#include + #define WIN32_LEAN_AND_MEAN + #include #endif #include "log/level.h" diff --git a/libopenage/presenter/presenter.cpp b/libopenage/presenter/presenter.cpp index 33c798bcef..a37362be95 100644 --- a/libopenage/presenter/presenter.cpp +++ b/libopenage/presenter/presenter.cpp @@ -121,7 +121,17 @@ void Presenter::init_graphics(bool debug) { this->camera->resize(w, h); }); + // Camera manager this->camera_manager = std::make_shared(this->camera); + // TODO: Make boundaries dynamic based on map size. + this->camera_manager->set_camera_boundaries( + renderer::camera::CameraBoundaries{ + renderer::camera::X_BOUND_MIN, + renderer::camera::X_BOUND_MAX, + renderer::camera::Y_BOUND_MIN, + renderer::camera::Y_BOUND_MAX, + renderer::camera::Z_BOUND_MIN, + renderer::camera::Z_BOUND_MAX}); // Skybox this->skybox_renderer = std::make_shared( diff --git a/libopenage/renderer/camera/CMakeLists.txt b/libopenage/renderer/camera/CMakeLists.txt index aea6391e7a..71de5c7bd1 100644 --- a/libopenage/renderer/camera/CMakeLists.txt +++ b/libopenage/renderer/camera/CMakeLists.txt @@ -1,4 +1,5 @@ add_sources(libopenage + boundaries.cpp camera.cpp definitions.cpp frustum_2d.cpp diff --git a/libopenage/renderer/camera/boundaries.cpp b/libopenage/renderer/camera/boundaries.cpp new file mode 100644 index 0000000000..ca77344c7e --- /dev/null +++ b/libopenage/renderer/camera/boundaries.cpp @@ -0,0 +1,9 @@ +// Copyright 2024-2024 the openage authors. See copying.md for legal info. + +#include "boundaries.h" + + +namespace openage::renderer::camera { + + +} // namespace openage::renderer::camera diff --git a/libopenage/renderer/camera/boundaries.h b/libopenage/renderer/camera/boundaries.h new file mode 100644 index 0000000000..484a398455 --- /dev/null +++ b/libopenage/renderer/camera/boundaries.h @@ -0,0 +1,25 @@ +// Copyright 2024-2024 the openage authors. See copying.md for legal info. + +#pragma once + +namespace openage::renderer::camera { + +/** + * Defines boundaries for the camera's view. + */ +struct CameraBoundaries { + /// The minimum boundary for the camera's X-coordinate. + float x_min; + /// The maximum boundary for the camera's X-coordinate. + float x_max; + /// The minimum boundary for the camera's Y-coordinate. + float y_min; + /// The maximum boundary for the camera's Y-coordinate. + float y_max; + /// The minimum boundary for the camera's Z-coordinate. + float z_min; + /// The maximum boundary for the camera's Z-coordinate. + float z_max; +}; + +} // namespace openage::renderer::camera diff --git a/libopenage/renderer/camera/camera.cpp b/libopenage/renderer/camera/camera.cpp index f70efbee64..14504998e0 100644 --- a/libopenage/renderer/camera/camera.cpp +++ b/libopenage/renderer/camera/camera.cpp @@ -61,38 +61,7 @@ Camera::Camera(const std::shared_ptr &renderer, } void Camera::look_at_scene(Eigen::Vector3f scene_pos) { - if (scene_pos[1] > this->scene_pos[1]) { - // TODO: camera can't look at a position that's - // higher than it's own position - } - - // TODO: Although the below method should be faster, calculating and adding the direction - // vector from scene_pos to new_pos may be easier to understand - // i.e. new_pos = scene_pos + b/sin(30) * direction_vec - - // due to the fixed angle, the centered scene position - // and the new camera position form a right triangle. - // - // c - + new camera pos - // - |b - // center +------+ - // a - // - // we can calculate the new camera position via the offset a - // using the angle and length of side b. - auto y_delta = this->scene_pos[1] - scene_pos[1]; // b (vertical distance) - auto xz_distance = y_delta * std::numbers::sqrt3; // a (horizontal distance); a = b * (cos(30°) / sin(30°)) - - // get x and z offsets - // the camera is pointed diagonally to the negative x and z axis - // a is the length of the diagonal from camera.xz to scene_pos.xz - // so the x and z offest are sides of a square with the same diagonal - auto side_length = xz_distance / std::numbers::sqrt2; - auto new_pos = Eigen::Vector3f( - scene_pos[0] + side_length, - this->scene_pos[1], // height unchanged - scene_pos[2] + side_length); - + auto new_pos = calc_look_at(scene_pos); this->move_to(new_pos); } @@ -102,15 +71,17 @@ void Camera::look_at_coord(coord::scene3 coord_pos) { this->look_at_scene(scene_pos); } -void Camera::move_to(Eigen::Vector3f scene_pos) { - // TODO: Check and set bounds for where the camera can go and check them here +void Camera::move_to(Eigen::Vector3f scene_pos, const CameraBoundaries &camera_boundaries) { + scene_pos[0] = std::clamp(scene_pos[0], camera_boundaries.x_min, camera_boundaries.x_max); + scene_pos[1] = std::clamp(scene_pos[1], camera_boundaries.y_min, camera_boundaries.y_max); + scene_pos[2] = std::clamp(scene_pos[2], camera_boundaries.z_min, camera_boundaries.z_max); this->scene_pos = scene_pos; this->moved = true; } -void Camera::move_rel(Eigen::Vector3f direction, float delta) { - this->move_to(this->scene_pos + (direction * delta)); +void Camera::move_rel(Eigen::Vector3f direction, float delta, const CameraBoundaries &camera_boundaries) { + this->move_to(this->scene_pos + (direction * delta), camera_boundaries); } void Camera::set_zoom(float zoom) { @@ -292,8 +263,43 @@ void Camera::init_uniform_buffer(const std::shared_ptr &renderer) { this->uniform_buffer = renderer->add_uniform_buffer(ubo_info); } +Eigen::Vector3f Camera::calc_look_at(Eigen::Vector3f target) { + if (target[1] > this->scene_pos[1]) { + // TODO: camera can't look at a position that's + // higher than it's own position + } + + // TODO: Although the below method should be faster, calculating and adding the direction + // vector from scene_pos to new_pos may be easier to understand + // i.e. new_pos = scene_pos + b/sin(30) * direction_vec + + // due to the fixed angle, the centered scene position + // and the new camera position form a right triangle. + // + // c - + new camera pos + // - |b + // center +------+ + // a + // + // we can calculate the new camera position via the offset a + // using the angle and length of side b. + auto y_delta = this->scene_pos[1] - target[1]; // b (vertical distance) + auto xz_distance = y_delta * std::numbers::sqrt3; // a (horizontal distance); a = b * (cos(30°) / sin(30°)) + + // get x and z offsets + // the camera is pointed diagonally to the negative x and z axis + // a is the length of the diagonal from camera.xz to scene_pos.xz + // so the x and z offest are sides of a square with the same diagonal + auto side_length = xz_distance / std::numbers::sqrt2; + return Eigen::Vector3f( + target[0] + side_length, + this->scene_pos[1], // height unchanged + target[2] + side_length); +} + inline float Camera::get_real_zoom_factor() const { return 0.5f * this->default_zoom_ratio * this->zoom; } + } // namespace openage::renderer::camera diff --git a/libopenage/renderer/camera/camera.h b/libopenage/renderer/camera/camera.h index eccb5b40e6..5e5759c06e 100644 --- a/libopenage/renderer/camera/camera.h +++ b/libopenage/renderer/camera/camera.h @@ -4,7 +4,9 @@ #include #include +#include #include +#include #include @@ -12,6 +14,7 @@ #include "coord/scene.h" #include "util/vector.h" +#include "renderer/camera/boundaries.h" #include "renderer/camera/definitions.h" #include "renderer/camera/frustum_2d.h" #include "renderer/camera/frustum_3d.h" @@ -83,18 +86,21 @@ class Camera { * Move the camera position in the direction of a given vector. * * @param scene_pos New 3D position of the camera in the scene. + * @param camera_boundaries 3D boundaries for the camera. */ - void move_to(Eigen::Vector3f scene_pos); + void move_to(Eigen::Vector3f scene_pos, const CameraBoundaries &camera_boundaries = DEFAULT_CAM_BOUNDARIES); /** - * Move the camera position in the direction of a given vector. + * Move the camera position in the direction of a given vector taking the + * camera boundaries into account. * * @param direction Direction vector. Added to the current position. * @param delta Delta for controlling the amount by which the camera is moved. The * value is multiplied with the directional vector before its applied to * the positional vector. + * @param camera_boundaries 3D boundaries for the camera. */ - void move_rel(Eigen::Vector3f direction, float delta = 1.0f); + void move_rel(Eigen::Vector3f direction, float delta = 1.0f, const CameraBoundaries &camera_boundaries = DEFAULT_CAM_BOUNDARIES); /** * Set the zoom level of the camera. Values smaller than 1.0f let the @@ -200,6 +206,7 @@ class Camera { */ const Frustum3d get_frustum_3d() const; + private: /** * Create the uniform buffer for the camera. @@ -208,6 +215,13 @@ class Camera { */ void init_uniform_buffer(const std::shared_ptr &renderer); + /** + * Calculates the camera's position needed to center its view on the given target. + * + * @param target The target position in the 3D scene the camera should focus on. + */ + Eigen::Vector3f calc_look_at(Eigen::Vector3f target); + /** * Get the zoom factor applied to the camera projection. * diff --git a/libopenage/renderer/camera/definitions.h b/libopenage/renderer/camera/definitions.h index 9586239a2f..7c3bbdd5d3 100644 --- a/libopenage/renderer/camera/definitions.h +++ b/libopenage/renderer/camera/definitions.h @@ -3,7 +3,9 @@ #pragma once #include +#include +#include "renderer/camera/boundaries.h" namespace openage::renderer::camera { @@ -58,4 +60,23 @@ static constexpr float DEFAULT_MAX_ZOOM_OUT = 64.0f; */ static constexpr float DEFAULT_ZOOM_RATIO = 1.0f / 49; +static constexpr CameraBoundaries DEFAULT_CAM_BOUNDARIES{ + std::numeric_limits::lowest(), + std::numeric_limits::max(), + std::numeric_limits::lowest(), + std::numeric_limits::max(), + std::numeric_limits::lowest(), + std::numeric_limits::max()}; + +/** + * Constant values for the camera bounds (based on current fix terrain grid of 20x20). + * TODO: Make boundaries dynamic based on map size. + */ +static constexpr float X_BOUND_MIN = 12.25f; +static constexpr float X_BOUND_MAX = 32.25f; +static constexpr float Y_BOUND_MIN = 0.0f; +static constexpr float Y_BOUND_MAX = 20.0f; +static constexpr float Z_BOUND_MIN = -8.25f; +static constexpr float Z_BOUND_MAX = 12.25f; + } // namespace openage::renderer::camera diff --git a/libopenage/renderer/color.cpp b/libopenage/renderer/color.cpp index 7333dfb073..f03f883a0a 100644 --- a/libopenage/renderer/color.cpp +++ b/libopenage/renderer/color.cpp @@ -1,12 +1,11 @@ -// Copyright 2015-2019 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. #include "color.h" namespace openage { namespace renderer { -Color::Color() - : +Color::Color() : r{0}, g{0}, b{0}, @@ -14,8 +13,7 @@ Color::Color() // Empty } -Color::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) - : +Color::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : r{r}, g{g}, b{b}, @@ -32,6 +30,7 @@ bool Color::operator!=(const Color &other) const { } Color Colors::WHITE = {255, 255, 255, 255}; -Color Colors::BLACK = { 0, 0, 0, 255}; +Color Colors::BLACK = {0, 0, 0, 255}; -}} // openage::renderer +} // namespace renderer +} // namespace openage diff --git a/libopenage/renderer/demo/demo_3.cpp b/libopenage/renderer/demo/demo_3.cpp index 9c294285f9..7de6372303 100644 --- a/libopenage/renderer/demo/demo_3.cpp +++ b/libopenage/renderer/demo/demo_3.cpp @@ -52,6 +52,18 @@ void renderer_demo_3(const util::Path &path) { // it is updated each frame before the render stages auto cam_manager = std::make_shared(camera); + // Set boundaries for camera movement in the scene + // this restricts camera movement to the area defined by the boundaries + // i.e. the map terrain in this case + cam_manager->set_camera_boundaries( + camera::CameraBoundaries{ + 12.25f, + 22.25f, + 0.0f, + 20.0f, + 2.25f, + 12.25f}); + // Render stages // every stage use a different subrenderer that manages renderables, // shaders, textures & more. diff --git a/libopenage/renderer/gui/guisys/private/opengl_debug_logger.cpp b/libopenage/renderer/gui/guisys/private/opengl_debug_logger.cpp index 61025528a6..0f565efaae 100644 --- a/libopenage/renderer/gui/guisys/private/opengl_debug_logger.cpp +++ b/libopenage/renderer/gui/guisys/private/opengl_debug_logger.cpp @@ -1,4 +1,4 @@ -// Copyright 2017-2023 the openage authors. See copying.md for legal info. +// Copyright 2017-2024 the openage authors. See copying.md for legal info. #include "opengl_debug_logger.h" @@ -7,11 +7,11 @@ #include #ifdef __APPLE__ -// from https://www.khronos.org/registry/OpenGL/api/GL/glext.h -#define GL_DEBUG_CALLBACK_FUNCTION 0x8244 -#define GL_DEBUG_OUTPUT_SYNCHRONOUS 0x8242 -#define GL_DEBUG_TYPE_ERROR 0x824C -#define GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR 0x824E + // from https://www.khronos.org/registry/OpenGL/api/GL/glext.h + #define GL_DEBUG_CALLBACK_FUNCTION 0x8244 + #define GL_DEBUG_OUTPUT_SYNCHRONOUS 0x8242 + #define GL_DEBUG_TYPE_ERROR 0x824C + #define GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR 0x824E #endif namespace qtgui { diff --git a/libopenage/renderer/stages/camera/manager.cpp b/libopenage/renderer/stages/camera/manager.cpp index 39eddd2b52..d3cbd3f3b2 100644 --- a/libopenage/renderer/stages/camera/manager.cpp +++ b/libopenage/renderer/stages/camera/manager.cpp @@ -3,19 +3,21 @@ #include "manager.h" #include +#include -#include "renderer/camera/camera.h" #include "renderer/uniform_buffer.h" #include "renderer/uniform_input.h" namespace openage::renderer::camera { -CameraManager::CameraManager(const std::shared_ptr &camera) : +CameraManager::CameraManager(const std::shared_ptr &camera, + const CameraBoundaries &camera_boundaries) : camera{camera}, move_motion_directions{static_cast(MoveDirection::NONE)}, zoom_motion_direction{static_cast(ZoomDirection::NONE)}, move_motion_speed{0.2f}, - zoom_motion_speed{0.05f} { + zoom_motion_speed{0.05f}, + camera_boundaries{camera_boundaries} { this->uniforms = this->camera->get_uniform_buffer()->new_uniform_input( "view", camera->get_view_matrix(), @@ -33,18 +35,18 @@ void CameraManager::move_frame(MoveDirection direction, float speed) { case MoveDirection::LEFT: // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), speed / 2); + this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, 1.0f), speed / 2, this->camera_boundaries); break; case MoveDirection::RIGHT: // half the speed because the relationship between forward/back and // left/right is 1:2 in our ortho projection. - this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), speed / 2); + this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, -1.0f), speed / 2, this->camera_boundaries); break; case MoveDirection::FORWARD: - this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), speed); + this->camera->move_rel(Eigen::Vector3f(-1.0f, 0.0f, -1.0f), speed, this->camera_boundaries); break; case MoveDirection::BACKWARD: - this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), speed); + this->camera->move_rel(Eigen::Vector3f(1.0f, 0.0f, 1.0f), speed, this->camera_boundaries); break; default: @@ -66,6 +68,10 @@ void CameraManager::zoom_frame(ZoomDirection direction, float speed) { } } +void CameraManager::set_camera_boundaries(const CameraBoundaries &camera_boundaries) { + this->camera_boundaries = camera_boundaries; +} + void CameraManager::update_motion() { if (this->move_motion_directions != static_cast(MoveDirection::NONE)) { Eigen::Vector3f move_dir{0.0f, 0.0f, 0.0f}; @@ -83,7 +89,7 @@ void CameraManager::update_motion() { move_dir += Eigen::Vector3f(1.0f, 0.0f, 1.0f); } - this->camera->move_rel(move_dir, this->move_motion_speed); + this->camera->move_rel(move_dir, this->move_motion_speed, this->camera_boundaries); } if (this->zoom_motion_direction != static_cast(ZoomDirection::NONE)) { diff --git a/libopenage/renderer/stages/camera/manager.h b/libopenage/renderer/stages/camera/manager.h index 13d876b25f..36d605e959 100644 --- a/libopenage/renderer/stages/camera/manager.h +++ b/libopenage/renderer/stages/camera/manager.h @@ -3,7 +3,9 @@ #pragma once #include +#include +#include "renderer/camera/camera.h" namespace openage::renderer { class UniformBufferInput; @@ -47,8 +49,10 @@ class CameraManager { * Create a new camera manager. * * @param camera Camera to manage. + * @param camera_boundaries Boundaries for the camera movement in the scene. */ - CameraManager(const std::shared_ptr &camera); + CameraManager(const std::shared_ptr &camera, + const CameraBoundaries &camera_boundaries = DEFAULT_CAM_BOUNDARIES); ~CameraManager() = default; /** @@ -103,6 +107,13 @@ class CameraManager { */ void set_zoom_motion_speed(float speed); + /** + * Set boundaries for camera movement in the scene. + * + * @param camera_boundaries XYZ boundaries for the camera movement. + */ + void set_camera_boundaries(const CameraBoundaries &camera_boundaries); + private: /** * Update the camera parameters. @@ -143,6 +154,11 @@ class CameraManager { * Uniform buffer input for the camera. */ std::shared_ptr uniforms; + + /** + * Camera boundaries for X and Z movement. Contains minimum and maximum values for each axes. + */ + CameraBoundaries camera_boundaries; }; } // namespace camera diff --git a/libopenage/renderer/vulkan/loader.cpp b/libopenage/renderer/vulkan/loader.cpp index fdeb6f8c94..2dcf6bd2fc 100644 --- a/libopenage/renderer/vulkan/loader.cpp +++ b/libopenage/renderer/vulkan/loader.cpp @@ -1,4 +1,4 @@ -// Copyright 2017-2018 the openage authors. See copying.md for legal info. +// Copyright 2017-2024 the openage authors. See copying.md for legal info. #include "loader.h" @@ -9,14 +9,14 @@ namespace openage { namespace renderer { namespace vulkan { -VlkLoader::VlkLoader() - : inited(false) {} +VlkLoader::VlkLoader() : + inited(false) {} void VlkLoader::init(VkInstance instance) { - #ifndef NDEBUG +#ifndef NDEBUG this->pCreateDebugReportCallbackEXT = PFN_vkCreateDebugReportCallbackEXT(vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT")); this->pDestroyDebugReportCallbackEXT = PFN_vkDestroyDebugReportCallbackEXT(vkGetInstanceProcAddr(instance, "vkDestroyDebugReportCallbackEXT")); - #endif +#endif this->inited = true; } @@ -24,10 +24,9 @@ void VlkLoader::init(VkInstance instance) { #ifndef NDEBUG VkResult VlkLoader::vkCreateDebugReportCallbackEXT( VkInstance instance, - const VkDebugReportCallbackCreateInfoEXT* pCreateInfo, - const VkAllocationCallbacks* pAllocator, - VkDebugReportCallbackEXT* pCallback -) { + const VkDebugReportCallbackCreateInfoEXT *pCreateInfo, + const VkAllocationCallbacks *pAllocator, + VkDebugReportCallbackEXT *pCallback) { if (!this->inited) { throw Error(MSG(err) << "Tried to request function from Vulkan extension loader before initializing it."); } @@ -42,8 +41,7 @@ VkResult VlkLoader::vkCreateDebugReportCallbackEXT( void VlkLoader::vkDestroyDebugReportCallbackEXT( VkInstance instance, VkDebugReportCallbackEXT callback, - const VkAllocationCallbacks* pAllocator -) { + const VkAllocationCallbacks *pAllocator) { if (!this->inited) { throw Error(MSG(err) << "Tried to request function from Vulkan extension loader before initializing it."); } @@ -54,4 +52,6 @@ void VlkLoader::vkDestroyDebugReportCallbackEXT( } #endif -}}} // openage::renderer::vulkan +} // namespace vulkan +} // namespace renderer +} // namespace openage diff --git a/libopenage/util/compiler.cpp b/libopenage/util/compiler.cpp index b854d91278..2fd850920d 100644 --- a/libopenage/util/compiler.cpp +++ b/libopenage/util/compiler.cpp @@ -1,22 +1,22 @@ -// Copyright 2015-2019 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. #include "compiler.h" #ifndef _WIN32 -#include -#include + #include + #include #else -#define WIN32_LEAN_AND_MEAN -#include -#include + #define WIN32_LEAN_AND_MEAN + #include + #include #endif #include "strings.h" #include #include -#include #include +#include namespace openage { namespace util { @@ -34,7 +34,8 @@ std::string demangle(const char *symbol) { if (status != 0) { return symbol; - } else { + } + else { std::string result{buf}; free(buf); return result; @@ -78,12 +79,12 @@ std::optional symbol_name_win(const void *addr) { constexpr int buffer_size = sizeof(SYMBOL_INFO) + name_buffer_size * sizeof(char); std::array buffer; - SYMBOL_INFO *symbol_info = reinterpret_cast(buffer.data()); + SYMBOL_INFO *symbol_info = reinterpret_cast(buffer.data()); symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO); symbol_info->MaxNameLen = name_buffer_size; - if (SymFromAddr(process_handle, reinterpret_cast(addr), nullptr, symbol_info)) { + if (SymFromAddr(process_handle, reinterpret_cast(addr), nullptr, symbol_info)) { return std::string(symbol_info->Name); } } @@ -92,7 +93,7 @@ std::optional symbol_name_win(const void *addr) { } -} +} // namespace #endif std::string symbol_name(const void *addr, bool require_exact_addr, bool no_pure_addrs) { @@ -100,8 +101,7 @@ std::string symbol_name(const void *addr, bool require_exact_addr, bool no_pure_ auto symbol_name_result = symbol_name_win(addr); - if (!initialized_symbol_handler_successfully || - !symbol_name_result.has_value()) { + if (!initialized_symbol_handler_successfully || !symbol_name_result.has_value()) { return no_pure_addrs ? "" : addr_to_string(addr); } @@ -113,8 +113,9 @@ std::string symbol_name(const void *addr, bool require_exact_addr, bool no_pure_ if (dladdr(addr, &addr_info) == 0) { // dladdr has... failed. return no_pure_addrs ? "" : addr_to_string(addr); - } else { - size_t symbol_offset = (size_t) addr - (size_t) addr_info.dli_saddr; + } + else { + size_t symbol_offset = (size_t)addr - (size_t)addr_info.dli_saddr; if (addr_info.dli_sname == nullptr or (symbol_offset != 0 and require_exact_addr)) { return no_pure_addrs ? "" : addr_to_string(addr); @@ -123,7 +124,8 @@ std::string symbol_name(const void *addr, bool require_exact_addr, bool no_pure_ if (symbol_offset == 0) { // this is our symbol name. return demangle(addr_info.dli_sname); - } else { + } + else { return util::sformat("%s+0x%lx", demangle(addr_info.dli_sname).c_str(), symbol_offset); @@ -138,7 +140,8 @@ bool is_symbol(const void *addr) { if (!initialized_symbol_handler_successfully) { return true; - } else { + } + else { return symbol_name_win(addr).has_value(); } @@ -154,4 +157,5 @@ bool is_symbol(const void *addr) { } -}} // openage::util +} // namespace util +} // namespace openage diff --git a/libopenage/util/fds.cpp b/libopenage/util/fds.cpp index 468d3354bc..8d5188c10d 100644 --- a/libopenage/util/fds.cpp +++ b/libopenage/util/fds.cpp @@ -1,4 +1,4 @@ -// Copyright 2014-2019 the openage authors. See copying.md for legal info. +// Copyright 2014-2024 the openage authors. See copying.md for legal info. #include "fds.h" @@ -7,12 +7,12 @@ #include #include -#include #include "pty.h" +#include #ifdef _WIN32 -#include + #include #else -#include + #include #endif #include "unicode.h" @@ -28,10 +28,10 @@ FD::FD(int fd, bool set_nonblocking) { this->close_on_destroy = true; if (set_nonblocking) { - #ifndef _WIN32 +#ifndef _WIN32 int flags = ::fcntl(this->fd, F_GETFL, 0); ::fcntl(this->fd, F_SETFL, flags | O_NONBLOCK); - #endif +#endif } } @@ -64,31 +64,32 @@ int FD::putbyte(char c) { int FD::putcp(int cp) { char utf8buf[5]; if (util::utf8_encode(cp, utf8buf) == 0) { - //unrepresentable character (question mark in black rhombus) + // unrepresentable character (question mark in black rhombus) return this->puts("\uFFFD"); - } else { + } + else { return this->puts(utf8buf); } } int FD::printf(const char *format, ...) { const unsigned buf_size = 16; - char *buf = static_cast(malloc(sizeof(char) * buf_size)); + char *buf = static_cast(malloc(sizeof(char) * buf_size)); if (!buf) { return -1; } va_list vl; - //first, try to vsnprintf to a buffer of length 16 + // first, try to vsnprintf to a buffer of length 16 va_start(vl, format); unsigned len = vsnprintf(buf, buf_size, format, vl); va_end(vl); - //if that wasn't enough, allocate more memory and try again + // if that wasn't enough, allocate more memory and try again if (len >= buf_size) { char *oldbuf = buf; - buf = static_cast(realloc(oldbuf, sizeof(char) * (len + 1))); + buf = static_cast(realloc(oldbuf, sizeof(char) * (len + 1))); if (!buf) { free(oldbuf); return -1; @@ -99,42 +100,42 @@ int FD::printf(const char *format, ...) { va_end(vl); } - //output buf to the socket + // output buf to the socket int result = this->puts(buf); - //free the buffer + // free the buffer free(buf); return result; } void FD::setinputmodecanon() { - #ifndef _WIN32 +#ifndef _WIN32 if (::isatty(this->fd)) { - //get the terminal settings for stdin + // get the terminal settings for stdin ::tcgetattr(this->fd, &this->old_tio); - //backup old settings + // backup old settings struct termios new_tio = this->old_tio; - //disable buffered i/o (canonical mode) and local echo + // disable buffered i/o (canonical mode) and local echo new_tio.c_lflag &= (~ICANON & ~ECHO & ~ISIG); - //set the settings + // set the settings ::tcsetattr(this->fd, TCSANOW, &new_tio); this->restore_input_mode_on_destroy = true; } - #endif /* _WIN32 */ +#endif /* _WIN32 */ } void FD::restoreinputmode() { - #ifndef _WIN32 +#ifndef _WIN32 if (::isatty(this->fd)) { ::tcsetattr(this->fd, TCSANOW, &this->old_tio); this->restore_input_mode_on_destroy = false; } - #endif /* _WIN32 */ +#endif /* _WIN32 */ } -} // openage::util +} // namespace openage::util diff --git a/libopenage/util/fds.h b/libopenage/util/fds.h index 251ad0aec7..309384e1ad 100644 --- a/libopenage/util/fds.h +++ b/libopenage/util/fds.h @@ -5,7 +5,7 @@ #include #ifndef _WIN32 -#include + #include #endif namespace openage { diff --git a/libopenage/util/fslike/directory.cpp b/libopenage/util/fslike/directory.cpp index 22055e2c32..420757f3a6 100644 --- a/libopenage/util/fslike/directory.cpp +++ b/libopenage/util/fslike/directory.cpp @@ -1,11 +1,11 @@ -// Copyright 2017-2019 the openage authors. See copying.md for legal info. +// Copyright 2017-2024 the openage authors. See copying.md for legal info. #include "directory.h" // HACK: windows.h defines max and min as macros. This results in compile errors. #ifdef _WIN32 -// defining `NOMINMAX` disables the definition of those macros. -#define NOMINMAX + // defining `NOMINMAX` disables the definition of those macros. + #define NOMINMAX #endif #include @@ -17,34 +17,32 @@ #include #ifdef __APPLE__ -#include + #include #endif #ifdef _WIN32 -#include -#include -#include -// HACK: What the heck? I want the std::filesystem library! -#define O_NOCTTY 0 -#define O_NONBLOCK 0 -#define W_OK 2 + #include + #include + #include + // HACK: What the heck? I want the std::filesystem library! + #define O_NOCTTY 0 + #define O_NONBLOCK 0 + #define W_OK 2 #else // ! _MSC_VER -#include + #include #endif -#include "./native.h" #include "../file.h" #include "../filelike/native.h" #include "../misc.h" #include "../path.h" +#include "./native.h" namespace openage::util::fslike { -Directory::Directory(std::string basepath, bool create_if_missing) - : +Directory::Directory(std::string basepath, bool create_if_missing) : basepath{std::move(basepath)} { - if (create_if_missing) { this->mkdirs({}); } @@ -78,8 +76,7 @@ bool Directory::is_file(const Path::parts_t &parts) { auto stat_result = this->do_stat(parts); // test for regular file - if (std::get<1>(stat_result) == 0 and - S_ISREG(std::get<0>(stat_result).st_mode)) { + if (std::get<1>(stat_result) == 0 and S_ISREG(std::get<0>(stat_result).st_mode)) { return true; } @@ -91,8 +88,7 @@ bool Directory::is_dir(const Path::parts_t &parts) { auto stat_result = this->do_stat(parts); // test for regular file - if (std::get<1>(stat_result) == 0 and - S_ISDIR(std::get<0>(stat_result).st_mode)) { + if (std::get<1>(stat_result) == 0 and S_ISDIR(std::get<0>(stat_result).st_mode)) { return true; } @@ -104,9 +100,7 @@ bool Directory::writable(const Path::parts_t &parts) { Path::parts_t parts_test = parts; // try to find the first existing path-part - while (not (this->is_dir(parts_test) or - this->is_file(parts_test))) { - + while (not(this->is_dir(parts_test) or this->is_file(parts_test))) { if (parts_test.size() == 0) { throw Error{ERR << "file not found"}; } @@ -120,7 +114,6 @@ bool Directory::writable(const Path::parts_t &parts) { std::vector Directory::list(const Path::parts_t &parts) { - const std::string path = this->resolve(parts); std::vector ret; @@ -145,7 +138,6 @@ std::vector Directory::list(const Path::parts_t &parts) { bool Directory::mkdirs(const Path::parts_t &parts) { - Path::parts_t all_parts = util::split(this->basepath, PATHSEP); vector_extend(all_parts, parts); @@ -158,9 +150,7 @@ bool Directory::mkdirs(const Path::parts_t &parts) { struct stat buf; // it it exists already, try creating the next one - if (stat(dirpath.c_str(), &buf) == 0 and - S_ISDIR(buf.st_mode)) { - + if (stat(dirpath.c_str(), &buf) == 0 and S_ISDIR(buf.st_mode)) { continue; } @@ -184,40 +174,35 @@ bool Directory::mkdirs(const Path::parts_t &parts) { File Directory::open_r(const Path::parts_t &parts) { return File{ std::make_shared(this->resolve(parts), - filelike::Native::mode_t::R) - }; + filelike::Native::mode_t::R)}; } File Directory::open_w(const Path::parts_t &parts) { return File{ std::make_shared(this->resolve(parts), - filelike::Native::mode_t::W) - }; + filelike::Native::mode_t::W)}; } File Directory::open_rw(const Path::parts_t &parts) { return File{ std::make_shared(this->resolve(parts), - filelike::Native::mode_t::RW) - }; + filelike::Native::mode_t::RW)}; } File Directory::open_a(const Path::parts_t &parts) { return File{ std::make_shared(this->resolve(parts), - filelike::Native::mode_t::A) - }; + filelike::Native::mode_t::A)}; } File Directory::open_ar(const Path::parts_t &parts) { return File{ std::make_shared(this->resolve(parts), - filelike::Native::mode_t::AR) - }; + filelike::Native::mode_t::AR)}; } @@ -228,9 +213,9 @@ std::string Directory::get_native_path(const Path::parts_t &parts) { bool Directory::rename(const Path::parts_t &parts, const Path::parts_t &target_parts) { - return std::rename(this->resolve(parts).c_str(), - this->resolve(target_parts).c_str()) == 0; + this->resolve(target_parts).c_str()) + == 0; } @@ -245,9 +230,8 @@ bool Directory::touch(const Path::parts_t &parts) { // create the file if missing int fd = open( path.c_str(), - O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, - 0666 - ); + O_WRONLY | O_CREAT | O_NOCTTY | O_NONBLOCK, + 0666); if (fd < 0) { return false; @@ -308,4 +292,4 @@ std::ostream &Directory::repr(std::ostream &stream) { return stream; } -} // openage::util::fslike +} // namespace openage::util::fslike diff --git a/libopenage/util/macro/concat.h b/libopenage/util/macro/concat.h index b8f7b746ca..dd057ccd9f 100644 --- a/libopenage/util/macro/concat.h +++ b/libopenage/util/macro/concat.h @@ -9,16 +9,16 @@ #define CONCAT_N(_1, _2, _3, NAME, ...) NAME #ifdef _MSC_VER -#define CONCAT_COUNT_ARGS_IMPL(args) CONCAT_N args -#define CONCAT_COUNT_ARGS(...) CONCAT_COUNT_ARGS_IMPL((__VA_ARGS__, 3, 2, 1)) -#define CONCAT_HELPER2(count) CONCAT_##count -#define CONCAT_HELPER1(count) CONCAT_HELPER2(count) -#define CONCAT_HELPER(count) CONCAT_HELPER1(count) -#define CONCAT_GLUE(x, y) x y -#define CONCAT(OP, ...) CONCAT_GLUE(CONCAT_HELPER(CONCAT_COUNT_ARGS(__VA_ARGS__)), (OP, __VA_ARGS__)) + #define CONCAT_COUNT_ARGS_IMPL(args) CONCAT_N args + #define CONCAT_COUNT_ARGS(...) CONCAT_COUNT_ARGS_IMPL((__VA_ARGS__, 3, 2, 1)) + #define CONCAT_HELPER2(count) CONCAT_##count + #define CONCAT_HELPER1(count) CONCAT_HELPER2(count) + #define CONCAT_HELPER(count) CONCAT_HELPER1(count) + #define CONCAT_GLUE(x, y) x y + #define CONCAT(OP, ...) CONCAT_GLUE(CONCAT_HELPER(CONCAT_COUNT_ARGS(__VA_ARGS__)), (OP, __VA_ARGS__)) #else -#define CONCAT(OP, ...) CONCAT_N(__VA_ARGS__, \ - CONCAT_3, \ - CONCAT_2, \ - CONCAT_1)(OP, __VA_ARGS__) + #define CONCAT(OP, ...) CONCAT_N(__VA_ARGS__, \ + CONCAT_3, \ + CONCAT_2, \ + CONCAT_1)(OP, __VA_ARGS__) #endif diff --git a/libopenage/util/macro/loop.h b/libopenage/util/macro/loop.h index 5b8dc59e8e..c868a12694 100644 --- a/libopenage/util/macro/loop.h +++ b/libopenage/util/macro/loop.h @@ -9,16 +9,16 @@ #define LOOP_N(_1, _2, _3, NAME, ...) NAME #ifdef _MSC_VER -#define LOOP_COUNT_ARGS_IMPL(args) LOOP_N args -#define LOOP_COUNT_ARGS(...) LOOP_COUNT_ARGS_IMPL((__VA_ARGS__, 3, 2, 1)) -#define LOOP_HELPER2(count) LOOP_##count -#define LOOP_HELPER1(count) LOOP_HELPER2(count) -#define LOOP_HELPER(count) LOOP_HELPER1(count) -#define LOOP_GLUE(x, y) x y -#define LOOP(MACRO, ...) LOOP_GLUE(LOOP_HELPER(LOOP_COUNT_ARGS(__VA_ARGS__)), (MACRO, __VA_ARGS__)) + #define LOOP_COUNT_ARGS_IMPL(args) LOOP_N args + #define LOOP_COUNT_ARGS(...) LOOP_COUNT_ARGS_IMPL((__VA_ARGS__, 3, 2, 1)) + #define LOOP_HELPER2(count) LOOP_##count + #define LOOP_HELPER1(count) LOOP_HELPER2(count) + #define LOOP_HELPER(count) LOOP_HELPER1(count) + #define LOOP_GLUE(x, y) x y + #define LOOP(MACRO, ...) LOOP_GLUE(LOOP_HELPER(LOOP_COUNT_ARGS(__VA_ARGS__)), (MACRO, __VA_ARGS__)) #else -#define LOOP(MACRO, ...) LOOP_N(__VA_ARGS__, \ - LOOP_3, \ - LOOP_2, \ - LOOP_1)(MACRO, __VA_ARGS__) + #define LOOP(MACRO, ...) LOOP_N(__VA_ARGS__, \ + LOOP_3, \ + LOOP_2, \ + LOOP_1)(MACRO, __VA_ARGS__) #endif diff --git a/libopenage/util/os.cpp b/libopenage/util/os.cpp index c3bbea2da6..ccc68d86c6 100644 --- a/libopenage/util/os.cpp +++ b/libopenage/util/os.cpp @@ -1,4 +1,4 @@ -// Copyright 2014-2019 the openage authors. See copying.md for legal info. +// Copyright 2014-2024 the openage authors. See copying.md for legal info. #include "os.h" @@ -7,16 +7,16 @@ #ifdef _WIN32 // TODO not yet implemented #else -#include + #include #endif #ifdef __APPLE__ -#include + #include #endif #ifdef __FreeBSD__ -#include -#include + #include + #include #endif #include "../log/log.h" @@ -77,8 +77,7 @@ std::string self_exec_filename() { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, - -1 - }; + -1}; while (true) { std::unique_ptr buf{new char[bufsize]}; @@ -103,23 +102,23 @@ int execute_file(const char *path, bool background) { // TODO some sort of shell-open, not yet implemented return -1; // failure #else - std::string runner = ""; -#ifdef __APPLE__ - runner = subprocess::which("open"); -#else - runner = subprocess::which("xdg-open"); - // fallback - if (runner.size() == 0) { - runner = subprocess::which("gnome-open"); - } -#endif - if (runner.size() == 0) { - log::log(MSG(err) << "could not locate file-opener"); - return -1; - } + std::string runner = ""; + #ifdef __APPLE__ + runner = subprocess::which("open"); + #else + runner = subprocess::which("xdg-open"); + // fallback + if (runner.size() == 0) { + runner = subprocess::which("gnome-open"); + } + #endif + if (runner.size() == 0) { + log::log(MSG(err) << "could not locate file-opener"); + return -1; + } - return subprocess::call({runner.c_str(), path, nullptr}, not background); + return subprocess::call({runner.c_str(), path, nullptr}, not background); #endif } -} // namespace openage +} // namespace openage::os diff --git a/libopenage/util/pty.h b/libopenage/util/pty.h index 0ae9e72d23..cb85f38865 100644 --- a/libopenage/util/pty.h +++ b/libopenage/util/pty.h @@ -3,14 +3,14 @@ #pragma once #ifdef __APPLE__ -#include + #include #elif defined(__FreeBSD__) -#include -#include -#include -#include + #include + #include + #include + #include #elif _WIN32 // TODO not yet implemented #else -#include + #include #endif diff --git a/libopenage/util/strings.h b/libopenage/util/strings.h index ad04e5267c..69f2759fb4 100644 --- a/libopenage/util/strings.h +++ b/libopenage/util/strings.h @@ -11,9 +11,9 @@ #include #if defined(__GNUC__) -#define ATTRIBUTE_FORMAT(i, j) __attribute__((format(printf, i, j))) + #define ATTRIBUTE_FORMAT(i, j) __attribute__((format(printf, i, j))) #else -#define ATTRIBUTE_FORMAT(i, j) + #define ATTRIBUTE_FORMAT(i, j) #endif namespace openage { diff --git a/libopenage/util/subprocess.cpp b/libopenage/util/subprocess.cpp index 98ae6720eb..c9a7670159 100644 --- a/libopenage/util/subprocess.cpp +++ b/libopenage/util/subprocess.cpp @@ -1,19 +1,19 @@ -// Copyright 2014-2019 the openage authors. See copying.md for legal info. +// Copyright 2014-2024 the openage authors. See copying.md for legal info. #include "subprocess.h" +#include #include #include -#include #ifdef _WIN32 // TODO not yet implemented #else -#include -#include -#include -#include -#include + #include + #include + #include + #include + #include #endif #include "../log/log.h" @@ -90,7 +90,7 @@ int call(const std::vector &argv, bool wait, const char *redirect_ if (redirect_stdout_to != nullptr) { replacement_stdout_fd = open( redirect_stdout_to, - O_WRONLY | O_CREAT|O_TRUNC|O_CLOEXEC, + O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); if (replacement_stdout_fd < 0) { @@ -215,8 +215,8 @@ int call(const std::vector &argv, bool wait, const char *redirect_ } if (child_errno > 0) { - log::log(MSG(err) << "execv has failed: " << strerror(child_errno)); - return -1; + log::log(MSG(err) << "execv has failed: " << strerror(child_errno)); + return -1; } } @@ -235,7 +235,7 @@ int call(const std::vector &argv, bool wait, const char *redirect_ // everything went well. return status; - #endif +#endif } } // namespace openage::subprocess diff --git a/libopenage/util/thread_id.cpp b/libopenage/util/thread_id.cpp index 0bcfc0abc9..3a178c441d 100644 --- a/libopenage/util/thread_id.cpp +++ b/libopenage/util/thread_id.cpp @@ -1,13 +1,13 @@ -// Copyright 2015-2018 the openage authors. See copying.md for legal info. +// Copyright 2015-2024 the openage authors. See copying.md for legal info. #include "thread_id.h" #include "config.h" #if HAVE_THREAD_LOCAL_STORAGE -#include + #include #else -#include + #include #endif namespace openage { @@ -23,9 +23,8 @@ namespace util { */ class ThreadIdSupplier { public: - ThreadIdSupplier() - : - val{ThreadIdSupplier::counting_id++} {} + ThreadIdSupplier() : + val{ThreadIdSupplier::counting_id++} {} const size_t val; @@ -42,12 +41,13 @@ std::atomic ThreadIdSupplier::counting_id{0}; #endif size_t get_current_thread_id() { - #if HAVE_THREAD_LOCAL_STORAGE +#if HAVE_THREAD_LOCAL_STORAGE static thread_local ThreadIdSupplier current_thread_id; return current_thread_id.val; - #else +#else return std::hash()(std::this_thread::get_id()); - #endif +#endif } -}} // namespace openage::util +} // namespace util +} // namespace openage diff --git a/libopenage/versions/versions.cpp b/libopenage/versions/versions.cpp index d2e1fd251a..29655b6db9 100644 --- a/libopenage/versions/versions.cpp +++ b/libopenage/versions/versions.cpp @@ -1,9 +1,9 @@ -// Copyright 2020-2023 the openage authors. See copying.md for legal info. +// Copyright 2020-2024 the openage authors. See copying.md for legal info. #include "versions.h" #ifdef __linux__ -#include + #include #endif #include