Advertisement
dbright

bitwise or + circles and squares fractal

Jul 17th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. // circles and squares fractals + bitwise or, video https://youtu.be/6HzbCWrIqSY
  2. // tgawrite: https://github.com/donbright/piliko/tgawrite
  3. // code by don bright http://github.com/donbright
  4. // Wildberger's Rational Trig ( https://www.youtube.com/watch?v=kRHKbJMm6xE )
  5. // Santiago Zubieta's art ( https://twitter.com/zubie7a ), http://imgur.com/a/epJxG
  6. #include <SDL.h>
  7. #include <stdio.h>
  8. #include "tgawrite.h"
  9.  
  10. #define pixelsize 4
  11. Uint32 pixels[256*256*pixelsize];
  12. struct pixl8_t {
  13. Uint8 blue,green,red,alpha;
  14. };
  15.  
  16. void quitf() {
  17. printf("%s\n",SDL_GetError() );
  18. exit(0);
  19. }
  20.  
  21.  
  22. static Uint32 frameno = 0;
  23. void dumptga( Uint32*pixels, Uint16 w, Uint16 h ) {
  24. char fname[20];
  25. sprintf(fname,"tmp%06d.tga",frameno++);
  26. FILE *f = fopen( fname, "wb" );
  27. tgawrite( pixels, w, h, f );
  28. fclose(f);
  29. printf("wrote %s\n",fname);
  30. }
  31.  
  32. int main( int argc, char* args[] )
  33. {
  34. if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) quitf();
  35. if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) quitf();
  36. SDL_Window *window = SDL_CreateWindow( "mpixel",
  37. SDL_WINDOWPOS_UNDEFINED,
  38. SDL_WINDOWPOS_UNDEFINED,
  39. 256,256,
  40. SDL_WINDOW_SHOWN );
  41. if (!window) quitf();
  42. SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
  43. if (!renderer) quitf();
  44. SDL_SetRenderDrawColor( renderer, 0, 0, 0, 255 );
  45. SDL_RenderClear( renderer );
  46. // http://gamedev.stackexchange.com/questions/87727/sdl-updatetexture-is-slow
  47. SDL_Texture* texture = SDL_CreateTexture(renderer,
  48. SDL_PIXELFORMAT_ARGB8888,
  49. SDL_TEXTUREACCESS_STREAMING,
  50. 256, 256);
  51. if( !texture ) quitf();
  52. bool quit = false;
  53. while (!quit)
  54. {
  55. SDL_Event event;
  56. while( SDL_PollEvent( &event ) )
  57. {
  58. if( event.type == SDL_QUIT ) quit = true;
  59. if( event.type == SDL_KEYDOWN ) {
  60. if ( event.key.keysym.sym == SDLK_ESCAPE ) quit = true;
  61. }
  62. }
  63. Uint8 tmp;
  64. for (Uint32 i=0;i<256;i++) {
  65. for (Uint32 j=0;j<256;j++) {
  66. struct pixl8_t *p;
  67. p = (struct pixl8_t *)&pixels[j*256+i];
  68. p->red += (i*i)|(j*j);
  69. //p->green = 2*i*j;
  70. p->blue = p->red; //= i*i+j*j;
  71. p->alpha = 255;
  72. //printf("%i\n",i);
  73. }
  74. }
  75. dumptga( pixels, 256, 256 );
  76. SDL_Delay(180);
  77. SDL_UpdateTexture( texture, NULL, pixels, 256 * pixelsize);
  78. SDL_RenderCopy( renderer, texture, NULL, NULL );
  79. SDL_RenderPresent( renderer );
  80. }
  81. SDL_DestroyTexture( texture );
  82. SDL_DestroyRenderer( renderer );
  83. SDL_DestroyWindow( window );
  84. SDL_Quit();
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement