|
|
@ -32,18 +32,18 @@ int main(void) |
|
|
const int screenHeight = 450; |
|
|
const int screenHeight = 450; |
|
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - starfield effect"); |
|
|
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - starfield effect"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Color bgColor = ColorLerp(DARKBLUE, BLACK, 0.69f); |
|
|
Color bgColor = ColorLerp(DARKBLUE, BLACK, 0.69f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Speed at which we fly forward |
|
|
// Speed at which we fly forward |
|
|
float speed = 10.0f/9.0f; |
|
|
float speed = 10.0f/9.0f; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We're either drawing lines or circles |
|
|
// We're either drawing lines or circles |
|
|
bool drawLines = true; |
|
|
bool drawLines = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Vector3 stars[STAR_COUNT] = { 0 }; |
|
|
Vector3 stars[STAR_COUNT] = { 0 }; |
|
|
Vector2 starsScreenPos[STAR_COUNT] = { 0 }; |
|
|
Vector2 starsScreenPos[STAR_COUNT] = { 0 }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Setup the stars with a random position |
|
|
// Setup the stars with a random position |
|
|
for (int i = 0; i < STAR_COUNT; i++) |
|
|
for (int i = 0; i < STAR_COUNT; i++) |
|
|
{ |
|
|
{ |
|
|
@ -65,22 +65,22 @@ int main(void) |
|
|
if ((int)mouseMove != 0) speed += 2.0f*mouseMove/9.0f; |
|
|
if ((int)mouseMove != 0) speed += 2.0f*mouseMove/9.0f; |
|
|
if (speed < 0.0f) speed = 0.1f; |
|
|
if (speed < 0.0f) speed = 0.1f; |
|
|
else if (speed > 2.0f) speed = 2.0f; |
|
|
else if (speed > 2.0f) speed = 2.0f; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Toggle lines / points with space bar |
|
|
// Toggle lines / points with space bar |
|
|
if (IsKeyPressed(KEY_SPACE)) drawLines = !drawLines; |
|
|
if (IsKeyPressed(KEY_SPACE)) drawLines = !drawLines; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float dt = GetFrameTime(); |
|
|
float dt = GetFrameTime(); |
|
|
for (int i = 0; i < STAR_COUNT; i++) |
|
|
|
|
|
|
|
|
for (int i = 0; i < STAR_COUNT; i++) |
|
|
{ |
|
|
{ |
|
|
// Update star's timer |
|
|
// Update star's timer |
|
|
stars[i].z -= dt*speed; |
|
|
stars[i].z -= dt*speed; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate the screen position |
|
|
// Calculate the screen position |
|
|
starsScreenPos[i] = (Vector2){ |
|
|
starsScreenPos[i] = (Vector2){ |
|
|
screenWidth*0.5f + stars[i].x/stars[i].z, |
|
|
screenWidth*0.5f + stars[i].x/stars[i].z, |
|
|
screenHeight*0.5f + stars[i].y/stars[i].z, |
|
|
screenHeight*0.5f + stars[i].y/stars[i].z, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the star is too old, or offscreen, it dies and we make a new random one |
|
|
// If the star is too old, or offscreen, it dies and we make a new random one |
|
|
if ((stars[i].z < 0.0f) || (starsScreenPos[i].x < 0) || (starsScreenPos[i].y < 0.0f) || |
|
|
if ((stars[i].z < 0.0f) || (starsScreenPos[i].x < 0) || (starsScreenPos[i].y < 0.0f) || |
|
|
(starsScreenPos[i].x > screenWidth) || (starsScreenPos[i].y > screenHeight)) |
|
|
(starsScreenPos[i].x > screenWidth) || (starsScreenPos[i].y > screenHeight)) |
|
|
@ -97,14 +97,14 @@ int main(void) |
|
|
BeginDrawing(); |
|
|
BeginDrawing(); |
|
|
|
|
|
|
|
|
ClearBackground(bgColor); |
|
|
ClearBackground(bgColor); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < STAR_COUNT; i++) |
|
|
for (int i = 0; i < STAR_COUNT; i++) |
|
|
{ |
|
|
{ |
|
|
if (drawLines) |
|
|
if (drawLines) |
|
|
{ |
|
|
{ |
|
|
// Get the time a little while ago for this star, but clamp it |
|
|
// Get the time a little while ago for this star, but clamp it |
|
|
float t = Clamp(stars[i].z + 1.0f/32.0f, 0.0f, 1.0f); |
|
|
float t = Clamp(stars[i].z + 1.0f/32.0f, 0.0f, 1.0f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If it's different enough from the current time, we proceed |
|
|
// If it's different enough from the current time, we proceed |
|
|
if ((t - stars[i].z) > 1e-3) |
|
|
if ((t - stars[i].z) > 1e-3) |
|
|
{ |
|
|
{ |
|
|
@ -113,26 +113,26 @@ int main(void) |
|
|
screenWidth*0.5f + stars[i].x/t, |
|
|
screenWidth*0.5f + stars[i].x/t, |
|
|
screenHeight*0.5f + stars[i].y/t, |
|
|
screenHeight*0.5f + stars[i].y/t, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Draw a line connecting the old point to the current point |
|
|
// Draw a line connecting the old point to the current point |
|
|
DrawLineV(startPos, starsScreenPos[i], RAYWHITE); |
|
|
DrawLineV(startPos, starsScreenPos[i], RAYWHITE); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
|
|
|
|
|
|
else |
|
|
{ |
|
|
{ |
|
|
// Make the radius grow as the star ages |
|
|
// Make the radius grow as the star ages |
|
|
float radius = Lerp(stars[i].z, 1.0f, 5.0f); |
|
|
float radius = Lerp(stars[i].z, 1.0f, 5.0f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Draw the circle |
|
|
// Draw the circle |
|
|
DrawCircleV(starsScreenPos[i], radius, RAYWHITE); |
|
|
DrawCircleV(starsScreenPos[i], radius, RAYWHITE); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DrawText(TextFormat("[MOUSE WHEEL] Current Speed: %.0f", 9.0f*speed/2.0f), 10, 40, 20, RAYWHITE); |
|
|
DrawText(TextFormat("[MOUSE WHEEL] Current Speed: %.0f", 9.0f*speed/2.0f), 10, 40, 20, RAYWHITE); |
|
|
DrawText(TextFormat("[SPACE] Current draw mode: %s", drawLines ? "Lines" : "Circles"), 10, 70, 20, RAYWHITE); |
|
|
DrawText(TextFormat("[SPACE] Current draw mode: %s", drawLines ? "Lines" : "Circles"), 10, 70, 20, RAYWHITE); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DrawFPS(10, 10); |
|
|
DrawFPS(10, 10); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EndDrawing(); |
|
|
EndDrawing(); |
|
|
//---------------------------------------------------------------------------------- |
|
|
//---------------------------------------------------------------------------------- |
|
|
} |
|
|
} |
|
|
|