wip_-_infinite_scroll.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // Trying to figure out how to do larger terrain by generating at the edges - work in progress
  3. //
  4. //
  5. #include "raylib.h"
  6. #include <math.h>
  7. static int map[600][600];
  8. static int mapWidth = 600;
  9. static int mapHeight = 600;
  10. static float tileWidth;
  11. static float tileHeight;
  12. static const int screenWidth = 640;
  13. static const int screenHeight = 480;
  14. static void makemap(int px, int py);
  15. static void drawmap();
  16. static void drawmap2();
  17. static void genmap();
  18. static void edgemap();
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. InitWindow(screenWidth, screenHeight, "raylib example.");
  24. // We want to have the tile width and height fill the entire screen.
  25. tileWidth = 1;//ceil((float)screenWidth / (float)mapWidth);
  26. tileHeight = 1;//ceil((float)screenHeight / (float)mapHeight);
  27. genmap();
  28. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  29. //--------------------------------------------------------------------------------------
  30. int scrolly=0;
  31. // Main game loop
  32. while (!WindowShouldClose()) // Detect window close button or ESC key
  33. {
  34. // Update
  35. //----------------------------------------------------------------------------------
  36. if(IsKeyDown(KEY_SPACE)){
  37. genmap();
  38. }
  39. //if(IsKeyPressed(KEY_A)){
  40. for(int y=3;y<mapHeight;y++){
  41. for(int x=0;x<mapWidth;x++){
  42. map[x][y-3] = map[x][y];
  43. }
  44. }
  45. scrolly++;
  46. if(scrolly>16){
  47. edgemap();
  48. scrolly=0;
  49. }
  50. //}
  51. //----------------------------------------------------------------------------------
  52. // Draw
  53. //----------------------------------------------------------------------------------
  54. BeginDrawing();
  55. ClearBackground(RAYWHITE);
  56. drawmap2();
  57. EndDrawing();
  58. //----------------------------------------------------------------------------------
  59. }
  60. // De-Initialization
  61. //--------------------------------------------------------------------------------------
  62. CloseWindow(); // Close window and OpenGL context
  63. //--------------------------------------------------------------------------------------
  64. return 0;
  65. }
  66. static void makemap(int px,int py){
  67. // First we create a couple of hills in the map
  68. // We create a random spot and draw a line there somewhat randomly
  69. // We create a line of some sort to create more realistic landmasses. :Experiment:
  70. int rx,ry;
  71. for(int i=0;i<40/4;i++){
  72. rx = GetRandomValue(-10,60-5);
  73. ry = GetRandomValue(-10,60-5);
  74. int mx=GetRandomValue(-1,1);
  75. int my=GetRandomValue(-1,1);
  76. int len=GetRandomValue(1,50/4);
  77. for(int ii=0;ii<len;ii++){
  78. if(GetRandomValue(0,10)<2){
  79. mx=GetRandomValue(-1,1);
  80. my=GetRandomValue(-1,1);
  81. }
  82. int nx=rx+mx;
  83. int ny=ry+my;
  84. //if(nx>0&&nx<55-1 && ny>0 && ny<55-1){
  85. if(nx+px<mapWidth && nx+px>-1 && ny+py<mapHeight && ny+py>-1){
  86. map[nx+px][ny+py]++;
  87. }
  88. //}
  89. }
  90. }
  91. // Now we want to pile sand ontop and right next to the current hills.
  92. // This will make them into larger hills. We do this by picking a random
  93. // spot. If there is something below that we increase the height there.
  94. int clunk=(50*50)*5;//GetRandomValue(2,3);
  95. for(int i=0;i<clunk;i++){
  96. int nx,ny;
  97. nx = GetRandomValue(-100,100-2);
  98. ny = GetRandomValue(-100,100-2);
  99. if(map[nx+px][ny+py]>0){
  100. for(int y=-1;y<2;y++){
  101. for(int x=-1;x<2;x++){
  102. if(nx+x+px>-1 && nx+x+px<mapWidth && ny+y+py>-1 && ny+y+py<mapHeight){
  103. map[nx+x+px][ny+y+py]++;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. };
  110. static void drawmap2(){
  111. int tileWidth2 = tileWidth*2;
  112. int tileHeight2 = tileHeight*2;
  113. int mapWidth2 = mapWidth/2;
  114. int mapHeight2 = mapHeight/2;
  115. for(int y=0;y<mapHeight2;y++){
  116. for(int x=0;x<mapWidth2+20;x++){
  117. int value = map[x+96][y];
  118. if(value==0){ // water
  119. DrawRectangle(x*tileWidth2,y*tileHeight2,tileWidth2,tileHeight2,BLUE);
  120. }
  121. if(value>0){
  122. if(value>0){//Draw the beach
  123. DrawRectangle(x*tileWidth2,y*tileHeight2,tileWidth2,tileHeight2,(Color){250,220,0,255});
  124. }
  125. if(value>3){//If the height is larger than 3 than draw trees
  126. DrawRectangle(x*tileWidth2,y*tileHeight2,tileWidth2,tileHeight2,(Color){0,210,0,255});
  127. }
  128. if(value>10){ // here we draw some more trees
  129. DrawRectangle(x*tileWidth2,y*tileHeight2,tileWidth2,tileHeight2,(Color){0,140,0,255});
  130. }
  131. if(value>22){ // draw mountains or rocks
  132. int col=(value-22)*4;
  133. DrawRectangle(x*tileWidth2,y*tileHeight2,tileWidth2,tileHeight2,(Color){110+col,110+col,110+col,255});
  134. }
  135. }
  136. }
  137. }
  138. };
  139. static void drawmap(){
  140. for(int y=0;y<mapHeight;y++){
  141. for(int x=0;x<mapWidth;x++){
  142. if(map[x][y]==0){ // water
  143. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLUE);
  144. }
  145. if(map[x][y]>0){
  146. if(map[x][y]>0){//Draw the beach
  147. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){250,220,0,255});
  148. }
  149. if(map[x][y]>3){//If the height is larger than 3 than draw trees
  150. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){0,210,0,255});
  151. }
  152. if(map[x][y]>10){ // here we draw some more trees
  153. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){0,140,0,255});
  154. }
  155. if(map[x][y]>22){ // draw mountains or rocks
  156. int col=(map[x][y]-22)*4;
  157. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,(Color){110+col,110+col,110+col,255});
  158. }
  159. }
  160. }
  161. }
  162. };
  163. //
  164. // Here we generate a new map part at the bottom part of the map
  165. // we shift the map up in memory to make place for that new part.
  166. // For scrolling back to the origal part we would need to save the map and
  167. // reload it.
  168. //
  169. //
  170. static void edgemap(){
  171. for(int y=8;y<9;y++){
  172. for(int x=2;x<9;x++){
  173. makemap(x*50,y*50);
  174. }}
  175. }
  176. static void genmap(){
  177. // Here we generate the map using the hill algorithm
  178. for(int y=0;y<mapHeight;y++){
  179. for(int x=0;x<mapWidth;x++){
  180. map[x][y]=0;
  181. }}
  182. for(int y=2;y<9;y++){
  183. for(int x=2;x<9;x++){
  184. makemap(x*50,y*50);
  185. }}
  186. };