From d0e7bac91ee97f48e8e11a54c56c48d050a10c46 Mon Sep 17 00:00:00 2001 From: miguel Date: Fri, 22 Sep 2017 19:14:34 +0200 Subject: added minimal template for browser version and audio testing --- Makefile | 2 +- audiotest.cpp | 101 +++++++++++++++++++++++++++++++++++ main.cpp | 3 +- shell_minimal.html | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 audiotest.cpp create mode 100644 shell_minimal.html diff --git a/Makefile b/Makefile index 0354a3d..70672a9 100644 --- a/Makefile +++ b/Makefile @@ -14,4 +14,4 @@ clean: browser-game: emcc --std=c++11 -O2 World.cpp -o World.bc emcc --std=c++11 -O2 main.cpp -o main.bc - emcc -O2 World.bc main.bc -o out.html -s USE_SDL=2 --preload-file coin.bmp --preload-file earth01.bmp --preload-file fonts.bmp --preload-file gridder01.bmp --preload-file guy01.bmp --preload-file guy02.bmp --preload-file gridder02.bmp --preload-file gridder03.bmp + emcc -O2 World.bc main.bc -o out.html -s USE_SDL=2 --preload-file coin.bmp --preload-file earth01.bmp --preload-file fonts.bmp --preload-file gridder01.bmp --preload-file guy01.bmp --preload-file guy02.bmp --preload-file gridder02.bmp --preload-file gridder03.bmp -s TOTAL_MEMORY=536870912 --shell-file shell_minimal.html diff --git a/audiotest.cpp b/audiotest.cpp new file mode 100644 index 0000000..f6b5a92 --- /dev/null +++ b/audiotest.cpp @@ -0,0 +1,101 @@ +//emcc -o test.html main.cpp -O2 -s USE_SDL=2 + +#include + +#if defined(_MSC_VER) +#include "SDL.h" +#else +#include "SDL2/SDL.h" +#endif + +#include +#include + +#ifdef __EMSCRIPTEN__ +#include "emscripten.h" +#endif + + + +void audiomixer(void *userdata, Uint8 *stream, int len) +{ + int samples = len / 4; + short *buf = (short*)stream; + + int i; + for (i = 0; i < samples*2; i++) + { + buf[i] = i * 655; + } +} + +int sdlstatic_init(unsigned int aSamplerate, unsigned int aBuffer) +{ + SDL_AudioSpec as; + as.freq = aSamplerate; + as.format = AUDIO_S16; + as.channels = 2; + as.samples = aBuffer; + as.callback = audiomixer; + + SDL_AudioSpec as2; + if (SDL_OpenAudio(&as, &as2) < 0) + { + return -1; + } + + SDL_PauseAudio(0); + + return 0; +} + +void mainloop() +{ + // Poll for events, and handle the ones we care about. + SDL_Event event; + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_KEYUP: + // If escape is pressed, return (and thus, quit) + switch (event.key.keysym.sym) + { + case SDLK_ESCAPE: + { + exit(0); + } + break; + } + break; + case SDL_QUIT: + SDL_CloseAudio(); + exit(0); + } + } +} + +int main(int argc, char *argv[]) +{ + printf("SDL INIT OK\n"); + if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) + { + fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); + exit(1); + } + printf("SDL INIT OK\n"); + + atexit(SDL_Quit); + + sdlstatic_init(44100, 1024); + +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(mainloop, 60, 0); +#else + while (1) + { + mainloop(); + } +#endif + return 0; +} diff --git a/main.cpp b/main.cpp index 6978fdb..c2c9280 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #endif #include +#include #include "World.h" @@ -70,8 +71,8 @@ void sdl_play_sound() // SDL_LoadWAV("music.wav", &wavSpec, &wavBuffer, &wavLength); // SDL_LoadWAV("win.wav", &wavSpec, &wavBuffer, &wavLength); -#ifndef __EMSCRIPTEN__ int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength); +#ifndef __EMSCRIPTEN__ #endif //SDL_CloseAudioDevice(deviceId); diff --git a/shell_minimal.html b/shell_minimal.html new file mode 100644 index 0000000..33a28b5 --- /dev/null +++ b/shell_minimal.html @@ -0,0 +1,152 @@ + + + + + + Emscripten-Generated Code + + + +
+
emscripten
+
Downloading...
+
+ +
+
+ +
+
+
+ Resize canvas + Lock/hide mouse pointer +     + +
+ +
+ +
+ + {{{ SCRIPT }}} + + -- cgit v1.2.3