SDL Problem

Started by
11 comments, last by BitMaster 11 years ago

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...

Advertisement

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?

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.

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...)

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??

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.

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.

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...

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.

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.

This topic is closed to new replies.

Advertisement