Reviewed example
This commit is contained in:
parent
49d2897b24
commit
f989048bda
@ -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 <math.h>
|
||||
|
||||
#include <math.h> // Required for: sinf(), cosf()
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@ -22,10 +23,10 @@ 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");
|
||||
|
||||
@ -35,20 +36,19 @@ int main(void)
|
||||
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,46 +83,34 @@ int main(void)
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(RED); // This is for debug purposes. If you see red, then you've probably done something wrong.
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
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);
|
||||
|
||||
DrawRectanglePro(rec01, origin, rotation, BLACK);
|
||||
DrawRectanglePro(rec02, origin, -rotation, RED);
|
||||
DrawRectanglePro(rec03, origin, rotation + 45.0f, BLUE);
|
||||
EndMode2D();
|
||||
EndTextureMode();
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(RED);
|
||||
|
||||
BeginMode2D(screenSpaceCamera);
|
||||
|
||||
// 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
|
||||
);
|
||||
|
||||
DrawTexturePro(target.texture, sourceRec, destRec, 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
|
||||
UnloadRenderTexture(target); // Unload render texture
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 6.2 KiB After Width: | Height: | Size: 16 KiB |
Loading…
x
Reference in New Issue
Block a user