From 4e3757bc1a4cac571a6139ce3414bcfb04e42298 Mon Sep 17 00:00:00 2001 From: Gianni Alessandroni <45824238+NotManyIdeasDev@users.noreply.github.com> Date: Sun, 9 May 2021 14:57:47 +0200 Subject: [PATCH] Added pixel-perfect camera example Added pixel-perfect camera example, both the .c file and the cover .png image. The example works with any resolution you want, as long as the ratio stays the same (ex. 16:9, 4:3) ecc. --- .../core/core_2d_camera_smooth_pixelperfect.c | 133 ++++++++++++++++++ .../core_2d_camera_smooth_pixelperfect.png | Bin 0 -> 6365 bytes 2 files changed, 133 insertions(+) create mode 100644 examples/core/core_2d_camera_smooth_pixelperfect.c create mode 100644 examples/core/core_2d_camera_smooth_pixelperfect.png diff --git a/examples/core/core_2d_camera_smooth_pixelperfect.c b/examples/core/core_2d_camera_smooth_pixelperfect.c new file mode 100644 index 000000000..46602e0ce --- /dev/null +++ b/examples/core/core_2d_camera_smooth_pixelperfect.c @@ -0,0 +1,133 @@ +/******************************************************************************************* +* +* raylib [core] example - smooth pixel-perfect camera +* +* This example has been created using raylib 3.7 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Example contributed by Giancamillo Alessandroni ([discord]NotManyIdeas#9972 - [github]NotManyIdeasDev) and +* reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2021 Giancamillo Alessandroni (NotManyIdeas#9972) and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" +#include + +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + const int virualScreenWidth = 160; + const int virtualScreenHeight = 90; + + const float virtualRatio = (float)screenWidth / (float)virualScreenWidth; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - smooth pixel-perfect camera"); + + Camera2D worldSpaceCamera = { 0 }; // Game world camera + worldSpaceCamera.zoom = 1.0f; + + Camera2D screenSpaceCamera = { 0 }; //Smoothing camera + screenSpaceCamera.zoom = 1.0f; + + RenderTexture2D renderTexture = LoadRenderTexture(virualScreenWidth, virtualScreenHeight); //This is where we'll draw all our objects. + + Rectangle firstRectangle = { 70.0f, 35.0f, 20.0f, 20.0f }; + Rectangle secondRectangle = { 90.0f, 55.0f, 30.0f, 10.0f }; + Rectangle thirdRectangle = { 80.0f, 65.0f, 15.0f, 25.0f }; + + Vector2 origin = { 0.0f, 0.0f }; + float rotation = 0.0f; + + float cameraX = 0.0f; + float cameraY = 0.0f; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + rotation++; // Rotate the rectangles. + + // Make the camera move to demonstrate the effect. + cameraX = (sin(GetTime()) * 50.0f) - 10.0f; + cameraY = cos(GetTime()) * 30.0f; + + screenSpaceCamera.target = { cameraX, cameraY }; + + if (screenSpaceCamera.target.x >= 1 || screenSpaceCamera.target.x <= -1) // Round worldCamera's X, keep the decimals on screenSpaceCamera. + { + worldSpaceCamera.target.x = (int)screenSpaceCamera.target.x; + screenSpaceCamera.target.x -= worldSpaceCamera.target.x; + screenSpaceCamera.target.x *= virtualRatio; + } + + if (screenSpaceCamera.target.y >= 1 || screenSpaceCamera.target.y <= -1) // Round worldCamera's Y, keep the decimals on screenSpaceCamera. + { + worldSpaceCamera.target.y = (int)screenSpaceCamera.target.y; + screenSpaceCamera.target.y -= worldSpaceCamera.target.y; + screenSpaceCamera.target.y *= virtualRatio; + } + + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + ClearBackground(RED); // This is for debug purposes. If you see red, then you've probably done something wrong. + + BeginTextureMode(renderTexture); + + BeginMode2D(worldSpaceCamera); + + ClearBackground(RAYWHITE); // This is the color you should see as background color. + + // Draw the rectangles + DrawRectanglePro(firstRectangle, origin, rotation, BLACK); + DrawRectanglePro(secondRectangle, origin, -rotation, RED); + DrawRectanglePro(thirdRectangle, origin, rotation + 45.0f, BLUE); + + EndMode2D(); + + EndTextureMode(); + + BeginMode2D(screenSpaceCamera); + + // Draw the render texture with an offset of 1 worldSpace unit/pixel, so that the content behind the renderTexture is not shown. + // Also the renderTexture's height is flipped (in the source Rectangle), due to OpenGL reasons. + DrawTexturePro( + renderTexture.texture, + { 0.0f, 0.0f, (float)renderTexture.texture.width, (float)-renderTexture.texture.height }, + { -virtualRatio, -virtualRatio, screenWidth + (virtualRatio * 2), screenHeight + (virtualRatio * 2) }, + { 0.0f, 0.0f }, + 0.0f, + WHITE + ); + + EndMode2D(); + + DrawText("Screen resolution: 800x450", 5, 0, 20, DARKBLUE); + DrawText("World resolution: 160x90", 5, 20, 20, DARKGREEN); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + + UnloadRenderTexture(renderTexture); // RenderTexture unloading + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/examples/core/core_2d_camera_smooth_pixelperfect.png b/examples/core/core_2d_camera_smooth_pixelperfect.png new file mode 100644 index 0000000000000000000000000000000000000000..aeac7944688c34c2a38b299721cf90915fb22084 GIT binary patch literal 6365 zcmeAS@N?(olHy`uVBq!ia0y~yU{+vYU_8XZ#=yWJp1k%10|NtNage(c!@6@aFBupZ zSkfJR9T^xl_H+M9WMyDr;4JWnEM{QfI}E~%$MaXDFfcF(mAFQf1m~xflqVLYGPoC| zrsfrA5WyH>IHl8sf#&&+VY=W%6r!H@ud51H-T<_Kl|P7bMNkx-*f)!ab@@Cw?Cf9 z*k`~0S53aj{_k}R2WI*G^?&jA_;dB!z5n0*y!YbIiF>bq|GfBh@n?nu#u^I$ubkih z_}+_}_m_W8Tnq}5+dtRdpZ)Jo#=e`?cQ5|j`|V!+x0?Ktx|H`{|NeRL3+&juNQX;1 zpS=0G`Th0V*7ld~=a>Aye=h&;y)Vz*KcBt#It>(xpn!x_^YdQDK2XHH{=RnS#J$(QeqQ|A{W)Vp`ixKWFS!5u zS!VO8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