Why is a sprite being created without a Draw function?

The Partridge Family were neither partridges nor a family. Discuss.
pupperoni
Posts: 12
Joined: August 12th, 2017, 12:56 am

Why is a sprite being created without a Draw function?

Post by pupperoni » August 12th, 2017, 1:18 am

Sorry for the long post in advance!
I finished watching tutorial 9 and decided I wanted to make something similar to the poop game, but with different sprites. I re-extracted the framework to start from new and decided to do the pixel art first. It's basically the same thing, but the poops are bones and the dude is a dog.

I started with Game.h, and created void DrawDog(); and void DrawBone(); then defined them with the appropriate gfx.PutPixel
However, I realized that I wanted to be able to switch between a dog looking forward and a dog looking backward. I didn't want to completely rewrite the code as a flipped image, so I wondered if I could just change x+ to x- and y- to y+. This, of course, completely flipped the dog upside down. So I changed the y back to -.
(The (x,y) sprite is the bottom right-hand corner of the sprite looking forward; so just under the dog's tail.)

At this point, in Game.cpp I have:

Code: Select all

void Game::ComposeFrame()
{
	DrawDogForward(400,300); 
	DrawBone(200,200); 
	DrawBone(500, 500); 
}


just to make sure the sprites look fine, and to test that inputting those coordinates worked in creating multiple sprites by hand. They only have two int because I'm not interested in changing their colors.

Back to the flipping dog. I wrote the code so that if you press the left arrow key, it executes DrawDogBackward, otherwise DrawDogForward. If I press right, the first forward-facing dog will move to the right. If I pressed left, the first forward-facing dog would disappear as expected, producing a backward-facing dog. Two problems with this backward-facing dog: it would be created at (400,300) and would be attached to a second forward-facing dog at the tails.

I ended up fixing this by changing the code to:

Code: Select all

left = wnd.kbd.KeyIsPressed(VK_LEFT);

	if (left)
	{
		DrawDogBackward(fx-1,fy); 
		bx = bx - 2; 
		fx = fx - 2; 
	}
	else
	{DrawDogForward(fx,fy); 
	}

*** fx/y = forward facing dog x/y; bx/y = backward facing dog x/y

This made it so the backward-facing dog would appear one pixel behind the forward-facing dog. I still don't understand why the second forward-facing dog was created, but I solved it by:

Code: Select all

void Game::ComposeFrame()
{
	
	DrawBone(500, 500); 

}


(getting rid of one of the bones had nothing to do with the creation of the second forward-facing dog, I was just experimenting with the bones)

So I'm thinking, okay, now I don't have a DrawDog function at all...will it still be created? CTRL+F5 and sure enough, a forward-facing dog is created at (400,300). So this is a very long story, basically wondering why the second forward-facing dog was attached to the backward-facing dog, and why the sprite is still able to be created at (400,300) despite not having a draw function in the ComposeFrame.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Why is a sprite being created without a Draw function?

Post by albinopapa » August 12th, 2017, 2:40 am

Can't just guess at these things. Upload a clean zipped solution or upload to github and share if you are seriously needing help.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

pupperoni
Posts: 12
Joined: August 12th, 2017, 12:56 am

Re: Why is a sprite being created without a Draw function?

Post by pupperoni » August 12th, 2017, 3:33 am

As I said, I fixed the issue. I posted literally all the code I wrote, excluding the pixels. The only additional information I have to give is this in game.h

Code: Select all

/*  User Functions  */            
	void DrawBone(int tx, int ty); 
	void DrawDogForward(int fx, int fy); 
	void DrawDogBackward(int bx, int by);

		/********************************/
private:
	MainWindow& wnd;
	Graphics gfx;
	/********************************/
	/*  User Variables              */
	int fx = 400;
	int fy = 300;

	int bx = 400;
	int by = 300; 

	int vfx = 0; 
	int vfy = 0; 
	int vbx = 0; 
	int vby = 0; 

	/********************************/


That's all the code I ended up writing. There's nothing needing help now; just trying to understand why two forward-facing dogs were created/why one attached to the backwards-facing dog and why a forward-facing dog is created without a DrawDogForward command in ComposeFrame.

My idea now is that the DrawDogForward was the second dog created, as it was DrawDogForward(400,300);
however it would move with the backward-facing dog instead of staying at (400,300) so perhaps not.

The only thing I can think of creating the forward-facing dog now is this:

Code: Select all

left = wnd.kbd.KeyIsPressed(VK_LEFT);

   if (left)
   {
      DrawDogBackward(fx-1,fy); 
      bx = bx - 2; 
      fx = fx - 2; 
   }
   else
   {DrawDogForward(fx,fy); 
   }


as that is the only place DrawDog commands exist.

Just trying to understand the logic that's running the code, the problem has been fixed.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Why is a sprite being created without a Draw function?

Post by albinopapa » August 12th, 2017, 4:05 am

I guess you misunderstand. I can't guess what is drawing a second image without seeing and running the code in it's entirety.

For instance:

Code: Select all

left = wnd.kbd.KeyIsPressed(VK_LEFT);

   if (left)
   {
      DrawDogBackward(fx-1,fy); 
      bx = bx - 2; 
      fx = fx - 2; 
   }
   else
   {DrawDogForward(fx,fy); 
   }
If this is really the code, where is the matching { for the else's }? This would lead me to believe you are missing a { and it shouldn't even compile. Seeing as how you are compiling and running, this is not the case so again, can't even guess as to why you are/were getting a second image or even why the backward dog was following the frontward dog or whatever, but whatevs.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

pupperoni
Posts: 12
Joined: August 12th, 2017, 12:56 am

Re: Why is a sprite being created without a Draw function?

Post by pupperoni » August 12th, 2017, 4:18 am

The code goes:

define left

if (left)
{ Draw the dog backwards }
else
{ Draw the dog forwards }

I'm not sure why you're suggesting there is a missing bracket, or it's simply too close to the "D" for you to distinguish.

Once again, the issue was fixed, and I did not keep a zipped copy of the malfunctioning code. Following the Draw commands, there is code detailing "if left, move left; if right, move right" and the constraints of the box. However, I did not believe those were prevalent to the creation of an image. Other than the physical sprite, the code I posted in the original comment is all the code I wrote. Even in the second post, I said, "maybe it could be due to this."

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Why is a sprite being created without a Draw function?

Post by albinopapa » August 12th, 2017, 4:20 am

Yeah, it was too close for me to see it, ma'bad, I'm visually impaired.

Still, in the future, you can avoid all this confusion by just sharing the project. Good luck.

Asking for help without the project is like standing with your nose against a painting of a red barn and getting asked "What color is the roof?". You'd have to step back and see the entire painting to give the answer.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Why is a sprite being created without a Draw function?

Post by chili » August 12th, 2017, 11:56 am

Yeah, basically what papa said. The thing about asking for help is, you might be sure that the problem is in one place and that you only need to show the code for that place. But in the majority of cases, the problem isn't where the help-seeker is assuming it is, and that's part of the problem. Also, it's just a lot easier on us if you provide the full code. Just something to think about for next time ;)
Chili

OrbitalReign
Posts: 19
Joined: July 17th, 2017, 1:24 pm

Re: Why is a sprite being created without a Draw function?

Post by OrbitalReign » August 13th, 2017, 12:26 am

Im not sure if it is the case as im a nooboid but by the looks and with a quick test you were calling your draw function twice each frame. so your 'if' statement called a draw then compose frame called the draw. getting rid of the compose frame calls didnt stop the ones in your if statement.

pressing left called the left dog and then compose frame called the right dog in the same frame.... maybe.

pupperoni
Posts: 12
Joined: August 12th, 2017, 12:56 am

Re: Why is a sprite being created without a Draw function?

Post by pupperoni » August 14th, 2017, 12:04 am

OrbitalReign wrote:Im not sure if it is the case as im a nooboid but by the looks and with a quick test you were calling your draw function twice each frame. so your 'if' statement called a draw then compose frame called the draw. getting rid of the compose frame calls didnt stop the ones in your if statement.

pressing left called the left dog and then compose frame called the right dog in the same frame.... maybe.
I agree, I think that's what was happening. Because when I let go of the right arrow key, the second dog would stay and move in the same manner as the first dog. And I guess they looked like they were "attached" because they both spawned at (400, 300); they weren't actually moving in tandem with each other, they were actually trying to occupy the same space.

So that's one mystery solved.
Now my problem is that I finally got collision to work on the top, bottom, and left side...but any time the dog passes to the right of a bone, it registers as collision no matter how far away it is as long as they're on the same y.
That, and my start screen won't just go away when I press Enter, I have to keep Enter held down.

pupperoni
Posts: 12
Joined: August 12th, 2017, 12:56 am

Re: Why is a sprite being created without a Draw function?

Post by pupperoni » August 14th, 2017, 12:27 am

If anyone one like to take a look, this is the current code. I have commented out both attempts at collision.

The "IsColliding" code (the second commented section) results in bones being eaten if the dog is anywhere to the left of them.

The "if (colliding)" code (first commented section) results in only the top two bones showing collision. But on the plus side...it's normal collision.
Attachments
Dog 2 - Share.zip
(3.4 MiB) Downloaded 149 times

Post Reply