📄 matsysapp.cpp
字号:
case WM_KEYDOWN:
AppKey( wParam, true );
break;
case WM_KEYUP:
AppKey( wParam, false );
break;
case WM_ACTIVATE:
if ((LOWORD(wParam) != WA_INACTIVE) && ((HWND)lParam == NULL))
{
ShowWindow(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
}
else
{
if (m_bFullScreen)
{
ShowWindow(hwnd, SW_MINIMIZE);
}
}
return 0;
case WM_SETFOCUS:
if(g_bCaptureOnFocus)
{
MouseCapture();
}
break;
case WM_KILLFOCUS:
if(g_bCaptureOnFocus)
{
MouseRelease();
}
break;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
{
if(!g_bCaptureOnFocus)
{
g_nCapture++;
MouseCapture();
}
}
break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
{
if(!g_bCaptureOnFocus)
{
g_nCapture--;
MouseRelease();
}
}
break;
case WM_CLOSE:
Term();
m_bActive = FALSE;
break;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam);
}
bool MaterialSystemApp::InitMaterialSystem()
{
RECT rect;
// Init libraries.
MathLib_Init( true, true, true, 2.2f, 2.2f, 0.0f, 2.0f );
SpewOutputFunc( MatSysAppSpewFunc );
if ((m_hDC = GetDC((HWND)m_hWnd)) == NULL)
{
ChangeDisplaySettings(0, 0);
MessageBox(NULL, "GetDC on main window failed", "FATAL ERROR", MB_OK);
return FALSE;
}
// Load the material system DLL and get its interface.
char *pDLLName = "MaterialSystem.dll";
m_hMaterialSystemInst = LoadLibrary( pDLLName );
if( !m_hMaterialSystemInst )
{
return Sys_Error( "Can't load MaterialSystem.dll\n" );
}
CreateInterfaceFn clientFactory = Sys_GetFactory( pDLLName );
if ( clientFactory )
{
m_pMaterialSystem = (IMaterialSystem *)clientFactory( MATERIAL_SYSTEM_INTERFACE_VERSION, NULL );
if ( !m_pMaterialSystem )
{
return Sys_Error( "Could not get the material system interface from materialsystem.dll" );
}
}
else
{
return Sys_Error( "Could not find factory interface in library MaterialSystem.dll" );
}
// Figure out the material path.
char fullPath[1024];
const char *pPath = FindParameterArg("-game");
char modDir[512];
if(!pPath)
{
// If they didn't specify -game on the command line, use VPROJECT.
SetQdirFromPath(".");
pPath = basegamedir;
strcpy( modDir, gamedir );
}
else
{
modDir[0] = 0;
}
sprintf(fullPath, "%smaterials", pPath);
char workingDir[512];
GetCurrentDirectory( sizeof(workingDir), workingDir );
const char *pShaderDLL = FindParameterArg("-shaderdll");
char defaultShaderDLL[256];
if(!pShaderDLL)
{
strcpy(defaultShaderDLL, "shaderapidx9.dll");
pShaderDLL = defaultShaderDLL;
}
m_pFileSystemDLL = Sys_LoadModule( "filesystem_stdio.dll" );
if( !m_pFileSystemDLL )
{
return Sys_Error( "Can't load file system DLL (filesystem_stdio.dll)" );
}
if ( !strlen( modDir ) )
{
sprintf( modDir, "%s\\%s", workingDir, pPath );
}
IFileSystem *pFileSystem = ( IFileSystem * )Sys_GetFactory(m_pFileSystemDLL)( FILESYSTEM_INTERFACE_VERSION, NULL );
if ( pFileSystem->Init() != INIT_OK )
return false;
pFileSystem->AddSearchPath( modDir, "PLATFORM" );
if(!m_pMaterialSystem->Init(pShaderDLL, &g_MatSysAppMaterialProxyFactory, Sys_GetFactory(m_pFileSystemDLL)))
return Sys_Error("IMaterialSystem::Init failed");
MaterialVideoMode_t mode;
memset(&mode, 0, sizeof(mode));
if(!m_pMaterialSystem->SetMode(m_hWnd, 0, mode, MATERIAL_VIDEO_MODE_WINDOWED))
return Sys_Error("IMaterialSystem::SetMode failed");
MaterialSystem_Config_t config;
InitMaterialSystemConfig(&config, fullPath);
if(!m_pMaterialSystem->ConfigInit(&config))
return Sys_Error("IMaterialSystem::ConfigInit failed. Make sure VPROJECT is set or use -game on the command line.");
GetClientRect((HWND)m_hWnd, &rect);
m_glnWidth= rect.right;
m_glnHeight = rect.bottom;
m_gldAspect = (float)m_glnWidth / m_glnHeight;
GetWindowRect( (HWND)m_hWnd, &rect );
m_centerx = (rect.left + rect.right) / 2;
m_centery = (rect.top + rect.bottom) / 2;
return true;
}
bool MaterialSystemApp::CreateMainWindow(int width, int height, int bpp, bool fullscreen)
{
HWND hwnd;
WNDCLASSEX wndclass;
DWORD dwStyle, dwExStyle;
int x, y, sx, sy, ex, ey, ty;
if ((hwnd = FindWindow(g_szAppName, g_szAppName)) != NULL)
{
SetForegroundWindow(hwnd);
return 0;
}
wndclass.cbSize = sizeof (wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndclass.lpfnWndProc = ::WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = (HINSTANCE)m_hInstance;
wndclass.hIcon = 0;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)COLOR_GRAYTEXT;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = g_szAppName;
wndclass.hIconSm = 0;
if (!RegisterClassEx (&wndclass))
{
MessageBox(NULL, "Window class registration failed.", "FATAL ERROR", MB_OK);
return FALSE;
}
if (fullscreen)
{
dwExStyle = WS_EX_TOPMOST;
dwStyle = WS_POPUP | WS_VISIBLE;
x = y = 0;
sx = m_width;
sy = m_height;
}
else
{
dwExStyle = 0;
//dwStyle = WS_CAPTION | WS_SYSMENU | WS_THICKFRAME; // Use this if you want a "normal" window
dwStyle = WS_CAPTION;
ex = GetSystemMetrics(SM_CXEDGE);
ey = GetSystemMetrics(SM_CYEDGE);
ty = GetSystemMetrics(SM_CYSIZE);
// Center the window on the screen
x = (m_DevInfo.width / 2) - ((m_width+(2*ex)) / 2);
y = (m_DevInfo.height / 2) - ((m_height+(2*ey)+ty) / 2);
sx = m_width+(2*ex);
sy = m_height+(2*ey)+ty;
/*
Check to be sure the requested window size fits on the screen and
adjust each dimension to fit if the requested size does not fit.
*/
if (sx >= m_DevInfo.width)
{
x = 0;
sx = m_DevInfo.width-(2*ex);
}
if (sy >= m_DevInfo.height)
{
y = 0;
sy = m_DevInfo.height-((2*ey)+ty);
}
}
if ((hwnd = CreateWindowEx (dwExStyle,
g_szAppName, // window class name
g_szAppName, // window caption
dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, // window style
x, // initial x position
y, // initial y position
sx, // initial x size
sy, // initial y size
NULL, // parent window handle
NULL, // window menu handle
(HINSTANCE)m_hInstance, // program instance handle
NULL)) // creation parameters
== NULL)
{
ChangeDisplaySettings(0, 0);
MessageBox(NULL, "Window creation failed.", "FATAL ERROR", MB_OK);
return FALSE;
}
m_hWnd = hwnd;
if (!InitMaterialSystem())
{
m_hWnd = NULL;
return FALSE;
}
ShowWindow((HWND)m_hWnd, m_iCmdShow);
UpdateWindow((HWND)m_hWnd);
SetForegroundWindow((HWND)m_hWnd);
SetFocus((HWND)m_hWnd);
return TRUE;
}
void MaterialSystemApp::RenderScene()
{
if(!m_pMaterialSystem)
return;
static DWORD lastTime = 0;
POINT cursorPoint;
float deltax = 0, deltay = 0, frametime;
DWORD newTime = GetTickCount();
DWORD deltaTime = newTime - lastTime;
if ( deltaTime > 1000 )
deltaTime = 0;
lastTime = newTime;
frametime = (float) ((double)deltaTime * 0.001);
g_Time = newTime;
if ( g_nCapture )
{
GetCursorPos( &cursorPoint );
SetCursorPos( m_centerx, m_centery );
deltax = (cursorPoint.x - m_centerx) * 0.1f;
deltay = (cursorPoint.y - m_centery) * -0.1f;
}
else
{
deltax = deltay = 0;
}
m_pMaterialSystem->ClearBuffers(true, true);
m_pMaterialSystem->MatrixMode(MATERIAL_PROJECTION);
m_pMaterialSystem->LoadIdentity();
m_pMaterialSystem->PerspectiveX(m_fov, m_gldAspect, m_NearClip, m_FarClip);
m_pMaterialSystem->MatrixMode(MATERIAL_VIEW);
m_pMaterialSystem->LoadIdentity();
AppRender( frametime, deltax, deltay );
m_pMaterialSystem->SwapBuffers();
}
void MaterialSystemApp::MouseCapture()
{
SetCapture( (HWND)m_hWnd );
ShowCursor(FALSE);
SetCursorPos( m_centerx, m_centery );
}
void MaterialSystemApp::MouseRelease()
{
ShowCursor(TRUE);
ReleaseCapture();
SetCursorPos( m_centerx, m_centery );
}
void MaterialSystemApp::GetParameters()
{
int count;
char *s, *tstring;
// Make a copy of the command line to count the parameters - strtok is destructive
tstring = (char *)malloc(sizeof(char)*(strlen(m_szCmdLine)+1));
strcpy(tstring, m_szCmdLine);
// Count the parameters
s = strtok(tstring, " ");
count = 1;
while (strtok(NULL, " ") != NULL)
{
count++;
}
free(tstring);
// Allocate "pockets" for the parameters
m_argv = (char **)malloc(sizeof(char*)*(count+1));
// Copy first parameter into the "pockets"
m_argc = 0;
s = strtok(m_szCmdLine, " ");
m_argv[m_argc] = (char *)malloc(sizeof(char)*(strlen(s)+1));
strcpy(m_argv[m_argc], s);
m_argc++;
// Copy the rest of the parameters
do
{
// get the next token
s = strtok(NULL, " ");
if (s != NULL)
{
// add it to the list
m_argv[m_argc] = (char *)malloc(sizeof(char)*(strlen(s)+1));
strcpy(m_argv[m_argc], s);
m_argc++;
}
}
while (s != NULL);
}
int MaterialSystemApp::FindNumParameter(const char *s, int defaultVal)
{
int i;
for (i = 0; i < (m_argc-1); i++)
{
if (stricmp(m_argv[i], s) == 0)
{
if (isdigits(m_argv[i+1]))
{
return(atoi(m_argv[i+1]));
}
else
{
return defaultVal;
}
}
}
return defaultVal;
}
bool MaterialSystemApp::FindParameter(const char *s)
{
int i;
for (i = 0; i < m_argc; i++)
{
if (stricmp(m_argv[i], s) == 0)
{
return true;
}
}
return false;
}
const char *MaterialSystemApp::FindParameterArg( const char *s )
{
int i;
for (i = 0; i < m_argc; i++)
{
if (stricmp(m_argv[i], s) == 0)
{
if( (i+1) < m_argc )
return m_argv[i+1];
else
return "";
}
}
return NULL;
}
void MaterialSystemApp::SetTitleText(const char *fmt, ...)
{
char str[4096];
va_list marker;
va_start(marker, fmt);
vsprintf(str, fmt, marker);
va_end(marker);
::SetWindowText((HWND)m_hWnd, str);
}
void MaterialSystemApp::MakeWindowTopmost()
{
::SetWindowPos((HWND)m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
}
void MaterialSystemApp::AppShutdown()
{
SendMessage( (HWND)m_hWnd, WM_CLOSE, 0, 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -