Original Post
I am getting a really weird access violation and I can't seem to find out how to fix it. I am using Visual Studio .net 2005, how can I find what is causing this? I've been messing around with a few different things, but it seems like the violation is also moving around with everything i've been trying. I don't know too much about using the debugger so any information in that regards would be much appreciated. I am looking more for how to find the cause of things like this than how to fix my exact problem, but basically in a nuthsell this is what I'm trying to do. I have a Image class that represents an image. This class works because I've used it before. Now I have another class GameGrid, that has a private variable declared as Image* m_background. Now when the constructor of GameGrid is called it initializes the Image member with an image to load. Now it seems like the m_background variable isn't keeping it's value for some reason. Here's the corresponding classes: Image.h Image.cpp GameGrid.h GameGrid.cpp Sorry about all the code. Any information on how to proceed in tracking this bug down would be great. I've been working on it for a day or two now, and I am sure it's something really simple that I am missing. Thanks, Casey
/*
Header file for Image Class
Represents a single bitmap.
*/
#ifndef _IMAGE_H
#define _IMAGE_H
#include "SDL.h"
#include "sdl_util.h"
class Image {
public:
Image(const char* filename, bool trans);
~Image();
// draw, copies m_image to given surface and coordinates.
void draw(SDL_Surface* dest, int x, int y);
void draw(SDL_Surface* dest);
void setCoords(int x, int y) {m_coords.x = x; m_coords.y = y; }
int getWidth() { return m_width; }
int getHeight() { return m_height; }
private:
SDL_Surface* m_image;
SDL_Surface* m_optimized;
SDL_Rect m_coords;
int m_width,
m_height;
bool m_transparent;
}; // end Image
#endif
#include "Image.h"
#include <cstdlib>
Image::Image(const char* filename, bool trans)
{
m_image = SDL_LoadBMP("background.bmp");
if (m_image) {
fprintf(stderr, "Image loaded successfully!!\n");
}
m_width = m_image->w;
m_height = m_image->h;
m_transparent = trans;
if (trans)
SDL_SetColorKey(m_image, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_image->format, 0, 0, 0));
// Convert the image to the display format
//m_optimized = SDL_DisplayFormat(m_image);
//freeSurface(m_image);
//SDL_FreeSurface(m_image);
}
Image::~Image()
{
freeSurface(m_image);
}
// Draws the whole image stored in m_image to dest surface at x and y
void Image::draw(SDL_Surface* dest, int x, int y)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = rect.x + m_width;
rect.h = rect.y + m_height;
SDL_BlitSurface(m_optimized, NULL, dest, ▭);
}
void Image::draw(SDL_Surface* dest)
{
SDL_Rect rect;
rect.x = m_coords.x;
rect.y = m_coords.y;
rect.w = rect.x + m_width;
rect.h = rect.y + m_height;
SDL_BlitSurface(m_optimized, NULL, dest, ▭);
}
#ifndef _GAME_GRID_H
#define _GAME_GRID_H
#include "SDL.h"
#include "sigma/Image.h"
#include "sigma/sdl_util.h"
class GameGrid {
public:
GameGrid();
~GameGrid();
void clear();
void randomize();
void draw(SDL_Surface* dest, int x, int y);
bool isMouseOver() { return true; }
void gridClicked() {}
void swapTiles() {}
private:
static const int GRID_X_SIZE = 4;
static const int GRID_Y_SIZE = 4;
int m_grid[GRID_X_SIZE][GRID_Y_SIZE];
int m_gridX, m_gridY;
Image* m_background;
Image* m_tile;
Image* m_empty;
};
#endif
#include "GameGrid.h"
#include <cstdlib>
GameGrid::GameGrid()
{
m_background = new Image("background.bmp", false);
//m_tile = new Image("bluetile.bmp", false);
clear();
}
GameGrid::~GameGrid()
{
delete m_background;
//delete m_tile;
}
void GameGrid::clear()
{
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 4; y++) {
m_grid[x][y] = 1;
}
}
}
void GameGrid::randomize()
{
}
void GameGrid::draw(SDL_Surface* dest, int x, int y)
{
if (!m_background) {
fprintf(stderr, "m_background loaded.\n");
exit(1);
}
m_background->draw(dest, x, y);
for (int j = 0; j <= 4; j++) {
for (int k = 0; k <= 4; k++) {
if (m_grid[x][y] == 1) {
//m_tile->draw(dest, (x * m_tile->getWidth()), (y * m_tile->getHeight()));
}
}
}
}