Example resize fixes using IsWindowResized()
This commit is contained in:
parent
a36905607c
commit
b16313a2d3
6 changed files with 325 additions and 177 deletions
|
|
@ -22,13 +22,13 @@ int main(void)
|
|||
{
|
||||
// Initialization
|
||||
//---------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
|
||||
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball");
|
||||
|
||||
Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
|
||||
Vector2 ballPosition = { screenWidth/2.0f, screenHeight/2.0f };
|
||||
Vector2 ballSpeed = { 5.0f, 4.0f };
|
||||
int ballRadius = 20;
|
||||
|
||||
|
|
@ -51,8 +51,8 @@ int main(void)
|
|||
ballPosition.y += ballSpeed.y;
|
||||
|
||||
// Check walls collision for bouncing
|
||||
if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
|
||||
if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
|
||||
if ((ballPosition.x >= (screenWidth - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
|
||||
if ((ballPosition.y >= (screenHeight - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
|
||||
}
|
||||
else framesCounter++;
|
||||
//-----------------------------------------------------
|
||||
|
|
@ -64,10 +64,10 @@ int main(void)
|
|||
ClearBackground(RAYWHITE);
|
||||
|
||||
DrawCircleV(ballPosition, (float)ballRadius, MAROON);
|
||||
DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
|
||||
DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, screenHeight - 25, 20, LIGHTGRAY);
|
||||
|
||||
// On pause, we draw a blinking message
|
||||
if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);
|
||||
if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", (screenWidth - 100)/2, (screenHeight- 50)/2, 30, GRAY);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
|
|
@ -81,4 +81,4 @@ int main(void)
|
|||
//----------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,9 +25,6 @@
|
|||
#define SCREEN_WIDTH 800
|
||||
#define SCREEN_HEIGHT 450
|
||||
|
||||
#define CENTER_X SCREEN_WIDTH * 0.5
|
||||
#define CENTER_Y SCREEN_HEIGHT * 0.5 - 100
|
||||
|
||||
// Constant for Simulation
|
||||
#define SIMULATION_STEPS 30
|
||||
#define G 9.81
|
||||
|
|
@ -37,7 +34,7 @@
|
|||
//----------------------------------------------------------------------------------
|
||||
static Vector2 CalculatePendulumEndPoint(float l, float theta);
|
||||
static Vector2 CalculateDoublePendulumEndPoint(float l1, float theta1, float l2, float theta2);
|
||||
|
||||
static Vector2 CalculateCenter(Vector2 screenSize) { return (Vector2){ screenSize.x * 0.5, screenSize.y * 0.5 - 100 }; }
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
|
|
@ -47,6 +44,8 @@ int main(void)
|
|||
//--------------------------------------------------------------------------------------
|
||||
SetConfigFlags(FLAG_WINDOW_HIGHDPI);
|
||||
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - Double Pendulum");
|
||||
Vector2 screenSize = { GetScreenWidth(), GetScreenHeight() };
|
||||
Vector2 screenCenter = CalculateCenter(screenSize);
|
||||
|
||||
// Simulation Paramters
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
|
@ -56,8 +55,8 @@ int main(void)
|
|||
float totalM = m1 + m2;
|
||||
|
||||
Vector2 previousPosition = CalculateDoublePendulumEndPoint(l1, theta1, l2, theta2);
|
||||
previousPosition.x += CENTER_X;
|
||||
previousPosition.y += CENTER_Y;
|
||||
previousPosition.x += screenCenter.x;
|
||||
previousPosition.y += screenCenter.y;
|
||||
|
||||
// Scale length
|
||||
float L1 = l1 * lengthScaler;
|
||||
|
|
@ -70,7 +69,7 @@ int main(void)
|
|||
|
||||
// Create Framebuffer
|
||||
//--------------------------------------------------------------------------------------
|
||||
RenderTexture2D target = LoadRenderTexture(SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
RenderTexture2D target = LoadRenderTexture(screenSize.x, screenSize.y);
|
||||
SetTextureFilter(target.texture, TEXTURE_FILTER_BILINEAR);
|
||||
|
||||
SetTargetFPS(60);
|
||||
|
|
@ -82,6 +81,16 @@ int main(void)
|
|||
// Update
|
||||
float dt = GetFrameTime();
|
||||
float step = dt / SIMULATION_STEPS, step2 = step * step;
|
||||
if(IsWindowResized())
|
||||
{
|
||||
// Update screen dimensions
|
||||
screenSize.x = GetScreenWidth();
|
||||
screenSize.y = GetScreenHeight();
|
||||
screenCenter = CalculateCenter(screenSize);
|
||||
UnloadRenderTexture(target);
|
||||
target = LoadRenderTexture(screenSize.x, screenSize.y);
|
||||
SetTextureFilter(target.texture, TEXTURE_FILTER_BILINEAR);
|
||||
}
|
||||
|
||||
// Update Physics - larger steps = better approximation
|
||||
//----------------------------------------------------------------------------------
|
||||
|
|
@ -115,15 +124,15 @@ int main(void)
|
|||
|
||||
// Calculate position
|
||||
Vector2 currentPosition = CalculateDoublePendulumEndPoint(l1, theta1, l2, theta2);
|
||||
currentPosition.x += CENTER_X;
|
||||
currentPosition.y += CENTER_Y;
|
||||
currentPosition.x += screenCenter.x;
|
||||
currentPosition.y += screenCenter.y;
|
||||
|
||||
// Draw to framebuffer
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginTextureMode(target);
|
||||
|
||||
// Draw a transparent rectangle - smaller alpha = longer trails
|
||||
DrawRectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, Fade(BLACK, fateAlpha));
|
||||
DrawRectangle(0, 0, screenSize.x, screenSize.y, Fade(BLACK, fateAlpha));
|
||||
|
||||
// Draw trail
|
||||
DrawCircleV(previousPosition, trailThick, RED);
|
||||
|
|
@ -146,11 +155,11 @@ int main(void)
|
|||
(Vector2){ 0, 0 }, WHITE);
|
||||
|
||||
// Draw Double Pendulum
|
||||
DrawRectanglePro((Rectangle){ CENTER_X, CENTER_Y, 10 * l1, lineThick },
|
||||
DrawRectanglePro((Rectangle){ screenCenter.x, screenCenter.y, 10 * l1, lineThick },
|
||||
(Vector2){0, lineThick * 0.5}, 90 - RAD2DEG * theta1, RAYWHITE);
|
||||
|
||||
Vector2 endpoint1 = CalculatePendulumEndPoint(l1, theta1);
|
||||
DrawRectanglePro((Rectangle){ CENTER_X + endpoint1.x, CENTER_Y + endpoint1.y, 10 * l2, lineThick },
|
||||
DrawRectanglePro((Rectangle){ screenCenter.x + endpoint1.x, screenCenter.y + endpoint1.y, 10 * l2, lineThick },
|
||||
(Vector2){0, lineThick * 0.5}, 90 - RAD2DEG * theta2, RAYWHITE);
|
||||
|
||||
EndDrawing();
|
||||
|
|
|
|||
|
|
@ -24,8 +24,8 @@ int main(void)
|
|||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings ball anim");
|
||||
|
||||
|
|
@ -45,6 +45,15 @@ int main(void)
|
|||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsWindowResized())
|
||||
{
|
||||
screenWidth = GetScreenWidth();
|
||||
screenHeight = GetScreenHeight();
|
||||
if(state > 0)
|
||||
ballPositionX = (int)EaseElasticOut((float)120, -100, screenWidth/2.0f + 100, 120);
|
||||
if(state > 1)
|
||||
ballRadius = (int)EaseElasticIn((float)200, 20, screenWidth/2.0f + 100, 200);
|
||||
}
|
||||
if (state == 0) // Move ball position X with easing
|
||||
{
|
||||
framesCounter++;
|
||||
|
|
@ -59,7 +68,7 @@ int main(void)
|
|||
else if (state == 1) // Increase ball radius with easing
|
||||
{
|
||||
framesCounter++;
|
||||
ballRadius = (int)EaseElasticIn((float)framesCounter, 20, 500, 200);
|
||||
ballRadius = (int)EaseElasticIn((float)framesCounter, 20, screenWidth/2.0f + 100, 200);
|
||||
|
||||
if (framesCounter >= 200)
|
||||
{
|
||||
|
|
@ -100,9 +109,9 @@ int main(void)
|
|||
ClearBackground(RAYWHITE);
|
||||
|
||||
if (state >= 2) DrawRectangle(0, 0, screenWidth, screenHeight, GREEN);
|
||||
DrawCircle(ballPositionX, 200, (float)ballRadius, Fade(RED, 1.0f - ballAlpha));
|
||||
DrawCircle(ballPositionX, screenHeight / 2, (float)ballRadius, Fade(RED, 1.0f - ballAlpha));
|
||||
|
||||
if (state == 3) DrawText("PRESS [ENTER] TO PLAY AGAIN!", 240, 200, 20, BLACK);
|
||||
if (state == 3) DrawText("PRESS [ENTER] TO PLAY AGAIN!", screenWidth/2 - 140, (screenHeight/2 - 10), 20, BLACK);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
|
@ -114,4 +123,4 @@ int main(void)
|
|||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,8 +24,8 @@ int main(void)
|
|||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - following eyes");
|
||||
|
||||
|
|
@ -46,6 +46,16 @@ int main(void)
|
|||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
if (IsWindowResized())
|
||||
{
|
||||
screenWidth = GetScreenWidth();
|
||||
screenHeight = GetScreenHeight();
|
||||
}
|
||||
scleraLeftPosition.x = screenWidth/2.0f - 100.0f;
|
||||
scleraLeftPosition.y = screenHeight/2.0f;
|
||||
scleraRightPosition.x = screenWidth/2.0f + 100.0f;
|
||||
scleraRightPosition.y = screenHeight/2.0f;
|
||||
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
irisLeftPosition = GetMousePosition();
|
||||
|
|
@ -108,4 +118,4 @@ int main(void)
|
|||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ int main(void)
|
|||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - raylib logo using shapes");
|
||||
|
||||
|
|
@ -37,7 +37,11 @@ int main(void)
|
|||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
if (IsWindowResized())
|
||||
{
|
||||
screenWidth = GetScreenWidth();
|
||||
screenHeight = GetScreenHeight();
|
||||
}
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
|
@ -48,7 +52,7 @@ int main(void)
|
|||
DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, RAYWHITE);
|
||||
DrawText("raylib", screenWidth/2 - 44, screenHeight/2 + 48, 50, BLACK);
|
||||
|
||||
DrawText("this is NOT a texture!", 350, 370, 10, GRAY);
|
||||
DrawText("this is NOT a texture!", screenWidth/2 - 50, screenHeight/2 + 145, 10, GRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
|
@ -60,4 +64,4 @@ int main(void)
|
|||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ int main(void)
|
|||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - raylib logo animation");
|
||||
|
||||
|
|
@ -48,6 +48,14 @@ int main(void)
|
|||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
if (IsWindowResized())
|
||||
{
|
||||
screenWidth = GetScreenWidth();
|
||||
screenHeight = GetScreenHeight();
|
||||
}
|
||||
logoPositionX = screenWidth/2 - 128;
|
||||
logoPositionY = screenHeight/2 - 128;
|
||||
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (state == 0) // State 0: Small box blinking
|
||||
|
|
@ -145,13 +153,13 @@ int main(void)
|
|||
DrawRectangle(logoPositionX + 240, logoPositionY + 16, 16, rightSideRecHeight - 32, Fade(BLACK, alpha));
|
||||
DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, Fade(BLACK, alpha));
|
||||
|
||||
DrawRectangle(GetScreenWidth()/2 - 112, GetScreenHeight()/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
|
||||
DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
|
||||
|
||||
DrawText(TextSubtext("raylib", 0, lettersCount), GetScreenWidth()/2 - 44, GetScreenHeight()/2 + 48, 50, Fade(BLACK, alpha));
|
||||
DrawText(TextSubtext("raylib", 0, lettersCount), screenWidth/2 - 44, screenHeight/2 + 48, 50, Fade(BLACK, alpha));
|
||||
}
|
||||
else if (state == 4)
|
||||
{
|
||||
DrawText("[R] REPLAY", 340, 200, 20, GRAY);
|
||||
DrawText("[R] REPLAY", screenWidth/2 - 60, screenHeight/2 - 25, 20, GRAY);
|
||||
}
|
||||
|
||||
EndDrawing();
|
||||
|
|
|
|||
Loading…
Reference in a new issue