Testing Meshes as Entities

Let’s see whether the entity manager can handle Mesh objects. I’m using a high-poly mesh (the cytovirus.x file from the DirectX SDK examples), so the load time is lengthy if you increase the number of entities in this demo. To enable the program to start up in a reasonable time, it defaults to only 10 mesh objects. (You could optionally use a low-poly mesh such as the ball.x file featured in the BouncingBalls demo back in Chapter 2.) In addition to demonstrating how mesh entities are handled, this program also highlights the weaknesses in the 3D rendering portion of the engine.

Let’s face it, we’re building a 2D engine here, we have ignored advanced 3D rendering and optimization issues completely, and we are rendering each mesh subset individually. Since Direct3D is a state-based renderer, it must change its state every time a new mesh subset is rendered, which is extremely slow. This engine needs a vertex buffer—badly. But we aren’t going to create one. There are plenty of books about 3D engine development, but this is not one of them, and it does not pretend to be.

Figure 7.2 shows the output from the MeshEntityDemo program. The entity-based code in the following listing is highlighted in bold.

Figure 7.2. This program automatically updates and renders mesh entities.


#include "..EngineAdvanced2D.h"
using namespace Advanced2D;
#define MAX 10
Camera *camera;
Light *light;

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

bool game_init(HWND)
{
    //set the camera and perspective
    camera = new Camera();
    camera->setPosition(0.0f, 2.0f, 50.0f);
    camera->setTarget(0.0f, 0.0f, 0.0f);
    camera->Update();

    //create a directional light
    D3DXVECTOR3 pos(0.0f,0.0f,0.0f);
    D3DXVECTOR3 dir(1.0f,0.0f,0.0f);
    light = new Light(0, D3DLIGHT_DIRECTIONAL, pos, dir, 100);
    light->setColor(D3DXCOLOR(1,0,0,0));
    g_engine->SetAmbient(D3DCOLOR_RGBA(0,0,0,0));

    //load meshes
    Mesh *mesh;
    for (int n=0; n<MAX; n++) {
        mesh = new Mesh();
        mesh->Load("cytovirus.x");
        mesh->SetScale(0.02f,0.02f,0.02f);
        float x = rand() % 40 - 20;
        float y = rand() % 40 - 20;
        float z = rand() % 10 - 5;
        mesh->SetPosition(x,y,z);
        //add mesh to entity manager
        g_engine->addEntity(mesh);
    }

    return 1;
}

void game_update()
{
    //nothing to update!
}

void game_render3d()
{
    g_engine->ClearScene(D3DCOLOR_RGBA(0,0,60,0));
    g_engine->SetIdentity();
}

void game_keyRelease(int key)
{
    if (key = = DIK_ESCAPE) g_engine->Close();
}
void game_entityUpdate(Advanced2D::Entity* entity)
{
    if (entity->getRenderType() = = RENDER3D) {
        //type-cast Entity to a Mesh
        Mesh* mesh = (Mesh*)entity;
        //perform a simple rotation
        mesh->Rotate(0,0.2f,0);
    }
}

void game_entityRender(Advanced2D::Entity* entity)
{
    //type-cast Entity to a Mesh
    Mesh* mesh = (Mesh*)entity;

    //engine automatically renders each entity
    //but we can respond to each render event here
}

void game_end()
{
    delete camera;
    delete light;
}

void game_render2d() { }
void game_keyPress(int key) { }
void game_mouseButton(int button) { }
void game_mouseMotion(int x,int y) { }
void game_mouseMove(int x,int y) { }
void game_mouseWheel(int wheel) { }

That wraps up entity management, at least for the time being. We’ll come back to the subject again two chapters from now, when we add physics-related features to the engine. But first, let’s spend some more time in sprite animation code in the next chapter and build a font system. Not only will that permit text output in a game, but more importantly, it will let us print out debugging info on the screen.

 

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

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