C++ & SFML?

Started by
22 comments, last by boogyman19946 10 years, 6 months ago

I see. I did some stuff with the words 'linking' and 'sfml_static' I dno if that was linking.

Advertisement

I have a question. What's going on in the Dog::Dog() constructor there?


Class Dog {
    private:
        Pig pig;
        Cat cat;
}

Dog::Dog()
    : pig(1)
    , cat()
{
}

Coming from other OOP languages, I find this strange tongue.png

It's just an initializer list.

Ah, I see. thanks :)

I have another question. in SFML there is a templated Vector2 class.

I've been trying to extend it for the past few hours but damn...

Here's my code:


#pragma once
#include <SFML/Graphics.hpp>
#include <cmath>

template <typename T>
class Vector2 : sf::Vector2 {

public:
	T mag();
	void normalize();
	void invert();
	void decrease(T i);
	void reset();
	void limit(int i);
};

template <typename T>
T Vector2<T>::mag() {
	return sqrt(this->x*2 + this->y*2);
}

I would not do it that way. By deriving from that class that is not much more than a struct with a constructor, you are overcomplicating this and you will have to copy the vectors you get from sfml into your new type before you can use those methods. You better just make some free functions in the same way most of the functionality for using sf::Vector2 is done.

Though you should think again before doing that, the SFML vectors are just a tiny thing for communicating with that library and you may be much better off with using a real vector/matrix library like GLM: http://glm.g-truc.net

Hm.. ok. I'll look into that, thanks!

I run into another issue.

I have a class 'Game'. and when instantiated, I want to store the instance on a static member of the 'Game' class but some code in SFML complains about NonCopyable.

I tried storing a reference, reference to a pointer and a the dereferenced object to no avail.

Here's the class:


class Game
{
	private:
		sf::RenderWindow mWindow;
		
	protected:
		std::vector<Entity> entities;

	public:
		static Game instance;
}

and the implementation:


Game::Game()
	: mWindow(sf::VideoMode(640, 480), "SFML!")
{
	//set the game instance
	Game::instance = *this;
}

Here's the full error:


error C2248: 'sf::NonCopyable::operator =' : cannot access private member declared in class 'sf::NonCopyable'	c:\sfml2.1\include\sfml\window\window.hpp	477	1	sfmltest2

What's going on here?

That means you should not do that with the RenderWindow as it was intentionally made not copyable to prevent errors.

Also singletons should be avoided as much as possible. Just create your objects somewhere and only pass a reference to as little functions through their arguments as possible and needed, to not get into the trap of creating spaghetti code.

I was able to make it work.

It's not exactly a singleton. I just need my input object, sound object, gui object etc to access each other.

I think passing things around all the time is more like spaghetti code than to have 1 single place to store a reference to something. Less typing as well.

This topic is closed to new replies.

Advertisement