turn audio.c and main.c into CPP files

fixes crash when trying to init audio on linux
This commit is contained in:
headshot2017
2023-01-12 13:44:50 -04:00
parent ef13bae2fc
commit 6e5accb909
4 changed files with 41 additions and 17 deletions
+11 -4
View File
@@ -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)
+9 -2
View File
@@ -5,8 +5,10 @@
#include <unistd.h>
#include <sys/time.h>
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);
}
+13 -11
View File
@@ -9,15 +9,17 @@
#include <unistd.h>
#include <pthread.h>
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;
+8
View File
@@ -5,8 +5,16 @@
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "../src/libsm64.h"
#ifdef __cplusplus
}
#endif
#include "context.h"
#include "cglm.h"