|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib textures example - magnifying glass |
| 4 | +* |
| 5 | +* Example complexity rating: [★★★☆] 3/4 |
| 6 | +* |
| 7 | +* Example originally created with raylib 5.6, last time updated with raylib 5.6 |
| 8 | +* |
| 9 | +* Example contributed by Luke Vaughan (@badram) 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) 2026 Luke Vaughan (@badram) |
| 15 | +* |
| 16 | +********************************************************************************************/ |
| 17 | + |
| 18 | +#include "raylib.h" |
| 19 | +#include "rlgl.h" // for rlSetBlendFactorsSeparate() |
| 20 | + |
| 21 | +//------------------------------------------------------------------------------------ |
| 22 | +// Program main entry point |
| 23 | +//------------------------------------------------------------------------------------ |
| 24 | +int main(void) |
| 25 | +{ |
| 26 | + // Initialization |
| 27 | + //-------------------------------------------------------------------------------------- |
| 28 | + const int screenWidth = 800; |
| 29 | + const int screenHeight = 450; |
| 30 | + |
| 31 | + InitWindow(screenWidth, screenHeight, "raylib [textures] example - magnifying glass"); |
| 32 | + |
| 33 | + Texture2D bunny = LoadTexture("resources/raybunny.png"); |
| 34 | + Texture2D parrots = LoadTexture("resources/parrots.png"); |
| 35 | + |
| 36 | + // Use image draw to generate a mask texture instead of loading it from a file. |
| 37 | + Image circle = GenImageColor(256, 256, BLANK); |
| 38 | + ImageDrawCircle(&circle, 128, 128, 128, WHITE); |
| 39 | + Texture2D mask = LoadTextureFromImage(circle); // Copy the mask image from RAM to VRAM |
| 40 | + UnloadImage(circle); // Unload the image from RAM |
| 41 | + |
| 42 | + RenderTexture2D magnifiedWorld = LoadRenderTexture(256, 256); |
| 43 | + |
| 44 | + Camera2D camera = { 0 }; |
| 45 | + // Set magnifying glass zoom |
| 46 | + camera.zoom = 2; |
| 47 | + // Offset by half the size of the magnifying glass to counteract drawing the texture centered on the mouse position |
| 48 | + camera.offset = (Vector2){128, 128}; |
| 49 | + |
| 50 | + |
| 51 | + SetTargetFPS(60); |
| 52 | + //-------------------------------------------------------------------------------------- |
| 53 | + |
| 54 | + // Main game loop |
| 55 | + while (!WindowShouldClose()) // Detect window close button or ESC key |
| 56 | + { |
| 57 | + // Update |
| 58 | + //---------------------------------------------------------------------------------- |
| 59 | + Vector2 mPos = GetMousePosition(); |
| 60 | + camera.target = mPos; |
| 61 | + |
| 62 | + //---------------------------------------------------------------------------------- |
| 63 | + |
| 64 | + // Draw |
| 65 | + //---------------------------------------------------------------------------------- |
| 66 | + BeginDrawing(); |
| 67 | + |
| 68 | + ClearBackground(RAYWHITE); |
| 69 | + |
| 70 | + // Draw the normal version of the world |
| 71 | + DrawTexture(parrots, 144, 33, WHITE); |
| 72 | + DrawText("Use the magnifying glass to find hidden bunnies!", 154, 6, 20, BLACK); |
| 73 | + |
| 74 | + // Render to a the magnifying glass |
| 75 | + BeginTextureMode(magnifiedWorld); |
| 76 | + ClearBackground(RAYWHITE); |
| 77 | + |
| 78 | + BeginMode2D(camera); |
| 79 | + // Draw the same things in the magnified world as were in the normal version |
| 80 | + DrawTexture(parrots, 144, 33, WHITE); |
| 81 | + DrawText("Use the magnifying glass to find hidden bunnies!", 154, 6, 20, BLACK); |
| 82 | + |
| 83 | + // Draw bunnies only in the magnified world. |
| 84 | + // BLEND_MULTIPLIED lets them take on the color of the image below them. |
| 85 | + BeginBlendMode(BLEND_MULTIPLIED); |
| 86 | + DrawTexture(bunny, 250, 350, WHITE); |
| 87 | + DrawTexture(bunny, 500, 100, WHITE); |
| 88 | + DrawTexture(bunny, 420, 300, WHITE); |
| 89 | + DrawTexture(bunny, 650, 10, WHITE); |
| 90 | + EndBlendMode(); |
| 91 | + EndMode2D(); |
| 92 | + |
| 93 | + // Mask the magnifying glass view texture to a circle |
| 94 | + // To make the mask affect only alpha, a CUSTOM blend mode is used with SEPARATE color/alpha functions |
| 95 | + BeginBlendMode(BLEND_CUSTOM_SEPARATE); |
| 96 | + // C: Color, A: Alpha, s: source (texture to draw), d: destination (texture drawn to) |
| 97 | + // glSrcRGB: RL_ZERO - Cs * 0 = 0 - discard source rgb because we don't want to draw our texture's colors at all |
| 98 | + // glDstRGB: RL_ONE - Cd * 1 = Cd - use destination colors unmodified |
| 99 | + // glSrcAlpha: RL_ONE - As * 1 = As - use source alpha unmodified |
| 100 | + // glDstAlpha: RL_ZERO - Ad * 0 = 0 - discard destination alpha |
| 101 | + // glEqRGB: RL_FUNC_ADD - Cs(0) + Cd = Cd - destination color is unmodified |
| 102 | + // glEqAlpha: RL_FUNC_ADD - As + Ad(0) = As - destination alpha is set to source alpha |
| 103 | + rlSetBlendFactorsSeparate(RL_ZERO, RL_ONE, RL_ONE, RL_ZERO, RL_FUNC_ADD, RL_FUNC_ADD); |
| 104 | + DrawTexture(mask, 0, 0, WHITE); |
| 105 | + EndBlendMode(); |
| 106 | + EndTextureMode(); |
| 107 | + |
| 108 | + // Draw magnifiedWorld to screen, centered on cursor |
| 109 | + DrawTextureRec(magnifiedWorld.texture, (Rectangle){0, 0, 256, -256}, (Vector2){mPos.x - 128, mPos.y - 128}, WHITE); |
| 110 | + |
| 111 | + // Draw the outer ring of the magnifying glass |
| 112 | + DrawRing(mPos, 126, 130, 0, 360, 64, BLACK); |
| 113 | + |
| 114 | + // Draw floating specular highlight on the glass |
| 115 | + float rx = mPos.x/800; |
| 116 | + float ry = mPos.y/800; |
| 117 | + DrawCircle((int)(mPos.x - 64*rx) - 32, (int)(mPos.y - 64*ry) - 32, 4, ColorAlpha(WHITE, 0.5)); |
| 118 | + |
| 119 | + EndDrawing(); |
| 120 | + //---------------------------------------------------------------------------------- |
| 121 | + } |
| 122 | + |
| 123 | + // De-Initialization |
| 124 | + //-------------------------------------------------------------------------------------- |
| 125 | + UnloadTexture(parrots); |
| 126 | + UnloadTexture(bunny); |
| 127 | + UnloadTexture(mask); |
| 128 | + UnloadRenderTexture(magnifiedWorld); |
| 129 | + |
| 130 | + CloseWindow(); // Close window and OpenGL context |
| 131 | + //-------------------------------------------------------------------------------------- |
| 132 | + |
| 133 | + return 0; |
| 134 | +} |
0 commit comments