raylib/examples/shapes/shapes_rectangle_scaling.c

94 lines
3.6 KiB
C
Raw Normal View History

/*******************************************************************************************
*
* raylib [shapes] example - rectangle scaling by mouse
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#define MOUSE_SCALE_MARK_SIZE 12
2019-05-20 17:36:42 +03:00
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle scaling mouse");
2019-05-20 17:36:42 +03:00
Rectangle rec = { 100, 100, 200, 80 };
2019-05-20 17:36:42 +03:00
Vector2 mousePosition = { 0 };
2019-05-20 17:36:42 +03:00
bool mouseScaleReady = false;
bool mouseScaleMode = false;
2019-05-20 17:36:42 +03:00
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
mousePosition = GetMousePosition();
2019-05-20 17:36:42 +03:00
if (CheckCollisionPointRec(mousePosition, rec) &&
CheckCollisionPointRec(mousePosition, (Rectangle){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE }))
{
mouseScaleReady = true;
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) mouseScaleMode = true;
}
else mouseScaleReady = false;
2019-05-20 17:36:42 +03:00
if (mouseScaleMode)
{
mouseScaleReady = true;
2019-05-20 17:36:42 +03:00
rec.width = (mousePosition.x - rec.x);
rec.height = (mousePosition.y - rec.y);
2019-05-20 17:36:42 +03:00
if (rec.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE;
if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = MOUSE_SCALE_MARK_SIZE;
2019-05-20 17:36:42 +03:00
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) mouseScaleMode = false;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
2019-05-20 17:36:42 +03:00
DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, GRAY);
DrawRectangleRec(rec, Fade(GREEN, 0.5f));
2019-05-20 17:36:42 +03:00
if (mouseScaleReady)
{
DrawRectangleLinesEx(rec, 1, RED);
2019-05-20 17:36:42 +03:00
DrawTriangle((Vector2){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height },
(Vector2){ rec.x + rec.width, rec.y + rec.height },
(Vector2){ rec.x + rec.width, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE }, RED);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
2019-05-20 17:36:42 +03:00
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}