Advertisement

Spaceolopy

Started by July 25, 2022 01:09 AM
86 comments, last by pbivens67 1Β year, 11Β months ago

What's wrong with the way I've shown you?

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

fleabay said:
texture[bugType + bugFrame] where bugFrame would be 0 or 1.

can you explain this code a little more?

Advertisement

@pbivens67 : If you had two different types of bugs, and 2 frames per bug, you'd create 4 images, each with a different index in your texture array. It would be indexed as follows. . .

Texture[0] is bugType = 0, bugFrame = 0

Texture[1] is bugType = 0, bugFrame = 1

Texture[2] is bugType = 2, bugFrame = 0

Texture[3] is bugType = 2, bugFrame = 1

Notice you'd iterate bugType based on the number of frames here. If you have 2 frames per bug, your bugTypes would go 0.2,4,6.8 etc

Each bug has its type, and its current frame (which, in our example will cycle between 0 and 1)

If you have bugType 0 and you want to animate that, you'd cycle between Texture[0] and Texture[1]. Meaning bugType 0 and bugFrame 0 or 1.

If you have bugType 2 and you want to animate that, you'd cycle between Texture[2] and Texture[3]. Meaning bugType 2 and bugFrame 0 or 1.

well texture[0]-texture[5] is used to draw the dice sprites.

@pbivens67 : in that case, you could either offset the bug textures by the last value you use for the 6 dice sprites. So it would be

texture[bugType + bugFrame + 6]

Or you could have a new texture array for the bugs, and keep the old array for the Dice. . .

DiceTextures[DiceIndex];

BugTextures[BugType+Frame]

well, I have got the sprite to alternate back and forth when it is being drawn, I just want it to move more slowly. I have adjusted the timer delay in glutTimerFunc(timer delay, timer, 0) but it does not slow it down.

void timer(int val)
{
	if (flag == 0)
	{
	drawBug(-10.0f, 90.0f, 12, 0);
	flag = 1;
	}
	else if (flag == 1)
	{
	drawBug(-10.0f, 90.0f, 8, 0);
	flag = 0;
	}
	glutPostRedisplay();
	glutTimerFunc(100, timer, 0);
}
Advertisement

@pbivens67 : Instead of alternating each frame, you can alternate every 20 frames (or whatever number of frames you want).



	// TransitionLimit controls how fast we cycle between animations

    int TransitionLimit = 20; // this is so that we change every 20 frames

    
	AnimationCount++; // keep bumping this value up each time
	
	// here we cycle back to 0 when our value goes to double our transition limit (40 in this case)
	if(AnimationCount >= TransitionLimit * 2 ) 
	      AnimationCount = 0;

    

    // here we use AnimationCount and our TransitionLimit to find our current animation state
	if (AnimationCount < TransitionLimit) // for the first 20 frames do this
	     AnimationState = 0; 
	else // we are equal to, or above, TransitionLimit
	     AnimationState = 1; 	      

In the example above, I've created a variable called AnimationState, which will be used in your draw code to control what specifically gets drawn, when you draw your bugs. It will cycle back and forth every 20 timer ticks. And if you want it to go slower or faster than that, simply change the TransitionLimit value.

I would also put the drawBug() code in your render function, instead of drawing things in your timer function. The timer function can call glutPostRedisplay() which will call your render code, and in that render code is where you should be drawing your bugs.

GLUT = Good Luck Using That

I'm so clever. ?

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

Warp9 said:
int TransitionLimit = 20; // this is so that we change every 20 frames AnimationCount++; // keep bumping this value up each time // here we cycle back to 0 when our value goes to double our transition limit (40 in this case) if(AnimationCount >= TransitionLimit * 2 ) AnimationCount = 0; // here we use AnimationCount and our TransitionLimit to find our current animation state if (AnimationCount < TransitionLimit) // for the first 20 frames do this AnimationState = 0; else // we are equal to, or above, TransitionLimit AnimationState = 1;

how do I integrate this in my code

pbivens67 said:

Warp9 said:
int TransitionLimit = 20; // this is so that we change every 20 frames AnimationCount++; // keep bumping this value up each time // here we cycle back to 0 when our value goes to double our transition limit (40 in this case) if(AnimationCount >= TransitionLimit * 2 ) AnimationCount = 0; // here we use AnimationCount and our TransitionLimit to find our current animation state if (AnimationCount < TransitionLimit) // for the first 20 frames do this AnimationState = 0; else // we are equal to, or above, TransitionLimit AnimationState = 1;

how do I integrate this in my code

Since you have been using global variables, I'd make AnimationCount and AnimationState global integers (although you could do AnimationCount as a static variable instead).

Then I'd put the code I posted previously in your timer function (so AnimationState gets set in the timer function).

And finally, you'd check AnimationState in your Rendering code, so that you'd know which version to draw (which would be consistent with your previous code). Or, if you wanted to do things a bit differently, you could make AnimationState one of the parameters for your drawBug() function.

This topic is closed to new replies.

Advertisement