Created
May 19, 2015 21:22
-
-
Save anonymous/a9d4f78345de0f79ed7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#include <SDL.h> | |
#ifdef EMSCRIPTEN | |
#include <emscripten/emscripten.h> | |
#endif | |
#define GAME_SIZE 256 | |
SDL_Window *window; | |
SDL_Renderer *renderer; | |
SDL_Texture *game_view; | |
SDL_AudioDeviceID dev; | |
SDL_AudioSpec want, have; | |
int quit = 0; | |
/* Generate some audio. */ | |
#define FREQ 200 | |
unsigned int audio_position; | |
int audio_len; | |
float audio_frequency; | |
float audio_volume; | |
void audioCallback(void* userdata, Uint8* stream, int len) { | |
len /= 2; | |
int i; | |
Sint16* buf = (Sint16*)stream; | |
for(i = 0; i < len; i++) { | |
buf[i] = audio_volume * sin(2 * M_PI * audio_position * audio_frequency); | |
audio_position++; | |
} | |
audio_len -= len; | |
return; | |
} | |
void loop_game(){ | |
SDL_Event event; | |
while(SDL_PollEvent(&event)){ | |
switch(event.type){ | |
case SDL_QUIT: | |
quit = 1; | |
break; | |
case SDL_FINGERDOWN: | |
case SDL_MOUSEBUTTONDOWN: | |
SDL_Init(SDL_INIT_AUDIO); | |
SDL_zero(want); | |
want.freq = 44100; | |
want.format = AUDIO_S16; | |
want.channels = 1; | |
want.samples = 4096; | |
want.callback = audioCallback; | |
audio_len = have.freq * 5; | |
audio_position = 0; | |
audio_frequency = 1.0 * FREQ / have.freq; | |
audio_volume = 6000; | |
dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); | |
SDL_PauseAudioDevice(dev, 0); | |
break; | |
} | |
} | |
SDL_SetRenderTarget(renderer, game_view); | |
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); | |
SDL_RenderClear(renderer); | |
SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE/2); | |
SDL_RenderDrawLine(renderer, 0, 0, GAME_SIZE, GAME_SIZE); | |
SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE/2); | |
SDL_RenderDrawLine(renderer, GAME_SIZE, 0, 0, GAME_SIZE); | |
SDL_SetRenderTarget(renderer, NULL); | |
SDL_RenderClear(renderer); | |
SDL_RenderCopy(renderer, game_view, NULL, NULL); | |
SDL_RenderPresent(renderer); | |
} | |
int main(int argc, const char * argv[]) { | |
SDL_Init(SDL_INIT_VIDEO); | |
window = SDL_CreateWindow( | |
"iOS SDL2 Audio Test", | |
SDL_WINDOWPOS_UNDEFINED, | |
SDL_WINDOWPOS_UNDEFINED, | |
GAME_SIZE, | |
GAME_SIZE, | |
SDL_WINDOW_SHOWN); | |
renderer = SDL_CreateRenderer(window, | |
-1, | |
SDL_RENDERER_ACCELERATED | | |
SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC); | |
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); | |
game_view = SDL_CreateTexture(renderer, | |
SDL_PIXELFORMAT_RGBA8888, | |
SDL_TEXTUREACCESS_TARGET, | |
GAME_SIZE, | |
GAME_SIZE); | |
#ifdef EMSCRIPTEN | |
emscripten_set_main_loop((em_callback_func)loop_game, 0, 1); | |
#else | |
while(!quit){ | |
loop_game(); | |
} | |
#endif | |
SDL_DestroyTexture(game_view); | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment