无法加载 .bmp SDL2 IMG_LoadTexture

Can't load .bmp SDL2 IMG_LoadTexture

本文关键字:IMG LoadTexture SDL2 bmp 加载      更新时间:2023-10-16

所以我一直在学习本教程,一切都很顺利,直到我遇到一个问题,即我无法加载.bmp。

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <iostream>
#include <SDL2/SDL_main.h>
using namespace std;

int main(int argc, char* argv[]) {
bool quit = false;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window;
window = SDL_CreateWindow("window", 100, 100, 1280, 720, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if(window == NULL){
    cout << "Disn't work, here is why: " << SDL_GetError()<< endl;
    return 0;
}

SDL_Renderer* renderer = NULL;
renderer= SDL_CreateRenderer(window, -1 ,SDL_RENDERER_ACCELERATED);
SDL_Event* mainEvent = new SDL_Event();
SDL_Texture* grass_image = NULL;
grass_image = IMG_LoadTexture(renderer, "grass.bmp");
SDL_Rect grass_rect;
grass_rect.x = 10;
grass_rect.y = 50;
grass_rect.w = 250;
grass_rect.h = 250;
while(!quit && mainEvent->type != SDL_QUIT){
    SDL_PollEvent(mainEvent);
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, grass_image, NULL, &grass_rect);
    SDL_RenderPresent(renderer);
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete mainEvent;
return 0;
}

当我试图编译代码(在code::Blocks中)时,它会给我的错误

对"IMG_LoadTexture"的未定义引用

嗯,我尝试将IMG_LoadTexture(renderer, "grass.bmp");更改为IMG_LoadTexture(renderer, "/the/full/path/of/grass.bmp");,但这也不起作用。同样的错误。我是不是写错了什么,或者漏了什么地方?此外,grass.bmp与main.cpp(上面的代码)位于同一文件夹中。

这里的错误表明您已经声明了一个函数,但尚未定义它-未定义的引用。

发生这种情况很可能是因为您忘记链接函数IMG_LoadTexture所属的库SDL_image。