Reformat to something more readable for me :3
Build libsm64 / Build libsm64 for linux (push) Successful in 30s
Build libsm64 / Build libsm64 for web (push) Successful in 30s
Build libsm64 / Build libsm64 for windows (push) Successful in 23s

This commit is contained in:
2026-05-24 11:31:35 -05:00
parent 4328104536
commit d3ea4e5e99
154 changed files with 30751 additions and 21008 deletions
+57 -55
View File
@@ -11,41 +11,42 @@
#include "../src/libsm64.h"
extern "C" {
#define SDL_MAIN_HANDLED
#include "level.h"
#include "context.h"
#include "renderer.h"
#include "gl33core/gl33core_renderer.h"
#include "gl20/gl20_renderer.h"
extern "C"
{
#define SDL_MAIN_HANDLED
#include "level.h"
#include "context.h"
#include "renderer.h"
#include "gl33core/gl33core_renderer.h"
#include "gl20/gl20_renderer.h"
}
#include "audio.h"
uint8_t *utils_read_file_alloc( const char *path, size_t *fileLength )
uint8_t *utils_read_file_alloc(const char *path, size_t *fileLength)
{
FILE *f = fopen( path, "rb" );
FILE *f = fopen(path, "rb");
if( !f ) return NULL;
if (!f) return nullptr;
fseek( f, 0, SEEK_END );
size_t length = (size_t)ftell( f );
rewind( f );
uint8_t *buffer = (uint8_t*)malloc( length + 1 );
fread( buffer, 1, length, f );
fseek(f, 0, SEEK_END);
size_t length = (size_t)ftell(f);
rewind(f);
uint8_t *buffer = (uint8_t *)malloc(length + 1);
fread(buffer, 1, length, f);
buffer[length] = 0;
fclose( f );
fclose(f);
if( fileLength ) *fileLength = length;
if (fileLength) *fileLength = length;
return buffer;
}
static float read_axis( int16_t val )
static float read_axis(int16_t val)
{
float result = (float)val / 32767.0f;
if( result < 0.2f && result > -0.2f )
if (result < 0.2f && result > -0.2f)
return 0.0f;
return result > 0.0f ? (result - 0.2f) / 0.8f : (result + 0.2f) / 0.8f;
@@ -67,50 +68,51 @@ static void DebugPrint(const char *message)
fflush(stdout);
}
int main( void )
int main(void)
{
size_t romSize;
uint8_t *rom = utils_read_file_alloc( "baserom.us.z64", &romSize );
uint8_t *rom = utils_read_file_alloc("baserom.us.z64", &romSize);
if( rom == NULL )
if (rom == nullptr)
{
printf("\nFailed to read ROM file \"baserom.us.z64\"\n\n");
return 1;
}
uint8_t *texture = (uint8_t*)malloc( 4 * SM64_TEXTURE_WIDTH * SM64_TEXTURE_HEIGHT );
uint8_t *texture = (uint8_t *)malloc(4 * SM64_TEXTURE_WIDTH * SM64_TEXTURE_HEIGHT);
sm64_global_terminate();
sm64_global_init( rom, texture );
sm64_global_init(rom, texture);
sm64_audio_init(rom);
// sm64_register_debug_print_function(DebugPrint);
sm64_static_surfaces_load( surfaces, surfaces_count );
int32_t marioId = sm64_mario_create( 0, 1000, 0 );
sm64_static_surfaces_load(surfaces, surfaces_count);
int32_t marioId = sm64_mario_create(0, 1000, 0);
printf("MarioID: %i\n", marioId);
free( rom );
free(rom);
RenderState renderState;
renderState.mario.index = NULL;
vec3 cameraPos = { 0, 0, 0 };
renderState.mario.index = nullptr;
vec3 cameraPos = {0, 0, 0};
float cameraRot = 0.0f;
struct Renderer *renderer;
int major, minor;
#ifdef GL33_CORE
#ifdef GL33_CORE
major = 3; minor = 3;
renderer = &gl33core_renderer;
#else
major = 2; minor = 0;
#else
major = 2;
minor = 0;
renderer = &gl20_renderer;
#endif
#endif
context_init( "libsm64", 1280, 720, major, minor );
renderer->init( &renderState, texture );
context_init("libsm64", 1280, 720, major, minor);
renderer->init(&renderState, texture);
struct SM64MarioInputs marioInputs;
struct SM64MarioState marioState;
@@ -120,10 +122,10 @@ int main( void )
float lastPos[3], currPos[3];
float lastGeoPos[9 * SM64_GEO_MAX_TRIANGLES], currGeoPos[9 * SM64_GEO_MAX_TRIANGLES];
marioGeometry.position = (float*)malloc( sizeof(float) * 9 * SM64_GEO_MAX_TRIANGLES );
marioGeometry.color = (float*)malloc( sizeof(float) * 9 * SM64_GEO_MAX_TRIANGLES );
marioGeometry.normal = (float*)malloc( sizeof(float) * 9 * SM64_GEO_MAX_TRIANGLES );
marioGeometry.uv = (float*)malloc( sizeof(float) * 6 * SM64_GEO_MAX_TRIANGLES );
marioGeometry.position = (float *)malloc(sizeof(float) * 9 * SM64_GEO_MAX_TRIANGLES);
marioGeometry.color = (float *)malloc(sizeof(float) * 9 * SM64_GEO_MAX_TRIANGLES);
marioGeometry.normal = (float *)malloc(sizeof(float) * 9 * SM64_GEO_MAX_TRIANGLES);
marioGeometry.uv = (float *)malloc(sizeof(float) * 6 * SM64_GEO_MAX_TRIANGLES);
marioGeometry.numTrianglesUsed = 0;
float tick = 0;
@@ -144,7 +146,7 @@ int main( void )
if (!controller) // keyboard
{
const Uint8* state = SDL_GetKeyboardState(NULL);
const Uint8 *state = SDL_GetKeyboardState(nullptr);
float dir;
float spd = 0;
@@ -199,43 +201,43 @@ int main( void )
}
else
{
x_axis = read_axis( SDL_GameControllerGetAxis( controller, SDL_CONTROLLER_AXIS_LEFTX ));
y_axis = read_axis( SDL_GameControllerGetAxis( controller, SDL_CONTROLLER_AXIS_LEFTY ));
x0_axis = read_axis( SDL_GameControllerGetAxis( controller, SDL_CONTROLLER_AXIS_RIGHTX ));
x_axis = read_axis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX));
y_axis = read_axis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY));
x0_axis = read_axis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX));
marioInputs.buttonA = SDL_GameControllerGetButton( controller, SDL_CONTROLLER_BUTTON_A );
marioInputs.buttonB = SDL_GameControllerGetButton( controller, SDL_CONTROLLER_BUTTON_X );
marioInputs.buttonZ = SDL_GameControllerGetButton( controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER );
marioInputs.buttonA = SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_A);
marioInputs.buttonB = SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_X);
marioInputs.buttonZ = SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
}
cameraRot += x0_axis * dt * 2;
cameraPos[0] = marioState.position[0] + 1000.0f * cosf( cameraRot );
cameraPos[0] = marioState.position[0] + 1000.0f * cosf(cameraRot);
cameraPos[1] = marioState.position[1] + 200.0f;
cameraPos[2] = marioState.position[2] + 1000.0f * sinf( cameraRot );
cameraPos[2] = marioState.position[2] + 1000.0f * sinf(cameraRot);
marioInputs.camLookX = marioState.position[0] - cameraPos[0];
marioInputs.camLookZ = marioState.position[2] - cameraPos[2];
marioInputs.stickX = x_axis;
marioInputs.stickY = y_axis;
while (tick >= 1.f/30)
while (tick >= 1.f / 30)
{
memcpy(lastPos, currPos, sizeof(currPos));
memcpy(lastGeoPos, currGeoPos, sizeof(currGeoPos));
tick -= 1.f/30;
sm64_mario_tick( marioId, &marioInputs, &marioState, &marioGeometry );
tick -= 1.f / 30;
sm64_mario_tick(marioId, &marioInputs, &marioState, &marioGeometry);
memcpy(currPos, marioState.position, sizeof(currPos));
memcpy(currGeoPos, marioGeometry.position, sizeof(currGeoPos));
}
for (int i=0; i<3; i++) marioState.position[i] = lerp(lastPos[i], currPos[i], tick / (1.f/30));
for (int i=0; i<marioGeometry.numTrianglesUsed*9; i++) marioGeometry.position[i] = lerp(lastGeoPos[i], currGeoPos[i], tick / (1.f/30));
for (int i = 0; i < 3; i++) marioState.position[i] = lerp(lastPos[i], currPos[i], tick / (1.f / 30));
for (int i = 0; i < marioGeometry.numTrianglesUsed * 9; i++) marioGeometry.position[i] = lerp(lastGeoPos[i], currGeoPos[i], tick / (1.f / 30));
renderer->draw( &renderState, cameraPos, &marioState, &marioGeometry );
renderer->draw(&renderState, cameraPos, &marioState, &marioGeometry);
}
while( context_flip_frame_poll_events() );
while (context_flip_frame_poll_events());
sm64_stop_background_music(sm64_get_current_background_music());
sm64_global_terminate();