Advertisement
Guest User

cub.c

a guest
Feb 21st, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.63 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* main.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: mel-haya <mel-haya@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2019/12/22 16:20:09 by mel-haya #+# #+# */
  9. /* Updated: 2020/01/21 15:59:35 by mel-haya ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12.  
  13. #include <math.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include "GNL/get_next_line.h"
  17. #include "libft/libft.h"
  18. #include "mlx.h"
  19. #define TILE_SIZE 400
  20. #define ESC 53
  21. #define NO 3
  22. #define SO 1
  23. #define EA 2
  24. #define WE 0
  25. #define S 5
  26. #define MOV_SPEED TILE_SIZE / 10
  27. #define KEY_UP 126
  28. #define KEY_LEFT 123
  29. #define KEY_DOWN 125
  30. #define KEY_RIGHT 124
  31. #define FOV_ANGLE 60 * (M_PI / 180)
  32. #define MINI_MAP_RATIO 0.2
  33.  
  34.  
  35. typedef struct s_point
  36. {
  37. float x;
  38. float y;
  39. } t_point;
  40.  
  41. typedef struct s_sprite
  42. {
  43. t_point pos;
  44. float distance;
  45. } t_sprite;
  46.  
  47.  
  48. typedef struct s_texture
  49. {
  50. int *data;
  51. int height;
  52. int width;
  53. } t_texture;
  54.  
  55. typedef struct s_ray
  56. {
  57. float ray_angle;
  58. int facing_down;
  59. int facing_left;
  60. float distance;
  61. int hit_horz;
  62. int hit_vert;
  63. t_point horz_collision;
  64. t_point vert_collision;
  65. t_point closest_collision;
  66. int inv;
  67. int inh;
  68. } t_ray;
  69.  
  70. typedef struct s_window
  71. {
  72. int width;
  73. int height;
  74. void *mlx_ptr;
  75. void *win_ptr;
  76. int **map;
  77. int map_rows;
  78. int map_cols;
  79. int floor;
  80. int ceiling;
  81. int sprite_index;
  82. t_sprite sprite[100];
  83.  
  84. } t_window;
  85.  
  86. typedef struct s_player
  87. {
  88.  
  89. t_point pos;
  90. int walk_direction;
  91. float facing_angle;
  92. int move_speed;
  93. float rotation_speed;
  94. int turn_direction;
  95. void *win_ptr;
  96. void *mlx_ptr;
  97. int forward_key;
  98. int backward_key;
  99. } t_player;
  100.  
  101. void draw_line(t_point p1 ,t_point p2, int color);
  102. void make_map();
  103. void draw_form(int x ,int y ,int width,int color);
  104. void draw_map(void *mlx_ptr, void *win_ptr);
  105. void render_fov(t_player *p);
  106. int close_win();
  107. void render_sprite(t_player *p);
  108. void *map_img;
  109. int *data;
  110. t_texture *texture;
  111. t_ray *rays;
  112. t_window window;
  113.  
  114. void norm_angle(float *angle)
  115. {
  116. *angle = fmod(*angle , 2 * M_PI);
  117. if(*angle < 0)
  118. *angle += (M_PI * 2);
  119. }
  120.  
  121. t_point make_point(float x, float y)
  122. {
  123. t_point a;
  124. a.x = x;
  125. a.y = y;
  126. return a;
  127. }
  128.  
  129. int rgba_color(char *colors,float alpha)
  130. {
  131. char **color;
  132.  
  133. color = ft_split(colors,',');
  134. int output;
  135. if(alpha > 1)
  136. alpha = 1;
  137. output = ft_atoi(color[0]) * alpha;
  138. output *= 256;
  139. output += ft_atoi(color[1]) * alpha;
  140. output *= 256;
  141. output += ft_atoi(color[2]) * alpha;
  142. free(color[0]);
  143. free(color[1]);
  144. free(color[2]);
  145. free(color);
  146. return(output);
  147. }
  148.  
  149. void render_walls(t_player *p)
  150. {
  151. int i,y;
  152. float projection_plane_dist;
  153. float wall_height;
  154. float correct_dist;
  155. int color;
  156. int offset_x,offset_y;
  157. int index;
  158.  
  159.  
  160. i = 0;
  161. projection_plane_dist = (window.width / 2) / tan(FOV_ANGLE / 2);
  162. while(i < window.width)
  163. {
  164. index = 0;
  165. correct_dist = rays[i].distance * cos(rays[i].ray_angle - p->facing_angle);
  166. wall_height = (TILE_SIZE / correct_dist) * projection_plane_dist;
  167. y = 0;
  168. index = rays[i].inv;
  169. if(rays[i].hit_vert)
  170. offset_x = (int)rays[i].closest_collision.y % TILE_SIZE;
  171. else
  172. offset_x = (int)rays[i].closest_collision.x % TILE_SIZE;
  173. while(y < window.height)
  174. {
  175. if(y < (window.height - wall_height) / 2)
  176. color = window.ceiling;
  177. else if (y > (window.height - wall_height) / 2 && y < (window.height + wall_height) / 2)
  178. {
  179. int distanceFromTop = y + (wall_height / 2) - (window.height / 2);
  180. offset_y = distanceFromTop * ((float)TILE_SIZE / wall_height);
  181. color = texture[index].data[(texture[index].width * ((int)(offset_y * texture[index].height / TILE_SIZE))) + ((int)(offset_x * texture[index].width / TILE_SIZE))];
  182. }
  183. else
  184. color = window.floor;
  185. data[(window.width * y) + i] = color;
  186. y++;
  187. }
  188. i++;
  189. }
  190. }
  191.  
  192. t_point horz_intersection(t_player *p, int column_id)
  193. {
  194. float xintercept, yintercept;
  195. float xstep, ystep;
  196. t_point output;
  197.  
  198. rays[column_id].facing_down = (rays[column_id].ray_angle < M_PI && rays[column_id].ray_angle > 0) ? 1 : 0;
  199. rays[column_id].facing_left = (rays[column_id].ray_angle > M_PI / 2 && rays[column_id].ray_angle < 1.5 * M_PI) ? 1 : 0;
  200. yintercept = floor(p->pos.y / TILE_SIZE) * TILE_SIZE;
  201. yintercept += rays[column_id].facing_down * TILE_SIZE;
  202. xintercept = p->pos.x + (yintercept - p->pos.y) / tan(rays[column_id].ray_angle);
  203. ystep = rays[column_id].facing_down ? TILE_SIZE : -TILE_SIZE;
  204. xstep = TILE_SIZE / tan(rays[column_id].ray_angle);
  205. xstep *= (rays[column_id].facing_left && xstep > 0) ? -1 : 1;
  206. xstep *= (!rays[column_id].facing_left && xstep < 0) ? -1 : 1;
  207.  
  208. output.x = xintercept;
  209. output.y = yintercept;
  210. while(output.x > 0 && output.y > 0 && output.y / TILE_SIZE < window.map_rows && output.x / TILE_SIZE < window.map_cols)
  211. {
  212. if(window.map[(int)(output.y - !rays[column_id].facing_down) / TILE_SIZE][(int)output.x / TILE_SIZE] == 1)
  213. {
  214. if (sin(rays[column_id].ray_angle) > 0)
  215. rays[column_id].inh = SO;
  216. else
  217. rays[column_id].inh = NO;
  218. return (output);
  219. }
  220. else
  221. {
  222. output.x += xstep;
  223. output.y += ystep;
  224. }
  225. }
  226. return (output);
  227. }
  228.  
  229. t_point vert_intersection(t_player *p, int column_id)
  230. {
  231. float xintercept, yintercept;
  232. float xstep, ystep;
  233. t_point output;
  234.  
  235. xintercept = floor(p->pos.x / TILE_SIZE) * TILE_SIZE;
  236. xintercept += !rays[column_id].facing_left * TILE_SIZE;
  237. yintercept = p->pos.y + (xintercept - p->pos.x) * tan(rays[column_id].ray_angle);
  238. xstep = rays[column_id].facing_left ? -TILE_SIZE : TILE_SIZE;
  239. ystep = TILE_SIZE * tan(rays[column_id].ray_angle);
  240. ystep *= (!rays[column_id].facing_down && ystep > 0) ? -1 : 1;
  241. ystep *= (rays[column_id].facing_down && ystep < 0) ? -1 : 1;
  242.  
  243. output.x = xintercept;
  244. output.y = yintercept;
  245. while(output.x > 0 && output.y > 0 && output.y / TILE_SIZE < window.map_rows && output.x / TILE_SIZE < window.map_cols)
  246. {
  247. if(window.map[(int)output.y / TILE_SIZE][(int)(output.x - rays[column_id].facing_left) / TILE_SIZE] == 1)
  248. {
  249. if (cos(rays[column_id].ray_angle) > 0)
  250. rays[column_id].inv = EA;
  251. else
  252. rays[column_id].inv = WE;
  253. return (output);
  254. }
  255. else
  256. {
  257. output.x += xstep;
  258. output.y += ystep;
  259. }
  260. }
  261. return (output);
  262. }
  263.  
  264.  
  265. int in_canvas(t_point p)
  266. {
  267. if(p.x > 0 && p.x < window.width && p.y > 0 && p.y < window.height)
  268. return 1;
  269. return 0;
  270. }
  271.  
  272. float dist_two_point(t_point p1, t_point p2)
  273. {
  274. return (sqrt( (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y)));
  275. }
  276.  
  277. void cast_rays(t_player *p)
  278. {
  279. int column_id = 0;
  280. float horz_hit,vert_hit;
  281. float ray_angle = p->facing_angle - (FOV_ANGLE / 2);
  282.  
  283. while(column_id < window.width)
  284. {
  285. norm_angle(&ray_angle);
  286. rays[column_id].ray_angle = ray_angle;
  287. rays[column_id].horz_collision = horz_intersection(p,column_id);
  288. rays[column_id].vert_collision = vert_intersection(p,column_id);
  289. horz_hit = dist_two_point(p->pos, rays[column_id].horz_collision);
  290. vert_hit = dist_two_point(p->pos, rays[column_id].vert_collision);
  291. if (horz_hit > vert_hit)
  292. {
  293. rays[column_id].closest_collision = rays[column_id].vert_collision;
  294. rays[column_id].hit_vert = 1;
  295. rays[column_id].hit_horz = 0;
  296. rays[column_id].distance = vert_hit;
  297. }
  298. else
  299. {
  300. rays[column_id].closest_collision = rays[column_id].horz_collision;
  301. rays[column_id].hit_vert = 0;
  302. rays[column_id].hit_horz = 1;
  303. rays[column_id].distance = horz_hit;
  304. rays[column_id].inv = rays[column_id].inh;
  305. }
  306. ray_angle += FOV_ANGLE / (window.width);
  307. column_id++;
  308. }
  309. }
  310.  
  311.  
  312. void draw_form(int x ,int y ,int width,int color)
  313. {
  314.  
  315. int i = 0, j;
  316. while (i <= width)
  317. {
  318. j = 0;
  319. while(j <= width)
  320. {
  321. data[(y + i) * window.width + (x + j)] = color;
  322. j++;
  323. }
  324. i++;
  325. }
  326. }
  327.  
  328. void get_player_pos(t_player *p,int i, int j)
  329. {
  330.  
  331. p->pos.x = j * TILE_SIZE + TILE_SIZE / 2;
  332. p->pos.y = i * TILE_SIZE + TILE_SIZE / 2;
  333. if(window.map[i][j] == 'E')
  334. p->facing_angle = 0;
  335. else if(window.map[i][j] == 'S')
  336. p->facing_angle = M_PI / 2;
  337. else if(window.map[i][j] == 'W')
  338. p->facing_angle = M_PI;
  339. else if(window.map[i][j] == 'N')
  340. p->facing_angle = 3 * M_PI / 2;
  341. p->turn_direction = 0;
  342. p->walk_direction = 0;
  343. p->forward_key = 0;
  344. p->backward_key = 0;
  345. }
  346.  
  347. void draw_line(t_point p1 ,t_point p2,int color)
  348. {
  349.  
  350. float x,y,dx,dy,step,i = 0;
  351. dx= p2.x - p1.x;
  352. dy= p2.y - p1.y;
  353.  
  354.  
  355. if(fabsf(dx)>=fabsf(dy))
  356. step=fabsf(dx);
  357. else
  358. step=fabsf(dy);
  359.  
  360. dx=dx/step;
  361. dy=dy/step;
  362.  
  363. x = p1.x;
  364. y = p1.y;
  365. while(i <= step)
  366. {
  367. if(x > 0 && y > 0 && x < window.width && y < window.height)
  368. data[(int)y * window.width + (int)x] = color;
  369. x+=dx;
  370. y+=dy;
  371. i++;
  372. }
  373. }
  374.  
  375. void move_player(t_player *p)
  376. {
  377. float x,y,newx,newy;
  378.  
  379. x = p->pos.x;
  380. y = p->pos.y;
  381. if(p->walk_direction)
  382. {
  383. newx = x + cos(p->facing_angle) * (float)(MOV_SPEED * p->walk_direction);
  384. newy = y + sin(p->facing_angle) * (float)(MOV_SPEED * p->walk_direction);
  385. if(window.map[(int)y /TILE_SIZE][(int)newx / TILE_SIZE] != 1)
  386. p->pos.x = newx;
  387. if(window.map[(int)newy /TILE_SIZE][(int)x / TILE_SIZE] != 1)
  388. p->pos.y = newy;
  389. }
  390. if(p->turn_direction)
  391. p->facing_angle += p->rotation_speed * - p->turn_direction;
  392. norm_angle(&p->facing_angle);
  393.  
  394. }
  395.  
  396. int get_key(int key, t_player *p)
  397. {
  398. cast_rays(p);
  399. render_walls(p);
  400. make_map();
  401. render_fov(p);
  402. render_sprite(p);
  403. mlx_put_image_to_window(window.mlx_ptr,window.win_ptr, map_img, 0,0);
  404. if(key == ESC)
  405. close_win();
  406. if(key == KEY_UP)
  407. p->forward_key = 1;
  408. else if(key == KEY_DOWN)
  409. p->backward_key = 1;
  410. p->walk_direction = p->forward_key - p->backward_key;
  411. if(key == KEY_LEFT)
  412. p->turn_direction = 1;
  413. else if(key == KEY_RIGHT)
  414. p->turn_direction = -1;
  415. return(1);
  416. }
  417.  
  418. int key_up(int key, t_player *p)
  419. {
  420. if(key==KEY_UP)
  421. {
  422. p->forward_key = 0;
  423. if(p->backward_key)
  424. p->walk_direction = -1;
  425. else
  426. p->walk_direction = 0;
  427. }
  428. else if(key == KEY_DOWN)
  429. {
  430. p->backward_key = 0;
  431. if(p->forward_key)
  432. p->walk_direction = 1;
  433. else
  434. p->walk_direction = 0;
  435. }
  436. if(key == KEY_LEFT)
  437. p->turn_direction = 0;
  438. else if(key == KEY_RIGHT)
  439. p->turn_direction = 0;
  440. return (1);
  441. }
  442.  
  443. int ft_word_count(char const *str, char c)
  444. {
  445. int i;
  446. int output;
  447.  
  448. i = 0;
  449. output = 0;
  450. while (str[i])
  451. {
  452. if (str[i] != c && (i == 0 || str[i - 1] == c))
  453. output++;
  454. i++;
  455. }
  456. return (output);
  457. }
  458.  
  459. void make_map()
  460. {
  461.  
  462. int x;
  463. int y;
  464. int i;
  465. int j;
  466. float ratio;
  467.  
  468. i = 0;
  469. x = 0;
  470. y = 0;
  471. ratio = (float)window.width / window.map_cols * MINI_MAP_RATIO;
  472.  
  473.  
  474. while(i < window.map_rows)
  475. {
  476. j = 0;
  477. while(j < window.map_cols)
  478. {
  479. if(window.map[i][j] == 1)
  480. draw_form(j * ratio,i * ratio , ratio, 0xffff00);
  481. else
  482. draw_form(j * ratio,i * ratio , ratio, 0);
  483. j++;
  484. }
  485. i++;
  486. }
  487. }
  488.  
  489.  
  490.  
  491. void render_fov(t_player *p)
  492. {
  493. int i = 0;
  494. float ratio = (float)window.width / window.map_cols * MINI_MAP_RATIO/TILE_SIZE;
  495. t_point player;
  496. t_point ray;
  497. player.x = p->pos.x * ratio;
  498. player.y = p->pos.y * ratio;
  499. while(i < window.width)
  500. {
  501. //printf("%f\n", ratio);
  502. ray.x = floor(rays[i].closest_collision.x * ratio);
  503. ray.y = floor(rays[i].closest_collision.y * ratio);
  504. draw_line(player,ray,0x0f550f);
  505. i+=5;
  506. }
  507. }
  508.  
  509. void draw_sprite(int x,float distance,float height)
  510. {
  511.  
  512. int i = x;
  513. int j;
  514. while (i <= x + height)
  515. {
  516. j = (window.height / 2) - (height / 2);
  517. if(distance < rays[i].distance && i >= 0 && i < window.width)
  518. {
  519. while(j <= (window.height / 2) + (height / 2))
  520. {
  521. if(j < window.height && j >= 0)
  522. data[(j) * window.width + (i)] = 0xff;
  523. j++;
  524. }
  525. }
  526. i++;
  527. }
  528.  
  529. }
  530.  
  531. void render_sprite(t_player *p)
  532. {
  533. //float ratio = (float)window.width / window.map_cols * MINI_MAP_RATIO/TILE_SIZE;
  534. float angle;
  535. float sprite_height;
  536. int column_index;
  537. float projection_plane_dist = (window.width / 2) / tan(FOV_ANGLE / 2);
  538. //float max_index = (2 * M_PI / (FOV_ANGLE)) * window.width;
  539. t_sprite *s = window.sprite;
  540. int i = 0;
  541. while(i < window.sprite_index)
  542. {
  543. angle = atan2(s[i].pos.y - p->pos.y, s[i].pos.x - p->pos.x);
  544.  
  545. s[i].distance = dist_two_point(s[i].pos,p->pos);
  546. //angle = angle - rays[window.width].ray_angle;
  547. norm_angle(&angle);
  548.  
  549. if (rays[0].ray_angle > rays[window.width].ray_angle && angle < rays[window.width].ray_angle)
  550. angle += 2 * M_PI;
  551. column_index = (angle - rays[0].ray_angle) / (FOV_ANGLE / window.width);
  552. // if(rays[0].ray_angle < angle)
  553. // angle -= 2 * M_PI;
  554.  
  555. //printf("%.2f | %7.2f |%.2f\n",rays[0].ray_angle * 180 / M_PI, angle * 180 / M_PI, rays[window.width - 1].ray_angle * 180 / M_PI);
  556. sprite_height = (TILE_SIZE / s[i].distance) * projection_plane_dist;
  557.  
  558. //column_index = angle * window.width / FOV_ANGLE + ((window.width / 2 )- (sprite_height / 2));
  559. //column_index = (angle - p->facing_angle) / FOV_ANGLE * window.width + (window.width / 2 - sprite_height / 2);
  560. draw_sprite(column_index - (sprite_height / 2) , s[i].distance, sprite_height);
  561. i++;
  562. }
  563. }
  564.  
  565. int loop_hook(t_player *p)
  566. {
  567. cast_rays(p);
  568. render_walls(p);
  569. make_map();
  570. render_fov(p);
  571. render_sprite(p);
  572. mlx_put_image_to_window(window.mlx_ptr,window.win_ptr, map_img, 0,0);
  573. move_player(p);
  574. return 0;
  575. }
  576.  
  577.  
  578. void load_textures(char *filename,int side)
  579. {
  580. int width,height,c;
  581. void *img;
  582.  
  583. img = mlx_xpm_file_to_image(window.mlx_ptr,filename,&width,&height);
  584. texture[side].width = width;
  585. texture[side].height = height;
  586. texture[side].data = (int *)mlx_get_data_addr(img,&c,&c,&c);
  587. free(img);
  588. return;
  589. }
  590.  
  591. int ft_checkset(char c, char const *set)
  592. {
  593. int i;
  594.  
  595. i = 0;
  596. while (set[i])
  597. {
  598. if (c == set[i])
  599. return (1);
  600. i++;
  601. }
  602. return (0);
  603. }
  604.  
  605. char *filter(char *str, char const *set)
  606. {
  607. int count;
  608. int i;
  609. int j;
  610. char *output;
  611.  
  612. count = 0;
  613. output = NULL;
  614. j = 0;
  615. i = 0;
  616. while(str[i])
  617. {
  618. if(!ft_checkset(str[i], set))
  619. count++;
  620. i++;
  621. }
  622. if(count)
  623. {
  624. output = malloc(count + 1);
  625. output[count] = 0;
  626. i = 0;
  627. while(str[i])
  628. {
  629. if(!ft_checkset(str[i], set))
  630. {
  631. output[j] = str[i];
  632. j++;
  633. }
  634. i++;
  635. }
  636. }
  637. return output;
  638. }
  639.  
  640. int check_row(char *line)
  641. {
  642. char *copy;
  643. int output;
  644.  
  645. output = 0;
  646. copy = filter(line,"012NSWE");
  647. if(copy)
  648. {
  649. output = ft_strlen(copy);
  650. free(copy);
  651. }
  652. return (output);
  653. }
  654.  
  655. void free_array(char ***array)
  656. {
  657. int i;
  658.  
  659. i = 0;
  660. while(i < window.map_rows)
  661. {
  662. free(*array[i]);
  663. }
  664. free(*array);
  665. }
  666.  
  667. char *check_map(char *line,t_player *p)
  668. {
  669. char *tmp;
  670. char **array;
  671. int i;
  672. int j;
  673. int player;
  674. char *error;
  675.  
  676. error = NULL;
  677. i = 0;
  678. player = 0;
  679. window.map = malloc(window.map_rows * sizeof(int *));
  680. array = ft_split(line, '\n');
  681. while(array[i])
  682. {
  683. tmp = array[i];
  684. array[i] = filter(array[i], " ");
  685. //printf("%s", array[i]);
  686. free(tmp);
  687. if((int)ft_strlen(array[i]) != window.map_cols)
  688. error = ft_strjoin("number of columns invalid at row",ft_itoa(i + 1));
  689. else if(array[i][window.map_cols - 1] != '1' && error)
  690. error = ft_strjoin("the map is not closed at row",ft_itoa(i + 1));
  691. else if(check_row(array[i])&& error)
  692. error = ft_strjoin("invalid character in the row ", ft_itoa(i + 1));
  693. j = 0;
  694. window.map[i] = malloc(window.map_cols * sizeof(int *));
  695. while(j < window.map_cols)
  696. {
  697. window.map[i][j] = ft_isdigit(array[i][j]) ? array[i][j] - 48 : array[i][j];
  698. if(ft_isalpha(array[i][j]))
  699. {
  700. get_player_pos(p,i,j);
  701. player++;
  702. }
  703. else if(array[i][j] == '2' && window.sprite_index < 100)
  704. {
  705. window.sprite[window.sprite_index].pos.x = (j * TILE_SIZE) + (float)(TILE_SIZE / 2);
  706. window.sprite[window.sprite_index].pos.y = (i * TILE_SIZE) + (float)(TILE_SIZE / 2);
  707. window.sprite_index++;
  708. }
  709. j++;
  710. }
  711. i++;
  712. }
  713. if(player != 1)
  714. error = ft_strjoin(ft_itoa(player)," player object detected");
  715. //free_array(&array);
  716. return (error);
  717. }
  718.  
  719.  
  720. int load_map(char **line,int fd,t_player *p)
  721. {
  722. char *buffer;
  723. char *tmp;
  724. char *error;
  725.  
  726.  
  727. window.map_cols = ft_word_count(*line,' ');
  728. buffer = malloc(window.map_cols * sizeof(char *));
  729. window.map_rows = 0;
  730. while(*line[0] == '1')
  731. {
  732. tmp = buffer;
  733. buffer = ft_strjoin(buffer, *line);
  734. free(tmp);
  735. tmp = buffer;
  736. buffer = ft_strjoin(buffer,"\n");
  737. free(tmp);
  738. free(*line);
  739. get_next_line(fd, line);
  740. window.map_rows++;
  741. }
  742. error = check_map(buffer,p);
  743. if(error)
  744. {
  745. printf("error:\n");
  746. printf("%s\n",error);
  747. free(error);
  748. return (1);
  749. }
  750. return (0);
  751. }
  752.  
  753. int load_cub_file(char *filename,t_player *p)
  754. {
  755. char *line;
  756. int fd;
  757. char **words;
  758. int error;
  759. int a;
  760.  
  761. error = 0;
  762. fd = open(filename, O_RDONLY);
  763. while(get_next_line(fd, &line))
  764. {
  765. words = ft_split(line, ' ');
  766. if(!words[0])
  767. continue;
  768. if(!ft_strncmp(words[0], "R", 1))
  769. {
  770. window.width = ft_atoi(words[1]);
  771. window.height = ft_atoi(words[2]);
  772. window.win_ptr = mlx_new_window(window.mlx_ptr,window.width,window.height,"Bruh");
  773. map_img = mlx_new_image(window.mlx_ptr, window.width,window.height);
  774. data = (int*)mlx_get_data_addr(map_img, &a,&a,&a);
  775. }
  776. else if(!ft_strncmp(words[0], "NO", 2))
  777. load_textures(words[1], NO);
  778. else if(!ft_strncmp(words[0], "SO", 2))
  779. load_textures(words[1], SO);
  780. else if(!ft_strncmp(words[0], "EA", 2))
  781. load_textures(words[1], EA);
  782. else if(!ft_strncmp(words[0], "WE", 2))
  783. load_textures(words[1], WE);
  784. else if(!ft_strncmp(words[0], "S", 2))
  785. load_textures(words[1], S);
  786. else if(!ft_strncmp(words[0], "F", 1))
  787. window.floor = rgba_color(words[1],1);
  788. else if(!ft_strncmp(words[0], "C", 1))
  789. window.ceiling = rgba_color(words[1],1);
  790. else if(words[0][0] == '1')
  791. load_map(&line,fd,p);
  792. free(words);
  793. }
  794. return (0);
  795. }
  796.  
  797. int close_win()
  798. {
  799. exit(0);
  800. }
  801.  
  802. int main()
  803. {
  804. t_player p;
  805.  
  806. void *mlx_ptr = mlx_init();
  807.  
  808. texture = malloc(sizeof(t_texture) * 8);
  809. window.mlx_ptr = mlx_ptr;
  810. load_cub_file("test.cub",&p);
  811. rays = malloc(sizeof(t_ray) * window.width);
  812. make_map();
  813. p.rotation_speed = 4 * (M_PI / 180);
  814. mlx_hook(window.win_ptr, 2, 0, get_key, &p);
  815. mlx_hook(window.win_ptr, 3, 0, key_up, &p);
  816. mlx_hook(window.win_ptr,17,0,close_win,NULL);
  817. mlx_loop_hook(mlx_ptr,loop_hook,&p);
  818. mlx_loop(mlx_ptr);
  819. return(0);
  820. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement