Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Holds data the graphics thread needs
- // This is initialized in the main
- // thread then sent to the graphics thread
- // Cleanup is done by the main thread after
- // the graphics thread exits
- struct GraphicsState {
- SDL_Window* window;
- SDL_Renderer* renderer;
- SDL_Texture* texture;
- ImageSurface cairoSurface;
- ubyte* pixels;
- }
- // a simple ring buffer
- struct RingBuffer {
- ulong pos;
- short[] buffer;
- }
- void main()
- {
- // ...
- GraphicsState graphicsState;
- gfxInit(graphicsState);
- shared RingBuffer ringBuffer;
- ringBuffer.pos = 0;
- ringBuffer.buffer.length = audioConf.sample_f * audioConf.chunkSize;
- Mix_SetPostMix(&finished, cast(void*)&ringBuffer);
- Tid tid = spawn(&doGfx);
- shared GraphicsState* sharedGfx = cast(shared GraphicsState*)&graphicsState;
- send(tid, sharedGfx, cast(shared(RingBuffer*))&ringBuffer);
- // ...
- }
- extern(C) void postMixCallback(void* udata, ubyte *stream, int len)
- {
- shared RingBuffer* buffer = cast(shared RingBuffer*)udata;
- short *samples = cast(short*)stream;
- // naive copy
- for (int i = 0; i < len / short.sizeof; i += 2)
- {
- buffer.buffer[buffer.pos] = *(samples + i);
- auto nextPos = buffer.pos + 1;
- if (nextPos == buffer.buffer.length)
- {
- nextPos = 0;
- }
- buffer.pos = nextPos;
- }
- }
- void doGfx(AudioConf audioConf)
- {
- GraphicsState graphicsState;
- Mix_Chunk* beat;
- shared RingBuffer* ringBuffer;
- auto msg = receiveOnly!(shared(GraphicsState*), shared(RingBuffer*));
- graphicsState = *(cast(GraphicsState*)msg[0]);
- ringBuffer = msg[1];
- while (true) {
- // read from ring buffer
- // render
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement