1
Graphics / Windows Resize is acting weird
« on: December 11, 2021, 09:51:56 pm »
So I wanted to use SFML Graphics to make kind of a menu for something I have to do at college. I do want to say that I am new to this and I don't know a lot about SFML.
In the first picture you can see that everything looks good but after I resize it, everything goes bad... The positions of the squares and their sizes are based on the window size so I have no clue why it looks like that.
Here is my code, sorry if it is long and that a few variables and name functions are written in Romanian and not in English.
In the first picture you can see that everything looks good but after I resize it, everything goes bad... The positions of the squares and their sizes are based on the window size so I have no clue why it looks like that.
Here is my code, sorry if it is long and that a few variables and name functions are written in Romanian and not in English.
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
float lungime_start;
float latime_start;
float lungime_finish;
float latime_finish;
float inaltime_input;
float inaltime_output;
float lungime_input;
float lungime_output;
float lungime_max_trapez;
float lungime_min_trapez;
float inaltime_trapez;
float lungime_conditie;
float MarimeText;
sf::Color culoare = sf::Color(159,39,9);
sf::Color outline = sf::Color(85,24,9);
sf::Color CuloareText = sf::Color(255,255,255);
sf::Color CuloareLinii = sf::Color(8,228,237);
sf::Color CuloareMeniuButoane = sf::Color(30,9,121);
sf::Color CuloareBackground = sf::Color(9, 70, 121);
sf::RectangleShape Instructiuni(sf::Vector2f(1.f,1.f));
sf::RectangleShape Meniu1(sf::Vector2f(1.f, 1.f));
sf::RectangleShape Meniu2(sf::Vector2f(1.f, 1.f));
sf::RectangleShape Meniu3(sf::Vector2f(1.f, 1.f));
sf::RectangleShape GenerareCod(sf::Vector2f(1.f, 1.f));
//tipuri de operatie ce pot fi realizate de catre blocurile grafice
enum tip_operatie {
start = 's',
finish = 'f',
input = 'i',
output = 'o',
assignment = 'a',
conditie = 'c',
};
void DesenareText(string Cuvant, sf::Color Culoare, float Marime, float CoordX, float CoordY, sf::RenderWindow* Pointer)
{
sf::Font font;
sf::Text Text;
font.loadFromFile("arial.ttf");
Text.setFont(font);
Text.setString(Cuvant);
Text.setFillColor(Culoare);
Text.setCharacterSize(Marime);
Text.setStyle(sf::Text::Bold);
Text.setOrigin(Text.getGlobalBounds().width / 2, Text.getGlobalBounds().height);
Text.setPosition(CoordX, CoordY);
Pointer->draw(Text);
}
void creareButoane(tip_operatie a, float x, float y, sf::RenderWindow* pointer) {
switch (a) {
case start: {
sf::RectangleShape start(sf::Vector2f(lungime_start, latime_start));
start.setFillColor(culoare);
start.setOutlineThickness(5.f);
start.setOutlineColor(outline);
start.setOrigin(start.getGlobalBounds().width / 2, start.getGlobalBounds().height / 2);
start.setPosition(x, y);
pointer->draw(start);
break;
}
case finish: {
sf::RectangleShape finish(sf::Vector2f(lungime_finish, latime_finish));
finish.setFillColor(culoare);
finish.setOutlineThickness(5.f);
finish.setOutlineColor(outline);
finish.setOrigin(finish.getGlobalBounds().width / 2, finish.getGlobalBounds().height / 2);
finish.setPosition(x, y);
pointer->draw(finish);
break;
}
case input: {
sf::ConvexShape input(3);
input.setFillColor(culoare);
input.setOutlineThickness(5.f);
input.setOutlineColor(outline);
input.setOrigin(input.getGlobalBounds().width / 2, input.getGlobalBounds().height / 2);
input.setPoint(0, { x,y - (inaltime_input / 2) - (inaltime_input / 4) });
input.setPoint(1, { x - (lungime_input / 2),y + (inaltime_input / 2) });
input.setPoint(2, { x + (lungime_input / 2),y + (inaltime_input / 2) });
pointer->draw(input);
break;
}
case output: {
sf::ConvexShape output(3);
output.setFillColor(culoare);
output.setOutlineThickness(5.f);
output.setOutlineColor(outline);
output.setOrigin(output.getGlobalBounds().width / 2, output.getGlobalBounds().height / 2);
output.setPoint(0,{x,y+(inaltime_output/2)+(inaltime_output/4)});
output.setPoint(1, { x-(lungime_output/2),y - (inaltime_output / 2) });
output.setPoint(2, { x+(lungime_output/2),y - (inaltime_output / 2) });
pointer->draw(output);
break;
}
case assignment: {
sf::ConvexShape assignment(4);
assignment.setFillColor(culoare);
assignment.setOutlineThickness(5.f);
assignment.setOutlineColor(outline);
assignment.setOrigin(assignment.getGlobalBounds().width / 2, assignment.getGlobalBounds().height / 2);
assignment.setPoint(2, { x,y + inaltime_trapez });
assignment.setPoint(3, { x + lungime_max_trapez,y + inaltime_trapez });
assignment.setPoint(0, { x + ((lungime_max_trapez - lungime_min_trapez) / 2) + lungime_min_trapez,y });
assignment.setPoint(1, { x + ((lungime_max_trapez - lungime_min_trapez) / 2),y });
pointer->draw(assignment);
break;
}
case conditie: {
sf::CircleShape conditie(lungime_conditie, 4);
conditie.setFillColor(culoare);
conditie.setOutlineThickness(5.f);
conditie.setOutlineColor(outline);
conditie.setPosition(x-lungime_conditie, y);
pointer->draw(conditie);
break;
}
}
}
void DesenareLinie(sf::Color Culoare, float Lungime, float Latime, float Rotatie, float x, float y,sf::RenderWindow* Pointer)
{
sf::RectangleShape line(sf::Vector2f(Lungime, Latime));
line.setPosition(x, y);
line.rotate(Rotatie);
line.setFillColor(Culoare);
Pointer->draw(line);
}
void creareMeniuPrincipal(sf::RenderWindow* pointer) {
float h = pointer->getSize().y / 7;
float l = pointer->getSize().x / 5;
lungime_start = l / 2;
latime_start = h / 2;
lungime_finish = l / 2;
latime_finish = h / 2;
inaltime_input = h / 2;
inaltime_output = h / 2;
lungime_input = l / 2;
lungime_output = l / 2;
lungime_max_trapez = l / 2;
lungime_min_trapez = l / 3;
inaltime_trapez = h / 2;
lungime_conditie = l / 5;
MarimeText = (l + h) / 18;
Instructiuni.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
Meniu1.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
Meniu2.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
Meniu3.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
GenerareCod.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
Instructiuni.setFillColor(CuloareMeniuButoane);
Meniu1.setFillColor(CuloareMeniuButoane);
Meniu2.setFillColor(CuloareMeniuButoane);
Meniu3.setFillColor(CuloareMeniuButoane);
GenerareCod.setFillColor(CuloareMeniuButoane);
Instructiuni.setPosition(l / 50 , h / 20);
Meniu1.setPosition(l+(l / 50), h / 20);
Meniu2.setPosition(l*2+(l / 50), h / 20);
Meniu3.setPosition(l*3+(l / 50), h / 20);
GenerareCod.setPosition(l*4+(l / 50), h / 20);
pointer->draw(Instructiuni);
pointer->draw(Meniu1);
pointer->draw(Meniu2);
pointer->draw(Meniu3);
pointer->draw(GenerareCod);
creareButoane(start, l / 2, (h + (h / 2)), pointer);
DesenareText("Start", CuloareText, MarimeText, l / 2, h+(h/2), pointer);
creareButoane(finish, l / 2, h * 2 + (h / 2), pointer);
DesenareText("Finish", CuloareText, MarimeText, l / 2, h*2 + (h / 2), pointer);
creareButoane(input, l / 2, h * 3 + (h / 2), pointer);
DesenareText("Input", CuloareText, MarimeText, l / 2, h*3 + (h / 2)+h/10, pointer);
creareButoane(output, l / 2, h * 4 + (h / 2), pointer);
DesenareText("Output", CuloareText, MarimeText, l / 2, h*4 + (h / 2), pointer);
creareButoane(assignment, l/4, h * 5 + (h / 3), pointer);
DesenareText("Assignment", CuloareText, MarimeText, l / 2, h*5 + h/1.5, pointer);
creareButoane(conditie, l / 2, h * 6 + (h / 4), pointer);
DesenareText("Condition", CuloareText, MarimeText, l / 2, h * 6 + (h / 1.25), pointer);
DesenareText("Instructiuni", CuloareText, MarimeText, l / 2, h / 2, pointer);
DesenareText("Buton Meniu 1", CuloareText, MarimeText, l+l/2, h/2, pointer);
DesenareText("Buton Meniu 2", CuloareText, MarimeText, l*2 + l / 2, h / 2, pointer);
DesenareText("Buton Meniu 3", CuloareText, MarimeText, l*3 + l / 2, h / 2, pointer);
DesenareText("Generare Cod", CuloareText, MarimeText, l*4 + l / 2, h / 2, pointer);
DesenareLinie(CuloareLinii, l*5, 5, 0, 0, h, pointer);
DesenareLinie(CuloareLinii, h*7, 5, 90, l, h, pointer);
DesenareLinie(CuloareLinii, h*7, 5, 90, l*4, h, pointer);
}
int main()
{
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height), "Interschem", sf::Style::Default,settings);
sf::Event event;
sf::RenderWindow* pointer = &window;
while (window.isOpen())
{
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
if (event.type == sf::Event::TextEntered)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::T))
{
cout << pointer->getSize().x << " " << pointer->getSize().y;
}
}
}
window.clear(CuloareBackground);
creareMeniuPrincipal(pointer);
window.display();
}
return 0;
}
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
float lungime_start;
float latime_start;
float lungime_finish;
float latime_finish;
float inaltime_input;
float inaltime_output;
float lungime_input;
float lungime_output;
float lungime_max_trapez;
float lungime_min_trapez;
float inaltime_trapez;
float lungime_conditie;
float MarimeText;
sf::Color culoare = sf::Color(159,39,9);
sf::Color outline = sf::Color(85,24,9);
sf::Color CuloareText = sf::Color(255,255,255);
sf::Color CuloareLinii = sf::Color(8,228,237);
sf::Color CuloareMeniuButoane = sf::Color(30,9,121);
sf::Color CuloareBackground = sf::Color(9, 70, 121);
sf::RectangleShape Instructiuni(sf::Vector2f(1.f,1.f));
sf::RectangleShape Meniu1(sf::Vector2f(1.f, 1.f));
sf::RectangleShape Meniu2(sf::Vector2f(1.f, 1.f));
sf::RectangleShape Meniu3(sf::Vector2f(1.f, 1.f));
sf::RectangleShape GenerareCod(sf::Vector2f(1.f, 1.f));
//tipuri de operatie ce pot fi realizate de catre blocurile grafice
enum tip_operatie {
start = 's',
finish = 'f',
input = 'i',
output = 'o',
assignment = 'a',
conditie = 'c',
};
void DesenareText(string Cuvant, sf::Color Culoare, float Marime, float CoordX, float CoordY, sf::RenderWindow* Pointer)
{
sf::Font font;
sf::Text Text;
font.loadFromFile("arial.ttf");
Text.setFont(font);
Text.setString(Cuvant);
Text.setFillColor(Culoare);
Text.setCharacterSize(Marime);
Text.setStyle(sf::Text::Bold);
Text.setOrigin(Text.getGlobalBounds().width / 2, Text.getGlobalBounds().height);
Text.setPosition(CoordX, CoordY);
Pointer->draw(Text);
}
void creareButoane(tip_operatie a, float x, float y, sf::RenderWindow* pointer) {
switch (a) {
case start: {
sf::RectangleShape start(sf::Vector2f(lungime_start, latime_start));
start.setFillColor(culoare);
start.setOutlineThickness(5.f);
start.setOutlineColor(outline);
start.setOrigin(start.getGlobalBounds().width / 2, start.getGlobalBounds().height / 2);
start.setPosition(x, y);
pointer->draw(start);
break;
}
case finish: {
sf::RectangleShape finish(sf::Vector2f(lungime_finish, latime_finish));
finish.setFillColor(culoare);
finish.setOutlineThickness(5.f);
finish.setOutlineColor(outline);
finish.setOrigin(finish.getGlobalBounds().width / 2, finish.getGlobalBounds().height / 2);
finish.setPosition(x, y);
pointer->draw(finish);
break;
}
case input: {
sf::ConvexShape input(3);
input.setFillColor(culoare);
input.setOutlineThickness(5.f);
input.setOutlineColor(outline);
input.setOrigin(input.getGlobalBounds().width / 2, input.getGlobalBounds().height / 2);
input.setPoint(0, { x,y - (inaltime_input / 2) - (inaltime_input / 4) });
input.setPoint(1, { x - (lungime_input / 2),y + (inaltime_input / 2) });
input.setPoint(2, { x + (lungime_input / 2),y + (inaltime_input / 2) });
pointer->draw(input);
break;
}
case output: {
sf::ConvexShape output(3);
output.setFillColor(culoare);
output.setOutlineThickness(5.f);
output.setOutlineColor(outline);
output.setOrigin(output.getGlobalBounds().width / 2, output.getGlobalBounds().height / 2);
output.setPoint(0,{x,y+(inaltime_output/2)+(inaltime_output/4)});
output.setPoint(1, { x-(lungime_output/2),y - (inaltime_output / 2) });
output.setPoint(2, { x+(lungime_output/2),y - (inaltime_output / 2) });
pointer->draw(output);
break;
}
case assignment: {
sf::ConvexShape assignment(4);
assignment.setFillColor(culoare);
assignment.setOutlineThickness(5.f);
assignment.setOutlineColor(outline);
assignment.setOrigin(assignment.getGlobalBounds().width / 2, assignment.getGlobalBounds().height / 2);
assignment.setPoint(2, { x,y + inaltime_trapez });
assignment.setPoint(3, { x + lungime_max_trapez,y + inaltime_trapez });
assignment.setPoint(0, { x + ((lungime_max_trapez - lungime_min_trapez) / 2) + lungime_min_trapez,y });
assignment.setPoint(1, { x + ((lungime_max_trapez - lungime_min_trapez) / 2),y });
pointer->draw(assignment);
break;
}
case conditie: {
sf::CircleShape conditie(lungime_conditie, 4);
conditie.setFillColor(culoare);
conditie.setOutlineThickness(5.f);
conditie.setOutlineColor(outline);
conditie.setPosition(x-lungime_conditie, y);
pointer->draw(conditie);
break;
}
}
}
void DesenareLinie(sf::Color Culoare, float Lungime, float Latime, float Rotatie, float x, float y,sf::RenderWindow* Pointer)
{
sf::RectangleShape line(sf::Vector2f(Lungime, Latime));
line.setPosition(x, y);
line.rotate(Rotatie);
line.setFillColor(Culoare);
Pointer->draw(line);
}
void creareMeniuPrincipal(sf::RenderWindow* pointer) {
float h = pointer->getSize().y / 7;
float l = pointer->getSize().x / 5;
lungime_start = l / 2;
latime_start = h / 2;
lungime_finish = l / 2;
latime_finish = h / 2;
inaltime_input = h / 2;
inaltime_output = h / 2;
lungime_input = l / 2;
lungime_output = l / 2;
lungime_max_trapez = l / 2;
lungime_min_trapez = l / 3;
inaltime_trapez = h / 2;
lungime_conditie = l / 5;
MarimeText = (l + h) / 18;
Instructiuni.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
Meniu1.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
Meniu2.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
Meniu3.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
GenerareCod.setSize(sf::Vector2f(l - (l / 20), h - (h / 10)));
Instructiuni.setFillColor(CuloareMeniuButoane);
Meniu1.setFillColor(CuloareMeniuButoane);
Meniu2.setFillColor(CuloareMeniuButoane);
Meniu3.setFillColor(CuloareMeniuButoane);
GenerareCod.setFillColor(CuloareMeniuButoane);
Instructiuni.setPosition(l / 50 , h / 20);
Meniu1.setPosition(l+(l / 50), h / 20);
Meniu2.setPosition(l*2+(l / 50), h / 20);
Meniu3.setPosition(l*3+(l / 50), h / 20);
GenerareCod.setPosition(l*4+(l / 50), h / 20);
pointer->draw(Instructiuni);
pointer->draw(Meniu1);
pointer->draw(Meniu2);
pointer->draw(Meniu3);
pointer->draw(GenerareCod);
creareButoane(start, l / 2, (h + (h / 2)), pointer);
DesenareText("Start", CuloareText, MarimeText, l / 2, h+(h/2), pointer);
creareButoane(finish, l / 2, h * 2 + (h / 2), pointer);
DesenareText("Finish", CuloareText, MarimeText, l / 2, h*2 + (h / 2), pointer);
creareButoane(input, l / 2, h * 3 + (h / 2), pointer);
DesenareText("Input", CuloareText, MarimeText, l / 2, h*3 + (h / 2)+h/10, pointer);
creareButoane(output, l / 2, h * 4 + (h / 2), pointer);
DesenareText("Output", CuloareText, MarimeText, l / 2, h*4 + (h / 2), pointer);
creareButoane(assignment, l/4, h * 5 + (h / 3), pointer);
DesenareText("Assignment", CuloareText, MarimeText, l / 2, h*5 + h/1.5, pointer);
creareButoane(conditie, l / 2, h * 6 + (h / 4), pointer);
DesenareText("Condition", CuloareText, MarimeText, l / 2, h * 6 + (h / 1.25), pointer);
DesenareText("Instructiuni", CuloareText, MarimeText, l / 2, h / 2, pointer);
DesenareText("Buton Meniu 1", CuloareText, MarimeText, l+l/2, h/2, pointer);
DesenareText("Buton Meniu 2", CuloareText, MarimeText, l*2 + l / 2, h / 2, pointer);
DesenareText("Buton Meniu 3", CuloareText, MarimeText, l*3 + l / 2, h / 2, pointer);
DesenareText("Generare Cod", CuloareText, MarimeText, l*4 + l / 2, h / 2, pointer);
DesenareLinie(CuloareLinii, l*5, 5, 0, 0, h, pointer);
DesenareLinie(CuloareLinii, h*7, 5, 90, l, h, pointer);
DesenareLinie(CuloareLinii, h*7, 5, 90, l*4, h, pointer);
}
int main()
{
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height), "Interschem", sf::Style::Default,settings);
sf::Event event;
sf::RenderWindow* pointer = &window;
while (window.isOpen())
{
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
if (event.type == sf::Event::TextEntered)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::T))
{
cout << pointer->getSize().x << " " << pointer->getSize().y;
}
}
}
window.clear(CuloareBackground);
creareMeniuPrincipal(pointer);
window.display();
}
return 0;
}