Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include Simple Direct-media Layer
- #include <SDL/SDL.h>
- #pragma comment( lib, "SDL.lib" )
- #include <math.h>
- // setup some basic data types
- typedef signed long long s64;
- typedef unsigned long long u64;
- typedef unsigned int u32;
- typedef signed int s32;
- typedef signed short s16;
- typedef unsigned char byte;
- typedef signed long long fp64;
- // ---- ----- ---- ---- ---- ---- ---- ---- FRAMEWORK SPECIFICS
- namespace framework
- {
- // the title name for this application
- const char *app_name = "Spinning Cube Demo";
- // size of the video buffer
- const int vidSize = 256;
- // pixel size
- const int vidScale = 2;
- // video buffer
- u32 *video = NULL;
- // number of pixels in the video buffer
- const int nPixels = vidSize * vidSize;
- // calculated real SDL video buffer size
- const int scrRes = vidSize * vidScale;
- // the real SDL video buffer
- SDL_Surface *screen = NULL;
- // rate onTick is called in FPS
- const int tickFPS = 45;
- // mouse position
- int mousex = 0;
- int mousey = 0;
- }; // namespace framework
- using namespace framework;
- // ---- ----- ---- ---- ---- ---- ---- ---- MATH HELPER FUNCTIONS
- namespace mathHelper
- {
- static u32 randSeed = 0xBABEFACE;
- static s64 absi( s64 a )
- {
- if ( a < 0 ) return -a;
- else return a;
- }
- static s64 maxi( s64 a, s64 b )
- {
- if ( a > b ) return a;
- else return b;
- }
- static s64 mini( s64 a, s64 b )
- {
- if ( a < b ) return a;
- else return b;
- }
- inline u32 randi( void )
- {
- randSeed *= 1103515245;
- randSeed += 12345;
- return (randSeed >> 15) & 0xFFFF;
- }
- inline u32 scale( u32 a, byte scale )
- {
- return (a * scale) >> 8;
- }
- }; // namespace mathHelper
- using namespace mathHelper;
- // ---- ----- ---- ---- ---- ---- ---- ---- FIXED POINT HELPER FUNCTIONS
- namespace fixedPointHelper
- {
- const fp64 fp_1 = 0x10000;
- fp64 fp_mul( fp64 a, fp64 b )
- {
- return (a * b) >> 16;
- }
- fp64 fp_div( fp64 a, fp64 b )
- {
- return (a << 16) / b;
- }
- fp64 fp_ceil( fp64 a )
- {
- return (a | 0xFFFF) + 1;
- }
- fp64 fp_floor( fp64 a )
- {
- return a & (~0xFFFF);
- }
- fp64 fp_fract( fp64 a )
- {
- return a & 0xFFFF;
- }
- fp64 fp_ifract( fp64 a )
- {
- return 0xFFFF - (a & 0xFFFF);
- }
- int fp_to_int( fp64 a )
- {
- return (int) (a >> 16);
- }
- fp64 int_to_fp( int a )
- {
- return (a << 16);
- }
- fp64 fp_rand( int max )
- {
- return ((randi() % max) << 16) | (randi() & 0xFFFF);
- }
- }; // namespace fixedPointHelper
- using namespace fixedPointHelper;
- // ---- ----- ---- ---- ---- ---- ---- ---- DRAWING HELPER FUNCTIONS
- namespace videoHelper
- {
- static u32 drawColour = 0xFFFFFF;
- inline void plot( int x, int y )
- {
- bool out = x < 0;
- out |= y < 0;
- out |= x >= vidSize;
- out |= y >= vidSize;
- if ( out ) return;
- video[ x + y * vidSize ] = drawColour;
- }
- inline u32 readPixel( int x, int y )
- {
- bool out = x < 0;
- out |= y < 0;
- out |= x >= vidSize;
- out |= y >= vidSize;
- if ( out ) return 0;
- return video[ x + y * vidSize ];
- }
- void circle( s32 x0, s32 y0, s32 radius )
- {
- s32 error = 1 - radius;
- s32 errorY = 1;
- s32 errorX = -2 * radius;
- s32 x = radius, y = 0;
- plot(x0, y0 + radius);
- plot(x0, y0 - radius);
- plot(x0 + radius, y0);
- plot(x0 - radius, y0);
- while(y < x)
- {
- if (error > 0)
- { x--; errorX += 2; error += errorX; }
- { y++; errorY += 2; error += errorY; }
- plot(x0 + x, y0 + y);
- plot(x0 - x, y0 + y);
- plot(x0 + x, y0 - y);
- plot(x0 - x, y0 - y);
- plot(x0 + y, y0 + x);
- plot(x0 - y, y0 + x);
- plot(x0 + y, y0 - x);
- plot(x0 - y, y0 - x);
- }
- }
- void line( s32 X0, s32 Y0, s32 X1, s32 Y1 )
- {
- s32 incrementVal = 0;
- s32 shortLen = Y1 - Y0;
- s32 longLen = X1 - X0;
- bool yLonger = false;
- s32 decInc = 0;
- s32 endVal = 0;
- s32 j = 0;
- if ( absi( shortLen ) > absi( longLen ) )
- {
- int swap = shortLen;
- shortLen = longLen;
- longLen = swap;
- yLonger = true;
- }
- endVal = longLen;
- if ( longLen < 0 )
- {
- incrementVal = -1;
- longLen = -longLen;
- }
- else
- incrementVal = 1;
- if ( longLen == 0 )
- decInc = 0;
- else
- decInc = (shortLen << 16) / longLen;
- if ( yLonger )
- {
- for ( s32 i=0; i!=endVal; i+=incrementVal )
- {
- plot( X0 + (j >> 16), Y0 + i );
- j += decInc;
- }
- }
- else
- {
- for ( s32 i=0; i!=endVal; i+=incrementVal )
- {
- plot( X0 + i, Y0 + (j >> 16) );
- j += decInc;
- }
- }
- }
- }; // namesapce videoHelper
- using namespace videoHelper;
- // ---- ----- ---- ---- ---- ---- ---- ---- DEMO CODE ( the good stuff )
- namespace demo
- {
- struct sPoint
- {
- float x, y, z;
- void rotateX( float a )
- {
- float c = cosf( a );
- float s = sinf( a );
- float ty = y * c - z * s;
- z = y * s + z * c;
- y = ty;
- }
- void rotateY( float a )
- {
- float c = cosf( a );
- float s = sinf( a );
- float tz = z * c - x * s;
- x = z * s + x * c;
- z = tz;
- }
- void rotateZ( float a )
- {
- float c = cosf( a );
- float s = sinf( a );
- float tx = x * c - y * s;
- y = x * s + y * c;
- x = tx;
- }
- void rotate( float x, float y, float z )
- {
- rotateX( x );
- rotateY( y );
- rotateZ( z );
- }
- void project( float w, float h, float fov, float dist )
- {
- float factor = fov / (dist + z);
- x = x * factor + (w * 0.5f);
- y = y * factor + (h * 0.5f);
- }
- };
- sPoint cube[8] =
- {
- {-1,-1, 1 }, { 1,-1, 1 }, {-1,-1,-1 }, { 1,-1,-1 },
- {-1, 1, 1 }, { 1, 1, 1 }, {-1, 1,-1 }, { 1, 1,-1 }
- };
- struct sTriangle
- {
- int i, j, k;
- };
- sTriangle tri[12] =
- {
- { 0, 1, 2 }, { 1, 2, 3 },
- { 4, 5, 6 }, { 5, 6, 7 },
- { 0, 1, 4 }, { 1, 4, 5 },
- { 0, 2, 4 }, { 2, 4, 6 },
- { 2, 3, 6 }, { 3, 6, 7 },
- { 1, 3, 5 }, { 3, 5, 7 },
- };
- float rx = 0.0f;
- float ry = 0.0f;
- float rz = 0.0f;
- void onInit( void )
- {
- rx = 0.0f;
- ry = 0.0f;
- rz = 0.0f;
- }
- void onKeyHit( SDLKey key )
- {
- switch ( key )
- {
- case ( SDLK_SPACE ):
- onInit( );
- }
- }
- void incWrap( float &x, float y )
- {
- const float _2PI = 6.28318530718f;
- if ( (x+=y) > _2PI ) x -= _2PI;
- }
- void onTick( void )
- {
- for ( int i=0; i<nPixels; i++ )
- video[ i ] = 0;
- // transformed cube
- sPoint tcube[8];
- for ( int i=0; i<8; i++ )
- {
- tcube[i] = cube[i];
- tcube[i].rotate( rx, ry, rz );
- tcube[i].project( vidSize, vidSize, 128.0f, 3.0f );
- }
- for ( int i=0; i<12; i++ )
- {
- sPoint &a = tcube[ tri[i].i ];
- sPoint &b = tcube[ tri[i].j ];
- sPoint &c = tcube[ tri[i].k ];
- line( a.x, a.y, b.x, b.y );
- line( a.x, a.y, c.x, c.y );
- line( b.x, b.y, c.x, c.y );
- }
- incWrap( rx, 0.01111f );
- incWrap( ry, 0.02123f );
- incWrap( rz, 0.01313f );
- }
- }; // namespace demo
- using namespace demo;
- // ---- ----- ---- ---- ---- ---- ---- ---- FRAMEWORK FUNCIONS
- namespace framework
- {
- static bool app_init( void )
- {
- if ( SDL_Init( SDL_INIT_VIDEO ) != 0 )
- return false;
- SDL_WM_SetCaption( app_name, NULL );
- screen = SDL_SetVideoMode( scrRes, scrRes, 32, 0 );
- if ( screen == NULL )
- return false;
- if ( vidScale == 1 ) video = (u32*)screen->pixels;
- else video = new u32[ nPixels ];
- return true;
- }
- static void blit_2x( void )
- {
- //
- const int stride = screen->w;
- u32 *dst = (u32*) screen->pixels;
- int a = 0;
- for ( int i=0; i<nPixels; i++ )
- {
- u32 colour = video[ i ];
- // x4 pixel write
- dst[0 ] = colour;
- dst[1 ] = colour;
- dst[ stride] = colour;
- dst[1+stride] = colour;
- dst += 2;
- if ( a++ >= vidSize )
- {
- a = 0;
- // add on ONE stride not two since one length
- // has already been walked
- dst += stride;
- }
- }
- }
- static void app_draw( void )
- {
- if ( vidScale <= 1 )
- return;
- if ( vidScale == 2 )
- {
- blit_2x( );
- return;
- }
- for ( int i=0; i<nPixels; i++ )
- {
- SDL_Rect rect =
- {
- (i % vidSize) * vidScale,
- (i / vidSize) * vidScale,
- vidScale,
- vidScale
- };
- SDL_FillRect( screen, &rect, video[ i ] );
- }
- }
- static bool app_tick( void )
- {
- static bool active = true;
- SDL_Event event;
- while ( SDL_PollEvent( &event ) )
- {
- switch ( event.type )
- {
- case ( SDL_QUIT ):
- active = false;
- break;
- case ( SDL_KEYUP ):
- {
- if ( event.key.keysym.sym == SDLK_ESCAPE )
- active = false;
- else
- onKeyHit( event.key.keysym.sym );
- }
- break;
- }
- }
- return active;
- }
- static void app_quit( void )
- {
- //
- if ( video != NULL )
- if ( video != screen->pixels )
- delete [] video;
- video = NULL;
- //
- if ( screen != NULL )
- SDL_FreeSurface( screen );
- //
- SDL_Quit( );
- }
- }; // namespace framework
- // windows entry point
- int __stdcall WinMain( int, int, int, int )
- {
- // start up SDL
- atexit( app_quit );
- if (! app_init( ) )
- return 1;
- // get keyboard access
- u32 keyCount = 0;
- byte *keys = SDL_GetKeyState( (int*) &keyCount );
- // seed the random
- randSeed ^= SDL_GetTicks( );
- // call the on initialize function
- onInit( );
- //
- int oldTicks = SDL_GetTicks( );
- // work out the tick threshold in milliseconds
- const int tickThresh = 1000 / tickFPS;
- //
- while ( app_tick( ) )
- {
- int diff = SDL_GetTicks() - oldTicks;
- if ( diff < 10 )
- SDL_Delay( 10 );
- if ( diff > 500 )
- {
- oldTicks += diff;
- continue;
- }
- while ( diff > tickThresh )
- {
- // grab the mouse state
- SDL_GetMouseState( &mousex, &mousey );
- // tick the application
- onTick( );
- app_draw( );
- SDL_Flip( screen );
- // progress the tick count
- oldTicks += tickThresh;
- diff -= tickThresh;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment