2020-06-28 00:59:14 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib [textures] example - Draw part of the texture tiled
|
|
|
|
*
|
|
|
|
* This example has been created using raylib 3.0 (www.raylib.com)
|
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
#include "raylib.h"
|
|
|
|
|
|
|
|
#define SIZEOF(A) (sizeof(A)/sizeof(A[0]))
|
2020-12-23 22:59:14 +03:00
|
|
|
#define OPT_WIDTH 220 // Max width for the options container
|
|
|
|
#define MARGIN_SIZE 8 // Size for the margins
|
|
|
|
#define COLOR_SIZE 16 // Size of the color select buttons
|
2020-06-28 00:59:14 +03:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
int screenWidth = 800;
|
|
|
|
int screenHeight = 450;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
SetConfigFlags(FLAG_WINDOW_RESIZABLE); // Make the window resizable
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [textures] example - Draw part of a texture tiled");
|
|
|
|
|
|
|
|
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
2020-12-23 22:59:14 +03:00
|
|
|
Texture texPattern = LoadTexture("resources/patterns.png");
|
2021-03-04 13:51:54 +03:00
|
|
|
SetTextureFilter(texPattern, TEXTURE_FILTER_TRILINEAR); // Makes the texture smoother when upscaled
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Coordinates for all patterns inside the texture
|
2021-04-22 19:55:24 +03:00
|
|
|
const Rectangle recPattern[] = {
|
|
|
|
(Rectangle){ 3, 3, 66, 66 },
|
|
|
|
(Rectangle){ 75, 3, 100, 100 },
|
2020-12-23 22:59:14 +03:00
|
|
|
(Rectangle){ 3, 75, 66, 66 },
|
|
|
|
(Rectangle){ 7, 156, 50, 50 },
|
|
|
|
(Rectangle){ 85, 106, 90, 45 },
|
|
|
|
(Rectangle){ 75, 154, 100, 60}
|
|
|
|
};
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Setup colors
|
2020-12-23 22:59:14 +03:00
|
|
|
const Color colors[] = { BLACK, MAROON, ORANGE, BLUE, PURPLE, BEIGE, LIME, RED, DARKGRAY, SKYBLUE };
|
|
|
|
enum { MAX_COLORS = SIZEOF(colors) };
|
2020-06-28 00:59:14 +03:00
|
|
|
Rectangle colorRec[MAX_COLORS] = { 0 };
|
|
|
|
|
|
|
|
// Calculate rectangle for each color
|
2020-12-23 22:59:14 +03:00
|
|
|
for (int i = 0, x = 0, y = 0; i < MAX_COLORS; i++)
|
|
|
|
{
|
2021-03-23 09:51:52 +03:00
|
|
|
colorRec[i].x = 2.0f + MARGIN_SIZE + x;
|
|
|
|
colorRec[i].y = 22.0f + 256.0f + MARGIN_SIZE + y;
|
|
|
|
colorRec[i].width = COLOR_SIZE*2.0f;
|
|
|
|
colorRec[i].height = (float)COLOR_SIZE;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-12-23 22:59:14 +03:00
|
|
|
if (i == (MAX_COLORS/2 - 1))
|
|
|
|
{
|
2021-04-22 19:55:24 +03:00
|
|
|
x = 0;
|
2020-12-23 22:59:14 +03:00
|
|
|
y += COLOR_SIZE + MARGIN_SIZE;
|
2021-04-22 19:55:24 +03:00
|
|
|
}
|
2020-12-23 22:59:14 +03:00
|
|
|
else x += (COLOR_SIZE*2 + MARGIN_SIZE);
|
2020-06-28 00:59:14 +03:00
|
|
|
}
|
|
|
|
|
2020-12-23 22:59:14 +03:00
|
|
|
int activePattern = 0, activeCol = 0;
|
2020-06-28 00:59:14 +03:00
|
|
|
float scale = 1.0f, rotation = 0.0f;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
SetTargetFPS(60);
|
|
|
|
//---------------------------------------------------------------------------------------
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
screenWidth = GetScreenWidth();
|
|
|
|
screenHeight = GetScreenHeight();
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Handle mouse
|
2021-05-08 19:26:24 +03:00
|
|
|
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
2020-12-23 22:59:14 +03:00
|
|
|
{
|
2020-06-28 00:59:14 +03:00
|
|
|
const Vector2 mouse = GetMousePosition();
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Check which pattern was clicked and set it as the active pattern
|
2020-12-23 22:59:14 +03:00
|
|
|
for (int i = 0; i < SIZEOF(recPattern); i++)
|
|
|
|
{
|
|
|
|
if (CheckCollisionPointRec(mouse, (Rectangle){ 2 + MARGIN_SIZE + recPattern[i].x, 40 + MARGIN_SIZE + recPattern[i].y, recPattern[i].width, recPattern[i].height }))
|
2021-04-22 19:55:24 +03:00
|
|
|
{
|
|
|
|
activePattern = i;
|
|
|
|
break;
|
2020-06-28 00:59:14 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Check to see which color was clicked and set it as the active color
|
2020-12-23 22:59:14 +03:00
|
|
|
for (int i = 0; i < MAX_COLORS; ++i)
|
|
|
|
{
|
|
|
|
if (CheckCollisionPointRec(mouse, colorRec[i]))
|
|
|
|
{
|
2020-06-28 00:59:14 +03:00
|
|
|
activeCol = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Handle keys
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Change scale
|
2020-12-23 22:59:14 +03:00
|
|
|
if (IsKeyPressed(KEY_UP)) scale += 0.25f;
|
|
|
|
if (IsKeyPressed(KEY_DOWN)) scale -= 0.25f;
|
|
|
|
if (scale > 10.0f) scale = 10.0f;
|
|
|
|
else if ( scale <= 0.0f) scale = 0.25f;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Change rotation
|
2020-12-23 22:59:14 +03:00
|
|
|
if (IsKeyPressed(KEY_LEFT)) rotation -= 25.0f;
|
|
|
|
if (IsKeyPressed(KEY_RIGHT)) rotation += 25.0f;
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Reset
|
2020-12-23 22:59:14 +03:00
|
|
|
if (IsKeyPressed(KEY_SPACE)) { rotation = 0.0f; scale = 1.0f; }
|
2020-06-28 00:59:14 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
ClearBackground(RAYWHITE);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Draw the tiled area
|
2021-03-23 09:51:52 +03:00
|
|
|
DrawTextureTiled(texPattern, recPattern[activePattern], (Rectangle){(float)OPT_WIDTH+MARGIN_SIZE, (float)MARGIN_SIZE, screenWidth - OPT_WIDTH - 2.0f*MARGIN_SIZE, screenHeight - 2.0f*MARGIN_SIZE},
|
2020-06-28 00:59:14 +03:00
|
|
|
(Vector2){0.0f, 0.0f}, rotation, scale, colors[activeCol]);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Draw options
|
2020-12-23 22:59:14 +03:00
|
|
|
DrawRectangle(MARGIN_SIZE, MARGIN_SIZE, OPT_WIDTH - MARGIN_SIZE, screenHeight - 2*MARGIN_SIZE, ColorAlpha(LIGHTGRAY, 0.5f));
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-12-23 22:59:14 +03:00
|
|
|
DrawText("Select Pattern", 2 + MARGIN_SIZE, 30 + MARGIN_SIZE, 10, BLACK);
|
|
|
|
DrawTexture(texPattern, 2 + MARGIN_SIZE, 40 + MARGIN_SIZE, BLACK);
|
2021-03-23 09:51:52 +03:00
|
|
|
DrawRectangle(2 + MARGIN_SIZE + (int)recPattern[activePattern].x, 40 + MARGIN_SIZE + (int)recPattern[activePattern].y, (int)recPattern[activePattern].width, (int)recPattern[activePattern].height, ColorAlpha(DARKBLUE, 0.3f));
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
DrawText("Select Color", 2+MARGIN_SIZE, 10+256+MARGIN_SIZE, 10, BLACK);
|
2020-12-23 22:59:14 +03:00
|
|
|
for (int i = 0; i < MAX_COLORS; i++)
|
|
|
|
{
|
2020-06-28 00:59:14 +03:00
|
|
|
DrawRectangleRec(colorRec[i], colors[i]);
|
2021-03-23 09:51:52 +03:00
|
|
|
if (activeCol == i) DrawRectangleLinesEx(colorRec[i], 3, ColorAlpha(WHITE, 0.5f));
|
2020-06-28 00:59:14 +03:00
|
|
|
}
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-12-23 22:59:14 +03:00
|
|
|
DrawText("Scale (UP/DOWN to change)", 2 + MARGIN_SIZE, 80 + 256 + MARGIN_SIZE, 10, BLACK);
|
|
|
|
DrawText(TextFormat("%.2fx", scale), 2 + MARGIN_SIZE, 92 + 256 + MARGIN_SIZE, 20, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-12-23 22:59:14 +03:00
|
|
|
DrawText("Rotation (LEFT/RIGHT to change)", 2 + MARGIN_SIZE, 122 + 256 + MARGIN_SIZE, 10, BLACK);
|
|
|
|
DrawText(TextFormat("%.0f degrees", rotation), 2 + MARGIN_SIZE, 134 + 256 + MARGIN_SIZE, 20, BLACK);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-12-23 22:59:14 +03:00
|
|
|
DrawText("Press [SPACE] to reset", 2 + MARGIN_SIZE, 164 + 256 + MARGIN_SIZE, 10, DARKBLUE);
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// Draw FPS
|
2020-12-23 22:59:14 +03:00
|
|
|
DrawText(TextFormat("%i FPS", GetFPS()), 2 + MARGIN_SIZE, 2 + MARGIN_SIZE, 20, BLACK);
|
2020-06-28 00:59:14 +03:00
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-06-28 00:59:14 +03:00
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2020-12-23 22:59:14 +03:00
|
|
|
UnloadTexture(texPattern); // Unload texture
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-12-23 22:59:14 +03:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
2020-06-28 00:59:14 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
2021-04-22 19:55:24 +03:00
|
|
|
|
2020-12-25 01:07:52 +03:00
|
|
|
return 0;
|
2020-06-28 00:59:14 +03:00
|
|
|
}
|
|
|
|
|