From f989048bda60aeb74111ec687cd44ec92deacfe3 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 23 Jun 2021 01:16:21 +0200 Subject: [PATCH] Reviewed example --- .../core/core_2d_camera_smooth_pixelperfect.c | 90 ++++++++---------- .../core_2d_camera_smooth_pixelperfect.png | Bin 6365 -> 15832 bytes 2 files changed, 39 insertions(+), 51 deletions(-) diff --git a/examples/core/core_2d_camera_smooth_pixelperfect.c b/examples/core/core_2d_camera_smooth_pixelperfect.c index ae40cdfc..75ffe262 100644 --- a/examples/core/core_2d_camera_smooth_pixelperfect.c +++ b/examples/core/core_2d_camera_smooth_pixelperfect.c @@ -5,15 +5,16 @@ * 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 +* Example contributed by Giancamillo Alessandroni (@NotManyIdeasDev) and * reviewed by Ramon Santamaria (@raysan5) * -* Copyright (c) 2021 Giancamillo Alessandroni (NotManyIdeas#9972) and Ramon Santamaria (@raysan5) +* Copyright (c) 2021 Giancamillo Alessandroni (@NotManyIdeasDev) and Ramon Santamaria (@raysan5) * ********************************************************************************************/ #include "raylib.h" -#include + +#include // Required for: sinf(), cosf() int main(void) { @@ -22,33 +23,32 @@ int main(void) const int screenWidth = 800; const int screenHeight = 450; - const int virualScreenWidth = 160; + const int virtualScreenWidth = 160; const int virtualScreenHeight = 90; - const float virtualRatio = (float)screenWidth/(float)virualScreenWidth; + const float virtualRatio = (float)screenWidth/(float)virtualScreenWidth; InitWindow(screenWidth, screenHeight, "raylib [core] example - smooth pixel-perfect camera"); - Camera2D worldSpaceCamera = { 0 }; // Game world camera + Camera2D worldSpaceCamera = { 0 }; // Game world camera worldSpaceCamera.zoom = 1.0f; - Camera2D screenSpaceCamera = { 0 }; //Smoothing camera + Camera2D screenSpaceCamera = { 0 }; // Smoothing camera screenSpaceCamera.zoom = 1.0f; - RenderTexture2D renderTexture = LoadRenderTexture(virualScreenWidth, virtualScreenHeight); //This is where we'll draw all our objects. + RenderTexture2D target = LoadRenderTexture(virtualScreenWidth, 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 }; + Rectangle rec01 = { 70.0f, 35.0f, 20.0f, 20.0f }; + Rectangle rec02 = { 90.0f, 55.0f, 30.0f, 10.0f }; + Rectangle rec03 = { 80.0f, 65.0f, 15.0f, 25.0f }; - //The renderTexture's height is flipped (in the source Rectangle), due to OpenGL reasons. - Rectangle renderTextureSource = { 0.0f, 0.0f, (float)renderTexture.texture.width, (float)-renderTexture.texture.height }; - Rectangle renderTextureDest = { -virtualRatio, -virtualRatio, screenWidth + (virtualRatio*2), screenHeight + (virtualRatio*2) }; + // The target's height is flipped (in the source Rectangle), due to OpenGL reasons + Rectangle sourceRec = { 0.0f, 0.0f, (float)target.texture.width, -(float)target.texture.height }; + Rectangle destRec = { -virtualRatio, -virtualRatio, screenWidth + (virtualRatio*2), screenHeight + (virtualRatio*2) }; Vector2 origin = { 0.0f, 0.0f }; float rotation = 0.0f; - float degreesPerSecond = 60.0f; float cameraX = 0.0f; float cameraY = 0.0f; @@ -61,16 +61,16 @@ int main(void) { // Update //---------------------------------------------------------------------------------- - rotation += degreesPerSecond*GetFrameTime(); // Rotate the rectangles. + rotation += 60.0f*GetFrameTime(); // Rotate the rectangles, 60 degrees per second - // Make the camera move to demonstrate the effect. + // Make the camera move to demonstrate the effect cameraX = (sinf(GetTime())*50.0f) - 10.0f; cameraY = cosf(GetTime())*30.0f; - // Set the camera's target to the values computed above. + // Set the camera's target to the values computed above screenSpaceCamera.target = (Vector2){ cameraX, cameraY }; - //Round worldSpace coordinates, keep decimals into screenSpace coordinates. + // Round worldSpace coordinates, keep decimals into screenSpace coordinates worldSpaceCamera.target.x = (int)screenSpaceCamera.target.x; screenSpaceCamera.target.x -= worldSpaceCamera.target.x; screenSpaceCamera.target.x *= virtualRatio; @@ -83,47 +83,35 @@ int main(void) // 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(); + BeginTextureMode(target); + ClearBackground(RAYWHITE); + + BeginMode2D(worldSpaceCamera); + DrawRectanglePro(rec01, origin, rotation, BLACK); + DrawRectanglePro(rec02, origin, -rotation, RED); + DrawRectanglePro(rec03, origin, rotation + 45.0f, BLUE); + EndMode2D(); EndTextureMode(); + + BeginDrawing(); + ClearBackground(RED); - BeginMode2D(screenSpaceCamera); + BeginMode2D(screenSpaceCamera); + DrawTexturePro(target.texture, sourceRec, destRec, origin, 0.0f, WHITE); + EndMode2D(); - // Draw the render texture with an offset of 1 worldSpace unit/pixel, so that the content behind the renderTexture is not shown. - DrawTexturePro( - renderTexture.texture, - renderTextureSource, - renderTextureDest, - origin, - 0.0f, - WHITE - ); - - EndMode2D(); - - //Debug info - DrawText("Screen resolution: 800x450", 5, 0, 20, DARKBLUE); - DrawText("World resolution: 160x90", 5, 20, 20, DARKGREEN); - DrawFPS(screenWidth - 75, 0); + DrawText(TextFormat("Screen resolution: %ix%i", screenWidth, screenHeight), 10, 10, 20, DARKBLUE); + DrawText(TextFormat("World resolution: %ix%i", virtualScreenWidth, virtualScreenHeight), 10, 40, 20, DARKGREEN); + DrawFPS(GetScreenWidth() - 95, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - UnloadRenderTexture(renderTexture); // RenderTexture unloading - CloseWindow(); // Close window and OpenGL context + UnloadRenderTexture(target); // Unload render texture + + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/core/core_2d_camera_smooth_pixelperfect.png b/examples/core/core_2d_camera_smooth_pixelperfect.png index aeac7944688c34c2a38b299721cf90915fb22084..ba8d89b7c1e7d68f069a4c25488a2e0a8a698d22 100644 GIT binary patch literal 15832 zcmeHOeO!{~8V2MT8XD7OU@CenYdf=ORvl|iv9FPuTG=Xey&=wT@cZ$M95^x#xXfC=`MH9Mw7J$3O26c!B4B?)$p0 z`?~K3wg&~W%}lLLF&K=Qzn||M493VFgTa1FFb1!TT2fGh!7vW``%a&`K4i)Bp6)xF zgde$B;~)nOmskQeR~lqzmMA5W2fbk9wp8$jEu|HRgcUqO>L3@F)X%+GmX(9w1Rp3G zo+zcHOMMxd%)drg0aWKX*V--er3rK`UgkP9cVpNaXiB z%=XY%NyzmJ83i}DHLiDP`UWbEq~TdUT*ak*B(7-R-xlAiI(ndyVz!rmq;+;ab5WN= z3p4!tOYOuO-V2Sm$@zp(RNtCgd6u;?r=@5__k)TrL;Pd`7`I`JsrBq1I?9KI72WrA z$Ox?!ZI!yNQ(YBVA1n>I*+VNnQ(g(Tuml6~>I`HwDu7t6wG27eTp|{L5f}HF>YnT>`uG%Av;q}Bpi)*;_<`H zkKZ0q@^QkpwI7x~^zE{F*Kv@LH$4YqGqhB zv0jBF`?Xh$82|CB|Mq@ztqhLh(|9dWta#FWr=<^S)0mooxa0(cL1HJV`mo=sx)>x; zYF@*$9-QG|>f7kxGR4R>^91`P_|*dr*``aYHJaekiluOjRE_G#xK$tGS)X~aKJL~; zlNgydw-}gkZNS1RPw5~bl)0($vclviTPon3JfSTo5vIPb#RS1JTE4;uf)Ixai9{6E z)KVDAb{x1a9$jrUb=j!jN_kt`_r%;qk?-_qb`k%zI9@pyDf3jB-Z_+xWR3 zZ0@2+MyM|mPDt$nNN`iTql823aBP`#rFEYB-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