瀏覽代碼

Added smooth pixel-perfect camera example + Small typo fix in examples_template.c (#1760)

* Typo fix

Changed "bsasic" to "basic" in the comments.

* 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.

* Fixed Typecasts

Fixed compiler errors (implicit conversions)

* 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.

* Fixed spacing and added more consistency with sinf() and cosf()

* Fixed *= operator spacing
pull/1766/head
Gianni Alessandroni 3 年之前
committed by GitHub
父節點
當前提交
4ab28fffb4
沒有發現已知的金鑰在資料庫的簽署中 GPG 金鑰 ID: 4AEE18F83AFDEB23
共有 3 個檔案被更改,包括 138 行新增2 行删除
  1. +136
    -0
      examples/core/core_2d_camera_smooth_pixelperfect.c
  2. 二進制
      examples/core/core_2d_camera_smooth_pixelperfect.png
  3. +2
    -2
      examples/examples_template.c

+ 136
- 0
examples/core/core_2d_camera_smooth_pixelperfect.c 查看文件

@ -0,0 +1,136 @@
/*******************************************************************************************
*
* 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 <math.h>
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 };
//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 };
float rotation = 0.0f;
float degreesPerSecond = 60.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 += degreesPerSecond*GetFrameTime(); // Rotate the rectangles.
// 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.
screenSpaceCamera.target = (Vector2){ cameraX, cameraY };
// 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;
screenSpaceCamera.target.x -= worldSpaceCamera.target.x;
screenSpaceCamera.target.x *= virtualRatio;
}
// 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;
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.
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);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadRenderTexture(renderTexture); // RenderTexture unloading
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

二進制
examples/core/core_2d_camera_smooth_pixelperfect.png 查看文件

Before After
Width: 800  |  Height: 450  |  Size: 6.2 KiB

+ 2
- 2
examples/examples_template.c 查看文件

@ -1,7 +1,7 @@
/*
WELCOME raylib EXAMPLES CONTRIBUTOR!
This is a bsasic template to anyone ready to contribute with some code example for the library,
This is a basic template to anyone ready to contribute with some code example for the library,
here there are some guidelines on how to create an example to be included in raylib
1. File naming: <module>_<description> - Lower case filename, words separated by underscore,
@ -95,4 +95,4 @@ int main()
//--------------------------------------------------------------------------------------
return 0;
}
}

Loading…
取消
儲存