Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Pointer Problem 1

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
CA
Hi,

I'm having some trouble with the code below. I'm actually using Watcom C++ with the 32-bit DOS4GW extender, but I believe my problem is compiler-independant -- I'm new to C++. It's a simple game I'm writing for VGA ModeX/13h, but it's crashing in the commented-out region...
Code:
const	SCREEN_WIDTH	= 320,
	SCREEN_HEIGHT	= 200;

const	UNIVERSE_WIDTH	= SCREEN_WIDTH * 5,
	UNIVERSE_HEIGHT	= SCREEN_HEIGHT * 5;

const	MASK_INDEX	= 0;

struct Image
{
	unsigned short	width,
			height;
	unsigned char	*buffer;
};

Image		*g_offscreen,
		*g_universe;

void init_environment(void)
{
	srand((unsigned)time(0));

	set_video_mode(VIDEO_MODE_VGA);
/*
	g_offscreen = new Image();
	g_offscreen->width = SCREEN_WIDTH;
	g_offscreen->height = SCREEN_HEIGHT;
	g_offscreen->buffer = new unsigned char[SCREEN_WIDTH * SCREEN_HEIGHT];
	memset(g_offscreen->buffer, MASK_INDEX, SCREEN_WIDTH * SCREEN_HEIGHT);

	g_universe = new Image();
	g_offscreen->width = UNIVERSE_WIDTH;
	g_offscreen->height = UNIVERSE_HEIGHT;
	g_offscreen->buffer = new unsigned char[UNIVERSE_WIDTH * UNIVERSE_HEIGHT];
	memset(g_universe->buffer, MASK_INDEX, UNIVERSE_WIDTH * UNIVERSE_HEIGHT);

	for (unsigned long i = 0; i < (UNIVERSE_WIDTH * UNIVERSE_HEIGHT); i++)
	{
		if (get_random(0, 9) == 0)
		{
			g_universe->buffer[i] = (unsigned char)get_random(20, 225);
		}
	}
*/
}

void cleanup_environment(void)
{
	delete[] g_universe->buffer;
	delete g_universe;

	delete[] g_offscreen->buffer;
	delete g_offscreen;

	set_video_mode(VIDEO_MODE_TEXT);
}

If someone could point out my memory-related issue(s) it would be greatly appreciated. The code compiles without errors or warnings. When executed, DOS goes into VGA mode 13h and then crashes in the commented-out region...I can narrow it down to somewhere around the new or memset keywords. I haven't quite figured out how to debug code in Watcom, so I have to hack my way through this :-(.

Thanks
 
Part 2 of the commented initialization:
Code:
    g_universe = new Image();
    g_offscreen->width = UNIVERSE_WIDTH;
    g_offscreen->height = UNIVERSE_HEIGHT;
    g_offscreen->buffer = new unsigned char[UNIVERSE_WIDTH * UNIVERSE_HEIGHT];
    memset(g_universe->buffer, MASK_INDEX, UNIVERSE_WIDTH * UNIVERSE_HEIGHT);
May be g_universe->...? Not g_offscreen->...
 
Oh for crying out loud, I can't believe I did that!!! That's the last time I stay up all night coding. [purpleface]

Guess I just needed a fresh pair of eyes. Thanks
 
copy&paste != divide&rule.
Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top