本篇是游戏中物件的定义与使用(8)的续篇。
下载源码和工程
CharICS浏览器的开发
界面设计:
resource.h:
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by CharICS.rc
//
#define IDC_INVENTORY 101
#define IDD_INVENTORY 101
#define IDC_OK 1000
#define IDC_CLOSE 1000
#define IDC_ITEMS 1001
#define IDC_PREVIEW 1003
#define IDC_EQUIP 1004
#define IDC_USE 1005
#define IDC_DROP 1006
#define IDC_ADD 1009
#define IDC_DESCRIPTION 1010
#define IDC_UP 1011
#define IDC_DOWN 1012
#define IDC_SORT 1013
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 104
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1011
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
CharICS.RC:
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_INVENTORY DIALOGEX 0, 0, 408, 254
STYLE DS_SETFONT | DS_MODALFRAME | WS_CAPTION
CAPTION "Inventory"
FONT 12, "Segoe UI", 400, 0, 0x0
BEGIN
DEFPUSHBUTTON "Close",IDC_CLOSE,272,233,50,14
LISTBOX IDC_ITEMS,8,5,158,172,LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
CONTROL "",IDC_PREVIEW,"Static",SS_BLACKRECT | WS_BORDER,171,0,230,172,WS_EX_CLIENTEDGE
PUSHBUTTON "Equip",IDC_EQUIP,7,180,50,15,WS_DISABLED
PUSHBUTTON "Use",IDC_USE,62,180,50,15,WS_DISABLED
PUSHBUTTON "Drop",IDC_DROP,117,180,50,15,WS_DISABLED
PUSHBUTTON "Add Item",IDC_ADD,8,223,50,15
LTEXT "",IDC_DESCRIPTION,172,182,229,38,SS_SUNKEN
PUSHBUTTON "Move Up",IDC_UP,7,202,50,13
PUSHBUTTON "Move Down",IDC_DOWN,62,202,50,13
PUSHBUTTON "Sort",IDC_SORT,117,202,50,13
END
/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_INVENTORY, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 401
TOPMARGIN, 7
BOTTOMMARGIN, 247
END
END
#endif // APSTUDIO_INVOKED
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
#endif // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
主程序:
/******************************************************************************
PURPOSE:
char ics test.
******************************************************************************/
#include <windows.h>
#include <stdio.h>
#include "core_common.h"
#include "core_graphics.h"
#include "tool.h"
#include "mil.h"
#include "char_ics.h"
#include "resource.h"
#define MAX_ITEMS 1024
sItem g_items[MAX_ITEMS]; // MIL item list
cCharIcs g_char_ics;
cCamera g_camera;
//==================================================================================
// Add all char items into listbox.
//==================================================================================
void list_items(HWND dlg)
{
char text[256];
HWND listbox = GetDlgItem(dlg, IDC_ITEMS);
reset_listbox(listbox);
for(sCharItemPtr item = g_char_ics.get_root_item(); item != NULL; item = item->next)
{
if(item->quantity != 1)
sprintf(text, "%s x %lu", g_items[item->item_index].name, item->quantity);
else
sprintf(text, "%s", g_items[item->item_index].name);
add_string_to_listbox(listbox, text);
}
// disable buttons
EnableWindow(GetDlgItem(dlg, IDC_EQUIP), FALSE);
EnableWindow(GetDlgItem(dlg, IDC_USE), FALSE);
EnableWindow(GetDlgItem(dlg, IDC_DROP), FALSE);
UpdateWindow(dlg);
}
//==================================================================================
// Dialog procedure.
//==================================================================================
BOOL CALLBACK dialog_proc(HWND dlg, UINT msg, WPARAM word_param, LPARAM long_param)
{
static bool loaded = false;
static cMesh mesh;
static cObject object;
sCharItemPtr item;
int sel;
char text[256];
HWND items_wnd;
switch(msg)
{
case WM_INITDIALOG:
// set the display mode to this dialog box
create_display(GetDlgItem(dlg, IDC_PREVIEW), 0, 0, 0, TRUE, TRUE);
list_items(dlg);
return TRUE;
case WM_PAINT:
clear_display(D3DCOLOR_RGBA(0, 64, 128, 255), 1.0f);
if(loaded)
{
g_d3d_device->SetTransform(D3DTS_VIEW, g_camera.get_view_matrix());
if(g_d3d_device->BeginScene())
{
object.rotate(0.0f, (float)timeGetTime() / 1000.0f, 0.0f);
object.render();
g_d3d_device->EndScene();
}
}
present_display();
return TRUE;
case WM_COMMAND:
switch(LOWORD(word_param))
{
case IDC_CLOSE:
EndDialog(dlg, TRUE);
return TRUE;
case IDC_ADD:
// add a random item
while(1)
{
sel = rand() % MAX_ITEMS;
if(g_items[sel].name[0])
break;
}
g_char_ics.add(sel, 1, NULL);
// list items and free mesh
list_items(dlg);
mesh.free();
loaded = FALSE;
break;
case IDC_USE:
items_wnd = GetDlgItem(dlg, IDC_ITEMS);
if((sel = (int)get_listbox_cur_sel(items_wnd)) == LB_ERR)
break;
item = g_char_ics.get_item(sel);
if(check_bit(g_items[item->item_index].flags, USEONCE))
{
g_char_ics.remove(item);
// list items and free mesh
list_items(dlg);
mesh.free();
loaded = FALSE;
}
break;
case IDC_UP:
items_wnd = GetDlgItem(dlg, IDC_ITEMS);
if((sel = (int)get_listbox_cur_sel(items_wnd)) == LB_ERR)
break;
item = g_char_ics.get_item(sel);
if(item->prev)
{
g_char_ics.move_up(item);
// list items and free mesh
list_items(dlg);
mesh.free();
loaded = FALSE;
// reselect item in list and force selection
set_listbox_cur_sel(items_wnd, sel-1);
SendMessage(dlg, WM_COMMAND, MAKEWPARAM(IDC_ITEMS, LBN_SELCHANGE), 0);
}
break;
case IDC_DOWN:
items_wnd = GetDlgItem(dlg, IDC_ITEMS);
if((sel = (int)get_listbox_cur_sel(items_wnd)) == LB_ERR)
break;
item = g_char_ics.get_item(sel);
if(item->next)
{
g_char_ics.move_down(item);
// list items and free mesh
list_items(dlg);
mesh.free();
loaded = FALSE;
// reselect item in list and force selection
set_listbox_cur_sel(items_wnd, sel+1);
SendMessage(dlg, WM_COMMAND, MAKEWPARAM(IDC_ITEMS, LBN_SELCHANGE), 0);
}
break;
case IDC_SORT:
g_char_ics.sort();
// list items and free mesh
list_items(dlg);
mesh.free();
loaded = FALSE;
break;
case IDC_DROP:
items_wnd = GetDlgItem(dlg, IDC_ITEMS);
if((sel = (int)get_listbox_cur_sel(items_wnd)) == LB_ERR)
break;
item = g_char_ics.get_item(sel);
g_char_ics.remove(item);
// list items and free mesh
list_items(dlg);
mesh.free();
loaded = FALSE;
// reselect item in list and force selection
set_listbox_cur_sel(items_wnd, sel);
SendMessage(dlg, WM_COMMAND, MAKEWPARAM(IDC_ITEMS, LBN_SELCHANGE), 0);
break;
case IDC_ITEMS:
if(HIWORD(word_param) == LBN_SELCHANGE)
{
items_wnd = GetDlgItem(dlg, IDC_ITEMS);
if((sel = (int)get_listbox_cur_sel(items_wnd)) == LB_ERR)
break;
item = g_char_ics.get_item(sel);
SetWindowText(GetDlgItem(dlg, IDC_DESCRIPTION), g_items[item->item_index].desc);
// set up drop button
BOOL can_drop = check_bit(g_items[item->item_index].flags, CANDROP) ? TRUE : FALSE;
EnableWindow(GetDlgItem(dlg, IDC_DROP), can_drop);
HWND equip_wnd = GetDlgItem(dlg, IDC_EQUIP);
HWND use_wnd = GetDlgItem(dlg, IDC_USE);
// set up equip/use button
EnableWindow(equip_wnd, FALSE);
EnableWindow(use_wnd, FALSE);
switch(g_items[item->item_index].category)
{
case WEAPON:
case ARMOR:
case SHIELD:
case ACCESSORY:
EnableWindow(equip_wnd, TRUE);
break;
case EDIBLE:
case HEALING:
case TRANSPORTATION:
EnableWindow(use_wnd, TRUE);
break;
}
// load the mesh and object if any
sprintf(text, "..\\Data\\%s", g_items[item->item_index].mesh_filename);
mesh.load(text, "..\\Data\\");
if((loaded = mesh.is_loaded()) == TRUE)
object.create(&mesh);
}
break;
}
break;
}
return FALSE;
}
//==================================================================================
// Routine entry.
//==================================================================================
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
// read all master items list from file
FILE* fp;
if((fp = fopen("..\\Data\\Default.mil", "rb")) == NULL)
return -1;
fread(g_items, 1, sizeof(g_items), fp);
fclose(fp);
// load all character items from file
if(! g_char_ics.load("..\\Data\\Char.ci"))
return -1;
g_camera.point(0.0f, 50.0f, -200.0f, 0.0f, 50.0f, 0.0f);
DialogBox(inst, MAKEINTRESOURCE(IDD_INVENTORY), NULL, dialog_proc);
g_char_ics.save("..\\Data\\Char.ci");
destroy_display();
return 0;
}
截图: