Advertisement
Guest User

AGG && SDL_Lock_texture

a guest
May 17th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 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.     SDL_Texture* tex = SDL_CreateTexture
  67.     (
  68.         ren,
  69.         SDL_PIXELFORMAT_RGBA8888,
  70.         SDL_TEXTUREACCESS_STREAMING,
  71.         frame_width,
  72.         frame_height
  73.     );
  74.            
  75.     SDL_LockTexture
  76.     (
  77.         tex,
  78.         NULL,
  79.         &surf->pixels,
  80.         &surf->pitch
  81.     );
  82.                        
  83.     agg::rendering_buffer rbuf //rendering buffer object
  84.     (
  85.         (unsigned char*)surf->pixels,
  86.         frame_width,
  87.         frame_height,
  88.         frame_width * 4
  89.     );
  90.    
  91.     agg::pixfmt_rgba32 pixf(rbuf);
  92.     agg::renderer_base<agg::pixfmt_rgba32> rbase(pixf);
  93.         //clear surf->pixels
  94.     rbase.clear(agg::rgba8(255, 255, 0, 0));
  95.    
  96.     agg::scanline_u8 scanline;
  97.     agg::rasterizer_scanline_aa<> ras;
  98.    
  99.     agg::renderer_scanline_aa_solid
  100.     <
  101.         agg::renderer_base
  102.         <
  103.             agg::pixfmt_rgba32
  104.         >
  105.     > rscan(rbase);
  106.    
  107.     //create agg paths
  108.     agg::path_storage path;
  109.     path.move_to(10, 10);
  110.     path.line_to(frame_width-10, frame_height-10);
  111.     path.line_to(frame_width-10, 10);
  112.     path.line_to(10, frame_height-10);
  113.    
  114.     ras.add_path(path);
  115.     rscan.color(agg::rgba8(0, 255, 0, 255));
  116.    
  117.     //draw paths to surf->pixels
  118.     agg::render_scanlines(ras, scanline, rscan);
  119.    
  120.     SDL_UnlockSurface(surf);
  121.     SDL_UnlockTexture(tex);
  122.  
  123.     SDL_RenderCopy
  124.     (
  125.         ren,
  126.         tex,
  127.         NULL,
  128.         NULL
  129.     );
  130.     SDL_RenderPresent(ren);
  131. /* 
  132.     SDL_Texture* tex2 = SDL_CreateTextureFromSurface(ren, surf);
  133.     SDL_RenderCopy
  134.     (
  135.         ren,
  136.         tex2,
  137.         NULL,
  138.         NULL
  139.     );
  140.     SDL_RenderPresent(ren);
  141. */
  142.     //main event loop
  143.     //---------------
  144.     SDL_Event e;
  145.    
  146.     while(1) //loop until user quits
  147.     {
  148.         if(SDL_PollEvent(&e))
  149.         {
  150.             switch(e.type)
  151.             {
  152.                 case SDL_QUIT:
  153.                     return 0;
  154.                
  155.                 case SDL_KEYDOWN:
  156.                     if(e.key.keysym.sym == SDLK_ESCAPE)
  157.                         return(0);
  158.             }
  159.         }
  160.         SDL_Delay(1); //speedbump, share the processor
  161.     }
  162.    
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement