Advertisement
Loan198

Untitled

May 13th, 2020
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1.  
  2. #include "game_map.h"
  3. #include<stdio.h>
  4. #include<conio.h>
  5.  
  6. void GameMap::LoadMap(char* name)
  7. {
  8. FILE* fp = NULL;
  9. fp = fopen(name, "rb");
  10. //fopen_s(&fp, name, "rb");
  11. if ( fp == NULL)
  12. {
  13. return;
  14. }
  15. game_map.max_x = 0;
  16. game_map.max_y = 0;
  17. for (int i =0; i< MAX_MAP_Y; i++)
  18. {
  19. for ( int j = 0; j < MAX_MAP_X; j++)
  20. {
  21. fscanf(fp,"%d",&game_map.tile[i][j]);
  22. //fscanf_s( fp, "%d", &game_map.tile[i][j]);
  23. int val = game_map.tile[i][j];
  24. if ( val > 0)
  25. {
  26. if (j> game_map.max_x)
  27. game_map.max_x = j;
  28. if ( i> game_map.max_y ) game_map.max_y = i;
  29. }
  30. }
  31. }
  32. game_map.max_x = (game_map.max_x + 1) *TILE_SIZE;
  33. game_map.max_y = ( game_map.max_y + 1) * TILE_SIZE;
  34. game_map.start_y = 0;
  35. game_map.start_x = 0;
  36. game_map.file_name_ = name;
  37. fclose(fp);
  38. }
  39. void GameMap:: LoadTiles ( SDL_Renderer* screen)
  40. {
  41. char file_img[30];
  42. FILE* fp = NULL;
  43.  
  44. for ( int i = 0; i< MAX_TILE; i++)
  45. {
  46. // sprintf_s(file_img,"map%d.png",i);
  47. sprintf(file_img,"map/map%d.png",i);
  48. //fopen_s( &fp, file_img, "rb");
  49. fp = fopen(file_img,"rb");
  50. if ( fp == NULL)
  51. {
  52. continue;
  53. }
  54. fclose(fp);
  55. tile_map[i].Load_Image(file_img, screen);
  56. }
  57. }
  58. void GameMap:: DrawMap ( SDL_Renderer* screen )
  59. {
  60. int x1 = 0, x2 = SCREEN_WIDTH;
  61. int y1 =0, y2 = SCREEN_HEIGHT;
  62. int max_x = game_map.start_x / TILE_SIZE;
  63. int max_y = game_map.start_y / TILE_SIZE;
  64. for ( int i = y1; i<= y2; i+= TILE_SIZE)
  65. {
  66. max_x = game_map.start_x / TILE_SIZE;
  67. for ( int j = x1; j<= x2; j += TILE_SIZE)
  68. {
  69. int val = game_map.tile[max_y][max_x];
  70. if ( val >0 && val < 20)
  71. {
  72. tile_map[val].SetRect(j,i);
  73. tile_map[val].Render(screen);
  74. }
  75. max_x ++;
  76. }
  77. max_y ++;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement