domingo, 15 de junio de 2014

Structure of a C/C++ realtime application.

Now that we know more or less how a function is defined in C, we can start creating a more complex program flow as follows:

#include <stdio.h>      // for printf()
#include <windows.h>    // for interacting with Windows

void setup()
{
    printf("- setup() called.\n");
};

void update()
{
    printf("- update() called.\n");
};

void draw()
{
    printf("- draw() called.\n");
};

int main( void )
{
    setup(); // call setup()
    

    int frameCounter=0; // variable for keeping track of the number of frame since execution began
    

    while( true ) // execute while what's inside () is true
    {
        
        printf("Current frame number: %i\n", frameCounter); // print the frame number        

        update();   // update frame
        draw();     // render frame
        if(GetAsyncKeyState(VK_ESCAPE)) // check for escape key pressed
        {
            break; // exit while()
        }
        Sleep(50);    // wait 50 milliseconds then continue executing

 
        frameCounter = frameCounter+1; // increase our frame counter by 1

    };
   
    return 0; // return an int
}

This program will start executing main and call the setup() function which is supposed to hold initialization code, then after that it will enter executing in a loop the code inside the while() block between braces until the contents inside the parenthesis evaluate to false, or until we use the "break" statement, which is used when the escape key is detected being pressed.

This leaves you with an execution flow that executes frames one after another until termination is requested, which is what realtime applications do to give the sensation of "realtime" (though they are discrete time steps shown one after another, as frames in a movie).

In the following tutorial we will be adding game logic to this skeleton code, so see you there!






No hay comentarios:

Publicar un comentario