Search code examples
c++imagetexturessdlsdl-2

C++ SDL texture Error Microsoft Visual Studio 2013


I'm trying to run this code but it keeps giving me an error. I copyed SDL2_image.lib in the debug folder but in vain. I'm at the beggining of programming so please be patient.

Errors: Error 1 error C3861: 'IMG_LoadTexture': identifier not found
Error 2 IntelliSense: identifier "IMG_LoadTexture" is undefined

#include<SDL/SDL.h>
#include<iostream>
using namespace std;


int main(int argc, char** argv)
{
bool quit = false;

//*Initializing Window;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = NULL;
window = SDL_CreateWindow("Game Test", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

//*If game Crushes;
if (window == NULL)
{
    cout << "The game window is not working";
}

//*Creating Update Function
SDL_Renderer* render = NULL;
render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event* mainEvent = new SDL_Event();
//*End Update Function

//*Adding Textures;
SDL_Texture* grass_image = NULL;
grass_image = IMG_LoadTexture(render, "grass.bmp");

//*Creating a Sprite;
SDL_Rect grass_rect;
grass_rect.x = 10;
grass_rect.y = 50;
grass_rect.w = 250;
grass_rect.h = 250;



//*Content Of the Window;
while (!quit && mainEvent->type!=SDL_QUIT)
{
    SDL_PollEvent(mainEvent);
    SDL_RenderClear(render);
    SDL_RenderCopy(render, grass_image, NULL, &grass_rect);
    SDL_RenderPresent(render);
}
//*End Window Content

//*Memory Cleaning
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
delete mainEvent;
//*End Memory Cleaning

return 0;

}

Solution

  • You are missing to include the header that contains the declaration of IMG_LoadTexture():

    #include <SDL/SDL_image.h>
    

    It's a separate extension library for SDL, and besides including that header, you'll also need to link that library with your project.