The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
load bmp returns NULL
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
HELLO EVERYONE i am very new to sdl so i have this problam,i cant load an image(sdl loadbmp returns NULL)
and i dont know what is the problam,i hope that you can halp me.
#include<SDL.h>
SDL_Window* g_pWindow = 0;
SDL_Renderer* g_pRenderer = 0;
SDL_Texture* m_pTexture;
SDL_Texture*fuck;
int main(int argc, char* args[])
{
// initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// if succeeded create our window
g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN);
SDL_Surface* pTempSurface = SDL_LoadBMP("Debug\GAME\Penguins.BMP");
fuck = SDL_CreateTextureFromSurface(g_pRenderer,pTempSurface);
fuck=m_pTexture;
SDL_FreeSurface(pTempSurface);
// if the window creation succeeded create our renderer
if(g_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
}
}
else
{
return 1; // sdl could not initialize
}
// everything succeeded lets draw the window
// set to black // This function expects Red, Green, Blue and
// Alpha as color values
SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
// clear the window to black
SDL_RenderClear(g_pRenderer);
SDL_RenderCopy(g_pRenderer, m_pTexture,NULL,
NULL);
SDL_RenderPresent(g_pRenderer);
// set a delay before quitting
SDL_Delay(5000);
// clean up SDL
SDL_Quit();
return 0;
}
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
cant any one help me??
mr_tawan


Joined: 13 Jan 2014
Posts: 161
Code:
SDL_Surface* pTempSurface = SDL_LoadBMP("Debug\GAME\Penguins.BMP");


Try changing every \ to \\ (two backslashes for each backslash), or to / (forward slash).

Also checks if the file presents in that path.
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
every thing that you said i did at least 2 times befure you put you commnt,
can you maby copy my code one your systeam and set a bpm pic ove you one to see if this problam is Universal?
mr_tawan


Joined: 13 Jan 2014
Posts: 161
Do you mind sharing the file Penguins.BMP ? It's probably the image file (and probably not).
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
i dont know how to upload the image but i tired it with multipile bmp{some of wich i crated my salf} and the result is the same
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
the ful dirctury is-C:\Users\David\Desktop\sdl\Debug
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
did you try my code by your self mr tawan ?,IS this problam only mine?
Naith


Joined: 03 Jul 2014
Posts: 158
@DAVIDlavi20091997, I found at least 2 errors in your code.

1. You're creating the renderer after you're creating the texture, which means that you're (while creating the texture) specifying a renderer that is NULL.

Code:

fuck = SDL_CreateTextureFromSurface(g_pRenderer, pTempSurface);

...

// if the window creation succeeded create our renderer
if(g_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
}



2. You're first creating the texture (in your case 'fuck') and then set that texture to be equal to your other texture (in your case 'm_pTexture'). Your other texture ('m_pTexture') is NULL (or contains garbage since you haven't NULL'ed it at the top of your code) which means that your first texture ('fuck') will be NULL / garbage aswell.

Code:

fuck = SDL_CreateTextureFromSurface(g_pRenderer, pTempSurface);
fuck = m_pTexture;



Fixed code:
In my case, in my Debug directory, I have a directory called Data and in that directory I have a directory called Textures which is where my Image.bmp is being located. Note that I'm using '/' instead of '\' in the file path.
Also note that I don't specify 'Debug/....' in the file path since SDL already know it's supposed to check in the same directory as the executable file is in.

Code:

#include <SDL.h>

SDL_Window* g_pWindow = NULL;
SDL_Renderer* g_pRenderer = NULL;

SDL_Texture* g_pTexture = NULL;

int main(int argc, char* args[])
{
   // Initialize SDL
   if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
   {
      // If SDL was succesfully initialized, create the SDL window
      g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);

      // If the SDL window was succesfully created
      if(g_pWindow)
      {
         // Create the renderer
         g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);

         // If the renderer was succesfully created
         if(g_pRenderer)
         {
            // Set the render draw color
            SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
            
            // Create the temp surface
            SDL_Surface* pTempSurface = SDL_LoadBMP("Data/Textures/Image.bmp");

            // If the temp surface was successfully created
            if(pTempSurface)
            {
               // Create the texture
               g_pTexture = SDL_CreateTextureFromSurface(g_pRenderer, pTempSurface);

               // Destroy the temp surface
               SDL_FreeSurface(pTempSurface);
            }
         }
      }
   }

   // If SDL for some reason couldn't initialize
   else
      return 1;



   // Clear the current render target
   SDL_RenderClear(g_pRenderer);
   
   // Render the texture
   if(g_pTexture)
      SDL_RenderCopy(g_pRenderer, g_pTexture, NULL, NULL);
   
   // Update the screen
   SDL_RenderPresent(g_pRenderer);

   // Wait 2 seconds and then exit the program
   SDL_Delay(2000);
   


   // Destroy the texture
   if(g_pTexture)
      SDL_DestroyTexture(g_pTexture);

   // Quit SDL
   SDL_Quit();
   
   return 0;
}

DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
MR NAITH FIRST OF ALL TANK YOU FOR SEEING BY NOOBIE PROGRAMMING MISTAKES AS YOU CAN SEE IM NEW TO PROGRAMMING IN GENERAL

but i did everything that you said to do but the result is the same as before, the image is not showing and the screen is black

is this maybe an visual studio mistake ?
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
im posting the new code

#include <SDL.h>

SDL_Window* g_pWindow = NULL;
SDL_Renderer* g_pRenderer = NULL;

SDL_Texture* g_pTexture = NULL;

int main(int argc, char* args[])
{
// Initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// If SDL was succesfully initialized, create the SDL window
g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);

// If the SDL window was succesfully created
if(g_pWindow)
{
// Create the renderer
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);

// If the renderer was succesfully created
if(!g_pRenderer)
{
// Set the render draw color
SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);

// Create the temp surface
SDL_Surface* pTempSurface = SDL_LoadBMP("david/a/first.bmp");

// If the temp surface was successfully created
if(pTempSurface)
{
// Create the texture
g_pTexture = SDL_CreateTextureFromSurface(g_pRenderer, pTempSurface);

// Destroy the temp surface
SDL_FreeSurface(pTempSurface);
}
}
}
}

// If SDL for some reason couldn't initialize
else
return 1;



// Clear the current render target
SDL_RenderClear(g_pRenderer);

// Render the texture
if(g_pTexture)
SDL_RenderCopy(g_pRenderer, g_pTexture, NULL, NULL);

// Update the screen
SDL_RenderPresent(g_pRenderer);

// Wait 2 seconds and then exit the program
SDL_Delay(2000);



// Destroy the texture
if(!g_pTexture)
SDL_DestroyTexture(g_pTexture);

// Quit SDL
SDL_Quit();

return 0;
}
mr_tawan


Joined: 13 Jan 2014
Posts: 161
You might want to check the 'Working Directory' settings, under Debugging section of Project Properties in Visual Studio. I think the SDL_LoadBMP() looks for file under that path.

In the case that it a variable/macro like $(ProjectDir) (which is the default), you could press <edit> and press the Macros button. Look for that variable in the list and the value is on the right side of the table.

Sorry I don't have the screenshot. I hope you can find what I mean Smile.

PS. please don't use all cap text, as it means you're shouting. You don't have to shout, we all can hear you Smile.
Naith


Joined: 03 Jul 2014
Posts: 158
@DAVIDlavi20091997, sorry but you haven't followed the code that I wrote in my last message.
I've tryed running the program (after I've fixed the errors) and have a file in the same directory as you (David/a/First.bmp) and it works like a charm.

What you wrote different from my code (in my last post) was two of the safe guards that I put in the code. The first one is where the renderer is created.

I wrote this:

Code:

         // If the renderer was successfully created
         if(g_pRenderer)
         {
            // Set the render draw color
            SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
           
            // Create the temp surface
            SDL_Surface* pTempSurface = SDL_LoadBMP("Data/Textures/Image.bmp");

            // If the temp surface was successfully created
            if(pTempSurface)
            {
               // Create the texture
               g_pTexture = SDL_CreateTextureFromSurface(g_pRenderer, pTempSurface);

               // Destroy the temp surface
               SDL_FreeSurface(pTempSurface);
            }


What this basically mean is: If the renderer was succesfully created, set the render draw color and create the temp surface and the texture.

You wrote this:

Code:

         // If the renderer was successfully created
         if(!g_pRenderer)
         {
            // Set the render draw color
            SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
           
            // Create the temp surface
            SDL_Surface* pTempSurface = SDL_LoadBMP("Data/Textures/Image.bmp");

            // If the temp surface was successfully created
            if(pTempSurface)
            {
               // Create the texture
               g_pTexture = SDL_CreateTextureFromSurface(g_pRenderer, pTempSurface);

               // Destroy the temp surface
               SDL_FreeSurface(pTempSurface);
            }


What this basically mean is: If the renderer was NOT succesfully created, set the render draw color and create the temp surface and the texture.
The result of this will be that whenever the SDL Renderer can not be created, the render draw color will be set (and so on...).
The SDL Renderer is usually always successfully created (some error might cause it to not be created). So.. I hope you see the problem here.

The second thing that you wrote different from my code is:

I wrote this:

Code:

   // Destroy the texture
   if(g_pTexture)
      SDL_DestroyTexture(g_pTexture);


What this basically mean is: If g_pTexture is created (ergo, not NULL), destroy it.

You wrote this:

Code:

   // Destroy the texture
   if(!g_pTexture)
      SDL_DestroyTexture(g_pTexture);


What this basically mean is: If g_pTexture is NOT created (ergo, the texture is NULL), destroy it.
The result of this will be: If you create the texture (in your case, with SDL_CreateTextureFromSurface), whenever the program is closing, you'll have a memory leak since the texture will only be destroyed when it is not created.
I hope you see the problem here aswell.

So. Change two things in your code. These are:

if(!g_pRenderer) into --> if(g_pRenderer)
if(!g_pTexture) into --> if(g_pTexture)
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
i just dont know what im doing wrong, i did everything you said multiple times and its still not working,i changed the working dirctury to every posible situation from the folder to debug and even outside the project and its stil dosnet work, i cureected the mistakes changed the bmp and the lpcitaion to every place in the project still...nuthing
Sad
#include <SDL.h>

SDL_Window* g_pWindow = NULL;
SDL_Renderer* g_pRenderer = NULL;

SDL_Texture* g_pTexture = NULL;

int main(int argc, char* args[])
{
// Initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// If SDL was succesfully initialized, create the SDL window
g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);

// If the SDL window was succesfully created
if(g_pWindow)
{
// Create the renderer
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);

// If the renderer was succesfully created
if(g_pRenderer)
{
// Set the render draw color
SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);

// Create the temp surface
SDL_Surface* pTempSurface = SDL_LoadBMP("//asd.bmp");

// If the temp surface was successfully created
if(pTempSurface)
{
// Create the texture
g_pTexture = SDL_CreateTextureFromSurface(g_pRenderer, pTempSurface);

// Destroy the temp surface
SDL_FreeSurface(pTempSurface);
}
}
}
}

// If SDL for some reason couldn't initialize
else
return 1;



// Clear the current render target
SDL_RenderClear(g_pRenderer);

// Render the texture
if(g_pTexture)
SDL_RenderCopy(g_pRenderer, g_pTexture, NULL, NULL);

// Update the screen
SDL_RenderPresent(g_pRenderer);

// Wait 2 seconds and then exit the program
SDL_Delay(2000);



// Destroy the texture
if(g_pTexture)
SDL_DestroyTexture(g_pTexture);

// Quit SDL
SDL_Quit();

return 0;
} Laughing Laughing Laughing Laughing Laughing Surprised Surprised Laughing Laughing Cool Laughing Laughing Surprised Sad Smile Very Happy Very Happy Surprised Surprised Surprised Surprised Surprised Surprised Laughing
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
ps-soory for the shouting and the smaly paces in the end Laughing Laughing Laughing Very Happy
Naith


Joined: 03 Jul 2014
Posts: 158
Really weird.

In your main file, remove all of your code and replace it with the code below.
Do not change anything in the code and do not leave any code left in your main file. Your main file should be completely empty before you're paste'ing in the code below.

Code:

#include <SDL.h>

SDL_Window* g_pWindow = NULL;
SDL_Renderer* g_pRenderer = NULL;

SDL_Texture* g_pTexture = NULL;

int main(int argc, char* args[])
{
   // Initialize SDL
   if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
   {
      // If SDL was successfully initialized, create the SDL window
      g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);

      // If the SDL window was successfully created
      if(g_pWindow)
      {
         // Create the renderer
         g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, SDL_RENDERER_ACCELERATED);

         // If the renderer was successfully created
         if(g_pRenderer)
         {
            // Set the render draw color
            SDL_SetRenderDrawColor(g_pRenderer, 255, 255, 255, SDL_ALPHA_OPAQUE);

            // Create the temp surface
            SDL_Surface* pTempSurface = SDL_LoadBMP("Image.bmp");

            // If the temp surface was successfully created
            if(pTempSurface)
            {
               // Create the texture
               g_pTexture = SDL_CreateTextureFromSurface(g_pRenderer, pTempSurface);

               // Destroy the temp surface
               SDL_FreeSurface(pTempSurface);
            }
         }
      }
   }

   // If SDL for some reason couldn't initialize
   else
      return 1;



   // Clear the current render target
   SDL_RenderClear(g_pRenderer);

   // Render the texture
   if(g_pTexture)
      SDL_RenderCopy(g_pRenderer, g_pTexture, NULL, NULL);

   // Update the screen
   SDL_RenderPresent(g_pRenderer);

   // Wait 2 seconds and then exit the program
   SDL_Delay(2000);



   // Destroy the texture
   if(g_pTexture)
      SDL_DestroyTexture(g_pTexture);

   // Quit SDL
   SDL_Quit();

   return 0;
}


After that I want you to download this image: http://oi60.tinypic.com/2cqms2c.jpg

If you're building the program as debug, put the image directly in your Debug folder (ergo, put the image at the same place as your exe file).
If you're building the program as release, put the image directly in your Release folder (ergo, put the image at the same place as your exe file).

After that take a screenshot of your Debug / Release folder containing the files and show the screenshot to me.

If this will not solve the problem, there's some problem with your Visual Studio project file and you should create a new blank one. In the new blank project file, after you've set it up correctly and linked in the SDL lib's and so on, create a cpp-file named Main.cpp. In that file (which should be empty), paste in the code above and try to compile and run. If that doesn't work, I'm gonna cry.
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
im surry it didnt worked,http://i60.tinypic.com/2zq4qdt_th.jpg
http://s8.tinypic.com/2mwz69f_th.jpg
http://s8.tinypic.com/10nezc7_th.jpg
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
http://s14.postimg.org/t9mtjuinj/New_Bitmap_Image_4.png

http://s14.postimg.org/yv98nwjch/New_Bitmap_Image_2.png

http://s29.postimg.org/y33rmv9x3/New_Bitmap_Image_3.png[/img]
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
im gonne try upload a video in it i start from the top, from including the sdl liblery to v-s and coping the image and your code to it and then i well run it
Naith


Joined: 03 Jul 2014
Posts: 158
The first image is correct.

Make sure that your directory structure looks like this: http://oi60.tinypic.com/2ngydlt.jpg

The first picture is the root directory.
In the second picture, the Image.bmp file is in the same directory as the Main.cpp file. When you're executing the program inside Visual Studio, Visual Studio looks for the image file in the same folder as the Main.cpp file and that's the reason why the image is there.
In the third picture, the Image.bmp file is in the same directory as the exe file and SDL will look for the image file in that directory.
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
http://tinypic.com/player.php?v=2h85tfd%3E&s=8#.U8_Sg_l_vpA

http://tinypic.com/player.php?v=96yesl%3E&s=8#.U8_TVPl_vpA

http://tinypic.com/player.php?v=358thft%3E&s=8#.U8_Tt_l_vpA
mr_tawan


Joined: 13 Jan 2014
Posts: 161
Well it's probably better if you can just zip your whole project, upload that file, and tell us where's your file. Or may be allow some one to remote to your machine using something like teamviewer Smile.
Naith


Joined: 03 Jul 2014
Posts: 158
Yeah, everything is done right and I finally know what the problem is. Now check my last post showing the directory structure and make sure that the Image.bmp file is in two of the directories.
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
http://speedy.sh/QryVp/sdl.zip here is the zip, i copyed the pic every where
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
by the way im using v-s express 2010
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
and i cant give direct access to my computer its just my private stuff and the things of my famlay is on it
Naith


Joined: 03 Jul 2014
Posts: 158
Listen, you can not download the image and change the file extension from jpg to bmp. The file extension get changed to *.jpg when it is uploaded to tinypic. So what you need to do is download the image, open up the image in, for example Paint, and then save the image as Image.bmp and put the saved image in the two directories you've images in.
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
i knowon that befure i just wanted to use your image so i cunverted it but i crated some bmp files and drew them my self {using paind} and copied them to there place,the result is the same no matter
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
om my frikking goood,IT WORKES.........
Naith


Joined: 03 Jul 2014
Posts: 158
And what did you do to make it work?
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
mr naith and mr tawan tanke you very much to help me dill with this problam the problam just was that the i didnt crated the renderr befure the sdl texture and that i didnt copied the image to the main cpp dirctaury, imn very surry for wasting your time on such an abstract and Trivial matter,tanke you very very much Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
no.. the probkam was just crating the texture befure the render i think the image is stored at the .exe dirc...i dont know what happind that it didnt work befure Embarrassed
mr_tawan


Joined: 13 Jan 2014
Posts: 161
DAVIDlavi20091997 wrote:
mr naith and mr tawan tanke you very much to help me dill with this problam the problam just was that the i didnt crated the renderr befure the sdl texture and that i didnt copied the image to the main cpp dirctaury, imn very surry for wasting your time on such an abstract and Trivial matter,tanke you very very much Very Happy Very Happy Very Happy Very Happy Very Happy Very Happy


I know that you're too excited by now. but you might want to consider asking someone to spell check first ^^' It's kinda hard to read.

Glad you get it working (I didn't really help much after all, all the credit goes to Nait.).
Naith


Joined: 03 Jul 2014
Posts: 158
Glad it works now.

Take this as a reminder

- Whenever you want to execute your program from within Visual Studio, all the files that the program needs (images, sounds and so on), need to be in the same directory as the source (*.h, *.cpp) files (or as in my case, some kind of Data directory).
- Whenever you want to execute your program from the Debug / Release directory, all the files that the program needs (images, sounds and so on), need to be in the same directory as the executable file (or as in my case, some kind of Data directory).
DAVIDlavi20091997


Joined: 19 Jul 2014
Posts: 22
mr naith your were right the problam was that the texture was crated befure the Renderer,i deleted all the if statmants just to chack if thats the error and it workes fine without them,tanke you for the halp and time.
and surry for my bad english...im an israeli and speak hebrew as my native tung.tanke you all and see on in the feuture Surprised