Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rectangle shape render from the bottom of my window  (Read 2048 times)

0 Members and 3 Guests are viewing this topic.

Kammil

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Rectangle shape render from the bottom of my window
« on: November 02, 2018, 03:09:00 am »
Hi, i kinda found a way around my problem, but since all origin(I think) are suppose to be in top left corner I just dont get this one :

I was trying to draw rectangle (for menu button) from the bottom right of the window with this function :

Context :

int tailleFenetre[2] = {static_cast<int>(fenetre.getSize().x),static_cast<int>(fenetre.getSize().y)};
const int MAIN_MENU_SIZE = 4;
const int MENU_LARGEUR = 120, MENU_HAUTEUR = 40;

my function :

void buildMenu(int tailleFenetre[],string optionsMenu[],sf::Texture* ptrTexture){

    sf::RectangleShape menuButton;
    menuButton.setSize(sf::Vector2f(MENU_LARGEUR,(MENU_HAUTEUR-5)));
    menuButton.setFillColor(sf::Color(221,133,175));

    sf::RenderTexture texture1;
    if(!texture1.create(tailleFenetre[0],tailleFenetre[1]))
       
         for(int i = 0; i < MAIN_MENU_SIZE; i++){

            menuButton.setPosition(sf::Vector2f((tailleFenetre[0]-MENU_LARGEUR-5),(tailleFenetre[1]-(MENU_HAUTEUR * i)-(MENU_HAUTEUR+5))));
            texture1.draw(menuButton);
        }

    texture1.draw(menuButton);
    *ptrTexture = texture1.getTexture();
}

I thought from that i should have gotten my rectangles in the bottom right corner but they drew on top right.

and if I replace my for loop with : 

menuButton.setPosition(sf::Vector2f((tailleFenetre[0]-MENU_LARGEUR-5),5));

where menuButton.setPosition().y == 5; it draws the rectangle in bottom right.


I'm a bit confuse, can someone explain ?
« Last Edit: November 02, 2018, 08:21:18 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rectangle shape render from the bottom of my window
« Reply #1 on: November 02, 2018, 08:22:21 am »
texture1.display();
Laurent Gomila - SFML developer

Kammil

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Rectangle shape render from the bottom of my window
« Reply #2 on: November 02, 2018, 11:11:08 am »
Hi, yea, actually I had it before, but ill have to show you then entire thing because when I was running the program with it, it would display the rectangles at the right place and then display the glitched one in the top right corner.

I call the function before the window loop to settup the screen for main menue ( since I just re-call the function when I need to) and when I call it at that moment it renders fine.

#include <iostream>
#include "Menu.h"
#include "Constante.h"
#include <SFML/Graphics.hpp>
#include <string>

using namespace std;

int loopCount = 0;

// MENU PRINCIPALE
string quitter("Quitter");
string options("Options");
string generateur("Generateur");
string Dessins("Dessin");
string optionsMenu[MAIN_MENU_SIZE] = {quitter, options,generateur,Dessins};

int main(){
       // Creation de la fenetre
    sf::RenderWindow fenetre(sf::VideoMode(600, 600), "Menu");
    int tailleFenetre[2] = {static_cast<int>(fenetre.getSize().x),static_cast<int>(fenetre.getSize().y)};
    fenetre.setFramerateLimit(100);
    sf::Texture texture;

    if(!texture.create(tailleFenetre[0],tailleFenetre[1])){
        cout << "cant create image " << endl;
    }

    sf::Texture* ptrTexture = &texture;

        // FONT
    sf::Font font;
    if(!font.loadFromFile("font/Cinzel/Cinzel-Regular.otf")){
        cout << "cant load font" << endl;
        }

        //TIME MANAGEMENT
    sf::Clock clock;
    sf::Time time;                                     // variable temps
    int fps = 0;                                         // variable frame/seconds
    string fpsString = to_string(fps);          // envoie la variable frame/secondes dans un string
        //construction variable text
    sf::Text text1(fpsString,font,12);
    text1.setFillColor(sf::Color::Black);
    text1.setPosition((tailleFenetre[0]-50),(tailleFenetre[1]*0));


    buildMenu(tailleFenetre,optionsMenu, ptrTexture);
    while (fenetre.isOpen()){

        time = clock.getElapsedTime();
        int timer = static_cast<int>(time.asSeconds());

            //update frame per seconds
        string toText = ((to_string(static_cast<int>(loopCount/time.asSeconds()))) + " " + (to_string(timer)));

        text1.setString(toText);

//this is to prevent loading the menu without need
        sf::IntRect mouseZone(sf::Vector2i((tailleFenetre[0]-MENU_LARGEUR),(tailleFenetre[1] - MAIN_MENU_SIZE*MENU_HAUTEUR)),sf::Vector2i( (MENU_LARGEUR),(MAIN_MENU_SIZE*MENU_HAUTEUR))) ;

        if(mouseZone.contains(sf::Mouse::getPosition(fenetre))){
            buildMenu(tailleFenetre,optionsMenu, ptrTexture);
            }

        // Process events
        sf::Event event;
        while (fenetre.pollEvent(event))
        {
            // Close window: exit
            switch (event.type){
                case sf::Event::Closed :
                    fenetre.close();
                    break;

                default :
                    break;
            }
        }
    sf::Sprite game(texture);
    fenetre.clear(sf::Color::White);
    fenetre.draw(text1);
    fenetre.draw(game);
    fenetre.display();
    if(loopCount <= 500000){
        loopCount++;
        }
    }
« Last Edit: November 02, 2018, 11:14:37 am by Kammil »

Kammil

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Rectangle shape render from the bottom of my window
« Reply #3 on: November 02, 2018, 01:03:58 pm »
OK, i figured what i was doing wrong :

Missed a Rendertexture.clear() after creating the it and was also missing the rendertexture.display() after the for loop. thanks !

sf::RenderTexture texture1;
    if(!texture1.create(taillefenetre[0],taillefenetre[1]))
        cout << "cant create texture1 for render texture" << endl;
    texture1.clear(sf::Color::White);
       for(int i = 0; i < MAIN_MENU_SIZE; i++){

            menuButton.setPosition(sf::Vector2f((taillefenetre[0]-MENU_LARGEUR-5),(taillefenetre[1]-(MENU_HAUTEUR * i)-(MENU_HAUTEUR+5))));
            texture1.draw(menuButton);
        }

    texture1.draw(menuButton);
    texture1.display();
    *ptrTexture = texture1.getTexture();

 

anything