Texture_Stone6.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Work in PRogress - todo put image map data into a texture and draw to screen - test conversion.
  2. //
  3. #define MAPWIDTH 800
  4. #define MAPHEIGHT 450
  5. #include "raylib.h"
  6. #include <math.h>
  7. static int mapr[MAPWIDTH][MAPHEIGHT];
  8. static int mapg[MAPWIDTH][MAPHEIGHT];
  9. static int mapb[MAPWIDTH][MAPHEIGHT];
  10. void mapsetcolor(int r,int g,int b);
  11. static float Clamp(float value, float min, float max);
  12. static void makelines(void);
  13. static void makelinesolid(float x,float y,float dist,int r,int g,int b);
  14. static void makelineshade(float x,float y,float dist);
  15. static void lightenrange(int low,int high);
  16. static void highsmoothrange(int low,int high,float perc);
  17. static void darkenrange(int low,int high,int ranval);
  18. static void smooth();
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib example. Please wait with Slow computer!!");
  26. RenderTexture2D target = LoadRenderTexture(MAPWIDTH, MAPHEIGHT);
  27. BeginTextureMode(target);
  28. ClearBackground(BLANK); // Make the entire Sprite Transparent.
  29. EndTextureMode();
  30. mapsetcolor(255,255,255);
  31. makelines();
  32. highsmoothrange(150,255,10);
  33. darkenrange(0,71,50);
  34. lightenrange(100,256);
  35. smooth();
  36. BeginTextureMode(target);
  37. for(int y=0;y<MAPHEIGHT;y++){
  38. for(int x=0;x<MAPWIDTH;x++){
  39. DrawRectangle(x,y,1,1,(Color){mapr[x][y],mapg[x][y],mapb[x][y],255});
  40. }
  41. }
  42. EndTextureMode();
  43. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. //----------------------------------------------------------------------------------
  51. // Draw
  52. //----------------------------------------------------------------------------------
  53. BeginDrawing();
  54. ClearBackground(RAYWHITE);
  55. DrawTextureEx(target.texture,(Vector2){0,0},0,1, WHITE);
  56. EndDrawing();
  57. //----------------------------------------------------------------------------------
  58. }
  59. // De-Initialization
  60. //--------------------------------------------------------------------------------------
  61. UnloadRenderTexture(target); // Unload render texture
  62. CloseWindow(); // Close window and OpenGL context
  63. //--------------------------------------------------------------------------------------
  64. return 0;
  65. }
  66. void mapsetcolor(int r,int g,int b){
  67. for(int y=0;y<MAPHEIGHT;y++){
  68. for(int x=0;x<MAPWIDTH;x++){
  69. mapr[x][y] = r;
  70. mapg[x][y] = g;
  71. mapb[x][y] = b ;
  72. }}
  73. }
  74. void makelines(){
  75. for(int i=0;i<(MAPWIDTH*MAPHEIGHT/10);i++){
  76. int x=GetRandomValue(0,MAPWIDTH-1);
  77. int y=GetRandomValue(0,MAPHEIGHT-1);
  78. int dist=GetRandomValue(2,12-1);
  79. if(GetRandomValue(0,10-1)<2){
  80. makelinesolid(x,y,dist,GetRandomValue(0,55),GetRandomValue(0,55),GetRandomValue(0,55));
  81. }
  82. makelineshade(x,y,dist);
  83. }
  84. }
  85. void makelinesolid(float x,float y,float dist,int r,int g,int b){
  86. int angle=GetRandomValue(0,360);
  87. for(float i = 0;i<dist;i+=0.2){
  88. int beh=GetRandomValue(0,9);
  89. if (beh<5)
  90. {
  91. x+=cos(angle);
  92. y+=sin(angle);
  93. }
  94. else if (beh>4 && beh<8)
  95. {
  96. angle-=GetRandomValue(1,15-1);
  97. if(angle<0)angle = 360-angle;
  98. x+=cos(angle);
  99. y+=sin(angle);
  100. }
  101. else
  102. {
  103. angle+=GetRandomValue(1,15-1);
  104. if(angle>359)angle = 0+angle;
  105. x+=cos(angle);
  106. y+=sin(angle);
  107. }
  108. if(x>-1 && y>-1 && x<MAPWIDTH && y<MAPHEIGHT){
  109. mapr[(int)x][(int)y] = r;
  110. mapg[(int)x][(int)y] = g;
  111. mapb[(int)x][(int)y] = b ;
  112. }
  113. }
  114. }
  115. void makelineshade(float x,float y,float dist){
  116. int angle=GetRandomValue(0,360);
  117. for(float i = 0;i<dist;i+=0.2){
  118. int beh=GetRandomValue(0,10-1);
  119. if(beh<5){
  120. x+=cos(angle);
  121. y+=sin(angle);
  122. }
  123. else if(beh>4 && beh<8){
  124. angle-=GetRandomValue(1,15-1);
  125. if(angle<0)angle= 360-angle;
  126. x+=cos(angle);
  127. y+=sin(angle);
  128. }
  129. else
  130. {
  131. angle+=GetRandomValue(1,15-1);
  132. if(angle>359) angle = 0+angle;
  133. x+=cos(angle);
  134. y+=sin(angle);
  135. }
  136. if(x>-1 && y>-1 && x<MAPWIDTH && y<MAPHEIGHT){
  137. int valr=mapr[(int)x][(int)y] / 1.2;
  138. int valg=mapg[(int)x][(int)y] / 1.2;
  139. int valb=mapb[(int)x][(int)y] / 1.2;
  140. valr = Clamp(valr,0,255);
  141. valg = Clamp(valg,0,255);
  142. valb = Clamp(valb,0,255);
  143. mapr[(int)x][(int)y] = valr;
  144. mapg[(int)x][(int)y] = valg;
  145. mapb[(int)x][(int)y] = valb;
  146. }
  147. }
  148. }
  149. void lightenrange(int low,int high){
  150. for(int i=0;i<(MAPWIDTH*MAPHEIGHT)*4;i++){
  151. int x1=GetRandomValue(1,MAPWIDTH-2);
  152. int y1=GetRandomValue(1,MAPHEIGHT-2);
  153. int cnt=0;
  154. for(int y=-1;y<2;y++){
  155. for(int x=-1;x<2;x++){
  156. if(mapr[x1+x][y1+y] > low && mapr[x1+x][y1+y] < high){
  157. if(mapg[x1+x][y1+y] > low && mapg[x1+x][y1+y] < high){
  158. if(mapb[x1+x][y1+y] > low && mapb[x1+x][y1+y] < high) {
  159. cnt+=1;
  160. }
  161. }
  162. }
  163. }}
  164. if(cnt>7){
  165. mapr[x1][y1] = Clamp(mapr[x1][y1] + 20,0,255);
  166. mapg[x1][y1] = Clamp(mapg[x1][y1] + 20,0,255);
  167. mapb[x1][y1] = Clamp(mapb[x1][y1] + 20,0,255);
  168. }
  169. }
  170. }
  171. void highsmoothrange(int low,int high,float perc){
  172. for(int i=0;i<(MAPWIDTH*MAPHEIGHT)*perc;i++){
  173. int x=GetRandomValue(1,MAPWIDTH-1);
  174. int y=GetRandomValue(1,MAPHEIGHT-1);
  175. int r1=mapr[x][y];
  176. int g1=mapg[x][y];
  177. int b1=mapb[x][y];
  178. int r2=mapr[x+1][y];
  179. int g2=mapg[x+1][y];
  180. int b2=mapb[x+1][y];
  181. int r3=mapr[x][y+1];
  182. int g3=mapg[x][y+1];
  183. int b3=mapb[x][y+1];
  184. int r4=mapr[x+1][y+1];
  185. int g4=mapg[x+1][y+1];
  186. int b4=mapb[x+1][y+1];
  187. if(r1<high && g1<high && b1<high){
  188. if(r1>low && g1>low && b1>low){
  189. int valr=((r1+r2+r3+r4)/4)*1.2;
  190. int valg=((g1+g2+g3+g4)/4)*1.2;
  191. int valb=((b1+b2+b3+b4)/4)*1.2;
  192. valr = Clamp(valr,0,255);
  193. valg = Clamp(valg,0,255);
  194. valb = Clamp(valb,0,255);
  195. mapr[x][y] = valr;
  196. mapg[x][y] = valg;
  197. mapb[x][y] = valb;
  198. }}
  199. }
  200. }
  201. void darkenrange(int low,int high,int ranval){
  202. for(int y=0;y<MAPHEIGHT;y++){
  203. for(int x=0;x<MAPWIDTH;x++){
  204. if(mapr[x][y] > low){
  205. mapr[x][y] -= GetRandomValue(ranval/2,ranval-1);
  206. mapg[x][y] -= GetRandomValue(ranval/2,ranval-1);
  207. mapb[x][y] -= GetRandomValue(ranval/2,ranval-1);
  208. }
  209. if(mapg[x][y] > low){
  210. mapr[x][y] -= GetRandomValue(ranval/2,ranval-1);
  211. mapg[x][y] -= GetRandomValue(ranval/2,ranval-1);
  212. mapb[x][y] -= GetRandomValue(ranval/2,ranval-1);
  213. }
  214. if(mapb[x][y] > low){
  215. mapr[x][y] -= GetRandomValue(ranval/2,ranval-1);
  216. mapg[x][y] -= GetRandomValue(ranval/2,ranval-1);
  217. mapb[x][y] -= GetRandomValue(ranval/2,ranval-1);
  218. }
  219. mapr[x][y] = Clamp(mapr[x][y],0,255);
  220. mapg[x][y] = Clamp(mapg[x][y],0,255);
  221. mapb[x][y] = Clamp(mapb[x][y],0,255);
  222. }}
  223. }
  224. void smooth(){
  225. for( int i=0;i< (MAPWIDTH*MAPHEIGHT);i++){
  226. int x=GetRandomValue(1,MAPWIDTH-1);
  227. int y=GetRandomValue(1,MAPHEIGHT-1);
  228. int col1r=mapr[x+1][y];
  229. int col1g=mapg[x+1][y];
  230. int col1b=mapb[x+1][y];
  231. int col2r=mapr[x+1][y+1];
  232. int col2g=mapg[x+1][y+1];
  233. int col2b=mapb[x+1][y+1];
  234. int col3r=mapr[x][y+1];
  235. int col3g=mapg[x][y+1];
  236. int col3b=mapb[x][y+1];
  237. int col4r=(col1r+col2r+col3r)/3;
  238. int col4g=(col1g+col2g+col3g)/3;
  239. int col4b=(col1b+col2b+col3b)/3;
  240. mapr[x][y] = col4r;
  241. mapg[x][y] = col4g;
  242. mapb[x][y] = col4b;
  243. mapr[x][y] = Clamp(mapr[x][y],0,255);
  244. mapg[x][y] = Clamp(mapg[x][y],0,255);
  245. mapb[x][y] = Clamp(mapb[x][y],0,255);
  246. }
  247. }
  248. // Clamp float value
  249. float Clamp(float value, float min, float max)
  250. {
  251. const float res = value < min ? min : value;
  252. return res > max ? max : res;
  253. }