Init Project

This commit is contained in:
n0ffie 2025-04-09 14:33:59 +02:00
parent ad9c952b90
commit 0ca3c9cc64
7 changed files with 4141 additions and 0 deletions

6
.gitignore vendored
View file

@ -100,3 +100,9 @@ Module.symvers
Mkfile.old
dkms.conf
cmake-**
.idea
.vs
.vscode
build

56
CMakeLists.txt Normal file
View file

@ -0,0 +1,56 @@
cmake_minimum_required(VERSION 3.20)
project(cigus
VERSION 0.0.1
LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include(cmake/CPM.cmake)
include(cmake/utils.cmake)
CPMAddPackage(
NAME spdlog
GITHUB_REPOSITORY gabime/spdlog
VERSION 1.15.2
GIT_SHALLOW ON
EXCLUDE_FROM_ALL
)
CPMAddPackage(
NAME sfml
GITHUB_REPOSITORY SFML/SFML
GIT_TAG 3.0.0
GIT_SHALLOW ON
EXCLUDE_FROM_ALL
SYSTEM
)
CPMAddPackage(
NAME nlohmann_json
GITHUB_REPOSITORY nlohmann/json
VERSION 3.11.2
GIT_SHALLOW ON
EXCLUDE_FROM_ALL
)
set(PROJECT_SOURCE_NAME "${PROJECT_NAME}_SOURCES")
find_files(ExampleSources src cpp hpp c h cxx hxx)
add_executable(${PROJECT_NAME} ${ExampleSources} cigus.hpp)
target_include_directories(${PROJECT_NAME} PUBLIC include ./)
target_link_libraries(${PROJECT_NAME} PUBLIC
spdlog::spdlog
SFML::Graphics
SFML::Window
SFML::System
SFML::Audio
SFML::Network
nlohmann_json::nlohmann_json)

123
cigus.hpp Normal file
View file

@ -0,0 +1,123 @@
#pragma once
#include <vector>
#include <SFML/Graphics.hpp>
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>
namespace cigus {
using json = nlohmann::json;
template<typename T>
class Ref {
T *ptr;
public:
explicit Ref(T *ptr) : ptr(ptr) {
if (ptr == nullptr) {
spdlog::error("Ref<T> cannot be null");
throw std::runtime_error("Ref<T> cannot be null");
}
}
Ref(const Ref &other) : ptr(other.ptr) {
if (other.ptr == nullptr) {
spdlog::error("Ref<T> cannot be null");
throw std::runtime_error("Ref<T> cannot be null");
}
}
Ref(Ref &&other) noexcept : ptr(other.ptr) {
if (other.ptr == nullptr) {
spdlog::error("Ref<T> cannot be null");
throw std::runtime_error("Ref<T> cannot be null");
}
}
Ref &operator=(Ref &&other) noexcept {
if (this->ptr != other.ptr) {
ptr = other.ptr;
}
return *this;
}
T &operator*() {
return *ptr;
}
const T &operator*() const {
return *ptr;
}
T *operator->() {
return ptr;
}
const T *operator->() const {
return ptr;
}
void free() {
delete ptr;
ptr = nullptr;
}
};
}
namespace cigus::UI {
class RenderCall {
sf::Drawable *drawable;
sf::RenderStates states;
public:
RenderCall(sf::Drawable *drawable, sf::RenderStates states) : drawable(drawable), states(states) {
}
~RenderCall() {
delete drawable;
}
void draw(sf::RenderTarget &target, const sf::RenderStates &states) const {
target.draw(*drawable, states);
}
};
class View {
std::vector<RenderCall> m_RenderCalls;
protected:
void Rectangle() {
m_RenderCalls.emplace_back(new sf::RectangleShape(sf::Vector2f(100, 100)), sf::RenderStates());
}
public:
virtual ~View() = default;
[[nodiscard]] const std::vector<RenderCall> &renderCalls() const {
return m_RenderCalls;
}
virtual void body() = 0;
};
class Renderer {
public:
explicit Renderer(const Ref<View> &_view) : view(_view) { view->body(); }
void destroy() {
view.free();
}
Ref<View> view;
void update() {
view->body();
}
void render(sf::RenderTarget &target, const sf::RenderStates &states) const {
for (const auto &renderCall: view->renderCalls()) {
renderCall.draw(target, states);
}
}
};
}

1291
cmake/CPM.cmake Normal file

File diff suppressed because it is too large Load diff

33
cmake/utils.cmake Normal file
View file

@ -0,0 +1,33 @@
function(put_targets_into_folder)
set(oneValueArgs FOLDER)
set(multiValueArgs TARGETS)
cmake_parse_arguments(ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
foreach(target ${ARGS_TARGETS})
# Check if target exists
if (NOT TARGET ${target})
message(FATAL_ERROR "${target} target not found")
endif()
# Get the actual target (if it is aliased)
get_target_property(actual_target ${target} ALIASED_TARGET)
if (NOT actual_target)
set(actual_target ${target})
endif()
# Set the folder property for the target
set_target_properties(${actual_target} PROPERTIES FOLDER ${ARGS_FOLDER})
endforeach()
endfunction()
function(find_files var_name path)
set(sources)
foreach(ext ${ARGN})
file(GLOB_RECURSE files "${path}/*.${ext}")
list(APPEND sources ${files})
endforeach()
set(${var_name} ${${var_name}} ${sources} PARENT_SCOPE)
endfunction()

2589
include/argparse.hpp Normal file

File diff suppressed because it is too large Load diff

43
src/main.cpp Normal file
View file

@ -0,0 +1,43 @@
//
// Created by n0ffie on 08/04/25.
//
#include <iostream>
#include <cigus.hpp>
class NewView : public cigus::UI::View {
public:
void body() override {
std::cout << "Hello World!" << std::endl;
Rectangle();
}
};
int main(int argc, char** argv) {
namespace UI = cigus::UI;
sf::RenderWindow window(sf::VideoMode({800, 600}), "Hello World!");
window.setFramerateLimit(60);
UI::Renderer renderer(cigus::Ref<UI::View>(new NewView()));
while (window.isOpen()) {
while (const std::optional event = window.pollEvent())
{
if (event->is<sf::Event::Closed>())
{
window.close();
}
}
window.clear();
renderer.render(window, sf::RenderStates());
window.display();
}
renderer.destroy();
return 0;
}