diff --git a/include/rd7.hpp b/include/rd7.hpp index 4f0f898..3bc3159 100644 --- a/include/rd7.hpp +++ b/include/rd7.hpp @@ -2,4 +2,5 @@ #include #include -#include \ No newline at end of file +#include +#include \ No newline at end of file diff --git a/rd7tf/romfs/thesound.wav b/rd7tf/romfs/thesound.wav new file mode 100644 index 0000000..9cf19fe Binary files /dev/null and b/rd7tf/romfs/thesound.wav differ diff --git a/rd7tf/source/main.cpp b/rd7tf/source/main.cpp index ad93d7b..6069968 100644 --- a/rd7tf/source/main.cpp +++ b/rd7tf/source/main.cpp @@ -8,6 +8,8 @@ int main() { RenderD7::Ftrace::Beg("app", f2s(RenderD7::LoadSettings)); RenderD7::LoadSettings(); RenderD7::Ftrace::End("app", f2s(RenderD7::LoadSettings)); + sound snd("romfs:/thesound.wav", 0); + snd.play(); RenderD7::Ftrace::End("app", "app_init"); while (RenderD7::MainLoop()) { RenderD7::Ftrace::Beg("app", "app_mainloop"); diff --git a/source/NFontApi.cpp b/source/NFontApi.cpp index 9b50987..c93db35 100644 --- a/source/NFontApi.cpp +++ b/source/NFontApi.cpp @@ -6,6 +6,8 @@ #define STB_IMAGE_WRITE_IMPLEMENTATION #include +#include + RenderD7::NFontApi::NFontApi() {} RenderD7::NFontApi::~NFontApi() {} @@ -14,13 +16,16 @@ void RenderD7::NFontApi::LoadTTF(std::string path) { /////READ FILE unsigned char *buffer; long size = 0; - FILE *ttf__ = fopen(path.c_str(), "rb"); - fseek(ttf__, 0, SEEK_END); - size = ftell(ttf__); - fseek(ttf__, 0, SEEK_SET); + std::fstream ttf__(path, std::ios::in | std::ios::binary); + if(!ttf__.is_open()) { + return; // Add Error Handler in future + } + ttf__.seekg(0, std::ios::end); + size = ttf__.tellg(); + ttf__.seekg(0, std::ios::beg); buffer = (unsigned char *)malloc(size); - fread(buffer, size, 1, ttf__); - fclose(ttf__); + ttf__.read(reinterpret_cast(buffer), size); + ttf__.close(); /////Setup Font if (!stbtt_InitFont(&font, buffer, 0)) { printf("failed\n"); diff --git a/source/lang.cpp b/source/lang.cpp index 5e63f87..200af61 100644 --- a/source/lang.cpp +++ b/source/lang.cpp @@ -1,8 +1,7 @@ #include <3ds.h> #include -#include -#include -#include +#include +#include static nlohmann::json appJson; @@ -73,26 +72,27 @@ std::string RenderD7::Lang::get(const std::string &key) { } void RenderD7::Lang::load(const std::string &lang) { - FILE *values; + std::fstream values; - if (access(("romfs:/lang/" + lang + "/app.json").c_str(), F_OK) == 0) { - values = fopen(("romfs:/lang/" + lang + "/app.json").c_str(), "rt"); - if (values) { + if(std::filesystem::is_character_file("romfs:/lang/" + lang + "/app.json")) { + values.open("romfs:/lang/" + lang + "/app.json", std::ios::in); + if(values.is_open()) { appJson = nlohmann::json::parse(values); - fclose(values); } - if (appJson.is_discarded()) + values.close(); + if(appJson.is_discarded()) { appJson = {}; + } return; - } else { - values = fopen("romfs:/lang/en/app.json", "rt"); - if (values) { + values.open("romfs:/lang/en/app.json", std::ios::in); + if(values.is_open()) { appJson = nlohmann::json::parse(values); - fclose(values); } - if (appJson.is_discarded()) + values.close(); + if(appJson.is_discarded()) { appJson = {}; + } return; } } \ No newline at end of file diff --git a/source/log.cpp b/source/log.cpp index 0024612..24577cb 100644 --- a/source/log.cpp +++ b/source/log.cpp @@ -1,5 +1,6 @@ #include - +#include +#include #include std::string Log::format(const std::string &fmt_str, ...) { @@ -28,12 +29,12 @@ void Log::Init(const char *filename) { printf("%s\n", filename); std::string fn = filename; std::string name = fn + ".txt"; - this->filename = name.c_str(); - if ((access(name.c_str(), F_OK) == 0)) { - + this->filename = name; + if(std::filesystem::exists(name)) { + // Do nothing } else { - FILE *logfile = fopen((name.c_str()), "w"); - fclose(logfile); + std::fstream f(name, std::ios::out); + f.close(); } } diff --git a/source/renderd7.cpp b/source/renderd7.cpp index d8f2315..7eefce9 100644 --- a/source/renderd7.cpp +++ b/source/renderd7.cpp @@ -666,14 +666,7 @@ void RenderD7::DrawSTObject(std::vector tobject, } bool RenderD7::FS::FileExist(const std::string &path) { - FILE *test = fopen(path.c_str(), "r"); - if (test != NULL) { - fclose(test); - - return true; - } - - return false; + return std::filesystem::exists(path); } bool RenderD7::IsNdspInit() { diff --git a/source/sound.cpp b/source/sound.cpp index 96beb73..cee9b4c 100644 --- a/source/sound.cpp +++ b/source/sound.cpp @@ -1,9 +1,8 @@ #include -#include -#include -#include +#include #include +#include extern bool isndspinit; using std::string; @@ -31,19 +30,20 @@ sound::sound(const string &path, int channel, bool toloop) { ndspSetOutputCount(2); // Num of buffers // Reading wav file - FILE *fp = fopen(path.c_str(), "rb"); + std::fstream fp(path, std::ios::in | std::ios::binary); - if (!fp) { + if (!fp.is_open()) { printf("Could not open the WAV file: %s\n", path.c_str()); return; } WavHeader wavHeader; - size_t read = fread(&wavHeader, 1, sizeof(wavHeader), fp); + fp.read(reinterpret_cast(&wavHeader), sizeof(WavHeader)); + size_t read = fp.tellg(); if (read != sizeof(wavHeader)) { // Short read. printf("WAV file header is too short: %s\n", path.c_str()); - fclose(fp); + fp.close(); return; } @@ -52,7 +52,7 @@ sound::sound(const string &path, int channel, bool toloop) { if (memcmp(wavHeader.magic, RIFF_magic, sizeof(wavHeader.magic)) != 0) { // Incorrect magic number. printf("Wrong file format.\n"); - fclose(fp); + fp.close(); return; } @@ -61,19 +61,20 @@ sound::sound(const string &path, int channel, bool toloop) { (wavHeader.bits_per_sample != 8 && wavHeader.bits_per_sample != 16)) { // Unsupported WAV file. printf("Corrupted wav file.\n"); - fclose(fp); + fp.close(); return; } // Get the file size. - fseek(fp, 0, SEEK_END); - dataSize = ftell(fp) - sizeof(wavHeader); + fp.seekg(0, std::ios::end); + dataSize = fp.tellg(); + dataSize -= sizeof(WavHeader); // Allocating and reading samples data = static_cast(linearAlloc(dataSize)); - fseek(fp, 44, SEEK_SET); - fread(data, 1, dataSize, fp); - fclose(fp); + fp.seekg(44, std::ios::beg); + fp.read(reinterpret_cast(data), dataSize); + fp.close(); dataSize /= 2; // FIXME: 16-bit or stereo? // Find the right format