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_Texturefuck;
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;
}
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.
Do you mind sharing the file Penguins.BMP ? It’s probably the image file (and probably not).
did you try my code by your self mr tawan ?,IS this problam only mine?
cant any one help me??
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?
the ful dirctury is-C:\Users\David\Desktop\sdl\Debug
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, I found at least 2 errors in you’re code.
- You’re creating the renderer after you’re creating the texture, which means that you’re (while creating the texture) referencing to 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);
}
- 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 ’ \ '.
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;
}
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 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 :).
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 :).
@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)
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 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 ?
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, 0);
// 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, I’m gonna cry.
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.
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 :).
Yeah, everything is done right so far. Now check my last post showing the directory structure and make sure that the Image.bmp file is in two of the directories.
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.
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.