From eed6defe91966fb6a5300d5ace303b39655ee2c7 Mon Sep 17 00:00:00 2001 From: Mypzik3D Date: Mon, 22 Apr 2024 20:29:28 +0200 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?src=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data-files.hpp | 9 ++ src/data.hpp | 257 +++++++++++++++++++++++++++++++++++++++++++++ src/draw.hpp | 13 +++ src/main.cpp | 41 ++++++++ 4 files changed, 320 insertions(+) create mode 100644 src/data-files.hpp create mode 100644 src/data.hpp create mode 100644 src/draw.hpp create mode 100644 src/main.cpp diff --git a/src/data-files.hpp b/src/data-files.hpp new file mode 100644 index 0000000..02fbcef --- /dev/null +++ b/src/data-files.hpp @@ -0,0 +1,9 @@ +#pragma once +#include + +sf::Font betterVCR; + +void get_files(){ + betterVCR.loadFromFile("../src/BetterVCR/BetterVCR_6.1.ttf"); + // betterVCR.loadFromFile("Arial.ttf"); +} diff --git a/src/data.hpp b/src/data.hpp new file mode 100644 index 0000000..cb1179b --- /dev/null +++ b/src/data.hpp @@ -0,0 +1,257 @@ +#pragma once +#include +#include +#include +#include "data-files.hpp" +#include + +sf::Vector2u old_size_window; + +void old_size(sf::RenderWindow& window){ + old_size_window = window.getSize(); +} + +class object; + +void RoundedRectangle(object& obj,sf::RenderWindow& window); +void rectangle_draw(sf::RenderWindow& window, object& obj); +void text_draw(sf::RenderWindow& window, object& obj); + +class object{ +public: + std::string name; + sf::Color color, outline_col; + + sf::Vector2f position; + sf::Vector2f size; + sf::Vector2f real_size=sf::Vector2f(0, 0), real_pos=sf::Vector2f(0, 0); + std::string type = "sprite"; + sf::String text; + bool isActive, isInput, statusInput, isWisible; + sf::Vector2i stick_pos = sf::Vector2i(0,0); + sf::Vector2i origin_pos = sf::Vector2i(-1,-1); + sf::Vector2i scale_pos = sf::Vector2i(0,0); + float rotate_angle = 0; + int layer; + float radius, outline; + object* stick_object; + + void draw(sf::RenderWindow& window){ + if(type == "sprite") + rectangle_draw(window, *this); + if(type == "round") + RoundedRectangle(*this, window); + if(type == "text") + text_draw(window, *this); + + } +}; + +object* null; + +void rectangle_draw(sf::RenderWindow& window, object& obj){ + sf::RectangleShape shape; + + float psX=obj.position.x; + float psY=obj.position.y; + float spX=obj.size.x; + float spY=obj.size.y; + + if(obj.scale_pos.x == 1 || obj.scale_pos.x == -1){ + spX = obj.size.x+(float)window.getSize().x/2-(float)old_size_window.x/2; + } + if(obj.scale_pos.y == 1 || obj.scale_pos.y == -1){ + spY = obj.size.y+(float)window.getSize().y/2-(float)old_size_window.y/2; + } + if(obj.scale_pos.x == 0){ + spX = obj.size.x+(float)window.getSize().x-(float)old_size_window.x; + } + if(obj.scale_pos.y == 0){ + spY = obj.size.y+(float)window.getSize().y-(float)old_size_window.y; + } + + obj.real_size.x = spX; + obj.real_size.y = spY; + + if(obj.stick_object != null){ + if(obj.stick_pos.x == 0){ + psX = ((float)obj.stick_object->real_size.x)/2-obj.real_size.x/2+obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == 1){ + psX = ((float)obj.stick_object->real_size.x)-obj.real_size.x-obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == -1){ + psX = (float)obj.position.x+obj.stick_object->real_pos.x; + } + + if(obj.stick_pos.y == 0){ + psY = ((float)obj.stick_object->real_size.y)/2-obj.real_size.y/2+obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == 1){ + psY = ((float)obj.stick_object->real_size.y)-obj.real_size.y-obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == -1){ + psY = (float)obj.position.y+obj.stick_object->real_pos.y; + } + } + obj.real_pos.x = psX; + obj.real_pos.y = psY; + + shape.setOutlineThickness(obj.outline); + shape.setOutlineColor(obj.outline_col); + shape.setSize(sf::Vector2f(spX, spY)); + shape.setPosition(sf::Vector2f(psX,psY)); + shape.setFillColor(obj.color); + window.draw(shape); +} + + +void RoundedRectangle(object& obj,sf::RenderWindow& window) +{ + int POINTS = obj.radius; + + float psX=obj.position.x; + float psY=obj.position.y; + float spX=obj.size.x; + float spY=obj.size.y; + + if(obj.scale_pos.x == 1 || obj.scale_pos.x == -1){ + spX = obj.size.x+(float)window.getSize().x/2-(float)old_size_window.x/2; + } + if(obj.scale_pos.y == 1 || obj.scale_pos.y == -1){ + spY = obj.size.y+(float)window.getSize().y/2-(float)old_size_window.y/2; + } + if(obj.scale_pos.x == 0){ + spX = obj.size.x+(float)window.getSize().x-(float)old_size_window.x; + } + if(obj.scale_pos.y == 0){ + spY = obj.size.y+(float)window.getSize().y-(float)old_size_window.y; + } + + obj.real_size.x = spX; + obj.real_size.y = spY; + + if(obj.stick_object != null){ + if(obj.stick_pos.x == 0){ + psX = ((float)obj.stick_object->real_size.x)/2-obj.real_size.x/2+obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == 1){ + psX = ((float)obj.stick_object->real_size.x)-obj.real_size.x-obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == -1){ + psX = (float)obj.position.x+obj.stick_object->real_pos.x; + } + + if(obj.stick_pos.y == 0){ + psY = ((float)obj.stick_object->real_size.y)/2-obj.real_size.y/2+obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == 1){ + psY = ((float)obj.stick_object->real_size.y)-obj.real_size.y-obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == -1){ + psY = (float)obj.position.y+obj.stick_object->real_pos.y; + } + } + obj.real_pos.x = psX; + obj.real_pos.y = psY; + + /* https://en.sfml-dev.org/forums/index.php?topic=973.0 */ + sf::ConvexShape rrect; + rrect.setPointCount(POINTS*4); + rrect.setOutlineThickness(obj.outline); + rrect.setOutlineColor(obj.outline_col); + rrect.setFillColor(obj.color); + float X=0,Y=0; + int a = 0; + for(int i=0; ireal_size.x)/2-obj.real_size.x/2+obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == 1){ + psX = ((float)obj.stick_object->real_size.x)-obj.real_size.x-obj.position.x+(float)obj.stick_object->real_pos.x; + } + if(obj.stick_pos.x == -1){ + psX = (float)obj.position.x+obj.stick_object->real_pos.x; + } + + if(obj.stick_pos.y == 0){ + psY = ((float)obj.stick_object->real_size.y)/2-obj.real_size.y/2+obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == 1){ + psY = ((float)obj.stick_object->real_size.y)-obj.real_size.y-obj.position.y+(float)obj.stick_object->real_pos.y; + } + if(obj.stick_pos.y == -1){ + psY = (float)obj.position.y+obj.stick_object->real_pos.y; + } + } + obj.real_pos.x = psX; + obj.real_pos.y = psY; + shape.setPosition(psX, psY); + window.draw(shape); +} + +object* new_object(std::vector>& massive,std::string name, float pos_x, float pos_y, float size_x, float size_y, object* obj_stick = null,int st_x=2,int st_y=0, sf::Color color=sf::Color::White){ + auto obj = std::make_unique(); + obj->name = name; + obj->position.x = pos_x; + obj->position.y = pos_y; + obj->size.x = size_x; + obj->size.y = size_y; + obj->color = color; + obj->stick_pos.x = st_x; + obj->stick_pos.y = st_y; + obj->stick_object = obj_stick; + massive.push_back(std::move(obj)); + return massive.back().get(); +} + + diff --git a/src/draw.hpp b/src/draw.hpp new file mode 100644 index 0000000..3c28b50 --- /dev/null +++ b/src/draw.hpp @@ -0,0 +1,13 @@ +#pragma once +#include +#include "data.hpp" + + +void update_window(sf::RenderWindow& window,std::vector>& objects){ + window.clear(); + for(const auto & i : objects){ + object* obj = i.get(); + obj->draw(window); + } + window.display(); +} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..cf9920b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,41 @@ +#include +#include "data-files.hpp" +#include "draw.hpp" +#include "data.hpp" +#include + + + +std::vector> objects; + +int main(){ + sf::ContextSettings settings; + settings.antialiasingLevel = 5; + sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!", sf::Style::Default, settings); old_size(window); get_files(); + + object* canvas = new_object(objects,"canvas" , 0, 0, 0, 0, null, 10, 10,sf::Color::Black); + canvas->size = sf::Vector2f(200,200); + + object* obj = new_object(objects,"center",0,0,120,50,canvas,-1,-1,sf::Color(120,100,200,100)); + obj->type = "round"; + obj->radius = 20; + + object* text = new_object(objects,"texte",0,0,30,0,obj,0,0,sf::Color::White); + text->type = "text"; + text->text = "UwU"; + + while (window.isOpen()){ + sf::Event event; + while (window.pollEvent(event)){ + if (event.type == sf::Event::Closed) + window.close(); + if (event.type == sf::Event::Resized){ + sf::FloatRect visible(0.f, 0.f, event.size.width, event.size.height); + window.setView(sf::View(visible)); + } + } + update_window(window,objects); + } + + return 0; +}