Sign in to follow this  

Unhandled exception

This topic is 4544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had this problem yesterday and the forum was down so I couldnt get any help :P. I still havent solved it and its getting reeeally frustrating. Anyways, its a runtime unhandled exception. Which (from what Ive learned) should mean Im accessing a vector outside the boundries. The program Im trying to write is a one where I can edit/make my maps for my tilebased game (im also doing a tile engine). Heres the class that holds and manage the tiles: TileSystem.h
#ifndef _TILESYSTEM 
#define _TILESYSTEM

#include <vector>
#include <map>
#include <fstream>
#include <string>
#include <sstream>

#include "Log.h"
#include "MapCell.h"

class TileSystem {
private:
	std::vector<MapCell>	World;	// holds all the cells in the world

	int		SizeX;	// how many chars on each axis
	int		SizeY;

public:
			TileSystem(int x, int y);	// sizes the map to the size specified

	int		GetSizeX() {return SizeX;}
	int		GetSizeY() {return SizeY;}				// returns the size of the map

	// --- Tile stuff

	void	Resize(int SizeX, int SizeY);	// resizes the vector, WARNING clears data

	std::string	GetTexture(int x, int y);	// returns the name of the texture at the coords

	void	FillWorld(std::string TextureName);		// fills the world with the specified tile
};

#endif



TileSystem.cpp
#include "TileSystem.h"

TileSystem::TileSystem(int x, int y) {
	Resize(x, y);
}

void TileSystem::Resize(int x, int y) {
	SizeX = x;	// store the new values in the class
	SizeY = y;

	World.resize(SizeX*SizeY);	// resize the vector

	std::stringstream str;	// and report to log
	str << "TileSystem: World cleared and resized to " << SizeX << " x " << SizeY;
	str << "\nTileSystem: The size of world is now " << World.size() << " elements";
	Log::Write(str.str().c_str());
}

void TileSystem::FillWorld(std::string TextureName) {
	for(int y = 0; y < GetSizeY(); y++) {
		for(int x = 0; x < GetSizeX(); x++) { 
			World[ (y*SizeX) + x ].Name = TextureName;
		}
	}
	std::stringstream str;		// log

	if(GetTexture(0, 0) != TextureName) {	// just a simple errorscheck
		str << "TileSystem: ERROR: 0, 0 Wasnt set to " << TextureName;
		Log::Write(str.str().c_str());
		return;
	}

	str << "TileSystem: World filled with tile \"" << TextureName << "\"";;
	//str << "\n0,0: " << GetTexture(0,0);
	Log::Write(str.str().c_str());
}

std::string	TileSystem::GetTexture(int x, int y) {
	unsigned int element = (y*SizeX)+x;

	if(element >= World.size())	// should be size 1200
		terminate();
	else
		return World[element].Name;
}



The vector is made up by map cells: MapCell.h
#ifndef _MAPCELL
#define _MAPCELL

#include <string>

struct MapCell {
	std::string		Name;		// the name of the texture/tile
	bool			Block;		// if the tile hinders movement	
};

#endif



Then we got the main class, who handles input and main loop: TileMapper.h
#ifndef _GAME
#define _GAME

#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

#include "Sdl_handle.h"
#include "AsciiMap.h"
#include "Log.h"
#include "Defines.h"
#include "TileSystem.h"
#include "MapCell.h"

class TileMapper {
private:
	Sdl_handle	sdl;			// for the graphics
	Log			Log;			// for the logs
	TileSystem	TileSystem;		// all the tiles

	bool		running;		// is the program running

	Uint8*		keys;			// to keep track of the keys
	SDL_Event	event;

public:
	// init stuff ---
			TileMapper();

	bool	LoadImages();		// loads all the images needed for the program
	bool	InitVideo();		// init graphics

	// main stuff --- 

	void	RenderWorld();		// draws all the tiles
	void	MainLoop();			// the main lo
};

#endif



TileMapper.cpp
#include "TileMapper.h"

TileMapper::TileMapper() : sdl( RESX, RESY, 32), TileSystem(RESX / GRIDSIZE, RESY / GRIDSIZE)
{
	running = true;
}

bool TileMapper::LoadImages() {
	// load the bitmaps
	if(!sdl.LoadBmp("Player"))	return false;
	if(!sdl.LoadBmp("Black"))	return false;
	if(!sdl.LoadBmp("Border"))	return false;

	return true;
}

bool TileMapper::InitVideo() {
	if(SDL_Init(SDL_INIT_VIDEO) != 0) {
		std::stringstream temp;
		temp << "Unable to init SDL: " << SDL_GetError();
		Log::Write(temp.str().c_str());
		return false;
	}

	Log::Write("Sdl: Video initialized");
	return true;
}

void TileMapper::MainLoop() {
	TileSystem.FillWorld("Player");

	RenderWorld();
	sdl.Flip();

	while(running) {
		while ( SDL_PollEvent(&event) ) {
			if ( event.type == SDL_QUIT )  
				running = false;

			if ( event.type == SDL_KEYDOWN ) {
				if ( event.key.keysym.sym == SDLK_ESCAPE )	// escape was pressed
					running = false;
			}
		}
	}
}

void TileMapper::RenderWorld() {
	int y;
	int x;

	std::stringstream str;

	if( (29*TileSystem.GetSizeX()+39) != 1199) {
		str << "ERROR: World size is " << 29*TileSystem.GetSizeX()+39;
		Log::Write(str.str().c_str());
		return;
	}
    
	for(y = 0; y < TileSystem.GetSizeY(); y++) {
		for(x = 0; x < TileSystem.GetSizeX(); x++) { 
			sdl.DrawImg( TileSystem.GetTexture(x, y), x*GRIDSIZE, y*GRIDSIZE );	// throws the tiles texture to sdl_handle
		}
	}

	str.clear();
	str << "RenderWorld() did render " << x*y << " tiles, the last one was a " << TileSystem.GetTile(x, y).Name;
	Log::Write(str.str().c_str());
}



There... somewhere in the renderworld loop I get that exception... Probarbly Im accessing somewhere outside the boundries. I did cut out some unneccessery functions.. if I post to much no one cares to read :P. I did also cut like a million IF statements that Ive placed everywhere to check that Im within the limits... but it just goes through the ifs and Im getting errors anyway. :( Can anyone see whats wrong? [Edited by - Mizipzor on July 4, 2005 5:36:18 AM]

Share this post


Link to post
Share on other sites
This sort of thing is probably best solved using the debugger. Place a breakpoint somewhere in the code, and step through until you find the exact line it falls over. Check that the values of the relevant variables are what you'd expect them to be.

Share this post


Link to post
Share on other sites

This topic is 4544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this