REPLACE: TABS by 4 spaces

This commit is contained in:
Ray 2023-02-15 17:36:31 +01:00
parent 9eaed07b77
commit 2766835ed4
3 changed files with 130 additions and 129 deletions

View file

@ -59,19 +59,19 @@ int main()
const int screenWidth = 800; const int screenWidth = 800;
const int screenHeight = 450; const int screenHeight = 450;
SetConfigFlags(FLAG_VSYNC_HINT | FLAG_MSAA_4X_HINT); SetConfigFlags(FLAG_VSYNC_HINT | FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "raylib [textures] examples - textured curve"); InitWindow(screenWidth, screenHeight, "raylib [textures] examples - textured curve");
// Load the road texture // Load the road texture
texRoad = LoadTexture("resources/road.png"); texRoad = LoadTexture("resources/road.png");
SetTextureFilter(texRoad, TEXTURE_FILTER_BILINEAR); SetTextureFilter(texRoad, TEXTURE_FILTER_BILINEAR);
// Setup the curve // Setup the curve
curveStartPosition = (Vector2){ 80, 100 }; curveStartPosition = (Vector2){ 80, 100 };
curveStartPositionTangent = (Vector2){ 100, 300 }; curveStartPositionTangent = (Vector2){ 100, 300 };
curveEndPosition = (Vector2){ 700, 350 }; curveEndPosition = (Vector2){ 700, 350 };
curveEndPositionTangent = (Vector2){ 600, 100 }; curveEndPositionTangent = (Vector2){ 600, 100 };
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -81,8 +81,8 @@ int main()
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
UpdateCurve(); UpdateCurve();
UpdateOptions(); UpdateOptions();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -99,13 +99,13 @@ int main()
DrawText(TextFormat("Curve width: %2.0f (Use + and - to adjust)", curveWidth), 10, 30, 10, DARKGRAY); DrawText(TextFormat("Curve width: %2.0f (Use + and - to adjust)", curveWidth), 10, 30, 10, DARKGRAY);
DrawText(TextFormat("Curve segments: %d (Use LEFT and RIGHT to adjust)", curveSegments), 10, 50, 10, DARKGRAY); DrawText(TextFormat("Curve segments: %d (Use LEFT and RIGHT to adjust)", curveSegments), 10, 50, 10, DARKGRAY);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadTexture(texRoad); UnloadTexture(texRoad);
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -118,142 +118,142 @@ int main()
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
static void DrawCurve(void) static void DrawCurve(void)
{ {
if (showCurve) DrawLineBezierCubic(curveStartPosition, curveEndPosition, curveStartPositionTangent, curveEndPositionTangent, 2, BLUE); if (showCurve) DrawLineBezierCubic(curveStartPosition, curveEndPosition, curveStartPositionTangent, curveEndPositionTangent, 2, BLUE);
// Draw the various control points and highlight where the mouse is // Draw the various control points and highlight where the mouse is
DrawLineV(curveStartPosition, curveStartPositionTangent, SKYBLUE); DrawLineV(curveStartPosition, curveStartPositionTangent, SKYBLUE);
DrawLineV(curveEndPosition, curveEndPositionTangent, PURPLE); DrawLineV(curveEndPosition, curveEndPositionTangent, PURPLE);
Vector2 mouse = GetMousePosition(); Vector2 mouse = GetMousePosition();
if (CheckCollisionPointCircle(mouse, curveStartPosition, 6)) DrawCircleV(curveStartPosition, 7, YELLOW); if (CheckCollisionPointCircle(mouse, curveStartPosition, 6)) DrawCircleV(curveStartPosition, 7, YELLOW);
DrawCircleV(curveStartPosition, 5, RED); DrawCircleV(curveStartPosition, 5, RED);
if (CheckCollisionPointCircle(mouse, curveStartPositionTangent, 6)) DrawCircleV(curveStartPositionTangent, 7, YELLOW); if (CheckCollisionPointCircle(mouse, curveStartPositionTangent, 6)) DrawCircleV(curveStartPositionTangent, 7, YELLOW);
DrawCircleV(curveStartPositionTangent, 5, MAROON); DrawCircleV(curveStartPositionTangent, 5, MAROON);
if (CheckCollisionPointCircle(mouse, curveEndPosition, 6)) DrawCircleV(curveEndPosition, 7, YELLOW); if (CheckCollisionPointCircle(mouse, curveEndPosition, 6)) DrawCircleV(curveEndPosition, 7, YELLOW);
DrawCircleV(curveEndPosition, 5, GREEN); DrawCircleV(curveEndPosition, 5, GREEN);
if (CheckCollisionPointCircle(mouse, curveEndPositionTangent, 6)) DrawCircleV(curveEndPositionTangent, 7, YELLOW); if (CheckCollisionPointCircle(mouse, curveEndPositionTangent, 6)) DrawCircleV(curveEndPositionTangent, 7, YELLOW);
DrawCircleV(curveEndPositionTangent, 5, DARKGREEN); DrawCircleV(curveEndPositionTangent, 5, DARKGREEN);
} }
static void UpdateCurve(void) static void UpdateCurve(void)
{ {
// If the mouse is not down, we are not editing the curve so clear the selection // If the mouse is not down, we are not editing the curve so clear the selection
if (!IsMouseButtonDown(MOUSE_LEFT_BUTTON)) if (!IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{ {
curveSelectedPoint = NULL; curveSelectedPoint = NULL;
return; return;
} }
// If a point was selected, move it // If a point was selected, move it
if (curveSelectedPoint) if (curveSelectedPoint)
{ {
*curveSelectedPoint = Vector2Add(*curveSelectedPoint, GetMouseDelta()); *curveSelectedPoint = Vector2Add(*curveSelectedPoint, GetMouseDelta());
return; return;
} }
// The mouse is down, and nothing was selected, so see if anything was picked // The mouse is down, and nothing was selected, so see if anything was picked
Vector2 mouse = GetMousePosition(); Vector2 mouse = GetMousePosition();
if (CheckCollisionPointCircle(mouse, curveStartPosition, 6)) curveSelectedPoint = &curveStartPosition; if (CheckCollisionPointCircle(mouse, curveStartPosition, 6)) curveSelectedPoint = &curveStartPosition;
else if (CheckCollisionPointCircle(mouse, curveStartPositionTangent, 6)) curveSelectedPoint = &curveStartPositionTangent; else if (CheckCollisionPointCircle(mouse, curveStartPositionTangent, 6)) curveSelectedPoint = &curveStartPositionTangent;
else if (CheckCollisionPointCircle(mouse, curveEndPosition, 6)) curveSelectedPoint = &curveEndPosition; else if (CheckCollisionPointCircle(mouse, curveEndPosition, 6)) curveSelectedPoint = &curveEndPosition;
else if (CheckCollisionPointCircle(mouse, curveEndPositionTangent, 6)) curveSelectedPoint = &curveEndPositionTangent; else if (CheckCollisionPointCircle(mouse, curveEndPositionTangent, 6)) curveSelectedPoint = &curveEndPositionTangent;
} }
static void DrawTexturedCurve(void) static void DrawTexturedCurve(void)
{ {
const float step = 1.0f/curveSegments; const float step = 1.0f/curveSegments;
Vector2 previous = curveStartPosition; Vector2 previous = curveStartPosition;
Vector2 previousTangent = { 0 }; Vector2 previousTangent = { 0 };
float previousV = 0; float previousV = 0;
// We can't compute a tangent for the first point, so we need to reuse the tangent from the first segment // We can't compute a tangent for the first point, so we need to reuse the tangent from the first segment
bool tangentSet = false; bool tangentSet = false;
Vector2 current = { 0 }; Vector2 current = { 0 };
float t = 0.0f; float t = 0.0f;
for (int i = 1; i <= curveSegments; i++) for (int i = 1; i <= curveSegments; i++)
{ {
// Segment the curve // Segment the curve
t = step*i; t = step*i;
float a = powf(1 - t, 3); float a = powf(1 - t, 3);
float b = 3*powf(1 - t, 2)*t; float b = 3*powf(1 - t, 2)*t;
float c = 3*(1 - t)*powf(t, 2); float c = 3*(1 - t)*powf(t, 2);
float d = powf(t, 3); float d = powf(t, 3);
// Compute the endpoint for this segment // Compute the endpoint for this segment
current.y = a*curveStartPosition.y + b*curveStartPositionTangent.y + c*curveEndPositionTangent.y + d*curveEndPosition.y; current.y = a*curveStartPosition.y + b*curveStartPositionTangent.y + c*curveEndPositionTangent.y + d*curveEndPosition.y;
current.x = a*curveStartPosition.x + b*curveStartPositionTangent.x + c*curveEndPositionTangent.x + d*curveEndPosition.x; current.x = a*curveStartPosition.x + b*curveStartPositionTangent.x + c*curveEndPositionTangent.x + d*curveEndPosition.x;
// Vector from previous to current // Vector from previous to current
Vector2 delta = { current.x - previous.x, current.y - previous.y }; Vector2 delta = { current.x - previous.x, current.y - previous.y };
// The right hand normal to the delta vector // The right hand normal to the delta vector
Vector2 normal = Vector2Normalize((Vector2){ -delta.y, delta.x }); Vector2 normal = Vector2Normalize((Vector2){ -delta.y, delta.x });
// The v texture coordinate of the segment (add up the length of all the segments so far) // The v texture coordinate of the segment (add up the length of all the segments so far)
float v = previousV + Vector2Length(delta); float v = previousV + Vector2Length(delta);
// Make sure the start point has a normal // Make sure the start point has a normal
if (!tangentSet) if (!tangentSet)
{ {
previousTangent = normal; previousTangent = normal;
tangentSet = true; tangentSet = true;
} }
// Extend out the normals from the previous and current points to get the quad for this segment // Extend out the normals from the previous and current points to get the quad for this segment
Vector2 prevPosNormal = Vector2Add(previous, Vector2Scale(previousTangent, curveWidth)); Vector2 prevPosNormal = Vector2Add(previous, Vector2Scale(previousTangent, curveWidth));
Vector2 prevNegNormal = Vector2Add(previous, Vector2Scale(previousTangent, -curveWidth)); Vector2 prevNegNormal = Vector2Add(previous, Vector2Scale(previousTangent, -curveWidth));
Vector2 currentPosNormal = Vector2Add(current, Vector2Scale(normal, curveWidth)); Vector2 currentPosNormal = Vector2Add(current, Vector2Scale(normal, curveWidth));
Vector2 currentNegNormal = Vector2Add(current, Vector2Scale(normal, -curveWidth)); Vector2 currentNegNormal = Vector2Add(current, Vector2Scale(normal, -curveWidth));
// Draw the segment as a quad // Draw the segment as a quad
rlSetTexture(texRoad.id); rlSetTexture(texRoad.id);
rlBegin(RL_QUADS); rlBegin(RL_QUADS);
rlColor4ub(255,255,255,255); rlColor4ub(255,255,255,255);
rlNormal3f(0.0f, 0.0f, 1.0f); rlNormal3f(0.0f, 0.0f, 1.0f);
rlTexCoord2f(0, previousV); rlTexCoord2f(0, previousV);
rlVertex2f(prevNegNormal.x, prevNegNormal.y); rlVertex2f(prevNegNormal.x, prevNegNormal.y);
rlTexCoord2f(1, previousV); rlTexCoord2f(1, previousV);
rlVertex2f(prevPosNormal.x, prevPosNormal.y); rlVertex2f(prevPosNormal.x, prevPosNormal.y);
rlTexCoord2f(1, v); rlTexCoord2f(1, v);
rlVertex2f(currentPosNormal.x, currentPosNormal.y); rlVertex2f(currentPosNormal.x, currentPosNormal.y);
rlTexCoord2f(0, v); rlTexCoord2f(0, v);
rlVertex2f(currentNegNormal.x, currentNegNormal.y); rlVertex2f(currentNegNormal.x, currentNegNormal.y);
rlEnd(); rlEnd();
// The current step is the start of the next step // The current step is the start of the next step
previous = current; previous = current;
previousTangent = normal; previousTangent = normal;
previousV = v; previousV = v;
} }
} }
static void UpdateOptions(void) static void UpdateOptions(void)
{ {
if (IsKeyPressed(KEY_SPACE)) showCurve = !showCurve; if (IsKeyPressed(KEY_SPACE)) showCurve = !showCurve;
// Update with // Update with
if (IsKeyPressed(KEY_EQUAL)) curveWidth += 2; if (IsKeyPressed(KEY_EQUAL)) curveWidth += 2;
if (IsKeyPressed(KEY_MINUS)) curveWidth -= 2; if (IsKeyPressed(KEY_MINUS)) curveWidth -= 2;
if (curveWidth < 2) curveWidth = 2; if (curveWidth < 2) curveWidth = 2;
// Update segments // Update segments
if (IsKeyPressed(KEY_LEFT)) curveSegments -= 2; if (IsKeyPressed(KEY_LEFT)) curveSegments -= 2;
if (IsKeyPressed(KEY_RIGHT)) curveSegments += 2; if (IsKeyPressed(KEY_RIGHT)) curveSegments += 2;
if (curveSegments < 2) curveSegments = 2; if (curveSegments < 2) curveSegments = 2;
} }

View file

@ -209,8 +209,8 @@ int main(int argc, char* argv[])
if (buffer == NULL) if (buffer == NULL)
{ {
printf("Could not read input file: %s\n", inFileName); printf("Could not read input file: %s\n", inFileName);
return 1; return 1;
} }
// Preprocess buffer to get separate lines // Preprocess buffer to get separate lines

View file

@ -160,7 +160,7 @@ Matrix GetCameraProjectionMatrix(Camera* camera, float aspect);
// MatrixOrtho() // MatrixOrtho()
// MatrixIdentity() // MatrixIdentity()
// raylib input functionality required: GetMouseDelta(), GetMouseWheelMove(), IsKeyDown(), IsKeyPressed() // raylib required functionality: GetMouseDelta(), GetMouseWheelMove(), IsKeyDown(), IsKeyPressed(), GetFrameTime()
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Defines and Macros // Defines and Macros
@ -195,7 +195,7 @@ Matrix GetCameraProjectionMatrix(Camera* camera, float aspect);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module specific Functions Declaration // Module specific Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
//...
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Definition // Module Functions Definition
@ -218,6 +218,7 @@ Vector3 GetCameraRight(Camera *camera)
{ {
Vector3 forward = GetCameraForward(camera); Vector3 forward = GetCameraForward(camera);
Vector3 up = GetCameraUp(camera); Vector3 up = GetCameraUp(camera);
return Vector3CrossProduct(forward, up); return Vector3CrossProduct(forward, up);
} }
@ -419,26 +420,26 @@ void UpdateCamera(Camera *camera, int mode)
bool lockView = mode == CAMERA_FIRST_PERSON || mode == CAMERA_THIRD_PERSON || mode == CAMERA_ORBITAL; bool lockView = mode == CAMERA_FIRST_PERSON || mode == CAMERA_THIRD_PERSON || mode == CAMERA_ORBITAL;
bool rotateUp = mode == CAMERA_FREE; bool rotateUp = mode == CAMERA_FREE;
if (mode == CAMERA_ORBITAL) if (mode == CAMERA_ORBITAL)
{ {
// Obital can just orbit // Orbital can just orbit
Matrix rotatation = MatrixRotate(GetCameraUp(camera), CAMERA_ORBITAL_SPEED * GetFrameTime()); Matrix rotatation = MatrixRotate(GetCameraUp(camera), CAMERA_ORBITAL_SPEED*GetFrameTime());
Vector3 viewVector = Vector3Subtract(camera->position, camera->target); Vector3 viewVector = Vector3Subtract(camera->position, camera->target);
viewVector = Vector3Transform(viewVector, rotatation); viewVector = Vector3Transform(viewVector, rotatation);
camera->position = Vector3Add(camera->target, viewVector); camera->position = Vector3Add(camera->target, viewVector);
} }
else else
{ {
// Camera rotation // Camera rotation
if (IsKeyDown(KEY_DOWN)) CameraPitch(camera, -CAMERA_ROTATION_SPEED, lockView, rotateAroundTarget, rotateUp); if (IsKeyDown(KEY_DOWN)) CameraPitch(camera, -CAMERA_ROTATION_SPEED, lockView, rotateAroundTarget, rotateUp);
if (IsKeyDown(KEY_UP)) CameraPitch(camera, CAMERA_ROTATION_SPEED, lockView, rotateAroundTarget, rotateUp); if (IsKeyDown(KEY_UP)) CameraPitch(camera, CAMERA_ROTATION_SPEED, lockView, rotateAroundTarget, rotateUp);
if (IsKeyDown(KEY_RIGHT)) CameraYaw(camera, -CAMERA_ROTATION_SPEED, rotateAroundTarget); if (IsKeyDown(KEY_RIGHT)) CameraYaw(camera, -CAMERA_ROTATION_SPEED, rotateAroundTarget);
if (IsKeyDown(KEY_LEFT)) CameraYaw(camera, CAMERA_ROTATION_SPEED, rotateAroundTarget); if (IsKeyDown(KEY_LEFT)) CameraYaw(camera, CAMERA_ROTATION_SPEED, rotateAroundTarget);
if (IsKeyDown(KEY_Q)) CameraRoll(camera, -CAMERA_ROTATION_SPEED); if (IsKeyDown(KEY_Q)) CameraRoll(camera, -CAMERA_ROTATION_SPEED);
if (IsKeyDown(KEY_E)) CameraRoll(camera, CAMERA_ROTATION_SPEED); if (IsKeyDown(KEY_E)) CameraRoll(camera, CAMERA_ROTATION_SPEED);
CameraYaw(camera, -mousePositionDelta.x * CAMERA_MOUSE_MOVE_SENSITIVITY, rotateAroundTarget); CameraYaw(camera, -mousePositionDelta.x * CAMERA_MOUSE_MOVE_SENSITIVITY, rotateAroundTarget);
CameraPitch(camera, -mousePositionDelta.y * CAMERA_MOUSE_MOVE_SENSITIVITY, lockView, rotateAroundTarget, rotateUp); CameraPitch(camera, -mousePositionDelta.y * CAMERA_MOUSE_MOVE_SENSITIVITY, lockView, rotateAroundTarget, rotateUp);
// Camera movement // Camera movement
if (IsKeyDown(KEY_W)) CameraMoveForward(camera, CAMERA_MOVE_SPEED, moveInWorldPlane); if (IsKeyDown(KEY_W)) CameraMoveForward(camera, CAMERA_MOVE_SPEED, moveInWorldPlane);