forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatsysapp.cpp
960 lines (778 loc) · 20.2 KB
/
matsysapp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
/*
glapp.c - Simple OpenGL shell
There are several options allowed on the command line. They are:
-height : what window/screen height do you want to use?
-width : what window/screen width do you want to use?
-bpp : what color depth do you want to use?
-window : create a rendering window rather than full-screen
-fov : use a field of view other than 90 degrees
*/
#include "stdafx.h"
#pragma warning(disable:4305)
#pragma warning(disable:4244)
#include <windows.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <mmsystem.h>
#include "matsysapp.h"
#include "cmdlib.h"
#include "mathlib/mathlib.h"
#include "materialsystem/imaterialproxyfactory.h"
#include "filesystem.h"
#include "materialsystem/imaterialproxy.h"
#include "materialsystem/MaterialSystem_Config.h"
#include "tier0/icommandline.h"
#include "filesystem_tools.h"
#include "materialsystem/imesh.h"
#include "vstdlib/cvar.h"
static int g_nCapture = 0;
#define OSR2_BUILD_NUMBER 1111
#define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
SpewRetval_t MatSysAppSpewFunc( SpewType_t type, char const *pMsg )
{
printf( "%s", pMsg );
OutputDebugString( pMsg );
if( type == SPEW_ASSERT )
return SPEW_DEBUGGER;
else if( type == SPEW_ERROR )
return SPEW_ABORT;
else
return SPEW_CONTINUE;
}
class MatSysAppMaterialProxyFactory : public IMaterialProxyFactory
{
public:
virtual IMaterialProxy *CreateProxy( const char *proxyName )
{
CreateInterfaceFn clientFactory = Sys_GetFactoryThis();
if( !clientFactory )
{
return NULL;
}
// allocate exactly enough memory for the versioned name on the stack.
char proxyVersionedName[1024];
strcpy( proxyVersionedName, proxyName );
strcat( proxyVersionedName, IMATERIAL_PROXY_INTERFACE_VERSION );
IMaterialProxy *materialProxy;
materialProxy = ( IMaterialProxy * )clientFactory( proxyVersionedName, NULL );
if( !materialProxy )
{
return NULL;
}
return materialProxy;
}
virtual void DeleteProxy( IMaterialProxy *pProxy )
{
if( pProxy )
{
pProxy->Release();
}
}
};
MatSysAppMaterialProxyFactory g_MatSysAppMaterialProxyFactory;
MaterialSystemApp g_MaterialSystemApp;
float fAngle = 0.0f;
char *szFSDesc[] = { "Windowed", "Full Screen" };
extern "C" unsigned int g_Time;
unsigned int g_Time = 0;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
return g_MaterialSystemApp.WinMain(hInstance, hPrevInstance, szCmdLine, iCmdShow);
}
BOOL isdigits( char *s )
{
int i;
for (i = 0; s[i]; i++)
{
if ((s[i] > '9') || (s[i] < '0'))
{
return FALSE;
}
}
return TRUE;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
return g_MaterialSystemApp.WndProc(hwnd, iMsg, wParam, lParam);
}
// This function builds a list the screen resolutions supported by the display driver
static void BuildModeList(screen_res_t* &pResolutions, int &iResCount)
{
DEVMODE dm;
int mode;
mode = 0;
while(EnumDisplaySettings(NULL, mode, &dm))
{
mode++;
}
pResolutions = (screen_res_t *)malloc(sizeof(screen_res_t)*mode);
mode = 0;
while(EnumDisplaySettings(NULL, mode, &dm))
{
pResolutions[mode].width = dm.dmPelsWidth;
pResolutions[mode].height = dm.dmPelsHeight;
pResolutions[mode].bpp = dm.dmBitsPerPel;
pResolutions[mode].flags = dm.dmDisplayFlags;
pResolutions[mode].frequency = dm.dmDisplayFrequency;
mode++;
}
iResCount = mode;
}
bool Sys_Error(const char *pMsg, ...)
{
va_list marker;
char msg[4096];
va_start(marker, pMsg);
vsprintf(msg, pMsg, marker);
va_end(marker);
MessageBox(NULL, msg, "FATAL ERROR", MB_OK);
g_MaterialSystemApp.Term();
exit(1);
return false;
}
void con_Printf(const char *pMsg, ...)
{
char msg[2048];
va_list marker;
va_start(marker, pMsg);
vsprintf(msg, pMsg, marker);
va_end(marker);
OutputDebugString(msg);
}
bool MSA_IsKeyDown(char key)
{
return !!(GetAsyncKeyState(key) & 0x8000);
}
bool MSA_IsMouseButtonDown( int button )
{
if( button == MSA_BUTTON_LEFT )
return !!(GetAsyncKeyState(VK_LBUTTON) & 0x8000);
else
return !!(GetAsyncKeyState(VK_RBUTTON) & 0x8000);
}
void MSA_Sleep(unsigned long count)
{
if(count > 0)
Sleep(count);
}
static void MaterialSystem_Error( char *fmt, ... )
{
char str[4096];
va_list marker;
va_start(marker, fmt);
vsprintf(str, fmt, marker);
va_end(marker);
Sys_Error(str);
}
static void MaterialSystem_Warning( char *fmt, ... )
{
}
void InitMaterialSystemConfig(MaterialSystem_Config_t *pConfig)
{
memset( pConfig, 0, sizeof(*pConfig) );
// pConfig->screenGamma = 2.2f;
// pConfig->texGamma = 2.2;
// pConfig->overbright = 2;
pConfig->bAllowCheats = false;
// pConfig->bLinearFrameBuffer = false;
pConfig->skipMipLevels = 0;
// pConfig->lightScale = 1.0f;
pConfig->bFilterLightmaps = true;
pConfig->bFilterTextures = true;
pConfig->bMipMapTextures = true;
pConfig->nShowMipLevels = 0;
pConfig->bReverseDepth = false;
pConfig->bCompressedTextures = true;
// pConfig->bBumpmap = true;
pConfig->bShowSpecular = true;
pConfig->bShowDiffuse = true;
// pConfig->maxFrameLatency = 1;
pConfig->bDrawFlat = false;
// pConfig->bLightingOnly = false;
pConfig->bSoftwareLighting = false;
pConfig->bEditMode = false; // No, we're not in WorldCraft.
// pConfig->m_bForceTrilinear = false;
pConfig->m_nForceAnisotropicLevel = 0;
// pConfig->m_bForceBilinear = false;
}
/*
====================
CalcFov
====================
*/
float CalcFov (float fov_x, float width, float height)
{
float a;
float x;
if (fov_x < 1 || fov_x > 179)
fov_x = 90; // error, set to 90
x = width/tan(fov_x/360*M_PI);
a = atan (height/x);
a = a*360/M_PI;
return a;
}
MaterialSystemApp::MaterialSystemApp()
{
Clear();
}
MaterialSystemApp::~MaterialSystemApp()
{
Term();
}
void MaterialSystemApp::Term()
{
int i;
// Free the command line holder memory
if (m_argc > 0)
{
// Free in reverse order of allocation
for (i = (m_argc-1); i >= 0; i--)
{
free(m_argv[i]);
}
// Free the parameter "pockets"
free(m_argv);
}
// Free the memory that holds the video resolution list
if (m_pResolutions)
free(m_pResolutions);
if (m_hDC)
{
if (!ReleaseDC((HWND)m_hWnd, (HDC)m_hDC))
{
MessageBox(NULL, "ShutdownOpenGL - ReleaseDC failed\n", "ERROR", MB_OK);
}
m_hDC = NULL;
}
if (m_bFullScreen)
{
ChangeDisplaySettings( 0, 0 );
}
Clear();
}
void MaterialSystemApp::Clear()
{
m_pMaterialSystem = NULL;
m_hMaterialSystemInst = 0;
m_hInstance = 0;
m_iCmdShow = 0;
m_hWnd = 0;
m_hDC = 0;
m_bActive = false;
m_bFullScreen = false;
m_width = m_height = 0;
m_centerx = m_centery = 0;
m_bpp = 0;
m_bChangeBPP = false;
m_bAllowSoft = 0;
g_nCapture = 0;
m_szCmdLine = 0;
m_argc = 0;
m_argv = 0;
m_glnWidth = 0;
m_glnHeight = 0;
m_gldAspect = 0;
m_NearClip = m_FarClip = 0;
m_fov = 90;
m_pResolutions = 0;
m_iResCount = 0;
m_iVidMode = 0;
}
int MaterialSystemApp::WinMain(void *hInstance, void *hPrevInstance, char *szCmdLine, int iCmdShow)
{
MSG msg;
HDC hdc;
memset(&msg,0,sizeof(msg));
CommandLine()->CreateCmdLine( Plat_GetCommandLine() );
// Not changable by user
m_hInstance = hInstance;
m_iCmdShow = iCmdShow;
m_pResolutions = 0;
m_NearClip = 8.0f;
m_FarClip = 28400.0f;
// User definable
m_fov = 90.0f;
m_bAllowSoft = FALSE;
m_bFullScreen = TRUE;
// Get the current display device info
hdc = GetDC( NULL );
m_DevInfo.bpp = GetDeviceCaps(hdc, BITSPIXEL);
m_DevInfo.width = GetSystemMetrics(SM_CXSCREEN);
m_DevInfo.height = GetSystemMetrics(SM_CYSCREEN);
ReleaseDC(NULL, hdc);
// Parse the command line if there is one
m_argc = 0;
if (strlen(szCmdLine) > 0)
{
m_szCmdLine = szCmdLine;
GetParameters();
}
// Default to 640 pixels wide
m_width = FindNumParameter("-width", 640);
m_height = FindNumParameter("-height", 480);
m_bpp = FindNumParameter("-bpp", 32);
m_fov = FindNumParameter("-fov", 90);
// Check for windowed rendering
m_bFullScreen = FALSE;
if (FindParameter("-fullscreen"))
{
m_bFullScreen = TRUE;
}
// Build up the video mode list
BuildModeList(m_pResolutions, m_iResCount);
// Create the main program window, start up OpenGL and create our viewport
if (CreateMainWindow( m_width, m_height, m_bpp, m_bFullScreen) != TRUE)
{
ChangeDisplaySettings(0, 0);
MessageBox(NULL, "Unable to create main window.\nProgram will now end.", "FATAL ERROR", MB_OK);
Term();
return 0;
}
// Turn the cursor off for full-screen mode
if (m_bFullScreen == TRUE)
{
// Probably want to do this all the time anyway
ShowCursor(FALSE);
}
// We're live now
m_bActive = TRUE;
// Define this funciton to init your app
AppInit();
RECT rect;
GetWindowRect( (HWND)m_hWnd, &rect );
m_centerx = ( rect.left + rect.right ) / 2;
m_centery = ( rect.top + rect.bottom ) / 2;
// Begin the main program loop
while (m_bActive == TRUE)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
if (m_pMaterialSystem)
{
RenderScene();
}
}
if (m_bFullScreen == TRUE)
{
ShowCursor(TRUE);
}
// Release the parameter and video resolution lists
Term();
// Tell the app to cleanup.
AppExit();
return msg.wParam;
}
long MaterialSystemApp::WndProc(void *inhwnd, long iMsg, long wParam, long lParam)
{
if(inhwnd != m_hWnd)
{
return DefWindowProc((HWND)inhwnd, iMsg, wParam, lParam);
}
HWND hwnd = (HWND)inhwnd;
switch (iMsg)
{
case WM_CHAR:
switch(wParam)
{
case VK_ESCAPE:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
}
AppChar( wParam );
break;
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" );
}
const char *pPath = CommandLine()->ParmValue("-game");
if(!pPath)
{
// If they didn't specify -game on the command line, use VPROJECT.
CmdLib_InitFileSystem( "." );
pPath = ".";
}
else
{
CmdLib_InitFileSystem( pPath );
}
// BUGBUG: Can't figure out why this is broken. EXECUTABLE_PATH should already be there
// but it isn't so the shader system is computing texture memory on each run...
g_pFullFileSystem->AddSearchPath( "U:\\main\\game\\bin", "EXECUTABLE_PATH", PATH_ADD_TO_TAIL );
const char *pShaderDLL = FindParameterArg("-shaderdll");
char defaultShaderDLL[256];
if(!pShaderDLL)
{
strcpy(defaultShaderDLL, "shaderapidx9.dll");
pShaderDLL = defaultShaderDLL;
}
if(!m_pMaterialSystem->Init(pShaderDLL, &g_MatSysAppMaterialProxyFactory, CmdLib_GetFileSystemFactory()))
return Sys_Error("IMaterialSystem::Init failed");
MaterialSystem_Config_t config;
InitMaterialSystemConfig(&config);
config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, true );
config.SetFlag( MATSYS_VIDCFG_FLAGS_RESIZING, true );
if(!m_pMaterialSystem->SetMode(m_hWnd, config))
return Sys_Error("IMaterialSystem::SetMode failed");
m_pMaterialSystem->OverrideConfig(config, false);
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;
}
CMatRenderContextPtr pRenderContext(m_pMaterialSystem);
m_pMaterialSystem->BeginFrame(deltaTime);
pRenderContext->ClearBuffers(true, true);
pRenderContext->Viewport( 0, 0, m_width, m_height );
pRenderContext->MatrixMode(MATERIAL_PROJECTION);
pRenderContext->LoadIdentity();
pRenderContext->PerspectiveX(m_fov, m_gldAspect, m_NearClip, m_FarClip);
pRenderContext->MatrixMode(MATERIAL_VIEW);
pRenderContext->LoadIdentity();
pRenderContext->MatrixMode(MATERIAL_MODEL);
pRenderContext->LoadIdentity();
AppRender( frametime, deltax, deltay );
m_pMaterialSystem->SwapBuffers();
m_pMaterialSystem->EndFrame();
}
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 );
}
void MaterialSystemApp::QuitNextFrame()
{
PostMessage( (HWND)m_hWnd, WM_CLOSE, 0, 0 );
}