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

Pages: [1]
1
General / Failed build thor 2.0 with SFML 2.6.1
« on: December 03, 2023, 02:37:41 pm »
Hi, I can't compile the library thor 2.0 with SFML 2.6.1
WinLibs MSVCRT 13.1.0 (64-bit)
Build log:
Quote
C:/Users/*****/Desktop/cpp/ide/mingw64/include/c++/13.1.0/bits/uniform_int_dist.h:284:71: error: call to non-'constexpr' function 'static sf::Uint32 thor::{anonymous}::Engine::min()'
  284 |         constexpr __uctype __urngmin = _UniformRandomBitGenerator::min();
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
C:\Users\*****\Desktop\cpp\lib\thor-v2.0-sdk\src\Random.cpp:79:43: note: 'static sf::Uint32 thor::{anonymous}::Engine::min()' declared here
   79 |                         static sf::Uint32 min()
      |                                           ^~~
C:/Users/*****/Desktop/cpp/ide/mingw64/include/c++/13.1.0/bits/uniform_int_dist.h:285:71: error: call to non-'constexpr' function 'static sf::Uint32 thor::{anonymous}::Engine::max()'
  285 |         constexpr __uctype __urngmax = _UniformRandomBitGenerator::max();
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
C:\Users\*****\Desktop\cpp\lib\thor-v2.0-sdk\src\Random.cpp:85:43: note: 'static sf::Uint32 thor::{anonymous}::Engine::max()' declared here
   85 |                         static sf::Uint32 max()
      |                                           ^~~
C:/Users/*****/Desktop/cpp/ide/mingw64/include/c++/13.1.0/bits/uniform_int_dist.h:286:34: error: non-constant condition for static assertion
  286 |         static_assert( __urngmin < __urngmax,
      |                        ~~~~~~~~~~^~~~~~~~~~~
mingw32-make.exe[2]: *** [src\CMakeFiles\thor.dir\build.make:361: src/CMakeFiles/thor.dir/Random.cpp.obj] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:97: src/CMakeFiles/thor.dir/all] Error 2
mingw32-make.exe: *** [C:/Users/*****/Desktop/cpp/lib/thor-v2.0-sdk/build/Makefile:135: all] Error 2
mingw32-make.exe[2]: Leaving directory 'C:/Users/*****/Desktop/cpp/lib/thor-v2.0-sdk/build'
mingw32-make.exe[1]: Leaving directory 'C:/Users/*****/Desktop/cpp/lib/thor-v2.0-sdk/build'
Process terminated with status 2 (0 minute(s), 14 second(s))
9 error(s), 6 warning(s) (0 minute(s), 14 second(s))
 

Build Messages:
Quote
||=== Build: all in thor (compiler: GNU GCC Compiler) ===|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h||In instantiation of 'std::uniform_int_distribution<_IntType>::result_type std::uniform_int_distribution<_IntType>::operator()(_UniformRandomBitGenerator&, const param_type&) [with _UniformRandomBitGenerator = thor::{anonymous}::Engine; _IntType = int; result_type = int]':|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h|193|required from 'std::uniform_int_distribution<_IntType>::result_type std::uniform_int_distribution<_IntType>::operator()(_UniformRandomBitGenerator&) [with _UniformRandomBitGenerator = thor::{anonymous}::Engine; _IntType = int; result_type = int]'|
C:\Users\*****\Desktop\cpp\lib\thor-v2.0-sdk\src\Random.cpp|114|required from here|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h|284|error: call to non-'constexpr' function 'static sf::Uint32 thor::{anonymous}::Engine::min()'|
C:\Users\*****\Desktop\cpp\lib\thor-v2.0-sdk\src\Random.cpp|79|note: 'static sf::Uint32 thor::{anonymous}::Engine::min()' declared here|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h|285|error: call to non-'constexpr' function 'static sf::Uint32 thor::{anonymous}::Engine::max()'|
C:\Users\*****\Desktop\cpp\lib\thor-v2.0-sdk\src\Random.cpp|85|note: 'static sf::Uint32 thor::{anonymous}::Engine::max()' declared here|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h|286|error: non-constant condition for static assertion|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h||In instantiation of 'std::uniform_int_distribution<_IntType>::result_type std::uniform_int_distribution<_IntType>::operator()(_UniformRandomBitGenerator&, const param_type&) [with _UniformRandomBitGenerator = thor::{anonymous}::Engine; _IntType = unsigned int; result_type = unsigned int]':|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h|193|required from 'std::uniform_int_distribution<_IntType>::result_type std::uniform_int_distribution<_IntType>::operator()(_UniformRandomBitGenerator&) [with _UniformRandomBitGenerator = thor::{anonymous}::Engine; _IntType = unsigned int; result_type = unsigned int]'|
C:\Users\*****\Desktop\cpp\lib\thor-v2.0-sdk\src\Random.cpp|121|required from here|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h|284|error: call to non-'constexpr' function 'static sf::Uint32 thor::{anonymous}::Engine::min()'|
C:\Users\*****\Desktop\cpp\lib\thor-v2.0-sdk\src\Random.cpp|79|note: 'static sf::Uint32 thor::{anonymous}::Engine::min()' declared here|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h|285|error: call to non-'constexpr' function 'static sf::Uint32 thor::{anonymous}::Engine::max()'|
C:\Users\*****\Desktop\cpp\lib\thor-v2.0-sdk\src\Random.cpp|85|note: 'static sf::Uint32 thor::{anonymous}::Engine::max()' declared here|
C:\Users\*****\Desktop\cpp\ide\mingw64\include\c++\13.1.0\bits\uniform_int_dist.h|286|error: non-constant condition for static assertion|
 [src\CMakeFiles\thor.dir\build.make|361|src/CMakeFiles/thor.dir/Random.cpp.obj] Error 1|
 [CMakeFiles\Makefile2|97|src/CMakeFiles/thor.dir/all] Error 2|
 [C:\Users\*****\Desktop\cpp\lib\thor-v2.0-sdk\build\Makefile|135|all] Error 2|
||=== Build failed: 9 error(s), 6 warning(s) (0 minute(s), 14 second(s)) ===|

2
General / tiled isometric map loader
« on: November 18, 2023, 06:38:58 pm »
Hello, please tell me the library for loading maps in isometric view

3
Graphics / Update code to sfml 2.5.1
« on: March 28, 2021, 07:53:54 pm »
I found an old version of the code for the old version of sfml.
code for displaying the map in isometric view. How to adjust it to the new version of sfml, or are there other options for drawing isometric?

https://www.gamedev.net/blog/33/entry-2250273-isometric-map-in-sfml/

#ifndef ISOMETRIC_H
#define ISOMETRIC_H
#include <SFML/Graphics.hpp>
#include <list>
#include <vector>
#include "imagemanager.h"

/************************************************************
IsometricMap

Implement a basic isometric map using SFML sprites.
************************************************************/


// Class to wrap up a sprite to include a world position
class IsometricMapSprite : public sf::Sprite
{
    public:
    IsometricMapSprite();
    IsometricMapSprite(const sf::Image &image, const sf::Vector2f &position=sf::Vector2f(0,0),
        const sf::Vector2f &scale=sf::Vector2f(1,1), float rotation=0.f, const sf::Color &color=sf::Color(255,255,255,255));
    ~IsometricMapSprite();

    void SetWorldPosition(float x, float y);
    void SetWorldPosition(const sf::Vector2f &position);
    sf::Vector2f GetWorldPosition() const;

    void SetWorldZ(float z);
    float GetWorldZ(){return m_z;}

    void setUseLighting(bool use){m_uselighting=use;}
    bool getUseLighting(){return m_uselighting;}

    protected:
    sf::Vector2f m_worldpos;
    float m_z;
    bool m_uselighting;
};

// Map node
class IsometricMapNode
{
    public:
    IsometricMapNode();
    ~IsometricMapNode();

    void drawFloors(sf::RenderWindow *win, sf::Color &color);
    void drawObjects(sf::RenderWindow *win, sf::Color &color);
    void drawWalls(sf::RenderWindow *win, sf::Color &color);
    void drawRoofs(sf::RenderWindow *win, sf::Color &color);

    void addFloor(IsometricMapSprite *s);
    void addWall(IsometricMapSprite *s);
    void addRoof(IsometricMapSprite *s);

    void insertSprite(IsometricMapSprite *s);
    void removeSprite(IsometricMapSprite *s);
    void clear();


    protected:

    std::list<IsometricMapSprite *> m_floors, m_walls, m_roofs, m_objects;
};

struct DynamicLight
{
    DynamicLight() : m_position(0,0), m_color(0,0,0), m_radius(0){}
    ~DynamicLight(){}

    void setColor(float r, float g, float b){m_color=sf::Vector3f(r,g,b);}
    void setRadius(float rad){m_radius=rad;}
    void setPosition(float x, float y){m_position=sf::Vector2f(x,y);}

    bool operator==(const DynamicLight &rhs) const
    {
        if(this == &rhs) return true;
        return false;
    }

    sf::Vector2f m_position;
    sf::Vector3f m_color;
    float m_radius;
};


class LightMap
{
    public:
    LightMap(int w, int h, int nodesize);
    ~LightMap();

    void resize(int w, int h);
    void clearStatic();
    void clearDynamic();
    void updateRegion(int sx, int sy, int num_nodes, int num_rows);
    DynamicLight *requestLight();
    void releaseLight(DynamicLight *light);
    sf::Color getLightValue(int x, int y);
    void setAmbient(float r, float g, float b);

    void addStaticLightCell(int x, int y, float r, float g, float b);
    void addStaticLightRadius(float x, float y, float r, float g, float b, float radius);

    void debugPrintLights();

    protected:
    std::vector<sf::Vector3f> m_static;
    std::vector<sf::Vector3f> m_final;
    std::list<DynamicLight> m_lights;
    sf::Vector3f m_ambient;
    int m_width, m_height, m_nodesize;
};


class IsometricMap
{
    public:
    IsometricMap(int w, int h, int nodesize);
    ~IsometricMap();

    void resize(int w, int h);
    void clear();

    void insertSprite(IsometricMapSprite *s);
    void removeSprite(IsometricMapSprite *s);
    void draw(sf::RenderWindow *win);

    sf::Vector2f projectMouseCoords(int mx, int my, sf::RenderWindow *win);

    void addLeftWall(int x, int y, std::string &name); // Set a left wall piece
    void addRightWall(int x, int y, std::string &name);
    void addFloor(int x, int y, std::string &name);
    void addRoof(int x, int y, std::string &name, float height);

    void setCenter(float x, float y) {m_centerx=x; m_centery=y;}

    LightMap *getLightMap(){return &m_lightmap;}


    protected:
    std::vector<IsometricMapNode> m_nodes;
    int m_width, m_height;
    int m_nodesize;
    float m_centerx, m_centery;
    std::list<IsometricMapSprite> m_mappieces;
    ImageManager m_mapimages;
    LightMap m_lightmap;

};


#endif
 

#include "2disometric.h"
#include <algorithm>
#include <cmath>
#include <iostream>

// Function to convert a world position to a screen (view) position
sf::Vector2f WorldToScreen(sf::Vector2f v)
{
    return sf::Vector2f(2.0f*v.x - 2.0f*v.y, v.x + v.y);
}

// Function to convert a screen (view) position to a world position
sf::Vector2f ScreenToWorld(sf::Vector2f v)
{
    return sf::Vector2f((v.x+2.0f*v.y)/4.0f, (2.0f*v.y-v.x)/4.0f);
}


/********************************************
IsometricMapSprite
*********************************************/

IsometricMapSprite::IsometricMapSprite() : sf::Sprite(), m_worldpos(0,0){m_uselighting=true;}
IsometricMapSprite::IsometricMapSprite(const sf::Image &image, const sf::Vector2f &position,
    const sf::Vector2f &scale, float rotation, const sf::Color &color) :
    sf::Sprite(image,position,scale,rotation,color)
{
    m_worldpos=position;
    m_z=0;
    sf::Vector2f screenpos=WorldToScreen(m_worldpos);
    screenpos.y -= m_z;
    SetPosition(screenpos);
    m_uselighting=true;
}

IsometricMapSprite::~IsometricMapSprite(){}
void IsometricMapSprite::SetWorldPosition(float x, float y)
{
    m_worldpos=sf::Vector2f(x,y);
    sf::Vector2f screenpos=WorldToScreen(m_worldpos);
    screenpos.y -= m_z;
    SetPosition(screenpos);
}

void IsometricMapSprite::SetWorldPosition(const sf::Vector2f &position)
{
    m_worldpos=position;
    sf::Vector2f screenpos=WorldToScreen(m_worldpos);
    screenpos.y -= m_z;
    SetPosition(screenpos);
}

void IsometricMapSprite::SetWorldZ(float z)
{
    m_z=z;
    sf::Vector2f screenpos=WorldToScreen(m_worldpos);
    screenpos.y -= m_z;
    SetPosition(screenpos);
}

sf::Vector2f IsometricMapSprite::GetWorldPosition() const
{
    return m_worldpos;
}


// Function to find the right place in a list to insert a sprite
// Lists are sorted ascending, with the sort value equal to the sprite's center X and Y summed together.
std::list<IsometricMapSprite *>::iterator findSpotInSpriteList(IsometricMapSprite *s, std::list<IsometricMapSprite *> &spritelist)
{
    std::list<IsometricMapSprite *>::iterator iter=spritelist.begin();
    sf::Vector2f worldpos=s->GetWorldPosition();
    float z=s->GetWorldZ();
    float sortval=worldpos.x + worldpos.y + z;

    while(iter != spritelist.end())
    {
        sf::Vector2f npos=(*iter)->GetWorldPosition();
        float nz=(*iter)->GetWorldZ();
        if(npos.x+npos.y+nz > sortval) return iter;
        ++iter;
    }
    return iter;
}

/*************************************************
IsometricMapNode
**************************************************/


IsometricMapNode::IsometricMapNode(){}
IsometricMapNode::~IsometricMapNode(){}

void IsometricMapNode::addFloor(IsometricMapSprite *s)
{
    m_floors.push_back(s);
}

void IsometricMapNode::addWall(IsometricMapSprite *s)
{
    m_walls.push_back(s);
}

void IsometricMapNode::addRoof(IsometricMapSprite *s)
{
    m_roofs.push_back(s);
}

void IsometricMapNode::drawFloors(sf::RenderWindow *win, sf::Color &color)
{
    for(std::list<IsometricMapSprite *>::iterator iter=m_floors.begin(); iter!=m_floors.end(); ++iter)
    {
        if((*iter)->getUseLighting()) (*iter)->SetColor(color);
        win->Draw(*(static_cast<sf::Sprite *>(*iter)));
    }
}
void IsometricMapNode::drawObjects(sf::RenderWindow *win, sf::Color &color)
{
    for (std::list<IsometricMapSprite *>::iterator iter=m_objects.begin(); iter!=m_objects.end(); ++iter)
    {
        if((*iter)->getUseLighting()) (*iter)->SetColor(color);
        sf::Sprite *s = static_cast<sf::Sprite *>(*iter);
        win->Draw(*s);
    }
}

void IsometricMapNode::drawWalls(sf::RenderWindow *win, sf::Color &color)
{
   for(std::list<IsometricMapSprite *>::iterator iter=m_walls.begin(); iter!=m_walls.end(); ++iter)
    {
        if((*iter)->getUseLighting()) (*iter)->SetColor(color);
        win->Draw(*(static_cast<sf::Sprite *>(*iter)));
    }
}

void IsometricMapNode::drawRoofs(sf::RenderWindow *win, sf::Color &color)
{
    for(std::list<IsometricMapSprite *>::iterator iter=m_roofs.begin(); iter!=m_roofs.end(); ++iter)
    {
        if((*iter)->getUseLighting()) (*iter)->SetColor(color);
        win->Draw(*(static_cast<sf::Sprite *>(*iter)));
    }
}

void IsometricMapNode::insertSprite(IsometricMapSprite *s)
{
    sf::Vector2f worldpos=s->GetWorldPosition();
    std::list<IsometricMapSprite *>::iterator iter=findSpotInSpriteList(s, m_objects);
    m_objects.insert(iter, s);
    m_objects.unique();
}

void IsometricMapNode::removeSprite(IsometricMapSprite *s)
{
    m_objects.remove(s);
}

void IsometricMapNode::clear()
{
    m_floors.clear();
    m_walls.clear();
    m_roofs.clear();
    m_objects.clear();
}





/**********************************************************************
IsometricMap
***********************************************************************/


IsometricMap::IsometricMap(int w, int h, int nodesize) : m_width(w), m_height(w), m_nodesize(nodesize), m_centerx(0), m_centery(0), m_lightmap(w,h,nodesize)
{
    m_nodes.resize(w*h);
}

IsometricMap::~IsometricMap()
{
}

void IsometricMap::resize(int w, int h)
{
    clear();
    m_nodes.resize(w*h);
}

void IsometricMap::clear()
{
    for(int c=0; c<m_width*m_height; ++c) m_nodes[c].clear();
    m_mappieces.clear();
    m_mapimages.clear();
}

void IsometricMap::insertSprite(IsometricMapSprite *s)
{
    // Figure out which cell it lies within
    sf::Vector2f worldpos=s->GetWorldPosition();
    int cx=(int)(worldpos.x) / m_nodesize;
    int cy=(int)(worldpos.y) / m_nodesize;
    if(cx<0) cx=0; if(cx>=m_width) cx=m_width-1; if(cy<0) cy=0; if(cy>=m_height) cy=m_height-1;

    m_nodes[cy*m_width+cx].insertSprite(s);
}

void IsometricMap::removeSprite(IsometricMapSprite *s)
{
    // Figure out which cell it lies within
    sf::Vector2f worldpos=s->GetWorldPosition();
    int cx=(int)(worldpos.x) / m_nodesize;
    int cy=(int)(worldpos.y) / m_nodesize;
    if(cx<0) cx=0; if(cx>=m_width) cx=m_width-1; if(cy<0) cy=0; if(cy>=m_height) cy=m_height-1;
    m_nodes[cy*m_width+cx].removeSprite(s);
}

sf::Vector2f IsometricMap::projectMouseCoords(int mx, int my, sf::RenderWindow *win)
{
    // Calculate view
    sf::View view=win->GetView();
    // Calculate coords of mouse relative to view
    sf::Vector2f center=WorldToScreen(sf::Vector2f(m_centerx, m_centery));
    sf::Vector2f viewsize=view.GetSize();
    sf::Vector2f mousecoords=ScreenToWorld(sf::Vector2f((center.x-viewsize.x/2.0f)+(float)mx, (center.y-viewsize.y/2.0f)+(float)my));
    return mousecoords;
}

void IsometricMap::draw(sf::RenderWindow *win)
{
    // Do a brain dead whole-map scan for now
    // Set view
    sf::View view=win->GetView();
    // Reverse project center
    sf::Vector2f center=WorldToScreen(sf::Vector2f(m_centerx,m_centery));
    view.SetCenter(center);
    win->SetView(view);

    // Reverse-project top-left corner
    sf::Vector2f viewsize=view.GetSize();
    sf::Vector2f topleft=ScreenToWorld(sf::Vector2f(center.x-viewsize.x/2.0f, center.y-viewsize.y/2.0f));
    int sx=(int)(topleft.x/(float)m_nodesize);
    int sy=(int)(topleft.y/(float)m_nodesize);

    // Move start location up and left one node. (subtract 1 from sx
    sx-=2;

    // Calculate how many nodes across to draw
    // A node's total width on-screen is calculated as 4*nodesize
    int num_nodes_across=(int)viewsize.x / (m_nodesize*4) + 4;

    // Calculate how many rows to draw
    // A node's total height on screen is calculated as 2*nodesize. Also, need to fudge it
    // a little bit by adding a value that approximates the maximum cell height to the size of the
    // viewport.
    int num_rows=(((int)viewsize.y+512) / (m_nodesize*2))*2;

    // Update lighting
    m_lightmap.updateRegion(sx,sy,num_nodes_across,num_rows);

    // Drawing proceeds as thus:
    // We begin at some starting node and proceed across the row. At each step, we increment x and decrement y
    // to move to the next node.
    // When a row is done, we move to the next row. This is done by:
    // If the current row is "even", then we move to the next row by incrementing x. If the current row is odd, we
    // move to the next row by incrementing y instead.
    // On even rows, we draw num_nodes+1 nodes, else we draw num_nodes nodes.
    int rowincx=1, rowincy=0;
    int drawnodes=num_nodes_across+1;

    int nodex=sx, nodey=sy;
    for(int row=0; row<num_rows; ++row)
    {
        if (row & 1)
        {
            // Odd row
            rowincx=0;
            rowincy=1;
            drawnodes=num_nodes_across;
        }
        else
        {
            rowincx=1;
            rowincy=0;
            drawnodes=num_nodes_across+1;
        }


        for(int node=0; node<drawnodes; ++node)
        {
            // Calculate cell coords
            int cellx=nodex+node;
            int celly=nodey-node;
            if(cellx>=0 && cellx<m_width && celly>=0 && celly<m_height)
            {
                sf::Color color=m_lightmap.getLightValue(cellx,celly);
                m_nodes[celly*m_width+cellx].drawFloors(win,color);
            }
        }

        nodex=nodex+rowincx;
        nodey=nodey+rowincy;
    }
// Draw objects
    nodex=sx, nodey=sy;
    for(int row=0; row<num_rows; ++row)
    {
        if (row & 1)
        {
            // Odd row
            rowincx=0;
            rowincy=1;
            drawnodes=num_nodes_across;
        }
        else
        {
            rowincx=1;
            rowincy=0;
            drawnodes=num_nodes_across+1;
        }


        for(int node=0; node<drawnodes; ++node)
        {
            // Calculate cell coords
            int cellx=nodex+node;
            int celly=nodey-node;
            if(cellx>=0 && cellx<m_width && celly>=0 && celly<m_height)
            {
                sf::Color color=m_lightmap.getLightValue(cellx,celly);
                m_nodes[celly*m_width+cellx].drawObjects(win,color);
                m_nodes[celly*m_width+cellx].drawRoofs(win,color);
                m_nodes[celly*m_width+cellx].drawWalls(win,color);
            }
        }

        nodex=nodex+rowincx;
        nodey=nodey+rowincy;
    }
}

void IsometricMap::addLeftWall(int x, int y, std::string &name)
{
    m_mappieces.push_front(IsometricMapSprite());
    IsometricMapSprite *s = &(m_mappieces.front());
    sf::Image *i=m_mapimages.GetImage(name);
    s->SetImage(*i);

    // Set the origin of the sprite
    // For left wall it is (w,h)
    unsigned int imgwidth=i->GetWidth();
    unsigned int imgheight=i->GetHeight();
    s->SetOrigin((float)imgwidth, (float)imgheight);

    // Set position of the sprite
    // For left wall, set to (originx+nodesize, originy+nodesize)
    float ox=(float)m_nodesize*(float)x;
    float oy=(float)m_nodesize*(float)y;
    s->SetWorldPosition(sf::Vector2f(ox+(float)m_nodesize-1.0f,oy+(float)m_nodesize-1.0f));

    // Add to cell
    m_nodes[y*m_width+x].addWall(s);
}

void IsometricMap::addRightWall(int x, int y, std::string &name)
{
    m_mappieces.push_front(IsometricMapSprite());
    IsometricMapSprite *s = &(m_mappieces.front());
    sf::Image *i=m_mapimages.GetImage(name);
    s->SetImage(*i);

    // Set the origin of the sprite
    // For right wall it is (0,h)
    unsigned int imgheight=i->GetHeight();
    s->SetOrigin((float)0, (float)imgheight);

    // Set position of the sprite
    // For right wall, set to (originx+nodesize, originy+nodesize)
    float ox=(float)m_nodesize*(float)x;
    float oy=(float)m_nodesize*(float)y;
    s->SetWorldPosition(sf::Vector2f(ox+(float)m_nodesize-1.0f,oy+(float)m_nodesize-1.0f));

    // Add to cell
    m_nodes[y*m_width+x].addWall(s);
}

void IsometricMap::addFloor(int x, int y, std::string &name)
{
    m_mappieces.push_front(IsometricMapSprite());
    IsometricMapSprite *s = &(m_mappieces.front());
    sf::Image *i=m_mapimages.GetImage(name);
    s->SetImage(*i);

    // Set the origin of the sprite
    // For floor it is (w/2,0)
    unsigned int imgwidth=i->GetWidth();
    s->SetOrigin((float)imgwidth/2.0f, 0.0f);

    // Set position of the sprite
    // For floor, set to (originx, originy)
    float ox=(float)m_nodesize*(float)x;
    float oy=(float)m_nodesize*(float)y;
    s->SetWorldPosition(sf::Vector2f(ox,oy));

    // Add to cell
    m_nodes[y*m_width+x].addFloor(s);
}

void IsometricMap::addRoof(int x, int y, std::string &name, float height)
{
    m_mappieces.push_front(IsometricMapSprite());
    IsometricMapSprite *s = &(m_mappieces.front());
    sf::Image *i=m_mapimages.GetImage(name);
    s->SetImage(*i);

    // Set the origin of the sprite
    // For roof it is (w/2,0)
    unsigned int imgwidth=i->GetWidth();
    s->SetOrigin((float)imgwidth/2.0f, 0.0f);

    // Set position of the sprite
    // For roof, set to (originx, originy)
    float ox=(float)m_nodesize*(float)x;
    float oy=(float)m_nodesize*(float)y;
    s->SetWorldPosition(sf::Vector2f(ox,oy));
    s->SetWorldZ(height);

    // Add to cell
    m_nodes[y*m_width+x].addRoof(s);
}



// LightMap
LightMap::LightMap(int w, int h, int nodesize) : m_ambient(1,1,1), m_width(w), m_height(h), m_nodesize(nodesize)
{
    m_static.resize(w*h);
    m_final.resize(w*h);
    clearStatic();
}

LightMap::~LightMap()
{
}

void LightMap::resize(int w, int h)
{
    m_width=w;
    m_height=h;
    m_static.resize(w*h);
    m_final.resize(w*h);
    clearStatic();
    clearDynamic();
}

void LightMap::clearStatic()
{
    for(int c=0; c<m_width*m_height; ++c)
    {
        m_static[c]=sf::Vector3f(0,0,0);
        m_final[c]=sf::Vector3f(0,0,0);
    }
}

void LightMap::clearDynamic()
{
    m_lights.clear();
}

void LightMap::debugPrintLights()
{
    for(std::list<DynamicLight>::iterator iter=m_lights.begin(); iter!=m_lights.end(); ++iter)
    {
        DynamicLight &l=(*iter);
        std::cout << "Light: (" << l.m_position.x<<","<<l.m_position.y<<") Color: (" <<
            l.m_color.x<<","<<l.m_color.y<<","<<l.m_color.z<<") Radius: " << l.m_radius << std::endl;
    }
}

void LightMap::updateRegion(int sx, int sy, int num_nodes, int num_rows)
{
    int rowincx=1, rowincy=0;
    int drawnodes=num_nodes+1;

    int nodex=sx, nodey=sy;
    for(int row=0; row<num_rows; ++row)
    {
        if (row & 1)
        {
            // Odd row
            rowincx=0;
            rowincy=1;
            drawnodes=num_nodes;
        }
        else
        {
            rowincx=1;
            rowincy=0;
            drawnodes=num_nodes+1;
        }


        for(int node=0; node<drawnodes; ++node)
        {
            // Calculate cell coords
            int cellx=nodex+node;
            int celly=nodey-node;
            if(cellx>=0 && cellx<m_width && celly>=0 && celly<m_height)
            {
               // m_final[celly*m_width+cellx]=sf::Vector3f(0,0,0);
                // Calculate coord of center of point
                float cx=(float)cellx * (float)m_nodesize + (float)m_nodesize/2.0f;
                float cy=(float)celly * (float)m_nodesize + (float)m_nodesize/2.0f;

                // Set cell to ambient + static
                sf::Vector3f staticlight=m_static[celly*m_width+cellx];
                sf::Vector3f light(0,0,0);

                // Iterate dynamic lights
                for(std::list<DynamicLight>::iterator iter=m_lights.begin(); iter!=m_lights.end(); ++iter)
                {
                    float dx=cx-(*iter).m_position.x, dy=cy-(*iter).m_position.y;
                    if (fabs(dx) <= (*iter).m_radius && fabs(dy) <= (*iter).m_radius)
                    {
                        // Light is in range of point
                        float dist=sqrt(dx*dx+dy*dy);
                        float intensity=((*iter).m_radius - dist) / (*iter).m_radius;
                        intensity = std::max(0.f, std::min(1.0f,intensity));  // Clamp it
                        light.x+=(*iter).m_color.x*intensity;
                        light.y+=(*iter).m_color.y*intensity;
                        light.z+=(*iter).m_color.z*intensity;
                    }
                }
                m_final[celly*m_width+cellx]=sf::Vector3f(m_ambient.x+staticlight.x+light.x, m_ambient.y+staticlight.y+light.y, m_ambient.z+staticlight.z+light.z);


            }
        }

        nodex=nodex+rowincx;
        nodey=nodey+rowincy;
    }
}

DynamicLight *LightMap::requestLight()
{
    m_lights.push_front(DynamicLight());
    return &m_lights.front();
}

void LightMap::releaseLight(DynamicLight *light)
{
    m_lights.remove(*light);
}

sf::Color LightMap::getLightValue(int x, int y)
{
    if(x<0 || x>=m_width || y<0 || y>=m_height) return sf::Color(0,0,0,255);
    sf::Vector3f color=m_final[y*m_width+x];
    color.x=std::max(0.f,std::min(1.f,color.x));
    color.y=std::max(0.f,std::min(1.f,color.y));
    color.z=std::max(0.f,std::min(1.f,color.z));
    return sf::Color((unsigned char)(color.x*255.0f), (unsigned char)(color.y*255.0f), (unsigned char)(color.z*255.0f));
}

void LightMap::setAmbient(float r, float g, float b)
{
    m_ambient=sf::Vector3f(r,g,b);
}

void LightMap::addStaticLightCell(int x, int y, float r, float g, float b)
{
    if(x<0 || x>=m_width || y<0 || y>=m_height) return;
    sf::Vector3f &s=m_static[y*m_width+x];
    s.x+=r;
    s.y+=g;
    s.z+=b;
}

void LightMap::addStaticLightRadius(float x, float y, float r, float g, float b, float radius)
{
    // Get region of cells
    int sx=(int)((x-radius)/(float)m_nodesize)-1;
    int sy=(int)((y-radius)/(float)m_nodesize)-1;
    int dx=(int)((x+radius)/(float)m_nodesize)+1;
    int dy=(int)((y+radius)/(float)m_nodesize)+1;

    for(int cx=sx; cx<=dx; ++cx)
    {
        for(int cy=sy; cy<=dy; ++cy)
        {
            if(cx>=0 && cx<m_width && cy>=0 && cy<m_height)
            {
                sf::Vector3f st=m_static[cy*m_width+cx];
                float cellx=(float)cx * (float)m_nodesize + (float)m_nodesize/2.0f;
                float celly=(float)cy * (float)m_nodesize + (float)m_nodesize/2.0f;
                float dx=cellx-x, dy=celly-y;
                float dist=sqrt(dx*dx+dy*dy);
                float intensity=(radius-dist)/radius;
                intensity=std::max(0.f,std::min(1.0f,intensity));
                st.x+=r*intensity;
                st.y+=g*intensity;
                st.z+=b*intensity;
                m_static[cy*m_width+cx]=st;
            }
        }
    }
}
 

4
General / NPC area view
« on: February 21, 2021, 11:20:06 am »
Hello. tell me how to make vision for an NPC with obstacles.
Example in the picture.
Now the left NPC sees the right one, but the right one is behind the wall, that is, the left one should not see the right one.
For vision, I do this: there is a viewArea sprite (in the image it is red), I check the viewArea collision with the NPC sprite, if true, then the NPC sees another NPC, but how to beat if there is a wall between them?

5
Graphics / Alpha blendMode
« on: July 14, 2020, 04:06:54 pm »
Hi, please tell me how to make the alpha channel merge and not duplicate
https://imgur.com/a/EyvuxJH

sf::RenderStates states;

states.blendMode = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcAlpha);
states.shader = &myShader;

Shader:
uniform sampler2D texture;

void main()
{
    float alpha = texture2D(texture, gl_TexCoord[0].xy).a;
    if(alpha < 0.1)
                {
                        discard;
                }
    vec4 color = vec4(0.098, 0.098, 0.098, 0.5);
    gl_FragColor = color;      
}

 

6
General / Best solution to draw map
« on: January 27, 2019, 09:49:20 pm »
Hi, please tell me what is the best way to render large game map
to get such a result? Attach 1


i have 48 blob sprites (8-bit masking)
like this: Attach 2



My problem is: Attach 3
i render my map in VertexArray, but VertexArray can not mix two sprites together, like this:


How can I draw a game map so that the sprite overlay on top of another sprite?

7
General / procedural map filling
« on: October 21, 2018, 06:57:09 pm »
Hi, I am generating a map using perlin noise.
But I do not know how to fill the generated map with trees, grass, bushes, etc.
Is it possible to fill the game map using perlin noise?

8
General / thor and SFML 2.5.0
« on: October 14, 2018, 01:54:04 am »
Hi, I want to connect thor to sfml
but when compiling produces an error:
Quote
ld.exe||cannot find -lthor|
Why ?

9
Graphics / Sprite Transformation
« on: October 11, 2018, 05:48:15 pm »
Hi, please tell me how to do this on spml without shaders

10
Graphics / sf::Drawable Draw only visible area
« on: July 22, 2018, 04:37:32 pm »
Hi, i have sf::Drawable class:
class DrawTileMap : public sf::Drawable
{
    public:
        void            load                (const Texture tileset, int ( &TileMap )[500][500]);
        void            update              (int ( &TileMap )[500][500]);
    private:
        virtual void    draw                (sf::RenderTarget& target, sf::RenderStates states) const;
        VertexArray     m_vertices;
        Texture         m_tileset;
        Vector2u        tileSize            = {64, 64};
};
//==================================================================================================
void DrawTileMap::load(const Texture tileset, int ( &TileMap )[500][500])
{
    m_tileset = tileset;

    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(500 * 500 * 4);
}
//==================================================================================================
void DrawTileMap::update(int ( &TileMap )[500][500])
{
    int tu = 0;
    int tv = 0;

    for(unsigned int x = 0; x < 500; ++x)
        for(unsigned int y = 0; y < 500; ++y)
            {
                switch(TileMap[ x ] [ y ] )
                    {
                        case 1:     tu = 0;tv = 2;break;
                        case 2:     tu = 1;tv = 2;break;
                        case 3:     tu = 2;tv = 2;break;
                        case 4:     tu = 3;tv = 2;break;
                        case 5:     tu = 4;tv = 2;break;
                        case 6:     tu = 0;tv = 3;break;
                        case 7:     tu = 1;tv = 3;break;
                        case 8:     tu = 2;tv = 3;break;
                        case 9:     tu = 3;tv = 3;break;
                        case 10:    tu = 4;tv = 3;break;

                        default:tu = 0;tv = 0;break;
                    }

                sf::Vertex* tile64 = &m_vertices[(x + y * 500) * 4];

                tile64[0].position = sf::Vector2f(x * tileSize.x, y * tileSize.y);
                tile64[1].position = sf::Vector2f((x + 1) * tileSize.x, y * tileSize.y);
                tile64[2].position = sf::Vector2f((x + 1) * tileSize.x, (y + 1) * tileSize.y);
                tile64[3].position = sf::Vector2f(x * tileSize.x, (y + 1) * tileSize.y);

                tile64[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                tile64[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                tile64[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                tile64[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }
}
//==================================================================================================
void DrawTileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.texture = &m_tileset;
    target.draw(m_vertices, states);
}
//==================================================================================================
 
my game map size is 500x500
this class draws a game map in full size (500x500)
how to draw only a visible area of the map, for example 100x100

11
Graphics / please tell me how to do it correctly (VertexArray)
« on: July 11, 2018, 06:20:33 pm »
hi, please tell me how to do it correctly:
I have a class of a game map. Map size 100x100
inside the class of the game map there is a class DrawTileMap
I draw map like this:
Quote
class DrawTileMap : public sf::Drawable, public sf::Transformable
{
    public:
        void            load                (const Texture tileset, int ( &TileMap )[100][100]);
        void            update              (int ( &TileMap )[100][100]);
    private:
        virtual void    draw                (sf::RenderTarget& target, sf::RenderStates states) const;
        VertexArray     m_vertices;
        Texture         m_tileset;
        Vector2u        tileSize            = {64, 64};
};

Quote
#include "DrawTileMap.h"

void DrawTileMap::load(const Texture tileset, int ( &TileMap )[100][100])
{
    m_tileset = tileset;

    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(100 * 100 * 4);
}

void DrawTileMap::update(int ( &TileMap )[100][100])
{
    int tu = 0;
    int tv = 0;

    for(unsigned int x = 0; x < 100; ++x)
        for(unsigned int y = 0; y < 100; ++y)
            {
                switch(TileMap[ x ] [ y ])
                    {
                        case 1: tu = 0;tv = 0;break; // Water
                        case 2: tu = 1;tv = 0;break; // Ground
                        case 3: tu = 2;tv = 0;break; // Grass

                        default:tu = 0;tv = 0;break;
                    }

                sf::Vertex* tile64 = &m_vertices[(x + y * 100) * 4];

                tile64[0].position = sf::Vector2f(x * tileSize.x, y * tileSize.y);
                tile64[1].position = sf::Vector2f((x + 1) * tileSize.x, y * tileSize.y);
                tile64[2].position = sf::Vector2f((x + 1) * tileSize.x, (y + 1) * tileSize.y);
                tile64[3].position = sf::Vector2f(x * tileSize.x, (y + 1) * tileSize.y);

                tile64[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                tile64[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                tile64[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                tile64[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }
}

void DrawTileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.transform *= getTransform();
    states.texture = &m_tileset;
    target.draw(m_vertices, states);
}

With this approach, I get pictures as pictured on "picture1.png"
but in my tile set there is a smooth transition from grass to earth, 3 sprite
How can I do this to get what is depicted in the "picture2.png" ?

12
General / Shader in sf::Drawable
« on: June 07, 2018, 07:48:31 pm »
Hello, please tell me it's possible to do this:
I have an image size of 128x64, that is 2 sprite.
I draw them like this:
(click to show/hide)
Tell me, please, is it possible to use the shader only on the second sprite inside the DrawTileMap class?

13
General / sf::Drawable and two sprite on one position
« on: June 07, 2018, 05:44:35 pm »
Hello, please tell me what I'm doing wrong
I have an image size of 128x64, that is 2 sprite.
I draw them like this:
(click to show/hide)
on 1 image my tileSet, the second result of the brown I'm trying to reach, and 3 that turns out
Tell me please how can I put sprite on the sprite?

14
General / How to mix two sprites?
« on: May 13, 2018, 06:36:10 pm »
Good afternoon, can someone tell me how to mix both sprites like we can see in this screenshot?

15
General / Mouse Select
« on: September 22, 2017, 03:44:42 pm »
Hello,
please tell me how can implement the selection with the mouse on the map,
in the photo selected area of 18x8 meters ?
I have tried:
Quote
                            sf::RectangleShape line[1] =
                                {
                                    sf::RectangleShape(sf::Vector2f(64, 64))
                                };
                            line[0].setFillColor(sf::Color(100, 250, 50, 128));

                            EndMouseX = mouseX - StartMouseX + 1;
                            EndMouseY = mouseY - StartMouseY + 1;
                            line[0].setPosition(StartMouseX * 64, StartMouseY * 64);
                            line[0].setSize(sf::Vector2f(EndMouseX * 64, EndMouseY * 64));

sorry for bad english

Pages: [1]
anything