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

Pages: [1] 2 3 ... 5
Graphics / Re: Drawing Visual Noise Quickly
« on: September 03, 2021, 08:12:19 pm »
You can fake it in some way, for example prerender noise into few textures and during game loop draw them randomly. Or use shaders.

You don't need render whole minimap each frame, especially terrain or another elements which never changes. Render it once to texture.

Ok, thanks.

After update to actual Radeon drivers (21.4.1) I have this message when running any SFML program with console.

It's also described by someone on Reddit https://www.reddit.com/r/sfml/comments/lowcus/error_message_on_window_creation/

General / Re: Entire game in single cpp file?
« on: April 14, 2021, 09:46:56 pm »
Cherno's C++


Graphics / Re: Tilemap cell ID
« on: April 13, 2021, 12:37:00 pm »
Good :) Map and terrain implementation in 2D is interesting topic but it's rather outside of SFML related problems to deal with all issues in code. In addition, specific advice can be misleading because no one knows the project.

It would be more interesting to address various techniques that can be used in general (SFML, graphical API). Like terrain texture blending, another options how deal with height, 2D sprites vs 3D terrain, layer rendering, overlapping (depth), use of shaders..

Graphics / Re: Tilemap cell ID
« on: April 10, 2021, 08:56:14 am »
It's calculation whether point (mouse) is inside polygon (4 verticles of map tile).

Checking all map tiles vs mouse cursor can be slow with such algorithm, you can get approximate region of nearest tiles first (like sf::Vector2i current_tile(sf::Mouse::getPosition(window).x/tile_size.x, sf::Mouse::getPosition(window).y/tile_size.y); for 2x2 tiles etc.) and check those only.

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <vector>

int PointInPolygon(const sf::Vector2f& p, const std::vector<sf::Vector2f>& v)
        int i, j, c = 0;
        for (i = 0, j = v.size() - 1; i < v.size(); j = i++)
                if (((v[i].y > p.y) != (v[j].y > p.y)) &&
                        (p.x < (v[j].x - v[i].x) * (p.y - v[i].y) / (v[j].y - v[i].y) + v[i].x))
                        c = !c;
        return c;

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

        std::vector<sf::Vector2f> v{ {25 , 50}, {100 , 50}, {100 , 150}, {45 , 160} };

        sf::ConvexShape polygon;
        polygon.setPoint(0, v[0]);
        polygon.setPoint(1, v[1]);
        polygon.setPoint(2, v[2]);
        polygon.setPoint(3, v[3]);

        while (window.isOpen())
                sf::Event event;
                while (window.pollEvent(event))
                        switch (event.type)
                        case sf::Event::Closed:

                        default: break;

                if (PointInPolygon(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)), v))

        return 0;

Graphics / Re: Tilemap cell ID
« on: April 09, 2021, 11:57:10 pm »
Use point in polygon calculation in all cases (you can find various implementations). Actually you must have all 4 coordinates already since you are able to draw them. Based on your previous code, you know x, y, tile_width and tile_height of each tile. Let's say tile in base is rectangle with size 64 x 64 pix. Plus you have y offset from m_heightmap.

In the for loop will be:
int current_tile = -1; // Not valid
int id = 0;

top_left.x = tile.x;
top_left.y = tile.y + m_heightmap[some_index];

top_right.x = tile.x + tile_width;
top_right.y = tile.y + m_heightmap[some_index];

bottom_right.x = tile.x + tile_width;
bottom_right.y = tile.y + tile_height + m_heightmap[some_index];

bottom_left.x = tile.x;
bottom_left.y = tile.y + tile_height + m_heightmap[some_index];

if (PointInPolygon(mouse, top_left, top_right, bottom_right, bottom_left)
    current_tile = id;

There are probably two concepts in the basics
1) Calculate tile coordinates in for loop
2) Pre-generate coordinates when creating a map and store it in tile classes or structs

Second approach is less messy (at least from my experience) and you can use data wherever it is needed.

There is no simple way how to get tile id in 3D or pseudo 3D maps, it leads into various problems. Thats why games have often flat maps or height is rather an illusion like in Transport Tycoon :) For example if height offset will be extend to other tile above, point in polygon can be valid in more than 1 case (>1 tile is selected). There is more ways, you can fill tiles with color and check ID based on that, use some raycast etc.

Graphics / Re: 2D or 3D?
« on: March 13, 2021, 07:08:37 pm »
I don't know what the rest of the code looks like, if there is any structure for each tile. Anyway while you are generating x and y positions during for loop, the heights for all tiles must be stored somewhere. For example in std::vector<int> m_heightmap[width * height - 1].

Then just add height to all y axis:
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y + m_heightmap[this_tile_index]);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y + m_heightmap[this_tile_index + 1]);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y + m_heightmap[this_tile_index + 1 + row_bellow]);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y + m_heightmap[index_of_tile_row_bellow]);

You must be carefull about index, it will be different in each line because quads have separated coordinates and you're counting with neighboring tiles which follow the changed height. It is probably not the best described, but the logic behind is straightforward.

Graphics / Re: 2D or 3D?
« on: March 13, 2021, 12:41:20 pm »
As you can see on picture it can be 2D grid [x;y] with offsets for vertices (height). Use sf::VertexArray, map texture and use black color for shading (texture will have a burnt contrast in the base).

For the visual can be problem to highlight the terrain line, then you probably need to use shaders somehow.

SFML projects / Re: [Release][GUI] ImGui-SFML
« on: February 26, 2021, 01:52:34 pm »
Thanks. And I thought the texture upside down was a feature :)

Graphics / Re: Why is this taking up so much memory
« on: November 15, 2020, 12:27:30 pm »
This code doesn't fill memory but burden CPU. If you want draw 1 000 000 pixels use vertex array and initialize them before clearing the screen.


Graphics / Re: Loading large textures
« on: October 21, 2020, 03:12:39 pm »
Are you preloading images in advance before they are rendered?

General / Re: Drag and Drop from a Vector of Shapes
« on: August 26, 2020, 01:56:06 pm »
You can make new variable selectedShape which will be index in shapeVec, -1 if nothing is selected, >0 if something is selected. Or it can be pointer. The index should be assigned in shape vs cursor collision loop, which you don't have.

Graphics / Re: Alpha blendMode
« on: August 24, 2020, 02:04:33 pm »
I'm not sure without code how is cameraPosition calculated but don't forget you must also recalculate the position of the shadows which are drawn into renderTexture. For example renderTexture have dimensions 800 x 600 and you move camera to position [250; 0] (top left corner), then simply if you draw shadow with position [950; 0] you'll never see it. It must be corrected.

Something like:
shadowSpritePosition -= cameraPosition;

Pages: [1] 2 3 ... 5