Runtime Cubes

Now let’s write the code for the Cube demo program. This will be a new project. First we set the basic properties in the game_preload event. In game_init we create the cube and set the ambient lighting. Finally, in game_render3D we clear the scene, set the identity (thus returning the current focus to the origin), and then rotate and render the cube.

#include "..EngineAdvanced2D.h"
using namespace Advanced2D;
Camera *camera;
Mesh *mesh;

bool game_preload()
{
    g_engine->setAppTitle("CUBE DEMO");
    g_engine->setFullscreen(false);
    g_engine->setScreenWidth(1024);
    g_engine->setScreenHeight(768);
    g_engine->setColorDepth(32);
    return true;
}

bool game_init(HWND)
{
    //create a cube
    mesh = new Mesh();
    mesh->CreateCube(2.0f, 2.0f, 2.0f);

    //set the camera and perspective
    camera = new Camera();
    camera->setPosition(0.0f, 2.0f, 6.0f);
    camera->setTarget(0.0f, 0.0f, 0.0f);
    camera->Update();

    //set the ambient color
    g_engine->SetAmbient(D3DCOLOR_XRGB(40,40,255));

    return true;
}

void game_update() { }
void game_end()
{
    delete camera;
    delete mesh;
}

void game_render3d()
{
    //clear the scene using a dark blue color
    g_engine->ClearScene(D3DCOLOR_RGBA(30,30,100, 0));

    //return to the origin
    g_engine->SetIdentity();

    //rotate and draw the cube
    mesh->Rotate(2.0f, 0.0f, 0.0f);
    mesh->Transform();
    mesh->Draw();
}

In order to compile this program using the Advanced2D engine’s new rendering capabilities, you will need to provide your Cube program with the following library files (added to the linker options):

  • Advanced2D

  • d3d9

  • d3dx9

  • dxguid

  • winmm

For each file listed, prepend the file with -l (lowercase letter L) for Dev-C++, or append .lib for Visual C++. If you aren’t sure how to do this, refer to the previous chapter.

All things considered, this is a ridiculously short code listing even if we are just rendering a flat-shaded cube with ambient lighting (see Figure 2.1).

Figure 2.1. The Cube demo renders a flat-shaded cube with ambient lighting.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset