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.

86 lines
3.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Texture drawing
  4. *
  5. * NOTE: This example illustrates how to draw into a blank texture using a shader
  6. *
  7. * Example originally created with raylib 2.0, last time updated with raylib 3.7
  8. *
  9. * Example contributed by Michał Ciesielski and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2019-2024 Michał Ciesielski and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #if defined(PLATFORM_DESKTOP)
  19. #define GLSL_VERSION 330
  20. #else // PLATFORM_ANDROID, PLATFORM_WEB
  21. #define GLSL_VERSION 100
  22. #endif
  23. //------------------------------------------------------------------------------------
  24. // Program main entry point
  25. //------------------------------------------------------------------------------------
  26. int main(void)
  27. {
  28. // Initialization
  29. //--------------------------------------------------------------------------------------
  30. const int screenWidth = 800;
  31. const int screenHeight = 450;
  32. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture drawing");
  33. Image imBlank = GenImageColor(1024, 1024, BLANK);
  34. Texture2D texture = LoadTextureFromImage(imBlank); // Load blank texture to fill on shader
  35. UnloadImage(imBlank);
  36. // NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
  37. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/cubes_panning.fs", GLSL_VERSION));
  38. float time = 0.0f;
  39. int timeLoc = GetShaderLocation(shader, "uTime");
  40. SetShaderValue(shader, timeLoc, &time, SHADER_UNIFORM_FLOAT);
  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. time = (float)GetTime();
  49. SetShaderValue(shader, timeLoc, &time, SHADER_UNIFORM_FLOAT);
  50. //----------------------------------------------------------------------------------
  51. // Draw
  52. //----------------------------------------------------------------------------------
  53. BeginDrawing();
  54. ClearBackground(RAYWHITE);
  55. BeginShaderMode(shader); // Enable our custom shader for next shapes/textures drawings
  56. DrawTexture(texture, 0, 0, WHITE); // Drawing BLANK texture, all magic happens on shader
  57. EndShaderMode(); // Disable our custom shader, return to default shader
  58. DrawText("BACKGROUND is PAINTED and ANIMATED on SHADER!", 10, 10, 20, MAROON);
  59. EndDrawing();
  60. //----------------------------------------------------------------------------------
  61. }
  62. // De-Initialization
  63. //--------------------------------------------------------------------------------------
  64. UnloadShader(shader);
  65. UnloadTexture(texture);
  66. CloseWindow(); // Close window and OpenGL context
  67. //--------------------------------------------------------------------------------------
  68. return 0;
  69. }