diff options
| author | raysan5 <[email protected]> | 2021-10-17 19:10:09 +0200 |
|---|---|---|
| committer | raysan5 <[email protected]> | 2021-10-17 19:10:09 +0200 |
| commit | c20df9aa47f3a9bdaf974d7e0f6c586a5852fca2 (patch) | |
| tree | b47a2db32069d92e66db034e76ec4c978dfa2c8c /examples/core | |
| parent | d47d7c000146ed90a3270ba507e4769cf80c86af (diff) | |
| download | raylib-c20df9aa47f3a9bdaf974d7e0f6c586a5852fca2.tar.gz raylib-c20df9aa47f3a9bdaf974d7e0f6c586a5852fca2.zip | |
Reviewed examples
Diffstat (limited to 'examples/core')
| -rw-r--r-- | examples/core/core_quat_conversion.c | 53 | ||||
| -rw-r--r-- | examples/core/core_random_values.c | 6 | ||||
| -rw-r--r-- | examples/core/core_smooth_pixelperfect.c (renamed from examples/core/core_2d_camera_smooth_pixelperfect.c) | 233 | ||||
| -rw-r--r-- | examples/core/core_split_screen.c | 4 |
4 files changed, 151 insertions, 145 deletions
diff --git a/examples/core/core_quat_conversion.c b/examples/core/core_quat_conversion.c index f3c9b757..3fc98b3d 100644 --- a/examples/core/core_quat_conversion.c +++ b/examples/core/core_quat_conversion.c @@ -10,7 +10,7 @@ * * Example contributed by Chris Camacho (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5) * -* Copyright (c) 2020 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5) +* Copyright (c) 2020-2021 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5) * ********************************************************************************************/ @@ -32,15 +32,23 @@ int main(void) camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) camera.fovy = 45.0f; // Camera field-of-view Y - camera.projection = CAMERA_PERSPECTIVE; // Camera mode type + camera.projection = CAMERA_PERSPECTIVE; // Camera mode type - Mesh mesh = GenMeshCylinder(0.2f, 1.0f, 32); - Model model = LoadModelFromMesh(mesh); + // Load a cylinder model for testing + Model model = LoadModelFromMesh(GenMeshCylinder(0.2f, 1.0f, 32)); - // Some required variables + // Generic quaternion for operations Quaternion q1 = { 0 }; - Matrix m1 = { 0 }, m2 = { 0 }, m3 = { 0 }, m4 = { 0 }; - Vector3 v1 = { 0 }, v2 = { 0 }; + + // Transform matrices required to draw 4 cylinders + Matrix m1 = { 0 }; + Matrix m2 = { 0 }; + Matrix m3 = { 0 }; + Matrix m4 = { 0 }; + + // Generic vectors for rotations + Vector3 v1 = { 0 }; + Vector3 v2 = { 0 }; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -50,6 +58,10 @@ int main(void) { // Update //-------------------------------------------------------------------------------------- + if (v2.x < 0) v2.x += PI*2; + if (v2.y < 0) v2.y += PI*2; + if (v2.z < 0) v2.z += PI*2; + if (!IsKeyDown(KEY_SPACE)) { v1.x += 0.01f; @@ -68,7 +80,7 @@ int main(void) q1 = QuaternionFromMatrix(m1); m3 = QuaternionToMatrix(q1); - v2 = QuaternionToEuler(q1); + v2 = QuaternionToEuler(q1); // Angles returned in radians m4 = MatrixRotateZYX(v2); //-------------------------------------------------------------------------------------- @@ -83,10 +95,13 @@ int main(void) model.transform = m1; DrawModel(model, (Vector3){ -1, 0, 0 }, 1.0f, RED); + model.transform = m2; DrawModel(model, (Vector3){ 1, 0, 0 }, 1.0f, RED); + model.transform = m3; DrawModel(model, (Vector3){ 0, 0, 0 }, 1.0f, RED); + model.transform = m4; DrawModel(model, (Vector3){ 0, 0, -1 }, 1.0f, RED); @@ -94,23 +109,13 @@ int main(void) EndMode3D(); - if (v2.x < 0) v2.x += PI*2; - if (v2.y < 0) v2.y += PI*2; - if (v2.z < 0) v2.z += PI*2; - - Color cx,cy,cz; - cx = cy = cz = BLACK; - if (v1.x == v2.x) cx = GREEN; - if (v1.y == v2.y) cy = GREEN; - if (v1.z == v2.z) cz = GREEN; - - DrawText(TextFormat("%2.3f", v1.x), 20, 20, 20, cx); - DrawText(TextFormat("%2.3f", v1.y), 20, 40, 20, cy); - DrawText(TextFormat("%2.3f", v1.z), 20, 60, 20, cz); + DrawText(TextFormat("%2.3f", v1.x), 20, 20, 20, (v1.x == v2.x)? GREEN: BLACK); + DrawText(TextFormat("%2.3f", v1.y), 20, 40, 20, (v1.y == v2.y)? GREEN: BLACK); + DrawText(TextFormat("%2.3f", v1.z), 20, 60, 20, (v1.z == v2.z)? GREEN: BLACK); - DrawText(TextFormat("%2.3f", v2.x), 200, 20, 20, cx); - DrawText(TextFormat("%2.3f", v2.y), 200, 40, 20, cy); - DrawText(TextFormat("%2.3f", v2.z), 200, 60, 20, cz); + DrawText(TextFormat("%2.3f", v2.x), 200, 20, 20, (v1.x == v2.x)? GREEN: BLACK); + DrawText(TextFormat("%2.3f", v2.y), 200, 40, 20, (v1.y == v2.y)? GREEN: BLACK); + DrawText(TextFormat("%2.3f", v2.z), 200, 60, 20, (v1.z == v2.z)? GREEN: BLACK); EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/examples/core/core_random_values.c b/examples/core/core_random_values.c index 260e708c..b44b5faf 100644 --- a/examples/core/core_random_values.c +++ b/examples/core/core_random_values.c @@ -20,10 +20,12 @@ int main(void) InitWindow(screenWidth, screenHeight, "raylib [core] example - generate random values"); - int framesCounter = 0; // Variable used to count frames + // SetRandomSeed(0xaabbccff); // Set a custom random seed if desired, by default: "time(NULL)" int randValue = GetRandomValue(-8, 5); // Get a random integer number between -8 and 5 (both included) - + + int framesCounter = 0; // Variable used to count frames + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- diff --git a/examples/core/core_2d_camera_smooth_pixelperfect.c b/examples/core/core_smooth_pixelperfect.c index 75ffe262..86a64701 100644 --- a/examples/core/core_2d_camera_smooth_pixelperfect.c +++ b/examples/core/core_smooth_pixelperfect.c @@ -1,118 +1,117 @@ -/*******************************************************************************************
-*
-* 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 (@NotManyIdeasDev) and
-* reviewed by Ramon Santamaria (@raysan5)
-*
-* Copyright (c) 2021 Giancamillo Alessandroni (@NotManyIdeasDev) and Ramon Santamaria (@raysan5)
-*
-********************************************************************************************/
-
-#include "raylib.h"
-
-#include <math.h> // Required for: sinf(), cosf()
-
-int main(void)
-{
- // Initialization
- //--------------------------------------------------------------------------------------
- const int screenWidth = 800;
- const int screenHeight = 450;
-
- const int virtualScreenWidth = 160;
- const int virtualScreenHeight = 90;
-
- const float virtualRatio = (float)screenWidth/(float)virtualScreenWidth;
-
- 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 target = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight); // This is where we'll draw all our objects.
-
- 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 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 cameraX = 0.0f;
- float cameraY = 0.0f;
-
- SetTargetFPS(60);
- //--------------------------------------------------------------------------------------
-
- // Main game loop
- while (!WindowShouldClose()) // Detect window close button or ESC key
- {
- // Update
- //----------------------------------------------------------------------------------
- rotation += 60.0f*GetFrameTime(); // Rotate the rectangles, 60 degrees per second
-
- // 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 worldSpace coordinates, keep decimals into screenSpace coordinates
- worldSpaceCamera.target.x = (int)screenSpaceCamera.target.x;
- screenSpaceCamera.target.x -= worldSpaceCamera.target.x;
- screenSpaceCamera.target.x *= virtualRatio;
-
- worldSpaceCamera.target.y = (int)screenSpaceCamera.target.y;
- screenSpaceCamera.target.y -= worldSpaceCamera.target.y;
- screenSpaceCamera.target.y *= virtualRatio;
-
- //----------------------------------------------------------------------------------
-
- // Draw
- //----------------------------------------------------------------------------------
- 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);
- DrawTexturePro(target.texture, sourceRec, destRec, origin, 0.0f, WHITE);
- EndMode2D();
-
- 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(target); // Unload render texture
-
- CloseWindow(); // Close window and OpenGL context
- //--------------------------------------------------------------------------------------
-
- return 0;
+/******************************************************************************************* +* +* 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 (@NotManyIdeasDev) and +* reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2021 Giancamillo Alessandroni (@NotManyIdeasDev) and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#include <math.h> // Required for: sinf(), cosf() + +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + const int virtualScreenWidth = 160; + const int virtualScreenHeight = 90; + + const float virtualRatio = (float)screenWidth/(float)virtualScreenWidth; + + 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 target = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight); // This is where we'll draw all our objects. + + 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 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 cameraX = 0.0f; + float cameraY = 0.0f; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + rotation += 60.0f*GetFrameTime(); // Rotate the rectangles, 60 degrees per second + + // 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 worldSpace coordinates, keep decimals into screenSpace coordinates + worldSpaceCamera.target.x = (int)screenSpaceCamera.target.x; + screenSpaceCamera.target.x -= worldSpaceCamera.target.x; + screenSpaceCamera.target.x *= virtualRatio; + + worldSpaceCamera.target.y = (int)screenSpaceCamera.target.y; + screenSpaceCamera.target.y -= worldSpaceCamera.target.y; + screenSpaceCamera.target.y *= virtualRatio; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + 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); + DrawTexturePro(target.texture, sourceRec, destRec, origin, 0.0f, WHITE); + EndMode2D(); + + 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(target); // Unload render texture + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; }
\ No newline at end of file diff --git a/examples/core/core_split_screen.c b/examples/core/core_split_screen.c index 31c3fb4b..1f4242e7 100644 --- a/examples/core/core_split_screen.c +++ b/examples/core/core_split_screen.c @@ -122,7 +122,7 @@ int main(void) BeginMode3D(cameraPlayer1); DrawScene(); EndMode3D(); - DrawText("PLAYER1 W/S to move", 0, 0, 20, RED); + DrawText("PLAYER1 W/S to move", 10, 10, 20, RED); EndTextureMode(); // Draw Player2 view to the render texture @@ -131,7 +131,7 @@ int main(void) BeginMode3D(cameraPlayer2); DrawScene(); EndMode3D(); - DrawText("PLAYER2 UP/DOWN to move", 0, 0, 20, BLUE); + DrawText("PLAYER2 UP/DOWN to move", 10, 10, 20, BLUE); EndTextureMode(); // Draw both views render textures to the screen side by side |
