Advertisement
Guest User

AGG && SDL

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