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

Pages: [1] 2
1
Graphics / shape texture and fill Colour
« on: August 04, 2016, 12:26:20 am »
Hi Folks,

So I setup a shape and put in a fill color. I then set a texture to the shape which has a transparent background. What I was expecting was that the fill color would remain as the background color of that shape and the texture would sit on top of the back ground color. However that doesn't seem to be correct?

However instead, the background is black as I am using the default window.clear().

Basically how can I set the background colour of a shape so it remains whilst having a texture (with a transparent background) applied. Also when I remove the setTexture statement, the fillColour is visible.

Here is a fully working example so should be possible to copy/paste.

class header file
#include <math.h>

using namespace std;


class hexgrid : public sf::Drawable
{
        public:
                hexgrid(sf::RenderWindow& window);
                ~hexgrid();

                void draw(sf::RenderTarget& target, sf::RenderStates states) const;

        private:
                std::vector<sf::CircleShape> hexaGrid;
                sf::Texture hexTexture;
};
 

class cpp file
#include "hexgrid.h"

hexgrid::hexgrid(sf::RenderWindow& window)
{
        sf::CircleShape hexagon;
        hexagon.setPointCount(6);
        hexagon.setRadius(40);
        hexagon.setFillColor(sf::Color(225, 247, 164));
        hexagon.setOutlineThickness(-1.5);
        hexagon.setOutlineColor(sf::Color::Black);
       
        if (!hexTexture.loadFromFile("E:\\downloads\\grassland.png"))
        {
                std::cout << "Failed to load hex texture image!" << std::endl;
        }

        hexagon.setTexture(&hexTexture);

        float xpos = 0, ypos = 0;
        hexagon.setPosition(xpos, ypos);
        hexaGrid.push_back(hexagon);
}

hexgrid::~hexgrid()
{

}

void hexgrid::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        std::vector<sf::CircleShape>::const_iterator hexit;

        for (hexit = hexaGrid.begin(); hexit != hexaGrid.end(); ++hexit)
        {
                target.draw(*hexit, states);
        }
}
 

and my main

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

int main()
{
        sf::ContextSettings settings;
        settings.antialiasingLevel = 8;

        sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Hexgrid Example", sf::Style::Default, settings);

        hexgrid grid(window);
               
        sf::Event e;
        bool running = true;
        while (running)
        {
                while (window.pollEvent(e))
                {
                        if (e.type == sf::Event::Closed)
                        {
                                window.close();
                                return 0;
                        }
                }
                window.clear();
                window.draw(grid);
                window.display();
               
        }
        return 0;
}
 

2
Graphics / [SOLVED] overlapping circle shapes
« on: July 14, 2016, 05:15:46 am »
Hi Folks,

I am trying to setup a hexGrid type game and I am currently playing with different ways to do it.
Right now I got a simple class which has a sf::CircleShape that has 6 points to make it a Hexagon. I also have a vector of the sf::CircleShape for the grid.

Now I got the grid drawing fine and I am now trying to detect the hex that the mouse is in. I've written a simple method that is called each time the mouse is moved. It basically checks the globalBounds of each sf::Circleshape to see if the mouse position is contained within that circleShape. If yes then it changes the fillColour of the sf::Circleshape.

Now this is working ok, except when the mouse is hovering near the edge of some grids, then both get highlighted white, i.e. the mouse is within both circleshapes.

Is this because there is a bounding box around the sf::CircleShape which is larger that the displayed shape? Is there a way to resolve this?

I have put down a full working example that should be able to be compile and run on it's own.

hexGrid.h
#ifndef HEXGRID_HPP
#define HEXGRID_HPP

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

using namespace std;

class hexgrid : public sf::Drawable
{
        public:
                hexgrid(sf::RenderWindow& window);
                ~hexgrid();

                void draw(sf::RenderTarget& target, sf::RenderStates states) const;

                bool chkHexGridWithMousePos(sf::Vector2f passed_mousePos);
                bool isMouseContainsHexagaon(sf::CircleShape *hexPtr, sf::Vector2f passed_mousePos);

                enum hexStyle
                {
                        translucent,
                        colorful,
                        green,
                        cyan
                };

        private:
                sf::CircleShape hexagon;
                std::vector<sf::CircleShape> hexaGrid;
};


#endif // HEXGRID_HPP
 

hexGrid.cpp
#include "hexgrid.h"

hexgrid::hexgrid(sf::RenderWindow& window)
{
        //set up background elements
        hexagon.setRadius(window.getSize().x / 16.f);
        hexagon.setPointCount(6);
        hexagon.setFillColor(sf::Color(150, 50, 250));
        hexagon.setOutlineThickness(2);
        hexagon.setOutlineColor(sf::Color(250, 150, 100));
        hexagon.setOrigin(hexagon.getGlobalBounds().width / 2.f, hexagon.getGlobalBounds().height / 2.f);

        std::vector<sf::ConvexShape>::iterator hexit;
        float xpos = 0, ypos = 0;

        for (int y = 0; y < 12; y++)
        {
                if (y == 0)
                {
                        ypos = y * hexagon.getGlobalBounds().height;
                }
                else
                {
                        ypos = ypos + (hexagon.getGlobalBounds().height - (hexagon.getGlobalBounds().height * 0.25f));
                }

                for (int x = 0; x < 12; x++)
                {
                        if (y % 2 == 0)
                        {
                                xpos = x * hexagon.getGlobalBounds().width;
                        }
                        else
                        {
                                xpos = (x * hexagon.getGlobalBounds().width) + (hexagon.getGlobalBounds().width * 0.5f);
                        }
                        hexagon.setPosition(xpos, ypos);
                        hexaGrid.push_back(hexagon);
                }
        }
}

hexgrid::~hexgrid()
{

}

void hexgrid::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        std::vector<sf::CircleShape>::const_iterator hexit;
        for (hexit = hexaGrid.begin(); hexit != hexaGrid.end(); ++hexit)
        {
                target.draw(*hexit, states);
        }
}

bool hexgrid::chkHexGridWithMousePos(sf::Vector2f passed_mousePos)
{
        bool returnVal = false;
       
        for (int i = 0; i < hexaGrid.size(); i++)
        {
                if (isMouseContainsHexagaon(&hexaGrid[i], passed_mousePos))
                {
                        returnVal = true;
                }
        }

        return returnVal;
}

bool hexgrid::isMouseContainsHexagaon(sf::CircleShape *hexPtr, sf::Vector2f passed_mousePos)
{
        if (hexPtr->getGlobalBounds().contains(passed_mousePos))
        {
                hexPtr->setFillColor(sf::Color(255, 255, 255));
                return true;
        }
        else
        {
                hexPtr->setFillColor(sf::Color(150, 50, 250));
                return false;
        }
}
 

main
#include <iostream>
#include <SFML/Graphics.hpp>
#include "hexgrid.h"

int main()
{
        sf::ContextSettings settings;
        settings.antialiasingLevel = 8;

        sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Hexgrid Example", sf::Style::Default, settings);

        hexgrid grid(window);
               
        sf::Event e;
        bool running = true;
        while (running)
        {
                while (window.pollEvent(e))
                {
                        if (e.type == sf::Event::Closed)
                        {
                                window.close();
                                return 0;
                        }

                        if (e.type == sf::Event::MouseMoved)
                        {
                               
                                grid.chkHexGridWithMousePos(sf::Vector2f(e.mouseMove.x, e.mouseMove.y));
                        }
                }
                window.clear();
                window.draw(grid);
                window.display();
               
        }
        return 0;
}
 

3
Graphics / Doing a drag and drop method
« on: June 14, 2016, 11:28:02 pm »
Hi Folks,

I am trying to implement method that can detect whether a user is holding down the left mouse button, thus indicating that the user is dragging a sprite.

In my event loop, I have this - note some is pseudo code. Basically I am sleeping the program for 100 milliseconds after the a sf::Mouse::Left event is detected. If the mouse is still pressed then I say the user is attempt to drag an icon.

I cannot assume that just having a sf::Mouse::Left indicates that the user is dragging an icon as he could just be left clicking the sprite to select it. If I did then it starts dragging/dropping icons when the user is just trying to select them.

It just seems a bit of an inefficient way to detect whether a user is dragging by adding a sleep. Is there a more efficient method to implement drag/drop, whilst at the same time support left clicking


                        if (event.type == sf::Event::MouseButtonPressed)
                        {
                                //if left button pressed
                                if (event.mouseButton.button == sf::Mouse::Left)
                                {
                                        cout << "left pressed \n";
                                        sf::sleep(sf::milliseconds(100));
                                        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                                        {
                                                isDragging = true;
                                                cout << "left pressed and isDragging \n";
                                               
                                                //Here I check if mouse was within the bounds of the sprite and do what I need to do
                                        }
                                }
                        }

 

4
Audio / Sounds stop playing too early when using std::vector
« on: June 13, 2016, 02:35:53 am »
Hi Folks,

So I am using sf::Sound for the first time and I am having a bit of trouble where sounds seem to stop playing before the entire wav file has finished playing.

What I have is:

- A std::vector of sf::Sound in my private section of my class.

vector<sf::Sound> soundVtr;
 

- When I play a sound I simply push_back a new sf::Sound to this std::vector

soundVtr.push_back(sf::Sound(*passed_SoundBuffer));
 

- During each game tick/loop, I call the below removeStoppedSounds() which goes through the std::vector of the sf::Sounds to check the status. If the status is sf::SoundSource::Stopped

However as stated, when I call the removeStoppedSounds() during the gameloop, I find that sounds don't finish, i.e. a 2 second wav file gets stopped withing a few milliseconds.

If I comment out the call to removeStoppedSounds() then it plays until the end. So I am assuming this method is something removing the sf::Sound before it can be played until the end of the wav file.

So is this something to do with using std::vector? If yes, then how I can store sf::sounds so that they can be played until the end of the wav file?

Or something to do with sf::Sound::getStatus()?

bool soundsList::isSoundPlaying(sf::Sound *passed_Sound)
{
        if (passed_Sound->getStatus() == sf::SoundSource::Stopped)
        {
                //cout << "stopped playing\n";
                return false;
        }
        else
        {
                cout << "still playing\n";
                return true;
        }
}


void soundsList::removeStoppedSounds()
{
        for (int i = 0; i < soundVtr.size(); i++)
        {
                if (!isSoundPlaying(&soundVtr[i]))
                {
                        soundVtr.erase(soundVtr.begin() + i);
                        cout << "erased sound at index " << i << "\n";
                        //i--;
                }
        }
}
 

5
Graphics / update texture from a sf::image
« on: April 21, 2016, 03:37:41 am »
Hi Folks,

So I have a sf::Image that I update. The image is quite large 5680 X 4178, so when I update the sf::Image, I ensure to pass in the sf::IntRect of the area that I am working on - see below. This works fine for my needs and it is pretty fast to update the IntRect area. (0.047 secs).

//set Pixel colour for all pixels in regionIntRect that match colourToCheck
void drawEngine::setImagePixelColour(sf::Image *passed_Image, sf::Color colourToCheck, sf::Color colourToSet, sf::IntRect regionIntRect)
{
        for (int y = regionIntRect.top; y < (regionIntRect.height + regionIntRect.top); y++)
        {
                for (int x = regionIntRect.left; x < (regionIntRect.width + regionIntRect.left); x++)
                {
                        if (passed_Image->getPixel(x, y) == colourToCheck)
                        {
                                passed_Image->setPixel(x, y, colourToSet);
                        }
                }
        }
}
 

However I am finding that update texture command is a bit slower. (0.27 secs) to run the below. Now I know the sf::Texture update method is having to load into the graphics memory so it will always be slower.

(regionLandHighlightTexture is the sf::Texture and landHighlightImageToDisplay is the sf::Image that I modified via the above method.)

regionLandHighlightTexture.update(landHighlightImageToDisplay);
 

Now I know the sf::Texture update method is having to load into the graphics memory so it will always be slower. I was wondering if there was a way to make to the update faster.

Basically I know the area of the texture that needs updating from the IntRect that I modified. However when I check the update methods, I don't see one where I can pass in an IntRect, i.e. only update this portion. I do see this method which is very similiar.

(What does "x   X offset in the texture where to copy the source image" mean in regards to this method?)

void    update (const Uint8 *pixels, unsigned int width, unsigned int height, unsigned int x, unsigned int y)

However I don't understand how I can get an array of pixels from my sf::Image using the sf::IntRect that I modified. I don't see any method that can pull out an array of pixels using sf::IntRect as part of the sf::Image class. I do see the below method but I don't really understand what it is doing. It seems to be pulling out the entire sf::Image pixel array? Whereas I only want a small portion of the sf::Image pixel array.

const Uint8 *    getPixelsPtr () const

6
Graphics / Highlight sprite when selected
« on: April 13, 2016, 07:24:24 am »
I need some guidance on how to go about achieving the below outcome as I am at a bit of a loss on how to go about it. I am not looking for code but any pseudo-code or a general outline of the steps involved would be much appreciated.

So I can detect when my sprite is selected. What I want to do is to highlight the outline of the sprite with a specific colour which indicates that it is selected.

Also rather than having this just a static colour, is there is way to make the outline appear as shiny? Would this just involve changing the colour very quickly?

I know the above is vague but I am still new to sfml and graphics programming and I feel like I hit a bit of a wall with this one.

7
Graphics / pixel map question
« on: March 09, 2016, 04:42:02 am »
Hi Folks,

I am trying to do a RISK type game where I have regions and you move armies onto each region etc.

Now I have large images which 5680 X 4178 pixels. However it only contains an outline of the region. I've tried uploading to tinypic but it doesn't display very well.



So I have created another image with the interior of the region as well as just the border



So I then use the 2nd image which the pixels within the region set to white to create a pixel vector. The below code, basically checks each pixel for color. If it is white, then I add a sf::Vector2i entry to a vector for that region.


vector<sf::Vector2i> regionLandPixelList; //stores a list of pixels for this region that denotes a land pixel

void drawEngine::initRegionPixelVector(Region *passed_Region)
{
        sf::Image image;
        sf::Color color;

        image.loadFromFile(passed_Region->getRegionImageFileName());

        if (passed_Region->isRegionPlayable())
        {
                for (int x = 0; x < image.getSize().x; x++)
                {
                        for (int y = 0; y < image.getSize().y; y++)
                        {
                                color = image.getPixel(x,y);

                                if (color == sf::Color::White)
                                {
                                        passed_Region->addPosToRegionLandPixelList(x,y);
                                }
                        }
                }
        }
}


void Region::addPosToRegionLandPixelList(sf::Vector2i pos)
{
        regionLandPixelList.push_back(pos);
}
 

So then I use the regionLandPixelList vector to determine if the user has moved an army into this region. Now this works well but I've found that the regionLandPixelList grows to very large sizes. For example the region above, the size of regionLandPixelList had over 40K entries. And I eventually plan to have around 60 regions....some of them much larger than the one above.

I am concerned about performance as I have to check the regionlandpixellist of each region, every time the user moves an army piece.

So I was wondering, is there any way/calc that I can use with the region outline to determine if the mouse position is within that region? Obviously each region outline is irregular which makes this very tricky.

8
Graphics / Change perspective
« on: February 27, 2016, 12:39:01 am »
So I have a collection of 2D image which are essentially regions of Europe. i.e. one image for Ireland, another for France etc.

I can load these as a vector of textures, set a sprite to use these textures and I can draw them perfectly fine.

However the image looks like it is shown directly above each region. What I am aiming is to change the perspective so it looks like the map is sitting on a table, so it looks like a board-game sitting on a table. I see in GIMP there is a perspective tool but I am finding it a bit hard to get exact look for each individual region. (I've over 50-60 images to change).

So I was wondering if there was some easy way to change/stretch the sprite so that the perspective changes.

9
Graphics / mini-map using views
« on: February 11, 2016, 03:21:10 am »
Hey Folks,

So I've setup a main window using a portion of the map that I want the player to use. Scrolling is working fine using keys and mouse.

Then in the bottom corner, I have a mini-map view which shows the entire map but obviously in mini format. Here is the calc function that I am using to determine the mini map sizes.


void drawEngine::calcMiniMapView()
{
        int xSize = gameWindow.getSize().x * 0.25;
        int ySize = gameWindow.getSize().y * 0.23;

        float xFloat = (float)xSize/gameWindow.getSize().x;
        float yFloat = (float)ySize/gameWindow.getSize().y;

        miniMapView.reset(sf::FloatRect(0.f, 0.f, xSize, ySize));
        miniMapView.setViewport(sf::FloatRect(0.f, 0.77, xFloat, yFloat));
}

 


So what I am trying to do now is to draw a small thin rectangle on the mini-map view, showing the location on the mini-map of what is displayed in the main mapview and I am really struggling to get my head around this idea.

I am struggling to understand how I can get the position from the main view of what is displayed and then convert that to the mini-map. So I was hoping for ideas on how to perform this task.

Do I need to:-

(1) To draw a rectangle, I need to set it's position? So I need to get m_center of the map_view and calc the top-left and bottom-right? I believe the below should do that.


sf::Vector2i topLeft, bottomRight;

        int x = (mapView.getCenter().x - mapView.getSize().x / 2);
        int y = (mapView.getCenter().y - mapView.getSize().y / 2);

        topLeft.x = x;
        topLeft.y = y;

        x = (mapView.getCenter().x + mapView.getSize().x / 2);
        y = (mapView.getCenter().y + mapView.getSize().y / 2);
         
        bottomRight.x = x;
        bottomRight.y = y;

 

(2) Once I get that top-left, I can use that to set the size and position of a rectangle.
(3) However these seem to be the values for the mapview. How can I convert that to a value appropraite to the mini-map dimensions?

10
Graphics / Slow adding textures to a vector
« on: June 05, 2015, 06:18:04 am »
Hey Folks,

So I have around 45 png files that I need to load in when the game first starts and I want to store in a vector.

During the initial process of the game I load in the textures using the below. I only call this once during the init phase of the game.

sf::Time time = gameClock.getElapsedTime();
sf::Texture texture;
vector<sf::Texture> regionTextureList;
cout << "Start time SFML2.2:- " << time.asMilliseconds() << endl;

        for (int i = 0; i < regionListPtr->size(); i++)
        {
                texture.loadFromFile(regionListPtr->at(i).getRegionImageFileName());
                texture.setSmooth(true);
                regionTextureList.push_back(texture);
        }

 

Strangely the loadFromFile is not the slowest part. I've found that the push_back onto the vector as the slowest part.

The PNG files are 5760 x 3240 so obviously quite large. However the content isn't large in size 99kb.  See below an example.



So when I say slow, I meaning minutes. My graphics card isn't the greatest AM Radeon HD8490 4818MB (when I going into advanced setting of my screen resolution page in windows7). My PC has tons of RAM though 32GB.

Whilst I can obviously scale down the png, load them in and scale up, I do find the png a bit sharper/nicer (especially scaling up) when I load them as 5760 X 3240, as opposed to loading them in as 1920 x 1080 and scaling up by 3.

I am wondering if there is a more efficient way to store very large textures? I thought a vector was the way to go in c++ but wondering if there is a better method? Or am I just limited by my rubbish graphics card?

11
Graphics / Scalable vector graphic support in SFML?
« on: June 03, 2015, 02:52:06 am »
Hi Folks,

I tried loading in a Scalable vector graphic (SVG) file into a texture using the loadfromFile method.
I get:-
Reason: Image not of any known type, or corrupt
Failed to create texture, invalid size (0x0)
 

So then I tried to search to see if SFML supports SVG. I found the change log but no mention of SVG support.
http://www.sfml-dev.org/changelog.php

In addition searches in this forum suggest this isn't supported. I found the below thread from 2012.

http://en.sfml-dev.org/forums/index.php?topic=7667.0

As this is a few years old now, I thought to re-ask the question. I'm assuming the answer is still no but wondering if this was a feature to be included later?

12
Graphics / Highlighting a sprite when mouse left clicks it
« on: May 31, 2015, 05:19:35 am »
So I've working code that can detect when the mouse has left clicked within the bounds of a sprite. For reference the method is below and works very well.

bool icon::hasMouseLeftClickedOnIcon(sf::Vector2f mousePosWhenLeftPressed)
{
    if (getIconSpriteBounds().contains(mousePosWhenLeftPressed))
    {
        // mouse is on sprite!
                return true;
    }
        else
        {
                return false;
        }
}
 

Now when this is true, I wanted the sprite to have a small outline in say bright yellow to show that this sprite is the one that has been selected. Initially I had planned to use outlines but then I realized that only shapes have a setOutlineThickness etc methods.

Now my sprite's texture is a small texture with a circle and a transparent background.
So then I tried creating a circle with the fill-colour, I set it's position to the sprites position. I draw the circle first and then the sprite on top of it. The circle's radius is just a little larger than the texture circle, so it get the appearance of an outline.

Code is simple as well.


sf::CircleShape circle(28);
circle.setFillColor(sf::Color::Yellow);
circle.setPosition(sprite.getPosition().x, sprite.getPosition().y);
gameWindow.draw(circle);
gameWindow.draw(sprite);

 

However I find that the circle is not always line up correctly. One side of the outline is not as thick/even as the other side etc. So I'm assuming that as the texture is rectangle with a transparent background, then the circle part of the texture doesn't quite sync up with the circle shape that I am drawing.

So I am wondering is there an easier way to achieve this functionality, i.e. create a small outline of a sprite/texture when it gets selected by the mouse.

13
Graphics / Smooth screen scrolling
« on: May 19, 2015, 05:34:05 am »
Hi Folks,

So I'm using a view to display the game area. When the mouse goes to the edge of the game window, then I simply move the view by offset in that direction. This seems to work ok, i.e. I can detect the mouse pos and the view.move does indeed move it in the correct direction.

However the scrolling is jerky and not consistence. Sometimes it is really fast and other times it is painfully slow.
I am still quite new to sfml and c++ in general so am I missing something obvious here in regards to how quickly it goes throw the event loop? Is there a technique to make that more consistent?

Here is some example code of what I have right now.


#include <TGUI/TGUI.hpp>

using namespace std;

class Test
{
public:
        Test();

        enum screenScrollDirection
        {
                NO_SCROLL = -1,
                SCROLL_LEFT,
                SCROLL_RIGHT,
                SCROLL_UP,
                SCROLL_DOWN
        };

        void calcMapView();
        void initTextureList();
       
        void scrollMapView(screenScrollDirection passed_ScrollDirection);
        screenScrollDirection isScreenScrollRequired();

        void drawAllRegions();
        void drawRegion(int passed_indx);

        tgui::Gui gui;
        tgui::Callback callback;
       
        sf::RenderWindow gameWindow;
        sf::VideoMode desktop;
        vector<sf::Texture> regionTextureList;
        sf::Font font;
        sf::Event event;
        sf::View mapView;
        sf::Vector2f mousePos;

};

Test::Test()
{
       
        desktop = sf::VideoMode::getDesktopMode();
        gameWindow.create(sf::VideoMode(desktop.width, desktop.height, desktop.bitsPerPixel), "Test", sf::Style::Fullscreen);
        gameWindow.setPosition(sf::Vector2i(0, 0));
        gui.setWindow(gameWindow);
        calcMapView();
        initTextureList();
}

void Test::calcMapView()
{
        int xSize = gameWindow.getSize().x;
        int ySize = gameWindow.getSize().y * 0.7500;

        float xFloat = (float)xSize/gameWindow.getSize().x;
        float yFloat = (float)ySize/gameWindow.getSize().y;

        mapView.reset(sf::FloatRect(0.f, 0.f, xSize, ySize));
        mapView.setViewport(sf::FloatRect(0.f, 0.f, xFloat, yFloat));
}

void Test::initTextureList()
{
        for (int i = 0; i < 14; i++)
        {
                regionTextureList.push_back(sf::Texture());
                regionTextureList[i].loadFromFile("regionImage_" + to_string(i) + ".png");
        }

}

void Test::scrollMapView(Test::screenScrollDirection passed_ScrollDirection)
{
        switch (passed_ScrollDirection)
        {
        case Test::SCROLL_LEFT:
                mapView.move(-1 * 30, 0);
                break;
        case Test::SCROLL_RIGHT:
                mapView.move(1 * 30, 0);
                break;
        case Test::SCROLL_UP:
                mapView.move(0, -1 * 30);
                break;
        case Test::SCROLL_DOWN:
                mapView.move(0, 1 * 30);
                break;
        case Test::NO_SCROLL:
        default:
                break;
        }
}

//checks mouse position if screen scroll is required. Return the enum direction or -1 if not required.
Test::screenScrollDirection Test::isScreenScrollRequired()
{
        if (mousePos.x < 10)
        {
                return SCROLL_LEFT;
        }
        else if (mousePos.x > gameWindow.getSize().x - 10)
        {
                return SCROLL_RIGHT;
        }
        else if (mousePos.y < 10)
        {
                return SCROLL_UP;
        }
        else if (mousePos.y > gameWindow.getSize().y - 10)
        {
                return SCROLL_DOWN;
        }
       
        return NO_SCROLL;
}

void Test::drawAllRegions()
{
        gameWindow.clear(sf::Color::Black);
       
        gameWindow.setView(mapView);
        for (int i = 0; i < 14; i++)
        {
                drawRegion(i);
        }
        gameWindow.setView(gameWindow.getDefaultView());
        gameWindow.display();
       
}

void Test::drawRegion(int passed_indx)
{
        sf::Sprite sprite;

        sprite.setTexture(regionTextureList[passed_indx]);
        sprite.setPosition(0, 0);
        sprite.setScale(4.0f, 3.0f);

        gameWindow.draw(sprite);
}


int main()
{
        Test testClass;

        do
        {
                testClass.drawAllRegions();

                while (testClass.gameWindow.pollEvent(testClass.event))
                {
                        if (testClass.event.type == sf::Event::MouseMoved)
                        {
                                testClass.mousePos.x = testClass.event.mouseMove.x;
                                testClass.mousePos.y = testClass.event.mouseMove.y;
                                testClass.scrollMapView(testClass.isScreenScrollRequired());
                        }
                }

               
        } while (true);
}

 

14
Graphics / Detecting shaded areas of a texture
« on: May 07, 2015, 04:38:32 am »
First I'm still newish to sfml. I have been tinkering with it on/off for a while now and I've been using only really basic items. i.e. loading texture, drawing sprites, changing colour of sprites. I've also setup a nice wee GUI using the tgui libs.

Anyway, I have a basic game working now but I want to start doing more advance stuff with the sprites. The game at it's core has a real-life map of Europe, split into individual regions. Each png are all of the same size, say 1218 X 730. The background is transparent and each image has each a shaded area for that region, drawn in the proper position. i.e. Italy is drawn where italy should be, etc, etc

The reason I did it this way was because it allows me to set the sprite position as 0,0 and the regions of europe all line up perfectly. When I tried having each region image as a small image, it was a nightmare to try to align up each region correctly.

Anyway, what I want to do now is to have army sprites on the map, but they should only go on the land areas of the region map. So I need a way to detect whether a sprite's position is within a shaded area and indeed which region it is located. As the region areas are all irregular, then defining via hard-coded values would seem to be quite a difficult and likely involve overlap.

Also I cannot simply check the bounding box i.e. something like below, as I assume the bounding box will the same for all regions i.e. 1218 * 730. Right?

if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
    // transform the mouse position from window coordinates to world coordinates
    sf::Vector2f mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));

    // retrieve the bounding box of the sprite
    sf::FloatRect bounds = sprite.getGlobalBounds();

    // hit test
    if (bounds.contains(mouse))
    {
        // mouse is on sprite!
    }
}
 

So I am wondering if there is any dynamic way to go about checking whether a mouse or another sprite is located on a shaded part of a texture? Is this possible with SFML and if so how (at a high-level - not looking for someone to write the code) ?

15
General / toAnsiString causes crash
« on: February 21, 2015, 06:47:29 am »
first, I'm using visual studio express 2012, with the sfml2.2.

I've trying to convert from sf::String to std::string. Basically I want user to input numbers, convert those numbers to int and then perform various calculations based on the input.

However every-time I run toAnsiString(), I get an error during the operator delete. The error is:-



When I press retry and I look into the stack, I see that the first entry in the stack is below.



Then the next stack entry up is below:



And then I see the operator delete in the next one.



My code is extremely basic as I've scaled it right back now to try to determine what I am doing wrong. I'm still very new to programming so help appreciated....with basic instructions. :) I have double checked to ensure that sfml lib are linked correctly, i.e. I ensure I didn't mix up -d libs in the release configuration and vice-versa.

Here is my code. first my header

#ifndef TEST_H
#define TEST_H

#include <string>
#include <SFML/Graphics.hpp>

using namespace std;

class testClass
{
public:

        testClass();

        void testSFString();
        bool chkStr();

private:
        sf::String SFStr;
        std::string stdStr;
};

#endif

 

Then my source file.
#include "test.h"

testClass::testClass()
{

}

void testClass::testSFString()
{
        SFStr = "testStr";
        if (chkStr())
        {
                stdStr = SFStr.toAnsiString(); //dies here
        }
}

bool testClass::chkStr()
{
        if (!SFStr.isEmpty())
                return true;
        else
                return false;
}
 

finally my main

#include "test.h"

int main()
{
        testClass test;

        test.testSFString();
}
 

Pages: [1] 2
anything