You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply a shader to some shape or texture
  4. *
  5. * Example complexity rating: [] 2/4
  6. *
  7. * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  8. * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
  9. *
  10. * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
  11. * on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
  12. * raylib comes with shaders ready for both versions, check raylib/shaders install folder
  13. *
  14. * Example originally created with raylib 1.7, last time updated with raylib 3.7
  15. *
  16. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  17. * BSD-like license that allows static linking with closed source software
  18. *
  19. * Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
  20. *
  21. ********************************************************************************************/
  22. #include "raylib.h"
  23. #if defined(PLATFORM_DESKTOP)
  24. #define GLSL_VERSION 330
  25. #else // PLATFORM_ANDROID, PLATFORM_WEB
  26. #define GLSL_VERSION 100
  27. #endif
  28. //------------------------------------------------------------------------------------
  29. // Program main entry point
  30. //------------------------------------------------------------------------------------
  31. int main(void)
  32. {
  33. // Initialization
  34. //--------------------------------------------------------------------------------------
  35. const int screenWidth = 800;
  36. const int screenHeight = 450;
  37. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shapes and texture shaders");
  38. Texture2D fudesumi = LoadTexture("resources/fudesumi.png");
  39. // Load shader to be used on some parts drawing
  40. // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
  41. // NOTE 2: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  42. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION));
  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. // TODO: Update your variables here
  51. //----------------------------------------------------------------------------------
  52. // Draw
  53. //----------------------------------------------------------------------------------
  54. BeginDrawing();
  55. ClearBackground(RAYWHITE);
  56. // Start drawing with default shader
  57. DrawText("USING DEFAULT SHADER", 20, 40, 10, RED);
  58. DrawCircle(80, 120, 35, DARKBLUE);
  59. DrawCircleGradient(80, 220, 60, GREEN, SKYBLUE);
  60. DrawCircleLines(80, 340, 80, DARKBLUE);
  61. // Activate our custom shader to be applied on next shapes/textures drawings
  62. BeginShaderMode(shader);
  63. DrawText("USING CUSTOM SHADER", 190, 40, 10, RED);
  64. DrawRectangle(250 - 60, 90, 120, 60, RED);
  65. DrawRectangleGradientH(250 - 90, 170, 180, 130, MAROON, GOLD);
  66. DrawRectangleLines(250 - 40, 320, 80, 60, ORANGE);
  67. // Activate our default shader for next drawings
  68. EndShaderMode();
  69. DrawText("USING DEFAULT SHADER", 370, 40, 10, RED);
  70. DrawTriangle((Vector2){430, 80},
  71. (Vector2){430 - 60, 150},
  72. (Vector2){430 + 60, 150}, VIOLET);
  73. DrawTriangleLines((Vector2){430, 160},
  74. (Vector2){430 - 20, 230},
  75. (Vector2){430 + 20, 230}, DARKBLUE);
  76. DrawPoly((Vector2){430, 320}, 6, 80, 0, BROWN);
  77. // Activate our custom shader to be applied on next shapes/textures drawings
  78. BeginShaderMode(shader);
  79. DrawTexture(fudesumi, 500, -30, WHITE); // Using custom shader
  80. // Activate our default shader for next drawings
  81. EndShaderMode();
  82. DrawText("(c) Fudesumi sprite by Eiden Marsal", 380, screenHeight - 20, 10, GRAY);
  83. EndDrawing();
  84. //----------------------------------------------------------------------------------
  85. }
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. UnloadShader(shader); // Unload shader
  89. UnloadTexture(fudesumi); // Unload texture
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }