From 11bf2568f472dd496776da639d64f2c1ee8cfcc6 Mon Sep 17 00:00:00 2001 From: Vadim Grigoruk Date: Thu, 17 May 2018 18:02:52 +0300 Subject: [PATCH] base cart player builds with cmake --- CMakeLists.txt | 12 +++++++++--- examples/sdl-renderer.c | 31 +++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 853e49a..58b5af1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -131,6 +131,8 @@ target_include_directories(tic80lib PRIVATE 3rd-party/fennel) add_dependencies(tic80lib lua lpeg wren giflib) target_link_libraries(tic80lib lua lpeg wren giflib) +set(SDL_STATIC ON) +set(HAVE_LIBC TRUE) add_subdirectory(3rd-party/SDL2-2.0.7) set(EXAMPLE_DIR examples) @@ -138,11 +140,15 @@ set(EXAMPLE_SRC ${EXAMPLE_DIR}/sdl-renderer.c ) -add_executable(example ${EXAMPLE_SRC}) +if(WIN32) + add_executable(example WIN32 ${EXAMPLE_SRC}) +else() + add_executable(example ${EXAMPLE_SRC}) +endif() target_include_directories(example PRIVATE 3rd-party/SDL2-2.0.7/include) target_include_directories(example PRIVATE include) target_include_directories(example PRIVATE src) -add_dependencies(example tic80lib SDL2 SDL2main) -target_link_libraries(example tic80lib SDL2 SDL2main) +add_dependencies(example tic80lib SDL2-static SDL2main) +target_link_libraries(example tic80lib SDL2-static SDL2main) diff --git a/examples/sdl-renderer.c b/examples/sdl-renderer.c index dc35667..c4ad533 100644 --- a/examples/sdl-renderer.c +++ b/examples/sdl-renderer.c @@ -24,6 +24,9 @@ #include #include +// TODO: take from tic.h?? +#define TIC_FRAMERATE 60 + static struct { bool quit; @@ -57,11 +60,12 @@ int main(int argc, char **argv) { SDL_Window* window = SDL_CreateWindow("TIC-80 SDL demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, TIC80_FULLWIDTH, TIC80_FULLHEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); - SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, TIC80_FULLWIDTH, TIC80_FULLHEIGHT); + SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);// TODO: disable vsync + SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, TIC80_FULLWIDTH, TIC80_FULLHEIGHT); SDL_AudioDeviceID audioDevice = 0; SDL_AudioSpec audioSpec; + SDL_AudioCVT cvt; bool audioStarted = false; { @@ -74,6 +78,14 @@ int main(int argc, char **argv) }; audioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &audioSpec, SDL_AUDIO_ALLOW_ANY_CHANGE); + + SDL_BuildAudioCVT(&cvt, want.format, want.channels, audioSpec.freq, audioSpec.format, audioSpec.channels, audioSpec.freq); + + if (cvt.needed) + { + cvt.len = audioSpec.freq * sizeof(s16) / TIC_FRAMERATE; + cvt.buf = SDL_malloc(cvt.len * cvt.len_mult); + } } tic80_input input; @@ -135,7 +147,18 @@ int main(int argc, char **argv) SDL_PauseAudioDevice(audioDevice, 0); } - SDL_QueueAudio(audioDevice, tic->sound.samples, tic->sound.count * sizeof(tic->sound.samples[0])); + { + SDL_PauseAudioDevice(audioDevice, 0); + s32 size = tic->sound.count * sizeof(tic->sound.samples[0]); + + if (cvt.needed) + { + SDL_memcpy(cvt.buf, tic->sound.samples, size); + SDL_ConvertAudio(&cvt); + SDL_QueueAudio(audioDevice, cvt.buf, cvt.len_cvt); + } + else SDL_QueueAudio(audioDevice, tic->sound.samples, size); + } SDL_RenderClear(renderer); @@ -143,7 +166,7 @@ int main(int argc, char **argv) void* pixels = NULL; int pitch = 0; SDL_LockTexture(texture, NULL, &pixels, &pitch); - SDL_memcpy(pixels, tic->screen, TIC80_FULLWIDTH * TIC80_FULLHEIGHT); + SDL_memcpy(pixels, tic->screen, pitch * TIC80_FULLHEIGHT); SDL_UnlockTexture(texture); SDL_RenderCopy(renderer, texture, NULL, NULL); }