Advertisement
Guest User

Untitled

a guest
Nov 9th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.66 KB | None | 0 0
  1. // Holds data the graphics thread needs
  2. // This is initialized in the main
  3. // thread then sent to the graphics thread
  4. // Cleanup is done by the main thread after
  5. // the graphics thread exits
  6. struct GraphicsState {
  7.   SDL_Window* window;
  8.   SDL_Renderer* renderer;
  9.   SDL_Texture* texture;
  10.   ImageSurface cairoSurface;
  11.   ubyte* pixels;  
  12. }
  13.  
  14. // a simple ring buffer
  15. struct RingBuffer {
  16.   ulong pos;
  17.   short[] buffer;
  18. }
  19.  
  20.  
  21. void main()
  22. {
  23.   // ...
  24.   GraphicsState graphicsState;
  25.   gfxInit(graphicsState);
  26.  
  27.   shared RingBuffer ringBuffer;
  28.   ringBuffer.pos = 0;
  29.   ringBuffer.buffer.length = audioConf.sample_f * audioConf.chunkSize;
  30.  
  31.   Mix_SetPostMix(&finished, cast(void*)&ringBuffer);
  32.  
  33.   Tid tid = spawn(&doGfx);
  34.   shared GraphicsState* sharedGfx = cast(shared GraphicsState*)&graphicsState;
  35.   send(tid, sharedGfx, cast(shared(RingBuffer*))&ringBuffer);
  36.   // ...
  37. }
  38.  
  39. extern(C) void postMixCallback(void* udata, ubyte *stream, int len)
  40. {
  41.   shared RingBuffer* buffer = cast(shared RingBuffer*)udata;
  42.   short *samples = cast(short*)stream;
  43.  
  44.   // naive copy
  45.   for (int i = 0; i < len / short.sizeof; i += 2)
  46.   {
  47.     buffer.buffer[buffer.pos] = *(samples + i);
  48.     auto nextPos = buffer.pos + 1;
  49.     if (nextPos == buffer.buffer.length)
  50.     {
  51.       nextPos = 0;
  52.     }        
  53.     buffer.pos = nextPos;
  54.   }
  55. }
  56.  
  57. void doGfx(AudioConf audioConf)
  58. {
  59.   GraphicsState graphicsState;
  60.   Mix_Chunk* beat;
  61.   shared RingBuffer* ringBuffer;
  62.   auto msg = receiveOnly!(shared(GraphicsState*), shared(RingBuffer*));
  63.  
  64.   graphicsState = *(cast(GraphicsState*)msg[0]);
  65.   ringBuffer = msg[1];
  66.  
  67.   while (true) {
  68.     // read from ring buffer
  69.     // render
  70.   }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement