Xtexture.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "raylib.h"
  2. #include <math.h>
  3. float direction(float x, float y);
  4. // Return the angle from - to in float
  5. float getangledeg(float x1,float y1,float x2,float y2);
  6. void createXtexture();
  7. RenderTexture2D target;
  8. int tw = 640;
  9. int th = 480;
  10. int main(void)
  11. {
  12. // Initialization
  13. //--------------------------------------------------------------------------------------
  14. const int screenWidth = 800;
  15. const int screenHeight = 600;
  16. //tw=screenWidth;
  17. //th=screenHeight;
  18. InitWindow(screenWidth, screenHeight, "raylib example.");
  19. //ToggleFullscreen();
  20. createXtexture();
  21. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  22. //--------------------------------------------------------------------------------------
  23. int x1=200;
  24. int y1=200;
  25. int x2=0;
  26. int y2=0;
  27. float an=0;
  28. int val=0;
  29. // Main game loop
  30. while (!WindowShouldClose()) // Detect window close button or ESC key
  31. {
  32. // Update
  33. //----------------------------------------------------------------------------------
  34. //an+=.01;
  35. //x2 = x1+cos(an)*64;
  36. //y2 = y1+sin(an)*64;
  37. //if(an>PI*2.0f)an=0;
  38. val++;
  39. if(val>200){
  40. val=0;
  41. createXtexture();
  42. }
  43. //----------------------------------------------------------------------------------
  44. // Draw
  45. //----------------------------------------------------------------------------------
  46. BeginDrawing();
  47. ClearBackground(RAYWHITE);
  48. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  49. //void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint);
  50. DrawTexturePro(target.texture, (Rectangle){ 0, 0,tw,-th },
  51. (Rectangle){ 0,0,screenWidth,screenHeight}, (Vector2){ 0,0 },0, WHITE);
  52. //float n = direction(cos(an),sin(an));//getangledeg(x1,y1,x2,y2); //*(180/PI));//direction(0.0f,0.0f);
  53. //float n = direction(0,-1);
  54. //DrawText(FormatText("%f",n),10,10,20,RED);
  55. //DrawLine(x1,y1,x2,y2,RED);
  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 createXtexture(){
  67. // A map array for getting the tiny spots on the canvas
  68. int map[tw][th];
  69. for(int y=0;y<th;y++){
  70. for(int x=0;x<tw;x++){
  71. map[x][y]=0;
  72. }}
  73. // Create some small line parts on the map
  74. for(int i=0;i<tw*2;i++){
  75. float x1 = GetRandomValue(0,tw);
  76. float y1 = GetRandomValue(0,th);
  77. float x = x1;
  78. float y = y1;
  79. float an=PI*2.0f/2;//GetRandomValue(0,360)*DEG2RAD;
  80. int len=GetRandomValue(1,3);
  81. if(GetRandomValue(0,10)<2)len*=4;
  82. for(int j=0;j<len;j++){
  83. x+=cos(an)*2;
  84. y+=sin(an)*2;
  85. //an=GetRandomValue(0,360)*DEG2RAD;
  86. an+=.5;
  87. if(x>=0 && y>=0 && x<tw && y<th)
  88. map[(int)x][(int)y]=1;
  89. }
  90. // here we add some lighter line parts nearby
  91. if(GetRandomValue(0,10)<8){
  92. x = x1+1;
  93. y = y1+1;
  94. an=PI*2.0f/2;
  95. len/=2;
  96. for(int j=0;j<len;j++){
  97. x+=cos(an)*1;
  98. y+=sin(an)*1;
  99. //an=GetRandomValue(0,360)*DEG2RAD;
  100. an+=.5;
  101. if(x>=0 && y>=0 && x<tw && y<th)
  102. map[(int)x][(int)y]=2;
  103. }
  104. }
  105. }
  106. // grow the spots on the map
  107. for(int i=0;i<tw*th;i++){
  108. int x=GetRandomValue(1,tw-2);
  109. int y=GetRandomValue(1,th-2);
  110. if(map[x][y]>0){
  111. int nx=GetRandomValue(-1,1);
  112. int ny=GetRandomValue(-1,1);
  113. map[x+nx][y+ny]=map[x][y];
  114. }
  115. }
  116. // add some light dots with shadow
  117. for(int i=0;i<tw/2;i++){
  118. int x = GetRandomValue(2,tw-2);
  119. int y = GetRandomValue(2,th-2);
  120. map[x][y]=3;
  121. map[x+1][y+1]=2;
  122. }
  123. // our rendertexture
  124. target = LoadRenderTexture(tw,th);
  125. // Clear our texture(image) before entering the game loop
  126. BeginTextureMode(target);
  127. //ClearBackground(DARKPURPLE);
  128. //DrawCircle(0,0, 100, YELLOW);
  129. // These are the colors we use
  130. Color back=(Color){75,50,80,255};
  131. Color high=(Color){80,75,99,255};
  132. Color low=(Color){30,50,70,255};
  133. ClearBackground(back);
  134. // Here we draw the data we created in the mapp[][]
  135. for(int y=0;y<th;y++){
  136. for(int x=0;x<tw;x++){
  137. // This is the noise
  138. if(GetRandomValue(0,10)<9){
  139. Color tc;
  140. int val = GetRandomValue(-50,50);
  141. if(GetRandomValue(0,10)<5)val/=2;
  142. if(GetRandomValue(0,10)<9)val=GetRandomValue(-5,5);
  143. tc.r = back.r+val;
  144. tc.g = back.g+val;
  145. tc.b = back.b+val;
  146. tc.a = 255;
  147. if(tc.r<0)tc.r=0;
  148. if(tc.g<0)tc.g=0;
  149. if(tc.b<0)tc.b=0;
  150. if(tc.r>255)tc.r=255;
  151. if(tc.g>255)tc.g=255;
  152. if(tc.b>255)tc.b=255;
  153. DrawPixel(x,y,tc);
  154. }
  155. if(map[x][y]==1)
  156. DrawPixel(x,y,low);
  157. if(map[x][y]==2)
  158. DrawPixel(x,y,high);
  159. if(map[x][y]==3)
  160. DrawPixel(x,y,BLACK);
  161. }}
  162. // here we create the cracks!!
  163. for(int j=0;j<tw/3;j++){
  164. int len=GetRandomValue(5,tw/2);
  165. if(GetRandomValue(0,20)<10)len=GetRandomValue(5,10);
  166. float angle=(float)(GetRandomValue(0,360)*DEG2RAD);
  167. float x=GetRandomValue(0,tw);
  168. float y=GetRandomValue(0,th);
  169. for(int i=0 ;i<len;i++){
  170. int val=GetRandomValue(-20,20);
  171. Color high2 = (Color){high.r-val,high.g-val,high.b-val,255};
  172. int val2=GetRandomValue(0,20);
  173. Color low2 = (Color){low.r+val2,low.g+val2,low.b+val2,255};
  174. int val3=GetRandomValue(0,20);
  175. Color BLACK2 = (Color){val3,val3,val3,255};
  176. DrawPixel(x,y,BLACK2);
  177. if(GetRandomValue(0,10)<5)DrawPixel(x,y,low2);
  178. int d=direction(cos(angle),sin(angle));
  179. if(d==0)// || d==3 || d==4 || d==5 || d==7)
  180. if(GetRandomValue(0,10)<2)DrawPixel(x,y+1,high2);
  181. if(d==1)
  182. if(GetRandomValue(0,10)<2)DrawPixel(x+1,y,high2);
  183. if(d==2)
  184. DrawPixel(x+1,y,high2);
  185. if(d==3)
  186. DrawPixel(x+1,y,high2);
  187. if(d==4)
  188. DrawPixel(x,y+1,high2);
  189. if(GetRandomValue(0,10)<2)DrawPixel(x,y-1,BLACK2);
  190. //if(d==5)
  191. // DrawPixel(x,y,YELLOW);
  192. if(d==6)
  193. DrawPixel(x+1,y,high2);
  194. if(d==7)
  195. DrawPixel(x,y+1,high2);
  196. x+=cos(angle)*1;
  197. y+=sin(angle)*1;
  198. if(GetRandomValue(0,20)<2)
  199. angle+=(float)(GetRandomValue(-90,90)*DEG2RAD);
  200. //angle+=.05;
  201. }
  202. }
  203. // Draw something on it.
  204. EndTextureMode(); // This needs to be called after every different draw command used. Do not forget to use begintexture also..
  205. }
  206. //
  207. // Takes two float numbers x and y -,+
  208. // Returns right=0;rightdown1;down=2....
  209. float direction(float x, float y){
  210. float angle = (float)atan2(y-0.0f, x-0.0f);
  211. angle*=RAD2DEG;
  212. if (angle < 0)
  213. {
  214. angle+=360;
  215. }
  216. int i = (angle + 22.50)/45;
  217. return i % 8;
  218. }
  219. // Return the angle from - to in float
  220. float getangledeg(float x1,float y1,float x2,float y2){
  221. float angle = (float)atan2(y2-y1, x2-x1);
  222. angle*=RAD2DEG;
  223. if (angle < 0)
  224. {
  225. angle+=360;
  226. }
  227. return angle;
  228. }