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

Author Topic: My menu  (Read 7263 times)

0 Members and 1 Guest are viewing this topic.

Kammil

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
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
« Last Edit: November 06, 2018, 04:49:52 pm by Kammil »

foo

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: My menu
« Reply #1 on: November 06, 2018, 05:14:36 pm »
It looks good! There's not much else you can do besides making a reusable activity for all the different menu subscreens you'll have. You don't want to have your main loop _filled_ with your entire app's logic. That gets messy and hard to maintain.

Consider using Swoosh for this case.

Swoosh has an Activity class that you can fill your menu's setup, update, draw, and enter/exit functions to make very lively applications. It also has segue support so you can transition from one menu item to the next with Sliding, Fading, and you can create your own.

It's super easy to use and you won't need to link any libraries or DLLs.