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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Kammil

Pages: [1]
1
SFML projects / My menu
« on: November 06, 2018, 04:27:08 pm »
Hi ! I just began c++ and found a Sfml tutorial and really liked it so I continued with it. Most tutorial show how to make games but i'm not much into that and more into tools so half way through the tutorial I just began to write my own codes(still referring to the tutorial alot for helps achieving and understanding what i was trying to do).

My goal is not really to finish something in particular, just to express what is inside my brain. That is why i'm not pushing to "advance" something over trying to code better as I've read alot.

I find my main function to be very hard to follow and that was the first pointer to ask for others opinion, but I would like some pointers at what i should try to work on(Not necessarily how to do it), perhaps just clues as to how I should structure the whole thing. Thanks in advance !

Here is the menu and 2 screenshots will follow. 

**Just the "quitter"(quit) menu buttons works for now as don't have any functionality to the app yet.

main.cpp
#include <iostream>
#include "Menu.h"
#include "Constante.h"

#include <SFML/Graphics.hpp>
#include <string>

using namespace std;

int loopCount = 0, selection = -1;

       
int main(){

       // Window creation and settings
    sf::RenderWindow fenetre(sf::VideoMode(600, 600), "Menu");
    fenetre.setFramerateLimit(100);
    sf::Image WindowIcon;
    if(!WindowIcon.loadFromFile("texture/cherry_leaf1.png"))
        cout << "cant load texture from file " << endl;
    fenetre.setIcon(60,60,WindowIcon.getPixelsPtr());
    fenetre.setTitle("Artisted");
    fenetre.setMouseCursorVisible(false);
   
    int tailleFenetre[4] = {static_cast<int>(fenetre.getSize().x),
                            static_cast<int>(fenetre.getSize().y),
                            sf::Mouse::getPosition(fenetre).x,
                            sf::Mouse::getPosition(fenetre).y
                            };


            // Image background for menu
    sf::Image image;
    sf::Image& ptrImage = image;
    if(!image.loadFromFile("texture/cherry_tree80%.png")){
        cout << "cant load image from file" << endl;
    }
            //Texture and sprite for background -->> rendertexture
    sf::Texture texture;
    if(!texture.loadFromImage(image)){
        cout << "cant load texture from image " << endl;
    }
        sf::Sprite cherryTree(texture);
    sf::Texture* ptrTexture = &texture;
   
            // for cursor
    sf::Texture cherryCursor;
    if(!cherryCursor.loadFromFile("texture/cherryCursor.png"))
        cout << "cant load texture from image " << endl;
    sf::Sprite cursorSprite(cherryCursor);
    sf::Transform CursorTransform;
    cursorSprite.setRotation(-45);
   
        // FONT
    sf::Font font;
    sf::Font &refFont = 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));

            // Main window
    while (fenetre.isOpen()){
        tailleFenetre[2] = sf::Mouse::getPosition(fenetre).x;
        tailleFenetre[3] = sf::Mouse::getPosition(fenetre).y;
       
            //update frame per seconds
        time = clock.getElapsedTime();
        int timer = static_cast<int>(time.asSeconds());

        string toText = ((to_string(static_cast<int>(loopCount/time.asSeconds()))) + " " + (to_string(timer)));
        text1.setString(toText);

            //Run Menu and get decision from it
        selection = buildMenu(tailleFenetre,ptrTexture, refFont,ptrImage);
        if(selection == 0){
            if(!texture.copyToImage().saveToFile("texture/background5.png")){
                cout << "cant save texture to file" << endl;
            }
            else{
                cout << "Texture saved to file" << endl;
                fenetre.close();
            }
        }
       
        // Process events
        sf::Event event;
        while (fenetre.pollEvent(event)){
        // Close window: exit
            if((event.type ==  sf::Event::Closed)){
                cout << "Saving texture" << endl;
                fenetre.close();
            }
        }

            //Drawing stuff
        cursorSprite.setPosition(sf::Mouse::getPosition(fenetre).x - cherryCursor.getSize().x/2, sf::Mouse::getPosition(fenetre).y+ cherryCursor.getSize().y/3);
        fenetre.clear(sf::Color::White);

        fenetre.draw(cherryTree);
        fenetre.draw(cursorSprite);
        fenetre.draw(text1);

        fenetre.display();

        if(loopCount == 1111 ){
            loopCount = 0;
            clock.restart();
            }
            loopCount++;
        }
    return EXIT_SUCCESS;
}

 

constante.h
#ifndef CONSTANTE_H_INCLUDED
#define CONSTANTE_H_INCLUDED

#include<string>

using namespace std;

const int MAIN_MENU_SIZE = 4;
const int MENU_LARGEUR = 140, MENU_HAUTEUR = 40;

              //Menu options
const string mainMenuOptions[MAIN_MENU_SIZE] = {"Quitter", "Options","Generateur","Dessin"};

#endif // CONSTANTE_H_INCLUDED

 

menu.h
#ifndef MENU_H_INCLUDED
#define MENU_H_INCLUDED
#include <array>
#include <SFML/Graphics.hpp>
using namespace std;

int buildMenu(int taillefenetre[],sf::Texture* ptrTexture,sf::Font &refFont, sf::Image &ptrImage);

#endif // MENU_H_INCLUDED
 


menu.cpp
#include <iostream>
#include "Menu.h"
#include "Constante.h"
#include <SFML/Graphics.hpp>
#include <array>


using namespace std;

int buildMenu(int taillefenetre[],sf::Texture* ptrTexture,sf::Font &refFont,sf::Image &ptrImage){
    int retour = -1;

   
    sf::Texture spriteTexture;
    if(!spriteTexture.loadFromImage(ptrImage)){
        cout << "cant load cherry sprite" << endl;
    }

   
    sf::Sprite cherryTree(spriteTexture);
    sf::RenderTexture texture1;
    sf::Text text;
    text.setFont(refFont);
    sf::FloatRect textRect;

    if(!texture1.create(taillefenetre[0],taillefenetre[1]))
    cout << "cant create texture1 for render texture" << endl;
    texture1.clear(sf::Color(248,228,228));

            //Displayin menu options
       for(int i = 0; i < MAIN_MENU_SIZE; i++){

            text.setString(mainMenuOptions[i]);
            text.setCharacterSize(15);
            text.setFillColor(sf::Color(221,133,175));
            textRect = text.getLocalBounds();
            text.setPosition(sf::Vector2f((taillefenetre[0]- MENU_LARGEUR/2-textRect.width/2) ,(taillefenetre[1]-(MENU_HAUTEUR * i)-(MENU_HAUTEUR+5))));

            textRect = text.getGlobalBounds();
           
                // Input for Highlighting and menu decision
            if(textRect.contains(taillefenetre[2],taillefenetre[3])){
                    text.setFillColor(sf::Color::Black);
                    text.setCharacterSize(17);
                    text.move(-6,-2.5);
                if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){        // Menu decision
                retour = i;
                }
            }

            texture1.draw(text);
        }
    texture1.draw(cherryTree);
    texture1.display();
    *ptrTexture = texture1.getTexture();
    return retour;
}
 

Thanks Again

Kammil

2
Graphics / 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 ?

Pages: [1]