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

Pages: 1 2 3 [4]
46
General / Re: issues with random point inside a floatRect
« on: March 08, 2016, 09:44:11 am »
 :D Thank you Laurent, worked like a charm. Thank you all, case closed ;)

47
General / Re: issues with random point inside a floatRect
« on: March 08, 2016, 08:39:16 am »
okay well, i might use the <random> header at some point, but I'm not too bothered about the distribution of numbers. As long as they look random at this point that is fine by me. I changed the casts to be static_cast<>, but no change sadly. Should the formula work? If so there has to be a problem elsewhere.

Here's the snippet updated with static casting:

sf::Vector2f(static_cast<float>(rand() % (static_cast<int>(spawn.left) + static_cast<int>(spawn.width)) + static_cast<int>(spawn.left)),
                                        static_cast<float>(rand() % (static_cast<int>(spawn.top) + static_cast<int>(spawn.height)) + static_cast<int>(spawn.top))),
 

48
General / Re: Making sf::Texture object a static member of a class.
« on: March 08, 2016, 08:22:55 am »
here's some code for a simple resourceHolder that im using atm, largely based off one that's in the book sfml essentials, main difference between them is that in the book they make the class a singleton. In this case i dont bother, because im lazy and i know that only my game class should hold the resourceHolder, other classes that need a texture get handed a pointer to it. Anyways, here's some code:

header:
class resourceHolder
{
public:
        resourceHolder();
        sf::Texture& getTexture(std::string path);
        sf::Font& getFont(std::string path);
        sf::SoundBuffer& getSound(std::string path);
private:
        std::map<std::string, sf::Texture> textures;
        std::map<std::string, sf::Font> fonts;
        std::map<std::string, sf::SoundBuffer> sounds;
};
 
cpp file:
#include "resourceHolder.h"



resourceHolder::resourceHolder()
        : textures()
{
}

sf::Texture & resourceHolder::getTexture(std::string path)
{
        auto& pairFound = textures.find(path);
        if (pairFound != textures.end()) {

                return pairFound->second;
        }
        else
        {
                auto& texture = textures[path];
                texture.loadFromFile(path);
                return texture;
        }
}

sf::Font & resourceHolder::getFont(std::string path)
{
        auto& pairFound = fonts.find(path);
        if (pairFound != fonts.end()) {

                return pairFound->second;
        }
        else
        {
                auto& font = fonts[path];
                font.loadFromFile(path);
                return font;
        }
}

sf::SoundBuffer & resourceHolder::getSound(std::string path)
{
        auto pairFound = sounds.find(path);
        if (pairFound != sounds.end())
        {
                return pairFound->second;
        }
        else
        {
               
                auto& sBuffer = sounds[path];
                sBuffer.loadFromFile(path);
                return sBuffer;
        }
}
 

basically, you just use this class to store your textures sounds whatever, and they stay there without being destroyed until the program exits. For each type of resource there is a map object that stores the resources filepath as its id and a resource of the desired type. It checks the map to see if that resource already exists - if it does, it returns it, if it doesn't, it adds it then returns it. To simplify the file paths, I wrote them as const strings in a header file behind a namespace and include that in the project too. So for example, instead of writing "graphics/player.png" i can instead type something like Sprites::player.

Hope this helps and isn't too awful a way of doing it.

49
General / issues with random point inside a floatRect
« on: March 08, 2016, 08:00:47 am »
Hello everyone, I'm currently working on a finite state machine for my enemy ai. One of the states i want to implement is a patrolling state. I want this to be the default state when the enemy spawns, so they patrol the given spawn area which is passed as a floatRect.

Now to do this, I figured i would make a randomly positioned floatrect, which is fine, but to get a random point inside the floatrect at which to spawn the enemy sprite i used the following method:

spawn = the name of the floatrect in question...


sf::Vector2f(float(rand() % (int(spawn.left) + int(spawn.width)) + int(spawn.left)),
                float(rand() % (int(spawn.top) + int(spawn.height)) + int(spawn.top)));

 
Now i've tried various versions of casting to int and not casting to int, without any noticeable change. they need to be ints for rand() to work, and each number needs to also be cast back into a float to work with vector2f. i add the left to the width to get the global position of the far side of the rect, and then add the left side to the formula to make it so that rand() starts its range at the beginning of the rect and not at 0.

Either way, none of this is working and the sprites appear seemingly wherever they like.

can include more code if needs be, but for testing purposes all im doing is drawing them to the screen in the initial position contained in the above vector2f.

Thanks for your time.

50
General / Re: SFML book, chapter 2 issue
« on: February 22, 2016, 11:49:01 am »
I mean that without adding a unique namespace for each enum class you don't get the uniformity of having all the enums called ID. I realise it is still very similar, plus is shorter to write, I just simply liked that each enum was prefixed with ID, no biggie.

51
General / Re: SFML book, chapter 2 issue
« on: February 22, 2016, 01:14:18 am »
Hi, yeah thanks, I had actually seen that article prior to my last question. Just needed some reassurance based on my presumptions. Although it seems to do the same thing, sadly, when you use enum class but no namespace, you lose the convention of each enum being ID:: so it's not as uniform, but still. I'll do it. 

52
General / Re: SFML book, chapter 2 issue
« on: February 20, 2016, 09:35:55 pm »
okay so i got it to work last night, by declaring the namespace and the enum in the textureHolder class header and including that header in my game class and then storing the textureHolder as a member variable in the game class.

however this is a bit messy and not very modular so I will do as Nexus suggested and put it in it's own file. As for using the c++11 strongly typed enum, from what I can gather, I would literally just use, for example, enum class textureId, and not use the namespace, because the strongly typed nature of enum class means that the enums are safely locked behind texturedId::textureName, which was the whole point of using a namespace. Is this correct?

code for clarity:
use:
enum class textureId
{
    textureName1,
    textureName2,
    etc..
};
instead of:
namespace Textures
{
    enum ID
    {
        textureName1,
        etc...
    };
}
 

53
General / Re: SFML book, chapter 2 issue
« on: February 20, 2016, 09:19:13 am »
hey, yeah I have included <SFML/Graphics.hpp>, but that's not what my issue is. My issue is that in the book/tutorial they make a namespace Textures, an enum for storing ids to later refer to textures. Its the part Textures::ID that causes the error that stops the program from compiling. And I'm not sure how I'm supposed to access the namespace from within that header file, having only declared the namespace in the main.cpp.

here is my code:
TextureHolder (where the error is):
#pragma once
#include <map>
#include <string>
#include <memory>
#include <stdexcept>
#include <cassert>
#include <SFML/Graphics.hpp>

class TextureHolder
{
        private:
                std::map <Textures::ID, std::unique_ptr<sf::Texture>> mTextureMap;
};

Main.cpp (where the namespace and enum are created):
#include <SFML/Graphics.hpp>
#include "Game.h"

namespace Textures
{
        enum ID
        {
                starfield,
                ship,
                laser
        };
}

int main()
{
        Game game;
        game.run();

        return 0;
}

Other than that there is the game class - header and cpp - but I don't believe they hold any relevance to this.

54
General / SFML book, chapter 2 issue
« on: February 20, 2016, 08:33:17 am »
Hello, I am currently working through the SFML Game Development book. I am on chapter 2 at the part where we first declare the namespace called Textures. I have declared the namespace, created the enum in my main.cpp, and when I start to make a header file for the TextureHolder class, the book says to add this map declaration:

private:
 std::map<Textures::ID,
 std::unique_ptr<sf::Texture>> mTextureMap;

The code doesn't work for me, I get error '::' must be followed by a namespace or class name. I do not know how to access the namespace from the main.cpp in this header. The book doesn't show how, and neither does the github code, partially, I think because the github code is what it could/should look like at the end of the chapter. I have seen that it is possible to declare a namespace in a header file and include that into TextureHolder.h, but in the book it is declared in main.

Any advice on how to achieve this step in the book would be appreciated.

55
General / Re: game crashing on draw. Possibly because of pointers.
« on: February 14, 2016, 05:23:29 am »
Thank you very much for your help. I tried using smart pointers but that didnt fix the issue, I had used just raw objects to start with (pointers are scary) but no matter what, I couldn't get the laser to find itself in the vector. I had an idea that it was because the vector copied them, thus changing their memory addresses, although that makes no sense, lol, as well, I'm sure you can imagine.  :-[

In the end, I went back to using raw objects, but instead of having my code for checking distance in the Laser class, I made a function for it the player class, which is where the vector of lasers was stored anyway. This basically stopped it from going out of scope. And instead of searching the vector for itself, I just poll the vector now to check if the lasers need removing.

So its working, at last. thanks again, you got me on the right track for sure.

56
General / game crashing on draw. Possibly because of pointers.
« on: February 13, 2016, 05:01:44 am »
Hello all, this is probably a fairly length bit of code for a forum post, so I do aplogise, but if anyone could help me figure out why this is crashing, I'd be very appreciative  ;)

okay, player headder file and cpp:
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>
#include "laserBeam.h"

class Player
{
public:
        int x;
        int y;
        sf::CircleShape shape;
        sf::Texture frame1;
        sf::Texture frame2;
        sf::Texture frame3;
        sf::Sprite sprite;
        sf::SoundBuffer buffer;
        sf::Sound sound;
        std::vector <Laser*> shots;
        int count;
        void move();
        void getAngle(sf::Vector2f mouse);
        Player(int xI, int yI, sf::CircleShape iconI);
        Player() {};
        ~Player();
       
};

#include <SFML/Graphics.hpp>
#include "Player.h"

void Player::move()
{
        float x = this->sprite.getPosition().x;
        float y = this->sprite.getPosition().y;
        float xR = sin((this->sprite.getRotation()*3.14159265 / 180)) * 3;
        float yR = cos((this->sprite.getRotation()*3.14159265 / 180))* -3;
        this->sprite.setPosition(x + xR, y + yR);
        switch(this->count){
                case 0:
                        this->sprite.setTexture(this->frame1);
                        this->sound.play();
                        break;
                case 6:
                        this->sprite.setTexture(this->frame2);
                        this->sound.play();
                        break;
                case 12:
                        this->sprite.setTexture(this->frame3);
                        this->sound.play();
                        break;
        }
        this->count++;
        if (this->count == 19) {
                this->count = 0;
        }
}

void Player::getAngle(sf::Vector2f mouse)
{
       
        float angle = atan2(mouse.y - this->sprite.getPosition().y, mouse.x - this->sprite.getPosition().x);
        angle = ((angle * 180) / 3.14159265) + 90;
        {
                angle = 360 - (-angle);
        }
       
        this->sprite.setRotation(angle);
       


}

Player::Player(int xI, int yI, sf::CircleShape iconI)
{
        this->shape = iconI;
        this->x = xI;
        this->y = yI;
        this->count = 0;
        this->frame1.loadFromFile("playerU.png");
        this->frame2.loadFromFile("playerU1.png");
        this->frame3.loadFromFile("playerU2.png");
        this->buffer.loadFromFile("conStep.wav");
        this->sprite.setTexture(this->frame1);
        this->sound.setBuffer(buffer);
        this->sprite.setOrigin(this->sprite.getGlobalBounds().width / 2, this->sprite.getGlobalBounds().height / 2);




}
Player::~Player()
{
}

okay so that's the player class etc, here is the laser class header and cpp:
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

class Laser
{
public:
        sf::Texture texture;
        sf::Sprite sprite;
        int speed;
        sf::Vector2i startPoint;
        sf::SoundBuffer buffer;
        sf::Sound sound;
        void move(std::vector <Laser*> shots);
        bool operator == (const Laser& rh)const;
        Laser(float rot, int spd, sf::Vector2i pos);
        Laser() {};
        ~Laser();
};

#include "laserBeam.h"
#include <iostream>
#include <algorithm>

void Laser::move(std::vector <Laser*> shots)
{
        float x = this->sprite.getPosition().x;
        float y = this->sprite.getPosition().y;
        float xR = sin((this->sprite.getRotation()*3.14159265 / 180)) * this->speed;
        float yR = cos((this->sprite.getRotation()*3.14159265 / 180))* -this->speed;
        this->sprite.setPosition(x + xR, y + yR);
       
        float dX = this->sprite.getPosition().x - this->startPoint.x;
        float dY = this->sprite.getPosition().y - this->startPoint.y;
        float dist = hypotf(dX, dY);
        if (dist > 32) {
                int index = -1;
                for (int i = 0; i < shots.size(); i++) {
                        std::cout << this << " " << &shots[i] << std::endl;
                        if (*this == *shots[i]) {
                                std::cout << "found match" << std::endl;
                                index = i;
                                break;
                        }
                }
                if (index >= 0) {
                        shots.erase(shots.begin() + index);
                }
               
               
        }
       
}

bool Laser::operator==(const Laser&  rh) const
{
        return (this == &rh);
}

Laser::Laser(float rot, int spd, sf::Vector2i pos)
{
        this->texture.loadFromFile("laser.png");
        this->sprite.setTexture(this->texture);
        this->sprite.setOrigin(this->sprite.getGlobalBounds().width/2, this->sprite.getGlobalBounds().height/2);
        this->sprite.setRotation(rot);
        this->sprite.setPosition(pos.x, pos.y);
        this->startPoint = pos;
        this->speed = spd;
        this->buffer.loadFromFile("target.wav");
        this->sound.setBuffer(buffer);
        this->sound.play();
       

}

Laser::~Laser()
{

}
 
and now the main cpp:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Player.h"
#include "builder.h"
#include "laserBeam.h"
#include <iostream>
#include <string>
#include <vector>

int main()
{
        sf::RenderWindow window(sf::VideoMode(1024, 768), "SFML works!");
        sf::CircleShape shape;
        Player p(100, 100, shape);
        Builder w(32, 640, 640);
        p.shape.setFillColor(sf::Color(75, 0, 130));
        p.shape.setRadius(20.f);
        p.shape.setPosition(sf::Vector2f(p.x, p.y));
        sf::Font font;
        font.loadFromFile("arial.ttf");
        sf::Text words;
        words.setFont(font);
        words.setString(sf::String("some words n shit"));
        words.setPosition(sf::Vector2f(100, 100));
        words.setColor(sf::Color(255,255,255));
        window.setFramerateLimit(60);
        sf::View view1(sf::FloatRect(p.x, p.y, 600, 600));
       
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed) {
                                window.close();
                        }
                        else if (event.type == sf::Event::MouseButtonReleased) {
                                if (event.mouseButton.button == sf::Mouse::Left) {
                                        std::cout << "mouse click" << std::endl;
                                        float rot = p.sprite.getRotation();
                                        int spd = 6;
                                        sf::Vector2i pos;
                                        pos = sf::Vector2i(p.sprite.getPosition());
                                        Laser l(rot, spd, pos);
                                        Laser * ptr;
                                        ptr = &l;
                                        p.shots.push_back(ptr);
                                }
                        }
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
                        p.move();
                }
                if (!sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
                        p.sprite.setTexture(p.frame1);
                        p.count = 0;
                }

                view1.setCenter(p.sprite.getPosition().x, p.sprite.getPosition().y);
                window.setView(view1);
                sf::Vector2i mouse = sf::Mouse::getPosition(window);
                sf::Vector2f worldPos = window.mapPixelToCoords(mouse);
                p.getAngle(worldPos);
                for (int i = 0; i < p.shots.size(); i++) {
                        (*p.shots[i]).move(p.shots);
                }
                window.clear();
                window.draw(w.floorSprite);
                for (int i = 0; i < p.shots.size(); i++) {
                        (*p.shots[i]).sprite.setTexture((*p.shots[i]).texture);
                        window.draw((*p.shots[i]).sprite);
                }
                window.draw(p.sprite);
                window.draw(words);
                window.display();
        }

        return 0;
}

So yeah, I just included what I thought was relevant. There is another class, but that's for building the map and has worked fine from the start. The problem is when i try and draw a laser from the 'p.shots' vector. It is a dereferenced pointer, and everything else works when i comment out the draw call. again, any help would be appreciated. Thanks for your time!

Pages: 1 2 3 [4]
anything