Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ DevIL function ilLoadImage - program exit, access violation

Tags:

c++

devil

I've got a path to my file defined this way:

const char* GROUND_TEXTURE_FILE = "objects/textures/grass.jpg";

And here is the function, which I use to load image:

bool loadTexImage2D(const string &fileName, GLenum target) {
    ...
    // this will load image data to the currently bound image
    // at first, we must convert fileName, for ascii, this method is fine?
    wstring file(fileName.begin(), fileName.end());

    if(ilLoadImage(file.c_str()) == IL_FALSE) { //here the program falls

What's wrong in my code? Why the program falls when ilLoadImage is called? I think, that file.c_str() should work fine as a wchar_t * type or not? Thanks for answer :)

like image 746
ketysek Avatar asked Nov 08 '22 15:11

ketysek


1 Answers

As the author's said, you can do pretty anything without initializing the lib :D

#include <iostream>
#include <IL/il.h>

int main ()
{
    std::string filename = "objects/textures/grass.jpg";

    ilInit();

    if (!ilLoadImage(filename.c_str())) {
        std::cout << ilGetError() << std::endl;
        return 1;
    }

    std::cout << ilGetInteger(IL_IMAGE_WIDTH) << std::endl;
    std::cout << ilGetInteger(IL_IMAGE_HEIGHT) << std::endl;

    return 0;
}

build:

g++ -Wall -pedantic --std=c++11 -g -o app main.cpp -lIL
like image 181
Hugo do Carmo Avatar answered Nov 23 '22 08:11

Hugo do Carmo