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.

120 lines
4.9 KiB

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