Advertisement
Guest User

AGG && SDL_Lock_texture

a guest
May 17th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include "SDL.h"
  2. #include "agg_renderer_base.h"
  3. #include "agg_pixfmt_rgba.h"
  4. #include "agg_scanline_u.h"
  5. #include "agg_rasterizer_scanline_aa.h"
  6. #include "agg_renderer_scanline.h"
  7. #include "agg_path_storage.h"
  8.  
  9. enum
  10. {
  11.     frame_width = 800,
  12.     frame_height = 600
  13. };
  14.  
  15. int main(int argc, char * argv[])
  16. {
  17.     //initialize SDL
  18.     //--------------
  19.     atexit(SDL_Quit);
  20.     SDL_Init(SDL_INIT_VIDEO);
  21.     SDL_Window* win = SDL_CreateWindow
  22.     (
  23.         "SDL&AGG",
  24.         SDL_WINDOWPOS_CENTERED,
  25.         SDL_WINDOWPOS_CENTERED,
  26.         frame_width,
  27.         frame_height,
  28.         0
  29.     );
  30.     SDL_Renderer* ren = SDL_CreateRenderer
  31.     (
  32.         win,
  33.         -1,
  34.         0
  35.     );
  36.                        
  37.     //initialize AGG with memory allocated by SDL
  38.     Uint32 rmask, gmask, bmask, amask;
  39.  
  40.     #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  41.     rmask = 0xff000000;
  42.     gmask = 0x00ff0000;
  43.     bmask = 0x0000ff00;
  44.     amask = 0x000000ff;
  45.     #else
  46.     rmask = 0x000000ff;
  47.     gmask = 0x0000ff00;
  48.     bmask = 0x00ff0000;
  49.     amask = 0xff000000;
  50.     #endif
  51.  
  52.     SDL_Surface* surf = SDL_CreateRGBSurface
  53.     (
  54.         0,
  55.         frame_width,
  56.         frame_height,
  57.         32,
  58.         rmask,
  59.         gmask,
  60.         bmask,
  61.         amask
  62.     );
  63.    
  64.     SDL_LockSurface(surf);
  65.                    
  66.     agg::rendering_buffer rbuf //rendering buffer object
  67.     (
  68.         (unsigned char*)surf->pixels,
  69.         frame_width,
  70.         frame_height,
  71.         frame_width * 4
  72.     );
  73.    
  74.     agg::pixfmt_rgba32 pixf(rbuf);
  75.     agg::renderer_base<agg::pixfmt_rgba32> rbase(pixf);
  76.         //clear surf->pixels
  77.     rbase.clear(agg::rgba8(0, 0, 255, 255));
  78.    
  79.     agg::scanline_u8 scanline;
  80.     agg::rasterizer_scanline_aa<> ras;
  81.    
  82.     agg::renderer_scanline_aa_solid
  83.     <
  84.         agg::renderer_base
  85.         <
  86.             agg::pixfmt_rgba32
  87.         >
  88.     > rscan(rbase);
  89.    
  90.     //create agg paths
  91.     agg::path_storage path;
  92.     path.move_to(10, 10);
  93.     path.line_to(frame_width-10, frame_height-10);
  94.     path.line_to(frame_width-10, 10);
  95.     path.line_to(10, frame_height-10);
  96.    
  97.     ras.add_path(path);
  98.     rscan.color(agg::rgba8(0, 255, 0, 255));
  99.    
  100.     //draw paths to surf->pixels
  101.     agg::render_scanlines(ras, scanline, rscan);
  102.    
  103.     SDL_UnlockSurface(surf); //finished drawing
  104.    
  105.     //convert pixel format and display image
  106.    
  107.     SDL_PixelFormat* sdl_pixfmt = new SDL_PixelFormat;
  108.     sdl_pixfmt->format = SDL_GetWindowPixelFormat(win);
  109.        
  110.     SDL_Surface* conv_surf = SDL_ConvertSurface
  111.     (
  112.         surf,
  113.         sdl_pixfmt,
  114.         0
  115.     );
  116.    
  117.     SDL_Surface* surf_to_tex = SDL_CreateRGBSurface
  118.     (
  119.         0,
  120.         frame_width,
  121.         frame_height,
  122.         32,
  123.         rmask,
  124.         gmask,
  125.         bmask,
  126.         amask
  127.     );
  128.    
  129.     SDL_Texture* tex = SDL_CreateTexture
  130.     (
  131.         ren,
  132.         SDL_PIXELFORMAT_RGBA8888,
  133.         SDL_TEXTUREACCESS_STREAMING,
  134.         frame_width,
  135.         frame_height
  136.     );
  137.    
  138.     SDL_LockTexture
  139.     (
  140.         tex,
  141.         NULL,
  142.         &surf_to_tex->pixels,
  143.         &surf_to_tex->pitch
  144.     );
  145.    
  146.     SDL_BlitSurface
  147.     (
  148.         conv_surf,
  149.         NULL,
  150.         surf_to_tex,
  151.         NULL
  152.     );
  153.  
  154.    
  155.     SDL_UnlockTexture(tex);
  156.  
  157.     SDL_RenderCopy
  158.     (
  159.         ren,
  160.         tex,
  161.         NULL,
  162.         NULL
  163.     );
  164.     SDL_RenderPresent(ren);
  165. /* 
  166.     SDL_Texture* tex2 = SDL_CreateTextureFromSurface(ren, surf);
  167.     SDL_RenderCopy
  168.     (
  169.         ren,
  170.         tex2,
  171.         NULL,
  172.         NULL
  173.     );
  174.     SDL_RenderPresent(ren);
  175. */
  176.     //main event loop
  177.     //---------------
  178.     SDL_Event e;
  179.    
  180.     while(1) //loop until user quits
  181.     {
  182.         if(SDL_PollEvent(&e))
  183.         {
  184.             switch(e.type)
  185.             {
  186.                 case SDL_QUIT:
  187.                     return 0;
  188.                
  189.                 case SDL_KEYDOWN:
  190.                     if(e.key.keysym.sym == SDLK_ESCAPE)
  191.                         return(0);
  192.             }
  193.         }
  194.         SDL_Delay(1); //speedbump, share the processor
  195.     }
  196.    
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement