Advertisement
Guest User

AGG && SDL_UpdateTexture

a guest
May 18th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 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. #include "agg_conv_stroke.h"
  9.  
  10. enum
  11. {
  12.     frame_width = 800,
  13.     frame_height = 600
  14. };
  15.  
  16. int main(int argc, char * argv[])
  17. {
  18.     //initialize SDL
  19.     //--------------
  20.     atexit(SDL_Quit);
  21.     SDL_Init(SDL_INIT_VIDEO);
  22.     SDL_Window* win = SDL_CreateWindow
  23.     (
  24.         "SDL&AGG",
  25.         SDL_WINDOWPOS_CENTERED,
  26.         SDL_WINDOWPOS_CENTERED,
  27.         frame_width,
  28.         frame_height,
  29.         0
  30.     );
  31.     SDL_Renderer* ren = SDL_CreateRenderer
  32.     (
  33.         win,
  34.         -1,
  35.         0
  36.     );
  37.                        
  38.     //initialize AGG with memory allocated manually
  39.     void *tex_pixels = new uint8_t[frame_width*frame_height*4];
  40.     int tex_pitch = frame_width*4;
  41.    
  42.     #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  43.     #define AGG_RGBA32_BYTE_ORDER agg::pixfmt_rgba32
  44.     #else
  45.     #define AGG_RGBA32_BYTE_ORDER agg::pixfmt_abgr32
  46.     #endif                     
  47.  
  48.     agg::rendering_buffer rbuf //rendering buffer object
  49.     (
  50.         (unsigned char*)tex_pixels,
  51.         frame_width,
  52.         frame_height,
  53.         tex_pitch
  54.     );
  55.    
  56.     AGG_RGBA32_BYTE_ORDER pixf(rbuf);
  57.     agg::renderer_base<AGG_RGBA32_BYTE_ORDER> rbase(pixf);
  58.         //clear tex_pixels
  59.     rbase.clear(agg::rgba8(0, 0, 255, 255));
  60.    
  61.     agg::scanline_u8 scanline;
  62.     agg::rasterizer_scanline_aa<> ras;
  63.    
  64.     agg::renderer_scanline_aa_solid
  65.     <
  66.         agg::renderer_base
  67.         <
  68.             AGG_RGBA32_BYTE_ORDER
  69.         >
  70.     > rscan(rbase);
  71.    
  72.     //create agg paths
  73.     agg::path_storage path;
  74.     path.move_to(10, 10);
  75.     path.line_to(frame_width-10, frame_height-10);
  76.     path.line_to(frame_width-10, 10);
  77.     path.line_to(10, frame_height-10);
  78.    
  79.     agg::conv_stroke<agg::path_storage> stroke(path);
  80.     stroke.width(8.2);
  81.     stroke.line_cap(agg::round_cap);
  82.     stroke.line_join(agg::round_join);
  83.     ras.add_path(stroke);
  84.     rscan.color(agg::rgba8(75, 150, 225, 255));
  85.    
  86.     //draw paths to tex_pixels
  87.     agg::render_scanlines(ras, scanline, rscan);
  88.  
  89.     //create more agg paths
  90.     path.remove_all();
  91.     path.line_to(frame_width-50, frame_height-50);
  92.     path.line_to(frame_width-50, 50);
  93.     path.line_to(50, frame_height-50);
  94.  
  95.     ras.add_path(stroke);
  96.    
  97.     rscan.color(agg::rgba8(200, 150, 50, 255));
  98.     agg::render_scanlines(ras, scanline, rscan);
  99.    
  100.     SDL_Texture* tex = SDL_CreateTexture
  101.     (
  102.         ren,
  103.         SDL_PIXELFORMAT_RGBA8888,
  104.         SDL_TEXTUREACCESS_STATIC,
  105.         frame_width,
  106.         frame_height
  107.     );
  108.    
  109.     SDL_UpdateTexture
  110.     (
  111.         tex,
  112.         NULL,
  113.         tex_pixels,
  114.         tex_pitch
  115.     );
  116.    
  117.     SDL_RenderCopy
  118.     (
  119.         ren,
  120.         tex,
  121.         NULL,
  122.         NULL
  123.     );
  124.     SDL_RenderPresent(ren);
  125.  
  126.     //main event loop
  127.     //---------------
  128.     SDL_Event e;
  129.     SDL_Point a, b;
  130.     while(1) //loop until user quits
  131.     {
  132.         if(SDL_PollEvent(&e))
  133.         {
  134.             switch(e.type)
  135.             {
  136.                 case SDL_QUIT:
  137.                     return 0;
  138.                
  139.                 case SDL_KEYDOWN:
  140.                     if(e.key.keysym.sym == SDLK_ESCAPE)
  141.                         return(0);
  142.                        
  143.                 case SDL_MOUSEBUTTONDOWN:
  144.                     if (e.button.button == SDL_BUTTON_LEFT)
  145.                     {
  146.                         SDL_GetMouseState(&a.x, &a.y);
  147.                         SDL_RenderDrawPoint(ren, a.x, a.y);
  148.                         path.move_to(a.x, a.y);
  149.                     }
  150.                     break;
  151.                    
  152.                 case SDL_MOUSEMOTION:
  153.                     if(e.motion.state & SDL_BUTTON_LMASK)
  154.                     {
  155.                         SDL_GetMouseState(&b.x, &b.y);
  156.                         SDL_RenderDrawLine
  157.                         (
  158.                             ren,
  159.                             a.x,
  160.                             a.y,
  161.                             b.x,
  162.                             b.y
  163.                         );
  164.                        
  165.                         path.line_to(b.x, b.y);
  166.                        
  167.                         a=b;
  168.                     }
  169.                     break;
  170.             }
  171.         }
  172.        
  173.         ras.add_path(stroke);
  174.         rscan.color(agg::rgba8(0, 150, 50, 255));
  175.         agg::render_scanlines(ras, scanline, rscan);
  176.         path.remove_all();
  177.        
  178.         SDL_UpdateTexture
  179.         (
  180.             tex,
  181.             NULL,
  182.             tex_pixels,
  183.             tex_pitch
  184.         );
  185.        
  186.         SDL_RenderCopy
  187.         (
  188.             ren,
  189.             tex,
  190.             NULL,
  191.             NULL
  192.         );
  193.  
  194.         SDL_RenderPresent(ren);
  195.         SDL_Delay(10); //speedbump
  196.     }
  197.    
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement