我围绕OpenGL制作了一个小框架来创建一个简单的2D游戏。 当我完成我的Texture类并在我的引擎Thogl(Orthogonal OpenGL)类中使用它 我有很多无法解释的错误,这些错误没有用。 我试图找到它,但我无法弄明白,我希望你们能帮我。我正在使用这些库:GLEW,GLFW,OpenGL32,GLM。
我的代码
Texture.h
#pragma once
#include <Magick++.h>
#include "Thogl.h"
class Texture
{
public:
Texture(GLenum textureTarget);
~Texture();
bool LoadImage(const char* fileName);
void Bind(GLenum textureUnit);
Magick::Image* image;
private:
Magick::Blob blob;
GLuint textureObject;
GLenum textureTarget;
};
Texture.cpp
#include "Texture.h"
Texture::Texture(GLenum target)
{
textureTarget = target;
image = NULL;
}
Texture::~Texture()
{
}
bool Texture::LoadImage(const char* fileName)
{
try
{
image = new Magick::Image(fileName);
image->write(&blob, "RGBA");
}
catch(Magick::Error error)
{
Error (error.what());
return false;
}
glGenTextures(1, &textureObject);
glBindTexture(textureTarget, textureObject);
glTexImage2D(textureTarget, 0, GL_RGBA, image->columns(), image->rows(), 0, GL_RGBA, GL_UNSIGNED_BYTE, blob.data());
glTexParameterf(textureObject, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(textureObject, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return true;
}
void Texture::Bind(GLenum textureUnit)
{
glActiveTexture(textureUnit);
glBindTexture(textureTarget, textureObject);
}
Thogl.h
#pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
#include <glm.hpp>
#include <gtx/transform.hpp>
#include <map>
#include "Texture.h"
using namespace std;
class Thogl
{
public:
Thogl();
~Thogl();
bool init(int windowWidth, int windowHeight, const char* windowTitle);
Texture* CreateTexture(const char* textureName, const char* fileName);
Texture* CreateTexture(const char* textureName);
void Start(void (*RunCallback)());
private:
void run();
GLFWwindow* window;
static void KeyCallback(GLFWwindow*, int key, int scancode, int action, int mods);
void SetCallbacks();
void Terminate();
map<string, Texture*> Textures;
void (*RunCallback)();
};
void Error(const char* error);
Thogl.cpp
#include "Thogl.h"
ofstream errorLog;
Thogl::Thogl()
{
Magick::Image* image;
Magick::Blob blob;
try
{
image = new Magick::Image("//Lol.png");
image->write(&blob);
}
catch(Magick::Error error)
{
Error(error.what());
}
}
Thogl::~Thogl()
{
}
void Thogl::run()
{
while (!glfwWindowShouldClose(window))
{
RunCallback();
glfwSwapBuffers(window);
glfwPollEvents();
}
}
bool Thogl::init(int screenWidth, int screenHeight, const char* windowTitle)
{
errorLog.open("Error.log");
if (!glfwInit())
{
Error("Could not initialize GLFW library");
return false;
}
window = glfwCreateWindow(screenWidth, screenHeight, windowTitle, NULL, NULL);
if (!window)
{
errorLog << "Error: Could not initialize GLFW window\n";
glfwTerminate();
return false;
}
glfwMakeContextCurrent(window);
GLenum error = glewInit();
if (error != GLEW_OK)
{
errorLog << "Error GLEW: " << glewGetErrorString(error) << "\n";
return false;
}
SetCallbacks();
glClearColor(138.f,43.f,226.f, 0);
return true;
}
void Thogl::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
}
void Thogl::SetCallbacks()
{
glfwSetKeyCallback(window, KeyCallback);
}
void Thogl::Terminate()
{
glfwDestroyWindow(window);
glfwTerminate();
errorLog.close();
}
void Error(const char* error)
{
errorLog << "Error: " << error << "\n";
}
void Thogl::Start(void (*Callback)())
{
RunCallback = Callback;
run();
}
Texture* Thogl::CreateTexture(const char* textureName, const char* fileName)
{
Texture* newTexture = new Texture(GL_TEXTURE_2D);
if (!newTexture->LoadImage(fileName))
{
Error("Couldn't load image file");
return 0;
}
Textures.insert(make_pair(textureName, newTexture));
return newTexture;
}
Texture* Thogl::CreateTexture(const char* textureName)
{
Texture *newTexture = new Texture(GL_TEXTURE_2D);
Textures.insert(make_pair(textureName, newTexture));
return newTexture;
}
这是在VS2010 express上编译时的输出:
>------ Build started: Project: 2DEngine, Configuration: Debug Win32 ------
> Thogl.cpp
> Texture.cpp
> thogl.h(25): error C2143: syntax error : missing ';' before '*'
> thogl.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
> thogl.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
> thogl.h(25): warning C4183: 'CreateTexture': missing return type; assumed to be a member function returning 'int'
> thogl.h(26): error C2143: syntax error : missing ';' before '*'
> thogl.h(26): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
> thogl.h(26): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
> thogl.h(26): warning C4183: 'CreateTexture': missing return type; assumed to be a member function returning 'int'
> thogl.h(36): error C2065: 'Texture' : undeclared identifier
> thogl.h(36): error C2059: syntax error : '>'
> thogl.h(38): error C2143: syntax error : missing ';' before '}'
> texture.h(8): error C2143: syntax error : missing ';' before '{'
> texture.h(24): error C2143: syntax error : missing ';' before '}'
> texture.cpp(4): error C2653: 'Texture' : is not a class or namespace name
> texture.cpp(5): error C2143: syntax error : missing ';' before '{'
> texture.cpp(8): error C2143: syntax error : missing ';' before '}'
> texture.cpp(9): error C2653: 'Texture' : is not a class or namespace name
> texture.cpp(9): fatal error C1903: unable to recover from previous error(s); stopping compilation
> Generating Code...
> Skipping... (no relevant changes detected)
> Main.cpp
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
答案 0 :(得分:2)
两个头文件之间存在循环依赖关系。它们包括在一起。但是编译器会跳过循环包含(你告诉它使用#pragma once
)并且在首先解析的文件中,其他文件中的类型将是未知的,从而导致错误。
要解决此问题,您应该删除循环包含(Texture.h
似乎不需要Thogl.h
)或使用所需类型的前向声明。