From 6e5accb909469238e8116d54517483cd2e78c8ed Mon Sep 17 00:00:00 2001 From: headshot2017 <> Date: Thu, 12 Jan 2023 13:44:50 -0400 Subject: [PATCH] turn audio.c and main.c into CPP files fixes crash when trying to init audio on linux --- Makefile | 15 +++++++++++---- test/{audio.c => audio.cpp} | 11 +++++++++-- test/{main.c => main.cpp} | 24 +++++++++++++----------- test/renderer.h | 8 ++++++++ 4 files changed, 41 insertions(+), 17 deletions(-) rename test/{audio.c => audio.cpp} (85%) rename test/{main.c => main.cpp} (90%) diff --git a/Makefile b/Makefile index ab861aa..010a61b 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,11 @@ default: lib ifdef LIBSM64_MUSL CC := musl-gcc + CXX := musl-g++ LDFLAGS := -lm -static -shared else CC := cc + CXX := c++ LDFLAGS := -lm -shared endif CFLAGS := -g -Wall -Wno-unused-function -fPIC -DSM64_LIB_EXPORT -DGBI_FLOATS -DVERSION_US -DNO_SEGMENTED_MEMORY @@ -26,8 +28,9 @@ C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c)) $(C_IMPORTED) O_FILES := $(foreach file,$(C_FILES),$(BUILD_DIR)/$(file:.c=.o)) DEP_FILES := $(O_FILES:.o=.d) -TEST_SRCS := test/main.c test/context.c test/level.c test/audio.c test/gl33core/gl33core_renderer.c test/gl20/gl20_renderer.c -TEST_OBJS := $(foreach file,$(TEST_SRCS),$(BUILD_DIR)/$(file:.c=.o)) +TEST_SRCS_C := test/context.c test/level.c test/gl33core/gl33core_renderer.c test/gl20/gl20_renderer.c +TEST_SRCS_CPP := test/main.cpp test/audio.cpp +TEST_OBJS := $(foreach file,$(TEST_SRCS_C),$(BUILD_DIR)/$(file:.c=.o)) $(foreach file,$(TEST_SRCS_CPP),$(BUILD_DIR)/$(file:.cpp=.o)) ifeq ($(OS),Windows_NT) LIB_FILE := $(DIST_DIR)/sm64.dll @@ -45,6 +48,10 @@ $(BUILD_DIR)/%.o: %.c $(IMPORTED) @$(CC) $(CFLAGS) -MM -MP -MT $@ -MF $(BUILD_DIR)/$*.d $< $(CC) -c $(CFLAGS) -I src/decomp/include -o $@ $< +$(BUILD_DIR)/%.o: %.cpp $(IMPORTED) + @$(CXX) $(CFLAGS) -MM -MP -MT $@ -MF $(BUILD_DIR)/$*.d $< + $(CXX) -c $(CFLAGS) -I src/decomp/include -o $@ $< + $(LIB_FILE): $(O_FILES) $(CC) $(LDFLAGS) -o $@ $^ @@ -55,7 +62,7 @@ $(LIB_H_FILE): src/libsm64.h test/level.c test/level.h: ./import-test-collision.py ./import-test-collision.py -test/main.c: test/level.h +test/main.cpp: test/level.h $(BUILD_DIR)/test/%.o: test/%.c @$(CC) $(CFLAGS) -MM -MP -MT $@ -MF $(BUILD_DIR)/test/$*.d $< @@ -65,7 +72,7 @@ $(TEST_FILE): $(LIB_FILE) $(TEST_OBJS) ifeq ($(OS),Windows_NT) $(CC) -o $@ $(TEST_OBJS) $(LIB_FILE) -lglew32 -lopengl32 -lSDL2 -lSDL2main -lm else - $(CC) -o $@ $(TEST_OBJS) $(LIB_FILE) -lGLEW -lGL -lSDL2 -lSDL2main -lm + $(CC) -o $@ $(TEST_OBJS) $(LIB_FILE) -lGLEW -lGL -lSDL2 -lSDL2main -lm -lpthread endif lib: $(LIB_FILE) $(LIB_H_FILE) diff --git a/test/audio.c b/test/audio.cpp similarity index 85% rename from test/audio.c rename to test/audio.cpp index db37946..fae4574 100644 --- a/test/audio.c +++ b/test/audio.cpp @@ -5,8 +5,10 @@ #include #include +extern "C" { #include "../src/libsm64.h" #include "context.h" +} static SDL_AudioDeviceID dev; @@ -24,7 +26,7 @@ void* audio_thread(void* keepAlive) // from https://github.com/ckosmic/libsm64/blob/audio/src/libsm64.c#L535-L555 // except keepAlive is a null pointer here, so don't use it - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL); long long currentTime = timeInMilliseconds(); @@ -50,6 +52,11 @@ void* audio_thread(void* keepAlive) void audio_init() { + if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { + fprintf(stderr, "SDL_InitSubSystem(SDL_INIT_AUDIO) failed: %s\n", SDL_GetError()); + return; + } + SDL_AudioSpec want, have; SDL_zero(want); want.freq = 32000; @@ -65,6 +72,6 @@ void audio_init() SDL_PauseAudioDevice(dev, 0); // it's best to run audio in a separate thread - pthread_create(&gSoundThread, NULL, audio_thread, NULL); + pthread_create(&gSoundThread, NULL, audio_thread, NULL); } diff --git a/test/main.c b/test/main.cpp similarity index 90% rename from test/main.c rename to test/main.cpp index 6a2c948..0b13dbd 100644 --- a/test/main.c +++ b/test/main.cpp @@ -9,15 +9,17 @@ #include #include +extern "C" { #include "../src/libsm64.h" - #define SDL_MAIN_HANDLED -#include "audio.h" #include "level.h" #include "context.h" #include "renderer.h" #include "gl33core/gl33core_renderer.h" #include "gl20/gl20_renderer.h" +} + +#include "audio.h" int WINDOW_WIDTH = 1280; int WINDOW_HEIGHT = 800; @@ -31,7 +33,7 @@ uint8_t *utils_read_file_alloc( const char *path, size_t *fileLength ) fseek( f, 0, SEEK_END ); size_t length = (size_t)ftell( f ); rewind( f ); - uint8_t *buffer = malloc( length + 1 ); + uint8_t *buffer = (uint8_t*)malloc( length + 1 ); fread( buffer, 1, length, f ); buffer[length] = 0; fclose( f ); @@ -68,7 +70,7 @@ int main( void ) return 1; } - uint8_t *texture = 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 ); @@ -122,10 +124,10 @@ int main( void ) float lastPos[3], currPos[3]; float lastGeoPos[9 * SM64_GEO_MAX_TRIANGLES], currGeoPos[9 * SM64_GEO_MAX_TRIANGLES]; - marioGeometry.position = malloc( sizeof(float) * 9 * SM64_GEO_MAX_TRIANGLES ); - marioGeometry.color = malloc( sizeof(float) * 9 * SM64_GEO_MAX_TRIANGLES ); - marioGeometry.normal = malloc( sizeof(float) * 9 * SM64_GEO_MAX_TRIANGLES ); - marioGeometry.uv = 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, dt = 0; @@ -200,9 +202,9 @@ int main( void ) 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, 0 ); - marioInputs.buttonB = SDL_GameControllerGetButton( controller, 2 ); - marioInputs.buttonZ = SDL_GameControllerGetButton( controller, 9 ); + marioInputs.buttonA = SDL_GameControllerGetButton( controller, (SDL_GameControllerButton)0 ); + marioInputs.buttonB = SDL_GameControllerGetButton( controller, (SDL_GameControllerButton)2 ); + marioInputs.buttonZ = SDL_GameControllerGetButton( controller, (SDL_GameControllerButton)9 ); } cameraRot += x0_axis * dt * 2; diff --git a/test/renderer.h b/test/renderer.h index f6f23fb..00f24e3 100644 --- a/test/renderer.h +++ b/test/renderer.h @@ -5,8 +5,16 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #include "../src/libsm64.h" +#ifdef __cplusplus +} +#endif + #include "context.h" #include "cglm.h"