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

Recent Posts

Pages: [1] 2 3 ... 10
1
Feature requests / Re: export vertexes from the Shape class
« Last post by FRex on Today at 01:40:07 am »
You can use a sf::Transform (or better - sf::Transformable).

I have my own helper class (called maker in the code) that lets me format Text with bold, italics, color and outline changing per character, and in the end I use an sf::Transform like so:

            sf::RenderStates states;
            states.texture = &font->getTexture(charsize);
            sf::Transformable trans;
            const auto bounds = maker.getBounds();
            trans.setPosition(kTileSize * sf::Vector2f(pos));
            const float scale = std::min(kTileSize / right(bounds), kTileSize / bottom(bounds));
            trans.setScale(scale, scale);
            states.transform = trans.getTransform();
            RENDERER_HISTORY(m_target->draw(maker.getTextVeritces(), states));
 

You should look into how ::draw() is implemented in SFML drawables, it's almost always just doing
states.transform *= getTransform();
since they derive from transformable (that's where setPosition, setRotation, etc. all come from). Some also add texture (sf::Sprite, sf::Text, even sf::Shape actually).
2
Graphics / Re: Tile-based graphic using Sprites.. right?
« Last post by LakySimi1 on May 08, 2024, 09:20:47 pm »
I think yes but i didn't use "isometric" since is perspective-related and not necessarily tile-related.

Thank you! Searching online i've found this https://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php , i think is exctly what i was looking for probably, rigt?
3
Graphics / Re: Tile-based graphic using Sprites.. right?
« Last post by eXpl0it3r on May 08, 2024, 08:18:31 am »
The term you're looking for is "isometric".

You can use sprites, but you may run into performance issues down the line, so you may consider starting off with vertex arrays.

The difficult part is correctly calculating which tile needs to be rendered on top of what other tile, depending if something is below or above or infront.
4
General / Re: How to make my game board occupy as much window space as possible
« Last post by gera on May 07, 2024, 08:14:31 pm »
Thanks, with this function everything works!

// -*- compile-command: "g++ board.cpp -o board -lsfml-graphics -lsfml-window -lsfml-system && ./board"; -*-

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Window.hpp>
#include <iostream>

sf::View getLetterboxView(sf::View view, int windowWidth, int windowHeight) {
    // Compares the aspect ratio of the window to the aspect ratio of the view,
    // and sets the view&#39;s viewport accordingly in order to achieve a letterbox effect.
    // A new view (with a new viewport set) is returned.

    float windowRatio = (float) windowWidth / (float) windowHeight;
    float viewRatio = view.getSize().x / (float) view.getSize().y;
    float sizeX = 1;
    float sizeY = 1;
    float posX = 0;
    float posY = 0;

    bool horizontalSpacing = true;
    if (windowRatio < viewRatio)
        horizontalSpacing = false;

    // If horizontalSpacing is true, the black bars will appear on the left and right side.
    // Otherwise, the black bars will appear on the top and bottom.

    if (horizontalSpacing) {
        sizeX = viewRatio / windowRatio;
        posX = (1 - sizeX) / 2.f;
    }

    else {
        sizeY = windowRatio / viewRatio;
        posY = (1 - sizeY) / 2.f;
    }

    view.setViewport( sf::FloatRect(posX, posY, sizeX, sizeY) );

    return view;
}

int main()
{
        const int BW = 70;
        const int BH = 40;

        sf::RenderWindow window(sf::VideoMode(960, 540), "Board drawing test");
        window.setFramerateLimit(10);

        sf::View view({0.0f, 0.0f, BW, BH});

        while (window.isOpen()) {
                sf::Event event;
                while (true) {
                        bool moreEvents = window.pollEvent(event);
                        switch (event.type) {
                                case sf::Event::Closed:
                                        window.close();
                                        break;
                                case sf::Event::Resized:
                                        view = getLetterboxView(view, event.size.width, event.size.height);
                                        break;
                        }
                        if (!moreEvents)
                                break;
                }

                window.setView(view);
                window.clear();

                // blue rectangle, filling the entire board
                sf::RectangleShape shape(sf::Vector2f(BW, BH));
                shape.setFillColor(sf::Color(0, 0, 255));
                window.draw(shape);
                // red square at the corner of the board
                shape.setFillColor(sf::Color(255, 0, 0, 255));
                shape.setPosition(BW-1, BH-1);
                shape.setSize(sf::Vector2(1.0f, 1.0f));
                window.draw(shape);

                window.display();
        }
       
        return 0;
}
 
5
General / Re: How to make my game board occupy as much window space as possible
« Last post by kimci86 on May 07, 2024, 07:33:54 pm »
You are trying to achieve a letterbox effect.
There is an example implementation on the wiki: https://github.com/SFML/SFML/wiki/Source:-Letterbox-effect-using-a-view
6
I want to draw my rectangular game board in a resizable window. When the window's aspect ratio does not match there should be black margins at either top&bottom or left&right.

Right now I have margins on both sides (see screenshot). This is because I first set view on a square with a side equal to the board's larger dimension and then I set its viewport to account for window's aspect ratio. I think to achieve what I want the viewport should take board's dimensions into account as well, but I don't see how. Please help.

// -*- compile-command: "g++ board.cpp -o board -lsfml-graphics -lsfml-window -lsfml-system && ./board"; -*-

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Window.hpp>

int main()
{
        const int BW = 40;
        const int BH = 30;

        sf::RenderWindow window(sf::VideoMode(960, 540), "Board drawing test");
        window.setFramerateLimit(10);

        const int MAX_DIMENSION = std::max(BW,BH);
        sf::View view({-(MAX_DIMENSION-BW)/2, -(MAX_DIMENSION-BH)/2, MAX_DIMENSION, MAX_DIMENSION});
        window.setView(view);

        while (window.isOpen()) {
                sf::Event event;
                while (true) {
                        bool moreEvents = window.pollEvent(event);
                        switch (event.type) {
                                case sf::Event::Closed:
                                        window.close();
                                        break;
                                case sf::Event::Resized:
                                        int ww = event.size.width;
                                        int wh = event.size.height;

                                        if (ww > wh) {
                                                float aspect = (float)wh/ww;
                                                float empty = 1.0f - aspect;
                                                view.setViewport(sf::FloatRect(empty/2, 0.0f, aspect, 1.0));
                                        } else {
                                                float aspect = (float)ww/wh;
                                                float empty = 1.0f - aspect;
                                                view.setViewport(sf::FloatRect(0.0f, empty/2, 1.0, aspect));
                                        }
                                        window.setView(view);
                                        break;
                        }
                        if (!moreEvents)
                                break;
                }

                window.clear();

                // blue rectangle, filling the entire board
                sf::RectangleShape shape(sf::Vector2f(BW, BH));
                shape.setFillColor(sf::Color(0, 0, 255));
                window.draw(shape);
                // red square at the corner of the board
                shape.setFillColor(sf::Color(255, 0, 0, 255));
                shape.setPosition(BW-1, BH-1);
                shape.setSize(sf::Vector2(1.0f, 1.0f));
                window.draw(shape);

                window.display();
        }
       
        return 0;
}
 
7
SFML projects / Re: [Multiplatform] GravytX The Gravytoid
« Last post by IsDaouda on May 07, 2024, 01:10:23 pm »
Hi all,
I hope that you all are ok!

GravytX The Gravytoid is already one year old! I hope you had fun during the intergalactic travels

Don’t hesitate to share your experiences with us!

Happy exploring and have a nice day!

Game link:
Web Version (Android, iOS, Xbox, PlayStation, PC, ...)
8
SFML projects / Re: VALA-vapi binding sfml
« Last post by eXpl0it3r on May 07, 2024, 07:55:05 am »
I've never used Vala, but looks quite similar to the C languages.

If you want, you could open a PR to add it to the binding page: https://github.com/SFML/SFML-Website
9
SFML projects / VALA-vapi binding sfml
« Last post by natou2000 on May 07, 2024, 04:47:23 am »
my project is... to make a SFML binding in Vala. (2.6.1)  ;D



I've finished it 100% today, I'm doing some testing and I've even started writing the API documentation online.  8)
I've tried to keep the API as close as possible to that of C++ and C  :D


project:   https://gitlab.com/nda-cunh/sfml-vala-binding
Documentation:  https://nda-cunh.gitlab.io/sfml-vala-binding/sfml/sf.html
10
Graphics / Tile-based graphic using Sprites.. right?
« Last post by LakySimi1 on May 06, 2024, 07:13:07 pm »
Hi everyone,
assuming that i want to make a 2D tile-based game, such as Avadon or Fallout 1/2, in which roofs can disappear to let look inside buildings (like Fallout 1/2), or in which different tiles get darkened depending if they are visible or not to player (like Avadon) (for example), is it right to use Sprites for substantially everyting?

for(each tile to show)
{
    Sprite.setTextureRect(sf::IntRect(x,y,z,k));    //Get tile
    Sprite.setPosition(sf::Vector2f(i.f,j.f));      //Draw coordinates
    Sprite.setColor(sf::Color(255, 255, 255));      //Light
    window.draw(Sprite);                            //Render
}



Avadon
(click to show/hide)
Fallout
(click to show/hide)
Pages: [1] 2 3 ... 10