Question

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. If you need anymore info I will be watching this closely.

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::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

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);
}

after reading Peter's suggestion I have changed my main.cpp file to:

#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. 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

Was it helpful?

Solution

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.

You need to call your Game::onInit() which also calls RenderEngine::initGraphics() before making any calls to OpenGL. 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

For details look at the OpenGL Wiki FAQ

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top