From 6f01e60b10054aa03ced3cff9080bc839c564b30 Mon Sep 17 00:00:00 2001 From: bx Date: Mon, 27 Dec 2021 05:29:16 +0000 Subject: [PATCH] it compiles on linux with gcc now --- .gitignore | 2 ++ ampler.c | 8 ++++---- audio.c | 12 ++++++------ compile-ampler.sh | 2 ++ compile-core.sh | 2 ++ core.c | 33 +++++++++++++++++++++++---------- draw.c | 2 +- udp.c | 35 +++++++++++++++++++++++++++++++++++ 8 files changed, 75 insertions(+), 21 deletions(-) create mode 100755 compile-ampler.sh create mode 100755 compile-core.sh diff --git a/.gitignore b/.gitignore index 5075450..f0fd694 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ *.dll reload-trigger *.wav +*.so +core diff --git a/ampler.c b/ampler.c index fff7875..3af3f7f 100644 --- a/ampler.c +++ b/ampler.c @@ -6,10 +6,10 @@ #include "draw.c" Ampler_state *init() { - Ampler_state *state = malloc(sizeof Ampler_state); - SDL_memset(state, 0, sizeof Ampler_state); + Ampler_state *state = malloc(sizeof (Ampler_state)); + SDL_memset(state, 0, sizeof (Ampler_state)); - state -> size = sizeof Ampler_state; + state -> size = sizeof (Ampler_state); SDL_AudioSpec want = { 0 }, have = { 0 }; want.freq = SAMPLE_RATE; @@ -42,7 +42,7 @@ Ampler_state *init() { int ampler_main(SDL_Window *w, SDL_Renderer *r, Ampler_state **u_data) { if *u_data == NULL do *u_data = init(); - u32 old_size = (*u_data) -> size, new_size = sizeof Ampler_state; + u32 old_size = (*u_data) -> size, new_size = sizeof(Ampler_state); if old_size < new_size do { printf("increasing size. %i to %i\n", old_size, new_size); Ampler_state *nu = malloc(new_size); diff --git a/audio.c b/audio.c index 8f56e88..d2d0055 100644 --- a/audio.c +++ b/audio.c @@ -62,7 +62,7 @@ void sound_src_frame(Sound_src *s, s8 *mix[]) { int audio_frame(Ampler_state *state) { int queued = SDL_GetQueuedAudioSize(state -> playdev); - queued /= sizeof s16; // queued is in bytes + queued /= sizeof (s16); // queued is in bytes if queued > FRAME_SAMPLES * CHANNELS * 2 do return 0; // else puts("queued audio."); @@ -72,8 +72,8 @@ int audio_frame(Ampler_state *state) { // TODO: We should use CHANNELS here // static s8 mix_l[FRAME_SAMPLES] = { 0 }, mix_r[FRAME_SAMPLES] = { 0 }; s8 *mix_l = state->frame_mix[0], *mix_r = state->frame_mix[1]; - SDL_memset(mix_l, 0, FRAME_SAMPLES / sizeof s8); - SDL_memset(mix_r, 0, FRAME_SAMPLES / sizeof s8); + SDL_memset(mix_l, 0, FRAME_SAMPLES / sizeof (s8)); + SDL_memset(mix_r, 0, FRAME_SAMPLES / sizeof (s8)); s8 *mix[] = { mix_l, mix_r }; foreach_ptr(Sound_src, snd, state -> sounds) @@ -85,7 +85,7 @@ int audio_frame(Ampler_state *state) { for int t = 0; t < CHANNELS; t += 1 do frame[i * CHANNELS + t] = ((s16) (mix[t][i])) * vol; - if SDL_QueueAudio(state -> playdev, frame, sizeof frame) do + if SDL_QueueAudio(state -> playdev, frame, sizeof (frame)) do puts(SDL_GetError()); return 1; // audio was qued @@ -115,7 +115,7 @@ void load_sample(Ampler_state *state, const char *file_name) { } const int chans = spec.channels; - const int length = (bytes / sizeof s16) / chans; + const int length = (bytes / sizeof (s16)) / chans; snd -> track_len = length; snd -> speed = 1.0f; @@ -124,7 +124,7 @@ void load_sample(Ampler_state *state, const char *file_name) { snd -> end = snd->track_len; snd -> state = SND_STOPPED; snd -> note_speed = 1.0f; // TODO: Look up a note ? - SDL_memset(snd->name, 0, sizeof snd->name); + SDL_memset(snd->name, 0, sizeof (snd->name)); for int i = 0; i < arraylen(snd->name) - 1; i += 1 do if file_name[i] == '\0' or file_name[i] == '.' do break; else snd->name[i] = file_name[i]; diff --git a/compile-ampler.sh b/compile-ampler.sh new file mode 100755 index 0000000..f6ee303 --- /dev/null +++ b/compile-ampler.sh @@ -0,0 +1,2 @@ +#!/bin/bash +gcc ampler.c udp.c -o ampler.so -shared -lSDL2 -g --std c99 && echo > reload-trigger diff --git a/compile-core.sh b/compile-core.sh new file mode 100755 index 0000000..da6260c --- /dev/null +++ b/compile-core.sh @@ -0,0 +1,2 @@ +#!/bin/bash +gcc core.c -o core -lSDL2 -g diff --git a/core.c b/core.c index 49d3c62..920283d 100644 --- a/core.c +++ b/core.c @@ -6,11 +6,30 @@ #define SDL_MAIN_HANDLED #include -#define SHARED_OBJECT "ampler.dll" +#ifdef __WIN32__ +# define SHARED_OBJECT "ampler.dll" +# define SHARED_OBJECT_ACTIVE SHARED_OBJECT "-active.dll" +#else +# define SHARED_OBJECT "./ampler.so" +# define SHARED_OBJECT_ACTIVE SHARED_OBJECT +#endif + #define SHARED_MAIN "ampler_main" - -#define SHARED_OBJECT_ACTIVE SHARED_OBJECT "-active.dll" +#ifdef __WIN32__ +int copy_shared_object() { + if (system("copy " SHARED_OBJECT " " SHARED_OBJECT_ACTIVE " /Y")) { + puts("failed copying to " SHARED_OBJECT_ACTIVE); + // puts(SDL_GetError()); + return 1; + } + return 0; +} +#else +int copy_shared_object() { + return 0; +} +#endif int main(int argc, char **argv) { puts("start."); @@ -25,16 +44,10 @@ int main(int argc, char **argv) { r = SDL_CreateRenderer(w, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); - //SDL_CreateWindowAndRenderer( - // 1080, 480, SDL_WINDOW_RESIZABLE, &w, &r); - void *u_data = NULL; while (1) { - if (system("copy " SHARED_OBJECT " " SHARED_OBJECT_ACTIVE " /Y")) { - puts("failed copying to " SHARED_OBJECT_ACTIVE); - // puts(SDL_GetError()); + if (copy_shared_object()) break; - } void *so = SDL_LoadObject(SHARED_OBJECT_ACTIVE); if (so == 0) { diff --git a/draw.c b/draw.c index 43532d2..47057ec 100644 --- a/draw.c +++ b/draw.c @@ -13,7 +13,7 @@ void draw_frame(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) { // TODO: DRAW WITH X SCALE FACTOR // TODO: DRAW SMOLER SDL_SetRenderDrawColor(r, 0xFF, 0x00, 0x4D, 255); - const space = (44100 * 8) / 512; + const int space = (44100 * 8) / 512; const int t = 0; // TODO: DRAW L / R OVERLAPPED IN DIFF COLORS ? const int wav_h = 128; if 0 do diff --git a/udp.c b/udp.c index 1e69a22..5b173a1 100644 --- a/udp.c +++ b/udp.c @@ -8,7 +8,14 @@ * store from ip + port in message ? */ +#ifdef __WIN32__ #include +#else +#include +#include +#include +#include +#endif #include "ampler.h" @@ -19,8 +26,13 @@ #undef while // G l o b a L E v i L // +#ifdef __WIN32__ static SOCKET udp_sock; +#else +static int udp_sock; +#endif +#ifdef __WIN32__ void udp_init(Ampler_state *state) { // https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup WSADATA wsa; @@ -53,12 +65,24 @@ void udp_init(Ampler_state *state) { if (bind_result == SOCKET_ERROR) printf("error: bind returned SOCKET_ERROR, WSAGetLastError() = %i\n" , WSAGetLastError()); } +#else +void udp_init(Ampler_state *state) { + // +} +#endif +#ifdef __WIN32__ void udp_quit(Ampler_state *state) { closesocket(udp_sock); WSACleanup(); } +#else +void udp_quit(Ampler_state *state) { + // close(udp_sock); +} +#endif +#ifdef __WIN32__ void recv_packet(Ampler_state *state) { char discard_buf[32] = { 0 }; // for dropped packets char *buf = discard_buf; @@ -98,7 +122,13 @@ void recv_packet(Ampler_state *state) { if (0) // FOR DEBUGGING printf("%s:%d\t%s\n", from_ip_str, from_port, buf); } +#else +void recv_packet(Ampler_state *state) { + // +} +#endif +#ifdef __WIN32__ void udp_frame(Ampler_state *state) { // https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-select fd_set recv_fd_set; @@ -133,6 +163,11 @@ void udp_frame(Ampler_state *state) { recv_packet(state); } } +#else +void udp_frame(Ampler_state *state) { + // +} +#endif