From 0b9309c62df219aabbe9bf5c1da43433c29fb6bb Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Fri, 13 Dec 2024 00:21:09 +0100 Subject: [PATCH 01/35] Bump v0.2 && Changed Debug/MinSizeRel rt library --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 038229e..e509413 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.15) -project(OpenJones3D VERSION 0.1.0) +project(OpenJones3D VERSION 0.2.0) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) @@ -18,7 +18,7 @@ configure_file( ) # Set global property for MSVC runtime library (CMake 3.15+) -set(CMAKE_MSVC_RUNTIME_LIBRARY "$<$:MultiThreaded>$<$:MultiThreadedDebug>") +set(CMAKE_MSVC_RUNTIME_LIBRARY "$<$:MultiThreaded>$<$:MultiThreaded>") if (MSVC) add_compile_definitions("__BASE_FILE__=\"%(Filename)%(Extension)\"") From dab85a6345bf745fc1fdd761866c2749787c0cb9 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Fri, 13 Dec 2024 00:21:29 +0100 Subject: [PATCH 02/35] Create .editorconfig --- .editorconfig | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a85be56 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,85 @@ +# Visual Studio generated .editorconfig file with C++ settings. +root = true + +[*.{c,c++,cc,cpp,cppm,cu,cuh,cxx,h,h++,hh,hpp,hxx,inl,ipp,ixx,tlh,tli}] + +# Visual C++ Code Style settings + +cpp_generate_documentation_comments = doxygen_slash_star + +# Visual C++ Formatting settings + +cpp_indent_braces = false +cpp_indent_multi_line_relative_to = innermost_parenthesis +cpp_indent_within_parentheses = indent +cpp_indent_preserve_within_parentheses = false +cpp_indent_case_contents = true +cpp_indent_case_labels = true +cpp_indent_case_contents_when_block = false +cpp_indent_lambda_braces_when_parameter = false +cpp_indent_goto_labels = one_left +cpp_indent_preprocessor = one_left +cpp_indent_access_specifiers = false +cpp_indent_namespace_contents = true +cpp_indent_preserve_comments = true + +cpp_new_line_before_open_brace_namespace = ignore +cpp_new_line_before_open_brace_type = ignore +cpp_new_line_before_open_brace_function = ignore +cpp_new_line_before_open_brace_block = ignore +cpp_new_line_before_open_brace_lambda = ignore +cpp_new_line_scope_braces_on_separate_lines = true +cpp_new_line_close_brace_same_line_empty_type = true +cpp_new_line_close_brace_same_line_empty_function = true +cpp_new_line_before_catch = true +cpp_new_line_before_else = true +cpp_new_line_before_while_in_do_while = false + +cpp_space_before_function_open_parenthesis = remove +cpp_space_within_parameter_list_parentheses = false +cpp_space_between_empty_parameter_list_parentheses = false +cpp_space_after_keywords_in_control_flow_statements = true +cpp_space_within_control_flow_statement_parentheses = true +cpp_space_before_lambda_open_parenthesis = false +cpp_space_within_cast_parentheses = false +cpp_space_after_cast_close_parenthesis = false +cpp_space_within_expression_parentheses = false +cpp_space_before_block_open_brace = true +cpp_space_between_empty_braces = false +cpp_space_before_initializer_list_open_brace = false +cpp_space_within_initializer_list_braces = true +cpp_space_preserve_in_initializer_list = true +cpp_space_before_open_square_bracket = false +cpp_space_within_square_brackets = false +cpp_space_before_empty_square_brackets = false +cpp_space_between_empty_square_brackets = false +cpp_space_group_square_brackets = true +cpp_space_within_lambda_brackets = false +cpp_space_between_empty_lambda_brackets = false +cpp_space_before_comma = false +cpp_space_after_comma = true +cpp_space_remove_around_member_operators = true +cpp_space_before_inheritance_colon = true +cpp_space_before_constructor_colon = true +cpp_space_remove_before_semicolon = true +cpp_space_after_semicolon = true +cpp_space_remove_around_unary_operator = true +cpp_space_around_binary_operator = insert +cpp_space_around_assignment_operator = ignore +cpp_space_pointer_reference_alignment = left +cpp_space_around_ternary_operator = insert + +cpp_use_unreal_engine_macro_formatting = false +cpp_wrap_preserve_blocks = one_liners + +# Visual C++ Inlcude Cleanup settings + +cpp_include_cleanup_add_missing_error_tag_type = suggestion +cpp_include_cleanup_remove_unused_error_tag_type = dimmed +cpp_include_cleanup_optimize_unused_error_tag_type = suggestion +cpp_include_cleanup_sort_after_edits = true +cpp_sort_includes_error_tag_type = suggestion +cpp_sort_includes_priority_case_sensitive = false +cpp_sort_includes_priority_style = quoted +cpp_includes_style = default +cpp_includes_use_forward_slash = true From 3b800adc8e845ec9d99ad5a4799e365f2fc12a08 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Fri, 13 Dec 2024 23:36:51 +0100 Subject: [PATCH 03/35] [rdroid] Fix floating point precision bug && Update code --- Libs/rdroid/Math/rdMatrix.c | 304 +++++------------------------------- Libs/rdroid/Math/rdVector.c | 124 +++++++-------- Libs/rdroid/Math/rdVector.h | 17 +- 3 files changed, 101 insertions(+), 344 deletions(-) diff --git a/Libs/rdroid/Math/rdMatrix.c b/Libs/rdroid/Math/rdMatrix.c index 926cdce..2906cad 100644 --- a/Libs/rdroid/Math/rdMatrix.c +++ b/Libs/rdroid/Math/rdMatrix.c @@ -50,16 +50,11 @@ void rdMatrix_ResetGlobals(void) void J3DAPI rdMatrix_Build34(rdMatrix34* res, const rdVector3* pyr, const rdVector3* pos) { - float yCos; - float zSin; - float xSin; - float zCos; - float xCos; - float ySin; - - stdMath_SinCos(pyr->x, &xSin, &xCos); - stdMath_SinCos(pyr->y, &ySin, &yCos); - stdMath_SinCos(pyr->z, &zSin, &zCos); + float xSin, ySin, zSin; + float xCos, yCos, zCos; + stdMath_SinCos(pyr->pitch, &xSin, &xCos); + stdMath_SinCos(pyr->yaw, &ySin, &yCos); + stdMath_SinCos(pyr->roll, &zSin, &zCos); float yzCos = yCos * zCos; float yzSin = ySin * zSin; @@ -78,13 +73,13 @@ void J3DAPI rdMatrix_Build34(rdMatrix34* res, const rdVector3* pyr, const rdVect res->uvec.y = -xSin * yzCos + yzSin; res->uvec.z = zCos * xCos; - memcpy(&res->dvec, pos, sizeof(res->dvec)); + res->dvec = *pos; } void J3DAPI rdMatrix_BuildFromLook34(rdMatrix34* mat, const rdVector3* look) { RD_ASSERTREL(mat && look); - memcpy(&mat->lvec, look, sizeof(mat->lvec)); + mat->lvec = *look; //mat->rvec.x = mat->lvec.y * 1.0f - mat->lvec.z * 0.0f; //mat->rvec.y = mat->lvec.z * 0.0f - mat->lvec.x * 1.0f; @@ -116,10 +111,6 @@ void J3DAPI rdMatrix_BuildFromLook34(rdMatrix34* mat, const rdVector3* look) void J3DAPI rdMatrix_InvertOrtho34(rdMatrix34* dest, const rdMatrix34* src) { - float z; - float y; - float x; - dest->rvec.x = src->rvec.x; dest->rvec.y = src->lvec.x; dest->rvec.z = src->uvec.x; @@ -132,9 +123,10 @@ void J3DAPI rdMatrix_InvertOrtho34(rdMatrix34* dest, const rdMatrix34* src) dest->uvec.y = src->lvec.z; dest->uvec.z = src->uvec.z; - x = src->dvec.x; - y = src->dvec.y; - z = src->dvec.z; + float x = src->dvec.x; + float y = src->dvec.y; + float z = src->dvec.z; + dest->dvec.x = -(x * src->rvec.x + src->rvec.y * y + src->rvec.z * z); dest->dvec.y = -(src->lvec.x * x + src->lvec.y * y + src->lvec.z * z); dest->dvec.z = -(src->uvec.x * x + src->uvec.y * y + src->uvec.z * z); @@ -142,25 +134,16 @@ void J3DAPI rdMatrix_InvertOrtho34(rdMatrix34* dest, const rdMatrix34* src) void J3DAPI rdMatrix_BuildRotate34(rdMatrix34* mat, const rdVector3* pyr) { - float yCos; - float zSin; - float xSin; - float yzSin; - float v6; - float v7; - float yzCos; - float zCos; - float xCos; - float ySin; - + float xSin, ySin, zSin; + float xCos, yCos, zCos; stdMath_SinCos(pyr->x, &xSin, &xCos); stdMath_SinCos(pyr->y, &ySin, &yCos); stdMath_SinCos(pyr->z, &zSin, &zCos); - yzCos = yCos * zCos; - yzSin = ySin * zSin; - v6 = yCos * zSin; - v7 = ySin * zCos; + float yzCos = yCos * zCos; + float yzSin = ySin * zSin; + float v6 = yCos * zSin; + float v7 = ySin * zCos; mat->rvec.x = -yzSin * xSin + yzCos; mat->rvec.y = v6 * xSin + v7; @@ -179,8 +162,8 @@ void J3DAPI rdMatrix_BuildRotate34(rdMatrix34* mat, const rdVector3* pyr) void J3DAPI rdMatrix_BuildTranslate34(rdMatrix34* mat, const rdVector3* vec) { - memcpy(mat, &rdroid_g_identMatrix34, sizeof(rdMatrix34)); - memcpy(&mat->dvec, vec, sizeof(mat->dvec)); + *mat = rdroid_g_identMatrix34; + mat->dvec = *vec; } void J3DAPI rdMatrix_BuildScale34(rdMatrix34* mat, const rdVector3* scale) @@ -200,94 +183,6 @@ void J3DAPI rdMatrix_BuildScale34(rdMatrix34* mat, const rdVector3* scale) memset(&mat->dvec, 0, sizeof(mat->dvec)); } -// TODO: test throughly rdMatrix_BuildFromVectorAngle34 function before removing this function -void J3DAPI rdMatrix_BuildFromVectorAngle34_old(rdMatrix34* mat, const rdVector3* vec, float angle) -{ - float v3; - float v4; - float v5; - float v6; - float v7; - float v8; - float v9; - float v10; - float v11; - float v12; - float zy; - float angCos; - float zx; - float angSin; - float v17; - float v18; - float yx; - - stdMath_SinCos(angle, &angSin, &angCos); - if ( vec->z < 1.0f ) - { - if ( vec->z > -1.0f ) - { - v17 = 1.0f - angCos; // 2*sin(angle/2)^2 - - yx = vec->y * vec->x; - zy = vec->z * vec->y; - zx = vec->z * vec->x; - - v7 = angSin * vec->x; - v6 = vec->y * angSin; - v4 = vec->z * angSin; - v11 = vec->x * vec->x; - v5 = vec->y * vec->y; - v18 = 1.0f - v11 - v5; - v12 = 1.0f - v18; - v9 = angCos * v11; - v3 = angCos * v5; - v10 = v17 * zx; - v8 = v17 * zy; - mat->rvec.x = (v9 * v18 + v3) / v12 + v11; - mat->lvec.y = (v3 * v18 + v9) / v12 + v5; - mat->uvec.z = v18 + v9 + v3; - mat->rvec.y = v17 * yx + v4; - mat->lvec.x = v17 * yx - v4; - mat->rvec.z = v10 - v6; - mat->lvec.z = v8 + v7; - mat->uvec.x = v10 + v6; - mat->uvec.y = v8 - v7; - } - else - { - mat->rvec.x = angCos; - mat->rvec.y = -angSin; - mat->rvec.z = 0.0f; - - mat->lvec.x = angSin; - mat->lvec.y = angCos; - mat->lvec.z = 0.0f; - - mat->uvec.x = 0.0f; - mat->uvec.y = 0.0f; - mat->uvec.z = 1.0f; - } - } - else - { - mat->rvec.x = angCos; - mat->rvec.y = angSin; - mat->rvec.z = 0.0f; - - mat->lvec.x = -angSin; - mat->lvec.y = angCos; - mat->lvec.z = 0.0f; - - mat->uvec.x = 0.0f; - mat->uvec.y = 0.0f; - mat->uvec.z = 1.0f; - } - - mat->dvec.x = 0.0f; - mat->dvec.y = 0.0f; - mat->dvec.z = 0.0f; -} - void J3DAPI rdMatrix_BuildFromVectorAngle34(rdMatrix34* mat, const rdVector3* vec, float angle) { float sinAngle, cosAngle; @@ -305,10 +200,10 @@ void J3DAPI rdMatrix_BuildFromVectorAngle34(rdMatrix34* mat, const rdVector3* ve float xs = vec->x * sinAngle; float ys = vec->y * sinAngle; float zs = vec->z * sinAngle; - float invZZ = 1.0f / (1.0f - zz); - mat->rvec.x = (cosAngle * xx * zz + cosAngle * yy) * invZZ + xx; - mat->lvec.y = (cosAngle * yy * zz + cosAngle * xx) * invZZ + yy; + double invZZ = 1.0 / (1.0 - (double)zz); + mat->rvec.x = (float)((cosAngle * xx * zz + cosAngle * yy) * invZZ) + xx; + mat->lvec.y = (float)((cosAngle * yy * zz + cosAngle * xx) * invZZ) + yy; mat->uvec.z = zz + cosAngle * xx + cosAngle * yy; mat->rvec.y = oneMinusCos * xy + zs; @@ -377,116 +272,6 @@ void J3DAPI rdMatrix_LookAt(rdMatrix34* mat, const rdVector3* eyePos, const rdVe //memcpy(&mat->dvec, eyePos, sizeof(mat->dvec)); } -void J3DAPI rdMatrix_ExtractAngles34_old(const rdMatrix34* mat, rdVector3* pyr) -{ - double v2; - double v3; - double v4; - float v5; - float v6; - float v7; - float v8; - float v9; - float v10; - float v11; - float v12; - float v13; - float v14; - float v15; - rdVector3 v16; - float v17; - float v18; - float v19; - float v20; - float v21; - - rdVector_Copy3(&v16, &mat->lvec); - v9 = -mat->rvec.x; - v10 = -mat->rvec.y; - v11 = -mat->rvec.z; - - v5 = v16.x * v16.x + v16.y * v16.y + 0.0f * 0.0f; - v2 = sqrtf(v5); - v15 = v2; - if ( v2 >= 0.001f ) - { - v7 = v16.y / v15; - v18 = 90.0f - stdMath_ArcSin3(v7); - if ( v16.x > 0.0f ) - { - v18 = -v18; - } - - pyr->y = v18; - } - else - { - v6 = -v9; - v17 = 90.0f - stdMath_ArcSin3(v6); - if ( v10 > 0.0f && v16.z > 0.0f || v10 < 0.0f && v16.z < 0.0f ) - { - v17 = -v17; - } - - pyr->z = v17; - pyr->y = 0.0f; - } - - if ( v15 >= 0.001f ) - { - v3 = (v16.x * v16.x + v16.y * v16.y + 0.0f * v16.z) / v15; - if ( v3 < 1.0f ) - { - v12 = v3; - v19 = 90.0f - stdMath_ArcSin3(v12); - pyr->x = v19; - } - else - { - pyr->x = 0.0f; - } - } - else - { - pyr->x = 90.0f; - } - - if ( v16.z < 0.0f ) - { - pyr->x = -pyr->x; - } - - v21 = -v16.y; - v8 = v21 * v21 + v16.x * v16.x + 0.0f * 0.0f; - v14 = sqrtf(v8); - if ( v15 >= 0.001f ) - { - v4 = (v21 * v9 + v16.x * v10 + 0.0f * v11) / v14; - v13 = v4; - if ( v4 < 1.0f ) - { - if ( v13 > -1.0f ) - { - v20 = 90.0f - stdMath_ArcSin3(v13); - pyr->z = v20; - } - else - { - pyr->z = 180.0f; - } - } - else - { - pyr->z = 0.0f; - } - - if ( v11 < 0.0f ) - { - pyr->z = -pyr->z; - } - } -} - void J3DAPI rdMatrix_ExtractAngles34(const rdMatrix34* mat, rdVector3* pyr) { float negRightX = -mat->rvec.x; @@ -495,12 +280,12 @@ void J3DAPI rdMatrix_ExtractAngles34(const rdMatrix34* mat, rdVector3* pyr) rdVector3 vecFwd; rdVector_Copy3(&vecFwd, &mat->lvec); - float fwdLenXY = sqrtf(vecFwd.x * vecFwd.x + vecFwd.y * vecFwd.y); + double fwdLenXY = sqrt(vecFwd.x * vecFwd.x + vecFwd.y * vecFwd.y); // Calculate Yaw (Y) - if ( fwdLenXY >= 0.001f ) + if ( fwdLenXY >= 0.001 ) { - float sinYaw = vecFwd.y / fwdLenXY; + float sinYaw = (float)(vecFwd.y / fwdLenXY); pyr->yaw = 90.0f - stdMath_ArcSin3(sinYaw); if ( vecFwd.x > 0.0f ) { pyr->yaw = -pyr->yaw; @@ -518,9 +303,9 @@ void J3DAPI rdMatrix_ExtractAngles34(const rdMatrix34* mat, rdVector3* pyr) // Calculate Pitch (X) pyr->pitch = 90.0f; - if ( fwdLenXY >= 0.001f ) + if ( fwdLenXY >= 0.001 ) { - float sinPitch = (vecFwd.x * vecFwd.x + vecFwd.y * vecFwd.y) / fwdLenXY; + float sinPitch = (float)((vecFwd.x * vecFwd.x + vecFwd.y * vecFwd.y) / fwdLenXY); if ( sinPitch < 1.0f ) { pyr->pitch = 90.0f - stdMath_ArcSin3(sinPitch); } @@ -535,12 +320,12 @@ void J3DAPI rdMatrix_ExtractAngles34(const rdMatrix34* mat, rdVector3* pyr) // Calculate Roll (Z) float negUpY = -vecFwd.y; - float upXYLen = sqrtf(negUpY * negUpY + vecFwd.x * vecFwd.x); + double upXYLen = sqrt(negUpY * negUpY + vecFwd.x * vecFwd.x); - if ( fwdLenXY >= 0.001f ) + if ( fwdLenXY >= 0.001 ) { pyr->roll = 0.0f; - float sinRoll = (negUpY * negRightX + vecFwd.x * negRightY) / upXYLen; + float sinRoll = (float)((negUpY * negRightX + vecFwd.x * negRightY) / upXYLen); if ( sinRoll < 1.0f ) { if ( sinRoll > -1.0f ) { @@ -555,22 +340,6 @@ void J3DAPI rdMatrix_ExtractAngles34(const rdMatrix34* mat, rdVector3* pyr) pyr->roll = -pyr->roll; } } - - // TEST scope - // TODO: Remove - #ifdef J3D_DEBUG - rdVector3 pyr2; - rdMatrix_ExtractAngles34_old(mat, &pyr2); - if ( ((pyr2.x != pyr->x) && (!isnan(pyr2.x) || !isnan(pyr->x))) - || ((pyr2.y != pyr->y) && (!isnan(pyr2.y) || !isnan(pyr->y))) - || ((pyr2.z != pyr->z) && (!isnan(pyr2.z) || !isnan(pyr->z))) - ) - { - RDLOG_ERROR("PYR p=%.f y%.f r=%.f op=%.f oy=%.f or=%.f\n", pyr2.x, pyr2.y, pyr2.z, pyr->x, pyr->y, pyr->z); - //RDLOG_FATAL("pyr2.x == pyr->x && pyr2.y == pyr->y && pyr2.z == pyr->z"); - } - #endif - // TEST scope remove } void J3DAPI rdMatrix_Normalize34(rdMatrix34* mat) @@ -578,8 +347,8 @@ void J3DAPI rdMatrix_Normalize34(rdMatrix34* mat) mat->uvec.x = mat->rvec.y * mat->lvec.z - mat->rvec.z * mat->lvec.y; mat->uvec.y = mat->rvec.z * mat->lvec.x - mat->lvec.z * mat->rvec.x; mat->uvec.z = mat->rvec.x * mat->lvec.y - mat->rvec.y * mat->lvec.x; - rdVector_Normalize3Acc(&mat->lvec); + rdVector_Normalize3Acc(&mat->lvec); rdVector_Normalize3Acc(&mat->uvec); mat->rvec.x = mat->lvec.y * mat->uvec.z - mat->lvec.z * mat->uvec.y; @@ -587,7 +356,7 @@ void J3DAPI rdMatrix_Normalize34(rdMatrix34* mat) mat->rvec.z = mat->lvec.x * mat->uvec.y - mat->lvec.y * mat->uvec.x; } -void __cdecl rdMatrix_Multiply34(rdMatrix34* dest, const rdMatrix34* a, const rdMatrix34* b) +void J3DAPI rdMatrix_Multiply34(rdMatrix34* dest, const rdMatrix34* a, const rdMatrix34* b) { RD_ASSERTREL(((dest != a) && (dest != b))); @@ -610,8 +379,8 @@ void __cdecl rdMatrix_Multiply34(rdMatrix34* dest, const rdMatrix34* a, const rd void J3DAPI rdMatrix_PreMultiply34(rdMatrix34* a, const rdMatrix34* b) { - rdMatrix34 acpy = { 0 }; - memcpy(&acpy, a, sizeof(acpy)); + rdMatrix34 acpy = *a; + a->rvec.x = acpy.rvec.x * b->rvec.x + b->rvec.y * acpy.lvec.x + b->rvec.z * acpy.uvec.x; a->rvec.y = acpy.rvec.y * b->rvec.x + b->rvec.y * acpy.lvec.y + b->rvec.z * acpy.uvec.y; a->rvec.z = acpy.rvec.z * b->rvec.x + b->rvec.y * acpy.lvec.z + b->rvec.z * acpy.uvec.z; @@ -631,8 +400,7 @@ void J3DAPI rdMatrix_PreMultiply34(rdMatrix34* a, const rdMatrix34* b) void J3DAPI rdMatrix_PostMultiply34(rdMatrix34* a, const rdMatrix34* b) { - rdMatrix34 acpy; - memcpy(&acpy, a, sizeof(acpy)); + rdMatrix34 acpy = *a; a->rvec.x = b->rvec.x * acpy.rvec.x + b->lvec.x * acpy.rvec.y + b->uvec.x * acpy.rvec.z; a->rvec.y = b->rvec.y * acpy.rvec.x + b->lvec.y * acpy.rvec.y + b->uvec.y * acpy.rvec.z; @@ -713,7 +481,7 @@ void J3DAPI rdMatrix_TransformVector34Acc(rdVector3* dest, const rdMatrix34* mat vec.x = mat->rvec.x * dest->x + mat->lvec.x * dest->y + mat->uvec.x * dest->z; vec.y = mat->rvec.y * dest->x + mat->lvec.y * dest->y + mat->uvec.y * dest->z; vec.z = mat->rvec.z * dest->x + mat->lvec.z * dest->y + mat->uvec.z * dest->z; - memcpy(dest, &vec, sizeof(rdVector3)); + rdVector_Copy3(dest, &vec); } void J3DAPI rdMatrix_TransformPoint34(rdVector3* dest, const rdVector3* src, const rdMatrix34* mat) diff --git a/Libs/rdroid/Math/rdVector.c b/Libs/rdroid/Math/rdVector.c index c99a7bb..82155f8 100644 --- a/Libs/rdroid/Math/rdVector.c +++ b/Libs/rdroid/Math/rdVector.c @@ -5,7 +5,6 @@ #include #include -#include void rdVector_InstallHooks(void) { @@ -42,26 +41,26 @@ void rdVector_ResetGlobals(void) float J3DAPI rdVector_Normalize2(rdVector2* dest, const rdVector2* src) { RD_ASSERTREL(dest != src); - float len = sqrtf(src->x * src->x + src->y * src->y); - if ( len == 0.0f ) + double len = sqrt(src->x * src->x + src->y * src->y); + if ( len == 0.0 ) { dest->x = src->x; dest->y = src->y; } else { - dest->x = src->x / len; - dest->y = src->y / len; + dest->x = (float)(src->x / len); + dest->y = (float)(src->y / len); } - return len; + return (float)len; } float J3DAPI rdVector_Normalize3(rdVector3* dest, const rdVector3* src) { RD_ASSERTREL(dest != src); - float len = sqrtf(src->x * src->x + src->y * src->y + src->z * src->z); - if ( len == 0.0f ) + double len = sqrt(src->x * src->x + src->y * src->y + src->z * src->z); + if ( len == 0.0 ) { dest->x = src->x; dest->y = src->y; @@ -69,19 +68,19 @@ float J3DAPI rdVector_Normalize3(rdVector3* dest, const rdVector3* src) } else { - dest->x = src->x / len; - dest->y = src->y / len; - dest->z = src->z / len; + dest->x = (float)(src->x / len); + dest->y = (float)(src->y / len); + dest->z = (float)(src->z / len); } - return len; + return (float)len; } float J3DAPI rdVector_Normalize4(rdVector4* dest, const rdVector4* src) { RD_ASSERTREL(dest != src); - float len = sqrtf(src->x * src->x + src->y * src->y + src->z * src->z + src->w * src->w); - if ( len == 0.0f ) + double len = sqrt(src->x * src->x + src->y * src->y + src->z * src->z + src->w * src->w); + if ( len == 0.0 ) { dest->x = src->x; dest->y = src->y; @@ -90,55 +89,45 @@ float J3DAPI rdVector_Normalize4(rdVector4* dest, const rdVector4* src) } else { - dest->x = src->x / len; - dest->y = src->y / len; - dest->z = src->z / len; - dest->w = src->w / len; + dest->x = (float)(src->x / len); + dest->y = (float)(src->y / len); + dest->z = (float)(src->z / len); + dest->w = (float)(src->w / len); } - return len; + return (float)len; } - float J3DAPI rdVector_Normalize2Acc(rdVector2* vec) { - float magnitude = sqrtf(vec->y * vec->y + vec->x * vec->x); - if ( magnitude == 0.0f ) - { - return magnitude; + double magnitude = sqrt(vec->y * vec->y + vec->x * vec->x); + if ( magnitude == 0.0 ) { + return 0.0f; } - vec->x = vec->x / magnitude; - vec->y = vec->y / magnitude; - return magnitude; + vec->x = (float)(vec->x / magnitude); + vec->y = (float)(vec->y / magnitude); + return (float)magnitude; } float J3DAPI rdVector_Normalize3Acc(rdVector3* vec) { - float len = sqrtf(vec->y * vec->y + vec->z * vec->z + vec->x * vec->x); - if ( len == 0.0f ) { - return len; + double len = sqrt(vec->y * vec->y + vec->z * vec->z + vec->x * vec->x); + if ( len == 0.0 ) { + return 0.0f; } - float invLen = 1.0f / len; - vec->x = vec->x * invLen; - vec->y = vec->y * invLen; - vec->z = vec->z * invLen; - return len; + double invLen = 1.0 / len; + vec->x = (float)(vec->x * invLen); + vec->y = (float)(vec->y * invLen); + vec->z = (float)(vec->z * invLen); + return (float)len; } float J3DAPI rdVector_Normalize3QuickAcc(rdVector3* src) { - float z; - float y; - float x; - float min; - float v6; - float v7; - float max; - float med; - - if ( src->x >= 0.0f ) + double x; + if ( src->x >= 0.0 ) { x = src->x; } @@ -147,8 +136,9 @@ float J3DAPI rdVector_Normalize3QuickAcc(rdVector3* src) x = -src->x; } - max = x; - if ( src->y >= 0.0f ) + double max = x; + double y; + if ( src->y >= 0.0 ) { y = src->y; } @@ -157,8 +147,9 @@ float J3DAPI rdVector_Normalize3QuickAcc(rdVector3* src) y = -src->y; } - med = y; - if ( src->z >= 0.0f ) + double med = y; + double z; + if ( src->z >= 0.0 ) { z = src->z; } @@ -167,7 +158,7 @@ float J3DAPI rdVector_Normalize3QuickAcc(rdVector3* src) z = -src->z; } - min = z; + double min = z; if ( z <= y ) { if ( x < y ) @@ -198,27 +189,27 @@ float J3DAPI rdVector_Normalize3QuickAcc(rdVector3* src) } RD_ASSERTREL((min <= med) && (med <= max)); - v6 = 0.34375f * med + 0.25f * min + max; - v7 = 1.0f / v6; - src->x = src->x * v7; - src->y = src->y * v7; - src->z = src->z * v7; - return v6; + double len = 0.34375 * med + 0.25 * min + max; + double invLen = 1.0 / len; + src->x = (float)(src->x * invLen); + src->y = (float)(src->y * invLen); + src->z = (float)(src->z * invLen); + return (float)len; } float J3DAPI rdVector_Normalize4Acc(rdVector4* vec) { - float len = sqrtf(vec->x * vec->x + vec->y * vec->y + vec->z * vec->z + vec->w * vec->w); - if ( len == 0.0f ) { - return len; + double len = sqrt(vec->x * vec->x + vec->y * vec->y + vec->z * vec->z + vec->w * vec->w); + if ( len == 0.0 ) { + return 0.0f; } - float invLen = 1.0f / len; - vec->x = vec->x * invLen; - vec->y = vec->y * invLen; - vec->z = vec->z * invLen; - vec->w = vec->w * invLen; - return len; + double invLen = 1.0 / len; + vec->x = (float)(vec->x * invLen); + vec->y = (float)(vec->y * invLen); + vec->z = (float)(vec->z * invLen); + vec->w = (float)(vec->w * invLen); + return (float)len; } void J3DAPI rdVector_Rotate3(rdVector3* vec, const rdVector3* pivot, const rdVector3* pyr) @@ -233,5 +224,4 @@ void J3DAPI rdVector_Rotate3Acc(rdVector3* vec, const rdVector3* pyr) rdMatrix34 mat; rdMatrix_BuildRotate34(&mat, pyr); rdMatrix_TransformVector34Acc(vec, &mat); -} - +} \ No newline at end of file diff --git a/Libs/rdroid/Math/rdVector.h b/Libs/rdroid/Math/rdVector.h index 9974cc8..19501b1 100644 --- a/Libs/rdroid/Math/rdVector.h +++ b/Libs/rdroid/Math/rdVector.h @@ -191,18 +191,18 @@ inline void J3DAPI rdVector_Scale3Acc(rdVector3* v, float scalar) inline void J3DAPI rdVector_InvScale3(rdVector3* dest, const rdVector3* src, float scalar) { - float invScalar = 1.0f / scalar; - dest->x = src->x * invScalar; - dest->y = src->y * invScalar; - dest->z = src->z * invScalar; + double invScalar = 1.0 / scalar; + dest->x = (float)(src->x * invScalar); + dest->y = (float)(src->y * invScalar); + dest->z = (float)(src->z * invScalar); } inline void J3DAPI rdVector_InvScale3Acc(rdVector3* v, float scalar) { - float inv = 1.0f / scalar; - v->x = v->x * inv; - v->y = v->y * inv; - v->z = v->z * inv; + double inv = 1.0 / scalar; + v->x = (float)(v->x * inv); + v->y = (float)(v->y * inv); + v->z = (float)(v->z * inv); } inline void J3DAPI rdVector_ScaleAdd3(rdVector3* dest, const rdVector3* src, float scalar, const rdVector3* add) @@ -249,7 +249,6 @@ inline void J3DAPI rdVector_Cross3Acc(rdVector3* dest, const rdVector3* v2) res.y = dest->z * v2->x - dest->x * v2->z; res.z = dest->x * v2->y - dest->y * v2->x; *dest = res; - // memcpy(dest, &res, sizeof(rdVector3)); } inline float J3DAPI rdVector_CrossDot3(const rdVector3* a, const rdVector3* b, const rdVector3* c) From 8046835e5c5501a75ca3a0e857fb95ad6eaa7d58 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sat, 14 Dec 2024 00:44:55 +0100 Subject: [PATCH 04/35] [rdroid] Fix drawing of horizontal wall line --- Libs/rdroid/Primitives/rdWallpaper.c | 59 +++++++++++++++++----------- Libs/rdroid/types.h | 4 +- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/Libs/rdroid/Primitives/rdWallpaper.c b/Libs/rdroid/Primitives/rdWallpaper.c index 81226f2..ab7d2bc 100644 --- a/Libs/rdroid/Primitives/rdWallpaper.c +++ b/Libs/rdroid/Primitives/rdWallpaper.c @@ -28,8 +28,7 @@ void rdWallpaper_InstallHooks(void) } void rdWallpaper_ResetGlobals(void) -{ -} +{} rdWallpaper* J3DAPI rdWallpaper_New(const char* pName) { @@ -347,8 +346,8 @@ rdWallLine* J3DAPI rdWallpaper_NewWallLine(float startX, float startY, float end pWallLine->startX = startX; pWallLine->startY = startY; - pWallLine->width = endX - startX; - pWallLine->height = endY - startY; + pWallLine->deltaX = endX - startX; + pWallLine->deltaY = endY - startY; rdVector_Copy4(&pWallLine->color, pColor); return pWallLine; } @@ -393,23 +392,32 @@ void J3DAPI rdWallpaper_DrawWallLine(const rdWallLine* pLine, float progress) float sheight = (float)height / RD_REF_HEIGHT; float thickness = RDWALLPAPER_LINE_THICKNESS * swidth; - float endY, endX = ceilf(pLine->width * progress / 100.0f); - if ( endX == 0.0f ) { - endY = pLine->height * progress / 100.0f; - } - else + // Calculate end point based on progress + float endY = 0.0f, endX = ceilf(pLine->deltaX * progress / 100.0f); + + // Changed: Added handing of case for when pLine->deltaY == 0, i.e.: horizontal line + float deltaRatio = 0.0; // Horizontal line case + if ( pLine->deltaY != 0.0f ) { - double laspect = pLine->height / pLine->width; - double startY = pLine->startY - laspect * pLine->startX; - endY = (float)((pLine->startX + endX) * laspect + startY - pLine->startY); + // Non-horizontal line calculations + double lineAspect = pLine->deltaY / pLine->deltaX; + double startY = pLine->startY - lineAspect * pLine->startX; + + if ( endX == 0.0f ) { + endY = pLine->deltaY * progress / 100.0f; + } + else { + endY = (float)((pLine->startX + endX) * lineAspect + startY - pLine->startY); + } + + deltaRatio = pLine->deltaX / pLine->deltaY; } - float widthHeightRatio = pLine->width / pLine->height; - double offsetY = pLine->startY - widthHeightRatio * pLine->startX; - double coeff1 = 2.0 * -pLine->startX; - double coeff2 = 2.0 * widthHeightRatio * (offsetY - pLine->startY); + double offsetY = pLine->startY - deltaRatio * pLine->startX; + double coeff1 = 2.0 * -pLine->startX; + double coeff2 = 2.0 * deltaRatio * (offsetY - pLine->startY); - double a = pow(widthHeightRatio, 2.0) + 1.0f; + double a = pow(deltaRatio, 2.0) + 1.0f; double b = coeff1 + coeff2; double c = pow(pLine->startX, 2.0) + pow(offsetY - pLine->startY, 2.0) - pow(thickness, 2.0); @@ -434,15 +442,20 @@ void J3DAPI rdWallpaper_DrawWallLine(const rdWallLine* pLine, float progress) } // Final position calculations based on the valid root - float adjustedX = (float)root; - float adjustedY = widthHeightRatio * adjustedX + (float)offsetY; - float deltaX = adjustedX - pLine->startX; - float deltaY = adjustedY - pLine->startY; + // Changed: Added handing of case for when pLine->deltaY == 0, i.e.: horizontal line + float deltaX = 0.0f, deltaY = thickness; + if ( pLine->deltaY != 0.0f ) + { + deltaX = (float)root; + deltaY = deltaRatio * deltaX + (float)offsetY; + deltaX -= pLine->startX; + deltaY -= pLine->startY; + } // Set vertices positions for drawing the line LPD3DTLVERTEX pCurVert = pPoly->aVertices; - // top right + // top left pCurVert->sx = pLine->startX * swidth + deltaX; pCurVert->sy = pLine->startY * sheight - deltaY; pCurVert->sz = 0.0f; @@ -451,7 +464,7 @@ void J3DAPI rdWallpaper_DrawWallLine(const rdWallLine* pLine, float progress) pCurVert->tv = 0.0f; ++pCurVert; - // top left + // top right pCurVert->sx = (pLine->startX + endX) * swidth + deltaX; pCurVert->sy = (pLine->startY + endY) * sheight - deltaY; pCurVert->sz = 0.0f; diff --git a/Libs/rdroid/types.h b/Libs/rdroid/types.h index f4ee9ba..55a1f66 100644 --- a/Libs/rdroid/types.h +++ b/Libs/rdroid/types.h @@ -588,8 +588,8 @@ typedef struct srdWallLine { float startX; float startY; - float width; - float height; + float deltaX; + float deltaY; int unknown4; rdMaterial* pMaterial; rdVector4 color; From fca6ef27cae639b2c9f7faf3f7f9faf8c33f5269 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sat, 14 Dec 2024 02:13:59 +0100 Subject: [PATCH 05/35] [Jones3D] Add absolute resource path to VFS This commit makes OpenJones3D binaries portable (Can be placed in other location than Resource folder) by adding absolute resource path to JonesFile VFS. This is achieved by: - Adds to JonesFile VFS a full resource path based on provided install path. - Changes searching for level files in developer dialog by using resource path from VFS. - Replaces unset registry setting entry `User Path` with existing registry entry `Install Path` --- CHANGELOG | 10 +++- Jones3D/Main/JonesFile.c | 102 ++++++++++++++++++++++++++++----------- Jones3D/Main/JonesFile.h | 33 +++++-------- Jones3D/Main/JonesMain.c | 48 ++++++++++-------- Jones3D/RTI/addresses.h | 6 +-- Jones3D/RTI/symbols.h | 10 ++-- Jones3D/types.h | 4 +- 7 files changed, 132 insertions(+), 81 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b0c1b49..440e7a0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,11 @@ -v0.1 +# v0.2 +General: + - Fixed floating point precision bug in `rdVector` & `rdMatrix34` functions (03b800ad) + +HUD: + - Fixed drawing of horizontal wall line (08046835) + +# v0.1 General: - Fixed minor bugs - Improve game loading times by disabling VSync and blt frame buffers instead of flip. @@ -15,7 +22,6 @@ General: - (`JonesFile`) Fixed path buffer size to 128 chars, was 48 chars - (`JonesFile`) Fixed writing to file by using correct file write function in `JonesFile_FilePrintf` - Added bounds check for number of animating joints when loading keyframe from *.key file - - Made save/load dialogs resizable - Fixed a bug in sound compression where clipping of the audio waveform occurred - Fixed infinitive loop bug when voice subtitle contains too long word (`sithVoice_AddSubtitle`) diff --git a/Jones3D/Main/JonesFile.c b/Jones3D/Main/JonesFile.c index a97af03..d3cefd0 100644 --- a/Jones3D/Main/JonesFile.c +++ b/Jones3D/Main/JonesFile.c @@ -1,18 +1,23 @@ +#include // Note, don't move as it requires including windows.h without WIN32_LEAN_AND_MEAN being defined + #include "JonesFile.h" #include + +#include #include #include -#include #include +#include #include #include #define JF_MAX_HANDLES 32 -#define JF_RES_INSTALL 0 -#define JF_RES_CWD 1 -#define JF_RES_CD 2 +#define JF_RES_INSTALL 0 +#define JF_RES_CWD 1 +#define JF_RES_CD 2 +#define JF_RES_RESOURCE 3 // Added #define JF_ISVALID_FH(fh) (((fh) >= (1)) ? (((fh) <= (32 - 1)) ? 1 : 0) : 0) @@ -24,11 +29,28 @@ tHostServices* JonesFile_pHS; tHostServices JonesFile_sysHS; tHostServices* JonesFile_pSysEnv; -JonesResource JonesFile_aResources[3]; +JonesResource JonesFile_aResources[4]; // Changed: Expand array for resource path char JonesFile_aPathBuf[128]; // Fixed: increased buffer size to 128, was 48 JonesFileHandle JonesFile_aFileHandles[JF_MAX_HANDLES]; +void JonesFile_InitServices(void); +void JonesFile_ResetServices(void); + +void J3DAPI JonesFile_OpenResource(JonesResource* pRsource, const char* pPath); +void J3DAPI JonesFile_CloseResource(JonesResource* pResource); + +tFileHandle J3DAPI JonesFile_OpenFile(const char* pFilename, const char* mode); +int J3DAPI JonesFile_CloseFile(tFileHandle fh); +size_t J3DAPI JonesFile_FileRead(tFileHandle fh, void* pDst, size_t size); +size_t J3DAPI JonesFile_FileWrite(tFileHandle fh, const void* pData, size_t size); +char* J3DAPI JonesFile_FileGets(tFileHandle fh, char* pStr, size_t size); +int J3DAPI JonesFile_FileEOF(tFileHandle fh); +int J3DAPI JonesFile_FileTell(tFileHandle fh); +int J3DAPI JonesFile_FileSeek(tFileHandle fh, int offset, int origin); +size_t J3DAPI JonesFile_FileSize(const char* pFilename); +int JonesFile_FilePrintf(tFileHandle fh, const char* aFormat, ...); + void JonesFile_InstallHooks(void) { @@ -38,11 +60,11 @@ void JonesFile_InstallHooks(void) J3D_HOOKFUNC(JonesFile_Close); J3D_HOOKFUNC(JonesFile_GetCurrentCDNum); J3D_HOOKFUNC(JonesFile_FileExists); - J3D_HOOKFUNC(JonesFile_AddInstallPath); - J3D_HOOKFUNC(JonesFile_AddCDPath); + J3D_HOOKFUNC(JonesFile_SetInstallPath); + J3D_HOOKFUNC(JonesFile_SetCDPath); J3D_HOOKFUNC(JonesFile_InitServices); J3D_HOOKFUNC(JonesFile_ResetServices); - J3D_HOOKFUNC(JonesFile_AddResourcePath); + J3D_HOOKFUNC(JonesFile_OpenResource); J3D_HOOKFUNC(JonesFile_CloseResource); J3D_HOOKFUNC(JonesFile_OpenFile); J3D_HOOKFUNC(JonesFile_CloseFile); @@ -105,18 +127,19 @@ int J3DAPI JonesFile_Open(tHostServices* pEnv, const char* pInstallPath, const c // 0 - install path // 1 - current work dir // 2 - CD path + // 3 - resource dir memset(JonesFile_aResources, 0, sizeof(JonesFile_aResources)); if ( pInstallPath ) { - JonesFile_AddInstallPath(pInstallPath); + JonesFile_SetInstallPath(pInstallPath); } if ( pPathCD ) { - JonesFile_AddCDPath(pPathCD); + JonesFile_SetCDPath(pPathCD); } // Add path to current working dir - JonesFile_AddResourcePath(&JonesFile_aResources[JF_RES_CWD], "."); + JonesFile_OpenResource(&JonesFile_aResources[JF_RES_CWD], "."); memset(JonesFile_aFileHandles, 0, sizeof(JonesFile_aFileHandles)); @@ -125,7 +148,7 @@ int J3DAPI JonesFile_Open(tHostServices* pEnv, const char* pInstallPath, const c } } -void J3DAPI JonesFile_Close() +void JonesFile_Close(void) { if ( !JonesFile_bOpened ) @@ -147,19 +170,20 @@ void J3DAPI JonesFile_Close() JonesFile_CloseResource(&JonesFile_aResources[JF_RES_INSTALL]); JonesFile_CloseResource(&JonesFile_aResources[JF_RES_CD]); JonesFile_CloseResource(&JonesFile_aResources[JF_RES_CWD]); + JonesFile_CloseResource(&JonesFile_aResources[JF_RES_RESOURCE]); // Added JonesFile_bOpened = false; } -int J3DAPI JonesFile_GetCurrentCDNum() +int JonesFile_GetCurrentCDNum(void) { char aPath[128]; - stdFnames_MakePath(aPath, sizeof(aPath), JonesFile_aResources[JF_RES_CD].dirPath, "CD1.GOB"); + stdFnames_MakePath(aPath, sizeof(aPath), JonesFile_aResources[JF_RES_CD].aPath, "CD1.GOB"); if ( std_g_pHS->pFileSize(aPath) ) { return 1; } - stdFnames_MakePath(aPath, sizeof(aPath), JonesFile_aResources[JF_RES_CD].dirPath, "CD2.GOB"); + stdFnames_MakePath(aPath, sizeof(aPath), JonesFile_aResources[JF_RES_CD].aPath, "CD2.GOB"); if ( std_g_pHS->pFileSize(aPath) ) { return 2; @@ -179,15 +203,31 @@ int J3DAPI JonesFile_FileExists(const char* pFilename) return 1; } -void J3DAPI JonesFile_AddInstallPath(const char* pPath) +void J3DAPI JonesFile_SetInstallPath(const char* pPath) { + JonesFile_CloseResource(&JonesFile_aResources[JF_RES_RESOURCE]); // Added JonesFile_CloseResource(&JonesFile_aResources[JF_RES_INSTALL]); - if ( *pPath ) { - JonesFile_AddResourcePath(&JonesFile_aResources[JF_RES_INSTALL], pPath); + if ( *pPath ) + { + JonesFile_OpenResource(&JonesFile_aResources[JF_RES_INSTALL], pPath); + + // Added: Add resource dir path + STD_MAKEPATH(JonesFile_aPathBuf, pPath, "resource"); + JonesFile_OpenResource(&JonesFile_aResources[JF_RES_RESOURCE], JonesFile_aPathBuf); } } -void J3DAPI JonesFile_AddCDPath(const char* pPath) +const char* JonesFile_GetInstallPath(void) +{ + return JonesFile_aResources[JF_RES_INSTALL].aPath; +} + +const char* JonesFile_GetResourcePath(void) +{ + return JonesFile_aResources[JF_RES_RESOURCE].aPath; +} + +void J3DAPI JonesFile_SetCDPath(const char* pPath) { JonesFile_CloseResource(&JonesFile_aResources[JF_RES_CD]); @@ -195,7 +235,7 @@ void J3DAPI JonesFile_AddCDPath(const char* pPath) { char aPath[128]; STD_FORMAT(aPath, "%sresource", pPath); - JonesFile_AddResourcePath(&JonesFile_aResources[JF_RES_CD], aPath); + JonesFile_OpenResource(&JonesFile_aResources[JF_RES_CD], aPath); } else { @@ -203,6 +243,11 @@ void J3DAPI JonesFile_AddCDPath(const char* pPath) } } +const char* JonesFile_GetCDPath(void) +{ + return JonesFile_aResources[JF_RES_CD].aPath; +} + void JonesFile_InitServices(void) { if ( !JonesFile_hsInitCount ) @@ -240,20 +285,21 @@ void JonesFile_ResetServices(void) } } -void J3DAPI JonesFile_AddResourcePath(JonesResource* pRsource, const char* pPath) +void J3DAPI JonesFile_OpenResource(JonesResource* pRsource, const char* pPath) { - STD_STRCPY(pRsource->dirPath, pPath); + STD_STRCPY(pRsource->aPath, pPath); JonesFile_ResetServices(); // Now find all *.gob files in dir pRsource->numGobFiles = 0; + + tFoundFileInfo fileInfo; FindFileData* pFileData = stdFileUtil_NewFind(pPath, /*mode=*/3, "gob"); - tFoundFileInfo pFile; - while ( stdFileUtil_FindNext(pFileData, &pFile) ) + while ( stdFileUtil_FindNext(pFileData, &fileInfo) ) { - if ( pFile.aName[0] != '.' ) + if ( fileInfo.aName[0] != '.' ) { - stdFnames_MakePath(JonesFile_aPathBuf, sizeof(JonesFile_aPathBuf), pPath, pFile.aName); + stdFnames_MakePath(JonesFile_aPathBuf, sizeof(JonesFile_aPathBuf), pPath, fileInfo.aName); pRsource->aGobFiles[pRsource->numGobFiles] = stdGob_Load(JonesFile_aPathBuf, /*numFileHandles*/16, /*bMapFile*/0); // TODO: After file mapping is fixed in stdGob enable file mapping here if ( !pRsource->aGobFiles[pRsource->numGobFiles] ) { @@ -313,11 +359,11 @@ tFileHandle J3DAPI JonesFile_OpenFile(const char* pFilename, const char* mode) JonesResource* pCurRes = JonesFile_aResources; for ( size_t resNum = 0; resNum < STD_ARRAYLEN(JonesFile_aResources) && !bFileFound; ++resNum ) { - if ( pCurRes->dirPath[0] ) + if ( pCurRes->aPath[0] ) { if ( pCurRes != &JonesFile_aResources[JF_RES_CD] )// If not CD resource { - stdFnames_MakePath(JonesFile_aPathBuf, sizeof(JonesFile_aPathBuf), pCurRes->dirPath, pFilename); + stdFnames_MakePath(JonesFile_aPathBuf, sizeof(JonesFile_aPathBuf), pCurRes->aPath, pFilename); fh = JonesFile_pHS->pFileOpen(JonesFile_aPathBuf, mode); if ( fh ) { diff --git a/Jones3D/Main/JonesFile.h b/Jones3D/Main/JonesFile.h index ad7f586..66fd4ae 100644 --- a/Jones3D/Main/JonesFile.h +++ b/Jones3D/Main/JonesFile.h @@ -1,36 +1,25 @@ #ifndef JONES3D_JONESFILE_H #define JONES3D_JONESFILE_H -#include -#include #include -#include -#include #include J3D_EXTERN_C_START void J3DAPI JonesFile_Startup(tHostServices* pHS); void JonesFile_Shutdown(void); + int J3DAPI JonesFile_Open(tHostServices* pEnv, const char* pInstallPath, const char* pPathCD); -void J3DAPI JonesFile_Close(); -int J3DAPI JonesFile_GetCurrentCDNum(); +void JonesFile_Close(void); + +int JonesFile_GetCurrentCDNum(void); int J3DAPI JonesFile_FileExists(const char* pFilename); -void J3DAPI JonesFile_AddInstallPath(const char* pPath); -void J3DAPI JonesFile_AddCDPath(const char* pPath); -void JonesFile_InitServices(void); -void JonesFile_ResetServices(void); -void J3DAPI JonesFile_AddResourcePath(JonesResource* pRsource, const char* pPath); -void J3DAPI JonesFile_CloseResource(JonesResource* pResource); -tFileHandle J3DAPI JonesFile_OpenFile(const char* pFilename, const char* mode); -int J3DAPI JonesFile_CloseFile(tFileHandle fh); -size_t J3DAPI JonesFile_FileRead(tFileHandle fh, void* pDst, size_t size); -size_t J3DAPI JonesFile_FileWrite(tFileHandle fh, const void* pData, size_t size); -char* J3DAPI JonesFile_FileGets(tFileHandle fh, char* pStr, size_t size); -int J3DAPI JonesFile_FileEOF(tFileHandle fh); -int J3DAPI JonesFile_FileTell(tFileHandle fh); -int J3DAPI JonesFile_FileSeek(tFileHandle fh, int offset, int origin); -size_t J3DAPI JonesFile_FileSize(const char* pFilename); -int JonesFile_FilePrintf(tFileHandle fh, const char* aFormat, ...); + +void J3DAPI JonesFile_SetInstallPath(const char* pPath); +const char* JonesFile_GetInstallPath(void); +const char* JonesFile_GetResourcePath(void); + +void J3DAPI JonesFile_SetCDPath(const char* pPath); +const char* JonesFile_GetCDPath(void); // Helper hooking functions void JonesFile_InstallHooks(void); diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index 1512559..db22c68 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -563,7 +563,7 @@ int J3DAPI JonesMain_Startup(const char* lpCmdLine) return 1; } - wuRegistry_GetStr("User Path", JonesMain_state.aInstallPath, STD_ARRAYLEN(JonesMain_state.aInstallPath), ""); + wuRegistry_GetStr("Install Path", JonesMain_state.aInstallPath, STD_ARRAYLEN(JonesMain_state.aInstallPath), ""); wuRegistry_GetStr("Source Dir", JonesMain_state.aCDPath, STD_ARRAYLEN(JonesMain_state.aCDPath), ""); std3D_SetFindAllDevices(wuRegistry_GetIntEx("AllDevices", 0)); @@ -2219,16 +2219,23 @@ int JonesMain_PlayIntroMovie(void) } else { - char aResDir[128]; - wuRegistry_GetStr("Source Dir", aPath, STD_ARRAYLEN(aPath), ""); - STD_FORMAT(aResDir, "%sresource", aPath); - STD_MAKEPATH(aPath, aResDir, aFilename); - } + // Added: Check if video exists in full resource path + STD_MAKEPATH(aPath, JonesFile_GetResourcePath(), aFilename); + if ( !stdFileUtil_FileExists(aPath) ) + { + // Fallback to CD + char aResDir[128]; + wuRegistry_GetStr("Source Dir", aPath, STD_ARRAYLEN(aPath), ""); + STD_FORMAT(aResDir, "%sresource", aPath); + STD_MAKEPATH(aPath, aResDir, aFilename); - if ( !stdFileUtil_FileExists(aPath) ) - { - wkernel_SetWindowProc(JonesMain_GameWndProc); - return wkernel_PeekProcessEvents(); + if ( !stdFileUtil_FileExists(aPath) ) + { + // Failed to locate game videos, return + wkernel_SetWindowProc(JonesMain_GameWndProc); + return wkernel_PeekProcessEvents(); + } + } } Sleep(100u); @@ -2716,7 +2723,7 @@ void J3DAPI JonesMain_LogErrorToFile(const char* pErrorText) if ( pFilename ) { char aInstallPath[128] ={ 0 }; - wuRegistry_GetStr("Install Path", aInstallPath, STD_ARRAYLEN(aInstallPath), ""); + wuRegistry_GetStr("Install Path", aInstallPath, STD_ARRAYLEN(aInstallPath), ""); // Changed: Changed setting key from 'User Path' to 'Install Path' size_t pathLen = strlen(aInstallPath); if ( pathLen ) { @@ -2994,15 +3001,18 @@ int J3DAPI JonesMain_InitDevDialog(HWND hDlg, WPARAM wParam, JonesState* pConfig hDlgItem = GetDlgItem(hDlg, 1001); if ( hDlgItem ) { + char aNdyDir[128]; + STD_MAKEPATH(aNdyDir, JonesFile_GetResourcePath(), "ndy"); // Changed: Use absolute resource path to search for level files + tFoundFileInfo fileInfo; - FindFileData* pFileData = stdFileUtil_NewFind("ndy", 3, "ndy"); + FindFileData* pFileData = stdFileUtil_NewFind(aNdyDir, 3, "ndy"); while ( stdFileUtil_FindNext(pFileData, &fileInfo) ) { itemIdx = SendMessage(hDlgItem, LB_ADDSTRING, 0, (LPARAM)&fileInfo); } - stdFileUtil_DisposeFind(pFileData); - pFileData = stdFileUtil_NewFind("ndy", 3, "cnd"); + + pFileData = stdFileUtil_NewFind(aNdyDir, 3, "cnd"); while ( stdFileUtil_FindNext(pFileData, &fileInfo) ) { itemIdx = SendMessage(hDlgItem, LB_ADDSTRING, 0, (LPARAM)&fileInfo); @@ -3224,7 +3234,7 @@ void J3DAPI JonesMain_DevDialogHandleCommand(HWND hWnd, int controlId, LPARAM lP wuRegistry_SaveInt("Debug Mode", pState->outputMode); wuRegistry_SaveInt("Verbosity", pState->logLevel); - wuRegistry_SaveStr("User Path", pState->aInstallPath); + wuRegistry_SaveStr("Install Path", pState->aInstallPath); // Changed: Changed setting key from 'User Path' to 'Install Path' wuRegistry_SaveInt("Performance Level", pState->performanceLevel); wuRegistry_SaveInt("HiPoly", sithModel_IsHiPolyEnabled()); // Added @@ -3304,8 +3314,8 @@ void J3DAPI JonesMain_DevDialogInitDisplayDevices(HWND hDlg, JonesState* pConfig for ( size_t modeNum = 0; modeNum < JonesMain_pStartupDisplayEnv->aDisplayInfos[pConfig->displaySettings.displayDeviceNum].numModes; ++modeNum ) { if ( pDisplay->aModes[modeNum].aspectRatio == 1.0f - && pDisplay->aModes[modeNum].rasterInfo.width >= 512 - && pDisplay->aModes[modeNum].rasterInfo.height >= 384 ) + && pDisplay->aModes[modeNum].rasterInfo.width >= 512 + && pDisplay->aModes[modeNum].rasterInfo.height >= 384 ) { if ( JonesMain_CurDisplaySupportsBPP(&pConfig->displaySettings, pDisplay->aModes[modeNum].rasterInfo.colorInfo.bpp) ) { @@ -3315,8 +3325,8 @@ void J3DAPI JonesMain_DevDialogInitDisplayDevices(HWND hDlg, JonesState* pConfig // Select mode if ( pDisplay->aModes[modeNum].rasterInfo.width == JonesMain_curVideoMode.rasterInfo.width - && pDisplay->aModes[modeNum].rasterInfo.height == JonesMain_curVideoMode.rasterInfo.height - && pDisplay->aModes[modeNum].rasterInfo.colorInfo.bpp == JonesMain_curVideoMode.rasterInfo.colorInfo.bpp ) + && pDisplay->aModes[modeNum].rasterInfo.height == JonesMain_curVideoMode.rasterInfo.height + && pDisplay->aModes[modeNum].rasterInfo.colorInfo.bpp == JonesMain_curVideoMode.rasterInfo.colorInfo.bpp ) { SendMessage(hCBDisplayMode, CB_SETCURSEL, itemIdx, 0); bDriverSet = true; diff --git a/Jones3D/RTI/addresses.h b/Jones3D/RTI/addresses.h index 6e7c241..84a8fc0 100644 --- a/Jones3D/RTI/addresses.h +++ b/Jones3D/RTI/addresses.h @@ -360,11 +360,11 @@ #define JonesFile_Close_ADDR 0x00412FA0 #define JonesFile_GetCurrentCDNum_ADDR 0x00413060 #define JonesFile_FileExists_ADDR 0x004130E0 -#define JonesFile_AddInstallPath_ADDR 0x00413110 -#define JonesFile_AddCDPath_ADDR 0x00413140 +#define JonesFile_SetInstallPath_ADDR 0x00413110 +#define JonesFile_SetCDPath_ADDR 0x00413140 #define JonesFile_InitServices_ADDR 0x004131C0 #define JonesFile_ResetServices_ADDR 0x00413250 -#define JonesFile_AddResourcePath_ADDR 0x00413300 +#define JonesFile_OpenResource_ADDR 0x00413300 #define JonesFile_CloseResource_ADDR 0x00413410 #define JonesFile_OpenFile_ADDR 0x00413450 #define JonesFile_CloseFile_ADDR 0x00413660 diff --git a/Jones3D/RTI/symbols.h b/Jones3D/RTI/symbols.h index df8c530..636f7e0 100644 --- a/Jones3D/RTI/symbols.h +++ b/Jones3D/RTI/symbols.h @@ -347,14 +347,14 @@ #define JonesFile_Startup_TYPE void (J3DAPI*)(tHostServices*) #define JonesFile_Shutdown_TYPE void (*)(void) #define JonesFile_Open_TYPE int (J3DAPI*)(tHostServices*, const char*, const char*) -#define JonesFile_Close_TYPE void (J3DAPI*)() -#define JonesFile_GetCurrentCDNum_TYPE int (J3DAPI*)() +#define JonesFile_Close_TYPE void (*)(void) +#define JonesFile_GetCurrentCDNum_TYPE int (*)(void) #define JonesFile_FileExists_TYPE int (J3DAPI*)(const char*) -#define JonesFile_AddInstallPath_TYPE void (J3DAPI*)(const char*) -#define JonesFile_AddCDPath_TYPE void (J3DAPI*)(const char*) +#define JonesFile_SetInstallPath_TYPE void (J3DAPI*)(const char*) +#define JonesFile_SetCDPath_TYPE void (J3DAPI*)(const char*) #define JonesFile_InitServices_TYPE void (*)(void) #define JonesFile_ResetServices_TYPE void (*)(void) -#define JonesFile_AddResourcePath_TYPE void (J3DAPI*)(JonesResource*, const char*) +#define JonesFile_OpenResource_TYPE void (J3DAPI*)(JonesResource*, const char*) #define JonesFile_CloseResource_TYPE void (J3DAPI*)(JonesResource*) #define JonesFile_OpenFile_TYPE tFileHandle (J3DAPI*)(const char*, const char*) #define JonesFile_CloseFile_TYPE int (J3DAPI*)(tFileHandle) diff --git a/Jones3D/types.h b/Jones3D/types.h index a840c51..7feb8b6 100644 --- a/Jones3D/types.h +++ b/Jones3D/types.h @@ -1,8 +1,8 @@ #ifndef JONES3D_TYPES_H #define JONES3D_TYPES_H -#include #include #include +#include #include #include @@ -181,7 +181,7 @@ typedef int (*JonesDialogFlipPageFunc)(void); typedef struct sJonesResource { - char dirPath[128]; + char aPath[128]; size_t numGobFiles; Gob* aGobFiles[32]; } JonesResource; From d6debbcb7c15e23169159466c81e95a2a05c12b7 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sat, 14 Dec 2024 02:17:45 +0100 Subject: [PATCH 06/35] [Injector] Add additional option to locate Indy3D.exe in resource dir --- Jones3D/exemain.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Jones3D/exemain.cpp b/Jones3D/exemain.cpp index 00aa9a4..f124675 100644 --- a/Jones3D/exemain.cpp +++ b/Jones3D/exemain.cpp @@ -60,6 +60,11 @@ int wmain(int argc, wchar_t* argv[]) } args = sargs.str(); } + else if ( !fs::exists(exePath) ) + { + // Try locating exe in resource folder + exePath = "Resource/Indy3D.exe"; + } if ( !fs::exists(exePath) ) { From 7c18685f4b6cd5592beda743f056f4aa545d1316 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sat, 14 Dec 2024 02:26:32 +0100 Subject: [PATCH 07/35] [Jones3D] Fix closing log file & Fix initing full array --- Jones3D/Main/JonesMain.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index db22c68..80445d3 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -539,7 +539,7 @@ int J3DAPI JonesMain_Startup(const char* lpCmdLine) JonesMain_curLevelNum = 0; memset(&JonesMain_circBuf, 0, sizeof(JonesMain_circBuf)); - memset(JonesMain_aToggleMenuKeyIds, 0, 8u); // TODO: [BUG] + memset(JonesMain_aToggleMenuKeyIds, 0, sizeof(JonesMain_aToggleMenuKeyIds)); // Fixed: Fixed the size // Setup process routines JonesMain_pfProcess = JonesMain_ProcessGame; @@ -1396,7 +1396,9 @@ void JonesMain_Shutdown(void) else if ( JonesMain_state.outputMode == JONES_OUTPUTMODE_LOGFILE ) { - fclose(JonesMain_pLogFile); // TODO: BUG JonesMain_pLogFile could be NULL + if ( JonesMain_pLogFile ) { // Fixed: Add null check + fclose(JonesMain_pLogFile); + } } sithSound_Shutdown(); From 47537aa862e0b229545a96db90f1da00c4f6d16a Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sat, 14 Dec 2024 22:02:29 +0100 Subject: [PATCH 08/35] [Jones3D|sith|std] Code cleanup & minor fixes --- Jones3D/Display/JonesDisplay.c | 46 ++++++++++++++-------------- Jones3D/Main/JonesFile.c | 54 ++++++++++++++++---------------- Jones3D/Main/JonesMain.c | 43 ++++++++++++++------------ Jones3D/RTI/symbols.h | 2 +- Libs/sith/Engine/sithRender.c | 56 +++++++++++++++++----------------- Libs/std/General/stdBmp.c | 15 ++++----- Libs/std/Win95/std3D.c | 30 +++++++++--------- Libs/std/Win95/stdDisplay.c | 37 ++++------------------ 8 files changed, 132 insertions(+), 151 deletions(-) diff --git a/Jones3D/Display/JonesDisplay.c b/Jones3D/Display/JonesDisplay.c index 4ade985..b39bb5a 100644 --- a/Jones3D/Display/JonesDisplay.c +++ b/Jones3D/Display/JonesDisplay.c @@ -514,28 +514,28 @@ int JonesDisplay_ShowSplashDemo(void) int numPressed; stdControl_ReadControls(); if ( stdControl_ReadKey(DIK_ESCAPE, &numPressed) - || stdControl_ReadKey(DIK_SPACE, &numPressed) - || stdControl_ReadKey(DIK_F1, &numPressed) - || stdControl_ReadKey(DIK_F2, &numPressed) - || stdControl_ReadKey(DIK_F3, &numPressed) - || stdControl_ReadKey(DIK_F4, &numPressed) - || stdControl_ReadKey(DIK_F5, &numPressed) - || stdControl_ReadKey(DIK_F6, &numPressed) - || stdControl_ReadKey(DIK_F7, &numPressed) - || stdControl_ReadKey(DIK_F8, &numPressed) - || stdControl_ReadKey(DIK_F9, &numPressed) - || stdControl_ReadKey(DIK_F10, &numPressed) - || stdControl_ReadKey(DIK_F11, &numPressed) - || stdControl_ReadKey(DIK_F12, &numPressed) - || stdControl_ReadKey(DIK_RETURN, &numPressed) - || stdControl_ReadKey(DIK_NUMPADENTER, &numPressed) - || stdControl_ReadKey(DIK_RCONTROL, &numPressed) - || stdControl_ReadKey(DIK_LCONTROL, &numPressed) - || stdControl_ReadKey(DIK_RMENU, &numPressed) - || stdControl_ReadKey(DIK_LMENU, &numPressed) - || stdControl_ReadKey(DIK_Q, &numPressed) - || stdControl_ReadKey(DIK_X, &numPressed) - || stdControl_ReadKey(DIK_W, &numPressed) ) + || stdControl_ReadKey(DIK_SPACE, &numPressed) + || stdControl_ReadKey(DIK_F1, &numPressed) + || stdControl_ReadKey(DIK_F2, &numPressed) + || stdControl_ReadKey(DIK_F3, &numPressed) + || stdControl_ReadKey(DIK_F4, &numPressed) + || stdControl_ReadKey(DIK_F5, &numPressed) + || stdControl_ReadKey(DIK_F6, &numPressed) + || stdControl_ReadKey(DIK_F7, &numPressed) + || stdControl_ReadKey(DIK_F8, &numPressed) + || stdControl_ReadKey(DIK_F9, &numPressed) + || stdControl_ReadKey(DIK_F10, &numPressed) + //|| stdControl_ReadKey(DIK_F11, &numPressed) + || stdControl_ReadKey(DIK_F12, &numPressed) + || stdControl_ReadKey(DIK_RETURN, &numPressed) + || stdControl_ReadKey(DIK_NUMPADENTER, &numPressed) + || stdControl_ReadKey(DIK_RCONTROL, &numPressed) + || stdControl_ReadKey(DIK_LCONTROL, &numPressed) + || stdControl_ReadKey(DIK_RMENU, &numPressed) + || stdControl_ReadKey(DIK_LMENU, &numPressed) + || stdControl_ReadKey(DIK_Q, &numPressed) + || stdControl_ReadKey(DIK_X, &numPressed) + || stdControl_ReadKey(DIK_W, &numPressed) ) { bFinish = 1; } @@ -549,7 +549,7 @@ int JonesDisplay_ShowSplashDemo(void) } rdWallpaper_Free(pWallpaper); - pWallpaper = 0; + pWallpaper = NULL; return 0; } diff --git a/Jones3D/Main/JonesFile.c b/Jones3D/Main/JonesFile.c index d3cefd0..7e0236b 100644 --- a/Jones3D/Main/JonesFile.c +++ b/Jones3D/Main/JonesFile.c @@ -15,7 +15,7 @@ #define JF_MAX_HANDLES 32 #define JF_RES_INSTALL 0 -#define JF_RES_CWD 1 +#define JF_RES_CWD 1 // current working dir #define JF_RES_CD 2 #define JF_RES_RESOURCE 3 // Added @@ -29,7 +29,7 @@ tHostServices* JonesFile_pHS; tHostServices JonesFile_sysHS; tHostServices* JonesFile_pSysEnv; -JonesResource JonesFile_aResources[4]; // Changed: Expand array for resource path +JonesResource JonesFile_aResources[4] = { 0 }; // Changed: Expand array for resource path; Added: Init to 0 char JonesFile_aPathBuf[128]; // Fixed: increased buffer size to 128, was 48 JonesFileHandle JonesFile_aFileHandles[JF_MAX_HANDLES]; @@ -105,8 +105,7 @@ void JonesFile_Shutdown(void) int J3DAPI JonesFile_Open(tHostServices* pEnv, const char* pInstallPath, const char* pPathCD) { - if ( !JonesFile_bStartup ) - { + if ( !JonesFile_bStartup ) { return 1; } @@ -115,37 +114,35 @@ int J3DAPI JonesFile_Open(tHostServices* pEnv, const char* pInstallPath, const c STDLOG_ERROR("Warning: System already open!\n"); return 1; } - else - { - memcpy(&JonesFile_sysHS, pEnv, sizeof(JonesFile_sysHS)); - JonesFile_pHS = &JonesFile_sysHS; - JonesFile_pSysEnv = pEnv; - JonesFile_InitServices(); // This will override current pEnv + memcpy(&JonesFile_sysHS, pEnv, sizeof(JonesFile_sysHS)); + JonesFile_pHS = &JonesFile_sysHS; + JonesFile_pSysEnv = pEnv; - // Add resource paths: - // 0 - install path - // 1 - current work dir - // 2 - CD path - // 3 - resource dir - memset(JonesFile_aResources, 0, sizeof(JonesFile_aResources)); + JonesFile_InitServices(); // This will override current pEnv - if ( pInstallPath ) { - JonesFile_SetInstallPath(pInstallPath); - } + // Add resource paths: + // 0 - install path + // 1 - current work dir + // 2 - CD path + // 3 - resource dir + memset(JonesFile_aResources, 0, sizeof(JonesFile_aResources)); - if ( pPathCD ) { - JonesFile_SetCDPath(pPathCD); - } + if ( pInstallPath ) { + JonesFile_SetInstallPath(pInstallPath); + } - // Add path to current working dir - JonesFile_OpenResource(&JonesFile_aResources[JF_RES_CWD], "."); + if ( pPathCD ) { + JonesFile_SetCDPath(pPathCD); + } - memset(JonesFile_aFileHandles, 0, sizeof(JonesFile_aFileHandles)); + // Add path to current working dir + JonesFile_OpenResource(&JonesFile_aResources[JF_RES_CWD], "."); - JonesFile_bOpened = true; - return 0; - } + memset(JonesFile_aFileHandles, 0, sizeof(JonesFile_aFileHandles)); + + JonesFile_bOpened = true; + return 0; } void JonesFile_Close(void) @@ -171,6 +168,7 @@ void JonesFile_Close(void) JonesFile_CloseResource(&JonesFile_aResources[JF_RES_CD]); JonesFile_CloseResource(&JonesFile_aResources[JF_RES_CWD]); JonesFile_CloseResource(&JonesFile_aResources[JF_RES_RESOURCE]); // Added + memset(JonesFile_aResources, 0, sizeof(JonesFile_aResources)); // Added: clear JonesFile_bOpened = false; } diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index 80445d3..61abfaa 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -412,7 +412,7 @@ static const JonesLevelInfo JonesMain_aNdyLevelLoadInfos[18] = int J3DAPI JonesMain_GameWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, int* pReturnVal); void J3DAPI JonesMain_HandleWMGetMinMaxInfo(HWND hwnd, LPMINMAXINFO pMinMaxInfo); void J3DAPI JonesMain_HandleWMPaint(HWND hWnd); -int J3DAPI JonesMain_HandleWMTimer(HWND hWnd, WPARAM timerID); +void J3DAPI JonesMain_HandleWMTimer(HWND hWnd, WPARAM timerID); void J3DAPI JonesMain_HandleWMKeydown(HWND hWnd, WPARAM vk, int a3, uint16_t repreatCount, uint16_t exkeyflags); void JonesMain_PrintQuickSave(void); int J3DAPI JonesMain_HandleWMActivateApp(HWND hWnd, WPARAM wParam, LPARAM lParam); @@ -569,6 +569,7 @@ int J3DAPI JonesMain_Startup(const char* lpCmdLine) std3D_SetFindAllDevices(wuRegistry_GetIntEx("AllDevices", 0)); JonesFile_Open(&JonesMain_hs, JonesMain_state.aInstallPath, JonesMain_state.aCDPath); + if ( jonesString_Startup() ) { JonesMain_LogErrorToFile("Could not open game text."); @@ -1079,13 +1080,14 @@ void J3DAPI JonesMain_HandleWMPaint(HWND hWnd) } } -int J3DAPI JonesMain_HandleWMTimer(HWND hWnd, WPARAM timerID) +void J3DAPI JonesMain_HandleWMTimer(HWND hWnd, WPARAM timerID) { - J3D_UNUSED(timerID); - - // TODO: Check if timerID is same as JONES_QUICKSAVE_TEXTSHOWTIMERID - JonesMain_bPrintQuickSave = false; - return KillTimer(hWnd, JONES_QUICKSAVE_TEXTSHOWTIMERID); + if ( timerID == JONES_QUICKSAVE_TEXTSHOWTIMERID ) // Added: Added timedID check + { + // TODO: If JonesMain_bWndMsgProcessed will be used by main wnd proc than it should be set to true here + JonesMain_bPrintQuickSave = false; + KillTimer(hWnd, JONES_QUICKSAVE_TEXTSHOWTIMERID); + } } void J3DAPI JonesMain_HandleWMKeydown(HWND hWnd, WPARAM vk, int a3, uint16_t repreatCount, uint16_t exkeyflags) @@ -1612,7 +1614,7 @@ void JonesMain_PrintFramerate(void) STD_FORMAT( std_g_genBuffer, - "%02.2fHz A:%d S:%d P:%d Z:%d", //TODO: 'Z' in format is probably typo and instead 'T' letter should be there + "%02.2fHz A:%d S:%d P:%d T:%d", // Changed: Fixed typo 'Z' -> 'T' JonesMain_frameRate, sithRender_g_numVisibleAdjoins, sithRender_g_numVisibleSectors, @@ -1697,7 +1699,7 @@ int JonesMain_Open(void) } JonesMain_CloseWindow(); // Note this will exit process - // TODO: should we still return error here? + return 1; // Added: Return 1, tho this code won't be executed as JonesMain_CloseWindow will end process } else { @@ -1731,7 +1733,7 @@ int JonesMain_Open(void) } JonesMain_CloseWindow(); // Note this will exit process - // TODO: should we still return error here? + return 1; // Added: Return 1, tho this code won't be executed as JonesMain_CloseWindow will end process } if ( JonesDisplay_Open(&JonesMain_state.displaySettings) ) @@ -1836,14 +1838,6 @@ int J3DAPI JonesMain_Restore(const char* pNdsFilePath) int JonesMain_ProcessGamesaveState(void) { - // TODO: this part should be moved in the load game scope - char aLoadLerrorStr[512] = { 0 }; - const char* pLoadErrStr = jonesString_GetString("JONES_STR_LOADERROR"); - if ( pLoadErrStr ) - { - STD_STRCPY(aLoadLerrorStr, pLoadErrStr); - } - char* pNdsFilename; JonesMain_curGamesaveState = sithGamesave_GetState(&pNdsFilename); @@ -1875,6 +1869,16 @@ int JonesMain_ProcessGamesaveState(void) if ( JonesMain_Restore(pNdsFilename) || sithGamesave_Process() ) { // Error + + // Changed: Moved retrieving error string down here. + // Originally was at the start of the functions scope and was executed on every function call + char aLoadLerrorStr[512] = { 0 }; + const char* pLoadErrStr = jonesString_GetString("JONES_STR_LOADERROR"); + if ( pLoadErrStr ) + { + STD_STRCPY(aLoadLerrorStr, pLoadErrStr); + } + if ( aLoadLerrorStr[0] ) { char aErrorText[128] = { 0 }; @@ -2663,7 +2667,7 @@ J3DNORETURN void J3DAPI JonesMain_Assert(const char* pErrorText, const char* pSr void J3DAPI JonesMain_BindToggleMenuControlKeys(const int* paKeyIds, int numKeys) { - memset(JonesMain_aToggleMenuKeyIds, 0, 8u); // TODO: bug zero all elements + memset(JonesMain_aToggleMenuKeyIds, 0, sizeof(JonesMain_aToggleMenuKeyIds)); // Fixed: Fixed size for ( size_t i = 0; i < numKeys; ++i ) { JonesMain_aToggleMenuKeyIds[i] = paKeyIds[i]; @@ -2720,6 +2724,7 @@ size_t JonesMain_GetCurrentLevelNum(void) void J3DAPI JonesMain_LogErrorToFile(const char* pErrorText) { char aFilePath[128] = { 0 }; + } const char* pFilename = jonesString_GetString("JONES_STR_ERRORFILE"); // TODO: pErrorText could be internally cached injonesString which will get overridden here. Best solution would be to use circ buffer by jonesString_GetString if ( pFilename ) diff --git a/Jones3D/RTI/symbols.h b/Jones3D/RTI/symbols.h index 636f7e0..a99847f 100644 --- a/Jones3D/RTI/symbols.h +++ b/Jones3D/RTI/symbols.h @@ -90,7 +90,7 @@ #define JonesMain_GameWndProc_TYPE int (J3DAPI*)(HWND, UINT, WPARAM, LPARAM, int*) #define JonesMain_HandleWMGetMinMaxInfo_TYPE void (J3DAPI*)(HWND, LPMINMAXINFO) #define JonesMain_HandleWMPaint_TYPE void (J3DAPI*)(HWND) -#define JonesMain_HandleWMTimer_TYPE int (J3DAPI*)(HWND, WPARAM) +#define JonesMain_HandleWMTimer_TYPE void (J3DAPI*)(HWND, WPARAM) #define JonesMain_HandleWMKeydown_TYPE void (J3DAPI*)(HWND, WPARAM, int, uint16_t, uint16_t) #define JonesMain_PrintQuickSave_TYPE void (*)(void) #define JonesMain_HandleWMActivateApp_TYPE int (J3DAPI*)(HWND, WPARAM, LPARAM) diff --git a/Libs/sith/Engine/sithRender.c b/Libs/sith/Engine/sithRender.c index 4a961d1..009339c 100644 --- a/Libs/sith/Engine/sithRender.c +++ b/Libs/sith/Engine/sithRender.c @@ -520,11 +520,11 @@ void J3DAPI sithRender_BuildVisibleSectorList(SithSector* pSector, rdClipFrustum rdPrimit3_ClipFace(pFrustrum, RD_GEOMETRY_WIREFRAME, RD_LIGHTING_LIT, &sithRender_faceView, &sithRender_clipFaceView, &pSurface->face.texVertOffset); if ( (sithRender_clipFaceView.numVertices >= 3 || (rdClip_g_faceStatus & 0x40) != 0) - && ((rdClip_g_faceStatus & 0x41) != 0 - || (pAdjoin->flags & SITH_ADJOIN_VISIBLE) != 0 - && (!pSurface->face.pMaterial || pSurface->face.geometryMode == RD_GEOMETRY_NONE - || (pSurface->face.flags & RD_FF_TEX_TRANSLUCENT) != 0 - || pSurface->face.pMaterial->formatType != STDCOLOR_FORMAT_RGB)) ) + && ((rdClip_g_faceStatus & 0x41) != 0 + || (pAdjoin->flags & SITH_ADJOIN_VISIBLE) != 0 + && (!pSurface->face.pMaterial || pSurface->face.geometryMode == RD_GEOMETRY_NONE + || (pSurface->face.flags & RD_FF_TEX_TRANSLUCENT) != 0 + || pSurface->face.pMaterial->formatType != STDCOLOR_FORMAT_RGB)) ) { rdCamera_g_pCurCamera->pfProjectList(sithRender_aTransformedClipVertices, sithRender_aClipVertices, sithRender_clipFaceView.numVertices); if ( (rdClip_g_faceStatus & 0x41) != 0 ) @@ -660,10 +660,10 @@ void J3DAPI sithRender_PVSBuildVisibleSectorList(SithSector* pSector, rdClipFrus //numVertices = sithRender_clipFaceView.numVertices; if ( (sithRender_clipFaceView.numVertices || (rdClip_g_faceStatus & 1) != 0) - && (!pSurface->face.pMaterial || pSurface->face.pMaterial->formatType != STDCOLOR_FORMAT_RGB - || (pSurface->face.flags & RD_FF_TEX_TRANSLUCENT) != 0 - || pSurface->face.geometryMode == RD_GEOMETRY_NONE) - && (pAdjoin->flags & SITH_ADJOIN_VISIBLE) != 0 ) + && (!pSurface->face.pMaterial || pSurface->face.pMaterial->formatType != STDCOLOR_FORMAT_RGB + || (pSurface->face.flags & RD_FF_TEX_TRANSLUCENT) != 0 + || pSurface->face.geometryMode == RD_GEOMETRY_NONE) + && (pAdjoin->flags & SITH_ADJOIN_VISIBLE) != 0 ) { // Project vertices to camera space rdCamera_g_pCurCamera->pfProjectList(sithRender_aTransformedClipVertices, sithRender_aClipVertices, sithRender_clipFaceView.numVertices); @@ -782,7 +782,7 @@ void J3DAPI sithRender_BuildVisibleSector(SithSector* pSector, const rdClipFrust { // Collect Thing's light if light range is > 0.01f if ( (pThing->light.color.red > 0.0f || pThing->light.color.green > 0.0f || pThing->light.color.blue > 0.0f) - && SITHRENDER_ISDYNAMICLIGHT(pThing->light.color.alpha) ) + && SITHRENDER_ISDYNAMICLIGHT(pThing->light.color.alpha) ) { rdVector_Copy4(&sithRender_aThingLights[sithRender_numThingLights].color, &pThing->light.color); @@ -795,11 +795,11 @@ void J3DAPI sithRender_BuildVisibleSector(SithSector* pSector, const rdClipFrust // Collect actor's head light if ( (pThing->type == SITH_THING_ACTOR || pThing->type == SITH_THING_PLAYER) - && sithRender_numThingLights < STD_ARRAYLEN(sithRender_aThingLights) - && (pThing->thingInfo.actorInfo.flags & SITH_AF_HEADLIGHT) != 0 - && (pThing->thingInfo.actorInfo.headLightIntensity.red > 0.0f - || pThing->thingInfo.actorInfo.headLightIntensity.green > 0.0f - || pThing->thingInfo.actorInfo.headLightIntensity.blue > 0.0f) ) + && sithRender_numThingLights < STD_ARRAYLEN(sithRender_aThingLights) + && (pThing->thingInfo.actorInfo.flags & SITH_AF_HEADLIGHT) != 0 + && (pThing->thingInfo.actorInfo.headLightIntensity.red > 0.0f + || pThing->thingInfo.actorInfo.headLightIntensity.green > 0.0f + || pThing->thingInfo.actorInfo.headLightIntensity.blue > 0.0f) ) { rdVector_Copy4(&sithRender_aThingLights[sithRender_numThingLights].color, &pThing->thingInfo.actorInfo.headLightIntensity); @@ -994,13 +994,13 @@ void J3DAPI sithRender_BuildSectorThingList(SithSector* pSector, float curDistan // Verify that collecting them here is really necessary if ( sithRender_numThingLights < STD_ARRAYLEN(sithRender_aThingLights) - && (pThing->flags & SITH_TF_EMITLIGHT) != 0 - && (pThing->flags & (SITH_TF_DISABLED | SITH_TF_DESTROYED)) == 0 ) + && (pThing->flags & SITH_TF_EMITLIGHT) != 0 + && (pThing->flags & (SITH_TF_DISABLED | SITH_TF_DESTROYED)) == 0 ) { // Collect thing light if range is > 0.01f if ( pThing->light.color.red > 0.0f - || pThing->light.color.green > 0.0f - || pThing->light.color.blue > 0.0f && SITHRENDER_ISDYNAMICLIGHT(pThing->light.color.alpha) ) + || pThing->light.color.green > 0.0f + || pThing->light.color.blue > 0.0f && SITHRENDER_ISDYNAMICLIGHT(pThing->light.color.alpha) ) { rdVector_Copy4(&sithRender_aThingLights[sithRender_numThingLights].color, &pThing->light.color); @@ -1013,11 +1013,11 @@ void J3DAPI sithRender_BuildSectorThingList(SithSector* pSector, float curDistan // Collect actor head light if ( (pThing->type == SITH_THING_ACTOR || pThing->type == SITH_THING_PLAYER) - && sithRender_numThingLights < STD_ARRAYLEN(sithRender_aThingLights) - && (pThing->thingInfo.actorInfo.flags & SITH_AF_HEADLIGHT) != 0 - && (pThing->thingInfo.actorInfo.headLightIntensity.x > 0.0f - || pThing->thingInfo.actorInfo.headLightIntensity.y > 0.0f - || pThing->thingInfo.actorInfo.headLightIntensity.z > 0.0f) ) + && sithRender_numThingLights < STD_ARRAYLEN(sithRender_aThingLights) + && (pThing->thingInfo.actorInfo.flags & SITH_AF_HEADLIGHT) != 0 + && (pThing->thingInfo.actorInfo.headLightIntensity.x > 0.0f + || pThing->thingInfo.actorInfo.headLightIntensity.y > 0.0f + || pThing->thingInfo.actorInfo.headLightIntensity.z > 0.0f) ) { rdVector_Copy4(&sithRender_aThingLights[sithRender_numThingLights].color, &pThing->thingInfo.actorInfo.headLightIntensity); @@ -1141,8 +1141,8 @@ void sithRender_RenderThings(void) for ( SithThing* pCurThing = pSector->pFirstThingInSector; pCurThing; pCurThing = pCurThing->pNextThingInSector ) { if ( (pCurThing->flags & (SITH_TF_DISABLED | SITH_TF_INVISIBLE | SITH_TF_DESTROYED)) == 0 - && ((sithCamera_g_pCurCamera->type & (SITHCAMERA_ORBITAL | SITHCAMERA_UNKNOWN_40 | SITHCAMERA_IDLE | SITHCAMERA_UNKNOWN_10 | SITHCAMERA_CINEMATIC | SITHCAMERA_EXTERNAL)) != 0 - || pCurThing != sithCamera_g_pCurCamera->pPrimaryFocusThing) ) + && ((sithCamera_g_pCurCamera->type & (SITHCAMERA_ORBITAL | SITHCAMERA_UNKNOWN_40 | SITHCAMERA_IDLE | SITHCAMERA_UNKNOWN_10 | SITHCAMERA_CINEMATIC | SITHCAMERA_EXTERNAL)) != 0 + || pCurThing != sithCamera_g_pCurCamera->pPrimaryFocusThing) ) { // Transform thing pos to camera orient rdMatrix_TransformPoint34(&pCurThing->transformedPos, &pCurThing->pos, &rdCamera_g_pCurCamera->orient); @@ -1218,8 +1218,8 @@ void sithRender_RenderThings(void) { // Collect thing flat light if ( (pCurThing->flags & SITH_TF_EMITLIGHT) != 0 - && (pCurThing->light.color.red > 0.0f || pCurThing->light.color.green > 0.0f || pCurThing->light.color.blue > 0.0f) - && SITHRENDER_ISFLATLIGHT(pCurThing->light.color.alpha) )// if light range is <= 0.01f + && (pCurThing->light.color.red > 0.0f || pCurThing->light.color.green > 0.0f || pCurThing->light.color.blue > 0.0f) + && SITHRENDER_ISFLATLIGHT(pCurThing->light.color.alpha) )// if light range is <= 0.01f { rdVector4 ambientLight; rdVector_Add4(&ambientLight, §orAmbientLight, &pCurThing->light.color); diff --git a/Libs/std/General/stdBmp.c b/Libs/std/General/stdBmp.c index 79cd48f..b97d14d 100644 --- a/Libs/std/General/stdBmp.c +++ b/Libs/std/General/stdBmp.c @@ -96,13 +96,14 @@ int J3DAPI stdBmp_WriteVBuffer(const char* pFilename, tVBuffer* pVBuffer) for ( int rowIdx = pVBuffer->rasterInfo.height - 1; rowIdx >= 0; rowIdx-- ) { stdColor_ColorConvertOneRow( - pRow, - &ciBmp, - &pVBuffer->pPixels[pVBuffer->rasterInfo.rowSize * rowIdx], - &pVBuffer->rasterInfo.colorInfo, - pVBuffer->rasterInfo.width, - 0, - 0); + pRow, + &ciBmp, + &pVBuffer->pPixels[pVBuffer->rasterInfo.rowSize * rowIdx], + &pVBuffer->rasterInfo.colorInfo, + pVBuffer->rasterInfo.width, + 0, + 0 + ); size_t nWritten = std_g_pHS->pFileWrite(fh, pRow, stride); if ( nWritten != stride ) diff --git a/Libs/std/Win95/std3D.c b/Libs/std/Win95/std3D.c index 765c876..d2b85e2 100644 --- a/Libs/std/Win95/std3D.c +++ b/Libs/std/Win95/std3D.c @@ -483,6 +483,7 @@ int J3DAPI std3D_Open(size_t deviceNum) &std3D_pDDPalette, NULL ); + if ( dres != DD_OK ) { STDLOG_ERROR("Error %s creating palette.\n", std3D_D3DGetStatus(dres)); @@ -496,6 +497,7 @@ int J3DAPI std3D_Open(size_t deviceNum) &std3D_pD3Device, NULL ); + if ( dres != D3D_OK ) { STDLOG_ERROR("Error %s creating Direct3D device.\n", std3D_D3DGetStatus(dres)); @@ -850,7 +852,7 @@ void J3DAPI std3D_SetRenderState(Std3DRenderState rdflags) if ( (std3D_renderState & STD3D_RS_TEXFILTER_ANISOTROPIC) != (rdflags & STD3D_RS_TEXFILTER_ANISOTROPIC) ) { if ( (rdflags & STD3D_RS_TEXFILTER_ANISOTROPIC) != 0 - && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC) != 0 ) + && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC) != 0 ) { IDirect3DDevice3_SetTextureStageState(std3D_pD3Device, 0, D3DTSS_MAGFILTER, D3DTFG_ANISOTROPIC); IDirect3DDevice3_SetTextureStageState(std3D_pD3Device, 0, D3DTSS_MINFILTER, D3DTFG_ANISOTROPIC); @@ -869,7 +871,7 @@ void J3DAPI std3D_SetRenderState(Std3DRenderState rdflags) else if ( (std3D_renderState & STD3D_RS_TEXFILTER_BILINEAR) != (rdflags & STD3D_RS_TEXFILTER_BILINEAR) ) { if ( (rdflags & STD3D_RS_TEXFILTER_BILINEAR) != 0 - && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR) != 0 ) + && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR) != 0 ) { IDirect3DDevice3_SetTextureStageState(std3D_pD3Device, 0, D3DTSS_MAGFILTER, D3DTFG_LINEAR); IDirect3DDevice3_SetTextureStageState(std3D_pD3Device, 0, D3DTSS_MINFILTER, D3DTFN_LINEAR); @@ -1327,9 +1329,9 @@ size_t J3DAPI std3D_FindClosestFormat(const ColorInfo* pMatch) } if ( pFormat->ci.redBPP == pMatch->redBPP - && pFormat->ci.greenBPP == pMatch->greenBPP - && pFormat->ci.blueBPP == pMatch->blueBPP - && pFormat->ci.alphaBPP == pMatch->alphaBPP ) + && pFormat->ci.greenBPP == pMatch->greenBPP + && pFormat->ci.blueBPP == pMatch->blueBPP + && pFormat->ci.alphaBPP == pMatch->alphaBPP ) { return i; } @@ -1440,8 +1442,8 @@ int std3D_InitRenderState(void) } if ( (std3D_pCurDevice->d3dDesc.dwTextureOpCaps & D3DTEXOPCAPS_MODULATE) != 0 - && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwSrcBlendCaps & D3DPBLENDCAPS_SRCALPHA) != 0 - && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwDestBlendCaps & D3DPBLENDCAPS_INVSRCALPHA) != 0 ) + && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwSrcBlendCaps & D3DPBLENDCAPS_SRCALPHA) != 0 + && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwDestBlendCaps & D3DPBLENDCAPS_INVSRCALPHA) != 0 ) { if ( IDirect3DDevice3_SetRenderState(std3D_pD3Device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE) != D3D_OK ) { @@ -1513,7 +1515,7 @@ int std3D_InitRenderState(void) if ( (std3D_aTextureFormats[std3D_RGBAKeyTextureFormat].bColorKey || std3D_aTextureFormats[std3D_RGBATextureFormat].bColorKey) - && IDirect3DDevice3_SetRenderState(std3D_pD3Device, D3DRENDERSTATE_COLORKEYENABLE, TRUE) != D3D_OK ) + && IDirect3DDevice3_SetRenderState(std3D_pD3Device, D3DRENDERSTATE_COLORKEYENABLE, TRUE) != D3D_OK ) { return 0; } @@ -1547,7 +1549,7 @@ int std3D_InitRenderState(void) } if ( (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGTABLE) == 0 - && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGVERTEX) == 0 ) + && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGVERTEX) == 0 ) { std3D_bRenderFog = 0; } @@ -1578,15 +1580,15 @@ int J3DAPI std3D_SetMipmapFilter(Std3DMipmapFilterType filter) { int d3dres = 0; if ( filter == STD3D_MIPMAPFILTER_TRILINEAR - && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR) == 0 - && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_LINEARMIPLINEAR) == 0 ) + && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR) == 0 + && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_LINEARMIPLINEAR) == 0 ) { filter = STD3D_MIPMAPFILTER_BILINEAR; } if ( filter == STD3D_MIPMAPFILTER_BILINEAR - && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT) == 0 - && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MIPLINEAR) == 0 ) + && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT) == 0 + && (std3D_pCurDevice->d3dDesc.dpcTriCaps.dwTextureFilterCaps & D3DPTFILTERCAPS_MIPLINEAR) == 0 ) { filter = STD3D_MIPMAPFILTER_NONE; } @@ -1672,7 +1674,7 @@ void J3DAPI std3D_SetFog(float red, float green, float blue, float startDepth, f { endDepth = (2.0f - std3D_g_fogDensity) * endDepth; if ( IDirect3DDevice3_SetRenderState(std3D_pD3Device, D3DRENDERSTATE_FOGTABLESTART, *(DWORD*)(&startDepth)) == D3D_OK - && IDirect3DDevice3_SetRenderState(std3D_pD3Device, D3DRENDERSTATE_FOGTABLEEND, *(DWORD*)(&endDepth)) == D3D_OK ) + && IDirect3DDevice3_SetRenderState(std3D_pD3Device, D3DRENDERSTATE_FOGTABLEEND, *(DWORD*)(&endDepth)) == D3D_OK ) { std3D_fogStartDepth = startDepth; std3D_fogEndDepth = endDepth; diff --git a/Libs/std/Win95/stdDisplay.c b/Libs/std/Win95/stdDisplay.c index c52c1f1..d58a06e 100644 --- a/Libs/std/Win95/stdDisplay.c +++ b/Libs/std/Win95/stdDisplay.c @@ -230,31 +230,6 @@ void stdDisplay_InstallHooks(void) void stdDisplay_ResetGlobals(void) { - /* const DXStatus stdDisplay_aDDStatusTbl_tmp[111] = { - memcpy((DXStatus*)&stdDisplay_aDDStatusTbl, &stdDisplay_aDDStatusTbl_tmp, sizeof(stdDisplay_aDDStatusTbl)); - - int stdDisplay_coopLevelFlags_tmp = 8; - memcpy(&stdDisplay_coopLevelFlags, &stdDisplay_coopLevelFlags_tmp, sizeof(stdDisplay_coopLevelFlags)); - memset(&stdDisplay_hFont, 0, sizeof(stdDisplay_hFont)); - memset(&stdDisplay_curDevice, 0, sizeof(stdDisplay_curDevice)); - memset(&stdDisplay_primaryVideoMode, 0, sizeof(stdDisplay_primaryVideoMode)); - memset(&stdDisplay_dword_5D73D8, 0, sizeof(stdDisplay_dword_5D73D8)); - memset(&stdDisplay_dword_5D73DC, 0, sizeof(stdDisplay_dword_5D73DC)); - memset(&stdDisplay_backbufWidth, 0, sizeof(stdDisplay_backbufWidth)); - memset(&stdDisplay_backbufHeight, 0, sizeof(stdDisplay_backbufHeight)); - memset(&stdDisplay_zBuffer, 0, sizeof(stdDisplay_zBuffer)); - memset(&stdDisplay_aVideoModes, 0, sizeof(stdDisplay_aVideoModes)); - memset(&stdDisplay_aDisplayDevices, 0, sizeof(stdDisplay_aDisplayDevices)); - memset(&stdDisplay_bStartup, 0, sizeof(stdDisplay_bStartup)); - memset(&stdDisplay_bOpen, 0, sizeof(stdDisplay_bOpen)); - memset(&stdDisplay_bModeSet, 0, sizeof(stdDisplay_bModeSet)); - memset(&stdDisplay_numDevices, 0, sizeof(stdDisplay_numDevices)); - memset(&stdDisplay_pCurDevice, 0, sizeof(stdDisplay_pCurDevice)); - memset(&stdDisplay_numVideoModes, 0, sizeof(stdDisplay_numVideoModes)); - memset(&stdDisplay_pCurVideoMode, 0, sizeof(stdDisplay_pCurVideoMode)); - memset(&stdDisplay_lpDD, 0, sizeof(stdDisplay_lpDD)); - memset(&stdDisplay_bFullscreen, 0, sizeof(stdDisplay_bFullscreen));*/ - memset(&stdDisplay_g_frontBuffer, 0, sizeof(stdDisplay_g_frontBuffer)); memset(&stdDisplay_g_backBuffer, 0, sizeof(stdDisplay_g_backBuffer)); } @@ -659,7 +634,7 @@ int J3DAPI stdDisplay_VBufferLock(tVBuffer* pVBuffer) if ( pVBuffer->lockRefCount == 1 ) { if ( (pVBuffer->surface.ddSurfDesc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER) != 0 - && (pVBuffer->surface.ddSurfDesc.ddsCaps.dwCaps & DDSCAPS_MODEX) != 0 ) { + && (pVBuffer->surface.ddSurfDesc.ddsCaps.dwCaps & DDSCAPS_MODEX) != 0 ) { return 0; } @@ -1973,11 +1948,11 @@ int J3DAPI stdDisplay_LockBackBuffer(void** pSurface, uint32_t* pWidth, uint32_t { stdDisplay_Refresh(1); ddres = IDirectDrawSurface4_Lock( - stdDisplay_g_backBuffer.surface.pDDSurf, - NULL, - &dddesc, - DDLOCK_NOSYSLOCK | DDLOCK_WRITEONLY | DDLOCK_WAIT, - NULL + stdDisplay_g_backBuffer.surface.pDDSurf, + NULL, + &dddesc, + DDLOCK_NOSYSLOCK | DDLOCK_WRITEONLY | DDLOCK_WAIT, + NULL ); } From ffa2e9de4d234c30df32e7c5d326a90f62a2c5e3 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sat, 14 Dec 2024 22:15:45 +0100 Subject: [PATCH 09/35] [Jones3D] Play 800x600 intro movie by default --- Jones3D/Main/JonesMain.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index 61abfaa..6bd8dc1 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -708,7 +708,7 @@ int J3DAPI JonesMain_Startup(const char* lpCmdLine) case JONES_OUTPUTMODE_LOGFILE: { - fopen_s(&JonesMain_pLogFile, "JonesLog.txt", "w+"); // TODO: Add check for case whe fopen fails + fopen_s(&JonesMain_pLogFile, "JonesLog.txt", "w+"); // TODO: Add check for case when fopen fails switch ( JonesMain_state.logLevel ) { case JONES_LOGLEVEL_ERROR: @@ -2195,7 +2195,7 @@ int JonesMain_PlayIntroMovie(void) } else if ( videoMode.rasterInfo.width != 640 ) { - STD_STRCPY(aFilename, aJonesopn_640x480); // TODO: use aJonesopn_800x600 by default + STD_STRCPY(aFilename, aJonesopn_800x600); // Changed: Changed to use aJonesopn_800x600 by default, was aJonesopn_640x480 if ( videoMode.rasterInfo.width == 800 ) { if ( JonesMain_state.performanceLevel < 3 ) From 485d553b476d2b434c0bdf80ab493f44a04be9b8 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sat, 14 Dec 2024 22:22:48 +0100 Subject: [PATCH 10/35] [Jones3D] Fix caching error string && Change writing JonesError.txt to cwd Fixes caching file log error string in case it was cached by `jonesString_GetString`. Changes the location of `JonesError.txt` to be written to cwd. Subsequently this change fixes also potential fatal error when error log is being written due to registry system failed to startup. --- Jones3D/Main/JonesFile.c | 5 +++++ Jones3D/Main/JonesFile.h | 2 ++ Jones3D/Main/JonesMain.c | 34 +++++++++++++++++++--------------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Jones3D/Main/JonesFile.c b/Jones3D/Main/JonesFile.c index 7e0236b..6d814e3 100644 --- a/Jones3D/Main/JonesFile.c +++ b/Jones3D/Main/JonesFile.c @@ -225,6 +225,11 @@ const char* JonesFile_GetResourcePath(void) return JonesFile_aResources[JF_RES_RESOURCE].aPath; } +const char* JonesFile_GetWorkingDirPath(void) +{ + return JonesFile_aResources[JF_RES_CWD].aPath; +} + void J3DAPI JonesFile_SetCDPath(const char* pPath) { diff --git a/Jones3D/Main/JonesFile.h b/Jones3D/Main/JonesFile.h index 66fd4ae..1a58017 100644 --- a/Jones3D/Main/JonesFile.h +++ b/Jones3D/Main/JonesFile.h @@ -18,6 +18,8 @@ void J3DAPI JonesFile_SetInstallPath(const char* pPath); const char* JonesFile_GetInstallPath(void); const char* JonesFile_GetResourcePath(void); +const char* JonesFile_GetWorkingDirPath(void); + void J3DAPI JonesFile_SetCDPath(const char* pPath); const char* JonesFile_GetCDPath(void); diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index 6bd8dc1..c3ccf75 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -553,6 +553,9 @@ int J3DAPI JonesMain_Startup(const char* lpCmdLine) sithSetServices(&JonesMain_hs); sithSound_InitializeSound(&JonesMain_hs); + // Fixed: Move initialization of JonesMain_circBuf here in case there is engine error and exiting happens sooner + stdCircBuf_New(&JonesMain_circBuf, 4, 128); + JonesDisplay_UpdateDualScreenWindowSize(&JonesMain_state.displaySettings); JonesFile_Startup(&JonesMain_hs); @@ -753,8 +756,6 @@ int J3DAPI JonesMain_Startup(const char* lpCmdLine) // Set assert handler JonesMain_hs.pAssert = JonesMain_Assert; - stdCircBuf_New(&JonesMain_circBuf, 4, 128); - // Startup modules and load level STDLOG_DEBUG("Starting up Indiana Jones and the Infernal Machine.\n"); @@ -2202,11 +2203,6 @@ int JonesMain_PlayIntroMovie(void) { STD_STRCPY(aFilename, aJonesopn_400x300); } - else - { - STD_STRCPY(aFilename, aJonesopn_800x600); - } - } } else // 640 x 480 @@ -2723,24 +2719,32 @@ size_t JonesMain_GetCurrentLevelNum(void) void J3DAPI JonesMain_LogErrorToFile(const char* pErrorText) { - char aFilePath[128] = { 0 }; + // Fixed: cache pErrorText, this prevents accidental override of pErrorText when it's cached in jonesString_GetString + char* pErrorStr = (char*)stdCircBuf_GetNextElement(&JonesMain_circBuf); + if ( pErrorStr ) + { + stdUtil_StringCopy(pErrorStr, 128, pErrorText); + pErrorText = pErrorStr; } - const char* pFilename = jonesString_GetString("JONES_STR_ERRORFILE"); // TODO: pErrorText could be internally cached injonesString which will get overridden here. Best solution would be to use circ buffer by jonesString_GetString + char aFilePath[128] = { 0 }; + const char* pFilename = jonesString_GetString("JONES_STR_ERRORFILE"); if ( pFilename ) { - char aInstallPath[128] ={ 0 }; - wuRegistry_GetStr("Install Path", aInstallPath, STD_ARRAYLEN(aInstallPath), ""); // Changed: Changed setting key from 'User Path' to 'Install Path' - size_t pathLen = strlen(aInstallPath); + // Changed: Use cwd path instead of install path from registry. + // This subsequently fixes also fatal error where there was registry startup error + // and accidentally trying to access shutdown registry system here to retrieve install path, which would result in assert. + const char* pCwdPath = JonesFile_GetWorkingDirPath(); + size_t pathLen = strlen(pCwdPath); if ( pathLen ) { - if ( aInstallPath[pathLen - 1] == '\\' ) // Fixed: before: aFilePath[pathLen + 127] == '\\' + if ( pCwdPath[pathLen - 1] == '\\' ) // Fixed: before: aFilePath[pathLen + 127] == '\\' { - STD_FORMAT(aFilePath, "%s%s", aInstallPath, pFilename); + STD_FORMAT(aFilePath, "%s%s", pCwdPath, pFilename); } else { - STD_FORMAT(aFilePath, "%s\\%s", aInstallPath, pFilename); + STD_FORMAT(aFilePath, "%s\\%s", pCwdPath, pFilename); } } else From d32623d94cdcfe3dcdb7ec4be287265f24300f66 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sat, 14 Dec 2024 22:41:09 +0100 Subject: [PATCH 11/35] Update CHANGELOG --- CHANGELOG | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 440e7a0..1b6ec3e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,18 @@ # v0.2 General: + - Fixed various bugs - Fixed floating point precision bug in `rdVector` & `rdMatrix34` functions (03b800ad) + - Made OpenJones3D binaries portable by adding absolute resource dir path to JonesFile VFS (0fca6ef2) + This change allows binaries to be placed in any arbitrary location, eliminating the requirement for a Resource folder. + - Fixed typo in framerate output text (047537aa) + - Fixed initializing and clearing resource array on `JonesFile_Close` (047537aa) + - Fixed the invocation of `jonesString_GetString("JONES_STR_LOADERROR")` on every call to `JonesMain_ProcessGamesaveState` when there is no load error or the game is not being restored (047537aa) + - Fixed error text when it was cached by `jonesString_GetString` in call to `JonesMain_LogErrorToFile` (0485d553) + - Changed the location of `JonesError.txt` to be always written in current working directory instead of installed path (0485d553) + This change resolves a potential fatal error that could occur when writing the error log due to the registry system failing to startup. + +Game play: + - Changed to play higher resolution intro movie (800x600) by default (0ffa2e9d) HUD: - Fixed drawing of horizontal wall line (08046835) From dcd30153b2c28bc08034e3ddb548db86e4312da4 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sun, 15 Dec 2024 02:49:57 +0100 Subject: [PATCH 12/35] [Jones3D] Add log functions Adds new log functions for different log levels. The warning log is now output to console or file. The console log print out is now colorized. --- Jones3D/Main/JonesMain.c | 118 +++++++++++++++++++++++++++++++-------- Jones3D/Main/JonesMain.h | 3 - Jones3D/RTI/addresses.h | 2 +- Jones3D/RTI/symbols.h | 2 +- 4 files changed, 98 insertions(+), 27 deletions(-) diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index c3ccf75..31a1be5 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -65,6 +65,11 @@ #define JONES_FPSPRINT_CONSOLEID 102u #define JONES_FPSPRINT_INTERVAL 2000u // 2 sec +#define JONES_LOGCONSOLE_ERRORCOLOR FOREGROUND_RED | FOREGROUND_INTENSITY +#define JONES_LOGCONSOLE_WARNINGRCOLOR FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY +#define JONES_LOGCONSOLE_STATUSCOLOR FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY +#define JONES_LOGCONSOLE_DEBUGCOLOR FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE + static bool JonesMain_bStartup = false; // Added: Init to false static JonesMainProcessFunc JonesMain_pfCurProcess = NULL; // Added: Init. to NULL @@ -81,7 +86,7 @@ static StdDisplayEnvironment* JonesMain_pDisplayEnv = NULL; // Added: Ini static StdDisplayEnvironment* JonesMain_pStartupDisplayEnv = NULL; // Added: Init. to NULL static tCircularBuffer JonesMain_circBuf; -static FILE* JonesMain_pLogFile = NULL; // Added: Init. to NULL +static FILE* JonesMain_pLogFile = NULL; // Added: Init. to NULL static bool JonesMain_bGamePaused = false; // Added: Init to false static bool JonesMain_bAssertTriggered = false; // Added: Init to false @@ -434,8 +439,16 @@ int J3DAPI JonesMain_Restore(const char* pNdsFilePath); int JonesMain_ProcessStartGame(void); int JonesMain_ProcessCredits(void); -int J3DAPI JonesMain_FilePrintf(const char* pFormat, ...); +int J3DAPI JonesMain_Log(int textColor, const char* pText); +int J3DAPI JonesMain_LogError(const char* pFormat, ...); +int J3DAPI JonesMain_LogWarning(const char* pFormat, ...); +int J3DAPI JonesMain_LogStatus(const char* pFormat, ...); +int J3DAPI JonesMain_LogDebug(const char* pFormat, ...); int J3DAPI JonesMain_NoLog(const char* pFormat, ...); // Added +int J3DAPI JonesMain_FilePrintf(const char* pFormat, ...); + +void J3DAPI JonesMain_LogErrorToFile(const char* pErrorText); +J3DNORETURN void J3DAPI JonesMain_Assert(const char* pErrorText, const char* pSrcFile, int line); int J3DAPI JonesMain_IntroWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, int* pRetValue); void J3DAPI JonesMain_IntroHandleWMLButtonUp(HWND hwnd, uint16_t curPosX, uint16_t curPosY, WPARAM mvk); @@ -498,7 +511,7 @@ void JonesMain_InstallHooks(void) J3D_HOOKFUNC(JonesMain_GetDisplaySettings); J3D_HOOKFUNC(JonesMain_CloseWindow); J3D_HOOKFUNC(JonesMain_FilePrintf); - J3D_HOOKFUNC(JonesMain_Log); + J3D_HOOKFUNC(JonesMain_LogError); J3D_HOOKFUNC(JonesMain_RefreshDisplayDevice); J3D_HOOKFUNC(JonesMain_PlayIntroMovie); J3D_HOOKFUNC(JonesMain_IntroWndProc); @@ -691,22 +704,26 @@ int J3DAPI JonesMain_Startup(const char* lpCmdLine) switch ( JonesMain_state.logLevel ) { case JONES_LOGLEVEL_ERROR: - JonesMain_hs.pErrorPrint = JonesMain_Log; + JonesMain_hs.pErrorPrint = JonesMain_LogError; break; case JONES_LOGLEVEL_NORMAL: - JonesMain_hs.pErrorPrint = JonesMain_Log; - JonesMain_hs.pStatusPrint = stdConsolePrintf; + JonesMain_hs.pErrorPrint = JonesMain_LogError; + JonesMain_hs.pWarningPrint = JonesMain_LogWarning; // Added + JonesMain_hs.pStatusPrint = JonesMain_LogStatus; // Changed: OG stdConsolePrintf break; case JONES_LOGLEVEL_VERBOSE: - JonesMain_hs.pErrorPrint = JonesMain_Log; - JonesMain_hs.pStatusPrint = stdConsolePrintf; - JonesMain_hs.pDebugPrint = stdConsolePrintf; + JonesMain_hs.pErrorPrint = JonesMain_LogError; + JonesMain_hs.pWarningPrint = JonesMain_LogWarning; // Added + JonesMain_hs.pStatusPrint = JonesMain_LogStatus; // Changed: OG stdConsolePrintf + JonesMain_hs.pDebugPrint = JonesMain_LogDebug; // Changed: OG stdConsolePrintf break; default: break; // No logging }; + // Startup console stdConsole_Startup("Debug", FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN, /*bShowMinimized=*/0); // White text + } break; case JONES_OUTPUTMODE_LOGFILE: @@ -715,16 +732,18 @@ int J3DAPI JonesMain_Startup(const char* lpCmdLine) switch ( JonesMain_state.logLevel ) { case JONES_LOGLEVEL_ERROR: - JonesMain_hs.pErrorPrint = JonesMain_Log; + JonesMain_hs.pErrorPrint = JonesMain_LogError; break; case JONES_LOGLEVEL_NORMAL: - JonesMain_hs.pErrorPrint = JonesMain_Log; - JonesMain_hs.pStatusPrint = JonesMain_FilePrintf; + JonesMain_hs.pErrorPrint = JonesMain_LogError; + JonesMain_hs.pWarningPrint = JonesMain_LogWarning; // Added + JonesMain_hs.pStatusPrint = JonesMain_LogStatus; // Changed: OG JonesMain_FilePrintf break; case JONES_LOGLEVEL_VERBOSE: - JonesMain_hs.pErrorPrint = JonesMain_Log; - JonesMain_hs.pStatusPrint = JonesMain_FilePrintf; - JonesMain_hs.pDebugPrint = JonesMain_FilePrintf; + JonesMain_hs.pErrorPrint = JonesMain_LogError; + JonesMain_hs.pWarningPrint = JonesMain_LogWarning; // Added + JonesMain_hs.pStatusPrint = JonesMain_LogStatus; // Changed: OG JonesMain_FilePrintf + JonesMain_hs.pDebugPrint = JonesMain_LogDebug; // Changed: OG JonesMain_FilePrintf break; default: break; // No logging @@ -745,7 +764,7 @@ int J3DAPI JonesMain_Startup(const char* lpCmdLine) JonesMain_hs.pStatusPrint = JonesMain_NoLog; JonesMain_hs.pWarningPrint = JonesMain_NoLog; JonesMain_hs.pDebugPrint = JonesMain_NoLog; - JonesMain_hs.pErrorPrint = JonesMain_Log; + JonesMain_hs.pErrorPrint = JonesMain_LogError; break; default: break; // No logging @@ -2109,6 +2128,20 @@ int JonesMain_CloseWindow(void) return PostMessage(stdWin95_GetWindow(), WM_CLOSE, 0, 0); } +int J3DAPI JonesMain_Log(int textColor, const char* ptext) +{ + if ( JonesMain_state.outputMode == JONES_OUTPUTMODE_CONSOLE ) + { + stdConsole_WriteConsole(ptext, textColor); + } + else if ( JonesMain_state.outputMode == JONES_OUTPUTMODE_LOGFILE ) + { + JonesMain_FilePrintf(ptext); + } + + return strlen(ptext); +} + int JonesMain_FilePrintf(const char* pFormat, ...) { // TODO: Check if file is open? Tho, the startup should take care of that @@ -2122,7 +2155,7 @@ int JonesMain_FilePrintf(const char* pFormat, ...) return STD_ARRAYLEN(std_g_genBuffer); } -int JonesMain_Log(const char* pFormat, ...) +int JonesMain_LogError(const char* pFormat, ...) { va_list args; va_start(args, pFormat); @@ -2136,17 +2169,58 @@ int JonesMain_Log(const char* pFormat, ...) stdUtil_StringCopy(pString, 128, std_g_genBuffer); } - if ( JonesMain_state.outputMode == JONES_OUTPUTMODE_CONSOLE ) + return JonesMain_Log(JONES_LOGCONSOLE_ERRORCOLOR, std_g_genBuffer); +} + +int J3DAPI JonesMain_LogWarning(const char* pFormat, ...) +{ + va_list args; + va_start(args, pFormat); + vsnprintf_s(std_g_genBuffer, STD_ARRAYLEN(std_g_genBuffer), STD_ARRAYLEN(std_g_genBuffer) - 1, pFormat, args); + va_end(args); // Fixed: Add missing call to va_end + + // TODO: What's the point of using circbuf?? + char* pString = (char*)stdCircBuf_GetNextElement(&JonesMain_circBuf); + if ( pString ) + { + stdUtil_StringCopy(pString, 128, std_g_genBuffer); + } + + return JonesMain_Log(JONES_LOGCONSOLE_WARNINGRCOLOR, std_g_genBuffer); +} + +int J3DAPI JonesMain_LogStatus(const char* pFormat, ...) +{ + va_list args; + va_start(args, pFormat); + vsnprintf_s(std_g_genBuffer, STD_ARRAYLEN(std_g_genBuffer), STD_ARRAYLEN(std_g_genBuffer) - 1, pFormat, args); + va_end(args); // Fixed: Add missing call to va_end + + // TODO: What's the point of using circbuf?? + char* pString = (char*)stdCircBuf_GetNextElement(&JonesMain_circBuf); + if ( pString ) { - stdConsole_WriteConsole(std_g_genBuffer, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); + stdUtil_StringCopy(pString, 128, std_g_genBuffer); } - else if ( JonesMain_state.outputMode == JONES_OUTPUTMODE_LOGFILE ) + return JonesMain_Log(JONES_LOGCONSOLE_STATUSCOLOR, std_g_genBuffer); +} + +int J3DAPI JonesMain_LogDebug(const char* pFormat, ...) +{ + va_list args; + va_start(args, pFormat); + vsnprintf_s(std_g_genBuffer, STD_ARRAYLEN(std_g_genBuffer), STD_ARRAYLEN(std_g_genBuffer) - 1, pFormat, args); + va_end(args); // Fixed: Add missing call to va_end + + // TODO: What's the point of using circbuf?? + char* pString = (char*)stdCircBuf_GetNextElement(&JonesMain_circBuf); + if ( pString ) { - JonesMain_FilePrintf(std_g_genBuffer); + stdUtil_StringCopy(pString, 128, std_g_genBuffer); } - return strlen(std_g_genBuffer); + return JonesMain_Log(JONES_LOGCONSOLE_DEBUGCOLOR, std_g_genBuffer); } int JonesMain_NoLog(const char* pFormat, ...) diff --git a/Jones3D/Main/JonesMain.h b/Jones3D/Main/JonesMain.h index b1118ce..8157302 100644 --- a/Jones3D/Main/JonesMain.h +++ b/Jones3D/Main/JonesMain.h @@ -52,9 +52,6 @@ int JonesMain_PlayIntroMovie(void); void J3DAPI JonesMain_BindToggleMenuControlKeys(const int* paKeyIds, int numKeys); -int J3DAPI JonesMain_Log(const char* pFormat, ...); -void J3DAPI JonesMain_LogErrorToFile(const char* pErrorText); -J3DNORETURN void J3DAPI JonesMain_Assert(const char* pErrorText, const char* pSrcFile, int line); J3DNORETURN void JonesMain_FatalErrorUnknown(void); // Added int J3DAPI JonesMain_ShowDevDialog(HWND hWnd, JonesState* pConfig); diff --git a/Jones3D/RTI/addresses.h b/Jones3D/RTI/addresses.h index 84a8fc0..d551845 100644 --- a/Jones3D/RTI/addresses.h +++ b/Jones3D/RTI/addresses.h @@ -134,7 +134,7 @@ #define JonesMain_GetDisplaySettings_ADDR 0x0041EC30 #define JonesMain_CloseWindow_ADDR 0x0041EC40 #define JonesMain_FilePrintf_ADDR 0x0041EC60 -#define JonesMain_Log_ADDR 0x0041ECB0 +#define JonesMain_LogError_ADDR 0x0041ECB0 #define JonesMain_RefreshDisplayDevice_ADDR 0x0041ED50 #define JonesMain_PlayIntroMovie_ADDR 0x0041ED60 #define JonesMain_IntroWndProc_ADDR 0x0041F4D0 diff --git a/Jones3D/RTI/symbols.h b/Jones3D/RTI/symbols.h index a99847f..1d46f18 100644 --- a/Jones3D/RTI/symbols.h +++ b/Jones3D/RTI/symbols.h @@ -124,7 +124,7 @@ #define JonesMain_GetDisplaySettings_TYPE JonesDisplaySettings* (*)(void) #define JonesMain_CloseWindow_TYPE int (*)(void) #define JonesMain_FilePrintf_TYPE int (J3DAPI*)(const char*, ...) -#define JonesMain_Log_TYPE int (J3DAPI*)(const char*, ...) +#define JonesMain_LogError_TYPE int (J3DAPI*)(const char*, ...) #define JonesMain_RefreshDisplayDevice_TYPE void (*)(void) #define JonesMain_PlayIntroMovie_TYPE int (*)(void) #define JonesMain_IntroWndProc_TYPE int (J3DAPI*)(HWND, UINT, WPARAM, LPARAM, int*) From 961558bce7c7894d08e288364e65c7839bc9967c Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sun, 15 Dec 2024 12:35:50 +0100 Subject: [PATCH 13/35] [Jones3D] Add debugger log output --- Jones3D/Main/JonesMain.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index 31a1be5..06f81da 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -2130,6 +2130,7 @@ int JonesMain_CloseWindow(void) int J3DAPI JonesMain_Log(int textColor, const char* ptext) { + OutputDebugString(ptext); // Added if ( JonesMain_state.outputMode == JONES_OUTPUTMODE_CONSOLE ) { stdConsole_WriteConsole(ptext, textColor); From 6c47508d0371ff110b69351d8643ba917a739a80 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sun, 15 Dec 2024 12:42:17 +0100 Subject: [PATCH 14/35] [sith] Remove material buffer size constraint Removes the material 64 buffer size constraint in `sithMaterial_LoadMaterialsText` --- Libs/sith/World/sithMaterial.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Libs/sith/World/sithMaterial.c b/Libs/sith/World/sithMaterial.c index 9d6e675..e4d6b12 100644 --- a/Libs/sith/World/sithMaterial.c +++ b/Libs/sith/World/sithMaterial.c @@ -98,9 +98,9 @@ void J3DAPI sithMaterial_FreeWorldMaterials(SithWorld* pWorld) int J3DAPI sithMaterial_WriteMaterialsText(const SithWorld* pWorld) { if ( stdConffile_WriteLine("##### Material information #####\n") - || stdConffile_WriteLine("SECTION: MATERIALS\n\n") - || stdConffile_Printf("World materials %d\n\n", pWorld->numMaterials + 64) - || stdConffile_WriteLine("#num:\tmat:\n") ) + || stdConffile_WriteLine("SECTION: MATERIALS\n\n") + || stdConffile_Printf("World materials %d\n\n", pWorld->numMaterials + 64) + || stdConffile_WriteLine("#num:\tmat:\n") ) { return 1; } @@ -176,11 +176,7 @@ int J3DAPI sithMaterial_LoadMaterialsText(SithWorld* pWorld, int bSkip) sithWorld_UpdateLoadProgress(progress); } - const size_t extraBuf = SITHMATERIAL_EXTRABUFFERSIZE + ((numMaterials < pWorld->sizeMaterials) ? SITHMATERIAL_EXTRABUFFERSIZE_HDMODELS : 0); // Changed: Adds SITHMATERIAL_EXTRABUFFERSIZE_HDMODELS if allocated size > requested size - if ( pWorld->sizeMaterials != (curMatNum + extraBuf) && pWorld->sizeMaterials != curMatNum )// TODO: remove this buffer size limitation - { - return 1; - } + // Removed: The SITHMATERIAL_EXTRABUFFERSIZE constraint was removed // Success sithWorld_UpdateLoadProgress(50.0f); From 669904d096de816174d62351d87ca4856178576d Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Wed, 18 Dec 2024 00:00:00 +0100 Subject: [PATCH 15/35] [sith] Fixed model index type of `sithModel_GetModelByIndexWrap` Fixes retrieving model by index in code that is not implemented yet (sithDSS*). --- Libs/sith/RTI/symbols.h | 2 +- Libs/sith/World/sithModel.c | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Libs/sith/RTI/symbols.h b/Libs/sith/RTI/symbols.h index e8657ad..c552507 100644 --- a/Libs/sith/RTI/symbols.h +++ b/Libs/sith/RTI/symbols.h @@ -966,7 +966,7 @@ #define sithModel_Load_TYPE rdModel3* (J3DAPI*)(const char*, int) #define sithModel_GetModelMemUsage_TYPE size_t (J3DAPI*)(const rdModel3*) #define sithModel_AllocWorldModels_TYPE int (J3DAPI*)(SithWorld*, size_t) -#define sithModel_GetModelByIndex_TYPE rdModel3* (J3DAPI*)(size_t) +#define sithModel_GetModelByIndex_TYPE rdModel3* (J3DAPI*)(uint16_t) #define sithModel_GetModelIndex_TYPE int16_t (J3DAPI*)(const rdModel3*) #define sithModel_GetModel_TYPE rdModel3* (J3DAPI*)(const char*) #define sithModel_GetMeshIndex_TYPE int (J3DAPI*)(const rdModel3*, const char*) diff --git a/Libs/sith/World/sithModel.c b/Libs/sith/World/sithModel.c index 2541a47..2854382 100644 --- a/Libs/sith/World/sithModel.c +++ b/Libs/sith/World/sithModel.c @@ -6,12 +6,14 @@ #include #include -#include #include #include #include #include #include +#include + +#include #define SITHMODEL_DEFAULTMODEL "dflt.3do" #define SITHMODEL_EXTRABUFFERSIZE 32 @@ -24,6 +26,13 @@ void J3DAPI sithModel_CacheRemove(const rdModel3* pModel); void J3DAPI sithModel_CacheAdd(rdModel3* pModel); rdModel3* J3DAPI sithModel_CacheFind(const char* pName); +// TODO: this is just temp wrapper for sithModel_GetModelByIndex with uint16_t modelIdx. +// Can be removed after all DSS functions that calls sithModel_GetModelByIndex are implemented +rdModel3* J3DAPI sithModel_GetModelByIndexWrap(uint16_t modelIdx) +{ + return sithModel_GetModelByIndex(modelIdx); +} + void sithModel_InstallHooks(void) { J3D_HOOKFUNC(sithModel_Startup); @@ -35,7 +44,8 @@ void sithModel_InstallHooks(void) J3D_HOOKFUNC(sithModel_Load); J3D_HOOKFUNC(sithModel_GetModelMemUsage); J3D_HOOKFUNC(sithModel_AllocWorldModels); - J3D_HOOKFUNC(sithModel_GetModelByIndex); + //J3D_HOOKFUNC(sithModel_GetModelByIndex); + J3DHookFunction(sithModel_GetModelByIndex_ADDR, (void*)sithModel_GetModelByIndexWrap); J3D_HOOKFUNC(sithModel_GetModelIndex); J3D_HOOKFUNC(sithModel_GetModel); J3D_HOOKFUNC(sithModel_GetMeshIndex); @@ -66,8 +76,8 @@ void sithModel_Shutdown(void) int J3DAPI sithModel_WriteModelsText(const SithWorld* pWorld) { if ( stdConffile_WriteLine("###### Models information ######\n") - || stdConffile_WriteLine("Section: MODELS\n\n") - || stdConffile_Printf("World models %d\n\n", pWorld->numModels + SITHMODEL_EXTRABUFFERSIZE) ) + || stdConffile_WriteLine("Section: MODELS\n\n") + || stdConffile_Printf("World models %d\n\n", pWorld->numModels + SITHMODEL_EXTRABUFFERSIZE) ) { return 1; } @@ -320,7 +330,7 @@ rdModel3* J3DAPI sithModel_GetModelByIndex(size_t modelIdx) modelIdx = SITHWORLD_FROM_STATICINDEX(modelIdx); } - if ( pWorld && !SITHWORLD_IS_STATICINDEX(modelIdx) && modelIdx < pWorld->numModels ) // "TOD: What's the point of !SITHWORLD_IS_STATICINDEX(modelIdx) check? + if ( pWorld && !SITHWORLD_IS_STATICINDEX(modelIdx) && modelIdx < pWorld->numModels ) // "TODO: What's the point of !SITHWORLD_IS_STATICINDEX(modelIdx) check? { return &pWorld->aModels[modelIdx]; } From facf7b1f7b18f6540be2433e24111ad4b8d6ba96 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Wed, 18 Dec 2024 00:07:54 +0100 Subject: [PATCH 16/35] [sith] Add system startup/shutdown guards --- Libs/sith/World/sithMaterial.c | 28 ++++++++++++++++++++++++---- Libs/sith/World/sithModel.c | 25 ++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Libs/sith/World/sithMaterial.c b/Libs/sith/World/sithMaterial.c index e4d6b12..8228b38 100644 --- a/Libs/sith/World/sithMaterial.c +++ b/Libs/sith/World/sithMaterial.c @@ -19,6 +19,7 @@ #define SITHMATERIAL_EXTRABUFFERSIZE 64u #define SITHMATERIAL_EXTRABUFFERSIZE_HDMODELS 32u // Added +static bool sithMaterial_bMaterialStartup = false; // Added static tHashTable* sithMaterial_pHashtable = NULL; // Added: Init to 0 rdMaterial* J3DAPI sithMaterial_CacheFind(const char* pName); @@ -46,21 +47,40 @@ void sithMaterial_ResetGlobals(void) int sithMaterial_Startup(void) { - // TODO: maybe add check for system already being started + // Added + if ( sithMaterial_bMaterialStartup ) + { + SITHLOG_ERROR("Multiple startup on material system.\n"); + return 1; + } + sithMaterial_pHashtable = stdHashtbl_New(SITHMATERIAL_TABLESIZE); - return sithMaterial_pHashtable == NULL; + if ( !sithMaterial_pHashtable ) // Added + { + SITHLOG_ERROR("Could not allocate material hashtable.\n"); + return 1; + } + + sithMaterial_bMaterialStartup = true; + return 0; } void sithMaterial_Shutdown(void) { - // TODO: maybe add check for system already being shut down + // Added + if ( !sithMaterial_bMaterialStartup ) + { + SITHLOG_ERROR("Material system not started.\n"); + return; + } if ( sithMaterial_pHashtable ) { stdHashtbl_Free(sithMaterial_pHashtable); + sithMaterial_pHashtable = NULL; } - sithMaterial_pHashtable = NULL; + sithMaterial_bMaterialStartup = false; } void J3DAPI sithMaterial_FreeWorldMaterials(SithWorld* pWorld) diff --git a/Libs/sith/World/sithModel.c b/Libs/sith/World/sithModel.c index 2854382..d948c4e 100644 --- a/Libs/sith/World/sithModel.c +++ b/Libs/sith/World/sithModel.c @@ -18,6 +18,7 @@ #define SITHMODEL_DEFAULTMODEL "dflt.3do" #define SITHMODEL_EXTRABUFFERSIZE 32 +static bool sithModel_bModelStartup = false; // Added static bool sithModel_bHiPoly; static tHashTable* sithModel_pHashtable = NULL; // Added: Init to null @@ -60,17 +61,39 @@ void sithModel_ResetGlobals(void) int sithModel_Startup(void) { + // Added + if ( sithModel_bModelStartup ) + { + SITHLOG_ERROR("Multiple startup on model system.\n"); + return 1; + } + sithModel_pHashtable = stdHashtbl_New(256u); - return sithModel_pHashtable == NULL; + if ( !sithModel_pHashtable ) // Added + { + SITHLOG_ERROR("Could not allocate model hashtable.\n"); + return 1; + } + + sithModel_bModelStartup = true; + return 0; } void sithModel_Shutdown(void) { + if ( !sithModel_bModelStartup ) + { + SITHLOG_ERROR("Material system not started.\n"); + return; + } + if ( sithModel_pHashtable ) { stdHashtbl_Free(sithModel_pHashtable); sithModel_pHashtable = NULL; } + + sithModel_bModelStartup = false; } int J3DAPI sithModel_WriteModelsText(const SithWorld* pWorld) From 9e6ba778919aedfd3f21afec0f1cb463e630a709 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Wed, 18 Dec 2024 00:32:15 +0100 Subject: [PATCH 17/35] [std] Fix scaling color component Fixes upscaling color component when converting row. --- Libs/std/General/stdColor.c | 60 +++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/Libs/std/General/stdColor.c b/Libs/std/General/stdColor.c index b5dfe2a..2daa23c 100644 --- a/Libs/std/General/stdColor.c +++ b/Libs/std/General/stdColor.c @@ -194,6 +194,23 @@ void J3DAPI stdColor_HSVtoRGB(float hue, float saturation, float value, float* r } } +uint32_t J3DAPI stdColor_ScaleColorComponent(uint32_t cc, int srcBPP, int deltaBPP) +{ + if ( deltaBPP <= 0 ) // Upscale + { + // Fixed: Fixed scaling to get correct value from lower bpp. + // Original was calculated only "cc >> -deltaBPP" which resulted in dimmer colors + int dsrcBPP = srcBPP + deltaBPP; + return (cc << -deltaBPP) + | (dsrcBPP >= 0 + ? (cc >> dsrcBPP) + : (cc * ((1 << -deltaBPP) - 1))); // Note: works for 1 bit, but might fail for 2 bit & 3 bit + } + + // Downscale + return cc >> deltaBPP; +} + void J3DAPI stdColor_ColorConvertOneRow(uint8_t* pDestRow, const ColorInfo* pDestInfo, const uint8_t* pSrcRow, const ColorInfo* pSrcInfo, int width, int bColorKey, LPDDCOLORKEY pColorKey) { unsigned int redMask = 0xFFFFFFFF >> (32 - (pSrcInfo->redBPP & 0xFF)); @@ -201,6 +218,7 @@ void J3DAPI stdColor_ColorConvertOneRow(uint8_t* pDestRow, const ColorInfo* pDes unsigned int blueMask = 0xFFFFFFFF >> (32 - (pSrcInfo->blueBPP & 0xFF)); unsigned int alphaMask = 0; // Added: Zero init unsigned int maxAlphaValue = 0; // Added: Zero init + if ( pSrcInfo->alphaBPP ) { alphaMask = 0xFFFFFFFF >> (32 - (pSrcInfo->alphaBPP & 0xFF)); @@ -223,7 +241,6 @@ void J3DAPI stdColor_ColorConvertOneRow(uint8_t* pDestRow, const ColorInfo* pDes alphaDelta = pSrcInfo->alphaBPP - pDestInfo->alphaBPP; } - for ( int i = 0; i < width; ++i ) { unsigned int pixel = 0; @@ -252,35 +269,18 @@ void J3DAPI stdColor_ColorConvertOneRow(uint8_t* pDestRow, const ColorInfo* pDes break; } - unsigned int r = redMask & (pixel >> pSrcInfo->redPosShift); - unsigned int g = greenMask & (pixel >> pSrcInfo->greenPosShift); - unsigned int b = blueMask & (pixel >> pSrcInfo->bluePosShift); - unsigned int a = 0; // Added: Zero init + uint32_t r = redMask & (pixel >> pSrcInfo->redPosShift); + uint32_t g = greenMask & (pixel >> pSrcInfo->greenPosShift); + uint32_t b = blueMask & (pixel >> pSrcInfo->bluePosShift); + uint32_t a = 0; // Added: Zero init if ( pSrcInfo->alphaBPP ) { a = alphaMask & (pixel >> pSrcInfo->alphaPosShift); } - if ( redDelta <= 0 ) { - r <<= -(char)redDelta; - } - else { - r >>= redDelta; - } - - if ( greenDelta <= 0 ) { - g <<= -(char)greenDelta; - } - else { - g >>= greenDelta; - } - - if ( redDelta <= 0 ) { - b <<= -(char)blueDelta; - } - else { - b >>= blueDelta; - } + r = stdColor_ScaleColorComponent(r, pSrcInfo->redBPP, redDelta); + g = stdColor_ScaleColorComponent(g, pSrcInfo->greenBPP, greenDelta); + b = stdColor_ScaleColorComponent(b, pSrcInfo->blueBPP, blueDelta); pixel = (b << pDestInfo->bluePosShift) | (g << pDestInfo->greenPosShift) | (r << pDestInfo->redPosShift); if ( pSrcInfo->alphaBPP ) @@ -288,18 +288,12 @@ void J3DAPI stdColor_ColorConvertOneRow(uint8_t* pDestRow, const ColorInfo* pDes if ( bColorKey ) { if ( a < maxAlphaValue ) { - pixel = (unsigned int)pColorKey; + pixel = pColorKey->dwColorSpaceLowValue; } } else { - if ( alphaDelta <= 0 ) { - a <<= -(char)alphaDelta; - } - else { - a >>= alphaDelta; - } - + a = stdColor_ScaleColorComponent(a, pSrcInfo->alphaBPP, alphaDelta); pixel |= a << pDestInfo->alphaPosShift; } } From 91d0a875c1f27194a0ef2648034cc35889f26b3d Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Wed, 18 Dec 2024 01:06:51 +0100 Subject: [PATCH 18/35] [sith] Add support for 24/32 bit textures in CND files --- Libs/sith/World/sithMaterial.c | 42 +++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/Libs/sith/World/sithMaterial.c b/Libs/sith/World/sithMaterial.c index 8228b38..32c64ea 100644 --- a/Libs/sith/World/sithMaterial.c +++ b/Libs/sith/World/sithMaterial.c @@ -216,17 +216,18 @@ int J3DAPI sithMaterial_WriteMaterialsBinary(tFileHandle fh, SithWorld* pWorld) { goto error; } - memset(aMatInfos, 0, sizeInfos); - // Calculate the required pixeldata buffer size for all materials + memset(aMatInfos, 0, sizeInfos); CndMaterialInfo* pCurInfo = aMatInfos; + + // Calculate the required pixeldata buffer size for all materials + size_t sizePixelBuffers = 0; for ( size_t i = 0; i < pWorld->numMaterials; ++i ) { const rdMaterial* pMat = &pWorld->aMaterials[i]; pCurInfo->numMipLevels = std3D_GetMipMapCount(pMat->aTextures); - - size_t texSize = 2 * rdMaterial_GetMipSize(pMat->width, pMat->height, pCurInfo->numMipLevels) * pMat->numCels; // TODO: 2 - is bpp of pixel size, should be taken from Mat colorInfo + size_t texSize = 4 * rdMaterial_GetMipSize(pMat->width, pMat->height, pCurInfo->numMipLevels) * pMat->numCels; // Changed: Raised pixel size to 4 bytes from 2 bytes to allow 32 bit textures sizePixelBuffers += texSize; ++pCurInfo; } @@ -236,10 +237,13 @@ int J3DAPI sithMaterial_WriteMaterialsBinary(tFileHandle fh, SithWorld* pWorld) { goto error; } - memset(aPixelBuffers, 0, sizePixelBuffers); + memset(aPixelBuffers, 0, sizePixelBuffers); uint8_t* pCurPixelBuffer = aPixelBuffers; + + // Reset cur info pointer pCurInfo = aMatInfos; + sizePixelBuffers = 0; // Added: Reset to 0 as the exact buffer size will be recalculated in the following loop // Loop goes over all world materials and collects for each material it's raster info and pixeldata for ( size_t i = 0; i < pWorld->numMaterials; ++i ) @@ -278,9 +282,8 @@ int J3DAPI sithMaterial_WriteMaterialsBinary(tFileHandle fh, SithWorld* pWorld) } // Now read pixledata if mat from mat file. - // TODO: Check if there is an option to get pixledata from in memory mat - - size_t texSize = 2 * rdMaterial_GetMipSize(pMat->width, pMat->height, pCurInfo->numMipLevels);// // TODO: 2 - is bpp of pixel size, should be taken from Mat colorInfo + // TODO: maybe a check should be made for colorInfo.bpp <= 32? + size_t texSize = (matHeader.colorInfo.bpp / 8) * rdMaterial_GetMipSize(pMat->width, pMat->height, pCurInfo->numMipLevels); // Fixed: Using actual pixel size from first texture; was hardcoded to 2 bytes - 16 bpp for ( size_t j = 0; j < pMat->numCels; ++j ) { if ( sith_g_pHS->pFileSeek(fhMat, sizeof(rdMatTextureHeader), 1) ) @@ -297,6 +300,9 @@ int J3DAPI sithMaterial_WriteMaterialsBinary(tFileHandle fh, SithWorld* pWorld) pCurPixelBuffer += texSize; } + // Added: Adds correct mat pixledata size to buffer. This allows having different pixel sizes. Before was hardcoded to 16 bpp + sizePixelBuffers += texSize * pMat->numCels; + sith_g_pHS->pFileClose(fhMat); fhMat = 0; @@ -375,7 +381,6 @@ int J3DAPI sithMaterial_LoadMaterialsBinary(tFileHandle fh, SithWorld* pWorld) } memset(aMatInfos, 0, sizeInfos); - // Read the size of pixeldata buffer from CND file size_t sizePixelBuffers; size_t nRead = sith_g_pHS->pFileRead(fh, &sizePixelBuffers, sizeof(uint32_t)); @@ -418,8 +423,10 @@ int J3DAPI sithMaterial_LoadMaterialsBinary(tFileHandle fh, SithWorld* pWorld) rdMaterial* pMat = sithMaterial_CacheFind(pCurInfo->aName); if ( pMat ) { - // Matirial already loaded, skip - size_t texSize = rdMaterial_GetMipSize(pMat->width, pMat->height, pCurInfo->numMipLevels) * 2 * pMat->numCels; // TODO: [BUG] the mipmap size should be multiplied by texture BPP in bytes instead of 2 aka 16 bpp + // Material already loaded, skip + size_t texSize = (pCurInfo->colorInfo.bpp / 8) // Fixed: Calculate exact pixel size. Was hardcoded to 2 bytes - 16 bpp + * rdMaterial_GetMipSize(pMat->width, pMat->height, pCurInfo->numMipLevels) + * pMat->numCels; pCurPixelBuffer += texSize; // Increase cur pixeldata pos pWorld->apMatArray[matNum] = pMat; } @@ -481,18 +488,17 @@ int J3DAPI sithMaterial_LoadMaterialsBinary(tFileHandle fh, SithWorld* pWorld) { memcpy(&apBuffers[j]->rasterInfo.colorInfo, &rasterInfo.colorInfo, sizeof(apBuffers[j]->rasterInfo.colorInfo)); - // TODO: [BUG] after copying the colorInfo also the VBuffer rowSize and size should be updated - // TODO: update raster size info - // (*ppCurVBuffer)->rowSize = ((*ppCurVBuffer)->rasterInfo.bpp >> 3) * (*ppCurVBuffer)->width; - // (*ppCurVBuffer)->size = (*ppCurVBuffer)->rowSize * (*ppCurVBuffer)->height; + // Fixed: Update row size and size before calling to match the new color info + apBuffers[j]->rasterInfo.rowSize = (apBuffers[j]->rasterInfo.colorInfo.bpp / 8) * apBuffers[j]->rasterInfo.width; + apBuffers[j]->rasterInfo.size = apBuffers[j]->rasterInfo.rowSize * apBuffers[j]->rasterInfo.height; stdDisplay_VBufferLock(apBuffers[j]); memcpy(apBuffers[j]->pPixels, pCurPixelBuffer, apBuffers[j]->rasterInfo.size); stdDisplay_VBufferUnlock(apBuffers[j]); - tVBuffer* pVBuffer = stdDisplay_VBufferConvertColorFormat(&desiredColorFormat, apBuffers[j], bHasColorKey, pColorKey); - apBuffers[j] = pVBuffer; - pCurPixelBuffer += apBuffers[j]->rasterInfo.size;// TODO: [BUG] the buffer should be incremented before call to stdDisplay_VBufferConvertColorFormat as the apBuffers[k]->rasterInfo could be different format here + // Now convert pixeldata to desired color format + pCurPixelBuffer += apBuffers[j]->rasterInfo.size; // Fixed: Increment pCurPixelBuffer before assigning new tVBuffer to apBuffers[j] + apBuffers[j] = stdDisplay_VBufferConvertColorFormat(&desiredColorFormat, apBuffers[j], bHasColorKey, pColorKey); } std3D_AllocSystemTexture(&pMat->aTextures[i], apBuffers, pCurInfo->numMipLevels, pMat->formatType); From 3167b79eb51af44edde84dc8e5796c8ac57f7bb0 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Thu, 19 Dec 2024 02:11:33 +0100 Subject: [PATCH 19/35] [std] Fix initializing `std3D_g_maxVertices` --- Libs/std/Win95/std3D.c | 55 +----------------------------------------- 1 file changed, 1 insertion(+), 54 deletions(-) diff --git a/Libs/std/Win95/std3D.c b/Libs/std/Win95/std3D.c index d2b85e2..f3f8d0e 100644 --- a/Libs/std/Win95/std3D.c +++ b/Libs/std/Win95/std3D.c @@ -309,62 +309,9 @@ void std3D_InstallHooks(void) void std3D_ResetGlobals(void) { - /*const ColorInfo std3D_cfRGB565_tmp = { STDCOLOR_RGB, 16, 5, 6, 5, 11, 5, 0, 3, 2, 3, 0, 0, 0 }; - memcpy((ColorInfo*)&std3D_cfRGB565, &std3D_cfRGB565_tmp, sizeof(std3D_cfRGB565)); - - const ColorInfo std3D_cfRGB5551_tmp = { STDCOLOR_RGBA, 16, 5, 5, 5, 11, 6, 1, 3, 3, 3, 1, 0, 7 }; - memcpy((ColorInfo*)&std3D_cfRGB5551, &std3D_cfRGB5551_tmp, sizeof(std3D_cfRGB5551)); - - const ColorInfo std3D_cfRGB4444_tmp = { STDCOLOR_RGBA, 16, 4, 4, 4, 12, 8, 4, 4, 4, 4, 4, 0, 4 }; - memcpy((ColorInfo*)&std3D_cfRGB4444, &std3D_cfRGB4444_tmp, sizeof(std3D_cfRGB4444)); - - const DXStatus std3D_aD3DStatusTbl_tmp[186] = { - - memcpy((DXStatus*)&std3D_aD3DStatusTbl, &std3D_aD3DStatusTbl_tmp, sizeof(std3D_aD3DStatusTbl));*/ - float std3D_g_fogDensity_tmp = 1.0f; memcpy(&std3D_g_fogDensity, &std3D_g_fogDensity_tmp, sizeof(std3D_g_fogDensity)); - - /*int std3D_texFilterMode_tmp = -1; - memcpy(&std3D_mipmapFilter, &std3D_texFilterMode_tmp, sizeof(std3D_mipmapFilter)); - - int std3D_bRenderFog_tmp = 1; - memcpy(&std3D_bRenderFog, &std3D_bRenderFog_tmp, sizeof(std3D_bRenderFog)); - - int std3D_frameCount_tmp = 1; - memcpy(&std3D_frameCount, &std3D_frameCount_tmp, sizeof(std3D_frameCount)); - - memset(&std3D_curDevice, 0, sizeof(std3D_curDevice)); - memset(&std3D_RGBATextureFormat, 0, sizeof(std3D_RGBATextureFormat)); - memset(&std3D_zDepth, 0, sizeof(std3D_zDepth)); - memset(&std3D_RGBTextureFormat, 0, sizeof(std3D_RGBTextureFormat)); - memset(&std3D_RGBAKeyTextureFormat, 0, sizeof(std3D_RGBAKeyTextureFormat)); - memset(&std3D_fogStartDepth, 0, sizeof(std3D_fogStartDepth)); - memset(&std3D_numDevices, 0, sizeof(std3D_numDevices)); - memset(&std3D_aTextureFormats, 0, sizeof(std3D_aTextureFormats)); - memset(&std3D_fogDepthFactor, 0, sizeof(std3D_fogDepthFactor)); - memset(&std3D_aDevices, 0, sizeof(std3D_aDevices)); - memset(&std3D_renderState, 0, sizeof(std3D_renderState)); - memset(&std3D_activeRect, 0, sizeof(std3D_activeRect)); - memset(&std3D_fogEndDepth, 0, sizeof(std3D_fogEndDepth)); - memset(&std3D_pCurDevice, 0, sizeof(std3D_pCurDevice)); - memset(&std3D_numTextureFormats, 0, sizeof(std3D_numTextureFormats)); - memset(&std3D_bHasRGBTextureFormat, 0, sizeof(std3D_bHasRGBTextureFormat)); - memset(&std3D_bFogTable, 0, sizeof(std3D_bFogTable));*/ - - /* memset(&std3D_g_maxVertices, 0, sizeof(std3D_g_maxVertices)); - memset(&std3D_pD3DTex, 0, sizeof(std3D_pD3DTex)); - memset(&std3D_bFindAllD3Devices, 0, sizeof(std3D_bFindAllD3Devices)); - memset(&std3D_numCachedTextures, 0, sizeof(std3D_numCachedTextures)); - memset(&std3D_pFirstTexCache, 0, sizeof(std3D_pFirstTexCache)); - memset(&std3D_pLastTexCache, 0, sizeof(std3D_pLastTexCache)); - memset(&std3D_lpDD, 0, sizeof(std3D_lpDD)); - memset(&std3D_pDDPalette, 0, sizeof(std3D_pDDPalette)); - memset(&std3D_pDirect3D, 0, sizeof(std3D_pDirect3D)); - memset(&std3D_pD3Device, 0, sizeof(std3D_pD3Device)); - memset(&std3D_lpD3DViewPort, 0, sizeof(std3D_lpD3DViewPort)); - memset(&std3D_bStartup, 0, sizeof(std3D_bStartup)); - memset(&std3D_bOpen, 0, sizeof(std3D_bOpen));*/ + memset(&std3D_g_maxVertices, 0, sizeof(std3D_g_maxVertices)); } int std3D_Startup(void) From 249a416ee6fb4769912231d7932484d33e8e2c90 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Thu, 19 Dec 2024 02:12:40 +0100 Subject: [PATCH 20/35] [std] Update to use 32 bit color texture formats --- Libs/std/Win95/std3D.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Libs/std/Win95/std3D.c b/Libs/std/Win95/std3D.c index f3f8d0e..fded47a 100644 --- a/Libs/std/Win95/std3D.c +++ b/Libs/std/Win95/std3D.c @@ -484,14 +484,10 @@ int J3DAPI std3D_Open(size_t deviceNum) std3D_pLastTexCache = NULL; // Get color formats for RGB, RGBA and RGBA key formats - std3D_RGBTextureFormat = std3D_FindClosestFormat(&std3D_cfRGB565); - std3D_RGBAKeyTextureFormat = std3D_FindClosestFormat(&std3D_cfRGB5551); - std3D_RGBATextureFormat = std3D_FindClosestFormat(&std3D_cfRGB4444); - - //TODO: can be changed to 32 bit after fixing bug 16npp texture bug in sithMaterial_LoadMaterialsBinary - /* std3D_RGBTextureFormat = std3D_FindClosestFormat(&std3D_cfRGB888); - std3D_RGBAKeyTextureFormat = std3D_FindClosestFormat(&std3D_cfRGBA8888); - std3D_RGBATextureFormat = std3D_FindClosestFormat(&std3D_cfRGBA8888);*/ + // Changed: Use 32 bit formats; was using 16 bit + std3D_RGBTextureFormat = std3D_FindClosestFormat(&stdColor_cfBGR8888); + std3D_RGBAKeyTextureFormat = std3D_FindClosestFormat(&stdColor_cfRGBA8888); + std3D_RGBATextureFormat = std3D_FindClosestFormat(&stdColor_cfRGBA8888); if ( std3D_aTextureFormats[std3D_RGBAKeyTextureFormat].ci.alphaBPP == 0 && std3D_pCurDevice->bColorkeyTextureSupported ) From 8ac56fb919c171e6be37f237419c2b358ecf9900 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Thu, 19 Dec 2024 02:13:09 +0100 Subject: [PATCH 21/35] Code cleanup --- Jones3D/Play/jonesInventory.c | 2 ++ Libs/rdroid/Primitives/rdModel3.c | 34 +++---------------------------- Libs/std/General/stdPlatform.c | 4 ++-- Libs/std/Win95/stdDisplay.c | 7 ++++++- 4 files changed, 13 insertions(+), 34 deletions(-) diff --git a/Jones3D/Play/jonesInventory.c b/Jones3D/Play/jonesInventory.c index b92fc15..ca2df27 100644 --- a/Jones3D/Play/jonesInventory.c +++ b/Jones3D/Play/jonesInventory.c @@ -15,6 +15,8 @@ #include #include +#include + static SithGameStatistics* jonesInventory_pGameStatistics; static size_t jonesInventory_msecStatisticsGameTime; diff --git a/Libs/rdroid/Primitives/rdModel3.c b/Libs/rdroid/Primitives/rdModel3.c index 9230623..bb697c8 100644 --- a/Libs/rdroid/Primitives/rdModel3.c +++ b/Libs/rdroid/Primitives/rdModel3.c @@ -34,8 +34,7 @@ static float maxGeoRadius = 0.0; // Vars used for drawing model static rdModel3DrawFaceFunc pfDrawFace = &rdModel3_DrawFace; -static const rdThing* pCurThing = NULL; - +static const rdThing* pCurThing = NULL; static rdLightMode curLightingMode = RD_LIGHTING_NONE; static size_t numGeoLights = 0; @@ -361,7 +360,7 @@ int J3DAPI rdModel3_LoadEntry(const char* pFilename, rdModel3* pModel3) { float red, green, blue, alpha = 1.0f; if ( sscanf_s(stdConffile_g_aLine, " %d: %f %f %f %f %f %f %f", &entryNum, &x, &y, &z, &red, &green, &blue, &alpha) != 8 - && sscanf_s(stdConffile_g_aLine, " %d: %f %f %f %f %f %f", &entryNum, &x, &y, &z, &red, &green, &blue) != 7 ) + && sscanf_s(stdConffile_g_aLine, " %d: %f %f %f %f %f %f", &entryNum, &x, &y, &z, &red, &green, &blue) != 7 ) { goto syntax_error; } @@ -526,7 +525,7 @@ int J3DAPI rdModel3_LoadEntry(const char* pFilename, rdModel3* pModel3) float red, green, blue, alpha = 1.0f; const char* aRGB = strtok_r(NULL, " \t", &pntok); if ( sscanf_s(aRGB, "(%f/%f/%f/%f)", &red, &green, &blue, &alpha) != 4 - && sscanf_s(aRGB, "(%f/%f/%f)", &red, &green, &blue) != 3 ) { + && sscanf_s(aRGB, "(%f/%f/%f)", &red, &green, &blue) != 3 ) { goto syntax_error; } @@ -1736,34 +1735,7 @@ void J3DAPI rdModel3_DrawFace(const rdFace* pFace, const rdVector3* aTransformed if ( bIsBackFace ) { rdVector_Neg3Acc(&faceNormal); - - /* pNormal.x = -pFace->normal.x; - pNormal.y = -pFace->normal.y; - pNormal.z = -pFace->normal.z; - rdLight_CalcFaceIntensity( - rdCamera_g_pCurCamera->aLights, - aLocalLightPos, - rdCamera_g_pCurCamera->numLights, - pFace, - &pNormal, - pCurMesh->apVertices, - rdCamera_g_pCurCamera->attenuationMin, - &faceIntensity - );*/ } - /*else - { - rdLight_CalcFaceIntensity( - rdCamera_g_pCurCamera->aLights, - aLocalLightPos, - rdCamera_g_pCurCamera->numLights, - pFace, - &pFace->normal, - pCurMesh->apVertices, - rdCamera_g_pCurCamera->attenuationMin, - &faceIntensity - ); - }*/ rdVector4 faceIntensity = { 0 }; rdLight_CalcFaceIntensity( diff --git a/Libs/std/General/stdPlatform.c b/Libs/std/General/stdPlatform.c index dd80a8c..d10c3df 100644 --- a/Libs/std/General/stdPlatform.c +++ b/Libs/std/General/stdPlatform.c @@ -30,7 +30,7 @@ void stdPlatform_ResetGlobals(void) //memset(&stdPlatform_bAssert, 0, sizeof(stdPlatform_bAssert)); } -// TODO: stdFile* functions should be moved her to stdPlatform +// TODO: stdFile* functions should be moved here to stdPlatform int J3DAPI stdPlatform_InitServices(tHostServices* pHS) { @@ -129,7 +129,7 @@ int stdPlatform_Printf(const char* format, ...) vsnprintf(std_g_genBuffer, sizeof(std_g_genBuffer), format, args); va_end(args); - OutputDebugStringA(std_g_genBuffer); // TODO: write also to printf + OutputDebugStringA(std_g_genBuffer); // TODO: write also to printf return 1; } diff --git a/Libs/std/Win95/stdDisplay.c b/Libs/std/Win95/stdDisplay.c index d58a06e..2867fe5 100644 --- a/Libs/std/Win95/stdDisplay.c +++ b/Libs/std/Win95/stdDisplay.c @@ -806,6 +806,8 @@ tVBuffer* J3DAPI stdDisplay_VBufferConvertColorFormat(const ColorInfo* pDesiredC } } + // Convert pixel data + STD_ASSERTREL(pSrc->pPixels != ((void*)0)); STD_ASSERTREL(pDest->pPixels != ((void*)0)); @@ -826,13 +828,16 @@ tVBuffer* J3DAPI stdDisplay_VBufferConvertColorFormat(const ColorInfo* pDesiredC &pSrc->rasterInfo.colorInfo, pDest->rasterInfo.width, bColorKey, - pColorKey); + pColorKey + ); } STD_ASSERTREL(pDestRow <= pDest->pPixels + pDest->rasterInfo.size); STD_ASSERTREL(pSrcRow <= pSrc->pPixels + pSrc->rasterInfo.size); stdDisplay_VBufferUnlock(pSrc); stdDisplay_VBufferUnlock(pDest); + + // Copy color format memcpy(&pDest->rasterInfo.colorInfo, pDesiredColorFormat, sizeof(pDest->rasterInfo.colorInfo)); if ( pDest != pSrc ) { From 25ceabaae4c94b919a7e094a1468a419f0e00355 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Thu, 19 Dec 2024 02:16:39 +0100 Subject: [PATCH 22/35] [rdroid] Increase `rdCache` vertex buffer size to 32768 * 64 --- Libs/rdroid/Raster/rdCache.c | 38 +++++++++++++++++++++++------------- Libs/rdroid/Raster/rdCache.h | 7 ++++++- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Libs/rdroid/Raster/rdCache.c b/Libs/rdroid/Raster/rdCache.c index bbe5cb8..45cab63 100644 --- a/Libs/rdroid/Raster/rdCache.c +++ b/Libs/rdroid/Raster/rdCache.c @@ -11,30 +11,29 @@ #include -#define RDCACHE_MAXVERTICES 2048 * 64 // TODO: This number was found in the engine but could be mistake (engine bug?) and the actual size could be 2*2048 (maybe 64 represents 2 * sizeof(D3DTLVERTEX) ?). -// This high number of vertices might be an issue for rdCache_aVertIndices buffer if total verts grows over 2^16-1 size +#define RDCACHE_VERTBUFFERSIZE RDCACHE_MAXVERTICES * RDCACHE_MAXFACEVERTICES static size_t rdCache_numProcFaces = 0; static rdCacheProcEntry rdCache_aProcFaces[128] = { 0 }; static size_t rdCache_numUsedVertices = 0; -static D3DTLVERTEX rdCache_aVertices[RDCACHE_MAXVERTICES] = { 0 }; -static rdVector4 rdCache_aVertIntensities[RDCACHE_MAXVERTICES] = { 0 }; // Added: Changed to match size of rdCache_aVertices, was 8192 +static D3DTLVERTEX rdCache_aVertices[RDCACHE_VERTBUFFERSIZE] = { 0 }; +static rdVector4 rdCache_aVertIntensities[RDCACHE_VERTBUFFERSIZE] = { 0 }; // Fixed: Changed to match size of rdCache_aVertices, was 8192 static size_t rdCache_numAlphaProcFaces = 0; static rdCacheProcEntry rdCache_aAlphaProcFaces[2048] = { 0 }; static size_t rdCache_numUsedAlphaVertices = 0; -static D3DTLVERTEX rdCache_aAlphaVertices[RDCACHE_MAXVERTICES] = { 0 }; -static rdVector4 rdCache_aAlphaVertIntensities[RDCACHE_MAXVERTICES] = { 0 }; +static D3DTLVERTEX rdCache_aAlphaVertices[RDCACHE_VERTBUFFERSIZE] = { 0 }; +static rdVector4 rdCache_aAlphaVertIntensities[RDCACHE_VERTBUFFERSIZE] = { 0 }; static size_t rdCache_totalVerts = 0; static LPD3DTLVERTEX rdCache_pCurInVert = NULL; static LPD3DTLVERTEX rdCache_pCurCacheVertex = NULL; -static D3DTLVERTEX rdCache_aHWVertices[RDCACHE_MAXVERTICES] = { 0 }; -static uint16_t rdCache_aVertIndices[RDCACHE_MAXVERTICES * 3] = { 0 }; +static D3DTLVERTEX rdCache_aHWVertices[RDCACHE_VERTBUFFERSIZE] = { 0 }; +static uint16_t rdCache_aVertIndices[RDCACHE_VERTBUFFERSIZE * 3] = { 0 }; static size_t rdCache_drawnFaces = 0; static size_t rdCache_frameNum = 0; @@ -100,6 +99,12 @@ rdCacheProcEntry* rdCache_GetProcEntry(void) procNum = rdCache_numProcFaces; } + // Added: Check if the vertex buffer is large enough to hold all face vertices. + // Note, taken from grimengine / OpenJKDF2 + if ( RDCACHE_VERTBUFFERSIZE - rdCache_numUsedVertices < RDCACHE_MAXFACEVERTICES ) { + return NULL; + } + // TODO: add bound check for cached verts pProcEntry = &rdCache_aProcFaces[procNum]; pProcEntry->aVertices = &rdCache_aVertices[rdCache_numUsedVertices]; @@ -120,6 +125,12 @@ rdCacheProcEntry* rdCache_GetAlphaProcEntry(void) entryNum = rdCache_numAlphaProcFaces; } + // Added: Check if the vertex buffer is large enough to hold all face vertices. + // Note, taken from grimengine / OpenJKDF2 + if ( RDCACHE_VERTBUFFERSIZE - rdCache_numUsedAlphaVertices < RDCACHE_MAXFACEVERTICES ) { + return NULL; + } + // TODO: add bound check for verts pProcEntry = &rdCache_aAlphaProcFaces[entryNum]; pProcEntry->aVertices = &rdCache_aAlphaVertices[rdCache_numUsedAlphaVertices]; @@ -402,13 +413,12 @@ void J3DAPI rdCache_SendFaceListToHardware(size_t numPolys, rdCacheProcEntry* pC ++polyNum; if ( 3 * (pCurPoly->numVertices - 2) + totalIndices >= std3D_g_maxVertices // i.e. totalIndices + num required triangle indices for next poly >= std3D_g_maxVertices - || polyNum >= numPolys - || pCurMat != pCurPoly->pMaterial - || curMatCelNum != pCurPoly->matCelNum - || fflags != pCurPoly->flags ) + || polyNum >= numPolys + || pCurMat != pCurPoly->pMaterial + || curMatCelNum != pCurPoly->matCelNum + || fflags != pCurPoly->flags ) { - //RD_ASSERTREL(totalVerts < (2048 * 64)); - RD_ASSERTREL(rdCache_totalVerts < RDCACHE_MAXVERTICES); + RD_ASSERTREL(rdCache_totalVerts < RDCACHE_VERTBUFFERSIZE); std3D_DrawRenderList(pD3DCachedTex, rdflags, rdCache_aHWVertices, rdCache_totalVerts, rdCache_aVertIndices, totalIndices); goto LABEL_4; } diff --git a/Libs/rdroid/Raster/rdCache.h b/Libs/rdroid/Raster/rdCache.h index 566fdf5..8e061fb 100644 --- a/Libs/rdroid/Raster/rdCache.h +++ b/Libs/rdroid/Raster/rdCache.h @@ -7,7 +7,12 @@ J3D_EXTERN_C_START -void J3DAPI rdCache_Startup(); +#define RDCACHE_MAXFACEVERTICES 64 // Max vertices per face + // Note, some clipping functions, such as rdClip_FaceToPlane can handle at max 144 vertices. So be conservative when raising this number + +#define RDCACHE_MAXVERTICES 32768 // Changed: Was 2048 + + void J3DAPI rdCache_Startup(); void rdCache_AdvanceFrame(void); int rdCache_GetFrameNum(void); From a0083862519a15b774dd882365997d19386c838d Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Thu, 19 Dec 2024 02:18:25 +0100 Subject: [PATCH 23/35] [rdroid] Increase `rdModel3` mesh constraints --- Libs/rdroid/Primitives/rdModel3.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Libs/rdroid/Primitives/rdModel3.h b/Libs/rdroid/Primitives/rdModel3.h index 2d7ae12..2fea89d 100644 --- a/Libs/rdroid/Primitives/rdModel3.h +++ b/Libs/rdroid/Primitives/rdModel3.h @@ -1,16 +1,17 @@ #ifndef RDROID_RDMODEL3_H #define RDROID_RDMODEL3_H #include +#include #include #include -#include J3D_EXTERN_C_START -#define RDMODEL3_MAX_FACE_VERTICES 64 -#define RDMODEL3_MAX_TEXVRTICES 768 -#define RDMODEL3_MAX_VERTICES 1024 -#define RDMODEL3_MAX_FACES 1528 +#define RDMODEL3_MAX_VERTICES RDCACHE_MAXVERTICES / 2 // Changed: Was fixed 1024, now is bound to RDCACHE_MAXVERTICES +#define RDMODEL3_MAX_TEXVRTICES RDMODEL3_MAX_VERTICES + (RDMODEL3_MAX_VERTICES / 2) // Changed: Was fixed to 768, now is calculated based on RDMODEL3_MAX_VERTICES + +#define RDMODEL3_MAX_FACES 16384 // Changed: Was 1528 +#define RDMODEL3_MAX_FACE_VERTICES RDCACHE_MAXFACEVERTICES #define rdModel3_g_numDrawnFaces J3D_DECL_FAR_VAR(rdModel3_g_numDrawnFaces, int) // extern int rdModel3_g_numDrawnFaces; From 6b71793684a8c90458c410437121612a6a70aad1 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Fri, 20 Dec 2024 00:43:46 +0100 Subject: [PATCH 24/35] [std] Fix infinitive loop when no line break is found in file Fixes potential infinitive loop bug when reading single line from file and there is no line break at the end of the file. --- Libs/std/General/stdStrTable.c | 35 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/Libs/std/General/stdStrTable.c b/Libs/std/General/stdStrTable.c index 98218ff..b19ae7d 100644 --- a/Libs/std/General/stdStrTable.c +++ b/Libs/std/General/stdStrTable.c @@ -79,7 +79,7 @@ int J3DAPI stdStrTable_Load(tStringTable* pStrTable, const char* pFilename) for ( int i = 0; bSuccess && (i < numMsgs); i++ ) { bSuccess = stdStrTable_ReadLine(fh, aLine, sizeof(aLine) - 1); - if ( !_strnicmp(aLine, "end", 3u) ) + if ( strncmpi(aLine, "end", 3u) == 0 ) // hit end { bSuccess = 0; pStrTable->nMsgs = i; @@ -229,34 +229,29 @@ wchar_t* J3DAPI stdStrTable_GetValueOrKey(const tStringTable* pStrTable, const c int J3DAPI stdStrTable_ReadLine(tFileHandle fh, char* pStr, int size) { - char* pch; - int curch; - int bEndLine; - char aBuf[64]; - - bEndLine = 0; - do + bool bFinish = false; + while ( !bFinish ) { - std_g_pHS->pFileGets(fh, pStr, size); - if ( !strchr(pStr, '\n') ) + size_t nRead = std_g_pHS->pFileGets(fh, pStr, size); + if ( nRead != 0 && !strchr(pStr, '\n') ) // Added: Added check for no data read from file { - do - { - std_g_pHS->pFileGets(fh, aBuf, sizeof(aBuf)); - } while ( !strchr(aBuf, '\n') ); + char aBuf[64] = { 0 }; + do { + nRead = std_g_pHS->pFileGets(fh, aBuf, sizeof(aBuf)); + } while ( nRead != 0 && !strchr(aBuf, '\n') ); // Fixed: Added check for no data read from file. This fixes potential infinitive loop bug when there is no line break at the end of the file } - for ( pch = pStr; isspace(*pch); ++pch ) - { + // Skip spaces + char* pch = pStr; + for ( ; isspace(*pch); ++pch ) { ; } - curch = (uint8_t)*pch; - if ( *pch != '#' && (uint8_t)curch && (uint8_t)curch != '\r' && (uint8_t)curch != '\n' ) + if ( nRead == 0 || (*pch != '#' && *pch && *pch != '\r' && *pch != '\n') ) // Fixed: Added check for no data read from file to prevent potential infinitive loop bug { - bEndLine = 1; + bFinish = true; } - } while ( !bEndLine ); + }; return 1; } From a478cb4365389465cf2e421d2ed3041fc6735803 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Fri, 20 Dec 2024 01:15:55 +0100 Subject: [PATCH 25/35] [Jones3D] Search for level files in cwd and resource dir Changes the discovery of level files for developer dialog to search first in ndy folder from current working directory, and if not found searches for files in resource dir path. --- Jones3D/Main/JonesMain.c | 7 ++++++- Libs/std/General/stdPlatform.c | 7 +++++++ Libs/std/General/stdPlatform.h | 5 +++++ Libs/std/General/stdUtil.c | 6 ++++++ Libs/std/General/stdUtil.h | 1 + 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index 06f81da..03ecfcc 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -3088,7 +3088,12 @@ int J3DAPI JonesMain_InitDevDialog(HWND hDlg, WPARAM wParam, JonesState* pConfig if ( hDlgItem ) { char aNdyDir[128]; - STD_MAKEPATH(aNdyDir, JonesFile_GetResourcePath(), "ndy"); // Changed: Use absolute resource path to search for level files + STD_MAKEPATH(aNdyDir, JonesFile_GetWorkingDirPath(), "ndy"); + + // Added + if ( !stdUtil_DirExists(aNdyDir) ) { + STD_MAKEPATH(aNdyDir, JonesFile_GetResourcePath(), "ndy"); // Use absolute resource path to search for level files + } tFoundFileInfo fileInfo; FindFileData* pFileData = stdFileUtil_NewFind(aNdyDir, 3, "ndy"); diff --git a/Libs/std/General/stdPlatform.c b/Libs/std/General/stdPlatform.c index d10c3df..96a031d 100644 --- a/Libs/std/General/stdPlatform.c +++ b/Libs/std/General/stdPlatform.c @@ -157,3 +157,10 @@ void J3DAPI stdPlatform_UnlockHandle() { } + +bool J3DAPI stdPlatform_DirExists(const char* pPath) +{ + DWORD dwAttrib = GetFileAttributes(pPath); + + return (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); +} diff --git a/Libs/std/General/stdPlatform.h b/Libs/std/General/stdPlatform.h index d070ef8..f2a4466 100644 --- a/Libs/std/General/stdPlatform.h +++ b/Libs/std/General/stdPlatform.h @@ -9,14 +9,19 @@ J3D_EXTERN_C_START int J3DAPI stdPlatform_InitServices(tHostServices* pHS); void J3DAPI stdPlatform_ClearServices(tHostServices* pHS); unsigned int stdPlatform_GetTimeMsec(void); + J3DNORETURN void J3DAPI stdPlatform_Assert(const char* pErrorStr, const char* pFilename, int linenum); int stdPlatform_Printf(const char* format, ...); + void* J3DAPI stdPlatform_AllocHandle(size_t size); void J3DAPI stdPlatform_FreeHandle(void* pData); void* J3DAPI stdPlatform_ReallocHandle(void* pMemory, size_t newSize); + int J3DAPI stdPlatform_LockHandle(int a1); void J3DAPI stdPlatform_UnlockHandle(); +bool J3DAPI stdPlatform_DirExists(const char* pPath); + // Helper hooking functions void stdPlatform_InstallHooks(void); void stdPlatform_ResetGlobals(void); diff --git a/Libs/std/General/stdUtil.c b/Libs/std/General/stdUtil.c index 63c7143..e07be9e 100644 --- a/Libs/std/General/stdUtil.c +++ b/Libs/std/General/stdUtil.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -218,6 +219,11 @@ int J3DAPI stdUtil_StrCmp(const char* str1, const char* str2) return 1; } +bool J3DAPI stdUtil_DirExists(const char* pPath) +{ + return stdPlatform_DirExists(pPath); +} + bool J3DAPI stdUtil_FileExists(const char* pFilename) { tFileHandle fh = std_g_pHS->pFileOpen(pFilename, "r"); diff --git a/Libs/std/General/stdUtil.h b/Libs/std/General/stdUtil.h index 2bb298c..ca39c51 100644 --- a/Libs/std/General/stdUtil.h +++ b/Libs/std/General/stdUtil.h @@ -130,6 +130,7 @@ void J3DAPI stdUtil_ToUpper(char* pStr); // Added int J3DAPI stdUtil_StrCmp(const char* str1, const char* str2); +bool J3DAPI stdUtil_DirExists(const char* pPath); // Added bool J3DAPI stdUtil_FileExists(const char* pFilename); // Added uint32_t J3DAPI stdUtil_CalcChecksum(const uint8_t* pData, size_t dataSize, uint32_t seed); // Added From 30630d1fcd59e225484e67db92af9df40dd1cda8 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Fri, 20 Dec 2024 01:16:51 +0100 Subject: [PATCH 26/35] [Jones3D] Fix exposing `JonesMain_LogErrorToFile` --- Jones3D/Main/JonesMain.c | 3 ++- Jones3D/Main/JonesMain.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index 03ecfcc..e0a44c3 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -447,7 +447,6 @@ int J3DAPI JonesMain_LogDebug(const char* pFormat, ...); int J3DAPI JonesMain_NoLog(const char* pFormat, ...); // Added int J3DAPI JonesMain_FilePrintf(const char* pFormat, ...); -void J3DAPI JonesMain_LogErrorToFile(const char* pErrorText); J3DNORETURN void J3DAPI JonesMain_Assert(const char* pErrorText, const char* pSrcFile, int line); int J3DAPI JonesMain_IntroWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, int* pRetValue); @@ -1050,6 +1049,7 @@ int J3DAPI JonesMain_GameWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa return JonesMain_bWndMsgProcessed; #else + J3D_UNUSED(pReturnVal); return 0; #endif } @@ -2439,6 +2439,7 @@ int J3DAPI JonesMain_IntroWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lP return JonesMain_bWndMsgProcessed; #else + J3D_UNUSED(pRetValue); return 0; #endif } diff --git a/Jones3D/Main/JonesMain.h b/Jones3D/Main/JonesMain.h index 8157302..66f9e41 100644 --- a/Jones3D/Main/JonesMain.h +++ b/Jones3D/Main/JonesMain.h @@ -52,6 +52,7 @@ int JonesMain_PlayIntroMovie(void); void J3DAPI JonesMain_BindToggleMenuControlKeys(const int* paKeyIds, int numKeys); +void J3DAPI JonesMain_LogErrorToFile(const char* pErrorText); J3DNORETURN void JonesMain_FatalErrorUnknown(void); // Added int J3DAPI JonesMain_ShowDevDialog(HWND hWnd, JonesState* pConfig); From 5e64e5bcc0a3379982e82b0fb39957f1323669ee Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sun, 22 Dec 2024 19:19:41 +0100 Subject: [PATCH 27/35] [Jones3D] Fix finding corresponding ndy file on level progress Fixes searching for ndy level file when corresponding cnd file is not found --- Jones3D/Main/JonesFile.c | 2 +- Jones3D/Main/JonesMain.c | 41 +++++++++++++++++++++++++++++++----- Jones3D/RTI/addresses.h | 2 +- Jones3D/RTI/symbols.h | 2 +- Libs/std/General/stdFnames.c | 14 +++++------- Libs/std/General/stdFnames.h | 2 +- Libs/std/RTI/symbols.h | 2 +- 7 files changed, 46 insertions(+), 19 deletions(-) diff --git a/Jones3D/Main/JonesFile.c b/Jones3D/Main/JonesFile.c index 6d814e3..5057c1c 100644 --- a/Jones3D/Main/JonesFile.c +++ b/Jones3D/Main/JonesFile.c @@ -210,7 +210,7 @@ void J3DAPI JonesFile_SetInstallPath(const char* pPath) JonesFile_OpenResource(&JonesFile_aResources[JF_RES_INSTALL], pPath); // Added: Add resource dir path - STD_MAKEPATH(JonesFile_aPathBuf, pPath, "resource"); + STD_MAKEPATH(JonesFile_aPathBuf, pPath, "Resource"); JonesFile_OpenResource(&JonesFile_aResources[JF_RES_RESOURCE], JonesFile_aPathBuf); } } diff --git a/Jones3D/Main/JonesMain.c b/Jones3D/Main/JonesMain.c index e0a44c3..bb7ffb8 100644 --- a/Jones3D/Main/JonesMain.c +++ b/Jones3D/Main/JonesMain.c @@ -433,7 +433,8 @@ void JonesMain_ProcessMenu(void); void J3DAPI JonesMain_PrintFramerate(); -int J3DAPI JonesMain_EnsureFile(const char* pFilename); +int J3DAPI JonesMain_EnsureLevelFile(const char* pFilename); +int J3DAPI JonesMain_EnsureLevelFileEx(const char* pFilename, bool bFindAll, char* pFoundFilename, size_t filenameSize); int J3DAPI JonesMain_Restore(const char* pNdsFilePath); int JonesMain_ProcessStartGame(void); @@ -496,7 +497,7 @@ void JonesMain_InstallHooks(void) J3D_HOOKFUNC(JonesMain_PrintFramerate); J3D_HOOKFUNC(JonesMain_TogglePrintFramerate); J3D_HOOKFUNC(JonesMain_Open); - J3D_HOOKFUNC(JonesMain_EnsureFile); + J3D_HOOKFUNC(JonesMain_EnsureLevelFile); J3D_HOOKFUNC(JonesMain_Close); J3D_HOOKFUNC(JonesMain_Restore); J3D_HOOKFUNC(JonesMain_ProcessGamesaveState); @@ -1674,7 +1675,8 @@ void JonesMain_TogglePrintFramerate(void) int JonesMain_Open(void) { - if ( JonesMain_EnsureFile(JonesMain_state.aCurLevelFilename) ) + // Changed: Look for both versions of level formats (cnd and ndy). This make sure that level progress can find ndy level file + if ( JonesMain_EnsureLevelFileEx(JonesMain_state.aCurLevelFilename, /*bFindAll=*/true, JonesMain_state.aCurLevelFilename, STD_ARRAYLEN(JonesMain_state.aCurLevelFilename)) ) { return 1; } @@ -1768,8 +1770,16 @@ int JonesMain_Open(void) return 0; } -int J3DAPI JonesMain_EnsureFile(const char* pFilename) +int J3DAPI JonesMain_EnsureLevelFile(const char* pFilename) { + // Changed + return JonesMain_EnsureLevelFileEx(pFilename, /*bFindAll*/false, NULL, 0); +} + +int J3DAPI JonesMain_EnsureLevelFileEx(const char* pFilename, bool bFindAll, char* pFoundFilename, size_t filenameSize) +{ + // Added overload function which searches for both cnd and ndy file in case one is not found. + // // TODO: there is an issue when loading level from different CD*.gob file than current // the insert CD dialog can be shown even if file exists on disk @@ -1781,6 +1791,27 @@ int J3DAPI JonesMain_EnsureFile(const char* pFilename) return 0; } + // Try looking for different format + if ( bFindAll && pFoundFilename ) + { + const char* pCurExt = stdFnames_FindExt(aPath); + if ( strcmpi(pCurExt, "cnd") == 0 ) { + stdFnames_ChangeExt(aPath, "ndy"); + } + else { + stdFnames_ChangeExt(aPath, "cnd"); + } + + if ( JonesFile_FileExists(aPath) ) + { + // File found + stdUtil_StringCopy(pFoundFilename, filenameSize, stdFnames_FindMedName(aPath)); + return 0; + } + } + + // No level file was found + JonesFile_Close(); size_t cndNum = 2 - (JonesFile_GetCurrentCDNum() != 1); @@ -1811,7 +1842,7 @@ int J3DAPI JonesMain_Restore(const char* pNdsFilePath) return 1; } - if ( JonesMain_EnsureFile(JonesMain_state.aCurLevelFilename) ) + if ( JonesMain_EnsureLevelFile(JonesMain_state.aCurLevelFilename) ) { return 1; } diff --git a/Jones3D/RTI/addresses.h b/Jones3D/RTI/addresses.h index d551845..51dc02c 100644 --- a/Jones3D/RTI/addresses.h +++ b/Jones3D/RTI/addresses.h @@ -120,7 +120,7 @@ #define JonesMain_PrintFramerate_ADDR 0x0041E1E0 #define JonesMain_TogglePrintFramerate_ADDR 0x0041E300 #define JonesMain_Open_ADDR 0x0041E320 -#define JonesMain_EnsureFile_ADDR 0x0041E520 +#define JonesMain_EnsureLevelFile_ADDR 0x0041E520 #define JonesMain_Close_ADDR 0x0041E5B0 #define JonesMain_Restore_ADDR 0x0041E5C0 #define JonesMain_ProcessGamesaveState_ADDR 0x0041E730 diff --git a/Jones3D/RTI/symbols.h b/Jones3D/RTI/symbols.h index 1d46f18..00f6878 100644 --- a/Jones3D/RTI/symbols.h +++ b/Jones3D/RTI/symbols.h @@ -110,7 +110,7 @@ #define JonesMain_PrintFramerate_TYPE void (J3DAPI*)() #define JonesMain_TogglePrintFramerate_TYPE void (*)(void) #define JonesMain_Open_TYPE int (*)(void) -#define JonesMain_EnsureFile_TYPE int (J3DAPI*)(const char*) +#define JonesMain_EnsureLevelFile_TYPE int (J3DAPI*)(const char*) #define JonesMain_Close_TYPE int (*)(void) #define JonesMain_Restore_TYPE int (J3DAPI*)(const char*) #define JonesMain_ProcessGamesaveState_TYPE int (*)(void) diff --git a/Libs/std/General/stdFnames.c b/Libs/std/General/stdFnames.c index a2bc7e3..db3230f 100644 --- a/Libs/std/General/stdFnames.c +++ b/Libs/std/General/stdFnames.c @@ -39,15 +39,11 @@ const char* J3DAPI stdFnames_FindMedName(const char* pFilePath) return pName; } -char* J3DAPI stdFnames_FindExt(const char* pFilePath) +const char* J3DAPI stdFnames_FindExt(const char* pFilePath) { - const char* pFile; - char* pExt; - - pFile = stdFnames_FindMedName(pFilePath); - pExt = strrchr(pFile, '.'); - if ( pExt ) - { + const char* pFile = stdFnames_FindMedName(pFilePath); + const char* pExt = strrchr(pFile, '.'); + if ( pExt ) { ++pExt; } @@ -56,7 +52,7 @@ char* J3DAPI stdFnames_FindExt(const char* pFilePath) void J3DAPI stdFnames_StripExtAndDot(char* pPath) { - char* pExt = stdFnames_FindExt(pPath); + char* pExt = (char*)stdFnames_FindExt(pPath); if ( pExt ) { *(pExt - 1) = 0; diff --git a/Libs/std/General/stdFnames.h b/Libs/std/General/stdFnames.h index 3f88c70..cfdc04c 100644 --- a/Libs/std/General/stdFnames.h +++ b/Libs/std/General/stdFnames.h @@ -15,7 +15,7 @@ J3D_EXTERN_C_START } while ( 0 ) const char* J3DAPI stdFnames_FindMedName(const char* pFilePath); -char* J3DAPI stdFnames_FindExt(const char* pFilePath); +const char* J3DAPI stdFnames_FindExt(const char* pFilePath); void J3DAPI stdFnames_StripExtAndDot(char* pPath); void J3DAPI stdFnames_ChangeExt(char* pPath, const char* pExt); void J3DAPI stdFnames_Concat(char* dst, const char* src, size_t size); diff --git a/Libs/std/RTI/symbols.h b/Libs/std/RTI/symbols.h index 17dfc21..5600349 100644 --- a/Libs/std/RTI/symbols.h +++ b/Libs/std/RTI/symbols.h @@ -267,7 +267,7 @@ #define stdBmp_WriteVBuffer_TYPE int (J3DAPI*)(const char*, tVBuffer*) #define stdBmp_Load_TYPE HBITMAP (J3DAPI*)(const char*) #define stdFnames_FindMedName_TYPE const char* (J3DAPI*)(const char*) -#define stdFnames_FindExt_TYPE char* (J3DAPI*)(const char*) +#define stdFnames_FindExt_TYPE const char* (J3DAPI*)(const char*) #define stdFnames_StripExtAndDot_TYPE void (J3DAPI*)(char*) #define stdFnames_ChangeExt_TYPE void (J3DAPI*)(char*, const char*) #define stdFnames_Concat_TYPE void (J3DAPI*)(char*, const char*, size_t) From 08a033bfbc585eab6344e7c927d263c630ce5aab Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sun, 22 Dec 2024 19:21:21 +0100 Subject: [PATCH 28/35] [rdroid] Increase max sectors in camera view to 1024 --- Libs/sith/Engine/sithRender.c | 162 +--------------------------------- Libs/sith/Engine/sithRender.h | 4 +- 2 files changed, 5 insertions(+), 161 deletions(-) diff --git a/Libs/sith/Engine/sithRender.c b/Libs/sith/Engine/sithRender.c index 009339c..5121f7c 100644 --- a/Libs/sith/Engine/sithRender.c +++ b/Libs/sith/Engine/sithRender.c @@ -3,9 +3,9 @@ #include #include +#include #include #include -#include #include #include #include @@ -32,8 +32,8 @@ #include #include -#include #include +#include #include #include #include @@ -41,6 +41,7 @@ #include #include +#include #define SITHRENDER_MAXTHINGLIGHTS RDCAMERA_MAX_LIGHTS / 2 // 64; note this var must not exceed RDCAMERA_MAX_LIGHTS-1 #define SITHRENDER_MAXSECTORLIGHTS (RDCAMERA_MAX_LIGHTS - SITHRENDER_MAXTHINGLIGHTS) @@ -99,47 +100,6 @@ static size_t sithRender_numSpritesToDraw; static size_t sithRender_numThingLights; static rdLight sithRender_aThingLights[SITHRENDER_MAXTHINGLIGHTS]; - - -////#define sithRender_bPVSClipEnabled J3D_DECL_FAR_VAR(sithRender_bPVSClipEnabled, int) -//// -//#define sithRender_aVisibleSectors J3D_DECL_FAR_ARRAYVAR(sithRender_aVisibleSectors, SithSector*(*)[SITHRENDER_MAX_VISIBLE_SECTORS]) -// -//#define sithRender_aClipVertices J3D_DECL_FAR_ARRAYVAR(sithRender_aClipVertices, rdVector3(*)[MAX_CLIP_VERTICIES]) -//// -////#define sithRender_lastPVSIndex J3D_DECL_FAR_VAR(sithRender_lastPVSIndex, int) -//// -//#define sithRender_clipFaceView J3D_DECL_FAR_VAR(sithRender_clipFaceView, rdPrimit3) -//// -//#define sithRender_aVisibleAdjoins J3D_DECL_FAR_ARRAYVAR(sithRender_aVisibleAdjoins, SithSurfaceAdjoin*(*)[SITHRENDER_MAX_VISIBLE_SECTORS]) -//// -// -//#define sithRender_curPVSIndex J3D_DECL_FAR_VAR(sithRender_curPVSIndex, int) -//// -////#define sithRender_renderflags J3D_DECL_FAR_VAR(sithRender_renderflags, int) -////#define sithRender_numSectorPointLights J3D_DECL_FAR_VAR(sithRender_numSectorPointLights, int) -////#define sithRender_numSpritesToDraw J3D_DECL_FAR_VAR(sithRender_numSpritesToDraw, int) -////#define sithRender_numAlphaAdjoins J3D_DECL_FAR_VAR(sithRender_numAlphaAdjoins, int) -//#define sithRender_aTransformedClipVertices J3D_DECL_FAR_ARRAYVAR(sithRender_aTransformedClipVertices, rdVector3(*)[MAX_CLIP_VERTICIES]) -////#define sithRender_numThingSectors J3D_DECL_FAR_VAR(sithRender_numThingSectors, int) -//#define sithRender_faceView J3D_DECL_FAR_VAR(sithRender_faceView, rdPrimit3) -////#define sithRender_aSectorPointLights J3D_DECL_FAR_ARRAYVAR(sithRender_aSectorPointLights, rdLight(*)[64]) -////#define sithRender_aThingLights J3D_DECL_FAR_ARRAYVAR(sithRender_aThingLights, rdLight(*)[64]) -////#define sithRender_aThingSectors J3D_DECL_FAR_ARRAYVAR(sithRender_aThingSectors, SithSector*(*)[256]) -////#define sithRender_numSecorFrustrums J3D_DECL_FAR_VAR(sithRender_numSecorFrustrums, int) -////#define sithRender_aSectorFrustrums J3D_DECL_FAR_ARRAYVAR(sithRender_aSectorFrustrums, rdClipFrustum(*)[128]) -////#define sithRender_curCamSectorIdx J3D_DECL_FAR_VAR(sithRender_curCamSectorIdx, int) -////#define sithRender_aAlphaAdjoins J3D_DECL_FAR_ARRAYVAR(sithRender_aAlphaAdjoins, SithSurface*(*)[64]) -//#define sithRender_aSurfaceTransformedVertices J3D_DECL_FAR_ARRAYVAR(sithRender_aSurfaceTransformedVertices, rdVector3(*)[MAX_CLIP_VERTICIES]) -////#define sithRender_numThingLights J3D_DECL_FAR_VAR(sithRender_numThingLights, int) -////#define sithRender_numRenderedSectors J3D_DECL_FAR_VAR(sithRender_numRenderedSectors, int) -//#define sithRender_numVisibleThingSectors J3D_DECL_FAR_VAR(sithRender_numVisibleThingSectors, int) -////#define sithRender_bResetCameraAspect J3D_DECL_FAR_VAR(sithRender_bResetCameraAspect, int) -////#define sithRender_pfUnknownFunc J3D_DECL_FAR_VAR(sithRender_pfUnknownFunc, SithRenderUnknownFunc) -//// -// -#define sithRender_aAdjoinTable J3D_DECL_FAR_VAR(sithRender_aAdjoinTable, uint8_t*) - void sithRender_InstallHooks(void) { J3D_HOOKFUNC(sithRender_Startup); @@ -178,125 +138,9 @@ void sithRender_ResetGlobals(void) memset(&sithRender_g_numAlphaThingPoly, 0, sizeof(sithRender_g_numAlphaThingPoly)); memset(&sithRender_g_numVisibleAdjoins, 0, sizeof(sithRender_g_numVisibleAdjoins)); - //memset(&sithRender_numVisibleThingSectors, 0, sizeof(sithRender_numVisibleThingSectors)); - //memset(&sithRender_bResetCameraAspect, 0, sizeof(sithRender_bResetCameraAspect)); - //memset(&sithRender_pfUnknownFunc, 0, sizeof(sithRender_pfUnknownFunc)); - //memset(&sithRender_aAdjoinTable, 0, sizeof(sithRender_aAdjoinTable)); - - //memset(&sithRender_lightMode, 0, sizeof(sithRender_lightMode)); memset(&sithRender_g_numVisibleSectors, 0, sizeof(sithRender_g_numVisibleSectors)); } -//int sithRender_Startup(void) -//{ -// return J3D_TRAMPOLINE_CALL(sithRender_Startup); -//} -// -//int J3DAPI sithRender_Open() -//{ -// return J3D_TRAMPOLINE_CALL(sithRender_Open); -//} -// -//void J3DAPI sithRender_Close() -//{ -// J3D_TRAMPOLINE_CALL(sithRender_Close); -//} -// -//void sithRender_Shutdown(void) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_Shutdown); -//} -// -//void J3DAPI sithRender_SetRenderFlags(int flags) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_SetRenderFlags, flags); -//} -// -//int J3DAPI sithRender_GetRenderFlags() -//{ -// return J3D_TRAMPOLINE_CALL(sithRender_GetRenderFlags); -//} -// -//void J3DAPI sithRender_SetLightingMode(rdLightMode mode) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_SetLightingMode, mode); -//} -// -//void sithRender_Draw(void) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_Draw); -//} -// -//void J3DAPI sithRender_BuildVisibleSectorList(SithSector* pSector, rdClipFrustum* pFrustrum) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_BuildVisibleSectorList, pSector, pFrustrum); -//} -// -//void J3DAPI sithRender_BuildVisibleSurface(SithSurface* pSurface) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_BuildVisibleSurface, pSurface); -//} -// -//void J3DAPI sithRender_BuildClipFrustrum(rdClipFrustum* pFrustrum, int numVertices, float orthLeft, float orthTop, float orthRight, float orthBottom) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_BuildClipFrustrum, pFrustrum, numVertices, orthLeft, orthTop, orthRight, orthBottom); -//} -// -//void J3DAPI sithRender_PVSBuildVisibleSectorList(SithSector* pSector, rdClipFrustum* pClipFrustum) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_PVSBuildVisibleSectorList, pSector, pClipFrustum); -//} -// -//void J3DAPI sithRender_PVSBuildVisibleSector(SithSector* pSector) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_PVSBuildVisibleSector, pSector); -//} -// -//void J3DAPI sithRender_BuildVisibleSector(SithSector* pSector, const rdClipFrustum* pFrustrum) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_BuildVisibleSector, pSector, pFrustrum); -//} -// -//void J3DAPI sithRender_RenderSectors() -//{ -// J3D_TRAMPOLINE_CALL(sithRender_RenderSectors); -//} -// -//void sithRender_BuildVisibleSectorsThingList(void) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_BuildVisibleSectorsThingList); -//} -// -//void J3DAPI sithRender_BuildSectorThingList(SithSector* pSector, float a2, float distance) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_BuildSectorThingList, pSector, a2, distance); -//} -// -//void sithRender_BuildDynamicLights(void) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_BuildDynamicLights); -//} -// -//void sithRender_RenderThings(void) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_RenderThings); -//} -// -//int J3DAPI sithRender_RenderThing(SithThing* pThing) -//{ -// return J3D_TRAMPOLINE_CALL(sithRender_RenderThing, pThing); -//} -// -//void sithRender_RenderAlphaAdjoins(void) -//{ -// J3D_TRAMPOLINE_CALL(sithRender_RenderAlphaAdjoins); -//} -// -//int sithRender_MakeScreenShot(void) -//{ -// return J3D_TRAMPOLINE_CALL(sithRender_MakeScreenShot); -//} - int sithRender_Startup(void) { rdMaterial_RegisterLoader(sithMaterial_Load); diff --git a/Libs/sith/Engine/sithRender.h b/Libs/sith/Engine/sithRender.h index 55e1a87..faa5729 100644 --- a/Libs/sith/Engine/sithRender.h +++ b/Libs/sith/Engine/sithRender.h @@ -8,8 +8,8 @@ #include J3D_EXTERN_C_START -#define SITHRENDER_MAX_VISIBLE_SECTORS 128 -#define SITHRENDER_MAX_SECTORS_WITH_THINGS SITHRENDER_MAX_VISIBLE_SECTORS * 2 +#define SITHRENDER_MAX_VISIBLE_SECTORS 1024 // Changed: Was 128 +#define SITHRENDER_MAX_SECTORS_WITH_THINGS SITHRENDER_MAX_VISIBLE_SECTORS * 2 // Changed: Was 256 #define sithRender_g_fogDensity J3D_DECL_FAR_VAR(sithRender_g_fogDensity, float) // extern float sithRender_g_fogDensity ; From b448041cd6bd5d53b07adeb41c35c5e045c52267 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Mon, 23 Dec 2024 00:47:07 +0100 Subject: [PATCH 29/35] [rdroid] Increase max visible sectors to 4096 --- Libs/sith/Engine/sithRender.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libs/sith/Engine/sithRender.h b/Libs/sith/Engine/sithRender.h index faa5729..a51869a 100644 --- a/Libs/sith/Engine/sithRender.h +++ b/Libs/sith/Engine/sithRender.h @@ -8,7 +8,7 @@ #include J3D_EXTERN_C_START -#define SITHRENDER_MAX_VISIBLE_SECTORS 1024 // Changed: Was 128 +#define SITHRENDER_MAX_VISIBLE_SECTORS 4096 // Changed: Was 128 #define SITHRENDER_MAX_SECTORS_WITH_THINGS SITHRENDER_MAX_VISIBLE_SECTORS * 2 // Changed: Was 256 #define sithRender_g_fogDensity J3D_DECL_FAR_VAR(sithRender_g_fogDensity, float) From 156748274a270a85eeea627f9feb296cdbe89628 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Sun, 22 Dec 2024 23:30:10 +0100 Subject: [PATCH 30/35] [sith] Implement `sithSurface` module --- Libs/rdroid/types.h | 2 +- Libs/sith/RTI/addresses.h | 10 +- Libs/sith/RTI/symbols.h | 18 +- Libs/sith/World/sithSurface.c | 983 ++++++++++++++++++++++++++++++++-- Libs/sith/World/sithSurface.h | 37 +- Libs/sith/World/sithWorld.c | 8 +- Libs/wkernel/wkernel.h | 2 + 7 files changed, 969 insertions(+), 91 deletions(-) diff --git a/Libs/rdroid/types.h b/Libs/rdroid/types.h index 55a1f66..d71d8a4 100644 --- a/Libs/rdroid/types.h +++ b/Libs/rdroid/types.h @@ -210,7 +210,7 @@ typedef struct srdFont } rdFont; static_assert(sizeof(rdFont) == 20, "sizeof(rdFont) == 20"); -typedef struct srdMatHeader +typedef struct srdMatHeader // Originally was probably named tMaterialHeader { char magic[4]; int version; diff --git a/Libs/sith/RTI/addresses.h b/Libs/sith/RTI/addresses.h index 0522d26..8abeaa8 100644 --- a/Libs/sith/RTI/addresses.h +++ b/Libs/sith/RTI/addresses.h @@ -1059,12 +1059,12 @@ #define sithSurface_ShowSectorAdjoin_ADDR 0x0045E600 #define sithSurface_AllocWorldSurfaces_ADDR 0x0045E640 #define sithSurface_FreeWorldSurfaces_ADDR 0x0045E720 -#define sithSurface_CNDWriteAdjoinSection_ADDR 0x0045E810 -#define sithSurface_CNDReadAdjoinSection_ADDR 0x0045E900 +#define sithSurface_WriteAdjoinsBinary_ADDR 0x0045E810 +#define sithSurface_LoadAdjoinsBinary_ADDR 0x0045E900 #define sithSurface_AllocWorldAdjoins_ADDR 0x0045E9F0 -#define sithSurface_CNDWriteSurfaceSection_ADDR 0x0045EAD0 -#define sithSurface_CNDReadSurfaceSection_ADDR 0x0045EDD0 -#define sithSurface_NDYReadSurfaceList_ADDR 0x0045F150 +#define sithSurface_WriteSurfacesBinary_ADDR 0x0045EAD0 +#define sithSurface_LoadSurfacesBinary_ADDR 0x0045EDD0 +#define sithSurface_LoadSurfacesText_ADDR 0x0045F150 #define sithSurface_ValidateSurfacePointer_ADDR 0x0045F830 #define sithSurface_ValidateWorldSurfaces_ADDR 0x0045F850 #define sithSurface_HandleThingImpact_ADDR 0x0045F8C0 diff --git a/Libs/sith/RTI/symbols.h b/Libs/sith/RTI/symbols.h index c552507..b869616 100644 --- a/Libs/sith/RTI/symbols.h +++ b/Libs/sith/RTI/symbols.h @@ -1055,21 +1055,21 @@ #define sithMaterial_CacheRemove_TYPE int (J3DAPI*)(const rdMaterial*) #define sithSurface_HideSectorAdjoin_TYPE char (J3DAPI*)(SithSurfaceAdjoin*) #define sithSurface_ShowSectorAdjoin_TYPE char (J3DAPI*)(SithSurfaceAdjoin*) -#define sithSurface_AllocWorldSurfaces_TYPE int (J3DAPI*)(SithWorld*, int) +#define sithSurface_AllocWorldSurfaces_TYPE int (J3DAPI*)(SithWorld*, size_t) #define sithSurface_FreeWorldSurfaces_TYPE void (J3DAPI*)(SithWorld*) -#define sithSurface_CNDWriteAdjoinSection_TYPE int (J3DAPI*)(tFileHandle, SithWorld*) -#define sithSurface_CNDReadAdjoinSection_TYPE int (J3DAPI*)(tFileHandle, SithWorld*) -#define sithSurface_AllocWorldAdjoins_TYPE int (J3DAPI*)(SithWorld*, int) -#define sithSurface_CNDWriteSurfaceSection_TYPE int (J3DAPI*)(tFileHandle, SithWorld*) -#define sithSurface_CNDReadSurfaceSection_TYPE int (J3DAPI*)(tFileHandle, SithWorld*) -#define sithSurface_NDYReadSurfaceList_TYPE signed int (J3DAPI*)(SithWorld*) +#define sithSurface_WriteAdjoinsBinary_TYPE int (J3DAPI*)(tFileHandle, SithWorld*) +#define sithSurface_LoadAdjoinsBinary_TYPE int (J3DAPI*)(tFileHandle, SithWorld*) +#define sithSurface_AllocWorldAdjoins_TYPE int (J3DAPI*)(SithWorld*, size_t) +#define sithSurface_WriteSurfacesBinary_TYPE int (J3DAPI*)(tFileHandle, SithWorld*) +#define sithSurface_LoadSurfacesBinary_TYPE int (J3DAPI*)(tFileHandle, SithWorld*) +#define sithSurface_LoadSurfacesText_TYPE signed int (J3DAPI*)(SithWorld*) #define sithSurface_ValidateSurfacePointer_TYPE int (J3DAPI*)(const SithSurface*) #define sithSurface_ValidateWorldSurfaces_TYPE int (J3DAPI*)(const SithWorld*) #define sithSurface_HandleThingImpact_TYPE void (J3DAPI*)(SithSurface*, SithThing*, float, int) #define sithSurface_PlaySurfaceHitSound_TYPE void (J3DAPI*)(const SithSurface*, SithThing*, int) #define sithSurface_GetCenterPoint_TYPE int (J3DAPI*)(const SithSurface*, rdVector3*) -#define sithSurface_SyncSurface_TYPE int (J3DAPI*)(SithSurface*) -#define sithSurface_SyncSurfaces_TYPE void (J3DAPI*)() +#define sithSurface_SyncSurface_TYPE void (J3DAPI*)(SithSurface*) +#define sithSurface_SyncSurfaces_TYPE void (*)(void #define sithSurface_GetSurfaceEx_TYPE SithSurface* (J3DAPI*)(const SithWorld*, int) #define sithSurface_GetSurfaceIndex_TYPE int (J3DAPI*)(const SithSurface*) #define sithSurface_GetSurfaceIndexEx_TYPE int (J3DAPI*)(const SithWorld*, const SithSurface*) diff --git a/Libs/sith/World/sithSurface.c b/Libs/sith/World/sithSurface.c index 5a7eed4..b584aa5 100644 --- a/Libs/sith/World/sithSurface.c +++ b/Libs/sith/World/sithSurface.c @@ -1,138 +1,1005 @@ #include "sithSurface.h" +#include "sithWorld.h" + #include + +#include +#include +#include +#include +#include #include -#define sithSurface_apUnsyncedSurfaces J3D_DECL_FAR_ARRAYVAR(sithSurface_apUnsyncedSurfaces, SithSurface*(*)[32]) -#define sithSurface_numUnsyncedSurfaces J3D_DECL_FAR_VAR(sithSurface_numUnsyncedSurfaces, int) +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +static SithSurface* sithSurface_apUnsyncedSurfaces[32] = { 0 }; // Added: Init to 0 +static size_t sithSurface_numUnsyncedSurfaces = 0; // Added: Init to 0 void sithSurface_InstallHooks(void) { - // Uncomment only lines for functions that have full definition and doesn't call original function (non-thunk functions) - - // J3D_HOOKFUNC(sithSurface_HideSectorAdjoin); - // J3D_HOOKFUNC(sithSurface_ShowSectorAdjoin); - // J3D_HOOKFUNC(sithSurface_AllocWorldSurfaces); - // J3D_HOOKFUNC(sithSurface_FreeWorldSurfaces); - // J3D_HOOKFUNC(sithSurface_CNDWriteAdjoinSection); - // J3D_HOOKFUNC(sithSurface_CNDReadAdjoinSection); - // J3D_HOOKFUNC(sithSurface_AllocWorldAdjoins); - // J3D_HOOKFUNC(sithSurface_CNDWriteSurfaceSection); - // J3D_HOOKFUNC(sithSurface_CNDReadSurfaceSection); - // J3D_HOOKFUNC(sithSurface_NDYReadSurfaceList); - // J3D_HOOKFUNC(sithSurface_ValidateSurfacePointer); - // J3D_HOOKFUNC(sithSurface_ValidateWorldSurfaces); - // J3D_HOOKFUNC(sithSurface_HandleThingImpact); - // J3D_HOOKFUNC(sithSurface_PlaySurfaceHitSound); - // J3D_HOOKFUNC(sithSurface_GetCenterPoint); - // J3D_HOOKFUNC(sithSurface_SyncSurface); - // J3D_HOOKFUNC(sithSurface_SyncSurfaces); - // J3D_HOOKFUNC(sithSurface_GetSurfaceEx); - // J3D_HOOKFUNC(sithSurface_GetSurfaceIndex); - // J3D_HOOKFUNC(sithSurface_GetSurfaceIndexEx); + J3D_HOOKFUNC(sithSurface_HideSectorAdjoin); + J3D_HOOKFUNC(sithSurface_ShowSectorAdjoin); + J3D_HOOKFUNC(sithSurface_AllocWorldSurfaces); + J3D_HOOKFUNC(sithSurface_FreeWorldSurfaces); + J3D_HOOKFUNC(sithSurface_WriteAdjoinsBinary); + J3D_HOOKFUNC(sithSurface_LoadAdjoinsBinary); + J3D_HOOKFUNC(sithSurface_AllocWorldAdjoins); + J3D_HOOKFUNC(sithSurface_WriteSurfacesBinary); + J3D_HOOKFUNC(sithSurface_LoadSurfacesBinary); + J3D_HOOKFUNC(sithSurface_LoadSurfacesText); + J3D_HOOKFUNC(sithSurface_ValidateSurfacePointer); + J3D_HOOKFUNC(sithSurface_ValidateWorldSurfaces); + J3D_HOOKFUNC(sithSurface_HandleThingImpact); + J3D_HOOKFUNC(sithSurface_PlaySurfaceHitSound); + J3D_HOOKFUNC(sithSurface_GetCenterPoint); + J3D_HOOKFUNC(sithSurface_SyncSurface); + J3D_HOOKFUNC(sithSurface_SyncSurfaces); + J3D_HOOKFUNC(sithSurface_GetSurfaceEx); + J3D_HOOKFUNC(sithSurface_GetSurfaceIndex); + J3D_HOOKFUNC(sithSurface_GetSurfaceIndexEx); } void sithSurface_ResetGlobals(void) +{} + +void J3DAPI sithSurface_HideSectorAdjoin(SithSurfaceAdjoin* pAdjoin) { - memset(&sithSurface_apUnsyncedSurfaces, 0, sizeof(sithSurface_apUnsyncedSurfaces)); - memset(&sithSurface_numUnsyncedSurfaces, 0, sizeof(sithSurface_numUnsyncedSurfaces)); + SITH_ASSERTREL(pAdjoin); + if ( (pAdjoin->flags & SITH_ADJOIN_VISIBLE) != 0 ) + { + pAdjoin->flags |= SITH_ADJOIN_SECTORSET; + pAdjoin->flags &= ~SITH_ADJOIN_VISIBLE; + } } -char J3DAPI sithSurface_HideSectorAdjoin(SithSurfaceAdjoin* pAdjoin) +void J3DAPI sithSurface_ShowSectorAdjoin(SithSurfaceAdjoin* pAdjoin) { - return J3D_TRAMPOLINE_CALL(sithSurface_HideSectorAdjoin, pAdjoin); + SITH_ASSERTREL(pAdjoin); + if ( (pAdjoin->flags & SITH_ADJOIN_SECTORSET) != 0 ) + { + pAdjoin->flags |= SITH_ADJOIN_VISIBLE; + pAdjoin->flags &= ~SITH_ADJOIN_SECTORSET; + } } -char J3DAPI sithSurface_ShowSectorAdjoin(SithSurfaceAdjoin* pAdjoin) +int J3DAPI sithSurface_AllocWorldSurfaces(SithWorld* pWorld, size_t numSurfaces) { - return J3D_TRAMPOLINE_CALL(sithSurface_ShowSectorAdjoin, pAdjoin); + SITH_ASSERTREL(pWorld != NULL); + SITH_ASSERTREL(pWorld->aSurfaces == NULL); + + const size_t sizeSurfaces = sizeof(SithSurface) * numSurfaces; + pWorld->aSurfaces = (SithSurface*)STDMALLOC(sizeSurfaces); + if ( !pWorld->aSurfaces ) + { + SITHLOG_ERROR("Failed to allocate mem for %d surfaces.\n", numSurfaces); + return 1; + } + + memset(pWorld->aSurfaces, 0, sizeSurfaces); + pWorld->numSurfaces = numSurfaces; + + SithSurface* pCurf = pWorld->aSurfaces; + for ( size_t i = 0; i < numSurfaces; ++i ) + { + rdFace_NewEntry(&pCurf->face); + ++pCurf; + } + + return 0; } -int J3DAPI sithSurface_AllocWorldSurfaces(SithWorld* pWorld, int numSurfaces) +void J3DAPI sithSurface_FreeWorldSurfaces(SithWorld* pWorld) { - return J3D_TRAMPOLINE_CALL(sithSurface_AllocWorldSurfaces, pWorld, numSurfaces); + SITH_ASSERTREL(pWorld != NULL); + SITH_ASSERTREL((pWorld->aSurfaces != NULL)); + + for ( size_t i = 0; i < pWorld->numSurfaces; ++i ) + { + SithSurface* pSurf = &pWorld->aSurfaces[i]; + + pSurf->face.numVertices = 0; + if ( pSurf->face.aVertices ) + { + stdMemory_Free(pSurf->face.aVertices); + } + + if ( pSurf->face.aTexVertices ) + { + stdMemory_Free(pSurf->face.aTexVertices); + } + + if ( pSurf->aIntensities ) + { + stdMemory_Free(pSurf->aIntensities); + } + + pSurf->flags = 0; + } + + stdMemory_Free(pWorld->aSurfaces); + + pWorld->aSurfaces = NULL; + pWorld->numSurfaces = 0; + sithSurface_numUnsyncedSurfaces = 0; + + if ( pWorld->aAdjoins ) + { + stdMemory_Free(pWorld->aAdjoins); + } + + pWorld->aAdjoins = NULL; + pWorld->numAdjoins = 0; } -void J3DAPI sithSurface_FreeWorldSurfaces(SithWorld* pWorld) +int J3DAPI sithSurface_WriteAdjoinsBinary(tFileHandle fh, SithWorld* pWorld) { - J3D_TRAMPOLINE_CALL(sithSurface_FreeWorldSurfaces, pWorld); + size_t sizeAdjoins = sizeof(CndSurfaceAdjoin) * pWorld->numAdjoins; + CndSurfaceAdjoin* aCndAdjoins = (CndSurfaceAdjoin*)STDMALLOC(sizeAdjoins); + if ( !aCndAdjoins ) + { + return 1; + } + + for ( size_t i = 0; i < pWorld->numAdjoins; ++i ) + { + SithSurfaceAdjoin* pAdjoin = &pWorld->aAdjoins[i]; + CndSurfaceAdjoin* pCndAdjoin = &aCndAdjoins[i]; + + pCndAdjoin->flags = pAdjoin->flags; + pCndAdjoin->mirrorIdx = pAdjoin->pMirrorAdjoin ? (pAdjoin->pMirrorAdjoin - pWorld->aAdjoins) : -1; + pCndAdjoin->distance = pAdjoin->distance; + } + + size_t nWritten = sith_g_pHS->pFileWrite(fh, aCndAdjoins, sizeAdjoins); + stdMemory_Free(aCndAdjoins); + return nWritten != sizeAdjoins; } -int J3DAPI sithSurface_CNDWriteAdjoinSection(tFileHandle fh, SithWorld* pWorld) +int J3DAPI sithSurface_LoadAdjoinsBinary(tFileHandle fh, SithWorld* pWorld) { - return J3D_TRAMPOLINE_CALL(sithSurface_CNDWriteAdjoinSection, fh, pWorld); + size_t sizeAdjoins = sizeof(CndSurfaceAdjoin) * pWorld->numAdjoins; + CndSurfaceAdjoin* aCndAdjoins = (CndSurfaceAdjoin*)STDMALLOC(sizeAdjoins); + if ( !aCndAdjoins || sith_g_pHS->pFileRead(fh, aCndAdjoins, sizeAdjoins) != sizeAdjoins || sithSurface_AllocWorldAdjoins(pWorld, pWorld->numAdjoins) ) + { + if ( aCndAdjoins ) + { + stdMemory_Free(aCndAdjoins); + } + + return 1; + } + + for ( size_t i = 0; i < pWorld->numAdjoins; ++i ) + { + SithSurfaceAdjoin* pAdjoin = &pWorld->aAdjoins[i]; + CndSurfaceAdjoin* pCndAdjoin = &aCndAdjoins[i]; + + pAdjoin->flags = pCndAdjoin->flags; + pAdjoin->pMirrorAdjoin = NULL; + if ( pCndAdjoin->mirrorIdx != -1 ) { + pAdjoin->pMirrorAdjoin = &pWorld->aAdjoins[pCndAdjoin->mirrorIdx]; + } + + pAdjoin->distance = pCndAdjoin->distance; + } + + stdMemory_Free(aCndAdjoins); + return 0; } -int J3DAPI sithSurface_CNDReadAdjoinSection(tFileHandle fh, SithWorld* pWorld) +int J3DAPI sithSurface_AllocWorldAdjoins(SithWorld* pWorld, size_t numAdjoins) { - return J3D_TRAMPOLINE_CALL(sithSurface_CNDReadAdjoinSection, fh, pWorld); + SITH_ASSERTREL(pWorld); + SITH_ASSERTREL(!pWorld->aAdjoins); + + if ( !numAdjoins ) + { + pWorld->aAdjoins = NULL; + return 0; + } + + const size_t sizeAdjoins = sizeof(SithSurfaceAdjoin) * numAdjoins; + pWorld->aAdjoins = (SithSurfaceAdjoin*)STDMALLOC(sizeAdjoins); + if ( !pWorld->aAdjoins ) + { + SITHLOG_ERROR("Failed to allocate mem for %d adjoins.\n", numAdjoins); + return 1; + } + + memset(pWorld->aAdjoins, 0, sizeAdjoins); + pWorld->numAdjoins = numAdjoins; + return 0; } -int J3DAPI sithSurface_AllocWorldAdjoins(SithWorld* pWorld, int numAdjoins) +int J3DAPI sithSurface_WriteAdjoinsText(const SithWorld* pWorld) { - return J3D_TRAMPOLINE_CALL(sithSurface_AllocWorldAdjoins, pWorld, numAdjoins); + if ( stdConffile_WriteLine("#------ Adjoins Subsection -----\n") + || stdConffile_Printf("World adjoins %d\n", pWorld->numAdjoins) + || stdConffile_WriteLine("#num:\tflags:\tmirror:\tdist:\n") ) + { + return 1; + } + + for ( size_t i = 0; i < pWorld->numAdjoins; ++i ) + { + SithSurfaceAdjoin* pAdjoin = &pWorld->aAdjoins[i]; + int mirror = -1; + if ( pAdjoin->pMirrorAdjoin ) + { + mirror = pAdjoin->pMirrorAdjoin - pWorld->aAdjoins; + } + + if ( stdConffile_Printf("%d:\t0x%x\t%d\t%.2f\n", i, pAdjoin->flags, mirror, pAdjoin->distance) ) + { + return 1; + } + } + + return stdConffile_WriteLine("\n") != 0; } -int J3DAPI sithSurface_CNDWriteSurfaceSection(tFileHandle fh, SithWorld* pWorld) +int J3DAPI sithSurface_WriteSurfacesBinary(tFileHandle fh, SithWorld* pWorld) { - return J3D_TRAMPOLINE_CALL(sithSurface_CNDWriteSurfaceSection, fh, pWorld); + size_t sizeSurfaces = sizeof(CndSurfaceInfo) * pWorld->numSurfaces; + CndSurfaceInfo* aCndSurfaces = (CndSurfaceInfo*)STDMALLOC(sizeSurfaces); + if ( !aCndSurfaces ) + { + return 1; + } + + uint32_t totalVerts = 0; + for ( size_t i = 0; i < pWorld->numSurfaces; ++i ) + { + SithSurface* pSurf = &pWorld->aSurfaces[i]; + CndSurfaceInfo* pCndSurf = &aCndSurfaces[i]; + + int matIdx = -1; + if ( pSurf->face.pMaterial ) + { + matIdx = pSurf->face.pMaterial->num; + } + + int numMaterials; + if ( SITHWORLD_IS_STATICINDEX(matIdx) ) + { + numMaterials = SITHWORLD_STATICINDEX(sithWorld_g_pStaticWorld->numMaterials); + } + else + { + numMaterials = pWorld->numMaterials; + } + + if ( matIdx > numMaterials ) + { + matIdx = -1; + SITHLOG_ERROR("Surface Material %s not in world.\n", pSurf->face.pMaterial->aName); + } + + if ( matIdx == -1 && (pSurf->face.geometryMode > RD_GEOMETRY_SOLID) ) + { + pSurf->face.geometryMode = RD_GEOMETRY_SOLID; + } + + pCndSurf->matNum = matIdx; + pCndSurf->surfflags = pSurf->flags; + pCndSurf->faceflags = pSurf->face.flags; + pCndSurf->geoMode = pSurf->face.geometryMode; + pCndSurf->lightMode = pSurf->face.lightingMode; + + int adjIdx = -1; + if ( pSurf->pAdjoin ) + { + adjIdx = pSurf->pAdjoin - pWorld->aAdjoins; + } + + pCndSurf->adjoinNum = adjIdx; + + rdVector_Copy4(&pCndSurf->color, &pSurf->face.extraLight); + rdVector_Copy3(&pCndSurf->normal, &pSurf->face.normal); + pCndSurf->numVerts = pSurf->face.numVertices; + + totalVerts += pSurf->face.numVertices; + } + + size_t nWritten = sith_g_pHS->pFileWrite(fh, aCndSurfaces, sizeSurfaces); + stdMemory_Free(aCndSurfaces); // Free + if ( nWritten != sizeSurfaces ) + { + return 1; + } + + nWritten = sith_g_pHS->pFileWrite(fh, &totalVerts, sizeof(totalVerts)); static_assert(sizeof(totalVerts) == 4, "sizeof(totalVerts) == 4"); + if ( nWritten != 4 ) + { + return 1; + } + + size_t sizeVerts = sizeof(CndSurfaceVertInfo) * totalVerts; + CndSurfaceVertInfo* aCndSurfVerts = (CndSurfaceVertInfo*)STDMALLOC(sizeVerts); + if ( !aCndSurfVerts ) + { + return 1; + } + + CndSurfaceVertInfo* pCndSurfVert = aCndSurfVerts; + for ( size_t i = 0; i < pWorld->numSurfaces; ++i ) + { + SithSurface* pSurf = &pWorld->aSurfaces[i]; + for ( size_t j = 0; j < pSurf->face.numVertices; ++j ) + { + pCndSurfVert->vertIdx = pSurf->face.aVertices[j]; + pCndSurfVert->texIdx = 0; + if ( pSurf->face.aTexVertices ) + { + pCndSurfVert->texIdx = pSurf->face.aTexVertices[j]; + } + + rdVector_Copy4(&pCndSurfVert->color, &pSurf->aIntensities[j]); + ++pCndSurfVert; + } + } + + nWritten = sith_g_pHS->pFileWrite(fh, aCndSurfVerts, sizeVerts); + stdMemory_Free(aCndSurfVerts); + return nWritten != sizeVerts; } -int J3DAPI sithSurface_CNDReadSurfaceSection(tFileHandle fh, SithWorld* pWorld) +int J3DAPI sithSurface_LoadSurfacesBinary(tFileHandle fh, SithWorld* pWorld) { - return J3D_TRAMPOLINE_CALL(sithSurface_CNDReadSurfaceSection, fh, pWorld); + CndSurfaceVertInfo* aCndSurfVerts = NULL; // Added: Init to null, fixes potential freeing unallocated mem when error occurs + + size_t sizeSurfaces = sizeof(CndSurfaceInfo) * pWorld->numSurfaces; + CndSurfaceInfo* aCndSurfaces = (CndSurfaceInfo*)STDMALLOC(sizeSurfaces); + if ( !aCndSurfaces ) + { + goto error; + } + + size_t nRead = sith_g_pHS->pFileRead(fh, aCndSurfaces, sizeSurfaces); + if ( nRead != sizeSurfaces || sithSurface_AllocWorldSurfaces(pWorld, pWorld->numSurfaces) ) + { + goto error; + } + + for ( size_t i = 0; i < pWorld->numSurfaces; ++i ) + { + SithSurface* pSurf = &pWorld->aSurfaces[i]; + CndSurfaceInfo* pCndSurf = &aCndSurfaces[i]; + + if ( pCndSurf->matNum == -1 ) + { + pSurf->face.pMaterial = NULL; + } + else if ( SITHWORLD_IS_STATICINDEX(pCndSurf->matNum) ) + { + pCndSurf->matNum = SITHWORLD_FROM_STATICINDEX(pCndSurf->matNum); + pSurf->face.pMaterial = &sithWorld_g_pStaticWorld->aMaterials[pCndSurf->matNum]; + } + else + { + pSurf->face.pMaterial = pWorld->apMatArray[pCndSurf->matNum]; + } + + pSurf->flags = pCndSurf->surfflags; + pSurf->face.flags = pCndSurf->faceflags; + pSurf->face.geometryMode = pCndSurf->geoMode; + pSurf->face.lightingMode = pCndSurf->lightMode; + + pSurf->pAdjoin = NULL; + if ( pCndSurf->adjoinNum != -1 ) + { + pSurf->pAdjoin = &pWorld->aAdjoins[pCndSurf->adjoinNum]; + pSurf->pAdjoin->pAdjoinSurface = pSurf; + } + + rdVector_Copy4(&pSurf->face.extraLight, &pCndSurf->color); + rdVector_Copy3(&pSurf->face.normal, &pCndSurf->normal); + pSurf->face.numVertices = pCndSurf->numVerts; + } + + if ( aCndSurfaces ) + { + stdMemory_Free(aCndSurfaces); + aCndSurfaces = NULL; // Added + } + + // Free apMatArray + // TODO: why is this freed + if ( pWorld->apMatArray ) { + stdMemory_Free(pWorld->apMatArray); + } + pWorld->apMatArray = NULL; + + size_t totalVerts; + nRead = sith_g_pHS->pFileRead(fh, &totalVerts, sizeof(int)); static_assert(sizeof(int) == 4, "sizeof(int) == 4"); + if ( nRead != 4 ) { + return 1; + } + + size_t sizeVerts = sizeof(CndSurfaceVertInfo) * totalVerts; + aCndSurfVerts = (CndSurfaceVertInfo*)STDMALLOC(sizeVerts); + if ( !aCndSurfVerts ) { + return 1; + } + + nRead = sith_g_pHS->pFileRead(fh, aCndSurfVerts, sizeVerts); + if ( nRead != sizeVerts ) { + goto error; + } + + CndSurfaceVertInfo* pCndSurfVert = aCndSurfVerts; + for ( size_t i = 0; i < pWorld->numSurfaces; ++i ) + { + SithSurface* pSurf = &pWorld->aSurfaces[i]; + + int* aVerts = (int*)STDMALLOC(4 * pSurf->face.numVertices); + pSurf->face.aVertices = aVerts; + if ( !pSurf->face.aVertices ) { + goto error; + } + + int* aTexVerts = (int*)STDMALLOC(4 * pSurf->face.numVertices); + pSurf->face.aTexVertices = aTexVerts; + if ( !pSurf->face.aTexVertices ) { + goto error; + } + + rdVector4* aVertColors = (rdVector4*)STDMALLOC(sizeof(rdVector4) * pSurf->face.numVertices); + pSurf->aIntensities = aVertColors; + if ( !pSurf->aIntensities ) { + goto error; + } + + for ( size_t j = 0; j < pSurf->face.numVertices; ++j ) + { + pSurf->face.aVertices[j] = pCndSurfVert->vertIdx; + pSurf->face.aTexVertices[j] = pCndSurfVert->texIdx; + rdVector_Copy4(&pSurf->aIntensities[j], &pCndSurfVert->color); + ++pCndSurfVert; + } + } + + // Success + stdMemory_Free(aCndSurfVerts); + return 0; + +error: + if ( aCndSurfaces ) { + stdMemory_Free(aCndSurfaces); + } + + if ( aCndSurfVerts ) { + stdMemory_Free(aCndSurfVerts); + } + + return 1; } -signed int J3DAPI sithSurface_NDYReadSurfaceList(SithWorld* pWorld) +int J3DAPI sithSurface_WriteSurfacesText(const SithWorld* pWorld) { - return J3D_TRAMPOLINE_CALL(sithSurface_NDYReadSurfaceList, pWorld); + if ( stdConffile_WriteLine("#----- Surfaces Subsection -----\n") + || stdConffile_Printf("World surfaces %d\n", pWorld->numSurfaces) + || stdConffile_WriteLine("#num:\tmat:\tsurfflags:\tfaceflags:\tgeo:\tlight:\ttex:\tadjoin:\tExLR:\tExLG:\tExLB:\tnverts:\tvertices:\t\t\tiR:\tiG\tiB\n") != 0 ) + { + return 1; + } + + for ( size_t i = 0; i < pWorld->numSurfaces; ++i ) + { + SithSurface* pSurf = &pWorld->aSurfaces[i]; + + int matIdx = -1; + if ( pSurf->face.pMaterial ) + { + matIdx = pSurf->face.pMaterial->num; + } + + int numMaterials = pWorld->numMaterials; + if ( SITHWORLD_IS_STATICINDEX(matIdx) ) + { + numMaterials = SITHWORLD_STATICINDEX(sithWorld_g_pStaticWorld->numMaterials); + } + + if ( matIdx > numMaterials ) + { + SITHLOG_ERROR("Surface Material %s not in world.\n", pSurf->face.pMaterial->aName); + + pSurf->face.pMaterial = NULL; // TODO: why set to NULL? + matIdx = -1; + } + + if ( matIdx == -1 && pSurf->face.geometryMode > RD_GEOMETRY_SOLID ) { + pSurf->face.geometryMode = RD_GEOMETRY_SOLID; + } + + int adjIdx = -1; + if ( pSurf->pAdjoin ) + { + adjIdx = pSurf->pAdjoin - pWorld->aAdjoins; + } + + if ( stdConffile_Printf( + "%d:\t%d\t0x%x\t\t0x%x\t\t%d\t%d\t%d\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t\t%d\t", + i, + matIdx, + pSurf->flags, + pSurf->face.flags, + pSurf->face.geometryMode, + pSurf->face.lightingMode, + 3, // texmode + adjIdx, + pSurf->face.extraLight.red, + pSurf->face.extraLight.green, + pSurf->face.extraLight.blue, + pSurf->face.extraLight.alpha, + pSurf->face.numVertices) != 0 ) + { + return 1; + } + + for ( size_t j = 0; j < pSurf->face.numVertices; ++j ) + { + if ( pSurf->face.geometryMode < RD_GEOMETRY_FULL ) + { + if ( stdConffile_Printf("%d,-1\t", pSurf->face.aVertices[j]) != 0 ) + { + return 1; + } + } + else + { + SITH_ASSERTREL(pSurf->face.pMaterial != NULL); + SITH_ASSERTREL(pSurf->face.aTexVertices != NULL); + if ( stdConffile_Printf("%d,%d\t", pSurf->face.aVertices[j], pSurf->face.aTexVertices[j]) != 0 ) + { + return 1; + } + } + } + + for ( size_t k = 0; k < pSurf->face.numVertices; ++k ) + { + if ( stdConffile_Printf("%f\t%f\t%f\t", pSurf->aIntensities[k].red, pSurf->aIntensities[k].green, pSurf->aIntensities[k].blue) != 0 ) + { + return 1; + } + } + + if ( stdConffile_WriteLine("\n") != 0 ) + { + return 1; + } + } + + if ( stdConffile_WriteLine("\n#--- Surface normals ---\n") != 0 ) + { + return 1; + } + + // Write face normals + for ( size_t i = 0; i < pWorld->numSurfaces; i++ ) + { + rdFace* pFace = &pWorld->aSurfaces[i].face; + if ( stdConffile_Printf("%d:\t%f\t%f\t%f\n", i, pFace->normal.x, pFace->normal.y, pFace->normal.z) != 0 ) + { + return 1; + } + } + + return 0; +} + +int J3DAPI sithSurface_LoadSurfacesText(SithWorld* pWorld) +{ + SITH_ASSERTREL(pWorld != NULL); + SITH_ASSERTREL(pWorld->apMatArray != NULL); + + // Parse adjoins + + int nRead = 0; + size_t numAdjoins; + if ( nRead = stdConffile_ScanLine(" world adjoins %d", &numAdjoins), nRead != 1 ) { + if ( nRead < 0 ) { + goto eof_error; + } + goto syntax_error; + } + + if ( sithSurface_AllocWorldAdjoins(pWorld, numAdjoins) ) + { + goto alloc_error; + } + + for ( size_t i = 0; i < numAdjoins; i++ ) + { + if ( !stdConffile_ReadArgs() ) { + goto eof_error; + } + + int curArg = 1; + SithSurfaceAdjoin* pAdjoin = &pWorld->aAdjoins[i]; + if ( sscanf_s(stdConffile_g_entry.aArgs[curArg++].argValue, "%x", &pAdjoin->flags) != 1 ) + { + goto syntax_error; + } + + size_t mirrorIdx = atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + if ( mirrorIdx >= pWorld->numAdjoins ) + { + SITHLOG_ERROR("Adjoin mirror index out of range!\n"); + goto range_error; + } + + pAdjoin->pMirrorAdjoin = &pWorld->aAdjoins[mirrorIdx]; + pAdjoin->distance = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + } + + // Parse surface + + size_t numSurfaces; + if ( nRead = stdConffile_ScanLine(" world surfaces %d", &numSurfaces), nRead != 1 ) { + if ( nRead < 0 ) { + goto eof_error; + } + goto syntax_error; + } + + if ( sithSurface_AllocWorldSurfaces(pWorld, numSurfaces) ) + { + goto alloc_error; + } + + for ( size_t i = 0; i < numSurfaces; i++ ) + { + SithSurface* pSurf = &pWorld->aSurfaces[i]; + + if ( !stdConffile_ReadArgs() ) { + goto eof_error; + } + + rdFace* pFace = &pSurf->face; + pFace->pMaterial = NULL; + + int curArg = 1; + int matIdx = atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + if ( matIdx >= 0 ) + { + if ( SITHWORLD_IS_STATICINDEX(matIdx) ) + { + matIdx = SITHWORLD_FROM_STATICINDEX(matIdx); + if ( matIdx >= sithWorld_g_pStaticWorld->numMaterials ) + { + SITHLOG_ERROR("Surface material index out of range!\n"); + goto range_error; + } + + pSurf->face.pMaterial = &sithWorld_g_pStaticWorld->aMaterials[matIdx]; // Why not grabbed from apMatArray?? + } + else + { + if ( matIdx >= pWorld->sizeMaterials ) + { + SITHLOG_ERROR("Surface material index out of range!\n"); + goto range_error; + } + + pFace->pMaterial = pWorld->apMatArray[matIdx]; + } + } + + // surfflags + if ( sscanf_s(stdConffile_g_entry.aArgs[curArg++].argValue, "%x", &pSurf->flags) != 1 ) + { + goto syntax_error; + } + + // faceflags + if ( sscanf_s(stdConffile_g_entry.aArgs[curArg++].argValue, "%x", &pFace->flags) != 1 ) + { + goto syntax_error; + } + + // geomode + pFace->geometryMode = atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + if ( !pFace->pMaterial && pFace->geometryMode > RD_GEOMETRY_SOLID ) { + pFace->geometryMode = RD_GEOMETRY_SOLID; + } + + // lightmode + pFace->lightingMode = atoi(stdConffile_g_entry.aArgs[curArg++].argValue);; + if ( (pSurf->flags & (SITH_SURFACE_CEILINGSKY | SITH_SURFACE_HORIZONSKY)) != 0 ) { + pFace->lightingMode = RD_LIGHTING_NONE; + } + + // Skip tex mode + atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + + // Adjoin + pSurf->pAdjoin = NULL; + int adjIdx = atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + if ( adjIdx != -1 ) + { + pSurf->pAdjoin = &pWorld->aAdjoins[adjIdx]; + pSurf->pAdjoin->pAdjoinSurface = pSurf; + + if ( (pSurf->pAdjoin->flags & SITH_ADJOIN_VISIBLE) == 0 || pFace->pMaterial + && pFace->geometryMode + && (pFace->flags & RD_FF_TEX_TRANSLUCENT) == 0 + && pFace->pMaterial->formatType == STDCOLOR_FORMAT_RGB ) + { + pSurf->pAdjoin->flags |= SITH_ADJOIN_UNKNOWN_80; // Probably solid + } + } + + // Extra light + pFace->extraLight.red = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + pFace->extraLight.green = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + pFace->extraLight.blue = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + pFace->extraLight.alpha = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + + // num verts + size_t numVerts = atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + if ( numVerts < 3 || numVerts > stdConffile_g_entry.numArgs - curArg ) + { + goto syntax_error; + } + + if ( numVerts > RDCACHE_MAXFACEVERTICES ) // Changed: Set to RDCACHE_MAXFACEVERTICES, was fixed 64 + { + goto range_error; + } + + // Alloc vert related buffers + int* aVerts = (int*)STDMALLOC(sizeof(int) * numVerts); + pFace->aVertices = aVerts; + if ( !pFace->aVertices ) { + goto alloc_error; + } + + pSurf->aIntensities = (rdVector4*)STDMALLOC(sizeof(rdVector4) * numVerts); + if ( !pSurf->aIntensities ) { + goto alloc_error; + } + + pFace->aTexVertices = (int*)STDMALLOC(sizeof(int) * numVerts);; + if ( !pFace->aTexVertices ) { + goto alloc_error; + } + + // parse vert/tex vert idxs + for ( size_t j = 0; j < numVerts; ++j ) + { + pFace->aVertices[j] = atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + + if ( pFace->pMaterial ) { + pFace->aTexVertices[j] = atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + } + else + { + ++curArg; // Skip UV vert arg + pFace->aTexVertices[j] = 0; + } + } + + // vert colors + for ( size_t j = 0; j < numVerts; ++j ) + { + + pSurf->aIntensities[j].red = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + pSurf->aIntensities[j].green = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + pSurf->aIntensities[j].blue = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + pSurf->aIntensities[j].alpha = pFace->extraLight.alpha; + } + + pFace->num = i; + pFace->numVertices = numVerts; + } + + // Read face normals + for ( size_t i = 0; i < numSurfaces; ++i ) + { + int normNum; + float nx, ny, nz; + if ( nRead = stdConffile_ScanLine("%d: %f %f %f", &normNum, &nx, &ny, &nz), nRead != 4 ) + { + if ( nRead < 0 ) { + goto eof_error; + } + goto syntax_error; + } + + rdFace* pFace = &pWorld->aSurfaces[i].face; + pFace->normal.x = nx; + pFace->normal.y = ny; + pFace->normal.z = nz; + } + + pWorld->numSurfaces = numSurfaces; + + // TODO: Why apMatArray is being freed here? + if ( pWorld->apMatArray ) + { + stdMemory_Free(pWorld->apMatArray); + } + + // Success + pWorld->apMatArray = NULL; + return 0; + +alloc_error: + SITHLOG_ERROR("Error: Bad memory allocation for '%s' in '%s'\n", stdConffile_g_aLine, stdConffile_GetFilename()); + return 1; + +eof_error: + SITHLOG_ERROR("Error: Unexpected EOF in '%s'\n", stdConffile_GetFilename()); + return 1; + +range_error: + SITHLOG_ERROR("Error: Value out of range for '%s' in '%s'\n", stdConffile_g_aLine, stdConffile_GetFilename()); + return 1; + +syntax_error: + SITHLOG_ERROR("Error: Invalid syntax '%s' in '%s'\n", stdConffile_g_aLine, stdConffile_GetFilename()); + return 1; } int J3DAPI sithSurface_ValidateSurfacePointer(const SithSurface* pSurf) { - return J3D_TRAMPOLINE_CALL(sithSurface_ValidateSurfacePointer, pSurf); + return pSurf && pSurf->pSector != NULL; } int J3DAPI sithSurface_ValidateWorldSurfaces(const SithWorld* pWorld) { - return J3D_TRAMPOLINE_CALL(sithSurface_ValidateWorldSurfaces, pWorld); + SITH_ASSERTREL(pWorld != NULL); + + for ( size_t i = 0; i < pWorld->numSurfaces; ++i ) + { + if ( !sithSurface_ValidateSurfacePointer(&pWorld->aSurfaces[i]) ) { + return 0; + } + } + + return 1; } void J3DAPI sithSurface_HandleThingImpact(SithSurface* pSurf, SithThing* pThing, float damage, int damageType) { - J3D_TRAMPOLINE_CALL(sithSurface_HandleThingImpact, pSurf, pThing, damage, damageType); + if ( !stdComm_IsGameActive() || !pThing || (pThing->flags & SITH_TF_REMOTE) == 0 ) // TODO: fix bug !pThing, should be pThing && + { + if ( pThing->type == SITH_THING_WEAPON && (pThing->thingInfo.weaponInfo.flags & SITH_WF_IMPACTSOUND) != 0 ) + { + sithSurface_PlaySurfaceHitSound(pSurf, pThing, damageType); + } + + if ( (pSurf->flags & SITH_SURFACE_COGLINKED) != 0 ) + { + sithCog_SurfaceSendMessageEx(pSurf, pThing, SITHCOG_MSG_DAMAGED, (int32_t)damage, damageType, 0, 0); + } + } } void J3DAPI sithSurface_PlaySurfaceHitSound(const SithSurface* pSurf, SithThing* pThing, int damageType) { - J3D_TRAMPOLINE_CALL(sithSurface_PlaySurfaceHitSound, pSurf, pThing, damageType); + tSoundHandle hSnd; + if ( (damageType & SITH_DAMAGE_MACHETE) != 0 ) + { + if ( (pSurf->flags & SITH_SURFACE_METAL) != 0 ) + { + hSnd = Sound_GetSoundHandle(SITHWORLD_STATICINDEX(82)); // 0x8052 + sithSoundMixer_PlaySoundPos(hSnd, &pThing->pos, pThing->pInSector, 1.0f, 0.5f, 2.5f, SOUNDPLAY_ABSOLUTE_POS); + } + else if ( (pSurf->flags & (SITH_SURFACE_SNOW | SITH_SURFACE_EARTH | 0x80000000)) != 0 ) // 0x80000000 - EARTH_ECHO + { + hSnd = Sound_GetSoundHandle(SITHWORLD_STATICINDEX(84)); //0x8054 + sithSoundMixer_PlaySoundPos(hSnd, &pThing->pos, pThing->pInSector, 1.0f, 0.5f, 2.5f, SOUNDPLAY_ABSOLUTE_POS); + } + else if ( (pSurf->flags & SITH_SURFACE_WEB) != 0 ) + { + hSnd = Sound_GetSoundHandle(SITHWORLD_STATICINDEX(85)); // 0x8055 + sithSoundMixer_PlaySoundPos(hSnd, &pThing->pos, pThing->pInSector, 1.0f, 0.5f, 2.5f, SOUNDPLAY_ABSOLUTE_POS); + } + else + { + if ( (pSurf->flags & (SITH_SURFACE_WOODECHO | SITH_SURFACE_WOOD)) != 0 ) + { + hSnd = Sound_GetSoundHandle(SITHWORLD_STATICINDEX(83)); // 0x8053 + } + else + { + hSnd = Sound_GetSoundHandle(SITHWORLD_STATICINDEX(81)); // 0x8051 + } + + sithSoundMixer_PlaySoundPos(hSnd, &pThing->pos, pThing->pInSector, 1.0f, 0.5f, 2.5f, SOUNDPLAY_ABSOLUTE_POS); + } + } } int J3DAPI sithSurface_GetCenterPoint(const SithSurface* pSurface, rdVector3* centerpoint) { - return J3D_TRAMPOLINE_CALL(sithSurface_GetCenterPoint, pSurface, centerpoint); + rdVector3 vertSum = { 0 }; + for ( size_t i = 0; i < pSurface->face.numVertices; ++i ) + { + rdVector_Add3Acc(&vertSum, &sithWorld_g_pCurrentWorld->aVertices[pSurface->face.aVertices[i]]); + } + + // Divide by num verts + float scale = (float)pSurface->face.numVertices; + rdVector_InvScale3(centerpoint, &vertSum, scale); + + if ( !sithIntersect_IsSphereInSector(sithWorld_g_pCurrentWorld, centerpoint, 0.0f, pSurface->pSector) ) + { + rdVector3 offset; + offset.x = pSurface->face.normal.x * 0.00019999999f; // / 5000 + offset.y = pSurface->face.normal.y * 0.00019999999f; + offset.z = pSurface->face.normal.z * 0.00019999999f; + rdVector_Add3Acc(centerpoint, &offset); + } + + int bValid = sithIntersect_IsSphereInSector(sithWorld_g_pCurrentWorld, centerpoint, 0.0f, pSurface->pSector); + if ( !bValid ) { + SITHLOG_ERROR("Cannot place surface centerpoint in parent sector.\n"); + } + + return bValid; } -int J3DAPI sithSurface_SyncSurface(SithSurface* pSurface) +void J3DAPI sithSurface_SyncSurface(SithSurface* pSurface) { - return J3D_TRAMPOLINE_CALL(sithSurface_SyncSurface, pSurface); + SITH_ASSERTREL(pSurface); + + pSurface->flags |= SITH_SURFACE_SYNC; + if ( sithSurface_numUnsyncedSurfaces < STD_ARRAYLEN(sithSurface_apUnsyncedSurfaces) ) { + sithSurface_apUnsyncedSurfaces[sithSurface_numUnsyncedSurfaces++] = pSurface; + } } -void J3DAPI sithSurface_SyncSurfaces() +void sithSurface_SyncSurfaces(void) { - J3D_TRAMPOLINE_CALL(sithSurface_SyncSurfaces); + if ( sithMessage_g_outputstream && sithSurface_numUnsyncedSurfaces ) + { + for ( size_t i = 0; i < sithSurface_numUnsyncedSurfaces; ++i ) { + sithDSS_SurfaceStatus(sithSurface_apUnsyncedSurfaces[i], SITHMESSAGE_SENDTOALL, 0xFFu); + } + + sithSurface_numUnsyncedSurfaces = 0; + } } SithSurface* J3DAPI sithSurface_GetSurfaceEx(const SithWorld* pWorld, int surfIdx) { - return J3D_TRAMPOLINE_CALL(sithSurface_GetSurfaceEx, pWorld, surfIdx); + if ( pWorld && surfIdx >= 0 && surfIdx < pWorld->numSurfaces ) { + return &pWorld->aSurfaces[surfIdx]; + } + return NULL; } int J3DAPI sithSurface_GetSurfaceIndex(const SithSurface* pSurf) { - return J3D_TRAMPOLINE_CALL(sithSurface_GetSurfaceIndex, pSurf); + if ( pSurf >= sithWorld_g_pCurrentWorld->aSurfaces && pSurf <= &sithWorld_g_pCurrentWorld->aSurfaces[sithWorld_g_pCurrentWorld->numSurfaces] ) { + return pSurf - sithWorld_g_pCurrentWorld->aSurfaces; + } + return -1; } int J3DAPI sithSurface_GetSurfaceIndexEx(const SithWorld* pWorld, const SithSurface* pSurface) { - return J3D_TRAMPOLINE_CALL(sithSurface_GetSurfaceIndexEx, pWorld, pSurface); + if ( pSurface >= pWorld->aSurfaces && pSurface <= &pWorld->aSurfaces[pWorld->numSurfaces] ) { + return pSurface - pWorld->aSurfaces; + } + return -1; } diff --git a/Libs/sith/World/sithSurface.h b/Libs/sith/World/sithSurface.h index 1d05b92..88ef5a5 100644 --- a/Libs/sith/World/sithSurface.h +++ b/Libs/sith/World/sithSurface.h @@ -3,29 +3,38 @@ #include #include #include -#include -#include #include J3D_EXTERN_C_START -char J3DAPI sithSurface_HideSectorAdjoin(SithSurfaceAdjoin* pAdjoin); -char J3DAPI sithSurface_ShowSectorAdjoin(SithSurfaceAdjoin* pAdjoin); -int J3DAPI sithSurface_AllocWorldSurfaces(SithWorld* pWorld, int numSurfaces); +void J3DAPI sithSurface_HideSectorAdjoin(SithSurfaceAdjoin* pAdjoin); +void J3DAPI sithSurface_ShowSectorAdjoin(SithSurfaceAdjoin* pAdjoin); + +int J3DAPI sithSurface_AllocWorldSurfaces(SithWorld* pWorld, size_t numSurfaces); void J3DAPI sithSurface_FreeWorldSurfaces(SithWorld* pWorld); -int J3DAPI sithSurface_CNDWriteAdjoinSection(tFileHandle fh, SithWorld* pWorld); -int J3DAPI sithSurface_CNDReadAdjoinSection(tFileHandle fh, SithWorld* pWorld); -int J3DAPI sithSurface_AllocWorldAdjoins(SithWorld* pWorld, int numAdjoins); -int J3DAPI sithSurface_CNDWriteSurfaceSection(tFileHandle fh, SithWorld* pWorld); -int J3DAPI sithSurface_CNDReadSurfaceSection(tFileHandle fh, SithWorld* pWorld); -signed int J3DAPI sithSurface_NDYReadSurfaceList(SithWorld* pWorld); + +int J3DAPI sithSurface_AllocWorldAdjoins(SithWorld* pWorld, size_t numAdjoins); + +int J3DAPI sithSurface_WriteAdjoinsBinary(tFileHandle fh, SithWorld* pWorld); +int J3DAPI sithSurface_LoadAdjoinsBinary(tFileHandle fh, SithWorld* pWorld); +int J3DAPI sithSurface_WriteAdjoinsText(const SithWorld* pWorld); // Added + +int J3DAPI sithSurface_WriteSurfacesBinary(tFileHandle fh, SithWorld* pWorld); +int J3DAPI sithSurface_LoadSurfacesBinary(tFileHandle fh, SithWorld* pWorld); + +int J3DAPI sithSurface_WriteSurfacesText(const SithWorld* pWorld); // Added +int J3DAPI sithSurface_LoadSurfacesText(SithWorld* pWorld); + int J3DAPI sithSurface_ValidateSurfacePointer(const SithSurface* pSurf); int J3DAPI sithSurface_ValidateWorldSurfaces(const SithWorld* pWorld); +int J3DAPI sithSurface_GetCenterPoint(const SithSurface* pSurface, rdVector3* centerpoint); + void J3DAPI sithSurface_HandleThingImpact(SithSurface* pSurf, SithThing* pThing, float damage, int damageType); void J3DAPI sithSurface_PlaySurfaceHitSound(const SithSurface* pSurf, SithThing* pThing, int damageType); -int J3DAPI sithSurface_GetCenterPoint(const SithSurface* pSurface, rdVector3* centerpoint); -int J3DAPI sithSurface_SyncSurface(SithSurface* pSurface); -void J3DAPI sithSurface_SyncSurfaces(); + +void J3DAPI sithSurface_SyncSurface(SithSurface* pSurface); +void sithSurface_SyncSurfaces(void); + SithSurface* J3DAPI sithSurface_GetSurfaceEx(const SithWorld* pWorld, int surfIdx); int J3DAPI sithSurface_GetSurfaceIndex(const SithSurface* pSurf); int J3DAPI sithSurface_GetSurfaceIndexEx(const SithWorld* pWorld, const SithSurface* pSurface); diff --git a/Libs/sith/World/sithWorld.c b/Libs/sith/World/sithWorld.c index d989ba4..27e8279 100644 --- a/Libs/sith/World/sithWorld.c +++ b/Libs/sith/World/sithWorld.c @@ -73,13 +73,13 @@ void sithWorld_ResetGlobals(void) }, { "adjoins", - &sithSurface_CNDReadAdjoinSection, - &sithSurface_CNDWriteAdjoinSection + &sithSurface_LoadAdjoinsBinary, + &sithSurface_WriteAdjoinsBinary }, { "surfaces", - &sithSurface_CNDReadSurfaceSection, - &sithSurface_CNDWriteSurfaceSection + &sithSurface_LoadSurfacesBinary, + &sithSurface_WriteSurfacesBinary }, { "sectors", diff --git a/Libs/wkernel/wkernel.h b/Libs/wkernel/wkernel.h index eff7197..773a130 100644 --- a/Libs/wkernel/wkernel.h +++ b/Libs/wkernel/wkernel.h @@ -4,6 +4,8 @@ #include #include +// TODO: move module to std + J3D_EXTERN_C_START int J3DAPI wkernel_Run(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd, LPCSTR lpWindowName); // Note, process proc must be set before From c523717dc9e80b23efe1b6cfc14b2816b7f3bd53 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Mon, 23 Dec 2024 23:48:58 +0100 Subject: [PATCH 31/35] [sith] Fix potential null pointer dereference --- Libs/sith/World/sithSurface.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Libs/sith/World/sithSurface.c b/Libs/sith/World/sithSurface.c index b584aa5..1c7042e 100644 --- a/Libs/sith/World/sithSurface.c +++ b/Libs/sith/World/sithSurface.c @@ -275,7 +275,7 @@ int J3DAPI sithSurface_WriteSurfacesBinary(tFileHandle fh, SithWorld* pWorld) } int numMaterials; - if ( SITHWORLD_IS_STATICINDEX(matIdx) ) + if ( matIdx != -1 && SITHWORLD_IS_STATICINDEX(matIdx) ) // Added: Check for matIdx != -1 { numMaterials = SITHWORLD_STATICINDEX(sithWorld_g_pStaticWorld->numMaterials); } @@ -284,7 +284,7 @@ int J3DAPI sithSurface_WriteSurfacesBinary(tFileHandle fh, SithWorld* pWorld) numMaterials = pWorld->numMaterials; } - if ( matIdx > numMaterials ) + if ( matIdx != -1 && matIdx > numMaterials ) // Added: Check for matIdx != -1 { matIdx = -1; SITHLOG_ERROR("Surface Material %s not in world.\n", pSurf->face.pMaterial->aName); @@ -510,16 +510,16 @@ int J3DAPI sithSurface_WriteSurfacesText(const SithWorld* pWorld) } int numMaterials = pWorld->numMaterials; - if ( SITHWORLD_IS_STATICINDEX(matIdx) ) + if ( matIdx != -1 && SITHWORLD_IS_STATICINDEX(matIdx) ) // Added: Check for matIdx != -1 { numMaterials = SITHWORLD_STATICINDEX(sithWorld_g_pStaticWorld->numMaterials); } - if ( matIdx > numMaterials ) + if ( matIdx != -1 && matIdx > numMaterials ) // Added: Check for matIdx != -1 { SITHLOG_ERROR("Surface Material %s not in world.\n", pSurf->face.pMaterial->aName); - pSurf->face.pMaterial = NULL; // TODO: why set to NULL? + pSurf->face.pMaterial = NULL; // TODO: why set to NULL here? matIdx = -1; } @@ -646,7 +646,7 @@ int J3DAPI sithSurface_LoadSurfacesText(SithWorld* pWorld) } pAdjoin->pMirrorAdjoin = &pWorld->aAdjoins[mirrorIdx]; - pAdjoin->distance = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + pAdjoin->distance = strtof(stdConffile_g_entry.aArgs[curArg++].argValue, NULL); } // Parse surface @@ -727,7 +727,8 @@ int J3DAPI sithSurface_LoadSurfacesText(SithWorld* pWorld) } // Skip tex mode - atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + // Removed: atoi(stdConffile_g_entry.aArgs[curArg++].argValue); + curArg++; // Adjoin pSurf->pAdjoin = NULL; @@ -747,10 +748,10 @@ int J3DAPI sithSurface_LoadSurfacesText(SithWorld* pWorld) } // Extra light - pFace->extraLight.red = atof(stdConffile_g_entry.aArgs[curArg++].argValue); - pFace->extraLight.green = atof(stdConffile_g_entry.aArgs[curArg++].argValue); - pFace->extraLight.blue = atof(stdConffile_g_entry.aArgs[curArg++].argValue); - pFace->extraLight.alpha = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + pFace->extraLight.red = strtof(stdConffile_g_entry.aArgs[curArg++].argValue, NULL); + pFace->extraLight.green = strtof(stdConffile_g_entry.aArgs[curArg++].argValue, NULL); + pFace->extraLight.blue = strtof(stdConffile_g_entry.aArgs[curArg++].argValue, NULL); + pFace->extraLight.alpha = strtof(stdConffile_g_entry.aArgs[curArg++].argValue, NULL); // num verts size_t numVerts = atoi(stdConffile_g_entry.aArgs[curArg++].argValue); @@ -800,9 +801,9 @@ int J3DAPI sithSurface_LoadSurfacesText(SithWorld* pWorld) for ( size_t j = 0; j < numVerts; ++j ) { - pSurf->aIntensities[j].red = atof(stdConffile_g_entry.aArgs[curArg++].argValue); - pSurf->aIntensities[j].green = atof(stdConffile_g_entry.aArgs[curArg++].argValue); - pSurf->aIntensities[j].blue = atof(stdConffile_g_entry.aArgs[curArg++].argValue); + pSurf->aIntensities[j].red = strtof(stdConffile_g_entry.aArgs[curArg++].argValue, NULL); + pSurf->aIntensities[j].green = strtof(stdConffile_g_entry.aArgs[curArg++].argValue, NULL); + pSurf->aIntensities[j].blue = strtof(stdConffile_g_entry.aArgs[curArg++].argValue, NULL); pSurf->aIntensities[j].alpha = pFace->extraLight.alpha; } @@ -879,7 +880,8 @@ int J3DAPI sithSurface_ValidateWorldSurfaces(const SithWorld* pWorld) void J3DAPI sithSurface_HandleThingImpact(SithSurface* pSurf, SithThing* pThing, float damage, int damageType) { - if ( !stdComm_IsGameActive() || !pThing || (pThing->flags & SITH_TF_REMOTE) == 0 ) // TODO: fix bug !pThing, should be pThing && + // Fixed: Fixed check for not null pThing + if ( pThing && (!stdComm_IsGameActive() || (pThing->flags & SITH_TF_REMOTE) == 0) ) { if ( pThing->type == SITH_THING_WEAPON && (pThing->thingInfo.weaponInfo.flags & SITH_WF_IMPACTSOUND) != 0 ) { From 3152496cbaa5c54b444e6087100099e13779d867 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Mon, 23 Dec 2024 23:44:22 +0100 Subject: [PATCH 32/35] [std] Allow only true color textures --- Libs/std/Win95/stdDisplay.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Libs/std/Win95/stdDisplay.c b/Libs/std/Win95/stdDisplay.c index 2867fe5..fe29079 100644 --- a/Libs/std/Win95/stdDisplay.c +++ b/Libs/std/Win95/stdDisplay.c @@ -1084,6 +1084,11 @@ HRESULT PASCAL stdDisplay_EnumVideoModesCallback(LPDDSURFACEDESC2 lpDDSurfaceDes return 1; } + // Added: Allow only true colors + if ( lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount < 24 ) { + return 1; + } + StdVideoMode* pMode = &stdDisplay_aVideoModes[stdDisplay_numVideoModes]; pMode->rasterInfo.width = lpDDSurfaceDesc->dwWidth; pMode->rasterInfo.height = lpDDSurfaceDesc->dwHeight; @@ -1150,13 +1155,13 @@ HRESULT PASCAL stdDisplay_EnumVideoModesCallback(LPDDSURFACEDESC2 lpDDSurfaceDes return 1; } - if ( pMode->rasterInfo.width <= 640 && pMode->rasterInfo.height <= 480 && pMode->rasterInfo.colorInfo.bpp == 16 ) + if ( pMode->rasterInfo.width <= 640 && pMode->rasterInfo.height <= 480 && pMode->rasterInfo.colorInfo.bpp == 32 ) { if ( totalTexMem < 0x200000 ) { - STDLOG_STATUS("Not enough video memory, allowing <= 640x480x16 anyway...\n"); + STDLOG_STATUS("Not enough video memory, allowing <= 640x480x32 anyway...\n"); } else { - STDLOG_STATUS("Allowing <= 640x480x16 with 2MB texture mem.\n"); + STDLOG_STATUS("Allowing <= 640x480x32 with 2MB texture mem.\n"); } ++stdDisplay_numVideoModes; From 1263b8f854a3df716f117bc19c158f90a4cedf94 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Mon, 23 Dec 2024 23:52:14 +0100 Subject: [PATCH 33/35] Update comment --- Libs/sith/World/sithModel.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libs/sith/World/sithModel.c b/Libs/sith/World/sithModel.c index d948c4e..5117443 100644 --- a/Libs/sith/World/sithModel.c +++ b/Libs/sith/World/sithModel.c @@ -45,8 +45,8 @@ void sithModel_InstallHooks(void) J3D_HOOKFUNC(sithModel_Load); J3D_HOOKFUNC(sithModel_GetModelMemUsage); J3D_HOOKFUNC(sithModel_AllocWorldModels); - //J3D_HOOKFUNC(sithModel_GetModelByIndex); - J3DHookFunction(sithModel_GetModelByIndex_ADDR, (void*)sithModel_GetModelByIndexWrap); + + J3DHookFunction(sithModel_GetModelByIndex_ADDR, (void*)sithModel_GetModelByIndexWrap); //J3D_HOOKFUNC(sithModel_GetModelByIndex); J3D_HOOKFUNC(sithModel_GetModelIndex); J3D_HOOKFUNC(sithModel_GetModel); J3D_HOOKFUNC(sithModel_GetMeshIndex); From 118ded0c95d07c95181a660ff217dbeefaf3a8f0 Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Mon, 23 Dec 2024 23:52:42 +0100 Subject: [PATCH 34/35] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e602b82..b4b45c2 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ rdroid: 100.00% (219/219) rdCache: 100.00% (14/14) rdFace: 100.00% (2/2) -sith: 31.74% (596/1878) +sith: 32.80% (616/1878) sithAI: 0.00% (0/27) sithAIAwareness: 0.00% (0/8) sithAIClass: 0.00% (0/12) @@ -197,7 +197,7 @@ sith: 31.74% (596/1878) sithSector: 100.00% (19/19) sithSoundClass: 0.00% (0/24) sithSprite: 0.00% (0/15) - sithSurface: 0.00% (0/20) + sithSurface: 100.00% (20/20) sithTemplate: 0.00% (0/15) sithThing: 95.38% (62/65) sithVoice: 100.00% (20/20) @@ -253,5 +253,5 @@ Jones3D: 67.46% (255/378) jonesInventory:100.00% (15/15) -Overall Progress: 47.41% | 1373 implemented out of 2896 +Overall Progress: 48.07% | 1392 implemented out of 2896 ``` \ No newline at end of file From d022422a3c962e18fdc340a03becd40e7501194a Mon Sep 17 00:00:00 2001 From: Crt Vavros <4377556+smlu@users.noreply.github.com> Date: Mon, 23 Dec 2024 23:52:47 +0100 Subject: [PATCH 35/35] Update CHANGELOG --- CHANGELOG | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 1b6ec3e..0491969 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,21 +1,43 @@ # v0.2 General: - Fixed various bugs - - Fixed floating point precision bug in `rdVector` & `rdMatrix34` functions (03b800ad) - - Made OpenJones3D binaries portable by adding absolute resource dir path to JonesFile VFS (0fca6ef2) + - Fixed floating point precision bug in `rdVector` & `rdMatrix34` functions (3b800ad) + - Made OpenJones3D binaries portable by adding absolute resource dir path to JonesFile VFS (fca6ef2) This change allows binaries to be placed in any arbitrary location, eliminating the requirement for a Resource folder. - - Fixed typo in framerate output text (047537aa) - - Fixed initializing and clearing resource array on `JonesFile_Close` (047537aa) - - Fixed the invocation of `jonesString_GetString("JONES_STR_LOADERROR")` on every call to `JonesMain_ProcessGamesaveState` when there is no load error or the game is not being restored (047537aa) - - Fixed error text when it was cached by `jonesString_GetString` in call to `JonesMain_LogErrorToFile` (0485d553) - - Changed the location of `JonesError.txt` to be always written in current working directory instead of installed path (0485d553) + - Fixed typo in framerate output text (47537aa) + - Fixed initializing and clearing resource array on `JonesFile_Close` (47537aa) + - Fixed the invocation of `jonesString_GetString("JONES_STR_LOADERROR")` on every call to `JonesMain_ProcessGamesaveState` when there is no load error or the game is not being restored (47537aa) + - Fixed error text when it was cached by `jonesString_GetString` in call to `JonesMain_LogErrorToFile` (485d553) + - Changed the location of `JonesError.txt` to be always written in current working directory instead of installed path (485d553) This change resolves a potential fatal error that could occur when writing the error log due to the registry system failing to startup. + - Changed warning log to output to console or file (dcd3015) + - Made console log output colorized (dcd3015) + - Added debugger log output (dcd3015) + - Removed material buffer size constraint when loading material section form NDY level file in `sithMaterial_LoadMaterialsText` (dcd3015) + - Fixed potential infinitive loop bug when .uni file is missing line break at the end of file (6b71793) + - Implemented `sithSurface` module (b448041) + +Display & Render: + - Fixed scaling of color component when converting pixel data row in `stdColor_ColorConvertOneRow` (9e6ba77) + - Changed default engine texture format to 32 bit from 16 bit (249a416) + - Removed support for 16 bpp textures (3152496) + - Increased `rdCache` vertex buffer size to 32768 * 64 (25ceaba) + - Increased `rdModel3` (.3DO) mesh constraints based on `rdCache` constraints (a008386) + - Increased max no. of sectors in camera view to 4096 (b448041) + - Increased max no. of thing with sectors to 8192 (b448041) Game play: - - Changed to play higher resolution intro movie (800x600) by default (0ffa2e9d) + - Changed to by default play the higher resolution intro movie (800x600) (ffa2e9d) + - Fixed retrieving model by index number when loading savegame (669904d) + This change adds temporary wrapper function `sithModel_GetModelByIndexWrap`. + - Added support for 24/32 bit textures to CND cnd file format (91d0a87) + - Changed how developer dialog discovers level files (30630d1) + Now dialog first looks for ndy folder in current working directory, and if found searches for level files in that folder. If ndy folder is not found the dialog checks for level files in `Install Path\Resource\ndy`. + - Fixed searching for ndy level file when corresponding cnd file is not found (5e64e5b) + When game progresses to the next level this enables using ndy file for original levels when the corresponding cnd file is not found HUD: - - Fixed drawing of horizontal wall line (08046835) + - Fixed drawing of horizontal wall line (8046835) # v0.1 General: