命名空间表现得很奇怪

时间:2014-08-13 21:57:24

标签: c++ namespaces sfml

好的伙计们......我用SFML编写了我的第一个游戏,我发现了一个非常可恶的问题......我会解释。

的main.cpp

#include "include/SFML/include/SFML.hpp"
#include "include/menu.h"
#include <iostream>
#include <fstream>
#include "include/checkfileexistence.h"
#include "include/fonts.h"

#define FPS 20

bool loadFonts();
bool loadMenu();

int main ()
{
    if (!loadFonts()) {std::cout << "Could not load fonts!"; return EXIT_FAILURE;}
    sf::RenderWindow window (sf::VideoMode(0,0),"Evility", sf::Style::Fullscreen);
    window.setFramerateLimit(FPS);
    sf::Event event;
    window.setKeyRepeatEnabled(false);
    while (window.isOpen())
    {
        window.clear();
        if (window.pollEvent(event))
        {
            switch (event.type)
            {
                case sf::Event::Closed: {window.close(); break;}
                case sf::Event::KeyPressed:
                {
                    switch (event.key.code)
                    {
                        case sf::Keyboard::Escape: {window.close(); break;}
                        case sf::Keyboard::LAlt && sf::Keyboard::F4: {window.close(); break;}
                    }
                }
            }
        }
        if (menu::isBeingUsed)
        {
            if (!menu::isRunning)
            {
                if (!loadMenu()) {std::cout << "Could not load menu files!"; return EXIT_FAILURE;}

                menu::music.setLoop(true);
                menu::music.play();
                menu::isRunning = true;
                window.setVisible(true);
            }

            if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) || (sf::Keyboard::isKeyPressed(sf::Keyboard::W)))
            {
                menu::selectedOption--;
                if (menu::selectedOption < 0)
                {
                    menu::selectedOption = 3;
                }
            }
            if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) || (sf::Keyboard::isKeyPressed(sf::Keyboard::S)))
            {
                menu::selectedOption++;
                if (menu::selectedOption > 3)
                {
                    menu::selectedOption = 0;
                }
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
            {
                switch (menu::selectedOption)
                {
                    case 3:
                        {
                         window.close();
                         break;
                        }
                }
            }

            switch (menu::selectedOption)
            {
            case 0:
                menu::optionSelector.setPosition(sf::Vector2f(60,105));
                break;
            case 1:
                menu::optionSelector.setPosition(sf::Vector2f(60,155));
                break;
            case 2:
                menu::optionSelector.setPosition(sf::Vector2f(60,205));
                break;
            case 3:
                menu::optionSelector.setPosition(sf::Vector2f(60,255));
                break;
            }
            window.draw(menu::spriteBackground);
            window.draw(menu::textStartGame);
            window.draw(menu::textContinueGame);
            window.draw(menu::textOptions);
            window.draw(menu::textQuitGame);
            window.draw(menu::optionSelector);
        }

        window.display();
    }
}

bool loadFonts()
{
    if (!font::ArcadePix.loadFromFile("resources/Fonts/ArcadePix.TTF"))
    {
        return false;
    }
    return true;
}


bool loadMenu ()
{
    menu::music.openFromFile("resources/Music/Unity.wav");
    menu::music.setLoop (true);
    menu::textureBackground.loadFromFile("resources/wallpaper.png");
    menu::spriteBackground.setTexture(menu::textureBackground, false);
    menu::spriteBackground.setPosition(0,0);
    menu::spriteBackground.setScale(window.getSize().width / menu::spriteBackground.getLocalBounds().width, window.getSize().height / menu::spriteBackground.getLocalBounds().height);
    menu::optionSelector.setSize (sf::Vector2f(25,25));
    menu::optionSelector.setFillColor(sf::Color::Yellow);
    menu::optionSelector.setPosition(60,105);
    menu::textStartGame.setFont(font::ArcadePix);
    menu::textStartGame.setColor(sf::Color::Red);
    menu::textStartGame.setPosition(sf::Vector2f(100,100));
    menu::textStartGame.setString("Start Game");
    menu::textContinueGame.setFont (font::ArcadePix);
    if (fileExists("resources/Saves/saves.txt"))
    {
        menu::textContinueGame.setColor(sf::Color::Red);
    }
    else
    {
        menu::textContinueGame.setColor(sf::Color(211,211,211,127));
    }
    menu::textContinueGame.setPosition(100,150);
    menu::textContinueGame.setString("Continue Game");
    menu::textOptions.setFont(font::ArcadePix);
    menu::textOptions.setColor(sf::Color::Red);
    menu::textOptions.setPosition(100,200);
    menu::textOptions.setString("Options");
    menu::textQuitGame.setFont(font::ArcadePix);
    menu::textQuitGame.setColor(sf::Color::Red);
    menu::textQuitGame.setPosition(100,250);
    menu::textQuitGame.setString("Quit Game");
    return true;
}

menu.h

#ifndef MENU_H_
#define MENU_H_

#include "SFML/include/SFML.hpp"

namespace menu{
bool isBeingUsed = true;
bool isRunning = false;

sf::RectangleShape rectBackground (sf::Vector2f (1080,720));


sf::Texture textureBackground;
sf::Sprite spriteBackground;

sf::Text textStartGame;
sf::Text textContinueGame;
sf::Text textQuitGame;
sf::Text textOptions;

sf::RectangleShape optionSelector (sf::Vector2f(0,0));

unsigned int selectedOption;

sf::Music music;
}

#endif

因此,在main.cpp中非常长的函数调用(我期望发生)是程序在RenderWindow对象窗口中查找类,对于类... 但是,函数调用引用了命名空间内的类函数,它还认为窗口在该命名空间中,因为编译返回window was not declared in this scope,我认为这意味着窗口未在菜单命名空间中声明。

我应该如何告诉我的程序在菜单命名空间之外?

和平。

编辑1:添加了所有main.cpp代码,并不是因为它是游戏的未来代码,但它很简单,我觉得没有人会偷它。

1 个答案:

答案 0 :(得分:2)

windowmain的本地变量。如果您需要,请将其传递给loadMenu