13,795,118 members
See more:
I'm doing a project to show 2 pictures one after another in a display with 60 Hz refresh rate. I want to show a sequence of slow transition (1 pic every second. Sec 1 = pic1, sec 2= pic2) and then slowly change to the faster transition (60x in a second = 60fps) using key.

I tried to use modulus (mod), but it seems like I'm still a little bit confused about how does it work.

What I have tried:

I tried to change sequence 60 times in a second (60fps). Here's what I do :
```if(p++ % 2 < 1) argDrawImage(image1);
else        argDrawImage(image2);```
This one works and this code the picture changes 60x in a second.

I also make a keyEvent so I can change the modulation value to get the transition but as I said I'm still a little bit confused about this mod function.
```if (key == '1' ) {
mod += 1;
if (mod > 100) mod = 100;
}

if (key == '2' ) {
mod -= 1;
if (mod < 0) mod = 0;
}
```
Posted 13-Oct-18 7:21am
Updated 13-Oct-18 8:23am
v3

## Solution 1

The modulus operator returns the remainder of a division operation. If the divisor is 2 then the possible results will be 0 and 1. A common tactic is to use an array (or vector) of the divisor in size. In this case it would be two. Then the result of the modulus can be used to directly index into the array. It is very, very simple and is difficult to mess up. Here is an implementation of code that passes the array, its size, and an index and the function figures out which image to draw.
```size_t DrawImageMod( Images * images[], size_t imgcount, size_t index )
{
size_t modvalue = index % imgcount;
argDrawImage( images[modvalue] );
return modvalue;
}

void DrawLoop()
{
// create an array of images

const size_t count = 2;
Images * images[count] = { 0 };
images[0] = CreateImage(...);    // returns a pointer to an Image
images[1] = CreateImage(...);    // returns a pointer to an Image

size_t index = 0;
while( ContinueDrawing() )       // loop until this returns false
{
index = DrawImageMod( images, count, index );
++index;
}

// clean up the images

DeleteImage( images[1] );
DeleteImage( images[0] );
}```
Member 13987597 14-Oct-18 13:02pm

I found another way by subtracting elapsed time and the time when I hit the key. Then compare the result with the delay. Thanks again.

Top Experts
Last 24hrsThis month
 Afzaal Ahmad Zeeshan 240 Richard MacCutchan 180 OriginalGriff 164 CPallini 90 Vincent Maverick Durano 60
 OriginalGriff 1,935 Richard MacCutchan 1,611 CPallini 618 RickZeeland 443 Patrice T 432