base cart player builds with cmake

This commit is contained in:
Vadim Grigoruk 2018-05-17 18:02:52 +03:00
parent 15fcf91c74
commit 11bf2568f4
2 changed files with 36 additions and 7 deletions

View File

@ -131,6 +131,8 @@ target_include_directories(tic80lib PRIVATE 3rd-party/fennel)
add_dependencies(tic80lib lua lpeg wren giflib) add_dependencies(tic80lib lua lpeg wren giflib)
target_link_libraries(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) add_subdirectory(3rd-party/SDL2-2.0.7)
set(EXAMPLE_DIR examples) set(EXAMPLE_DIR examples)
@ -138,11 +140,15 @@ set(EXAMPLE_SRC
${EXAMPLE_DIR}/sdl-renderer.c ${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 3rd-party/SDL2-2.0.7/include)
target_include_directories(example PRIVATE include) target_include_directories(example PRIVATE include)
target_include_directories(example PRIVATE src) target_include_directories(example PRIVATE src)
add_dependencies(example tic80lib SDL2 SDL2main) add_dependencies(example tic80lib SDL2-static SDL2main)
target_link_libraries(example tic80lib SDL2 SDL2main) target_link_libraries(example tic80lib SDL2-static SDL2main)

View File

@ -24,6 +24,9 @@
#include <SDL.h> #include <SDL.h>
#include <tic80.h> #include <tic80.h>
// TODO: take from tic.h??
#define TIC_FRAMERATE 60
static struct static struct
{ {
bool quit; 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_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_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);// TODO: disable vsync
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, TIC80_FULLWIDTH, TIC80_FULLHEIGHT); SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, TIC80_FULLWIDTH, TIC80_FULLHEIGHT);
SDL_AudioDeviceID audioDevice = 0; SDL_AudioDeviceID audioDevice = 0;
SDL_AudioSpec audioSpec; SDL_AudioSpec audioSpec;
SDL_AudioCVT cvt;
bool audioStarted = false; 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); 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; tic80_input input;
@ -135,7 +147,18 @@ int main(int argc, char **argv)
SDL_PauseAudioDevice(audioDevice, 0); 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); SDL_RenderClear(renderer);
@ -143,7 +166,7 @@ int main(int argc, char **argv)
void* pixels = NULL; void* pixels = NULL;
int pitch = 0; int pitch = 0;
SDL_LockTexture(texture, NULL, &pixels, &pitch); 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_UnlockTexture(texture);
SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderCopy(renderer, texture, NULL, NULL);
} }