2012-01-24 31 views
0

我自己和其他一些人正在构建一个简单的侧滚动器类游戏。然而,我无法得到他们的帮助来回答我的问题,所以我把它给你,下面的代码留下SIGSEGV错误在注释的地方...如果有人可以告诉我为什么,我会很感激。如果您需要更多信息,我会密切关注这一点。在openGL和SDL上使用C++在Linux上的SIGSEGV错误

Main.cpp的

Vector2 dudeDim(60,60); 
Vector2 dudePos(300, 300); 
Entity *test = new Entity("img/images.jpg", dudeDim, dudePos, false); 

导致:

Entity.cpp

Entity::Entity(std::string filename, Vector2 size, Vector2 position, bool passable): 
mTexture(filename) 
{ 
    mTexture.load(false); 
    mDimension2D = size; 

    mPosition2D = position; 

    mPassable = passable; 
} 

导致:

Textures.cpp

void Texture::load(bool generateMipmaps) 
{ 
    FREE_IMAGE_FORMAT imgFormat = FIF_UNKNOWN; 

    FIBITMAP *dib(0); 

    imgFormat = FreeImage_GetFileType(mFilename.c_str(), 0); 

//std::cout << "File format: " << imgFormat << std::endl; 

if (FreeImage_FIFSupportsReading(imgFormat)) // Check if the plugin has reading capabilities and load the file 
    dib = FreeImage_Load(imgFormat, mFilename.c_str()); 
if (!dib) 
    std::cout << "Error loading texture files!" << std::endl; 

BYTE* bDataPointer = FreeImage_GetBits(dib); // Retrieve the image data 

mWidth = FreeImage_GetWidth(dib); // Get the image width and height 
mHeight = FreeImage_GetHeight(dib); 
mBitsPerPixel = FreeImage_GetBPP(dib); 

if (!bDataPointer || !mWidth || !mHeight) 
    std::cout << "Error loading texture files!" << std::endl; 

// Generate and bind ID for this texture 

vvvvvvvvvv !!! ERROR HERE !!! vvvvvvvvvvv

glGenTextures(1, &mId); 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^

glBindTexture(GL_TEXTURE_2D, mId); 

int format = mBitsPerPixel == 24 ? GL_BGR_EXT : mBitsPerPixel == 8 ? GL_LUMINANCE : 0; 
int iInternalFormat = mBitsPerPixel == 24 ? GL_RGB : GL_DEPTH_COMPONENT; 

if(generateMipmaps) 
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mWidth, mHeight, 0, format, GL_UNSIGNED_BYTE, bDataPointer); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering 

//std::cout << "texture generated " << mId << std::endl; 
FreeImage_Unload(dib); 
} 

读彼得的建议后,我已经改变了我的main.cpp文件:

#include <iostream> 
#include <vector> 

#include "Game.h" 

using namespace std; 


int main(int argc, char** argv) 
{ 

Game theGame; 

/* Initialize game control objects and resources */ 
if (theGame.onInit() != false) 
{ 
    return theGame.onExecute(); 
} 
else 
{ 
    return -1; 
} 
} 

,它似乎在SIGSEGV错误不见了,现在我离开有些东西没有初始化。所以,谢谢你,彼得你是对的,现在我要解决这个问题了。

好了,所以这是很明显的代码量小,但为了节省时间,有点理智的:所有的代码,请访问:

GitHub Repo

+0

什么是'&mId'的价值?看起来问题在于你将一个错误的地址传递给'glGenTextures()'。如果你的'this'指针搞乱了,那么mId可能就是一个随机的内存。 – user1118321

+0

是否确定在打开任何API调用之前打开了OpenGL上下文? – PeterT

+0

即时通讯对不起,但你打开OpenGL上下文是什么意思?纹理文件是由我的小组的另一个成员编写的,所以我不完全确定它是100%,但我似乎是唯一有问题的文档... –

回答

2

看你的代码,我所以以后可以说在执行代码之前你可能没有初始化你的OpenGL上下文。

在打电话给OpenGL之前,您需要致电Game::onInit(),这也称为RenderEngine::initGraphics()。你现在不这样做。您目前main()->Game ctor (calls rendering engine ctor but that ctor doesn't init SDL and OpenGL)->Entity ctor->load texture

详情看OpenGL Wiki FAQ

+0

ahhh我明白了!谢谢,我会尝试并报告 –