Click here to Skip to main content
15,042,684 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 13-Oct-18 7:23am
v3

1 solution

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.
C++
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] );
}
   
Comments
lock&_lock 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900