From ddf06eb18e7e8435e24440dd2d76400115aabddf Mon Sep 17 00:00:00 2001 From: tobid7 Date: Mon, 10 Jun 2024 19:50:12 +0200 Subject: [PATCH] Changes: Port Sheet and SptAnim to SMART_CTOR New Logger --- include/renderd7/{log.hpp => Log2.hpp} | 99 +++++++++++++------------- include/renderd7/Sheet.hpp | 2 + include/renderd7/Sprite.hpp | 2 +- include/renderd7/SpriteAnimation.hpp | 6 +- include/renderd7/renderd7.hpp | 3 + source/Log2.cpp | 60 ++++++++++++++++ source/Sprite.cpp | 2 +- source/SpriteSheetAnimation.cpp | 5 +- source/log.cpp | 70 ------------------ source/renderd7.cpp | 20 +++++- 10 files changed, 140 insertions(+), 129 deletions(-) rename include/renderd7/{log.hpp => Log2.hpp} (64%) create mode 100644 source/Log2.cpp delete mode 100644 source/log.cpp diff --git a/include/renderd7/log.hpp b/include/renderd7/Log2.hpp similarity index 64% rename from include/renderd7/log.hpp rename to include/renderd7/Log2.hpp index aa6d4f9..2068f70 100644 --- a/include/renderd7/log.hpp +++ b/include/renderd7/Log2.hpp @@ -1,50 +1,49 @@ -/** - * This file is part of RenderD7 - * Copyright (C) 2021-2024 NPI-D7, tobid7 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once -#include -#include - -#include - -/// @brief Log Class -class Log { - public: - /// @brief Constructor - Log(); - /// @brief Deconstructor - ~Log(); - /// @brief Init the Logger - /// @param filename Filename[_data_time.log] - void Init(const char *filename); - /// @brief Write a String to the File - /// @param debug_text string - void Write(std::string debug_text); - /// @brief Get the Date - /// @return Date as string fmt[data_time] - std::string logDate(void); - /// @brief Format a string like sprintf - /// @param fmt_str the string wich defines the fmt - /// @param ... Additional Data - /// @return Formatted String - std::string format(const std::string &fmt_str, ...); - - private: - /// \param filename the name of the logfile - std::string filename; -}; +/** + * This file is part of RenderD7 + * Copyright (C) 2021-2024 NPI-D7, tobid7 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include +#include +#include + +namespace RenderD7 { + +/// @brief Logger base Class +class LoggerBase { + public: + /// @brief Constructor + LoggerBase() = default; + /// @brief Deconstructor + ~LoggerBase(); + RD7_SMART_CTOR(LoggerBase) + /// @brief Init the Logger + /// @param filename name[_date_time.txt] + void Init(const std::string& name, bool fileless = false); + /// @brief Write a String to the File + /// @param debug_text string + void Write(const std::string& debug_text); + + private: + /// \param filename the name of the logfile + std::string filename; + std::string log_path; + std::ofstream _log; + std::vector lines; +}; +} // namespace RenderD7 \ No newline at end of file diff --git a/include/renderd7/Sheet.hpp b/include/renderd7/Sheet.hpp index 22132cb..b28bc01 100644 --- a/include/renderd7/Sheet.hpp +++ b/include/renderd7/Sheet.hpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace RenderD7 { @@ -33,6 +34,7 @@ class Sheet { ~Sheet() { if (spritesheet) Free(); } + RD7_SMART_CTOR(Sheet); /// @brief Load A Spritesheet File /// @param path Path to the t3x /// @return Result Code diff --git a/include/renderd7/Sprite.hpp b/include/renderd7/Sprite.hpp index 25a80b1..d770590 100644 --- a/include/renderd7/Sprite.hpp +++ b/include/renderd7/Sprite.hpp @@ -37,7 +37,7 @@ class Sprite { /// \brief Load a Sprite From SpriteSheet /// \param sheet the Sheet to load from.(RenderD7::Sheet) /// \param index the number of the Sprite in the Sheet - void FromSheet(RenderD7::Sheet *sheet, size_t index); + void FromSheet(RenderD7::Sheet::Ref sheet, size_t index); /// \brief Load a Sprite From SpriteSheet /// \param img the Image to load from.(RenderD7::Image) void FromImage(RenderD7::Image::Ref img); diff --git a/include/renderd7/SpriteAnimation.hpp b/include/renderd7/SpriteAnimation.hpp index c6690be..6c3bd46 100644 --- a/include/renderd7/SpriteAnimation.hpp +++ b/include/renderd7/SpriteAnimation.hpp @@ -23,6 +23,7 @@ #include #include +#include namespace RenderD7 { /// @brief SpriteSheetAnimation Class @@ -32,13 +33,14 @@ class SpriteSheetAnimation : public RenderD7::Sprite { SpriteSheetAnimation(); /// @brief Deconstructor ~SpriteSheetAnimation(); + RD7_SMART_CTOR(SpriteSheetAnimation); /// @brief Setup an Animation /// @param sheet Input Spritesheet /// @param imagecount Count of Images /// @param startimage Where to Start the Loop /// @param frame_begin Current Time (Should be 0) /// @param frame_finish Time Length - void Setup(RenderD7::Sheet *sheet, size_t imagecount, size_t startimage, + void Setup(RenderD7::Sheet::Ref sheet, size_t imagecount, size_t startimage, float frame_begin, float frame_finish); /// @brief Play the Animation /// @param timespeed Speed of the animation @@ -52,7 +54,7 @@ class SpriteSheetAnimation : public RenderD7::Sprite { /// @param D_totaltime Current Time float D_totaltime; /// @param sheet The Sheet of Images - RenderD7::Sheet *sheet; + RenderD7::Sheet::Ref sheet; /// @param time Total Time from frame_finish float time; }; diff --git a/include/renderd7/renderd7.hpp b/include/renderd7/renderd7.hpp index 851158e..56900b8 100644 --- a/include/renderd7/renderd7.hpp +++ b/include/renderd7/renderd7.hpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -69,6 +70,8 @@ extern bool rd7_enable_scene_system; namespace RenderD7 { // Reference to the New Renderer R2Base::Ref R2(); +// Reference to Global Logger +LoggerBase::Ref Logger(); /// @brief Get Deltatime /// @return Deltatime float GetDeltaTime(); diff --git a/source/Log2.cpp b/source/Log2.cpp new file mode 100644 index 0000000..bb49b01 --- /dev/null +++ b/source/Log2.cpp @@ -0,0 +1,60 @@ +/** + * This file is part of RenderD7 + * Copyright (C) 2021-2024 NPI-D7, tobid7 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +namespace RenderD7 { +LoggerBase::~LoggerBase() { + if (_log.is_open()) _log.close(); +} +void LoggerBase::Init(const std::string& name, bool fileless) { + if (!fileless) { + std::string path_base = RenderD7::GetAppDirectory() + "/logs/"; + if (!std::filesystem::is_directory(path_base)) { + std::filesystem::create_directories(path_base); + } + auto ts = RenderD7::GetTimeStr(); + std::string fn = name + ts + ".txt"; + this->filename = name; + this->log_path = path_base + name; + if (std::filesystem::exists(this->log_path)) { + // Do nothing + } else { + _log.open(this->log_path, std::ios::out); + } + } + this->Write("RenderD7 Log\n\n"); +} + +void LoggerBase::Write(const std::string& debug_text) { + std::string msg = "[" + RenderD7::GetTimeStr() + "]: " + debug_text; + if (this->_log.is_open()) { + this->_log << msg << std::endl; + } + while (msg.find_first_of('\n') != 0) { + lines.push_back(msg.substr(0, msg.find_first_of('\n'))); + msg = msg.substr(msg.find_first_of('\n')); + } + lines.push_back(msg); +} +} // namespace RenderD7 \ No newline at end of file diff --git a/source/Sprite.cpp b/source/Sprite.cpp index 946c36c..910ae7c 100644 --- a/source/Sprite.cpp +++ b/source/Sprite.cpp @@ -18,7 +18,7 @@ #include -void RenderD7::Sprite::FromSheet(RenderD7::Sheet *sheet, size_t index) { +void RenderD7::Sprite::FromSheet(RenderD7::Sheet::Ref sheet, size_t index) { C2D_SpriteFromSheet(&this->sprite, sheet->spritesheet, index); } bool RenderD7::Sprite::Draw() { diff --git a/source/SpriteSheetAnimation.cpp b/source/SpriteSheetAnimation.cpp index 06b9b12..2ed62cf 100644 --- a/source/SpriteSheetAnimation.cpp +++ b/source/SpriteSheetAnimation.cpp @@ -17,9 +17,6 @@ */ #include -#include - -extern Log renderd7log; RenderD7::SpriteSheetAnimation::SpriteSheetAnimation() { // @@ -28,7 +25,7 @@ RenderD7::SpriteSheetAnimation::~SpriteSheetAnimation() { // } -void RenderD7::SpriteSheetAnimation::Setup(RenderD7::Sheet *sheet, +void RenderD7::SpriteSheetAnimation::Setup(RenderD7::Sheet::Ref sheet, size_t imagecount, size_t startimage, float frame_begin, float frame_finish) { diff --git a/source/log.cpp b/source/log.cpp deleted file mode 100644 index 4dc5bc5..0000000 --- a/source/log.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/** - * This file is part of RenderD7 - * Copyright (C) 2021-2024 NPI-D7, tobid7 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include - -std::string Log::format(const std::string &fmt_str, ...) { - va_list ap; - char *fp = NULL; - va_start(ap, fmt_str); - vasprintf(&fp, fmt_str.c_str(), ap); - va_end(ap); - std::unique_ptr formatted(fp, free); - return std::string(formatted.get()); -} - -std::string Log::logDate(void) { - time_t unixTime; - struct tm timeStruct; - time(&unixTime); - localtime_r(&unixTime, &timeStruct); - return format("%04i-%02i-%02i_%02i-%02i-%02i", timeStruct.tm_year + 1900, - timeStruct.tm_mon + 1, timeStruct.tm_mday, timeStruct.tm_hour, - timeStruct.tm_min, timeStruct.tm_sec); -} - -Log::Log() {} - -void Log::Init(const char *filename) { - printf("%s\n", filename); - std::string fn = filename; - std::string name = fn + ".txt"; - this->filename = name; - if (std::filesystem::exists(name)) { - // Do nothing - } else { - std::fstream f(name, std::ios::out); - f.close(); - } -} - -void Log::Write(std::string debug_text) { - printf("%s\n", debug_text.c_str()); - std::ofstream logFile; - logFile.open((this->filename), std::ofstream::app); - std::string writeDebug = "["; - writeDebug += logDate(); - writeDebug += "] "; - writeDebug += debug_text.c_str(); - logFile << writeDebug << std::endl; - logFile.close(); -} -Log::~Log() {} diff --git a/source/renderd7.cpp b/source/renderd7.cpp index 10b9d1e..371e270 100644 --- a/source/renderd7.cpp +++ b/source/renderd7.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -34,6 +33,8 @@ #include RenderD7::R2Base::Ref rd7i_render2; +RenderD7::LoggerBase::Ref rd7i_logger; +RenderD7::LoggerBase::Ref rd7i_glogger; static void RD7i_ExitHook() { C2D_TextBufDelete(rd7i_text_buffer); @@ -240,6 +241,14 @@ RenderD7::R2Base::Ref RenderD7::R2() { return rd7i_render2; } +RenderD7::LoggerBase::Ref RenderD7::Logger() { + if (!rd7i_glogger) { + RenderD7::Error("Logger Was Called before being Init!"); + // return schould not be reached then + } + return rd7i_glogger; +} + float RenderD7::GetDeltaTime() { return (float)rd7i_dtm; } bool RenderD7::DrawImageFromSheet(RenderD7::Sheet *sheet, size_t index, float x, @@ -353,6 +362,9 @@ void RenderD7::Init::Graphics() { Result RenderD7::Init::Main(std::string app_name) { RenderD7::Ftrace::ScopedTrace st("rd7-core", f2s(Init::Main)); rd7i_app_name = app_name; + rd7i_logger = LoggerBase::New(); + rd7i_logger->Init("renderd7", true); + rd7i_glogger = LoggerBase::New(); gfxInitDefault(); atexit(gfxExit); @@ -373,6 +385,7 @@ Result RenderD7::Init::Main(std::string app_name) { auto ret = rd7i_soc_init(); if (ret) { + rd7i_logger->Write("Failed to Init Soc!"); RenderD7::PushMessage("RenderD7", "Failed to\nInit Soc!"); } else { atexit(rd7i_soc_deinit); @@ -413,6 +426,9 @@ Result RenderD7::Init::Main(std::string app_name) { Result RenderD7::Init::Minimal(std::string app_name) { RenderD7::Ftrace::ScopedTrace st("rd7-core", f2s(Init::Minimal)); rd7i_app_name = app_name; + rd7i_logger = LoggerBase::New(); + rd7i_logger->Init("renderd7", true); + rd7i_glogger = LoggerBase::New(); gfxInitDefault(); atexit(gfxExit); @@ -423,6 +439,7 @@ Result RenderD7::Init::Minimal(std::string app_name) { auto ret = rd7i_soc_init(); if (ret) { + rd7i_logger->Write("Failed to Init Soc!"); RenderD7::PushMessage("RenderD7", "Failed to\nInit Soc!"); } else { atexit(rd7i_soc_deinit); @@ -447,6 +464,7 @@ Result RenderD7::Init::Minimal(std::string app_name) { rd7i_d2_dimbuf = C2D_TextBufNew(4096); rd7i_base_font = C2D_FontLoadSystem(CFG_REGION_USA); rd7i_render2 = R2Base::New(); + rd7i_graphics_on = true; if (rd7_do_splash) PushSplash();