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
/
include
RSK World
cpp-shape-drawer
C++ Shape Drawer - Advanced Graphics Application + Raylib + OOP Design + Shape Tools + Transformation + Export + Educational Design
include
  • Circle.hpp2 KB
  • DrawingCanvas.hpp7.3 KB
  • PatternGenerator.hpp1.6 KB
  • Polygon.hpp2.3 KB
  • RectangleShape.hpp2.7 KB
  • Shape.hpp4.2 KB
  • Star.hpp2.8 KB
  • TextShape.hpp2 KB
  • Triangle.hpp2.5 KB
DrawingCanvas.hpp
include/DrawingCanvas.hpp
Raw Download
Find: Go to:
/**
 * @file DrawingCanvas.hpp
 * @brief Manages a collection of shapes and handles rendering.
 * 
 * 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
 */

#ifndef DRAWING_CANVAS_HPP
#define DRAWING_CANVAS_HPP

#include "Shape.hpp"
#include "Circle.hpp"
#include "RectangleShape.hpp"
#include "Triangle.hpp"
#include "Star.hpp"
#include "Polygon.hpp"
#include "TextShape.hpp"
#include <vector>
#include <memory>
#include <fstream>
#include <sstream>
#include <algorithm>

class DrawingCanvas {
private:
    std::vector<std::unique_ptr<Shape>> shapes;

public:
    void addShape(std::unique_ptr<Shape> shape) {
        shapes.push_back(std::move(shape));
    }

    void updateAll(float deltaTime) {
        for (auto& shape : shapes) {
            shape->update(deltaTime);
        }
    }

    Shape* getHoveredShape(Vector2 point) {
        // Search from top to bottom (reverse list)
        for (auto it = shapes.rbegin(); it != shapes.rend(); ++it) {
            if ((*it)->contains(point)) {
                return it->get();
            }
        }
        return nullptr;
    }

    void drawAll() const {
        for (const auto& shape : shapes) {
            shape->draw();
        }
    }

    void clear() {
        shapes.clear();
    }

    void undo() {
        if (!shapes.empty()) {
            shapes.pop_back();
        }
    }

    void bringToFront(Shape* target) {
        auto it = std::find_if(shapes.begin(), shapes.end(), [&](const std::unique_ptr<Shape>& s) {
            return s.get() == target;
        });
        if (it != shapes.end()) {
            std::unique_ptr<Shape> s = std::move(*it);
            shapes.erase(it);
            shapes.push_back(std::move(s));
        }
    }

    void sendToBack(Shape* target) {
        auto it = std::find_if(shapes.begin(), shapes.end(), [&](const std::unique_ptr<Shape>& s) {
            return s.get() == target;
        });
        if (it != shapes.end()) {
            std::unique_ptr<Shape> s = std::move(*it);
            shapes.erase(it);
            shapes.insert(shapes.begin(), std::move(s));
        }
    }

    void saveToFile(const std::string& filename) {
        std::ofstream file(filename);
        if (file.is_open()) {
            for (const auto& shape : shapes) {
                file << shape->serialize() << "\n";
            }
            file.close();
        }
    }

    void loadFromFile(const std::string& filename) {
        std::ifstream file(filename);
        if (file.is_open()) {
            shapes.clear();
            std::string line;
            while (std::getline(file, line)) {
                std::stringstream ss(line);
                std::string type, token;
                std::vector<std::string> parts;
                while (std::getline(ss, token, ',')) {
                    parts.push_back(token);
                }
                if (parts.size() < 10) continue;

                type = parts[0];
                Shape* s_ptr = nullptr;

                if (type == "CIRCLE") {
                    auto s = std::make_unique<Circle>((Vector2){std::stof(parts[1]), std::stof(parts[2])}, std::stof(parts[3]), 
                             (Color){(unsigned char)std::stoi(parts[4]), (unsigned char)std::stoi(parts[5]), (unsigned char)std::stoi(parts[6]), 255}, std::stoi(parts[8]));
                    s->setAlpha(std::stof(parts[7])); s->setRotation(std::stof(parts[9])); s->setScale(std::stof(parts[10]));
                    s_ptr = s.get();
                    addShape(std::move(s));
                } else if (type == "RECT") {
                    auto s = std::make_unique<RectangleShape>((Vector2){std::stof(parts[1]), std::stof(parts[2])}, std::stof(parts[3]), std::stof(parts[4]),
                             (Color){(unsigned char)std::stoi(parts[5]), (unsigned char)std::stoi(parts[6]), (unsigned char)std::stoi(parts[7]), 255}, std::stoi(parts[9]));
                    s->setAlpha(std::stof(parts[8])); s->setRotation(std::stof(parts[10])); s->setScale(std::stof(parts[11]));
                    s_ptr = s.get();
                    addShape(std::move(s));
                } else if (type == "TEXT") {
                    auto s = std::make_unique<TextShape>((Vector2){std::stof(parts[1]), std::stof(parts[2])}, parts[3], std::stof(parts[4]),
                             (Color){(unsigned char)std::stoi(parts[5]), (unsigned char)std::stoi(parts[6]), (unsigned char)std::stoi(parts[7]), 255});
                    s->setAlpha(std::stof(parts[8])); s->setRotation(std::stof(parts[10])); s->setScale(std::stof(parts[11]));
                    s_ptr = s.get();
                    addShape(std::move(s));
                } else if (type == "STAR") {
                    auto s = std::make_unique<Star>((Vector2){std::stof(parts[1]), std::stof(parts[2])}, std::stof(parts[3]), std::stof(parts[4]), std::stoi(parts[5]),
                             (Color){(unsigned char)std::stoi(parts[6]), (unsigned char)std::stoi(parts[7]), (unsigned char)std::stoi(parts[8]), 255}, std::stoi(parts[10]));
                    s->setAlpha(std::stof(parts[9])); s->setRotation(std::stof(parts[11])); s->setScale(std::stof(parts[12]));
                    s_ptr = s.get();
                    addShape(std::move(s));
                } else if (type == "POLY") {
                    auto s = std::make_unique<PolygonShape>((Vector2){std::stof(parts[1]), std::stof(parts[2])}, std::stof(parts[3]), std::stoi(parts[4]),
                             (Color){(unsigned char)std::stoi(parts[5]), (unsigned char)std::stoi(parts[6]), (unsigned char)std::stoi(parts[7]), 255}, std::stoi(parts[9]));
                    s->setAlpha(std::stof(parts[8])); s->setRotation(std::stof(parts[10])); s->setScale(std::stof(parts[11]));
                    s_ptr = s.get();
                    addShape(std::move(s));
                } else if (type == "TRI") {
                    auto s = std::make_unique<Triangle>((Vector2){std::stof(parts[1]), std::stof(parts[2])}, (Vector2){std::stof(parts[3]), std::stof(parts[4])}, (Vector2){std::stof(parts[5]), std::stof(parts[6])},
                             (Color){(unsigned char)std::stoi(parts[7]), (unsigned char)std::stoi(parts[8]), (unsigned char)std::stoi(parts[9]), 255}, (bool)std::stoi(parts[11]));
                    s->setAlpha(std::stof(parts[10])); s->setRotation(std::stof(parts[12])); s->setScale(std::stof(parts[13]));
                    s_ptr = s.get();
                    addShape(std::move(s));
                }

                if (s_ptr && parts.size() > 12) {
                    int n = (int)parts.size();
                    bool grad = std::stoi(parts[n-5]);
                    Color sec = {(unsigned char)std::stoi(parts[n-4]), (unsigned char)std::stoi(parts[n-3]), (unsigned char)std::stoi(parts[n-2]), 255};
                    s_ptr->setGradient(grad, sec);
                    s_ptr->setAnimation((AnimationType)std::stoi(parts[n-1]));
                }
            }
            file.close();
        }
    }

    size_t getCount() const { return shapes.size(); }
};

#endif // DRAWING_CANVAS_HPP
176 lines•7.3 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