Precomputed rectangles, time-based movement and whitespace fix

Moved the source and destination rectangles for the renderTexture into their own variables, modified the animation to be time-based instead of frame-based, fixed the bug with whitespaces.
This commit is contained in:
Gianni Alessandroni 2021-05-09 21:31:49 +02:00
parent 1cc6c02b62
commit 1a79ffd766

View File

@ -41,8 +41,14 @@ int main(void)
Rectangle secondRectangle = { 90.0f, 55.0f, 30.0f, 10.0f }; Rectangle secondRectangle = { 90.0f, 55.0f, 30.0f, 10.0f };
Rectangle thirdRectangle = { 80.0f, 65.0f, 15.0f, 25.0f }; Rectangle thirdRectangle = { 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) };
Vector2 origin = { 0.0f, 0.0f }; Vector2 origin = { 0.0f, 0.0f };
float rotation = 0.0f; float rotation = 0.0f;
float degreesPerSecond = 60.0f;
float cameraX = 0.0f; float cameraX = 0.0f;
float cameraY = 0.0f; float cameraY = 0.0f;
@ -55,28 +61,30 @@ int main(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
rotation++; // Rotate the rectangles. rotation += degreesPerSecond * GetFrameTime(); // Rotate the rectangles.
// Make the camera move to demonstrate the effect. // Make the camera move to demonstrate the effect.
cameraX = (sin(GetTime()) * 50.0f) - 10.0f; cameraX = (sin(GetTime()) * 50.0f) - 10.0f;
cameraY = cos(GetTime()) * 30.0f; cameraY = cos(GetTime()) * 30.0f;
// Set the camera's target to the values computed above.
screenSpaceCamera.target = (Vector2){ cameraX, cameraY }; screenSpaceCamera.target = (Vector2){ cameraX, cameraY };
if (screenSpaceCamera.target.x >= 1 || screenSpaceCamera.target.x <= -1) // Round worldCamera's X, keep the decimals on screenSpaceCamera. // Round worldCamera's X, keep the decimals on screenSpaceCamera.
if (screenSpaceCamera.target.x >= 1 || screenSpaceCamera.target.x <= -1)
{ {
worldSpaceCamera.target.x = (int)screenSpaceCamera.target.x; worldSpaceCamera.target.x = (int)screenSpaceCamera.target.x;
screenSpaceCamera.target.x -= worldSpaceCamera.target.x; screenSpaceCamera.target.x -= worldSpaceCamera.target.x;
screenSpaceCamera.target.x *= virtualRatio; screenSpaceCamera.target.x *= virtualRatio;
} }
if (screenSpaceCamera.target.y >= 1 || screenSpaceCamera.target.y <= -1) // Round worldCamera's Y, keep the decimals on screenSpaceCamera. // Round worldCamera's Y, keep the decimals on screenSpaceCamera.
if (screenSpaceCamera.target.y >= 1 || screenSpaceCamera.target.y <= -1)
{ {
worldSpaceCamera.target.y = (int)screenSpaceCamera.target.y; worldSpaceCamera.target.y = (int)screenSpaceCamera.target.y;
screenSpaceCamera.target.y -= worldSpaceCamera.target.y; screenSpaceCamera.target.y -= worldSpaceCamera.target.y;
screenSpaceCamera.target.y *= virtualRatio; screenSpaceCamera.target.y *= virtualRatio;
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -85,9 +93,7 @@ int main(void)
ClearBackground(RED); // This is for debug purposes. If you see red, then you've probably done something wrong. ClearBackground(RED); // This is for debug purposes. If you see red, then you've probably done something wrong.
BeginTextureMode(renderTexture); BeginTextureMode(renderTexture);
BeginMode2D(worldSpaceCamera); BeginMode2D(worldSpaceCamera);
ClearBackground(RAYWHITE); // This is the color you should see as background color. ClearBackground(RAYWHITE); // This is the color you should see as background color.
// Draw the rectangles // Draw the rectangles
@ -96,45 +102,33 @@ int main(void)
DrawRectanglePro(thirdRectangle, origin, rotation + 45.0f, BLUE); DrawRectanglePro(thirdRectangle, origin, rotation + 45.0f, BLUE);
EndMode2D(); EndMode2D();
EndTextureMode(); EndTextureMode();
BeginMode2D(screenSpaceCamera); BeginMode2D(screenSpaceCamera);
// Draw the render texture with an offset of 1 worldSpace unit/pixel, so that the content behind the renderTexture is not shown. // 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( DrawTexturePro(
renderTexture.texture, renderTexture.texture,
(Rectangle) renderTextureSource,
{ renderTextureDest,
0.0f, 0.0f, (float)renderTexture.texture.width, (float)-renderTexture.texture.height origin,
},
(Rectangle)
{
-virtualRatio, -virtualRatio, screenWidth + (virtualRatio * 2), screenHeight + (virtualRatio * 2)
},
(Vector2)
{
0.0f, 0.0f
},
0.0f, 0.0f,
WHITE WHITE
); );
EndMode2D(); EndMode2D();
//Debug info
DrawText("Screen resolution: 800x450", 5, 0, 20, DARKBLUE); DrawText("Screen resolution: 800x450", 5, 0, 20, DARKBLUE);
DrawText("World resolution: 160x90", 5, 20, 20, DARKGREEN); DrawText("World resolution: 160x90", 5, 20, 20, DARKGREEN);
DrawFPS(screenWidth - 75, 0);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadRenderTexture(renderTexture); // RenderTexture unloading UnloadRenderTexture(renderTexture); // RenderTexture unloading
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------