sdl_image 找不到我的 jpg 图像;无法加载

发布于 2024-10-18 21:36:49 字数 9848 浏览 7 评论 0原文

我目前正在使用 Code::Blocks IDE、c++、opengl 和 SDL 来制作游戏 - 如果您愿意的话,我遇到了严重的障碍。我正在使用 SDL_image 加载这些 jpg 图像作为纹理,但它们就是无法加载。目前,我已经尝试将它们放置在项目中的每个文件夹中,甚至是那些被认为没有区别的地方。我尝试通过双击可执行文件并从命令行运行可执行文件,从 IDE 运行调试版本和发布版本。它们都会产生相同的错误:无法加载图像:“我的图像的路径”。我尝试过使用完整路径,我尝试过使用相对路径,我几乎尝试过一切。我尝试过各种图像格式,但没有成功。其他有用的信息可能是:我正在使用 ubuntu 11.04,我正在使用 GIMP 来创建图像,这是我的完整代码(我确信其中大部分都是无关紧要的,但我只是将其全部放下-case):

#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <Cg/cg.h>// NEW: Cg Header
#include <Cg/cgGL.h>// NEW: Cg OpenGL Specific Header
#include <iostream>
#include <math.h>
#include <string>
#include "../include/Camera.h"

#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480

// Keydown booleans
bool key[321];
Uint8 *keystate;
float fogDensity = 0.02f;
static float fog_color[] = { 0.8f, 0.8f, 0.8f, 1.0f };

// Process pending events
bool events()
{
    SDL_Event event;

    if( !SDL_PollEvent(&event) ){
        return true;
    }

    switch( event.type )
    {
        case SDL_KEYDOWN : key[ event.key.keysym.sym ]=true;   break;
        case SDL_KEYUP   : key[ event.key.keysym.sym ]=false;   break;
        case SDL_QUIT    : return false; break; //one-per-keypress
    }

    keystate = SDL_GetKeyState(NULL); //continuous
    return true;
}

// Initialze OpenGL perspective matrix
void setup(int width, int height)
{
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glEnable( GL_DEPTH_TEST );
   gluPerspective( 45, (float)width/height, 0.1, 100 );
   glMatrixMode( GL_MODELVIEW );

}

Camera* cam = new Camera();
GLuint rocktexture;         // This is a handle to our texture object
GLuint earthtexture;


//add some default materials here
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 0.0f, 1.0f, 0.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

void MouseUpdate() {
    int x, y;
    SDL_GetMouseState(&x,&y);
    SDL_WarpMouse(WINDOW_WIDTH/2,WINDOW_HEIGHT/2);

    cam->yaw+=(x-WINDOW_WIDTH/2)*0.2;
    cam->pitch+=(y-WINDOW_HEIGHT/2)*0.2;

    if(cam->pitch<-90) {
    cam->pitch=-90;
    }

    if(cam->pitch>90) {
    cam->pitch=90;
    }
}


static void display(void)
{
    while(events())
    {
        MouseUpdate();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();
        glRotatef(cam->pitch,1.0,0.0,0.0);  //rotate our camera on the x-axis (left and right)
        glRotatef(cam->yaw,0.0,1.0,0.0);  //rotate our camera on the y-axis (up and down)
        glTranslated(cam->camx,cam->camy,cam->camz); //translate the screen to the position of our camera

        glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
        glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);

        //glColor4f(1,1,1,0.5);
        glPushMatrix();
        glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
        glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
        glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
        //glDisable(GL_CULL_FACE);
        glBindTexture(GL_TEXTURE_2D, rocktexture);
        glBegin(GL_TRIANGLE_FAN);
        glTexCoord2i(0,0);glVertex3f(-32,0,32);
        glTexCoord2i(1,0);glVertex3f(32,0,32);
        glTexCoord2i(1,1);glVertex3f(32,0,-32);
        glTexCoord2i(0,1);glVertex3f(-32,0,-32);
        glEnd();
        //glEnable(GL_CULL_FACE);
        glPopMatrix();
        /*glPushMatrix();
        GLUquadric* earth = gluNewQuadric();
    gluQuadricTexture(earth, true);
    gluQuadricDrawStyle(earth, GLU_FILL);
    gluQuadricNormals(earth, GLU_SMOOTH);
    gluQuadricOrientation(earth, GLU_OUTSIDE);
    gluSphere(earth, 1, 16, 16);
    gluDeleteQuadric(earth);
        glPopMatrix();*/
        glFlush();

        SDL_GL_SwapBuffers();

        if (key[SDLK_ESCAPE] || key[SDLK_q])
        {
            std::cout<<"Game Ended."<<std::endl;
            exit(0);
        }

        if (keystate[SDLK_w])
        {
            cam->camx-=sin(cam->yaw*M_PI/180)/4;
            cam->camz+=cos(cam->yaw*M_PI/180)/4;
        }

        if (keystate[SDLK_s])
        {
            cam->camx+=sin(cam->yaw*M_PI/180)/4;
            cam->camz-=cos(cam->yaw*M_PI/180)/4;
        }

        if (keystate[SDLK_a])
        {
            cam->camx+=cos(cam->yaw*M_PI/180)/4;
            cam->camz+=sin(cam->yaw*M_PI/180)/4;
        }

        if (keystate[SDLK_d])
        {
            cam->camx-=cos(cam->yaw*M_PI/180)/4;
            cam->camz-=sin(cam->yaw*M_PI/180)/4;
        }
    }
}

// Load the OpenGL texture
GLuint loadImage(std::string path, GLuint tx,SDL_Surface* sur)
{
    GLenum texture_format;
    GLint  nOfColors;
    if ((sur = SDL_LoadBMP(path.c_str())))
    {
        // Check that the image's width is a power of 2
        if ((sur->w & (sur->w - 1)) != 0)
    {
        printf("warning: image's width is not a power of 2\n");
    }
        // Also check if the height is a power of 2
    if ((sur->h & (sur->h - 1)) != 0)
    {
        printf("warning: image's height is not a power of 2\n");
    }
        // get the number of channels in the SDL surface
        nOfColors = sur->format->BytesPerPixel;
        if (nOfColors == 4)     // contains an alpha channel
        {
            if (sur->format->Rmask == 0x000000ff)
            {
                texture_format = GL_RGBA;
            }
            else
            {
                texture_format = GL_BGRA;
            }

        }
        else if (nOfColors == 3)     // no alpha channel
        {
            if (sur->format->Rmask == 0x000000ff)
            {
                texture_format = GL_RGB;
            }
            else
            {
                texture_format = GL_BGR;
            }
        }
        else
        {
            printf("warning: the image is not truecolor..  this will probably break\n");
            // this error should not go unhandled
        }


        // Have OpenGL generate a texture object handle for us
        glGenTextures( 1, &tx );
        // Bind the texture object
    glBindTexture( GL_TEXTURE_2D, tx );
        // Set the texture's stretching properties
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        // Edit the texture object's image data using the information SDL_Surface gives us
    glTexImage2D( GL_TEXTURE_2D, 0, 3, sur->w, sur->h, 0, GL_BGR, GL_UNSIGNED_BYTE, sur->pixels);
    //build mip-maps
    gluBuild2DMipmaps(tx, 3, sur->w, sur->h, texture_format, GL_UNSIGNED_BYTE, sur->pixels);
    }
    else
    {
        printf("SDL could not load image: %s\n", SDL_GetError());
            SDL_Quit();
    return 1;
    }
    // Free the SDL_Surface only if it was successfully created
    if (sur)
    {
    SDL_FreeSurface(sur);
    }
    delete &texture_format;
    delete &nOfColors;
    return tx;
}

/* Program entry point */

int main(int argc, char *argv[])
{
    cam->camz=-5;
    cam->camy=-1;
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
    {
        std::cout<<"Unable to initialize SDL: %s\n"<<SDL_GetError()<<std::endl;
        return 1;
    }
    const SDL_VideoInfo* info = SDL_GetVideoInfo();
    int vidFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
    if (info->hw_available) {vidFlags |= SDL_HWSURFACE;}
    else {vidFlags |= SDL_SWSURFACE;}
    //int bpp = info->vfmt->BitsPerPixel;
    SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, vidFlags);
    SDL_WM_SetCaption( "Treason", NULL);

    SDL_WarpMouse(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
    gluLookAt(cam->camx,cam->camy,cam->camz,0,0,0,0,1,0);

    glClearColor(0, 0, 0, 0);//background color white

    float lmodel_ambient[] = { 0.4f, 0.4f, 0.4f, 1.0f };
    float local_view[] = { 0.0f };

    glEnable(GL_TEXTURE_2D);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
    glLightModelf(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
    //glEnable(GL_BLEND);
    //glBlendFunc(GL_SRC_ALPHA, GL_ONE);

    glEnable(GL_DITHER);

    //glHint(GL_PHONG_HINT_WIN, GL_NICEST);
    glShadeModel(GL_SMOOTH);//GL_PHONG_WIN


    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

/*    glEnable(GL_FOG);
    glHint(GL_FOG_HINT, GL_NICEST);
    glEnable(GL_FOG);
    glFogi(GL_FOG_MODE, GL_EXP);
    glFogf(GL_FOG_DENSITY, fogDensity);
    glFogfv(GL_FOG_COLOR, fog_color);*/


    // glEnable(GL_STENCIL_TEST);
    //glStencilFunc(GL_KEEP, GL_KEEP, GL_INCR);

    SDL_Surface *rocktexsur;
    rocktexture = loadImage("rock.jpg", rocktexture, rocktexsur);
    SDL_Surface *earthtexsur;
    earthtexture = loadImage("earth.jpg", earthtexture, earthtexsur);


    setup(WINDOW_WIDTH, WINDOW_HEIGHT);
    display();

    glDeleteTextures(1, &rocktexture);
    glDeleteTextures(1, &earthtexture);

    return 0;
}

因此,如果您发现任何内容或知道任何原因会返回“SDL 无法加载图像”错误,请回复!在解决此问题之前我无法继续我的项目,因为它会产生段错误。

I am currently using the Code::Blocks IDE, c++, opengl and SDL to make a game- and I have hit a severe block in road, if you will. I am using SDL_image to load these jpg images for the textures, but they just will not load. Currently, I have tried placing them in every folder in the project, even places where it would supposedly make no difference. I have tried running both the debug and release versions from the IDE, by double-clicking the executable, and by running the executable from the command line. They all produce the same error: Could not load image: "the path to my image". I have tried using full paths, I have tried using relative paths, I have tried just about everything. I have tried various image formats, to no success. Other useful information may be: I am using ubuntu 11.04, I am using GIMP to create the images, and this is my code in complete (I'm sure most of it is irrelevant, but I am just putting it all down just-in-case):

#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <Cg/cg.h>// NEW: Cg Header
#include <Cg/cgGL.h>// NEW: Cg OpenGL Specific Header
#include <iostream>
#include <math.h>
#include <string>
#include "../include/Camera.h"

#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480

// Keydown booleans
bool key[321];
Uint8 *keystate;
float fogDensity = 0.02f;
static float fog_color[] = { 0.8f, 0.8f, 0.8f, 1.0f };

// Process pending events
bool events()
{
    SDL_Event event;

    if( !SDL_PollEvent(&event) ){
        return true;
    }

    switch( event.type )
    {
        case SDL_KEYDOWN : key[ event.key.keysym.sym ]=true;   break;
        case SDL_KEYUP   : key[ event.key.keysym.sym ]=false;   break;
        case SDL_QUIT    : return false; break; //one-per-keypress
    }

    keystate = SDL_GetKeyState(NULL); //continuous
    return true;
}

// Initialze OpenGL perspective matrix
void setup(int width, int height)
{
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glEnable( GL_DEPTH_TEST );
   gluPerspective( 45, (float)width/height, 0.1, 100 );
   glMatrixMode( GL_MODELVIEW );

}

Camera* cam = new Camera();
GLuint rocktexture;         // This is a handle to our texture object
GLuint earthtexture;


//add some default materials here
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 0.0f, 1.0f, 0.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

void MouseUpdate() {
    int x, y;
    SDL_GetMouseState(&x,&y);
    SDL_WarpMouse(WINDOW_WIDTH/2,WINDOW_HEIGHT/2);

    cam->yaw+=(x-WINDOW_WIDTH/2)*0.2;
    cam->pitch+=(y-WINDOW_HEIGHT/2)*0.2;

    if(cam->pitch<-90) {
    cam->pitch=-90;
    }

    if(cam->pitch>90) {
    cam->pitch=90;
    }
}


static void display(void)
{
    while(events())
    {
        MouseUpdate();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();
        glRotatef(cam->pitch,1.0,0.0,0.0);  //rotate our camera on the x-axis (left and right)
        glRotatef(cam->yaw,0.0,1.0,0.0);  //rotate our camera on the y-axis (up and down)
        glTranslated(cam->camx,cam->camy,cam->camz); //translate the screen to the position of our camera

        glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
        glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
        glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);

        //glColor4f(1,1,1,0.5);
        glPushMatrix();
        glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
        glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
        glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
        //glDisable(GL_CULL_FACE);
        glBindTexture(GL_TEXTURE_2D, rocktexture);
        glBegin(GL_TRIANGLE_FAN);
        glTexCoord2i(0,0);glVertex3f(-32,0,32);
        glTexCoord2i(1,0);glVertex3f(32,0,32);
        glTexCoord2i(1,1);glVertex3f(32,0,-32);
        glTexCoord2i(0,1);glVertex3f(-32,0,-32);
        glEnd();
        //glEnable(GL_CULL_FACE);
        glPopMatrix();
        /*glPushMatrix();
        GLUquadric* earth = gluNewQuadric();
    gluQuadricTexture(earth, true);
    gluQuadricDrawStyle(earth, GLU_FILL);
    gluQuadricNormals(earth, GLU_SMOOTH);
    gluQuadricOrientation(earth, GLU_OUTSIDE);
    gluSphere(earth, 1, 16, 16);
    gluDeleteQuadric(earth);
        glPopMatrix();*/
        glFlush();

        SDL_GL_SwapBuffers();

        if (key[SDLK_ESCAPE] || key[SDLK_q])
        {
            std::cout<<"Game Ended."<<std::endl;
            exit(0);
        }

        if (keystate[SDLK_w])
        {
            cam->camx-=sin(cam->yaw*M_PI/180)/4;
            cam->camz+=cos(cam->yaw*M_PI/180)/4;
        }

        if (keystate[SDLK_s])
        {
            cam->camx+=sin(cam->yaw*M_PI/180)/4;
            cam->camz-=cos(cam->yaw*M_PI/180)/4;
        }

        if (keystate[SDLK_a])
        {
            cam->camx+=cos(cam->yaw*M_PI/180)/4;
            cam->camz+=sin(cam->yaw*M_PI/180)/4;
        }

        if (keystate[SDLK_d])
        {
            cam->camx-=cos(cam->yaw*M_PI/180)/4;
            cam->camz-=sin(cam->yaw*M_PI/180)/4;
        }
    }
}

// Load the OpenGL texture
GLuint loadImage(std::string path, GLuint tx,SDL_Surface* sur)
{
    GLenum texture_format;
    GLint  nOfColors;
    if ((sur = SDL_LoadBMP(path.c_str())))
    {
        // Check that the image's width is a power of 2
        if ((sur->w & (sur->w - 1)) != 0)
    {
        printf("warning: image's width is not a power of 2\n");
    }
        // Also check if the height is a power of 2
    if ((sur->h & (sur->h - 1)) != 0)
    {
        printf("warning: image's height is not a power of 2\n");
    }
        // get the number of channels in the SDL surface
        nOfColors = sur->format->BytesPerPixel;
        if (nOfColors == 4)     // contains an alpha channel
        {
            if (sur->format->Rmask == 0x000000ff)
            {
                texture_format = GL_RGBA;
            }
            else
            {
                texture_format = GL_BGRA;
            }

        }
        else if (nOfColors == 3)     // no alpha channel
        {
            if (sur->format->Rmask == 0x000000ff)
            {
                texture_format = GL_RGB;
            }
            else
            {
                texture_format = GL_BGR;
            }
        }
        else
        {
            printf("warning: the image is not truecolor..  this will probably break\n");
            // this error should not go unhandled
        }


        // Have OpenGL generate a texture object handle for us
        glGenTextures( 1, &tx );
        // Bind the texture object
    glBindTexture( GL_TEXTURE_2D, tx );
        // Set the texture's stretching properties
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        // Edit the texture object's image data using the information SDL_Surface gives us
    glTexImage2D( GL_TEXTURE_2D, 0, 3, sur->w, sur->h, 0, GL_BGR, GL_UNSIGNED_BYTE, sur->pixels);
    //build mip-maps
    gluBuild2DMipmaps(tx, 3, sur->w, sur->h, texture_format, GL_UNSIGNED_BYTE, sur->pixels);
    }
    else
    {
        printf("SDL could not load image: %s\n", SDL_GetError());
            SDL_Quit();
    return 1;
    }
    // Free the SDL_Surface only if it was successfully created
    if (sur)
    {
    SDL_FreeSurface(sur);
    }
    delete &texture_format;
    delete &nOfColors;
    return tx;
}

/* Program entry point */

int main(int argc, char *argv[])
{
    cam->camz=-5;
    cam->camy=-1;
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
    {
        std::cout<<"Unable to initialize SDL: %s\n"<<SDL_GetError()<<std::endl;
        return 1;
    }
    const SDL_VideoInfo* info = SDL_GetVideoInfo();
    int vidFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
    if (info->hw_available) {vidFlags |= SDL_HWSURFACE;}
    else {vidFlags |= SDL_SWSURFACE;}
    //int bpp = info->vfmt->BitsPerPixel;
    SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, vidFlags);
    SDL_WM_SetCaption( "Treason", NULL);

    SDL_WarpMouse(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
    gluLookAt(cam->camx,cam->camy,cam->camz,0,0,0,0,1,0);

    glClearColor(0, 0, 0, 0);//background color white

    float lmodel_ambient[] = { 0.4f, 0.4f, 0.4f, 1.0f };
    float local_view[] = { 0.0f };

    glEnable(GL_TEXTURE_2D);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
    glLightModelf(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
    //glEnable(GL_BLEND);
    //glBlendFunc(GL_SRC_ALPHA, GL_ONE);

    glEnable(GL_DITHER);

    //glHint(GL_PHONG_HINT_WIN, GL_NICEST);
    glShadeModel(GL_SMOOTH);//GL_PHONG_WIN


    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

/*    glEnable(GL_FOG);
    glHint(GL_FOG_HINT, GL_NICEST);
    glEnable(GL_FOG);
    glFogi(GL_FOG_MODE, GL_EXP);
    glFogf(GL_FOG_DENSITY, fogDensity);
    glFogfv(GL_FOG_COLOR, fog_color);*/


    // glEnable(GL_STENCIL_TEST);
    //glStencilFunc(GL_KEEP, GL_KEEP, GL_INCR);

    SDL_Surface *rocktexsur;
    rocktexture = loadImage("rock.jpg", rocktexture, rocktexsur);
    SDL_Surface *earthtexsur;
    earthtexture = loadImage("earth.jpg", earthtexture, earthtexsur);


    setup(WINDOW_WIDTH, WINDOW_HEIGHT);
    display();

    glDeleteTextures(1, &rocktexture);
    glDeleteTextures(1, &earthtexture);

    return 0;
}

So if you find anything or know any reason why it would return an "SDL could not load image" Error, please respond! I can't continue on my project until this is resolved because it produces a segfault.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心头的小情儿 2024-10-25 21:36:49

SDL 本身只能加载 BMP 图像,如果使用函数 SDL_LoadBMP() 加载除 BMP 之外的任何内容,它将返回错误。为了加载 JPEG,您需要使用补充库 SDL_image,该库位于 http://www .libsdl.org/projects/SDL_image/ 。正如 JJG 回答的那样,您必须使用的函数原型是:

SDL_Surface *IMG_Load( const char* );

而不是 SDL_LoadBMP()。

SDL by itself can only load BMP images, and it will return an error if you load anything but a BMP in using the function SDL_LoadBMP(). In order to load JPEGs, you need to use the supplemental library SDL_image, which is available at http://www.libsdl.org/projects/SDL_image/ . As JJG answered, the prototype for the function you have to use is:

SDL_Surface *IMG_Load( const char* );

rather than SDL_LoadBMP().

醉生梦死 2024-10-25 21:36:49

不是 SDL 或 C++ 专家。但我只是花了一分钟查看我不久前制作的小程序。我用过:

 some_image = IMG_Load( "someimage.jpg" );

Not an expert on SDL or C++. But I just spent a minute looking over my tiny program I made a while back. I used:

 some_image = IMG_Load( "someimage.jpg" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文