简体   繁体   English

使用openGL和SDL在Linux上使用c ++时出现SIGSEGV错误

[英]SIGSEGV error while using c++ on linux with openGL and SDL

Myself and a few other guys are taking a crack at building a simple side scroller type game. 我和其他一些人正在制作一个简单的侧卷轴式游戏。 However, I can not get a hold of them to help answer my question so I put it to you, the following code leaves me with a SIGSEGV error in the notated place... if anyone can tell me why, I would really appreciate it. 但是,我无法抓住他们来帮助回答我的问题,所以我把它给你,以下代码在标记的地方留下了SIGSEGV错误...如果有人能告诉我原因,我会非常感激。 If you need anymore info I will be watching this closely. 如果您需要更多信息,我会密切关注这一点。

Main.cpp Main.cpp的

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

leads to: 导致:

Entity.cpp Entity.cpp

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

    mPosition2D = position;

    mPassable = passable;
}

leads to: 导致:

Textures.cpp 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 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);
}

after reading Peter's suggestion I have changed my main.cpp file to: 在阅读Peter的建议后,我将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;
}
}

and it would seem the SIGSEGV error is gone and I'm now left with something not initializing. 似乎SIGSEGV错误消失了,我现在还没有初始化。 So thank you peter you were correct now I'm off to solve this issue. 所以,谢谢你,彼得说你说得对,我现在就解决了这个问题。

ok so this is obviously a small amount of the code but in order to save time and a bit of sanity: all the code is available at: 好的,所以这显然是一小部分代码,但为了节省时间和一点理智:所有代码都可以在:

GitHub Repo GitHub回购

So after looking at your code I can say that it's probably that you have not initialized you OpenGL context before executing that code. 因此,在查看您的代码后,我可以说,在执行该代码之前,您可能尚未初始化OpenGL上下文。

You need to call your Game::onInit() which also calls RenderEngine::initGraphics() before making any calls to OpenGL. 在调用OpenGL之前,你需要调用你的Game::onInit() ,它也会调用RenderEngine::initGraphics() Which you currently don't do. 你目前不做的。 You currently do main()->Game ctor (calls rendering engine ctor but that ctor doesn't init SDL and OpenGL)->Entity ctor->load texture 你现在做main()->Game ctor (calls rendering engine ctor but that ctor doesn't init SDL and OpenGL)->Entity ctor->load texture

For details look at the OpenGL Wiki FAQ 有关详细信息,请参阅OpenGL Wiki常见问题解答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM