Hi all- first post…
Anyway, I’ve been following the Lazy Foo’ Productions SDL tutorials (I won’t link to it, might trigger the spam filter), and I’m stuck at lesson 3. It compiles fine (using Visual Studio Express 2010), but I’ve been getting a weird crash whenever I try and run the program, and I get these errors from the debug log:
Code:
First-chance exception at 0x68126030 in SDL_Projects.exe: 0xC0000005: Access violation reading location 0x00000004.
Unhandled exception at 0x68126030 in SDL_Projects.exe: 0xC0000005: Access violation reading location 0x00000004.
The program ‘[7944] SDL_Projects.exe: Native’ has exited with code -1073741819 (0xc0000005).
Now, I’ve deviated from the tutorial quite a bit, and since it won’t run at all, I’m not sure if other problems exist, like in my image loading functions, but it seems to me that it’s happening even when following the tutorial word-for-word. Any ideas as to what’s causing it? Also, one of my functions is attempting to load multiple images at once. Is there a better way to do that than with arrays? My code is below:
Code:
#include <SDL.h>
#include <SDL_image.h>
#define DEBUG
#define SX 640
#define SY 480
#define SBPP 32
#ifdef DEBUG
#include
#include
#endif
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;
#ifdef DEBUG
std::ofstream debugLog;
#endif
SDL_Surface loadImage(char filename) {
SDL_Surface* rawImg = NULL;
SDL_Surface* optImg = NULL;
rawImg = IMG_Load(filename);
if(rawImg != NULL) {
optImg = SDL_DisplayFormat(rawImg);
}
#ifdef _DEBUG_
else {
debugLog << "Error: Could not load image. filepath: " << filename << std::endl;
}
#endif
SDL_FreeSurface(rawImg);
return optImg;
}
void blitToPos(int x, int y, SDL_Surface* src, SDL_Surface* dest) {
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(src, NULL, dest, &offset);
}
bool init(SDL_Surface* screen, int screenX = 640, int screenY = 480, int bitsPerPixel = 32, char* windowName = “SDL Window”) {
if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {
#ifdef DEBUG
debugLog << “Error initializing SDL.\n”;
#endif
return false;
}
screen = SDL_SetVideoMode(screenX, screenY, bitsPerPixel, SDL_SWSURFACE);
if(screen = NULL) {
#ifdef _DEBUG_
debugLog << "Error setting up screen.\n";
#endif
return false;
}
SDL_WM_SetCaption(windowName, NULL);
return true;
}
bool loadImgs(char* files, SDL_Surface* surfaces[]) {
char* fileToLoad = “”;
int i = 0;
fileToLoad = strtok(files, ",");
#ifdef _DEBUG_
debugLog << "fileToLoad: " << fileToLoad << std::endl;
#endif
while(fileToLoad != NULL) {
surfaces[i] = IMG_Load(fileToLoad);
fileToLoad = strtok(NULL, ",");
i++;
if(surfaces[i] == NULL) {
#ifdef _DEBUG_
debugLog << "Error loading image at filepath \"" << fileToLoad << "\" to surface at pos " << i << ".\n";
#endif
return false;
}
}
return true;
}
void quit(SDL_Surface* surfaces[], int surfaceNum, bool quitSDL = true) {
int i = 0;
while(i <= surfaceNum - 1) {
SDL_FreeSurface(surfaces[i]);
i++;
}
if(quitSDL == true) {
SDL_Quit();
}
}
int main(int argc, char* args[]) {
#ifdef DEBUG
debugLog.open(“debugLog.txt”, std::ios::out);
debugLog << “-----BEGIN LOG-----\n”;
#endif
SDL_Surface *surfaces[1] = {image};
init(screen, SX, SY, BPP);
loadImgs("hello.bmp",surfaces);
blitToPos(160, 120, surfaces[0], screen);
if(SDL_Flip(screen) == -1) {
return 1;
}
SDL_Delay(2000);
quit(surfaces, 1);
#ifdef _DEBUG_
debugLog << "-----END LOG-----\n";
#endif
return 0;
}