Advertisement
Guest User

Minecraft4k_SDL

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