Advertisement
dbright

Untitled

Jul 9th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. // for https://flic.kr/p/JPkDSE math art
  2.  
  3. #include <SDL.h>
  4. #include <stdio.h>
  5.  
  6. #define pixelsize 4
  7. Uint32 pixels[256*256*pixelsize];
  8. struct pixl8_t {
  9. Uint8 blue,green,red,alpha;
  10. };
  11.  
  12. void quitf() {
  13. printf("%s\n",SDL_GetError() );
  14. exit(0);
  15. }
  16.  
  17. int main( int argc, char* args[] )
  18. {
  19. if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) quitf();
  20. if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) quitf();
  21. SDL_Window *window = SDL_CreateWindow( "mpixel",
  22. SDL_WINDOWPOS_UNDEFINED,
  23. SDL_WINDOWPOS_UNDEFINED,
  24. 256,256,
  25. SDL_WINDOW_SHOWN );
  26. if (!window) quitf();
  27. SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
  28. if (!renderer) quitf();
  29. SDL_SetRenderDrawColor( renderer, 0, 0, 0, 255 );
  30. SDL_RenderClear( renderer );
  31. // http://gamedev.stackexchange.com/questions/87727/sdl-updatetexture-is-slow
  32. SDL_Texture* texture = SDL_CreateTexture(renderer,
  33. SDL_PIXELFORMAT_ARGB8888,
  34. SDL_TEXTUREACCESS_STREAMING,
  35. 256, 256);
  36. if( !texture ) quitf();
  37. bool quit = false;
  38. while (!quit)
  39. {
  40. SDL_Event event;
  41. while( SDL_PollEvent( &event ) )
  42. {
  43. if( event.type == SDL_QUIT ) quit = true;
  44. if( event.type == SDL_KEYDOWN ) {
  45. if ( event.key.keysym.sym == SDLK_ESCAPE ) quit = true;
  46. }
  47. }
  48. for (Uint32 i=0;i<256;i++) {
  49. for (Uint32 j=0;j<256;j++) {
  50. struct pixl8_t *p;
  51. p = (struct pixl8_t *)&pixels[j*256+i];
  52. //p->alpha = i;
  53. p->red = i;
  54. p->green = j;
  55. p->blue = i*i+j*j;
  56. printf("%i\n",i);
  57. }
  58. }
  59. SDL_UpdateTexture( texture, NULL, pixels, 256 * pixelsize);
  60. SDL_RenderCopy( renderer, texture, NULL, NULL );
  61. SDL_RenderPresent( renderer );
  62. }
  63. SDL_DestroyTexture( texture );
  64. SDL_DestroyRenderer( renderer );
  65. SDL_DestroyWindow( window );
  66. SDL_Quit();
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement