load bmp returns NULL |
DAVIDlavi20091997
|
cant any one help me??
|
|||||||||||
|
mr_tawan
|
Try changing every \ to \\ (two backslashes for each backslash), or to / (forward slash). Also checks if the file presents in that path. |
|||||||||||||
|
DAVIDlavi20091997
|
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
|
Do you mind sharing the file Penguins.BMP ? It's probably the image file (and probably not).
|
|||||||||||
|
DAVIDlavi20091997
|
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
|
the ful dirctury is-C:\Users\David\Desktop\sdl\Debug
|
|||||||||||
|
DAVIDlavi20091997
|
did you try my code by your self mr tawan ?,IS this problam only mine?
|
|||||||||||
|
Naith
|
@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.
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.
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.
|
|||||||||||||||||
|
DAVIDlavi20091997
|
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
|
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
|
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 . 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 . |
|||||||||||
|
Naith
|
@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:
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:
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:
What this basically mean is: If g_pTexture is created (ergo, not NULL), destroy it. You wrote this:
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
|
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
#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; } |
|||||||||||
|
DAVIDlavi20091997
|
ps-soory for the shouting and the smaly paces in the end
|
|||||||||||
|
Naith
|
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.
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
|
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
|
|
|||||||||||
|
DAVIDlavi20091997
|
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
|
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
|
|
|||||||||||
|
mr_tawan
|
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 .
|
|||||||||||
|
Naith
|
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
|
http://speedy.sh/QryVp/sdl.zip here is the zip, i copyed the pic every where
|
|||||||||||
|
DAVIDlavi20091997
|
by the way im using v-s express 2010
|
|||||||||||
|
DAVIDlavi20091997
|
and i cant give direct access to my computer its just my private stuff and the things of my famlay is on it
|
|||||||||||
|
Naith
|
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
|
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
|
om my frikking goood,IT WORKES.........
|
|||||||||||
|
Naith
|
And what did you do to make it work?
|
|||||||||||
|
DAVIDlavi20091997
|
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
|
|||||||||||
|
DAVIDlavi20091997
|
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
|
|||||||||||
|
mr_tawan
|
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
|
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
|
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 |
|||||||||||
|