shaders_ascii_rendering.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - ascii rendering
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.5, last time updated with raylib 5.6
  8. *
  9. * Example contributed by Maicon Santana (@maiconpintoabreu) 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) 2025-2025 Maicon Santana (@maiconpintoabreu)
  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 - ascii rendering");
  33. // Texture to test static drawing
  34. Texture2D fudesumi = LoadTexture("resources/fudesumi.png");
  35. // Texture to test moving drawing
  36. Texture2D raysan = LoadTexture("resources/raysan.png");
  37. // Load shader to be used on postprocessing
  38. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/ascii.fs", GLSL_VERSION));
  39. // These locations are used to send data to the GPU
  40. int resolutionLoc = GetShaderLocation(shader, "resolution");
  41. int fontSizeLoc = GetShaderLocation(shader, "fontSize");
  42. // Set the character size for the ASCII effect
  43. // Fontsize should be 9 or more
  44. float fontSize = 9.0f;
  45. // Send the updated values to the shader
  46. float resolution[2] = { (float)screenWidth, (float)screenHeight };
  47. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  48. Vector2 circlePos = (Vector2){40.0f, (float)screenHeight*0.5f};
  49. float circleSpeed = 1.0f;
  50. // RenderTexture to apply the postprocessing later
  51. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  52. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  53. //--------------------------------------------------------------------------------------
  54. // Main game loop
  55. while (!WindowShouldClose()) // Detect window close button or ESC key
  56. {
  57. // Update
  58. //----------------------------------------------------------------------------------
  59. if (IsKeyPressed(KEY_LEFT) && fontSize > 9.0) fontSize -= 1; // Reduce fontSize
  60. if (IsKeyPressed(KEY_RIGHT) && fontSize < 15.0) fontSize += 1; // Increase fontSize
  61. if (circlePos.x > 200.0f || circlePos.x < 40.0f) circleSpeed *= -1; // Revert speed
  62. circlePos.x += circleSpeed;
  63. // Set fontsize for the shader
  64. SetShaderValue(shader, fontSizeLoc, &fontSize, SHADER_UNIFORM_FLOAT);
  65. // Draw
  66. //----------------------------------------------------------------------------------
  67. BeginTextureMode(target);
  68. ClearBackground(WHITE); // The background of the scene itself
  69. // Samples Using custom shader
  70. DrawTexture(fudesumi, 500, -30, WHITE);
  71. DrawTextureV(raysan, circlePos, WHITE);
  72. EndTextureMode();
  73. BeginDrawing();
  74. ClearBackground(RAYWHITE);
  75. BeginShaderMode(shader);
  76. // Draw the scene texture (that we rendered earlier) to the screen
  77. // The shader will process every pixel of this texture
  78. DrawTextureRec(target.texture,
  79. (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height },
  80. (Vector2){ 0, 0 },
  81. WHITE);
  82. EndShaderMode();
  83. DrawRectangle(0, 0, screenWidth, 40, BLACK);
  84. DrawText(TextFormat("Ascii effect - FontSize:%2.0f - [Left] -1 [Right] +1 ", fontSize), 120, 10, 20, LIGHTGRAY);
  85. DrawFPS(10, 10);
  86. EndDrawing();
  87. //----------------------------------------------------------------------------------
  88. }
  89. // De-Initialization
  90. //--------------------------------------------------------------------------------------
  91. UnloadShader(shader); // Unload shader
  92. UnloadTexture(fudesumi); // Unload texture
  93. UnloadTexture(raysan); // Unload texture
  94. CloseWindow(); // Close window and OpenGL context
  95. //--------------------------------------------------------------------------------------
  96. return 0;
  97. }