help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
cpp-shape-drawer
/
src
RSK World
cpp-shape-drawer
C++ Shape Drawer - Advanced Graphics Application + Raylib + OOP Design + Shape Tools + Transformation + Export + Educational Design
src
  • main.cpp8.1 KB
main.cpp
src/main.cpp
Raw Download
Find: Go to:
/**
 * @file main.cpp
 * @brief Main entry point for the C++ Shape Drawer application.
 * 
 * Part of the C++ Shape Drawer Project.
 * 
 * @author Molla Samser (Founder of RSK World)
 * @designer Rima Khatun
 * @website https://rskworld.in
 * @email hello@rskworld.in
 * @phone +91 93305 39277
 * @location Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147
 * @year 2026
 */

#include <raylib.h>
#include "DrawingCanvas.hpp"
#include "Circle.hpp"
#include "RectangleShape.hpp"
#include "Triangle.hpp"
#include "Star.hpp"
#include "Polygon.hpp"
#include "TextShape.hpp"
#include "PatternGenerator.hpp"
#include <iostream>

// Application State
enum ShapeType { CIRCLE, RECTANGLE, TRIANGLE, STAR, POLYGON, TEXT };

int main() {
    // Initialization
    const int screenWidth = 1000;
    const int screenHeight = 700;

    InitWindow(screenWidth, screenHeight, "C++ Shape Drawer Masterpiece - by Molla Samser (rskworld.in)");

    DrawingCanvas canvas;
    ShapeType currentType = CIRCLE;
    Color currentColor = SKYBLUE;
    bool isFilled = true;
    int polySides = 5;
    
    Camera2D camera = { 0 };
    camera.target = (Vector2){ 0, 0 };
    camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
    camera.rotation = 0.0f;
    camera.zoom = 1.0f;

    Shape* selectedShape = nullptr;
    Vector2 dragOffset = { 0, 0 };

    SetTargetFPS(60);

    // Main game loop
    while (!WindowShouldClose()) {
        float dt = GetFrameTime();
        canvas.updateAll(dt);

        // Update
        // ----------------------------------------------------------------------------------
        Vector2 mousePos = GetMousePosition();
        Vector2 worldMousePos = GetScreenToWorld2D(mousePos, camera);
        
        // Camera Control
        if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
            Vector2 delta = GetMouseDelta();
            delta = Vector2Scale(delta, -1.0f/camera.zoom);
            camera.target = Vector2Add(camera.target, delta);
        }

        float wheel = GetMouseWheelMove();
        if (wheel != 0) {
            Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera);
            camera.offset = GetMousePosition();
            camera.target = mouseWorldPos;
            camera.zoom += wheel * 0.1f;
            if (camera.zoom < 0.1f) camera.zoom = 0.1f;
        }

        // Selection and Dragging
        if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
            selectedShape = canvas.getHoveredShape(worldMousePos);
            if (selectedShape) {
                dragOffset = { worldMousePos.x - selectedShape->getPosition().x, worldMousePos.y - selectedShape->getPosition().y };
            } else {
                switch (currentType) {
                    case CIRCLE:
                        canvas.addShape(std::make_unique<Circle>(worldMousePos, 30.0f, currentColor, isFilled));
                        break;
                    case RECTANGLE:
                        canvas.addShape(std::make_unique<RectangleShape>(worldMousePos, 80.0f, 60.0f, currentColor, isFilled));
                        break;
                    case TRIANGLE:
                        canvas.addShape(std::make_unique<Triangle>(
                            worldMousePos, 
                            {worldMousePos.x + 50, worldMousePos.y + 80}, 
                            {worldMousePos.x - 50, worldMousePos.y + 80}, 
                            currentColor, isFilled));
                        break;
                    case STAR:
                        canvas.addShape(std::make_unique<Star>(worldMousePos, 40.0f, 15.0f, 5, currentColor, isFilled));
                        break;
                    case POLYGON:
                        canvas.addShape(std::make_unique<PolygonShape>(worldMousePos, 40.0f, polySides, currentColor, isFilled));
                        break;
                    case TEXT:
                        canvas.addShape(std::make_unique<TextShape>(worldMousePos, "RSK World 2026", 30.0f, currentColor));
                        break;
                }
            }
        }

        if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) && selectedShape) {
            selectedShape->setPosition({ worldMousePos.x - dragOffset.x, worldMousePos.y - dragOffset.y });
        }

        if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) {
            selectedShape = nullptr;
        }

        // Transformations on selected/hovered
        Shape* target = selectedShape;
        if (!target) target = canvas.getHoveredShape(worldMousePos);

        if (target) {
            if (IsKeyDown(KEY_Q)) target->rotate(-2.0f);
            if (IsKeyDown(KEY_E)) target->rotate(2.0f);
            
            if (IsKeyDown(KEY_PLUS)) target->setScale(target->getScale() + 0.02f);
            if (IsKeyDown(KEY_MINUS)) target->setScale(target->getScale() - 0.02f);

            // Alpha (Alt + scroll/keys)
            if (IsKeyDown(KEY_LEFT_ALT)) {
                if (IsKeyDown(KEY_UP)) target->setAlpha(target->getAlpha() + 0.01f);
                if (IsKeyDown(KEY_DOWN)) target->setAlpha(target->getAlpha() - 0.01f);
            }

            // Animation (Press A)
            if (IsKeyPressed(KEY_A)) {
                int nextAnim = ((int)target->getAnimation() + 1) % 4;
                target->setAnimation((AnimationType)nextAnim);
            }

            // Gradient (Press G)
            if (IsKeyPressed(KEY_G)) {
                target->setGradient(!target->getGradient(), ColorBrightness(target->getColor(), -0.5f));
            }

            if (IsKeyPressed(KEY_PAGE_UP)) canvas.bringToFront(target);
            if (IsKeyPressed(KEY_PAGE_DOWN)) canvas.sendToBack(target);
        }

        // Keys 1-6
        if (IsKeyPressed(KEY_ONE)) currentType = CIRCLE;
        if (IsKeyPressed(KEY_TWO)) currentType = RECTANGLE;
        if (IsKeyPressed(KEY_THREE)) currentType = TRIANGLE;
        if (IsKeyPressed(KEY_FOUR)) currentType = STAR;
        if (IsKeyPressed(KEY_FIVE)) currentType = POLYGON;
        if (IsKeyPressed(KEY_SIX)) currentType = TEXT;

        if (IsKeyPressed(KEY_R)) currentColor = RED;
        if (IsKeyPressed(KEY_G) && !IsKeyDown(KEY_LEFT_CONTROL)) currentColor = LIME;
        if (IsKeyPressed(KEY_B)) currentColor = BLUE;
        if (IsKeyPressed(KEY_Y)) currentColor = YELLOW;
        if (IsKeyPressed(KEY_P)) currentColor = PURPLE;
        if (IsKeyPressed(KEY_F)) isFilled = !isFilled;
        if (IsKeyPressed(KEY_C)) canvas.clear();
        if (IsKeyPressed(KEY_Z)) canvas.undo();
        if (IsKeyPressed(KEY_K)) canvas.saveToFile("masterpiece_save.txt");
        if (IsKeyPressed(KEY_L)) canvas.loadFromFile("masterpiece_save.txt");
        // ----------------------------------------------------------------------------------

        // Draw
        // ----------------------------------------------------------------------------------
        BeginDrawing();
            ClearBackground(RAYWHITE);

            BeginMode2D(camera);
                canvas.drawAll();
                if (target) DrawCircleLinesV(target->getPosition(), 10, RED);
            EndMode2D();

            // UI
            DrawRectangle(0, 0, screenWidth, 100, Fade(BLACK, 0.8f));
            DrawText("1-6: TYPE | R/G/B/Y/P: Color | F: Fill | C: Clear | Z: Undo | K/L: Save/Load", 10, 10, 17, WHITE);
            DrawText("DRAG: Move | RMB: Pan | SCROLL: Zoom | Q/E: Rotate | +/- Scale | ALT+Arrows: Alpha", 10, 32, 17, LIGHTGRAY);
            DrawText("A: Toggle ANIMATION (Pulse/Bounce/Shake) | G: Toggle GRADIENT | PgUp/Dn: Layers", 10, 54, 17, SKYBLUE);
            DrawText("MASTERPIECE EDITION - Molla Samser (rskworld.in) - 2026", 10, 76, 17, GOLD);

            DrawRectangle(0, screenHeight - 30, screenWidth, 30, Fade(BLACK, 0.9f));
            DrawText(TextFormat("Zoom: %.2f | Shapes: %d", camera.zoom, canvas.getCount()), 10, screenHeight - 25, 18, WHITE);
            DrawText("rskworld.in", screenWidth - 110, screenHeight - 25, 18, GOLD);
        EndDrawing();
    }

    // De-Initialization
    CloseWindow();

    return 0;
}
201 lines•8.1 KB
cpp

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer