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 ae40cdfc1..75ffe262a 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 zcmeAS@N?(olHy`uVBq!ia0y~yU{+vYU_8XZ#=yWJp1k%11B2~+PZ!6Kin!!IzrMb% zZwoY#Vp^<-gDBx(Ty`-)j<@lmfCN^8v8wez278Lh+TOMU85})W2!oz72RGq^*d=?~ z4s76VyeK*0qKhpqXEavS7 zdoNz`JTD-j^^$kGb>Ufw3sJUfA9q}-P`tW-@y;9*7B}YvDDgs+X~Rs`q_>Bc&buPX zJEL~VE&nxkG4B+w+hrX){(|R3#y_0tW&;-y zZfkSF{nX37?1>xRTPjY8KiS)U(Wo41A&S+DjTIJW=3>Y|)imTOI(*{cSgHKOV17XM zS&MZOB(t(VWbSRNTe0S%dU-$l+DEfvz86fc@RC_uWO3&2juF9MZb3e6YtFhd@-g1etYL{~8-;78*DQKd}TM3mQjTiY&WJqBz;Xp-SouYviv(ciWS=`bc+=9R2Dkd53IwfpjHkx$SPH{n2^=lqXl=%afpDQusC=Cp4s*tbv9>L!9D)vuAq~uU=mt@2-q)FAK!BB6+Np zM&m_+1<@P?Z6n}eP$qlN_%P3a-TGDbVb4V}?yIW2YJJw#ekptIusk>G=k$nTw(1{; z?yz@@NBnXVKQ5a8WR<$9ERWguo@o~`qQW7MqbJWv-q#u|W5UUpBYkdTq{%MBS`)@T zPR_?kS*Ow2AuHWlg zXr%0ZV(wz&?!{ZtE42b%a9-GgGru@&7g+FHtt4x0qPl6zioIg*u>uG^@V^m6Z1}%?g;Zfy@OJ|=ans4+@>e=F8dGi zO5;H}CQvz=Sps!4EX%{W4SA6A3sQJ^a5XM8U~W3=ym+(HV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