Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sdl/SDL.h>
- #include "framework.h"
- #include "video.h"
- #include <math.h>
- #include <stdio.h>
- //
- static sint32 pen_x, pen_y;
- //
- SDL_Surface *reference = NULL;
- //
- const bool cDrawCircles = false;
- const int cInterval = 3000;
- const int cIterations = 32;
- const int cSamples = 16;
- //
- int time = 0;
- //
- int iabs( int a )
- {
- if ( a < 0 ) return -a;
- return a;
- }
- //
- void loadFile( void )
- {
- //
- char myPath[64] = { '\0' };
- //
- printf( "computer sketch - By 8BitPimp\nimage must be 32bit per pixel bitmap format\n" );
- //
- while ( true )
- {
- //
- printf( "please enter file name:\n" );
- //
- gets( myPath );
- //
- reference = SDL_LoadBMP( myPath );
- if ( reference != NULL )
- break;
- else
- {
- printf( "unable to load file!\n" );
- continue;
- }
- }
- }
- //
- bool start_game( void )
- {
- //
- loadFile( );
- //
- time = SDL_GetTicks( );
- //
- srand( time );
- //
- pen_x = (sint32)( rand() % _WIDTH );
- pen_y = (sint32)( rand() % _HEIGHT );
- //
- return true;
- }
- //
- float rand_angle( void )
- {
- sint32 irand = ( rand() & (0x1000-1) ) - 0x800;
- float frand = ((float)irand) / (float)0x800;
- //
- const float pi = 3.14159265359f;
- //
- return frand * pi;
- }
- //
- float rand_length( float min, float max )
- {
- float dif = max - min;
- //
- sint32 irand = ( rand() & (0x1000-1) ) - 0x800;
- float frand = ((float)irand) / (float)0x800;
- //
- return min + frand * dif;
- }
- //
- float rand_gauss( void )
- {
- sint32 irand = ( rand() & (0x1000-1) ) - 0x800;
- float frand = ((float)irand) / (float)0x800;
- //
- return frand * fabs( frand );
- }
- //
- void randomisePen( void )
- {
- float w = (float)(_WIDTH ) * 0.5f;
- float h = (float)(_HEIGHT) * 0.5f;
- pen_x = rand_gauss( ) * w + w;
- pen_y = rand_gauss( ) * h + h;
- }
- //
- void keyhit_game( int sym )
- {
- if ( sym == SDLK_c ) cls( 0x252525 );
- if ( sym == SDLK_r ) randomisePen( );
- }
- //
- uint32 sample_colour( sint32 x, sint32 y )
- {
- //
- if ( mouse.bchange[0] == 1 )
- {
- SDL_GetMouseState( &pen_x, &pen_y );
- }
- //
- if ( reference == NULL )
- return 0;
- //
- float u = (float)x / (float)_WIDTH ;
- float v = (float)y / (float)_HEIGHT;
- //
- sint32 i = (sint32)( u * (float)reference->w );
- sint32 j = (sint32)( v * (float)reference->h );
- //
- if ( i < 0 ) i = 0; if ( i >= reference->w ) i = reference->w-1;
- if ( j < 0 ) j = 0; if ( j >= reference->w ) j = reference->h-1;
- //
- if ( reference->format->BitsPerPixel == 32 )
- {
- uint32 *p = (uint32*)reference->pixels;
- return p[ i + j * (reference->pitch / sizeof( uint32 )) ];
- }
- if ( reference->format->BitsPerPixel == 24 )
- {
- return 0;
- }
- return 0;
- }
- //
- bool tick_game( void )
- {
- //
- int dif = SDL_GetTicks( ) - time;
- if ( dif > cInterval )
- {
- time += dif;
- randomisePen( );
- }
- //
- for ( int i=0; i<cIterations; i++ )
- {
- //
- while ( true )
- {
- //
- float length = rand_length( 4.0f, 24.0f );
- //
- int col = sample_colour( pen_x, pen_y );
- setColour( col );
- //
- sint32 new_x = pen_x;
- sint32 new_y = pen_y;
- bool not_set = true;
- //
- for ( int j=0; j<cSamples; j++ )
- {
- //
- float angle = rand_angle( );
- float p_new_x = pen_x + (sint32)(length * sinf( angle ));
- float p_new_y = pen_y + (sint32)(length * cosf( angle ));
- //
- if ( p_new_x >= _WIDTH ) continue;
- if ( p_new_y >= _HEIGHT ) continue;
- if ( p_new_x <= 0 ) continue;
- if ( p_new_y <= 0 ) continue;
- //
- int p_new_col = (col & 0xFF) - (sample_colour( p_new_x, p_new_y ) & 0xFF);
- int new_col = (col & 0xFF) - (sample_colour( new_x, new_y ) & 0xFF);
- //
- if ( (iabs(p_new_col) < iabs(new_col)) || not_set )
- {
- new_x = p_new_x;
- new_y = p_new_y;
- not_set = false;
- }
- }
- //
- if ( cDrawCircles )
- {
- int midx = (new_x + pen_x) / 2;
- int midy = (new_y + pen_y) / 2;
- drawCircle( midx, midy, length / 2 );
- }
- else
- drawLine( pen_x, pen_y, new_x, new_y );
- //
- pen_x = new_x;
- pen_y = new_y;
- //
- break;
- }
- }
- //
- SDL_Delay( 2 );
- //
- return true;
- }
- //
- void finish_game( void )
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement