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 - JLesage

Pages: [1]
1
General / C++/SFML: display several shapes simultaneously on screen
« on: February 22, 2024, 11:05:51 am »
Hello. I'm a beginner in c++ and I'm learning SFML. I have coded a small program to simultaneously display several shapes of different size, color and position on the screen, but I don't understand why there is just one shape that is displayed when the code is executed. Please help me.

#include <SFML/Graphics.hpp>

#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

#include <iostream>

using std::vector;

using std::cout;
using std::cin;
using std::endl;



int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");

    vector<sf::RectangleShape> vecRec(25);

    srand(time(NULL));

    int r, g, b = rand()%255+1;

    sf::Vector2f recSize(rand()%40+20, rand()%40+20);
    sf::Vector2f recPos(rand()%760+1, rand()%560+1);

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();


        sf::RectangleShape rec;
        rec.setFillColor(sf::Color(r,g,b));
        rec.setSize(recSize);
        rec.setPosition(recPos);
        vecRec.push_back(rec);

        /*for(auto& r: vecRec){
            window.draw(r);
        }*/


        for(int i=0; i<vecRec.size();i++){
            window.draw(vecRec[i]);
        }


        window.display();
    }

    return 0;
}
 

2
Graphics / SFML text display problem under Linux
« on: May 09, 2023, 01:07:26 pm »
Hello. I am learning C++ and SFML under Linux(Pop!_OS 22.04 LTS). I run some code ()to display some text on my window, I have no error when running, but the text does not display. Please help me.

Note: I did the library inclusions and the constants declaration in another file(.hpp).

#include "main.hpp"
 
#include <iostream>
 
sf::Font font;
 
int main(){
 
 
    void gestionEntrees(sf::Event, sf::RenderWindow &window);
 
    void chargerPolice();
 
   
    sf::Text txt;
    txt.setFont(font);
    txt.setString("Hello, World.");
    txt.setCharacterSize(26);
    txt.setFillColor(sf::Color::Blue);
    txt.setStyle(sf::Text::Bold | sf::Text::Underlined);
 
   
 
    sf::RenderWindow window(sf::VideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32), "Mon premier jeu", sf::Style::Titlebar | sf::Style::Resize | sf::Style::Close);
 
    window.setVerticalSyncEnabled(true);
 
    while(window.isOpen()){
 
        sf::Event event;
        while(window.pollEvent(event)){
            // fermeture de la fenĂȘtre lorsque l&#39;utilisateur le souhaite
            gestionEntrees(event, window);
             
        }
 
       
        window.clear(sf::Color::White);
 
         
       
        window.draw(txt);
 
         
        window.display();
 
    }
 
    return 0;
 
}
 
 
void gestionEntrees(sf::Event event, sf::RenderWindow &window){
    if (event.type == sf::Event::Closed)
        window.close();
}
 
//Load font
void chargerPolice(){
    if (!font.loadFromFile("/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc"))
        throw("Erreur de chargement de police");
}
 

3
Window / Title bar display and closing problem
« on: May 06, 2023, 02:05:05 pm »
I'm a beginner in C++ and SFML for game creation under linux(Pop!_OS 22.04 LTS). I tested the code on the official SFML website, everything seems to work but the title bar doesn't display; it's the same with other codes. I need your help, please. Here is an example of a code I tested:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
 
 
int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!", sf::Style::Titlebar);
    sf::Event ev;
 
    while(window.isOpen()){
       
        while(window.pollEvent(ev)){
 
            switch(ev.type){
                case sf::Event::Closed:
                    window.close();
                    break;
                case sf::Event::KeyPressed:
                    if(ev.key.code == sf::Keyboard::Escape){
                        window.close();
                        break;
                    }
                default:
                    break;
            }
             
        }
 
        //Render
        window.clear(sf::Color::Blue);
        window.display();
    }
 
    return 0;
}

Pages: [1]