Advertisement
Guest User

Untitled

a guest
Jan 5th, 2020
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <SDL2/SDL.h>
  3. #include <stdbool.h>
  4. #include "map.h"
  5. #include <math.h>
  6. #define USE_REFLECTIONS 1
  7. #define RESX 800
  8. #define RESY 600
  9. #define HALFRESX (RESX/2.0)
  10. #define HALFRESY (RESY/2.0)
  11. #define MOVESPEED 0.008
  12. #define STRAFEMOVESPEED 0.00565685424948
  13. #define ROTATESPEED 0.002
  14. #define MAXLIGHTS 4
  15. #define MAPSIZE 32
  16.  
  17.  
  18. struct Point3D {
  19. float x, y, z;
  20. };
  21.  
  22.  
  23. struct pos {
  24. int x, y, z;
  25. };
  26.  
  27. void randGenMap(int level) {
  28. for(int x = 1; x < 31; x++) {
  29. for(int y = 1; y < 31; y++) {
  30. char material = rand()%20;
  31. if(material!=2 && material < 6) {
  32. MAP[x][level][y] = material;
  33. }
  34. else MAP[x][level][y] = 0;
  35. }
  36. }
  37. }
  38.  
  39. void main() {
  40.  
  41. char placeDist = 5;
  42. SDL_Window* window = NULL; //init SDL
  43. SDL_Renderer* renderer = NULL;
  44.  
  45. bool running = true;
  46.  
  47. if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
  48. printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
  49. running = false;
  50. }
  51.  
  52. window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, RESX, RESY, SDL_WINDOW_SHOWN );
  53.  
  54. if( window==NULL) {
  55. printf( "SDL_Error: %s\n", SDL_GetError() );
  56. running = false;
  57. }
  58.  
  59. renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
  60.  
  61. if(renderer==NULL) {
  62. printf("Renderer error: %s\n", SDL_GetError() );
  63. running = false;
  64. }
  65.  
  66. SDL_Texture * texture = SDL_CreateTexture(renderer,
  67. SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, RESX,RESY);
  68. auto Uint32 pixels[RESY][RESX];
  69.  
  70.  
  71. const Uint8* keystate = SDL_GetKeyboardState( NULL );
  72. int t1, t2;
  73. struct Point3D pos;
  74. pos.x = 16;
  75. pos.y = 16;
  76. pos.z = 16;
  77. struct Point3D dir;
  78. dir.x = 0;
  79. dir.y = -1; //direction vector
  80. dir.z = 0;
  81. struct Point3D plane;
  82. plane.x = 1.0;
  83. plane.y = 0;
  84. plane.z = 0.75;
  85. float planeConstant = sqrtf(plane.x*plane.x+plane.y*plane.y+plane.z*plane.z);
  86. char light[MAXLIGHTS][3];
  87. light[0][0] = 4;
  88. light[0][1] = 4;
  89. light[0][2] = 4;
  90. unsigned char voxelColour;
  91.  
  92. void rotatez(float rotspeed) {
  93. float oldDirX = dir.x;
  94. dir.x = dir.x * cos(rotspeed) - dir.y * sin(rotspeed);
  95. dir.y = oldDirX * sin(rotspeed) + dir.y * cos(rotspeed);
  96. oldDirX = plane.x;
  97. plane.x = plane.x * cos(rotspeed) - plane.y * sin(rotspeed);
  98. plane.y = oldDirX * sin(rotspeed) + plane.y * cos(rotspeed);
  99. }
  100. void rotatey(float rotspeed) {
  101. //How?
  102. return; // placeholder
  103. }
  104.  
  105. void move(int ms) {
  106. float velocity = ((keystate[SDL_SCANCODE_D] ^ keystate[SDL_SCANCODE_A]) &&
  107. (keystate[SDL_SCANCODE_W] ^ keystate[SDL_SCANCODE_S])) ? STRAFEMOVESPEED*ms : MOVESPEED*ms;
  108.  
  109. struct Point3D oldpos = pos;
  110.  
  111. if(keystate[SDL_SCANCODE_D]) {
  112. pos.x -= dir.y * velocity;
  113. pos.y += dir.x * velocity;
  114. }
  115. if(keystate[SDL_SCANCODE_A]) {
  116. pos.x += dir.y * velocity;
  117. pos.y -= dir.x * velocity;
  118. }
  119. if(keystate[SDL_SCANCODE_W]) {
  120. pos.x += dir.x * velocity;
  121. pos.y += dir.y * velocity;
  122. }
  123. if(keystate[SDL_SCANCODE_S]) {
  124. pos.x -= dir.x * velocity;
  125. pos.y -= dir.y * velocity;
  126. }
  127. if(keystate[SDL_SCANCODE_E]) {
  128. pos.z -= STRAFEMOVESPEED*ms;
  129. }
  130. if(keystate[SDL_SCANCODE_Q]) {
  131. pos.z += STRAFEMOVESPEED*ms;
  132. }
  133.  
  134. if(MAP[(char)pos.x][(char)pos.y][(char)pos.z]) pos = oldpos;
  135. } //remove oldpos and velocity when not needed
  136.  
  137. int ms = 5;
  138. int a = 0;
  139. while( running ) {
  140. t1 = SDL_GetTicks();
  141.  
  142. SDL_Event e;
  143. running = !(SDL_PollEvent( &e ) && e.type == SDL_QUIT);
  144.  
  145. move(ms);
  146.  
  147.  
  148. if(keystate[SDL_SCANCODE_LEFT]) {
  149. rotatez(-ROTATESPEED*ms);
  150. }
  151. if(keystate[SDL_SCANCODE_RIGHT]){
  152. rotatez(ROTATESPEED*ms);
  153. }
  154. if(keystate[SDL_SCANCODE_UP]) {
  155. rotatey(ROTATESPEED*ms);
  156. }
  157. if(keystate[SDL_SCANCODE_DOWN]){
  158. rotatey(-ROTATESPEED*ms);
  159. }
  160.  
  161. char blockMat;
  162. if(keystate[SDL_SCANCODE_1]) blockMat = 1;
  163. else if(keystate[SDL_SCANCODE_2]) blockMat = 3;
  164. else if(keystate[SDL_SCANCODE_3]) blockMat = 4;
  165. else if(keystate[SDL_SCANCODE_4]) blockMat = 5;
  166. #ifdef USE_REFLECTIONS
  167. else if(keystate[SDL_SCANCODE_5]) blockMat = 6;
  168. #endif
  169. if(keystate[SDL_SCANCODE_SPACE] && !MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z]) {
  170. MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] = blockMat;
  171. }
  172. else if(keystate[SDL_SCANCODE_LCTRL] && MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] != 2) {
  173. MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z] = 0;
  174. }
  175. else if(keystate[SDL_SCANCODE_LSHIFT] && !MAP[(char)(pos.x+dir.x*placeDist)][(char)(pos.y+dir.y*placeDist)][(char)pos.z]) {
  176. light[0][0] = (char)(pos.x+dir.x*placeDist);
  177. light[0][1] = (char)(pos.y+dir.y*placeDist);
  178. light[0][2] = (char) pos.z;
  179. }
  180. else if(keystate[SDL_SCANCODE_ESCAPE]) running = false;
  181. else if(keystate[SDL_SCANCODE_B]) {
  182. randGenMap(1);
  183. randGenMap(2);
  184. randGenMap(3);
  185. randGenMap(4);
  186. randGenMap(5);
  187. randGenMap(6);
  188. randGenMap(7);
  189. randGenMap(8);
  190. }
  191.  
  192.  
  193. for(int x = 0; x < RESX; x ++) {
  194. float cameraX = x/HALFRESX -1;
  195.  
  196. for(int y = 0; y < RESY; y ++) {
  197.  
  198.  
  199. float cameraY = y/HALFRESY -1;//cam vec, -1 to 1
  200.  
  201.  
  202. struct Point3D rayd;
  203. rayd.z = dir.z + plane.z*cameraY;
  204. rayd.x = dir.x + plane.x*cameraX;
  205. rayd.y = dir.y + plane.y*cameraX;
  206.  
  207. struct Point3D delta;
  208. delta.y = fabsf(1/rayd.y);
  209. delta.x = fabsf(1/rayd.x);
  210. delta.z = fabsf(1/rayd.z);
  211.  
  212.  
  213. struct pos map;
  214. map.x = (char)pos.x;
  215. map.y = (char)pos.y;
  216. map.z = (char)pos.z;
  217.  
  218. struct pos s;
  219. struct pos step;
  220. struct Point3D sdist;
  221. if(rayd.x < 0) {
  222. step.x = -1;
  223. s.x = 1;
  224. sdist.x = (pos.x - map.x) * delta.x;
  225. }
  226. else {
  227. step.x = 1;
  228. s.x = 0;
  229. sdist.x = (map.x + 1 - pos.x) * delta.x;
  230. }
  231. if(rayd.y < 0) {
  232. step.y = -1;
  233. s.y = 1;
  234. sdist.y = (pos.y - map.y) * delta.y;
  235. }
  236. else {
  237. step.y = 1;
  238. s.y = 0;
  239. sdist.y = (map.y + 1 - pos.y) * delta.y;
  240. }
  241.  
  242. if(rayd.z < 0) {
  243. step.z = -1;
  244. s.z = 1;
  245. sdist.z = (pos.z - map.z) * delta.z;
  246. }
  247. else {
  248. step.z = 1;
  249. s.z = 0;
  250. sdist.z = (map.z + 1 - pos.z) * delta.z;
  251. }
  252.  
  253.  
  254.  
  255. char side; //either 0 (NS), or 1 (EW), or 2(UD)
  256. while( !MAP[map.x][map.y][map.z]) {
  257. if(sdist.y < sdist.x ) {
  258. if(sdist.y < sdist.z) {
  259. sdist.y += delta.y;
  260. map.y += step.y;
  261. side = 1; // y
  262. }
  263. else {
  264. sdist.z += delta.z;
  265. map.z += step.z;
  266. side = 2;
  267. }
  268. }
  269. else {
  270. if(sdist.x < sdist.z) {
  271. sdist.x += delta.x;
  272. map.x += step.x;
  273. side = 0;
  274. }
  275. else {
  276. sdist.z += delta.z;
  277. map.z += step.z;
  278. side = 2;
  279. }
  280. }
  281. }
  282. char blockHit = MAP[map.x][map.y][map.z];
  283.  
  284.  
  285. #ifdef USE_REFLECTIONS
  286. bool reflect = false;
  287. reflect:;
  288. if(blockHit == 6){
  289. reflect = true;
  290. if(!side) {
  291. step.x*=-1;
  292. sdist.x += delta.x;
  293. map.x += step.x;
  294. rayd.x*=-1;
  295. }
  296. else if(side == 1) {
  297. step.y*=-1;
  298. sdist.y += delta.y;
  299. map.y += step.y;
  300. rayd.y*=-1;
  301. }
  302. else {
  303. step.z*=-1;
  304. sdist.z += delta.z;
  305. map.z += step.z;
  306. rayd.z*=-1;
  307. }
  308.  
  309. while( !MAP[map.x][map.y][map.z]) {
  310. if(sdist.y < sdist.x ) {
  311. if(sdist.y < sdist.z) {
  312. sdist.y += delta.y;
  313. map.y += step.y;
  314. side = 1; // y
  315. }
  316. else {
  317. sdist.z += delta.z;
  318. map.z += step.z;
  319. side = 2;
  320. }
  321. }
  322. else {
  323. if(sdist.x < sdist.z) {
  324. sdist.x += delta.x;
  325. map.x += step.x;
  326. side = 0;
  327. }
  328. else {
  329. sdist.z += delta.z;
  330. map.z += step.z;
  331. side = 2;
  332. }
  333. }
  334. }
  335.  
  336. blockHit = MAP[map.x][map.y][map.z];
  337. }
  338. #endif
  339. struct Point3D lightDist;
  340. struct Point3D hit;
  341.  
  342. if(!side) { // x side hit
  343.  
  344. hit.x = map.x + s.x;
  345. float relativeHit = hit.x-pos.x;
  346. hit.y = pos.y + relativeHit/rayd.x*rayd.y;
  347. hit.z = pos.z + relativeHit/rayd.x*rayd.z;
  348.  
  349. }
  350. else if(side == 1) { // y side hit
  351.  
  352. hit.y = map.y + s.y;
  353. float relativeHit = hit.y-pos.y;
  354. hit.x = pos.x + relativeHit/rayd.y*rayd.x;
  355. hit.z = pos.z + relativeHit/rayd.y*rayd.z;
  356.  
  357. }
  358. else { // z side hit
  359.  
  360. hit.z = map.z + s.z;
  361. float relativeHit = hit.z-pos.z;
  362. hit.x = pos.x + relativeHit/rayd.z*rayd.x;
  363. hit.y = pos.y + relativeHit/rayd.z*rayd.y;
  364. }
  365. lightDist.x = light[0][0] - hit.x; // x
  366. lightDist.y = light[0][1] - hit.y; // y
  367. lightDist.z = light[0][2] - hit.z; // z
  368.  
  369. double lightdist = sqrt(lightDist.x*lightDist.x + lightDist.y*lightDist.y + lightDist.z*lightDist.z);
  370. if(lightdist > 32) {voxelColour = 32;}
  371. else if(lightdist > 4) voxelColour = 1024/lightdist;
  372. else voxelColour = 255;
  373.  
  374.  
  375.  
  376. #ifdef USE_REFLECTIONS
  377. if(reflect) voxelColour/=1.2;
  378. #endif
  379.  
  380. void drawPixel( Uint32 colour) {
  381. pixels[y][x] = colour;
  382. }
  383.  
  384. if(blockHit <3) drawPixel(0x010101*voxelColour);
  385. else if(blockHit == 3) drawPixel(voxelColour);
  386. else if(blockHit == 4) drawPixel(0x10000*voxelColour);
  387. #ifdef USE_REFLECTIONS
  388. else if(blockHit == 6) goto reflect;
  389. #endif
  390. else drawPixel(0x100*voxelColour);
  391.  
  392. }
  393. }
  394. SDL_UpdateTexture(texture, NULL, pixels, RESX*sizeof(Uint32));
  395. SDL_RenderCopy(renderer, texture, NULL, NULL);
  396. SDL_RenderPresent( renderer );
  397. t2 = SDL_GetTicks();
  398. ms = t2-t1;
  399. printf("pos: %f, %f, %f | render time: %dms\n", pos.x, pos.y, pos.z, ms );
  400.  
  401. }
  402. SDL_DestroyWindow( window );
  403. SDL_DestroyRenderer( renderer );
  404. SDL_DestroyTexture( texture );
  405. SDL_Quit();
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement