0

我和其他几个人正在尝试构建一个简单的横向卷轴类型的游戏。但是,我无法让他们帮助回答我的问题,所以我把它给你,下面的代码在标记的地方给我留下了一个 SIGSEGV 错误......如果有人能告诉我为什么,我真的很感激. 如果您需要更多信息,我将密切关注。

主文件

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

导致:

实体.cpp

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

    mPosition2D = position;

    mPassable = passable;
}

导致:

纹理.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 !!!这里有错误!!!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 回购

4

1 回答 1

2

因此,在查看您的代码之后,我可以说您可能在执行该代码之前没有初始化您的 OpenGL 上下文。

在对 OpenGL 进行任何调用之前,您需要调用Game::onInit()which 也调用。RenderEngine::initGraphics()你目前不这样做。你目前做main()->Game ctor (calls rendering engine ctor but that ctor doesn't init SDL and OpenGL)->Entity ctor->load texture

有关详细信息,请查看OpenGL Wiki 常见问题解答

于 2012-01-24T06:29:06.720 回答