bazz

SDl2 Fogframework SoftwareConnect, SurfaceRenderer

Mar 23rd, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. /* Working SDL2 and Fog-Framework
  2. Method 2: Software Renderers
  3. Also using Fog::Image with SDL2 Surface
  4.  
  5. It's Software so probably Slower ?? ?
  6. */
  7.  
  8. SDL_Window *win;
  9. SDL_Surface *RectSurface;
  10. SDL_Renderer *RectRenderer;
  11.  
  12.     win = SDL_CreateWindow("SDL2 & FOG", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
  13.    
  14.     FogSurface = SDL_GetWindowSurface(win);
  15.     RectSurface = SDL_CreateRGBSurface(0,width,height,32,0,0,0,0);
  16.     RectRenderer = SDL_CreateSoftwareRenderer(RectSurface);
  17.  
  18. void onRender()
  19. {
  20. // This is how we Draw Rects to the Renderer ( Setup to Draw to a Surface!! RectSurface!! )
  21.     SDL_SetRenderDrawColor(RectRenderer,255,0,0,255);
  22.     SDL_RenderClear(RectRenderer);
  23.    
  24.     SDL_SetRenderDrawColor(RectRenderer, 255,255,0,255);
  25.     SDL_RenderFillRect(RectRenderer, &r);
  26.     // next line appears to be unnecessary on the Software Renderer, but I use it anyway
  27.     SDL_RenderPresent(RectRenderer);
  28. // Lock surface pixels.
  29.     SDL_LockSurface(FogSurface);
  30.     SDL_LockSurface(RectSurface);   // The surface is directly connected to the renderer, its results intact
  31.     // Create Fog::Painter instance mapped to the SDL surface data.
  32.     Fog::Painter p;
  33.    
  34.     // Setup image buffer for painter.
  35.     Fog::ImageBits buf;
  36.    
  37.     buf.setData(Fog::SizeI(FogSurface->w, FogSurface->h), Fog::IMAGE_FORMAT_XRGB32, FogSurface->pitch, (reinterpret_cast<uint8_t*>(FogSurface->pixels)));
  38.  
  39.    
  40.     // Call our paint handler.
  41.     if (p.begin(buf) == Fog::ERR_OK) onPaint(p);
  42.    
  43.     // Never forget to call p.end(). Painting can be asynchronous and
  44.     // SDL_UnlockSurface() can invalidate the surface->pixels pointer.
  45.     p.end();
  46.    
  47.     SDL_UnlockSurface(RectSurface);
  48.     // Unlock surface pixels.
  49.     SDL_UnlockSurface(FogSurface);
  50.    
  51.    
  52.    
  53.    
  54.     // Flip buffer.
  55.     SDL_UpdateWindowSurface(win);
  56. }
  57.  
  58. void SdlApplication::onPaint(Fog::Painter& p)
  59. {
  60.     // Just use the Fog-Framework ;-)
  61.     Image image;
  62.     Imagebits mybuf;
  63.  
  64.     // Get RectSurface data
  65.     mybuf.setData(Fog::SizeI(RectSurface->w, RectSurface->h), Fog::IMAGE_FORMAT_XRGB32, RectSurface->pitch, (reinterpret_cast<uint8_t*>(RectSurface->pixels)));
  66.        
  67.     // adopt that data into a Fog::Image
  68.     image.adopt(mybuf);
  69.    
  70.    
  71.     p.setSource(Argb32(0xFFFFFFFF));
  72.     p.fillAll();
  73.     PointI pos(0,0);
  74.     p.blitImage(pos,image);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment