Drawing items to the screen with scrolling

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Drawing items to the screen with scrolling

Post by Bullet Pulse »

In my program, an inventory is a collection of items, not necessarily belonging to the player.

Code: Select all

void Inventory::Draw(SDL_Surface *surface)
{
	for each (Item *i in items)
	{
		i->setY(i->getY()+ Level::getScrollY());
		i->setX(i->getX() + Level::getScrollX());

		if (i->getX() < System::SCREEN_WIDTH || i->getY() < System::SCREEN_HEIGHT)
			i->Draw(surface);

		i->setY(i->getY() - Level::getScrollY());
		i->setX(i->getX() - Level::getScrollX());
	}
}

void Item::Draw(SDL_Surface *surface)
{
	SDL_BlitSurface(this->surface, &srcRect, surface, &dstRect);
}

int getX() { return dstRect.x; }

int getY() { return dstRect.y; }

void setX(int x) { dstRect.x = x; }

void setY(int y) { dstRect.y = y; }
When I load up the game, any items that were given a negative coordinate, end up at 0,0.
Also, when the items should scroll off the screen, because I'm walking in the other direction, they just end up at the edge of the map.
It's almost as if they refuse to become negative.
If you could help me figure this out, I would be very grateful ;)
Image
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Drawing items to the screen with scrolling

Post by lotios611 »

It sounds like there's a signed int in there somewhere.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Drawing items to the screen with scrolling

Post by Ginto8 »

lotios611 wrote:It sounds like there's a signed int in there somewhere.
you mean unsigned ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Drawing items to the screen with scrolling

Post by lotios611 »

Ginto8 wrote:
lotios611 wrote:It sounds like there's a signed int in there somewhere.
you mean unsigned ;)
Oops, I must of looked to fast.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
Post Reply