Skip to content

Commit

Permalink
Fixing up filesystem lib
Browse files Browse the repository at this point in the history
Working on vert color display
  • Loading branch information
EPICGameGuy committed Nov 27, 2023
1 parent 4e4fa8a commit fa88088
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 49 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

.vscode

build
build

dependencies/ps2gl
Binary file modified assets/models/kettle.fbx
Binary file not shown.
Binary file modified assets/models/testmap.fbx
Binary file not shown.
4 changes: 2 additions & 2 deletions dependencies/egg-library/include/egg/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
init_scr(); \
printf("ERROR! Check failed! file: %s, function: %s, line: %d\n", \
__FILE__, __func__, __LINE__); \
print_stack_trace(); \
init_scr(); \
scr_printf("ERROR! Check failed! file: %s, function: %s, line: %d\n", \
__FILE__, __func__, __LINE__); \
SleepThread(); \
Expand All @@ -32,7 +32,7 @@
printf("ERROR! Check failed! file: %s, function: %s, line: %d\n", \
__FILE__, __func__, __LINE__); \
printf("Check msg: %s\n", (msg)); \
print_stack_trace(); \
init_scr(); \
scr_printf("ERROR! Check failed! file: %s, function: %s, line: %d\n", \
__FILE__, __func__, __LINE__); \
scr_printf("Check msg: %s\n", (msg)); \
Expand Down
15 changes: 15 additions & 0 deletions dependencies/egg-library/include/egg/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>
#include <string_view>
#include <memory>
#include <functional>

namespace Filesystem
{
Expand Down Expand Up @@ -94,6 +95,8 @@ static constexpr const char* constexpr_convert_filepath_to_systempath(std::strin
// Returns true if the file was loaded succesfully
bool load_file(const Path& path, std::vector<std::byte>& out_bytes);
bool load_file(const Path& path, std::unique_ptr<std::byte[]>& out_bytes, size_t& size, size_t alignment = 1);
bool file_exists(const Path& path);
void iterate_dir(const Path& dir, std::function<void(const Path&)> itr_func, bool recursive = false);

struct Path
{
Expand All @@ -111,6 +114,18 @@ struct Path
}
}

Path(const char* in_path, bool convert_path = true)
{
if (convert_path)
{
_path_str = convert_filepath_to_systempath(in_path);
}
else
{
_path_str = in_path;
}
}

inline const char* c_str() const
{
return _path_str.c_str();
Expand Down
123 changes: 97 additions & 26 deletions dependencies/egg-library/src/filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string.h>
#include <fstream>
#include <memory>
#include <filesystem>

#include "egg/assert.hpp"

Expand All @@ -26,6 +27,41 @@ static constexpr std::size_t constexpr_strlen(const char* s)
return std::char_traits<char>::length(s);
}

static void convert_to_83_path(std::string_view path, char* buffer, size_t len)
{
// Iterate backwards and look for the beginning of the filename
size_t filename_begin = 0;
size_t extension_begin = path.size();
for (int i = path.size(); i >= 0; --i)
{
if (path[i] == '.')
{
extension_begin = i;
}
else if (path[i] == '/' || path[i] == '\\')
{
filename_begin = i + 1;
break;
}
}

// Copy up to the first 8 filename characters over
size_t itr_end = std::min(filename_begin + 8, extension_begin);
itr_end = std::min(itr_end, path.size());
for (size_t i = 0; i < itr_end; ++i)
{
buffer[i] = path[i];
}

// Copy the extension over
size_t itr_extension_end = std::min(4U, path.size() - extension_begin);
for (size_t i = 0; i < itr_extension_end; ++i)
{
buffer[itr_end + i] = path[extension_begin + i];
}
buffer[itr_end + itr_extension_end] = '\0';
}

template <Type type>
static std::string convert_filepath_to_systempath_impl(std::string_view path)
{
Expand All @@ -36,9 +72,6 @@ static std::string convert_filepath_to_systempath_impl(std::string_view path)

static char out_path_chars[256];

// snprintf(out_path_chars, host_string_length + 1, "%s%c%.*s",
// get_filesystem_prefix(type), get_filesystem_separator(type), path.length(), path.data());

constexpr const char* filesystem_prefix = get_filesystem_prefix(type);
constexpr int filesystem_prefix_len = constexpr_strlen(filesystem_prefix);

Expand All @@ -50,39 +83,49 @@ static std::string convert_filepath_to_systempath_impl(std::string_view path)

int index = filesystem_prefix_len;

if (path[0] != '\\' || path[0] != '/')
int path_start = 0;
if constexpr (type != Type::host)
{
out_path_chars[index] = get_filesystem_separator();
index++;
}

// Write out the filename
int filename_start_index = -1;
int extension_start_index = -1;
for (size_t i = 0; i < host_string_length; ++i)
{
if (path[i] == '\\' || path[i] == '/')
if (path[0] != '\\' && path[0] != '/')
{
out_path_chars[index + i] = get_filesystem_separator(type);

filename_start_index = i + 1;
out_path_chars[index] = get_filesystem_separator();
index++;
}
else if (path[i] == '-')
}
else
{
// iterate past the start of the path on host filesystem
while (path[path_start] == '\\' || path[path_start] == '/')
{
out_path_chars[index + i] = '_';
path_start++;
}
else if (get_filesystem_type() == Type::cdrom)
}

path = std::string_view(path.data() + path_start, path.end());

if constexpr (type == Type::cdrom)
{
convert_to_83_path(path, out_path_chars + filesystem_prefix_len, (256 - filesystem_prefix_len) - 1);
}
else
{
strncpy(out_path_chars + filesystem_prefix_len, path.data(), (256 - filesystem_prefix_len) - 1);
}

// Convert to uppercase, etc
for (size_t i = filesystem_prefix_len - 1; i < host_string_length; ++i)
{
if (out_path_chars[i] == '\\' || out_path_chars[i] == '/')
{
out_path_chars[index + i] = toupper(path[i]);
out_path_chars[i] = get_filesystem_separator(type);
}
else
else if (out_path_chars[i] == '-')
{
out_path_chars[index + i] = path[i];
out_path_chars[i] = '_';
}

if (path[i] == '.')
else if (get_filesystem_type() == Type::cdrom)
{
extension_start_index = i + 1;
out_path_chars[i] = toupper(out_path_chars[i]);
}
}

Expand Down Expand Up @@ -150,6 +193,34 @@ bool load_file(const Path& path, std::unique_ptr<std::byte[]>& out_bytes, size_t
return false;
}

bool file_exists(const Path& path)
{
return std::filesystem::exists(path.c_str());
}

void iterate_dir(const Path& dir, std::function<void(const Path&)> itr_func, bool recursive)
{
checkf(itr_func, "iterate_dir called with empty itr_func!");

namespace fs = std::filesystem;
if (recursive)
{
for (const fs::directory_entry& dir_entry :
fs::recursive_directory_iterator(dir.c_str()))
{
itr_func(Path(dir_entry.path().c_str(), false));
}
}
else
{
for (const fs::directory_entry& dir_entry :
fs::directory_iterator(dir.c_str()))
{
itr_func(Path(dir_entry.path().c_str(), false));
}
}
}

void run_tests()
{
switch (get_filesystem_type())
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ $(OUTPUT_DIR)/$(VU_OUTPUT_DIR)/%.vsm:
$(PS2_VCL) -o$@ $(dir $@)$*.pp.vcl

run: $(EE_BIN)
ps2client -h $(PS2HOST) execee host:$(EE_BIN)
cd $(dir $(EE_BIN))
ps2client -h $(PS2HOST) execee host:$(notdir $(EE_BIN))

reset:
ps2client -h $(PS2HOST) reset
25 changes: 14 additions & 11 deletions src/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,25 @@ static u32 frameCounter = 0;

void init()
{
SifExitIopHeap();
SifLoadFileExit();
SifExitRpc();
Filesystem::set_filesystem_type(Filesystem::Type::host);

SifInitRpc(0);
if (Filesystem::get_filesystem_type() != Filesystem::Type::host)
{
SifExitIopHeap();
SifLoadFileExit();
SifExitRpc();

while (!SifIopReset("", 0))
;
while (!SifIopSync())
;
SifInitRpc(0);

while (!SifIopReset("", 0))
;
while (!SifIopSync())
;
}
SifInitRpc(0);
SifLoadFileInit();
SifInitIopHeap();

Filesystem::set_filesystem_type(Filesystem::Type::cdrom);

check(SifLoadModule("rom0:LIBSD", 0, NULL) > 0);
check(SifLoadModule("rom0:SIO2MAN", 0, NULL) > 0);

Expand All @@ -72,7 +75,7 @@ void init()
Stats::init();
Input::init();
//Filesystem::run_tests();
//sound::init();
//Sound::init();
GS::init();

printf("Graph mode (region): ");
Expand Down
2 changes: 1 addition & 1 deletion src/objects/teapot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static class teapot_render_proxy
void on_gs_init()
{
teapot_mesh = new Mesh("/assets/models/kettle.mdl"_p); //&Mesh::loaded_meshes["/assets/models/kettle.ps2_model"_p];
//teapot_mesh = nullptr;
//teapot_mesh = nullptr;
teapot_mesh->compile();
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/gs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static void init_renderer()

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
//glEnableClientState(GL_COLOR_ARRAY);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);


Expand Down
8 changes: 4 additions & 4 deletions src/renderer/mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void Mesh::compile()
check(num_lists <= 4096);
glNewList(list, GL_COMPILE);
{
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
//glEnable(GL_COLOR_MATERIAL);
//glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
//glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
//static float material_diff_amb[] = {0.5f, 0.5f, 0.5f, 0};
//glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material_diff_amb);
Expand All @@ -81,13 +81,13 @@ void Mesh::compile()

if (mesh->uvs.offset > 0)
{
glTexCoordPointer(2, GL_FLOAT, 0, mesh->uvs.get_ptr());
//glTexCoordPointer(2, GL_FLOAT, 0, mesh->uvs.get_ptr());
}

if (mesh->colors.offset > 0)
{
printf("Mesh: %d has colors\n", list);
glColorPointer(4, GL_FLOAT, 0, mesh->colors.get_ptr());
//glColorPointer(4, GL_FLOAT, 0, mesh->colors.get_ptr());
}

int i = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/sound/sound.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void init()
{
int ret = SifLoadModule("AUDSRV.IRX"_p.c_str(), 0, nullptr);
printf("ret: %d\n", ret);
check(ret >= 0);
checkf(ret >= 0, "AUDSRV.IRX"_p.c_str());
}

printf("initializing audiosrv...\n");
Expand Down Expand Up @@ -102,7 +102,7 @@ void init()
}
}

song_file = std::ifstream("/assets/sounds/white_wa.wav"_p.c_str());
song_file = std::ifstream("/assets/sounds/white_waking.wav"_p.c_str());
check(song_file.is_open());
song_file.seekg(0x30, std::ios_base::beg);
chunkReadStatus = -1;
Expand Down

0 comments on commit fa88088

Please sign in to comment.