Click here to Skip to main content
13,736,490 members
Rate this:
 
Please Sign up or sign in to vote.
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 6:21am
Updated 13-Oct-18 7:23am
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

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] );
}
  Permalink  
Comments
5 days ago
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180920.1 | Last Updated 13 Oct 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100