我決定在沒有深入研究和實踐的情況下編寫一個簡單的SFML和OpenGL窗口。不過,我覺得我可以從我自己的錯誤調整中得到更快的學習,並且需要我的結構和主要來自示例的破碎程序的一些幫助。通過使用EGE文件,我試圖簡化代碼並使調用和使用函數變得容易,但是這種方式也失敗了。C++ SFML和OpenGL錯誤
「EGE.h」
#ifndef EGE_H
#define EGE_H
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
namespace EGE
{
class API
{
public:
//Constants
static const int VIDEO_HEIGHT;
static const int VIDEO_WIDTH;
static const int VIDEO_BITS_PER_PIXEL;
//Variables
std::string static eTitle;
sf::Window static eWindow;
sf::VideoMode static eVideoMode;
sf::WindowSettings static eSettings;
sf::Clock static eClock;
sf::Event static eEvent;
bool Running;
//Functions
int Initialize_Window();
void Initialize_OpenGL();
void Finish_OpenGL();
};
}
#endif /* EGE_H */
「EGE.cpp」
#include "EGE.h"
namespace EGE
{
typedef EGE::API EGE;
int Initialize_Window()
{
EGE::eSettings.DepthBits = 24;
EGE::eSettings.StencilBits = 8;
EGE::eSettings.AntialiasingLevel = 2;
EGE::eWindow window(EGE::eVideoMode(EGE::VIDEO_HEIGHT = 800,EGE::VIDEO_WIDTH = 600,EGE::VIDEO_BITS_PER_PIXEL = 32), "SFML OpenGL", EGE::eSettings);
//Start Game Loop
while (EGE::eWindow.IsOpened() window)
{ //Process Events
while (EGE::eWindow.GetEvent window(EGE::eEvent))
{ //Close Window
if (EGE::eEvent.Type == EGE.eEvent.Closed)
{
EGE::eWindow.Close() window;
}
//Resize Event : Adjust Viewport
if (EGE::eEvent.Type == EGE::eEvent.Resized)
{
glViewport(0, 0, EGE::eEvent.Size.Width, EGE::eEvent.Size.Height);
}
EGE::eWindow.SetActive() window;
EGE::eWindow.Display() window;
}
return EXIT_SUCCESS;
}
}
void Initialize_OpenGL()
{
// Set color and depth clear value
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
}
void Finish_OpenGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
}
「的main.cpp」
#include "EGE.h"
int main(int argc, char* argv[])
{
typedef EGE::API call; //edited thanks!
call.Initialize_OpenGL();
call.Initialize_Window();
call.Finish_OpenGL();
return 0;
}
我的錯誤是:
EGE.cpp: In function 'int EGE::Initialize_Window()':
EGE.cpp:18:22: error: expected ';' before 'window'
EGE.cpp:21:40: error: expected ')' before 'window'
EGE.cpp:21:40: error: 'window' was not declared in this scope
EGE.cpp:21:46: error: expected ';' before ')' token
EGE.cpp:65:1: error: expected '}' at end of input
EGE.cpp: At global scope:
EGE.cpp:65:1: error: expected '}' at end of input
'命名空間EGE {typedef EGE :: API EGE;'這是非常混亂的代碼。你永遠不應該那樣做。如果你想從命名空間中取出某些東西並且typedef,那很好。但是不要將它定義爲與其他名稱相同的名稱。 – 2013-03-15 04:50:40
@尼科爾我當然不會在那裏想!感謝您的建議立即改變。我仍然不明白一些例子是如何從「namespace :: function()」中跳出來的,並跳過這個類。這就是爲什麼我的代碼充斥着namespace :: class :: variable。你可以建議如何在我的主要我可以直接從EGE :: API直接聲明一個變量沒有所有多餘的代碼? – asd 2013-03-15 05:08:45
我不知道你指的是什麼「例子」,但這是不允許的。如果你正在編寫一個類是一個成員的函數,那麼它可以引用沒有限定符的其他成員。但是,這也沒有*名稱空間*限定符。所以你很容易誤解這些例子。要麼這樣,要麼他們不把這些東西放在一個班級裏。 C++不是Java;並非所有事情都要上課。 – 2013-03-15 06:04:28