Sign in to follow this  

[Solved] (C++, Win32 console) Problem drawing a buffer to output

This topic is 3459 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

EDIT: Solved after a few days of banging my head against a wall. I guess it is a bit specific to my particular program, so I'm not surprised nobody responded. Thanks anyways. The topic is a little misleading, but it's hard to sum up in a few words... I already wrote comments for it, so I'll just [ source]-tag it. It describes what goes on, and what the problem is, pretty well.
// The Draw function copies a rectangle of data from one buffer to another,
// and draws the second buffer to the screen.

// 'viewloc' is set beforehand, and is a COORD representing
// the upper-left corner of the rectangle copied to the second buffer

// First it loops through and copies the buffer area. If any
// part of the rect is over the edge of the first buffer, it "loops" the
// opposite edge over, to cause an "infinite map" kind of effect.

// Second, it tries to loop through each entity and draw the sprites to
// the correct location on the temporary buffer. The problem is that this
// doesn't work correctly when you want to draw the entity to the
// part of the buffer that was looped around. I haven't been able to figure
// out what's going on to cause it to do that.. the actual putting the entity
// to the tempbuf works fine as long as it's on the part of the map that
// wasn't looped around.

void GameView::Draw(int x, int y)
{
	CHAR_INFO* tempbuf = new CHAR_INFO[viewsize.X*viewsize.Y];

	// Construct what the window sees on the map
	for (int i = viewloc.Y; i < viewloc.Y+viewsize.Y; ++i)
	{
		for (int j = viewloc.X; j < viewloc.X+viewsize.X; ++j)
		{
			int tempidx = (i-viewloc.Y)*viewsize.X+(j-viewloc.X); // index within the tempbuf
			int playidx = i*realsize.X+j;                         // these two indices refer to the same conceptual location.

			// This "sews" the left/right and up/down map edges together, if they're visible.
			// The loop properly accounts for viewing windows longer or wider than the map.
			if (j >= realsize.X)
				for (int sewcount = j/realsize.X; sewcount > 0; --sewcount)
					playidx -= realsize.X;
			if (i >= realsize.Y)
				for (int sewcount = i/realsize.Y; sewcount > 0; --sewcount)
					playidx -= realsize.X*realsize.Y;

			tempbuf[tempidx].Attributes = playbuf[playidx].Attributes;
			tempbuf[tempidx].Char = playbuf[playidx].Char;
		}
	}

	std::list<entity*>::iterator itr = entities.begin();

	for (; itr != entities.end(); ++itr)
	{
		entity* ent = *itr;

		if (ent->position.X + ent->size.X - 1 < viewloc.X || //left bound
			ent->position.X > viewloc.X + viewsize.X  || //right bound
			ent->position.Y + ent->size.Y - 1 < viewloc.Y || //upper bound
			ent->position.Y > viewloc.Y + viewsize.Y) //lower bound
		{
			continue;
		}

		for (int Y = ent->position.Y; Y < ent->position.Y + ent->size.Y; ++Y)
		{
			for (int X = ent->position.X; X < ent->position.X + ent->size.X; ++X)
			{
				int Yidx = Y - viewloc.Y;
				int Xidx = X - viewloc.X;

				if (Xidx >= viewsize.X || Yidx >= viewsize.Y)
					continue;

				int tempidx = Yidx * viewsize.X + Xidx;
				int spriteidx = Y * (ent->size.X-1) + X;

				tempbuf[tempidx].Attributes = ent->sprite[spriteidx].Attributes;
				tempbuf[tempidx].Char = ent->sprite[spriteidx].Char;
			}
		}
	}

	SMALL_RECT writerect = {x, y, x+viewsize.X-1, y+viewsize.Y-1};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD topleft = {0, 0};

	WriteConsoleOutput(hOut, tempbuf, viewsize, topleft, &writerect);
}




EDIT: It looks like it has to do with the if statement, after I create an iterator for the entities list, that tries to clip out the entities that aren't visible in the current window. Although I think the problem has to deal with my apparent blurriness regarding screen versus window coordinatess, so to speak, the solution is still eluding me. [Edited by - Twisol on June 29, 2008 5:52:40 PM]

Share this post


Link to post
Share on other sites
Okay, I couldn't for the life of me find a rule on bumping your own thread, so if I shouldn't be doing this, tell me where it says that please!

I'm getting really frustrated with this code, I've tried everything I can think of, and I still have no solution to the problem with the entities not appearing on the wrapped-around part of the map.

Share this post


Link to post
Share on other sites

This topic is 3459 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