2
0

Example_-_MiniMap.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // MiniMap example
  3. #include "raylib.h"
  4. #include <math.h>
  5. static int map[300][300];
  6. static int mapWidth = 100;
  7. static int mapHeight = 100;
  8. static float tileWidth;
  9. static float tileHeight;
  10. static const int screenWidth = 640;
  11. static const int screenHeight = 480;
  12. static int mapx,mapy;
  13. static void makemap();
  14. static void drawminimap();
  15. static void drawmap();
  16. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2);
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. InitWindow(screenWidth, screenHeight, "raylib example.");
  22. // We want to have the tile width and height fill the entire screen.
  23. tileWidth = 32 ;
  24. tileHeight = 32;
  25. // Here we generate the map using the hill algorithm
  26. makemap();
  27. mapx = 0;
  28. mapy = 0;
  29. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  30. //--------------------------------------------------------------------------------------
  31. // Main game loop
  32. while (!WindowShouldClose()) // Detect window close button or ESC key
  33. {
  34. // Update
  35. //----------------------------------------------------------------------------------
  36. if(IsKeyReleased(KEY_SPACE)){
  37. makemap();
  38. }
  39. if(rectsoverlap(GetMouseX(),GetMouseY(),1,1,screenWidth-105,32,100,100)){
  40. mapx = GetMouseX()-(screenWidth-105);
  41. mapy = GetMouseY()-32;
  42. if(mapx>100-17)mapx=100-17;
  43. if(mapy>100-14)mapy=100-14;
  44. }
  45. //----------------------------------------------------------------------------------
  46. // Draw
  47. //----------------------------------------------------------------------------------
  48. BeginDrawing();
  49. ClearBackground(RAYWHITE);
  50. drawmap();
  51. drawminimap();
  52. DrawRectangle(0,3,screenWidth,24,DARKGRAY);
  53. DrawText("Move mouse over minimap.",2,6,20,WHITE);
  54. DrawRectangle(0,screenHeight-30,screenWidth,24,DARKGRAY);
  55. DrawText("Press Space to Generate new map",2,screenHeight-28,20,WHITE);
  56. EndDrawing();
  57. //----------------------------------------------------------------------------------
  58. }
  59. // De-Initialization
  60. //--------------------------------------------------------------------------------------
  61. CloseWindow(); // Close window and OpenGL context
  62. //--------------------------------------------------------------------------------------
  63. return 0;
  64. }
  65. static void makemap(){
  66. for(int y=0;y<mapHeight;y++){
  67. for(int x=0;x<mapWidth;x++){
  68. map[x][y]=0;
  69. }}
  70. // First we create a couple of hills in the map
  71. // We create a random spot and draw a line there somewhat randomly
  72. // We create a line of some sort to create more realistic landmasses. :Experiment:
  73. int rx,ry;
  74. for(int i=0;i<mapWidth/3;i++){
  75. rx = GetRandomValue(5,mapWidth-5);
  76. ry = GetRandomValue(5,mapHeight-5);
  77. int mx=GetRandomValue(-1,1);
  78. int my=GetRandomValue(-1,1);
  79. int len=GetRandomValue(1,mapWidth/4);
  80. for(int ii=0;ii<len;ii++){
  81. if(GetRandomValue(0,10)<2){
  82. mx=GetRandomValue(-1,1);
  83. my=GetRandomValue(-1,1);
  84. }
  85. int nx=rx+mx;
  86. int ny=ry+my;
  87. if(nx>0&&nx<mapWidth-1 && ny>0 && ny<mapHeight-1){
  88. map[nx][ny] = 1;
  89. }
  90. }
  91. }
  92. // Now we want to pile sand ontop and right next to the current hills.
  93. // This will make them into larger hills. We do this by picking a random
  94. // spot. If there is something below that we increase the height there.
  95. for(int i=0;i<(mapWidth*mapHeight)*2;i++){
  96. int nx,ny;
  97. nx = GetRandomValue(1,mapWidth-2);
  98. ny = GetRandomValue(1,mapHeight-2);
  99. if(map[nx][ny]>0){
  100. for(int y=-1;y<2;y++){
  101. for(int x=-1;x<2;x++){
  102. map[nx+x][ny+y]++;
  103. if(map[nx+x][ny+y]>100)map[nx+x][ny+y]=100;
  104. }
  105. }
  106. }
  107. }
  108. };
  109. static void drawmap(){
  110. for(int y=0;y<14;y++){
  111. for(int x=0;x<17;x++){
  112. if(x+mapx>=100 && y+mapy>=100)continue;
  113. int m = map[x+mapx][y+mapy];
  114. if(m==0){ // water
  115. DrawRectangle(x*tileWidth,y*tileHeight+32,tileWidth,tileHeight,BLUE);
  116. }
  117. if(m>0){
  118. if(m>0){//Draw the beach
  119. DrawRectangle(x*tileWidth,y*tileHeight+32,tileWidth,tileHeight,(Color){250,220,0,255});
  120. }
  121. if(m>3){//If the height is larger than 3 than draw trees
  122. DrawRectangle(x*tileWidth,y*tileHeight+32,tileWidth,tileHeight,(Color){0,210,0,255});
  123. }
  124. if(m>15){ // here we draw some more trees
  125. DrawRectangle(x*tileWidth,y*tileHeight+32,tileWidth,tileHeight,(Color){0,170,0,255});
  126. }
  127. if(m>20){ // draw mountains or rocks
  128. DrawRectangle(x*tileWidth,y*tileHeight+32,tileWidth,tileHeight,(Color){110,110,110,255});
  129. }
  130. }
  131. }
  132. }
  133. }
  134. static void drawminimap(){
  135. DrawRectangle(0,0,screenWidth,32,BLACK);
  136. DrawRectangle(screenWidth-110,32,110,screenHeight-64,BLACK);
  137. DrawRectangle(0,screenHeight-32,screenWidth,32,BLACK);
  138. for(int y=0;y<mapHeight;y++){
  139. for(int x=0;x<mapWidth;x++){
  140. if(map[x][y]==0){ // water
  141. DrawRectangle(screenWidth-105+x,y+32,1,1,BLUE);
  142. }
  143. if(map[x][y]>0){
  144. if(map[x][y]>0){//Draw the beach
  145. DrawRectangle(screenWidth-105+x,y+32,1,1,(Color){250,220,0,255});
  146. }
  147. if(map[x][y]>3){//If the height is larger than 3 than draw trees
  148. DrawRectangle(screenWidth-105+x,y+32,1,1,(Color){0,210,0,255});
  149. }
  150. if(map[x][y]>15){ // here we draw some more trees
  151. DrawRectangle(screenWidth-105+x,y+32,1,1,(Color){0,170,0,255});
  152. }
  153. if(map[x][y]>20){ // draw mountains or rocks
  154. DrawRectangle(screenWidth-105+x,y+32,1,1,(Color){110,110,110,255});
  155. }
  156. }
  157. }
  158. }
  159. //draw selection
  160. DrawRectangleLines(screenWidth-105+mapx,32+mapy,17,14,WHITE);
  161. };
  162. // Rectangles overlap
  163. bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
  164. if(x1 >= (x2 + w2) || (x1 + w1) <= x2) return false;
  165. if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false;
  166. return true;
  167. }