我做了一些命名空间Game和名为Button的类。而且我在cpp文件中定义了功能。我可以使用“使用命名空间游戏”吗?在这个cpp文件中?这不好吗?
我做了这个 Button.h
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <string>
#include <SFML/Window/Event.hpp>
namespace Game {
class Button : public sf::Sprite
{
private:
bool isactive;
public:
Button(sf::Texture texture);
void listen(void (*callback)(), sf::Event);
};
}
然后 Button.cpp
#include "Button.h"
#include <SFML/Window/Mouse.hpp>
using namespace Game;
//for defines
Button::Button(sf::Texture t) : sf::Sprite(t) {
isactive = true;
};
void Button::listen(void (*callback)(),sf::Event e) {
if (isactive && e.type == sf::Event::EventType::MouseLeft && this->getGlobalBounds().intersects(sf::Rect<float>(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y, sf::Mouse::getPosition().x + 1, sf::Mouse::getPosition().y + 1)))
callback();
}