Jump to content

  • Log In with Google      Sign In   
  • Create Account


SDL Problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Motoky   Members   -  Reputation: 178

Like
0Likes
Like

Posted 15 February 2013 - 09:04 AM

Hey, I've been practicing with SDL for a little bit now. I've been following the Lazy Foo tutorials, whilst also trying to remember what each function does, trying to see if I can remember some of the important parts without having to look at his tutorials and it's been helping. This next tutorial I have put 90% of the code in through remembering it, the rest is the new stuff. Currently working on True Type Fonts.

 

I've got all of the right files included, linked, etc. All of my previous SDL projects have worked without any problems, but after my last one it just started playing up. First it was having trouble linking, had to go through the setup so many times, still wouldn't work, but managed to get it working again with so random luck and messing with the property settings.

 

I have the code all written out, it compiles without any problems at all, but when I try to start the program a dialog box appears saying:

 

"The application was unable to start correctly (0xc000007b). Click OK to close the application."

 

Tried searching the internet for this, but can't find anything that helps me.

 

I tried adding the code to the post, but it comes out all wrong, even when using the code symbol...



Sponsor:

#2 rip-off   Moderators   -  Reputation: 8109

Like
2Likes
Like

Posted 15 February 2013 - 09:12 AM

According to here, the error means: STATUS_INVALID_IMAGE_FORMAT. The "image" here refers to an executable or DLL file. Where did you get the DLL?



#3 Motoky   Members   -  Reputation: 178

Like
0Likes
Like

Posted 16 February 2013 - 12:17 PM

The DLL files are from the SDL website which was put on the Lazy Foo tutorial:

 

http://www.libsdl.org/projects/SDL_ttf

 

I'm placing the DLL files in the lib file of the main SDL file. I have set up the directories for my project to the right library folder and include folder.

 

I have also copied the SDL.dll and SDL_ttf.dll in my projects folder where the .exe is.



#4 Servant of the Lord   Crossbones+   -  Reputation: 18474

Like
0Likes
Like

Posted 17 February 2013 - 12:38 AM

libSDL.org is the official SDL site, so it's not that the DLLs are bad or anything, just that they might be incompatible with your compiler.

 

What compiler are you using? What version is it?

If you don't know what compiler, which IDE are you using? What version? (Visual Studio, QtCreator, Code::Blocks, etc...) 


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#5 Motoky   Members   -  Reputation: 178

Like
0Likes
Like

Posted 17 February 2013 - 04:42 AM

I am using MS Visual C++ 2010 Express.

 

All my other SDL projects seem to work, so is it just the True Type Font DLL that is the problem.

 

Should I also try another IDE??



#6 Servant of the Lord   Crossbones+   -  Reputation: 18474

Like
0Likes
Like

Posted 17 February 2013 - 11:27 AM

SDL_ttf requires 3 DLLs: SDL_ttf's dll (SDL_ttf.dll), and SDL_ttf's two dependencies (zlib1.dll and libfreetype-6.dll). They are all in the SDL_ttf zip file you downloaded. You don't have to link all three (SDL_ttf.dll is linked to its dependencies already), but you do have to have all three in the same folder as your executable.

Have you already done that?

Also, are you running a 32-bit version of Windows or a 64-bit version? If your Windows version is 32-bit, or if your Windows is 64-bit but you are compiling your project as 32-bit, you must use the x86 version of the DLLs and not the x64 ones (both versions are included in the zip file you downloaded).

I am using MS Visual C++ 2010 Express.

And you downloaded this DLL?
 
Try redownloading it, maybe you grabbed the wrong one.
 

All my other SDL projects seem to work, so is it just the True Type Font DLL that is the problem.

To confirm that hypothesis, compile your exact same project without SDL_ttf (and comment out the SDL_ttf stuff), and see if it gives the same error message.

If it doesn't, it isolates the problem so we know for sure that that one DLL is the problem.
 

Should I also try another IDE??

No, the solution in this case isn't switching tools, but making sure your libraries work with your tools. Sometimes the libraries need to be recompiled using your compiler to make sure they work with your compiler.
 
If A) You have all three DLLs alongside your executable, and you are using the proper x86 version (if your project is a 32-bit project).
and B) Your program runs fine when it doesn't rely on SDL_ttf (isolating the problem)
and C) Your program still doesn't work when you redownloaded the correct DLL (confirming the problem)
 
Then maybe someone can help you compile the DLL from scratch - I don't use Visual Studio myself, so I personally can't walk you through it.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#7 Motoky   Members   -  Reputation: 178

Like
0Likes
Like

Posted 22 February 2013 - 07:47 AM

Sorry it took so long to get back. I've gone through each thing you mentioned. I made sure I downloaded the correct DLL files, I linked up the x86 folder and put the x86 DLL's inside my project's file where the .exe is.

 

I commented out the anything TTF related in my project and it still comes up with the same error after doing all of this.

 

EDIT: Ok, I decided to make a new project. I linked everything up again, copied and pasted the code, compiled and no error. Not to sure what I did wrong the first time, but if this one is working, might as well delete the old one.


Edited by Motoky, 22 February 2013 - 08:06 AM.


#8 Motoky   Members   -  Reputation: 178

Like
0Likes
Like

Posted 15 March 2013 - 06:44 AM

Ok, I'm working on another tutorial and now the screen is appearing then disappearing pretty much instantly. None of the images are being displayed.

 

It's the program which gets you to play sounds and such by including the SDL_mixer library which I have included properly just like with the SDL_ttf library.

 

 

I've tried running the program without any code from the SDL_mixer and even the SDL_ttf library and it's still doing the same thing, so it can't be any of them.

 

I made a seperate class for the SDL_mixer library to make it easier for me to read.

 

lazyfoo.net/SDL_tutorials/lesson11/index.php

 

If you go to that link, scroll down and download the tutorial. That's what my code pretty much looks like, but without the SDL_image library. I've even copied and pasted that exact same code, with a few alterations for it to not give me any errors and it does the same thing. The program just turns off straight away.

 

I've even tried creating a new project again, but still not working...


Edited by Motoky, 15 March 2013 - 06:52 AM.


#9 Servant of the Lord   Crossbones+   -  Reputation: 18474

Like
0Likes
Like

Posted 15 March 2013 - 12:07 PM

Copy and paste your exact same code into [ code ]  [ /code ] brackets, so we can see your alterations. If you made alterations just for it not to give you errors, that might mean that you just modified the code until the compiler stopped complaining, when the compiler had a good reason to complain. smile.png

 

Your first line of defense in programming is listening to the compiler, and your second line of defense is your own code that error-checks your game code. Can we see what you have so far? We might be able to spot the errors.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#10 Motoky   Members   -  Reputation: 178

Like
0Likes
Like

Posted 17 March 2013 - 04:36 AM

My Sound header file

 

 

 
#include
 
 
<SDL_mixer.h>
#include
 
 
<string>
using
 
 
namespace std;
class
 
 
Sound
{
public
 
 
:
Sound();
~Sound();
 
 
bool initSound();
 
 
bool loadFile();
Mix_Chunk* loadSound(string filename);
 
 
void playSound(int playWhich);
 
 
void pauseSound();
 
 
void stopSound();
 
 
void shutDown();
private
 
 
:
 
 
 
Mix_Music* music;
 
 
Mix_Chunk* scratch;
Mix_Chunk* high;
Mix_Chunk* med;
Mix_Chunk* low;
};

 

Sound Source Code

 

 
#include
 
 
"Sound.h"
Sound::Sound()
{
music = NULL;
scratch = NULL;
high = NULL;
med = NULL;
low = NULL;
}
Sound::~Sound()
{
}
bool
 
 
Sound::initSound()
{
 
 
int freq = 0;
Uint16 format = 0;
 
 
int channels = 0;
Mix_QuerySpec(&freq, &format, &channels);
 
 
if(Mix_OpenAudio(freq, format, channels, 4096) == -1)
 
 
return false;
 
 
return true;
}
bool
 
 
Sound::loadFile()
{
music = Mix_LoadMUS(
 
"beat.wav");
 
 
if(music == NULL)
{
 
 
return false;
}
scratch = loadSound(
 
"scratch.wav");
high = loadSound(
 
"high.wav");
med = loadSound(
 
"medium.wav");
low = loadSound(
 
"low.wav");
 
 
if((scratch == NULL) || (high == NULL) ||
(med == NULL) || (low == NULL))
{
 
 
return false;
}
 
 
return true;
}
Mix_Chunk* Sound::loadSound(string filename)
{
Mix_Chunk* sound = NULL;
sound = Mix_LoadWAV(filename.c_str());
 
 
if(sound == NULL)
{
 
 
return NULL;
}
 
 
return sound;
}
void
 
 
Sound::playSound(int playWhich)
{
 
 
switch(playWhich)
{
 
 
case 1:
 
 
if(Mix_PlayChannel(-1, scratch, 0) == -1)
 
 
break;
 
 
case 2:
 
 
if(Mix_PlayChannel(-1, high, 0) == -1)
 
 
break;
 
 
case 3:
 
 
if(Mix_PlayChannel(-1, med, 0) == -1)
 
 
break;
 
 
case 4:
 
 
if(Mix_PlayChannel(-1, low, 0) == -1)
 
 
break;
 
 
default:
 
 
break;
}
}
void
 
 
Sound::pauseSound()
{
 
 
if(Mix_PlayingMusic() == 0)
{
 
 
if(Mix_PlayMusic(music, -1) == -1)
 
 
return;
}
 
 
else
{
 
 
if(Mix_PausedMusic() == 1)
{
Mix_ResumeMusic();
}
 
 
else
{
Mix_PauseMusic();
}
}
}
void
 
 
Sound::stopSound()
{
Mix_HaltMusic();
}
void
 
 
Sound::shutDown()
{
Mix_FreeChunk(scratch);
Mix_FreeChunk(high);
Mix_FreeChunk(med);
Mix_FreeChunk(low);
Mix_FreeMusic(music);
Mix_CloseAudio();
}

 

My Main Source File

 

#include
 
 
<SDL.h>
#include
 
 
<SDL_ttf.h>
#include
 
 
"Sound.h"
#include
 
 
<string>
using
 
 
namespace std;
 
const
 
 
int SCRN_WIDTH = 640;
const
 
 
int SCRN_HEIGHT = 480;
const
 
 
int SCRN_BPP = 32;
SDL_Surface* videoSurface = NULL; 
 
SDL_Surface* background = NULL; 
 
 
SDL_Surface* instruction1 = NULL; 
 
 
SDL_Surface* instruction2 = NULL; 
 
 
SDL_Surface* instruction3 = NULL; 
 
 
SDL_Event 
 
event;
TTF_Font* font = NULL;
SDL_Color fontColor = {255, 255, 255};
Sound* soundMgr = NULL;
 
bool
 
 
initSDL();
 
 
bool
 
 
initTTF();
bool
 
 
loadFiles();
SDL_Surface* loadBMP(string filename); 
 
void
 
 
applySurface(int x, int y,
SDL_Surface* src, 
SDL_Surface* dest,
SDL_Rect* clip = NULL);
 
void
 
 
setText();
void
 
 
renderText();
 
 
void
 
 
shutDown(); 
 
int
 
 
main(int argc, char** argv)
{
 
 
bool quit = false;
 
 
if(!initSDL())
 
 
return -1;
 
 
if(!loadFiles())
 
 
return -1;
 
 
if(!soundMgr->initSound())
 
 
return -1;
setText();
applySurface(0, 0, background, videoSurface, 0);
renderText();
 
 
while(!quit)
{
 
 
if(SDL_PollEvent(&event))
{ 
 
 
if(event.type == SDL_KEYDOWN)
{
 
 
if(event.key.keysym.sym == SDLK_1)
{
soundMgr->playSound(1);
}
 
 
 
if(event.key.keysym.sym == SDLK_2)
{
soundMgr->playSound(2);
}
 
 
if(event.key.keysym.sym == SDLK_3)
{
soundMgr->playSound(3);
}
 
 
if(event.key.keysym.sym == SDLK_4)
{
soundMgr->playSound(4);
}
 
 
if(event.key.keysym.sym == SDLK_9)
{
soundMgr->pauseSound();
}
 
 
if(event.key.keysym.sym == SDLK_0)
{
soundMgr->stopSound();
}
}
 
 
else if(event.type == SDL_QUIT)
{
quit = 
 
true;
}
}
 
 
if(SDL_Flip(videoSurface) == -1)
{
 
 
return -1;
}
}
shutDown();
 
 
return 0;
}
bool
 
 
initSDL()
{
 
 
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
 
 
return false;
videoSurface = SDL_SetVideoMode(SCRN_WIDTH, SCRN_HEIGHT, SCRN_BPP,
SDL_SWSURFACE);
 
 
if(videoSurface == NULL)
 
 
return false;
 
 
if(!initTTF())
 
 
return false;
SDL_WM_SetCaption(
 
"Playing Sound", NULL);
 
 
return true;
}
bool
 
 
loadFiles()
{
background = loadBMP(
 
"background.bmp");
 
 
if(background == NULL)
 
 
return false;
 
 
if(!soundMgr->loadFile())
 
 
return false;
font = TTF_OpenFont(
 
"robotic.ttf", 24);
 
 
if(font == NULL)
 
 
return false;
 
 
return true;
}
bool
 
 
initTTF()
{
 
 
if(TTF_Init() == -1)
 
 
return false;
 
 
return true;
}
SDL_Surface* loadBMP(string filename)
{
SDL_Surface* tempSurface = NULL;
SDL_Surface* optimizedImage = NULL;
tempSurface = SDL_LoadBMP(filename.c_str());
 
 
if(tempSurface != NULL)
{
optimizedImage = SDL_DisplayFormat(tempSurface);
SDL_FreeSurface(tempSurface);
 
 
if(optimizedImage != NULL)
{
Uint32 color = SDL_MapRGB(optimizedImage->format,
0, 0xFF, 0xFF);
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, color);
}
}
 
 
return optimizedImage;
}
void
 
 
applySurface(int x, int y, SDL_Surface* src, SDL_Surface* dest,
SDL_Rect* clip)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(src, clip, dest, &offset);
}
void
 
 
setText()
{
instruction1 = TTF_RenderText_Solid(font,
 
 
"Press 1, 2, 3, or 4 to play a sound effect", fontColor);
instruction2 = TTF_RenderText_Solid(font,
 
 
"Press 9 to play or pause the music", fontColor);
instruction3 = TTF_RenderText_Solid(font,
 
 
"Press 0 to stop the music", fontColor);
}
void
 
 
renderText()
{
 
 
applySurface((SCRN_WIDTH/2) - (instruction1->w/2),
(SCRN_HEIGHT/2)/2,
instruction1, videoSurface);
 
 
applySurface((SCRN_WIDTH/2) - (instruction2->w/2),
(SCRN_HEIGHT/2),
instruction2, videoSurface);
 
 
applySurface((SCRN_WIDTH/2) - (instruction2->w/2),
(SCRN_HEIGHT/2)/2 + (SCRN_HEIGHT/2),
instruction3, videoSurface);
}
void
 
 
shutDown()
{
SDL_FreeSurface(background);
SDL_FreeSurface(instruction1);
SDL_FreeSurface(instruction2);
SDL_FreeSurface(instruction3);
soundMgr->shutDown();
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}

 

It still comes out quite terrible even using the [ code ] brackets.


Edited by Motoky, 17 March 2013 - 04:37 AM.


#11 Servant of the Lord   Crossbones+   -  Reputation: 18474

Like
1Likes
Like

Posted 17 March 2013 - 03:26 PM

When you load an image or resource, you check if it is NULL and then exit if it is. Very good!
 
However, you don't go far enough. Your code needs to tell you where/what fail, when it fails, and hopefully even why.
 
For example, you have this code:
bool loadFiles()
{
	background = loadBMP("background.bmp");
	if(background == NULL)
		return false;
	 
	if(!soundMgr->loadFile())
		return false;
	
	font = TTF_OpenFont("robotic.ttf", 24);
	if(font == NULL)
		return false;
	 
	return true;
}
It's (really truly) excellent that you check if those things return NULL, but if that function fails and returns false, you have zero clue what part of it failed. Was the font failing to load or was it the image? Or was it the sound? You have no idea - but your code does. So you need to have your code report the details of failures to you.

I said, "Your first line of defense in programming is listening to the compiler, and your second line of defense is your own code that error-checks your game code."
Exiting on failure isn't enough - your second line of defense needs to be bolstered to explain and report the errors. When the function fails, you need to know where it failed, and how.
 
One way is to use asserts() for things that are never supposed to fail, but you don't always want your program crashing to the desktop whenever image file #2057 failed to load.
 
Instead, you need some kind of logging system. Whether you report your errors to a log system that creates text files with the error logs in it, or whether you pump the output to the command prompt, you need some kind of textual description when things fail.
 
SDL redirects the std::cout and std::cerr output streams to text files, so you can take advantage of that. Report your errors to std::cout or std::cerr, and SDL will create files next to your executables called 'stdout.txt' and 'stderr.txt' so you can read the output even if your program crashed.
 
So, let's say your code is running along, and it comes across this line:
background = loadBMP("background.bmp");
 
First, you need to check for failures. This is good, you already got that covered:
if(background == NULL)
Second, you need to report the failure, to communicate to the developer or user where your code failed.
std::cout << "Failed in the 'loadFiles()' function." << std::endl;
 
Then you need to report what failed:
std::cout << "Failed to load 'background.bmp'" << std::endl;
Further, if you are able to, you need to communicate when it failed. By 'when', I mean the circumstances surrounding it. This could be the timing of when it failed, or even better, a callstack if you can get one. A callstack is the list of functions that led up to the function you are currently in. In your case, it'd output something like:
  • loadBMP() failed, called from:
  • loadFiles(), which was called from
  • main()
This is especially important as projects get larger and larger, and loadBMP() might get called from dozens of different locations. However, C++ doesn't have built-in standard support for callstacks (sad.png), so only compiler-specific methods exist. This is something you can ignore for now, but being aware of their existence as a very useful bit of information to report.
 
Also included in the 'when' is any variables that are relevant to the failure. In this situation, the only variable involved ("background.bmp") was already reported as part of the 'what'.
'When' is the circumstances surrounding the failure that led, or contributed, to the failure.
 
If possible, you'll want to report why the function failed. The only one who knows why the function failed is the function itself, so different APIs have different ways of reporting what errors occurred. Some APIs return special values that mean certain types of failures, other APIs have other methods.
 
SDL uses a function called SDL_GetError() to return a text description of what went wrong. Oftentimes, it's not too detailed and contains redundant information with what you are already reporting, but it's good to log that information also. When hunting down bugs, every little bit helps.
std::cout << "SDL_GetError() says: " << SDL_GetError() << std::endl;
Finally, you need to handle the failures. To handle a failure means to have your code decide what to do when something goes wrong.
In your existing code, your method of handling the failure is to exit. This is not always the ideal solution, because sometimes you can recover from errors, and sometimes failures are minor. But exiting is at least some kind of solution, and definitely needs to be done if your program can't continue running without a certain resource.
return false;
 
So putting it together, your code might look like this:
background = loadBMP("background.bmp");

if(background == NULL) //Did a failure happen?
{
	std::cerr << "In loadFiles():\n"                               //Report [Where] it happened.
			  << "Failed to load 'background.bmp'\n"               //Report [What] happened.
			  << "SDL_GetError(): \"" << SDL_GetError() << "\"\n"  //Report [Why/How] it happened.
			  << "<The callstack, if you got it>"                  //Report [When] it happened.
			  << "<the values of any variables that contributed to the failure>" //Also part of [When].
			  << "-----------------------------------------" << std::endl;
				 
	return false;
}
But let's think about this for a second further. This is alot of information to report. If your loadFile() loads multiple images, it'd look like this:
bool loadFiles()
{
	myImage1 = loadBMP("myImage1.bmp");
	
	if(myImage1 == NULL) //Did a failure happen?
	{
		std::cerr << "In loadFiles():\n"                               //Report [Where] it happened.
				  << "Failed to load 'myImage1.bmp'\n"               //Report [What] happened.
				  << "SDL_GetError(): \"" << SDL_GetError() << "\"\n"  //Report [Why/How] it happened.
				  << "<The callstack, if you got it>"                  //Report [When] it happened.
				  << "<the values of any variables that contributed to the failure>" //Also part of [When].
				  << "-----------------------------------------" << std::endl;
					 
		return false;
	}
	
	myImage2 = loadBMP("myImage2.bmp");
	
	if(myImage2 == NULL) //Did a failure happen?
	{
		std::cerr << "In loadFiles():\n"                               //Report [Where] it happened.
				  << "Failed to load 'myImage2.bmp'\n"               //Report [What] happened.
				  << "SDL_GetError(): \"" << SDL_GetError() << "\"\n"  //Report [Why/How] it happened.
				  << "<The callstack, if you got it>"                  //Report [When] it happened.
				  << "<the values of any variables that contributed to the failure>" //Also part of [When].
				  << "-----------------------------------------" << std::endl;
					 
		return false;
	}
	
	myImage3 = loadBMP("myImage3.bmp");
	
	if(myImage3 == NULL) //Did a failure happen?
	{
		std::cerr << "In loadFiles():\n"                               //Report [Where] it happened.
				  << "Failed to load 'myImage3.bmp'\n"               //Report [What] happened.
				  << "SDL_GetError(): \"" << SDL_GetError() << "\"\n"  //Report [Why/How] it happened.
				  << "<The callstack, if you got it>"                  //Report [When] it happened.
				  << "<the values of any variables that contributed to the failure>" //Also part of [When].
				  << "-----------------------------------------" << std::endl;
					 
		return false;
	}
	 
	return true;
}
That's alot of super-redundant information that is needlessly copied and pasted, and makes the function loadFile() harder to read.
So the error reporting should actually go into the function that actually failed, and not the function that decides how to handle the failure. In this case, loadBMP() should handle all of loadBMP()'s own error reporting. loadFile() only needs to respond to the failure and decide what to do next.
SDL_Surface *loadBMP(std::string filename)
{
	SDL_Surface *tempSurface = SDL_LoadBMP(filename.c_str());
	if(!tempSurface)
	{
		std::cerr << "In loadImage():\n"
				  << "\tFailed to load \"" << filename << "\"\n"
				  << "\tReason: SDL_LoadBMP() returned NULL\n"
				  << "\tSDL_GetError(): \"" << SDL_GetError() << "\"\n"
				  << "-----------------------------------------" << std::endl;
		
		return NULL; //Use nullptr if you're using the "C++11" standard of C++.
	}
	
	SDL_Surface *optimizedImage = SDL_DisplayFormat(tempSurface);
	SDL_FreeSurface(tempSurface);
	
	if(!optimizedImage)
	{
		std::cerr << "In loadImage():\n"
				  << "\tFailed to load \"" << filename << "\"\n"
				  << "\tReason: SDL_DisplayFormat() returned NULL\n"
				  << "\tSDL_GetError(): \"" << SDL_GetError() << "\"\n"
				  << "-----------------------------------------" << std::endl;
		
		return NULL; //Use nullptr if you're using the "C++11" standard of C++.
	}
	
	Uint32 color = SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF);
	SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, color);
	
	return optimizedImage;
}
Then any function that uses loadBMP() looks nice and neat:
bool loadFiles()
{
	myImage1 = loadBMP("myImage1.bmp");
	if(!myImage1)
		return false;
	
	myImage2 = loadBMP("myImage2.bmp");
	if(!myImage2)
		return false;
	
	myImage3 = loadBMP("myImage3.bmp");
	if(!myImage3)
		return false;
	 
	return true;
}
Or even:
bool loadFiles()
{
	myImage1 = loadBMP("myImage1.bmp");
	myImage2 = loadBMP("myImage2.bmp");
	myImage3 = loadBMP("myImage3.bmp");
	 
	return (myImage1 && myImage2 && myImage3);
}
Though this last version has different logic: It loads all the files, even if some failed, and then checks for failure. In some situations, this may not be desired (you don't want to wait for 10 minutes of loading, only to find out it failed in the first 20 seconds but decided to load the rest before exiting anyway).

So loadBMP() reports (to the log files) the loadBMP() problems, let's the calling functions know that an error occured, and the functions calling loadBMP() then decide how to handle the problem.

Since you are exiting immediately on failure, you could assert():
void loadFiles()
{
	assert(myImage1 = loadBMP("myImage1.bmp"));
	assert(myImage2 = loadBMP("myImage2.bmp"));
	assert(myImage3 = loadBMP("myImage3.bmp"));
}
This at least gives a pop-up box on failure (instead of a silent exit), and gives the piece of code that failed the assertion.

However, just because 'myImage2' failed to load, you still might want to go through your regular freeing of resources before you fully exit.
But, as you've experienced yourself, if a program failed you at least want to pop up a message box letting the user know that something failed (and hopefully give them enough information to report back to the developers so the developers can fix the problem). A silent exit or a messy crash to the desktop leaves the users confused and unsure what steps they should take next.

By doing this:
bool loadFiles()
{
	myImage1 = loadBMP("myImage1.bmp");
	if(!myImage1)
		return false;
	
	myImage2 = loadBMP("myImage2.bmp");
	if(!myImage2)
		return false;
	
	myImage3 = loadBMP("myImage3.bmp");
	if(!myImage3)
		return false;
	 
	return true;
}
And then this:
int main(int argc, char** argv)
{
	if(!initSDL())
	{
		DisplayPopUpBox("Failed to initialize SDL.");
		return -1;
	}
	 
	 
	if(!loadFiles())
	{
		DisplayPopUpBox("Failed to load the resources.");
		return -1;
	}
	
	//etc...
		
}
You can at least let the user know that something went wrong, and then you also have detailed information in your log files (stderr.txt and stdout.txt) for you, the developer, to track down the problem.

In the same way loadBMP() reports its own errors, I'm sure you can make your other functions like soundMgr->loadFile() do the same.

Then, next time you run the code, your own code will tell you what the problem is, and defense line #2 will be a whole lot stronger.

As a random shot at what might be causing the crash, you accidentally call 'loadFiles()' before you call 'soundMgr->initSound()'.
Since 'loadFiles()' calls 'soundMgr->loadFile()', this means you are loading your sound files before you are initializing your sound system, which might fail to work. If this is the case, your error reporting will inform you of this. I also don't see you calling Mix_Init() anywhere.

Best of luck with your project!
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#12 Motoky   Members   -  Reputation: 178

Like
0Likes
Like

Posted 25 March 2013 - 06:51 AM

Ok, I had trouble logging onto the site... So it's taken a bit of time to reply.

 

I've sorted out that problem I originally had. I will put more effort into catching errors and finding out what part of my code is having trouble. By doing this I managed to find out that it was where I was trying to load the background file. I had it saved as a PNG while I was trying to load a BMP. I changed the file format of my image.

 

 

Now I am just having a hell of a lot of trouble with the SDL_mixer library. When I debug, it stalls at the point of loading a sound file. I've tried searching on the internet, but I'm not getting any answers.

 

Then I started getting this System Error:

 

"The program can't start because libstdc++-6.dll is missing from your computer. Try reinstalling the program to fix this problem."

 

I have no idea what file this is, I wasn't getting this error before. I tried redownloading the SDL_mixer library, remaking the project and the same thing happens...



#13 BitMaster   Crossbones+   -  Reputation: 3881

Like
0Likes
Like

Posted 25 March 2013 - 07:25 AM

Unless I'm mistaken libstdc++-6.dll is part of the MinGW runtime. If that DLL was not included I would strongly suggest not using the whole package since whoever compiled it didn't take good care of the dependencies and probably made others mistakes too that will come back to bite you.

That said, I would suggest learning to compile your own libraries instead of trawling the web until you find a DLL with the right name. You will be learning a lot doing that and you will most importantly be able to control your dependencies much better. And while that will initially take a bit longer, in the long run you will be able to avoid/solve yourself the whole class of problems this thread is about.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS