|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib [shaders] example - ascii effect |
| 4 | +* |
| 5 | +* Example complexity rating: [★☆☆☆] 1/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 | + |
| 18 | +#include "raylib.h" |
| 19 | + |
| 20 | +#if defined(PLATFORM_DESKTOP) |
| 21 | + #define GLSL_VERSION 330 |
| 22 | +#else // PLATFORM_ANDROID, PLATFORM_WEB |
| 23 | + #define GLSL_VERSION 100 |
| 24 | +#endif |
| 25 | + |
| 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 | + |
| 36 | + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - ascii effect"); |
| 37 | + |
| 38 | + // Texture to test static drawing |
| 39 | + Texture2D fudesumi = LoadTexture("resources/fudesumi.png"); |
| 40 | + // Texture to test moving drawing |
| 41 | + Texture2D raysan = LoadTexture("resources/raysan.png"); |
| 42 | + |
| 43 | + // Load shader to be used on postprocessing |
| 44 | + Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/ascii.fs", GLSL_VERSION)); |
| 45 | + |
| 46 | + // These locations are used to send data to the GPU. |
| 47 | + int resolutionLoc = GetShaderLocation(shader, "resolution"); |
| 48 | + int fontSizeLoc = GetShaderLocation(shader, "fontSize"); |
| 49 | + |
| 50 | + // Set the character size for the ASCII effect |
| 51 | + float fontSize = 4.0f; |
| 52 | + |
| 53 | + // Send the updated values to the shader |
| 54 | + float resolution[2] = { (float)screenWidth, (float)screenHeight }; |
| 55 | + SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2); |
| 56 | + SetShaderValue(shader, fontSizeLoc, &fontSize, SHADER_UNIFORM_FLOAT); |
| 57 | + |
| 58 | + Vector2 circlePos = (Vector2){40.0f, (float)screenHeight * 0.5f}; |
| 59 | + float circleSpeed = 1.0f; |
| 60 | + |
| 61 | + // RenderTexture to apply the postprocessing later |
| 62 | + RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); |
| 63 | + |
| 64 | + SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 65 | + //-------------------------------------------------------------------------------------- |
| 66 | + |
| 67 | + // Main game loop |
| 68 | + while (!WindowShouldClose()) // Detect window close button or ESC key |
| 69 | + { |
| 70 | + // Update |
| 71 | + //---------------------------------------------------------------------------------- |
| 72 | + if (circlePos.x > 200.0f || circlePos.x < 40.0f) { |
| 73 | + circleSpeed *= -1; |
| 74 | + } |
| 75 | + circlePos.x += circleSpeed; |
| 76 | + |
| 77 | + // Draw |
| 78 | + //---------------------------------------------------------------------------------- |
| 79 | + |
| 80 | + BeginTextureMode(target); |
| 81 | + ClearBackground(WHITE); // The background of the scene itself |
| 82 | + |
| 83 | + DrawTexture(fudesumi, 500, -30, WHITE); // Using custom shader |
| 84 | + DrawTextureV(raysan, circlePos, WHITE); |
| 85 | + |
| 86 | + EndTextureMode(); |
| 87 | + BeginDrawing(); |
| 88 | + |
| 89 | + ClearBackground(RAYWHITE); |
| 90 | + |
| 91 | + BeginShaderMode(shader); |
| 92 | + |
| 93 | + // Draw the scene texture (that we rendered earlier) to the screen. |
| 94 | + // The shader will process every pixel of this texture. |
| 95 | + DrawTextureRec(target.texture, |
| 96 | + (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, |
| 97 | + (Vector2){ 0, 0 }, |
| 98 | + WHITE); |
| 99 | + EndShaderMode(); |
| 100 | + |
| 101 | + DrawRectangle(0, 0, screenWidth, 40, BLACK); |
| 102 | + DrawText("Ascii effect", 120, 10, 20, LIGHTGRAY); |
| 103 | + DrawFPS(10, 10); |
| 104 | + |
| 105 | + EndDrawing(); |
| 106 | + //---------------------------------------------------------------------------------- |
| 107 | + } |
| 108 | + |
| 109 | + // De-Initialization |
| 110 | + //-------------------------------------------------------------------------------------- |
| 111 | + UnloadShader(shader); // Unload shader |
| 112 | + UnloadTexture(fudesumi); // Unload texture |
| 113 | + UnloadTexture(raysan); // Unload texture |
| 114 | + |
| 115 | + CloseWindow(); // Close window and OpenGL context |
| 116 | + //-------------------------------------------------------------------------------------- |
| 117 | + |
| 118 | + return 0; |
| 119 | +} |
0 commit comments