Advertisement
Guest User

sdltest.c

a guest
Dec 4th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <stdio.h>
  3.  
  4.  
  5.  
  6. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  7. #define rmask  0xff000000
  8. #define gmask  0x00ff0000
  9. #define bmask  0x0000ff00
  10. #define amask  0x000000ff
  11.  
  12. #define rshift 24
  13. #define gshift 16
  14. #define bshift 8
  15. #define ashift 0
  16.  
  17. #else
  18. #define rmask  0x000000ff
  19. #define gmask  0x0000ff00
  20. #define bmask  0x00ff0000
  21. #define amask  0xff000000
  22.  
  23. #define rshift 0
  24. #define gshift 8
  25. #define bshift 16
  26. #define ashift 24
  27.  
  28. #endif
  29.  
  30. void PutPixel32( SDL_Surface *surface, int x, int y, Uint32 pixel )
  31. {
  32.     //Convert the pixels to 32 bit
  33.     Uint32 *pixels = (Uint32 *)surface->pixels;
  34.     //Set the pixel
  35.     pixels[ ( y * surface->w ) + x ] = pixel;
  36. }
  37.  
  38. int main(int argc, char ** argv)
  39. {
  40.     SDL_Window * window=NULL;
  41.     SDL_Renderer *ren;
  42.     SDL_Surface * Smain,*s1,*s2;
  43.     SDL_Texture * Tmain,*t1,*t2;
  44.     int x,y;
  45.     SDL_Rect r;
  46.  
  47.     //Initialize SDL
  48.     if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  49.     {
  50.         return -1;
  51.     }
  52.     //create windows and renderer
  53.     window = SDL_CreateWindow( "test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN );
  54.     ren=SDL_CreateRenderer(window,-1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  55.  
  56.     //create the main surfaces
  57.     Smain= SDL_CreateRGBSurface(0,500,500,32,rmask,gmask,bmask,amask);
  58.     s1= SDL_CreateRGBSurface(0,500,500,32,rmask,gmask,bmask,amask);
  59.     s2= SDL_CreateRGBSurface(0,500,500,32,rmask,gmask,bmask,amask);
  60.  
  61.     //white background
  62.     SDL_FillRect(Smain,NULL,0xffffffff);
  63.     Tmain=SDL_CreateTextureFromSurface(ren,Smain);
  64.  
  65.     //create surface 1 with varied opacity with red color
  66.     for(x=0;x<256;x++)
  67.     {
  68.         for(y=0;y<500;y++)
  69.         {
  70.             PutPixel32(s1,x,y,rmask| (x<<ashift));
  71.         }
  72.     }
  73.  
  74.     //fill the rest of red color,  opacity 100%
  75.     r.x=256;
  76.     r.y=0;
  77.     r.w=500-r.x;
  78.     r.h=500;
  79.     SDL_FillRect(s1,&r,rmask|amask);
  80.  
  81.     //blit surface when it should have the bug
  82.     SDL_BlitSurface(s1,NULL,s2,NULL);
  83.  
  84.  
  85.     //create the rest of texture
  86.     t1=SDL_CreateTextureFromSurface(ren,s1);
  87.     t2=SDL_CreateTextureFromSurface(ren,s2);
  88.     while (1)
  89.     {
  90.         //render t1
  91.         SDL_RenderClear(ren);
  92.         SDL_RenderCopy(ren,Tmain,NULL,NULL);
  93.         SDL_RenderCopy(ren,t1,NULL,NULL);
  94.         SDL_RenderPresent(ren);
  95.         puts("t1");
  96.         SDL_Delay(500);//wait
  97.  
  98.  
  99.         //render t2
  100.         SDL_RenderClear(ren);
  101.         SDL_RenderCopy(ren,Tmain,NULL,NULL);
  102.         SDL_RenderCopy(ren,t2,NULL,NULL);
  103.         SDL_RenderPresent(ren);
  104.         puts("t2");
  105.         SDL_Delay(500);//wait
  106.  
  107.  
  108.     }
  109.  
  110.     SDL_DestroyWindow(window);
  111.     SDL_Quit();
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement