Advertisement
Guest User

Sketcher.cpp

a guest
May 10th, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include <sdl/SDL.h>
  2. #include "framework.h"
  3. #include "video.h"
  4. #include <math.h>
  5. #include <stdio.h>
  6.  
  7. //
  8. static sint32 pen_x, pen_y;
  9.  
  10. //
  11. SDL_Surface *reference = NULL;
  12.  
  13. //
  14. const bool cDrawCircles = false;
  15. const int  cInterval   = 3000;
  16. const int  cIterations = 32;
  17. const int  cSamples    = 16;
  18.  
  19. //
  20. int time = 0;
  21.  
  22. //
  23. int iabs( int a )
  24. {
  25.     if ( a < 0 ) return -a;
  26.                  return  a;
  27. }
  28.  
  29. //
  30. void loadFile( void )
  31. {
  32.     //
  33.     char myPath[64] = { '\0' };
  34.     //
  35.     printf( "computer sketch - By 8BitPimp\nimage must be 32bit per pixel bitmap format\n" );
  36.     //
  37.     while ( true )
  38.     {
  39.         //
  40.         printf( "please enter file name:\n" );
  41.         //
  42.         gets( myPath );
  43.         //
  44.         reference = SDL_LoadBMP( myPath );
  45.         if ( reference != NULL )
  46.             break;
  47.         else
  48.         {
  49.             printf( "unable to load file!\n" );
  50.             continue;
  51.         }
  52.     }
  53. }
  54.  
  55. //
  56. bool start_game( void )
  57. {
  58.     //
  59.     loadFile( );
  60.     //
  61.     time = SDL_GetTicks( );
  62.     //
  63.     srand( time );
  64.     //
  65.     pen_x = (sint32)( rand() % _WIDTH  );
  66.     pen_y = (sint32)( rand() % _HEIGHT );
  67.     //
  68.     return true;
  69. }
  70.  
  71. //
  72. float rand_angle( void )
  73. {
  74.     sint32 irand = ( rand() & (0x1000-1) ) - 0x800;
  75.     float  frand = ((float)irand) / (float)0x800;
  76.     //
  77.     const float pi = 3.14159265359f;
  78.     //
  79.     return frand * pi;
  80. }
  81.  
  82. //
  83. float rand_length( float min, float max )
  84. {
  85.     float dif = max - min;
  86.     //
  87.     sint32 irand = ( rand() & (0x1000-1) ) - 0x800;
  88.     float  frand = ((float)irand) / (float)0x800;
  89.     //
  90.     return min + frand * dif;
  91. }
  92.  
  93. //
  94. float rand_gauss( void )
  95. {
  96.     sint32 irand = ( rand() & (0x1000-1) ) - 0x800;
  97.     float  frand = ((float)irand) / (float)0x800;
  98.     //
  99.     return frand * fabs( frand );
  100. }
  101.  
  102. //
  103. void randomisePen( void )
  104. {
  105.     float w = (float)(_WIDTH ) * 0.5f;
  106.     float h = (float)(_HEIGHT) * 0.5f;
  107.     pen_x = rand_gauss( ) * w + w;
  108.     pen_y = rand_gauss( ) * h + h;
  109. }
  110.  
  111. //
  112. void keyhit_game( int sym )
  113. {
  114.     if ( sym == SDLK_c )    cls( 0x252525 );
  115.     if ( sym == SDLK_r )    randomisePen( );
  116. }
  117.  
  118. //
  119. uint32 sample_colour( sint32 x, sint32 y )
  120. {
  121.     //
  122.     if ( mouse.bchange[0] == 1 )
  123.     {
  124.         SDL_GetMouseState( &pen_x, &pen_y );
  125.     }
  126.     //
  127.     if ( reference == NULL )
  128.         return 0;
  129.     //
  130.     float  u  = (float)x / (float)_WIDTH ;
  131.     float  v  = (float)y / (float)_HEIGHT;
  132.     //
  133.     sint32 i  = (sint32)( u * (float)reference->w );
  134.     sint32 j  = (sint32)( v * (float)reference->h );
  135.     //
  136.     if ( i < 0 ) i = 0; if ( i >= reference->w ) i = reference->w-1;
  137.     if ( j < 0 ) j = 0; if ( j >= reference->w ) j = reference->h-1;
  138.     //
  139.     if ( reference->format->BitsPerPixel == 32 )
  140.     {
  141.         uint32 *p = (uint32*)reference->pixels;
  142.         return p[ i + j * (reference->pitch / sizeof( uint32 )) ];
  143.     }
  144.     if ( reference->format->BitsPerPixel == 24 )
  145.     {
  146.         return 0;
  147.     }
  148.     return 0;
  149. }
  150.  
  151. //
  152. bool tick_game( void )
  153. {
  154.     //
  155.     int dif = SDL_GetTicks( ) - time;
  156.     if ( dif > cInterval )
  157.     {
  158.         time += dif;
  159.         randomisePen( );
  160.     }
  161.     //
  162.     for ( int i=0; i<cIterations; i++ )
  163.     {
  164.         //
  165.         while ( true )
  166.         {
  167.             //
  168.             float length = rand_length( 4.0f, 24.0f );
  169.             //
  170.             int col = sample_colour( pen_x, pen_y );
  171.             setColour( col );
  172.             //
  173.             sint32 new_x = pen_x;
  174.             sint32 new_y = pen_y;
  175.             bool not_set = true;
  176.             //
  177.             for ( int j=0; j<cSamples; j++ )
  178.             {
  179.                 //
  180.                 float angle   = rand_angle( );
  181.                 float p_new_x = pen_x + (sint32)(length * sinf( angle ));
  182.                 float p_new_y = pen_y + (sint32)(length * cosf( angle ));
  183.                 //
  184.                 if ( p_new_x >= _WIDTH  ) continue;
  185.                 if ( p_new_y >= _HEIGHT ) continue;
  186.                 if ( p_new_x <= 0       ) continue;
  187.                 if ( p_new_y <= 0       ) continue;
  188.                 //
  189.                 int p_new_col = (col & 0xFF) - (sample_colour( p_new_x, p_new_y ) & 0xFF);
  190.                 int   new_col = (col & 0xFF) - (sample_colour(   new_x,   new_y ) & 0xFF);
  191.                 //
  192.                 if ( (iabs(p_new_col) < iabs(new_col)) || not_set )
  193.                 {
  194.                     new_x = p_new_x;
  195.                     new_y = p_new_y;
  196.                     not_set = false;
  197.                 }
  198.             }
  199.             //
  200.             if ( cDrawCircles )
  201.             {
  202.                 int midx = (new_x + pen_x) / 2;
  203.                 int midy = (new_y + pen_y) / 2;
  204.                 drawCircle( midx, midy, length / 2 );
  205.             }
  206.             else
  207.                 drawLine( pen_x, pen_y, new_x, new_y );
  208.             //
  209.             pen_x = new_x;
  210.             pen_y = new_y;
  211.             //
  212.             break;
  213.         }
  214.     }
  215.     //
  216.     SDL_Delay( 2 );
  217.     //
  218.     return true;
  219. }
  220.  
  221. //
  222. void finish_game( void )
  223. {
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement