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.


Messages - 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 / Re: Rectangle shape render from the bottom of my window
« 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();

3
Graphics / Re: Rectangle shape render from the bottom of my window
« 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++;
        }
    }

4
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]