Advertisement

drawing sprites

Started by June 22, 2017 04:18 AM
87 comments, last by Tom Sloper 7 years, 4 months ago
20 hours ago, Lactose said:

In one of Josh's earlier posts he said where the timeStampPrevious variable should be stored. It needs to be a member variable of a class (which also contains the drawScene function), or it needs to be a global variable or something similar. It should also be initialized once, not every time the function is run. Globals are not the best suggestion (they rarely are), but it might be easier to start with a global and make things work.

Similarly for the timeUntilSecondSprite variable. It is currently created, initialized every time the function is called, and is destroyed once the function exits.

If you want to have info persist across multiple calls to the same function, you can't use just local variables like you are currently doing.

 

In general, there's a lot of strange stuff in your code. Read through your code line by line and try to understand what each line is supposed to do.

 

Additionally, all the "upper_row_xxx" functions also screams that you should look into loops as well, but that's a problem for another time...

in  your second paragraph you said "and is destroyed once the function exits"

1 minute ago, phil67rpg said:

in  your second paragraph you said "and is destroyed once the function exits"

Exactly, "it is destroyed", not "it should be destroyed". Bolded for emphasis:

"It is currently created, initialized every time the function is called, and is destroyed once the function exits."

Hello to all my stalkers.

Advertisement

I am sorry lactose but I am confused about what you mean.

12 minutes ago, phil67rpg said:

I am sorry lactose but I am confused about what you mean.

A quick function example to show what I mean:


void exampleFunction()
{
	int exampleVariable = 5; //This creates and initializes a variable.
	exampleVariable += 2; //This adds 2 to the variable.
	std::cout << exampleVariable << std::endl; //This prints the variable.
}

Every single time the function is called, the "exampleVariable" is created and initialized to the value of 5.

Every single time the function completes (reaches the closing curly bracket), the variable is destroyed.

This means that every single time the print (cout in this case) happens, it will always print "7", even if I called the function 1000 times.

 

This is why you need to have some variables in a different place (as a global variable, or as a member variable in a class, etc.), if you want to have it update and change over time. This is fundamental stuff which you need to know, and if you are struggling with this you should read up on variables and scope before continuing.

Hello to all my stalkers.

To be clear, your variables timeStampNow and timeStampPrevious will be affected as Lactose has described each time drawScene() is called.

As I understand, the precision timer itself is a piece of hardware that your "chrono::high_resolution_clock" object examines. This piece of hardware never stops or clears. However, the "chrono::high_resolution_clock" object may have functionality to give the appearance of resetting a timer. But this will likely only work if you account for scope as Lactose has described.

well I  have done several flowcharts and parsed through my code many times. I am really trying to solve this problem.

Advertisement

@phil67rpg, I'm sure you can agree that you are a beginner. Personally, I think that it's reasonable to say that things relating to time and timing are very challenging for a beginner to tackle and I'd also say that things relating to *precision* timing can be very challenging for an intermediate programmer. But I do think that you may yet be able to create the illusion of movement.

For whatever direction the communication in this thread went, we are now at this point where you are trying to use a precision timer to create a delay between screen redraws. This is not how I went about it when I was a beginner but it is something that I do use now. I think you're not too far off from a solution and it's a good solution to find so why not try to continue down this path.

First, I would recommend that you try an experiment to simply try and create a delay of maybe 10 seconds using the timer object that you're working with. Personally, I would do this with my timer object by first getting the current time and then adding the appropriate duration to that (if you don't know how to do that, consult the documentation for your timer object). Then, I enter a while loop that continually gets the current time and exits when the destination time has been reached or passed. Nothing else executes while in that while loop, but it works.

Next, I would recommend that you take out all of the timer code that you have in your drawScene() function and put it in whatever function you have that calls drawScene(). I presume that will be your main game loop. Get the times that you need and implement your delay timer for the duration that you desire (I presume 30 milliseconds) after your drawScene() function has completed running.

Good luck with your project.

thanks kseh I will take your advice, and yes this is quite complex

is there anymore advice anyone can give me?

4 minutes ago, phil67rpg said:

is there anymore advice anyone can give me?

What have you tried since last time?

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement