download souce and solution
Mapping demo main source:
#include "core_common.h"
#include "core_graphics.h"
#include "core_framework.h"
#include "core_input.h"
#include "frustum.h"
#include "node_tree_mesh.h"
#include "trigger.h"
#include "barrier.h"
#include "auto_map.h"
#include "tool.h"
#pragma warning(disable : 4100)
#define CLIENT_WIDTH 800
#define CLIENT_HEIGHT 600
#define ANIM_WALK 0
#define ANIM_IDLE 1
const float g_angles[13] =
{
0.0f,
0.0f, 1.57f, 0.785f, 3.14f,
0.0f, 2.355f, 0.0f, 4.71f,
5.495f, 0.0f, 0.0f, 3.925f
};
/************************************************************************************************/
class cApp : public cFramework
{
private:
cCamera m_camera;
cInput m_input;
cInputDevice m_keyboard;
cInputDevice m_mouse;
cMesh m_terrain_mesh;
cNodeTreeMesh m_node_tree_mesh;
cAutoMap m_auto_map;
cMesh m_char_mesh;
cObject m_char_object;
cAnimation m_char_anim;
cTrigger m_trigger;
cBarrier m_barrier;
cMesh m_door_mesh;
cAnimation m_door_anim;
float m_x_pos, m_y_pos, m_z_pos;
ID3DXFont* m_font;
/////////////////////////////////////////////////////////////////////////////////////////
public:
bool init()
{
create_display(g_hwnd, CLIENT_WIDTH, CLIENT_HEIGHT, 16, true, true);
set_perspective(D3DX_PI/4, 1.3333f, 1.0f, 10000.0f);
create_font(&m_font, "Arial", 16, true, false);
m_input.create(g_hwnd, get_window_inst());
m_keyboard.create_keyboard(&m_input);
m_mouse.create_mouse(&m_input, true);
m_terrain_mesh.load("..\\Data\\Level.x", "..\\Data\\");
m_node_tree_mesh.create(&m_terrain_mesh, QUADTREE, 256, 32);
m_auto_map.create("..\\Data\\AutoSrc.x", D3DCOLOR_RGBA(0, 0, 255, 255));
m_auto_map.set_viewport(440, 0, 200, 200);
// load character data
m_char_mesh.load("..\\Data\\Warrior.x", "..\\Data\\");
m_char_anim.load("..\\Data\\Warrior.x", &m_char_mesh);
m_char_anim.set_loop(true, "Idle");
m_char_anim.set_loop(true, "Walk");
m_char_object.create(&m_char_mesh);
// set character position
m_x_pos = -800.0f;
m_y_pos = 0.0f;
m_z_pos = -800.0f;
m_trigger.load("..\\Data\\Level.trg");
m_barrier.load("..\\Data\\Level.bar");
// load door mesh and animation
m_door_mesh.load("..\\Data\\Door.x", "..\\Data\\");
m_door_anim.load("..\\Data\\Door.x", &m_door_mesh);
m_door_anim.set_loop(false, "Open");
m_door_anim.set_loop(false, "Close");
// configure door data
for(short i = 0; i < 4; i++)
{
const char* anim_name = m_barrier.get_enable_state(i+1) ? "Close" : "Open";
m_barrier.set_mesh(i+1, &m_door_mesh);
m_barrier.set_anim(i+1, &m_door_anim, anim_name, 0);
}
return true;
}
////////////////////////////////////////////////////////////////////////////
bool frame()
{
static DWORD timer = timeGetTime();
ulong elapsed = timeGetTime() - timer;
timer = timeGetTime(); // update timers
m_mouse.acquire();
m_mouse.read();
m_keyboard.acquire();
m_keyboard.read();
if(m_keyboard.get_key_state(KEY_ESC))
return false;
static float auto_height = 300.0f; // auto map camera height
// process automap 'zoom'
if(m_keyboard.get_key_state(KEY_PAGEUP))
{
if(auto_height < 700.0f)
auto_height += 4.0f;
}
if(m_keyboard.get_key_state(KEY_PAGEDOWN))
{
if(auto_height > 100.0f)
auto_height -= 4.0f;
}
// procces barrier changes
if(m_keyboard.get_key_state(KEY_SPACE))
{
m_keyboard.set_key_state(KEY_SPACE, false);
m_keyboard.m_locks[KEY_SPACE] = true;
for(short i = 0; i < 4; i++)
{
if(m_barrier.get_enable_state(i+1))
{
m_barrier.disable(i+1);
m_barrier.set_anim(i+1, &m_door_anim, "Open", timeGetTime()/20);
}
else
{
m_barrier.enable(i+1);
m_barrier.set_anim(i+1, &m_door_anim, "Close", timeGetTime()/20);
}
}
}
// process movement
long dir = 0;
if(m_keyboard.get_key_state(KEY_UP) || m_keyboard.get_key_state(KEY_W))
dir |= 1;
if(m_keyboard.get_key_state(KEY_RIGHT) || m_keyboard.get_key_state(KEY_D))
dir |= 2;
if(m_keyboard.get_key_state(KEY_DOWN) || m_keyboard.get_key_state(KEY_S))
dir |= 4;
if(m_keyboard.get_key_state(KEY_LEFT) || m_keyboard.get_key_state(KEY_A))
dir |= 8;
float x_move = 0.0f, z_move = 0.0f;
static float cam_angle = 0.0f, move_angle = 0.0f;
static long last_anim = -1;
if(dir) // press direction arrow
{
move_angle = g_angles[dir] + m_camera.m_y_rot; // key diretion + mouse direction
float speed = elapsed / 1000.0f * 512.0f;
x_move = sin(move_angle) * speed;
z_move = cos(move_angle) * speed;
// check for movement collisions - can not walk past anything blocking path.
if(m_node_tree_mesh.is_ray_intersect(m_x_pos, 32.0f, m_z_pos,
m_x_pos + x_move, 32.0f, m_z_pos + z_move,
NULL))
{
x_move = z_move = 0.0f;
}
// check for barrier collisions
if(m_barrier.get_barrier(m_x_pos + x_move, m_y_pos, m_z_pos + z_move))
x_move = z_move = 0.0f;
m_x_pos += x_move;
m_z_pos += z_move;
// change animation and check triggers
if(!float_equal(x_move, 0.0f) || !float_equal(z_move, 0.0f))
{
if(last_anim != ANIM_WALK)
{
last_anim = ANIM_WALK;
m_char_object.set_anim_set(&m_char_anim, "Walk", timeGetTime()/20);
}
long trigger_id = m_trigger.get_trigger(m_x_pos, m_y_pos, m_z_pos);
if(trigger_id != 0)
{
m_auto_map.visible_section(trigger_id - 1);
m_trigger.disable(trigger_id);
}
}
}
else // no press arrow key, just set idle animation.
{
if(last_anim != ANIM_IDLE)
{
last_anim = ANIM_IDLE;
m_char_object.set_anim_set(&m_char_anim, "Idle", timeGetTime()/20);
}
}
m_char_object.move(m_x_pos, m_y_pos, m_z_pos);
m_char_object.rotate(0.0f, move_angle, 0.0f);
// position camera
cam_angle -= ((float)m_mouse.get_x_delta() * elapsed / 800.0f);
m_camera.point(m_x_pos + cos(cam_angle) * 300.0f, m_y_pos + 100.0f, m_z_pos + sin(cam_angle) * 300.0f,
m_x_pos, m_y_pos, m_z_pos);
set_display_camera(&m_camera);
cFrustum frustum;
frustum.create(0.0f);
// render everything
clear_display(0, 1.0f);
if(begin_display_scene())
{
enable_zbuffer();
m_node_tree_mesh.render(&frustum, 0.0f);
m_barrier.render(timeGetTime()/20, &frustum);
m_char_object.update_anim(timeGetTime()/20, true);
m_char_object.render();
m_auto_map.render(&m_camera, m_x_pos, auto_height, m_z_pos, 1, &m_x_pos, &m_z_pos, &move_angle);
// draw character position
char text[256];
sprintf(text, "%.3f, %.3f, %.3f",
m_char_object.get_x_pos(), m_char_object.get_y_pos(), m_char_object.get_z_pos());
draw_font(m_font, text, 10, 10, 0, 0, COLOR_WHITE, DT_LEFT);
end_display_scene();
}
present_display();
return true;
}
};
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
DWORD pos_x = (get_screen_width() - CLIENT_WIDTH) / 2;
DWORD pos_y = (get_screen_height() - CLIENT_HEIGHT) / 4;
build_window(inst, "AutoMapClass", "Auto Map Demo",
WS_BORDER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
pos_x, pos_y, CLIENT_WIDTH, CLIENT_HEIGHT);
cApp app;
app.run();
return 0;
}