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

Recent Posts

Pages: [1] 2 3 ... 10
1
General / Re: Can you integrate allegro into an sfml window?
« Last post by Me-Myself-And-I on Today at 04:18:20 pm »
This might sound dumb but... how exactly do you do that? :P I don't think I've ever heard of excluding header files from compilation when the cpp file is using them. Neither have I found any documentation on how to do that.
2
The view controls how the scene is mapped onto the window. But when a window resizes, the view isn't modified. So you'll need to generate a new view. For example:
                                if (event.type == sf::Event::Resized)
                                {
                                        sf::Vector2u size = g_window.getSize();
                                        g_window.setView(sf::View(sf::Vector2f(size.x / 2.0f, size.y / 2.0f), sf::Vector2f(size.x, size.y)));
                                }
 
3
SFML development / Re: Font/Text Direction
« Last post by eXpl0it3r on Today at 09:52:07 am »
I believe SFML's font would need to adjust a lot more than just providing the horizontal advance when trying to support non-horizontal layouts.

Meaning, I'm not against changing this, just questioning if that's actually helpful.

Also I found this page and the graphics helpful to understand the terminology.



I wonder if that advance is ever used in normal LTR text too or just for column writing systems. FT docs don't say. sf::Text and sf::Font are also a bit iffy for RTL text (technically you can reverse your string before inputting it but that's a bit of a hack to make RTL be just LTR backwards), and has 0 shaping needed for cursive scripts (like HarfBuzz has).
Eventually, we'll be integrating HarfBuzz, but we'd first have to solve the dependency issue.
4
I've noticed a majorly strange issue in my project. Here is some very simplified code that still shows the issue:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
int main()
{
    //path = current directory
    std::string path = "";
    const float defaultx = 1024;
    bool resized = false;
    const float defaulty = 768;
    sf::RenderWindow window(sf::VideoMode(defaultx, defaulty), "SFML works!");
    sf::Sprite s;
    sf::Sprite l;
    sf::Texture t;
    if (!t.loadFromFile(path + "Textures/playbutton.jpg")) {
        cout << "something went wrong" << endl;
    } else {
        s.setTexture(t);
        s.setOrigin(s.getTextureRect().getSize().x/2, s.getTextureRect().getSize().y/2);
        s.setPosition(sf::Vector2f(window.getSize().x/2, window.getSize().y/2));
    }
    while (window.isOpen())
    {
        window.clear();
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::Resized) {
                resized = true;
                l.setTexture(t);
                l.setOrigin(l.getTextureRect().getSize().x/2, l.getTextureRect().getSize().y/2);
                l.setScale(sf::Vector2f(defaultx*0.5/window.getSize().x, defaulty*0.5/window.getSize().y));
                l.setPosition(sf::Vector2f(window.getSize().x/2, window.getSize().y/2));
                s.setScale(sf::Vector2f(defaultx/window.getSize().x, defaulty/window.getSize().y));
//                s.setPosition(sf::Vector2f(window.getSize().x/2, window.getSize().y/2));
            }
        }
        window.draw(s);
        if(resized) window.draw(l);
        window.display();
    }
    return 0;
}
A quick rundown of what the code is doing, to my understanding:
- application starts, displays sprite 's' as intended: centered in the screen perfectly.
- on window resize, the sprite 'l' is assigned the same texture as sprite 's', with half the scale.
- l's position *should* be set to the same as s's: centered on the screen perfectly.
What is actually happening:
- everything goes as planned until the application is resized. When resized, l's position is offset by some value (I'm not entirely sure what this value is, but it is directly correlated to the amount of resizing done to the window)
- if window has been downsized diagonally, l seems to be positioned too far to the left and too high. if window has been upsized diagonally, l seems to be positioned too far to the right and low. (pattern seems to follow as horizontal expansion/contraction -> l moves right/left respectively. vertical expansion/contraction -> l moves down/up respectively)

I've also included s being drawn to the screen after this occurs to show that s is unaffected by this resizing, which is strange. To be completely honest, I'm at a loss. I've been fiddling with this for a few hours now and I can't quite determine why this is happening. I tried looking on the forums to see if anyone else had this issue, but not much came up. I assume my code may be faulty somehow if nobody else is having this issue, or it could be my sfml version. I'm using SFML version 2.6.1, on MacOS, with a 2020 MacBook Air that has an M1 chip.

If it's an issue with my code, I am more than happy to take criticism. This has been bugging me for hours

5
General / error during initialisation of SFML
« Last post by Epichado04 on May 21, 2024, 11:21:56 am »
Hi everyone.
I have a problem with the use of SFML.
I have tried to use SFML for a work study. I have see many tutos and read the documentation, but everytime i tried to install it and start the easiest program to see the window open, it doesn't work.
I have this error which pop:

The procedure entry point _ZNSt15basic_streambuflcSt11char_traitslcEE7seekposESt4fposliESt13_los_Openmode is not found in the dynamic link library C:\Users\epichado\OneDrive\Documents\Epitech\Cours\ApprentissageC++\asteroide\sfml-system-2.dll.

I don't know how to repair this error.
If somebody can help me, it will be a pleasure!
(Excuse my english, i'm a french guy so i probably make some mistake in my post)

Thanks !
6
SFML development / Re: Font/Text Direction
« Last post by FRex on May 20, 2024, 05:00:37 am »
I wonder if that advance is ever used in normal LTR text too or just for column writing systems. FT docs don't say. sf::Text and sf::Font are also a bit iffy for RTL text (technically you can reverse your string before inputting it but that's a bit of a hack to make RTL be just LTR backwards), and has 0 shaping needed for cursive scripts (like HarfBuzz has).
7
General / I have a problem with background
« Last post by swthxbld3 on May 19, 2024, 03:09:15 pm »
I need to make the background rise with the platforms when jumping, but I do not know how to do this. You also need the background to be drawn infinitely many times. Can you help me please
main.cpp
(#include <SFML/Graphics.hpp>
#include "hero.h"
#include "game.h"
#include <iostream>


int main() {
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "WayofDarkness");



    game(window);

   

    return 0;
})
hero.h
(#ifndef HERO_H
#define HERO_H

#include <SFML/Graphics.hpp>

class Hero {
private:
    sf::Sprite sprite;
    sf::Texture texture;
    float x, y;

public:
    Hero(std::string spritePath, float posX, float posY) {
        sprite.setTextureRect(sf::IntRect(0, 2048, 1024 ,1024));
        sprite.scale(sf::Vector2f(0.15, 0.15));
        if (!texture.loadFromFile(spritePath)) {
        }

        sprite.setTexture(texture);
        x = posX;
        y = posY;
        sprite.setPosition(x, y);
    }

    void Idle(sf::RenderWindow& window, float, float&);

    void draw(sf::RenderWindow& window);

    void move(sf::RenderWindow& window, float&, float&, float, float&, bool&);








};

#endif HERO_H)
hero.cpp
(#include <iostream>
#include "hero.h"
#include "update.h"



void Hero::Idle(sf::RenderWindow& window, float time, float& CurrentFrame)
{

}

void Hero::draw(sf::RenderWindow& window)
{
        window.draw(sprite);
}




void Hero::move(sf::RenderWindow& window, float&  playerX, float& playerY, float time, float& CurrentFrame, bool& Idle)
{

       

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
                playerX -= 0.1 * time;
                CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
                if (CurrentFrame > 4) CurrentFrame -= 3; //если пришли к третьему кадру - откидываемся назад.
                if (CurrentFrame != 0) {
                        sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 3072, 1024, 1024));
                }
                Idle = true;
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
                playerX += 0.1 * time;
                CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
                if (CurrentFrame > 4) CurrentFrame -= 3; //если пришли к третьему кадру - откидываемся назад.
                sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 3072, -1024, 1024));
                Idle = false;
        }
        else {
                if (Idle == true) {
                        CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
                        if (CurrentFrame > 3) CurrentFrame -= 2; //если пришли к третьему кадру - откидываемся назад.
                        sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 2048, 1024, 1024));
                }
                if (Idle == false) {
                        CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
                        if (CurrentFrame > 3) CurrentFrame -= 2; //если пришли к третьему кадру - откидываемся назад.
                        sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 2048, -1024, 1024));
                }
        }


    sprite.setPosition(playerX, playerY);

}






)
game.h
(#pragma once

#include <SFML/Graphics.hpp>
#include "hero.h"
#include "scence.h"
#include <iostream>


float playerX = 250;
float playerY = 151;



void game(sf::RenderWindow& window) {

    window.setFramerateLimit(60);


    sf::Clock clock;

    while (window.isOpen()) {


        std::cout << time<< std::endl;
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }




        window.clear();
        scence(window, playerX, playerY);
        window.display();
    }
}
)
scene.h
(#pragma once

#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include <random>
#include <ctime>
#include <sstream>
#include "hero.h"



float CurrentFrame = 0;
bool Idle;
float backGroundY = 0;

void scence(sf::RenderWindow& window, float& playerX, float& playerY) {
        Hero hero("ALL/images/heroAnimation.png", 100, 100);



        sf::Texture backgroundTexture;
        sf::Texture platformTexture;
        backgroundTexture.loadFromFile("ALL/images//BG.jpg");
        platformTexture.loadFromFile("ALL/images/platform.png");
        sf::Sprite background(backgroundTexture);
        sf::Sprite platform(platformTexture);

        sf::RectangleShape gameoverBackground(sf::Vector2f(1920, 1080));
        gameoverBackground.setFillColor(sf::Color::White);
        sf::Clock clock;
        sf::Time t1 = sf::microseconds(10000);
        sf::Font font;
        font.loadFromFile("ALL/font/arial.ttf");
        sf::Text scoreText;
        scoreText.setFont(font);
        scoreText.setCharacterSize(50);
        scoreText.setFillColor(sf::Color::Red);
        sf::Text gameoverText;
        gameoverText.setFont(font);
        gameoverText.setString("Game Over!");
        gameoverText.setCharacterSize(80);
        gameoverText.setFillColor(sf::Color::Red);

        sf::SoundBuffer buffer;
        buffer.loadFromFile("ALL/sound/jump.wav");
        sf::Sound sound;
        sound.setBuffer(buffer);

        sf::Vector2u platformPosition[20];
        std::uniform_int_distribution<unsigned> x(0, 1920);       //-platformTexture.getSize().x
        std::uniform_int_distribution<unsigned> y(100, 1080);
        std::default_random_engine e(time(0));
        for (size_t i = 0; i < 20; ++i)
        {
                platformPosition[i].x = x(e);
                platformPosition[i].y = y(e);
        }

        float dy = 0;
        int height = 150;
        int score = 0;

        //const int PLAYER_LEFT_BOUNDING_BOX = 20;
        //const int PLAYER_RIGHT_BOUNDING_BOX = 60;
        //const int PLAYER_BOTTOM_BOUNDING_BOX = 70;

        const int PLAYER_LEFT_BOUNDING_BOX = 20;
        const int PLAYER_RIGHT_BOUNDING_BOX = 80;
        const int PLAYER_BOTTOM_BOUNDING_BOX = 100;


        while (window.isOpen())
        {
                float time = clock.restart().asMicroseconds();
                time = time / 800;
                clock.restart();
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        playerX -= 10;
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        playerX += 10;


                if (playerX > 1800)
                        playerX = 1800; //0
                if (playerX < -40)
                        playerX = -40; // 1920

                if (playerY == height && dy < (-1.62))
                {
                        score += 1;
                        scoreText.setString("Score: " + std::to_string(score));
                }

                // player&#39;s jump mechanism
                dy += 0.2;
                playerY += dy;
                backGroundY += dy;
               

                if (playerY < height)
                        for (size_t i = 0; i < 20; ++i)
                        {

                                playerY = height;
                                platformPosition[i].y -= dy;  // vertical translation
                                if (platformPosition[i].y > 1080) // set new platform on the top
                                {
                                        platformPosition[i].y = 0;
                                        platformPosition[i].x = x(e);
                                }

                        }
                // detect jump on the platform
                for (size_t i = 0; i < 20; ++i)
                {
                        if ((playerX + PLAYER_RIGHT_BOUNDING_BOX > platformPosition[i].x) && (playerX + PLAYER_LEFT_BOUNDING_BOX < platformPosition[i].x + platformTexture.getSize().x)        // player&#39;s horizontal range can touch the platform
                                && (playerY + PLAYER_BOTTOM_BOUNDING_BOX > platformPosition[i].y) && (playerY + PLAYER_BOTTOM_BOUNDING_BOX < platformPosition[i].y + platformTexture.getSize().y)  // player&#39;s vertical   range can touch the platform
                                && (dy > 0)) // player is falling
                        {
                                sound.play();
                                dy = -20;
                        }

                }
                hero.move(window, playerX, playerY, time, CurrentFrame, Idle);
                hero.Idle(window, time, CurrentFrame);

                window.draw(background);
                background.setPosition(0, backGroundY);
                hero.draw(window);

                // set and draw platforms
                for (size_t i = 0; i < 20; ++i)
                {
                        platform.setPosition(platformPosition[i].x, platformPosition[i].y);
                        window.draw(platform);
                }

                // game over
                if (playerY > 1080)
                {
                        gameoverText.setPosition(30, 200);
                        scoreText.setPosition(150, 400);
                        goto gameover;
                }
                window.draw(scoreText);
                window.display();
        }

        // Game Over
gameover:
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                window.draw(gameoverBackground);
                window.draw(gameoverText);
                window.draw(scoreText);
                window.display();
        }
       

})
8
Graphics / Re: How to correctly draw a triangle fan?
« Last post by Nortski on May 19, 2024, 12:42:34 pm »
If the triangle solution works, you must check your formation of the triangle strip to see if it's correct, including infinitely thin or 'backwards' triangles (breaking the triangle strip shape).
It might be worth making the calculations using doubles - for more accuracy - and then only converting to floats when creating the graphics (triangle strip).

Thank you for taking the time to help me with this.

I actually fixed the problem. As it turned out it was an off by one problem with my loop. A triangle fan needs to be 2 elements larger than the number of vertex points in my vector, 1 for the central position and 1 to close the gap between the last vertex and the first vertex.

// Make polygon 2 larger than vertex container
sf::VertexArray visibilityPolygon(sf::TriangleFan, vectorAngleContainer.size() + 2);
visibilityPolygon[0] = ray[0].position;
visibilityPolygon[0].color = sf::Color::Red;

for (int i = 1; i <= vectorAngleContainer.size(); i++)
{
    visibilityPolygon[i] = sf::Vertex(sf::Vector2f(vectorAngleContainer[i - 1].position.x, vectorAngleContainer[i - 1].position.y), sf::Color::Red);
}

// Set the last point == to the first vertex [1] (not [0] as that is the central point)
visibilityPolygon[vectorAngleContainer.size() + 1] = visibilityPolygon[1].position;
visibilityPolygon[vectorAngleContainer.size() + 1].color = sf::Color::Red;
9
General / Re: Can you integrate allegro into an sfml window?
« Last post by fallahn on May 19, 2024, 11:57:24 am »
When you add the files to your project, make sure you're only compiling VideoTexture.cpp (not the header files)
10
Graphics / Re: How to correctly draw a triangle fan?
« Last post by Nortski on May 19, 2024, 11:38:23 am »
Ok, I will try your suggestions.
It's weird though as it only happens on the right side of that shape and nowhere else.
Pages: [1] 2 3 ... 10
anything