Advertisement
Guest User

Minecraft4K_JS_Port

a guest
Jul 30th, 2014
2,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.50 KB | None | 0 0
  1. // C++ port of Minecraft 4k JS (http://jsdo.it/notch/dB1E)
  2. // By The8BitPimp
  3. // See: the8bitpimp.wordpress.com
  4.  
  5. #define _SDL_main_h
  6. #include <SDL\SDL.h>
  7. #pragma comment( lib, "sdl.lib" )
  8. #include <math.h>
  9.  
  10. const int w = 320;
  11. const int h = 240;
  12.  
  13. SDL_Surface *screen = nullptr;
  14.  
  15. const float math_pi = 3.14159265359f;
  16.  
  17. static inline float math_sin( float x ) {
  18.     return sinf( x );
  19. }
  20.  
  21. static inline float math_cos( float x ) {
  22.     return cosf( x );
  23. }
  24.  
  25. // the texture map
  26. int texmap[ 16 * 16 * 16 * 3 ];
  27.  
  28. // the voxel map
  29. char map[ 64 * 64 * 64 ];
  30.  
  31. static inline int random( int max ) {
  32.     return (rand()^(rand()<<16)) % max;
  33. }
  34.  
  35. static inline void plot( int x, int y, int c ) {
  36.     int *p = (int*) screen->pixels;
  37.     p[ y * w + x ] = c;
  38. }
  39.  
  40. static void makeTextures( void ) {
  41.    
  42.     // each texture
  43.     for ( int j=0; j<16; j++ ) {
  44.  
  45.         int k = 255 - random(96);
  46.  
  47.         // each pixel in the texture
  48.         for ( int m=0; m<16 * 3; m++ )
  49.             for ( int n = 0; n<16; n++ ) {
  50.  
  51.                 int i1 = 0x966C4A;
  52.                 int i2 = 0;
  53.                 int i3 = 0;
  54.  
  55.                 if (j == 4)
  56.                     i1 = 0x7F7F7F;
  57.                 if ((j != 4) || (random(3) == 0))
  58.                     k = 255 - random(96);
  59.                 if ( j == 1 )
  60.                 {
  61.                     if (m < (((n * n * 3 + n * 81) >> 2) & 0x3) + 18)
  62.                         i1 = 0x6AAA40;
  63.                     else if (m < (((n * n * 3 + n * 81) >> 2) & 0x3) + 19)
  64.                         k = k * 2 / 3;
  65.                 }
  66.                 if (j == 7)
  67.                 {
  68.                     i1 = 0x675231;
  69.                     if ((n > 0) && (n < 15) && (((m > 0) && (m < 15)) || ((m > 32) && (m < 47))))
  70.                     {
  71.                         i1 = 0xBC9862;
  72.                         i2 = n - 7;
  73.                         i3 = (m & 0xF) - 7;
  74.                         if (i2 < 0)
  75.                             i2 = 1 - i2;
  76.  
  77.                         if (i3 < 0)
  78.                             i3 = 1 - i3;
  79.  
  80.                         if (i3 > i2)
  81.                             i2 = i3;
  82.  
  83.                         k = 196 - random(32) + i2 % 3 * 32;
  84.                     }
  85.                     else if (random(2) == 0)
  86.                         k = k * (150 - (n & 0x1) * 100) / 100;
  87.                 }
  88.                 if (j == 5)
  89.                 {
  90.                     i1 = 0xB53A15;
  91.                     if (((n + m / 4 * 4) % 8 == 0) || (m % 4 == 0))
  92.                         i1 = 0xBCAFA5;
  93.                 }
  94.                 i2 = k;
  95.                 if (m >= 32)
  96.                     i2 /= 2;
  97.                 if (j == 8)
  98.                 {
  99.                     i1 = 5298487;
  100.                     if (random(2) == 0)
  101.                     {
  102.                         i1 = 0;
  103.                         i2 = 255;
  104.                     }
  105.                 }
  106.  
  107.                 // fixed point colour multiply between i1 and i2
  108.                 i3 =
  109.                     ((((i1 >> 16) & 0xFF) * i2 / 255) << 16) |
  110.                     ((((i1 >>  8) & 0xFF) * i2 / 255) <<  8) |
  111.                       ((i1        & 0xFF) * i2 / 255);
  112.                 // pack the colour away
  113.                 texmap[ n + m * 16 + j * 256 * 3 ] = i3;
  114.             }
  115.     }
  116. }
  117.  
  118. static void makeMap( void ) {
  119.     // add random blocks to the map
  120.     for ( int x = 0; x < 64; x++) {
  121.         for ( int y = 0; y < 64; y++) {
  122.             for ( int z = 0; z < 64; z++) {
  123.                 int i = (z << 12) | (y << 6) | x;
  124.                 float yd = (y - 32.5) * 0.4;
  125.                 float zd = (z - 32.5) * 0.4;
  126.                 map[i] = random( 16 );
  127.  
  128.                 float th = random( 256 ) / 256.0f;
  129.  
  130.                 if (th > sqrtf( sqrtf( yd * yd + zd * zd ) ) - 0.8f)
  131.                     map[i] = 0;
  132.             }
  133.         }
  134.     }
  135. }
  136.  
  137. static void init( void ) {
  138.     makeTextures( );
  139.     makeMap( );
  140. }
  141.  
  142. // fixed point byte byte multiply
  143. static inline int fxmul( int a, int b ) {
  144.     return (a*b)>>8;
  145. }
  146.  
  147. // fixed point 8bit packed colour multiply
  148. static inline int rgbmul( int a, int b ) {
  149.     int _r = (((a>>16) & 0xff) * b) >> 8;
  150.     int _g = (((a>> 8) & 0xff) * b) >> 8;
  151.     int _b = (((a    ) & 0xff) * b) >> 8;
  152.     return (_r<<16) | (_g<<8) | _b;
  153. }
  154.  
  155. static void render( void ) {
  156.  
  157.  
  158.     float now = (float)(SDL_GetTicks( ) % 10000) / 10000.f;
  159.  
  160.     float xRot = math_sin(now * math_pi * 2) * 0.4 + math_pi / 2;
  161.     float yRot = math_cos(now * math_pi * 2) * 0.4;
  162.     float yCos = math_cos(yRot);
  163.     float ySin = math_sin(yRot);
  164.     float xCos = math_cos(xRot);
  165.     float xSin = math_sin(xRot);
  166.  
  167.     float ox = 32.5 + now * 64.0;
  168.     float oy = 32.5;
  169.     float oz = 32.5;
  170.  
  171.     // for each column
  172.     for ( int x = 0; x < w; x++) {
  173.         // get the x axis delta
  174.         float ___xd = ((float)x - (float)w / 2.f) / (float)h;
  175.         // for each row
  176.         for ( int y = 0; y < h; y++) {
  177.             // get the y axis delta
  178.             float  __yd = ((float)y - (float)h / 2.f) / (float)h;
  179.             float  __zd = 1;
  180.             float ___zd =  __zd * yCos +  __yd * ySin;
  181.             float   _yd =  __yd * yCos -  __zd * ySin;
  182.             float   _xd = ___xd * xCos + ___zd * xSin;
  183.             float   _zd = ___zd * xCos - ___xd * xSin;
  184.  
  185.             int col = 0;
  186.             int br = 255;
  187.             float ddist = 0;
  188.  
  189.             float closest = 32.f;
  190.            
  191.             // for each principle axis  x,y,z
  192.             for ( int d = 0; d < 3; d++) {
  193.                 float dimLength = _xd;
  194.                 if (d == 1)
  195.                     dimLength = _yd;
  196.                 if (d == 2)
  197.                     dimLength = _zd;
  198.  
  199.                 float ll = 1.0f / (dimLength < 0.f ? -dimLength : dimLength);
  200.                 float xd = (_xd) * ll;
  201.                 float yd = (_yd) * ll;
  202.                 float zd = (_zd) * ll;
  203.  
  204.                 float       initial = ox - floor(ox);
  205.                 if (d == 1) initial = oy - floor(oy);
  206.                 if (d == 2) initial = oz - floor(oz);
  207.  
  208.                 if (dimLength > 0) initial = 1 - initial;
  209.  
  210.                 float dist = ll * initial;
  211.  
  212.                 float xp = ox + xd * initial;
  213.                 float yp = oy + yd * initial;
  214.                 float zp = oz + zd * initial;
  215.  
  216.                 if (dimLength < 0) {
  217.                     if (d == 0) xp--;
  218.                     if (d == 1) yp--;
  219.                     if (d == 2) zp--;
  220.                 }
  221.  
  222.                 // while we are concidering a ray that is still closer then the best so far
  223.                 while (dist < closest) {
  224.                
  225.                     // quantize to the map grid
  226.                     int tex = map[ (((int)zp & 63) << 12) | (((int)yp & 63) << 6) | ((int)xp & 63) ];
  227.  
  228.                     // if this voxel has a texture applied
  229.                     if (tex > 0) {
  230.  
  231.                         // find the uv coordinates of the intersection point
  232.                         int u = ((int)((xp + zp) * 16.f)) & 15;
  233.                         int v = ((int) (yp       * 16.f)  & 15) + 16;
  234.                        
  235.                         // fix uvs for alternate directions?
  236.                         if (d == 1) {
  237.                             u =  ((int)(xp * 16.f)) & 15;
  238.                             v = (((int)(zp * 16.f)) & 15);
  239.                             if (yd < 0)
  240.                                 v += 32;
  241.                         }
  242.                        
  243.                         // find the colour at the intersection point
  244.                         int cc = texmap[ u + v * 16 + tex * 256 * 3 ];
  245.                        
  246.                         // if the colour is not transparent
  247.                         if (cc > 0) {
  248.                             col = cc;
  249.                             ddist = 255 - ((dist / 32 * 255));
  250.                             br = 255 * (255 - ((d + 2) % 3) * 50) / 255;
  251.                            
  252.                             // we now have the closest hit point (also terminates this ray)
  253.                             closest = dist;
  254.                         }
  255.                     }
  256.                    
  257.                     // advance the ray
  258.                     xp += xd;
  259.                     yp += yd;
  260.                     zp += zd;
  261.                     dist += ll;
  262.                 }
  263.             }
  264.  
  265.             plot( x, y, rgbmul( col, fxmul( br, ddist ) ) );
  266.         }
  267.     }
  268. }
  269.  
  270. int main( void ) {
  271.     SDL_Init( SDL_INIT_VIDEO );
  272.     screen = SDL_SetVideoMode( w, h, 32, 0 );
  273.     if ( screen == nullptr )
  274.         return -1;
  275.     init( );
  276.     bool running = true;
  277.     while ( running ) {
  278.         SDL_Event event;
  279.         while ( SDL_PollEvent( &event ) ) {
  280.             running &= (event.type != SDL_QUIT);
  281.         }
  282.         render( );
  283.         SDL_Flip( screen );
  284.     }
  285.     return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement