CopperBars.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Copper bars. An background effect. Put a title or some text in front of it.
  3. //
  4. //
  5. #include "raylib.h"
  6. #include <math.h>
  7. void copperbars(float val, int y, int height,int screenWidth, Color col);
  8. static float Clamp(float value, float min, float max);
  9. int main(void)
  10. {
  11. // Initialization
  12. //--------------------------------------------------------------------------------------
  13. const int screenWidth = 800;
  14. const int screenHeight = 450;
  15. InitWindow(screenWidth, screenHeight, "raylib example.");
  16. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  17. //--------------------------------------------------------------------------------------
  18. float val=0;
  19. Color col=RED;
  20. col.a=GetRandomValue(100,255);
  21. int sw=0;
  22. // Main game loop
  23. while (!WindowShouldClose()) // Detect window close button or ESC key
  24. {
  25. // Update
  26. //----------------------------------------------------------------------------------
  27. // val is the speed of which the copperbars bounce on the screen.
  28. val+=.076;
  29. if(val>PI*2.0f)val=0;
  30. // Every 120 frames change to a different color
  31. sw++;
  32. if(sw>120){
  33. sw=0;
  34. col=(Color){GetRandomValue(0,255),GetRandomValue(0,255),GetRandomValue(0,255),GetRandomValue(100,255)};
  35. // Make sure we have a bright color.
  36. if(col.r<150 && col.g<150 && col.b<150)col.r=255;
  37. }
  38. //----------------------------------------------------------------------------------
  39. // Draw
  40. //----------------------------------------------------------------------------------
  41. BeginDrawing();
  42. ClearBackground(BLACK);
  43. //
  44. // value(0..PI*2.0f)
  45. // y location
  46. // height(height of screen taken)
  47. // width of screen
  48. // Color
  49. copperbars(val,0,240,screenWidth,col);
  50. for(int y=-3;y<3;y++){
  51. for(int x=-3;x<3;x++){
  52. DrawText("COPPER BARS",x+50,y+20,90,BLACK);
  53. }}
  54. DrawText("COPPER BARS",50,20,90,YELLOW);
  55. EndDrawing();
  56. //----------------------------------------------------------------------------------
  57. }
  58. // De-Initialization
  59. //--------------------------------------------------------------------------------------
  60. CloseWindow(); // Close window and OpenGL context
  61. //--------------------------------------------------------------------------------------
  62. return 0;
  63. }
  64. // value(0..PI*2.0f)
  65. // y location
  66. // height(height of screen taken)
  67. // width of screen
  68. // Color
  69. void copperbars(float val, int y, int height, int screenWidth, Color col){
  70. // Convert the height to fit the screen.
  71. height/=2.3;
  72. // Draw 8 bars
  73. for(int i=0;i<8;i++){
  74. // Draw the top of the bar
  75. for(int j=0;j<16;j++){
  76. int py=y+((sin(val)*height+height));
  77. DrawRectangle(0,py+j,screenWidth,1,(Color){ Clamp((col.r/16)*j,0,255),
  78. Clamp((col.g/16)*j,0,255),
  79. Clamp((col.b/16)*j,0,255),col.a});
  80. }
  81. // Draw the bottom of the bar
  82. for(int j=16;j>0;j--){
  83. int py=y+sin(val)*height+height+15;
  84. DrawRectangle(0,py+16-j,screenWidth,1,(Color){ Clamp((col.r/16)*j,0,255),
  85. Clamp((col.g/16)*j,0,255),
  86. Clamp((col.b/16)*j,0,255),col.a});
  87. }
  88. // Distance between the bars.
  89. val+=.35;
  90. }
  91. }
  92. // Clamp float value
  93. float Clamp(float value, float min, float max)
  94. {
  95. const float res = value < min ? min : value;
  96. return res > max ? max : res;
  97. }