forked from JACoders/OpenJK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodviewview.cpp
635 lines (509 loc) · 14 KB
/
modviewview.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
// ModViewView.cpp : implementation of the CModViewView class
//
#include "stdafx.h"
#include "includes.h"
#include <mmsystem.h> // for timeGettime()
#include "ModView.h"
#include "ModViewDoc.h"
#include "ModViewTreeView.h"
#include "TEXT.H"
#include "drag.h"
#include "wintalk.h"
#include "textures.h"
#include "sof2npcviewer.h" // for gallery stuff
#include "clipboard.h" // for clipboard
//
#include "ModViewView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
bool DraggingMouse();
int g_iScreenWidth = 0;
int g_iScreenHeight = 0;
int g_iViewAreaMouseX = 0;
int g_iViewAreaMouseY = 0;
/////////////////////////////////////////////////////////////////////////////
// CModViewView
IMPLEMENT_DYNCREATE(CModViewView, CView)
BEGIN_MESSAGE_MAP(CModViewView, CView)
//{{AFX_MSG_MAP(CModViewView)
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_ERASEBKGND()
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_MOUSEWHEEL()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModViewView construction/destruction
void testcode(void);
CModViewView::CModViewView()
{
testcode();
}
CModViewView::~CModViewView()
{
}
BOOL CModViewView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CModViewView drawing
void CModViewView::OnDraw(CDC* pDC)
{
CModViewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (m_hRC && m_hDC)
{
if (wglMakeCurrent(m_hDC,m_hRC))
{
ModelList_Render(m_iWindowWidth, m_iWindowDepth);
SwapBuffers(pDC->GetSafeHdc());
// VERIFY(wglMakeCurrent(m_hDC,NULL));
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CModViewView printing
BOOL CModViewView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CModViewView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CModViewView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CModViewView diagnostics
#ifdef _DEBUG
void CModViewView::AssertValid() const
{
CView::AssertValid();
}
void CModViewView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CModViewDoc* CModViewView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CModViewDoc)));
return (CModViewDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CModViewView message handlers
BOOL CModViewView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
m_hRC = 0; // rendering context
m_hDC = 0; // device context
m_iWindowWidth = m_iWindowDepth = 0;
m_TimerHandle_Update100FPS = 0;
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
int CModViewView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
AppVars.hWnd = m_hWnd;
m_hDC = ::GetDC(m_hWnd);
m_hRC = GL_GenerateRC(m_hDC, true ); // bool bDoubleBuffer
m_TimerHandle_Update100FPS = SetTimer( th_100FPS, // UINT nIDEvent,
10, // UINT nElapse, (in milliseconds)
NULL // void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD)
);
if (!m_TimerHandle_Update100FPS)
{
ErrorBox("Warning: no Timer available for CModViewView update!");
}
OnceOnly_GLVarsInit();
return 0;
}
// app shutdown
void CModViewView::OnDestroy()
{
AppVars.bFinished = true; // may be useful
Media_Delete();
if (m_hRC) // if we had an opengl rendering context for this window
{
wglDeleteContext(m_hRC);
m_hRC = NULL;
}
if (m_hDC)
{
::ReleaseDC(m_hWnd,m_hDC);
m_hDC = NULL;
}
if (m_TimerHandle_Update100FPS)
{
bool bOk = !!KillTimer(m_TimerHandle_Update100FPS); // NZ return if good
if (bOk)
{
m_TimerHandle_Update100FPS = NULL;
}
else
{
ErrorBox("Error killing timer!"); // should never happen
}
}
CView::OnDestroy();
}
void CModViewView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
m_iWindowWidth = cx;
m_iWindowDepth = cy;
g_iScreenWidth = cx;
g_iScreenHeight= cy;
}
BOOL CModViewView::OnEraseBkgnd(CDC* pDC)
{
// actually this looks nicer to not do it at all, so...
//
/*
// return CView::OnEraseBkgnd(pDC);
CBrush backBrush(RGB(AppVars._R, AppVars._G, AppVars._B)); // Save old brush
CBrush* pOldBrush = pDC->SelectObject(&backBrush);
CRect rect;
pDC->GetClipBox(&rect); // Erase the area needed
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
pDC->SelectObject(pOldBrush);
*/
return TRUE;
}
int Sys_Milliseconds (void)
{
static bool bInitialised = false;
static int sys_timeBase;
int sys_curtime;
if (!bInitialised)
{
sys_timeBase = timeGetTime();
bInitialised = true;
}
sys_curtime = timeGetTime() - sys_timeBase;
return sys_curtime;
}
float GetFloatTime(void)
{
float fTime = (float)Sys_Milliseconds() / 1000.0f; // reduce to game-type seconds
return fTime;
}
// should be called roughly at 100 times per second... (which is enough for our needs when lerping is factored in)
//
int giGalleryItemsRemaining = 0; // declared here so StatusMessage can reference it (which is pretty tacky.... again).
CString strGalleryErrors;
CString strGalleryWarnings;
CString strGalleryInfo;
void Gallery_AddError(LPCSTR psText)
{
strGalleryErrors += psText;
}
void Gallery_AddInfo(LPCSTR psText)
{
strGalleryInfo += psText;
}
void Gallery_AddWarning(LPCSTR psText)
{
strGalleryWarnings += psText;
}
//
void CModViewView::OnTimer(UINT nIDEvent)
{
if (nIDEvent != th_100FPS)
{
CView::OnTimer(nIDEvent);
return;
}
// otherwise, it's one of our timer events, so...
{ // new bit, poll the remote control stuff 10 times per second (it's also done in OnIdle(), but that's not always fast enough when animating)
static float fTime = 0.0f;
float fTimeNow = GetFloatTime();
#define UPDATE_FRAMES_PER_SECOND 10.0f
if (fTimeNow - fTime > 1.0f/UPDATE_FRAMES_PER_SECOND)
{
fTime = fTimeNow;
// OutputDebugString(va("Time = %f seconds\n",GetFloatTime()));
if (WinTalk_HandleMessages())
{
// app exit requested
AppVars.bAnimate = qfalse; // groan... stop the animation so the app doesn't spend all it's time
// in the render loop. This allows the App::OnIdle() version
// of the Wintalk_HandleMessages() handler to get a look in,
// and therefore spot that an exit is being requested.
}
}
}
// if (!DraggingMouse())
{
if (ModelList_Animation())
{
// one or more models have updated frames (or lerping)...
//
Invalidate(false);
}
}
if (Gallery_Active())
{
extern bool gbInRenderer;
if (!gbInRenderer)
{
static bool bAlreadyHere = false;
if (!bAlreadyHere) // jic.
{
bAlreadyHere = true;
extern int giRenderCount;
static CString strCaption;
static CString strScript;
static bool bSnapshotTakingPlace = false;
if (!bSnapshotTakingPlace)
{
int iRemainingPlusOne = GalleryRead_ExtractEntry(strCaption, strScript);
if (iRemainingPlusOne) // because 0 would be fail/empty
{
giGalleryItemsRemaining = iRemainingPlusOne-1;
StatusMessage( va("( Gallery: %d remaining )", giGalleryItemsRemaining) );
OutputDebugString(va("\"%s\" (script len %d)\n",(LPCSTR)strCaption,strScript.GetLength()));
strScript += "\n";
string strOutputFileName( va("%s\\%s",scGetTempPath(),"temp.mvs") );
int iReturn = SaveFile(strOutputFileName.c_str(),(LPCSTR)strScript, strScript.GetLength());
if (iReturn != -1)
{
extern bool Document_ModelLoadPrimary(LPCSTR psFilename);
if (Document_ModelLoadPrimary(strOutputFileName.c_str()))
{
if (Model_Loaded())
{
ModelHandle_t hModel = AppVars.Container.hModel;
Model_Sequence_Lock( hModel, Gallery_GetSeqToLock(), true, false);
}
giRenderCount = 0;
bSnapshotTakingPlace = true;
}
}
}
else
{
// all done...
//
gbTextInhibit = false;
Gallery_Done();
StatusMessage( NULL );
//
// report...
//
CString strReport;
if (!strGalleryErrors.IsEmpty())
{
strReport += "====================== Errors: ===================\n\n";
strReport += strGalleryErrors;
strReport += "\n\n";
}
if (!strGalleryWarnings.IsEmpty())
{
strReport += "====================== Warnings: ===================\n\n";
strReport += strGalleryWarnings;
strReport += "\n\n";
}
if (!strGalleryInfo.IsEmpty())
{
strReport += "====================== Info: ===================\n\n";
strReport += strGalleryInfo;
strReport += "\n\n";
}
if (!strReport.IsEmpty())
{
strReport.Insert(0,"The following messages appeared during gallery-snapshots....\n\n");
}
else
{
strReport = va("All gallery-snapshots done\n\nOutput dir was: \"%s\\n",Gallery_GetOutputDir());
}
SendStringToNotepad(strReport,"gallery_report.txt");
}
}
else
{
if (giRenderCount == 2) // ... so it's rendered to back buffer for snapshot, and front for user
{
//
// generate a filename...
//
char sOutputFileName[MAX_PATH];
CString strBaseName(strCaption);
while (strBaseName.Replace("\t"," "));
while (strBaseName.Replace(" "," "));
sprintf(sOutputFileName, "%s\\%s.bmp",Gallery_GetOutputDir(),strBaseName);
ScreenShot(sOutputFileName,/*strCaption*/strBaseName);
BMP_Free();
bSnapshotTakingPlace = false; // trigger next snapshot
}
else
{
Invalidate(false); // cause another screen update until render count satisfied
}
}
bAlreadyHere = false;
}
}
}
}
bool sys_rbuttondown = false;
bool sys_lbuttondown = false;
bool sys_mbuttondown = false;
bool DraggingMouse()
{
return !!(sys_rbuttondown || sys_lbuttondown || sys_mbuttondown);
}
POINT DragStartPoint;
void CModViewView::OnLButtonDown(UINT nFlags, CPoint point)
{
GetCursorPos(&DragStartPoint);
start_drag( (mkey_enum)nFlags, DragStartPoint.x, DragStartPoint.y );
SetCapture();
ShowCursor(false);
sys_lbuttondown = true;
// CView::OnLButtonDown(nFlags, point);
}
void CModViewView::OnLButtonUp(UINT nFlags, CPoint point)
{
// CView::OnLButtonUp(nFlags, point);
if (sys_lbuttondown)
{
ReleaseCapture();
ShowCursor( true );
end_drag( (mkey_enum)nFlags, point.x, point.y );
sys_lbuttondown = false;
}
}
void CModViewView::OnRButtonDown(UINT nFlags, CPoint point)
{
GetCursorPos(&DragStartPoint);
start_drag( (mkey_enum)nFlags, DragStartPoint.x, DragStartPoint.y );
SetCapture();
ShowCursor(false);
sys_rbuttondown = true;
// CView::OnRButtonDown(nFlags, point);
}
void CModViewView::OnRButtonUp(UINT nFlags, CPoint point)
{
// CView::OnRButtonUp(nFlags, point);
if (sys_rbuttondown)
{
ReleaseCapture();
ShowCursor( true );
end_drag( (mkey_enum)nFlags, point.x, point.y );
sys_rbuttondown = false;
}
}
void CModViewView::OnMouseMove(UINT nFlags, CPoint point)
{
CView::OnMouseMove(nFlags, point);
GetCursorPos(&point);
if (!DraggingMouse())
{
ScreenToClient(&point);
g_iViewAreaMouseX = point.x;
g_iViewAreaMouseY = point.y;
return;
}
static int _i=0;
// OutputDebugString(va("(%d): x:(%d) y:(%d) (dragstart: %d %d)\n",_i++,point.x,point.y, DragStartPoint.x,DragStartPoint.y));
if (drag( (mkey_enum)nFlags, point.x, point.y ))
{
SetCursorPos(DragStartPoint.x,DragStartPoint.y);
//OutputDebugString("drag-painting\n");
Invalidate(false);
//UpdateWindow();
}
}
BOOL CModViewView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// wheel down = -ve, wheel up = +ve zDelta (test vals were +/-120, but just check sign)
//
OutputDebugString(va("zDelta = %d\n",zDelta));
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
typedef unsigned int MBChar_t;
MBChar_t *UTF8AsciiToMBC(LPCSTR psSource)
{
static vector <MBChar_t> Out;
Out.clear();
typedef struct
{
byte bRangeStart, bRangeStop;
byte b1stByteAND;
byte bBytesLong;
byte b2ndRangeStart,b2ndRangeStop;
} UTF8DecodeTable_t;
static const UTF8DecodeTable_t UTF8DecodeTable[] =
{
{ 0x00, 0x7F, 0x7F, 1, 0x00,0x00 },
{ 0xC0, 0xDF, 0x1F, 2, 0x80,0xBF },
{ 0xE0, 0xEF, 0x0F, 3, 0x80,0xBF },
{ 0xF0, 0xF7, 0x07, 4, 0x80,0xBF },
{ 0xF8, 0xFB, 0x03, 5, 0x80,0xBF },
{ 0xFC, 0xFD, 0x01, 6, 0x80,0xBF }
};
while (*psSource)
{
byte b = *psSource++;
MBChar_t m=0;
for (int i=0; i< (sizeof(UTF8DecodeTable) / sizeof(UTF8DecodeTable[0])); i++)
{
if (b >= UTF8DecodeTable[i].bRangeStart && b <= UTF8DecodeTable[i].bRangeStop)
{
m = (b & UTF8DecodeTable[i].b1stByteAND);
m <<= (UTF8DecodeTable[i].bBytesLong - 1)*6;
// supplementary chars...
//
for (int j=0; j<UTF8DecodeTable[i].bBytesLong-1; j++)
{
b = *psSource++;
if (b)
{
const int iShift = (UTF8DecodeTable[i].bBytesLong-2)-j;
m |= (b&63) << (iShift*6);
}
else
{
assert(0); // bad UTF-8 stream, bail
break;
}
}
Out.push_back(m);
break;
}
}
}
Out.push_back(0);
return &Out[0];
}
void testcode(void)
{
MBChar_t* p = UTF8AsciiToMBC(va("%c",0x4D));
p = UTF8AsciiToMBC(va("%c",0x79));
p = UTF8AsciiToMBC(va("%c",0x20));
p = UTF8AsciiToMBC(va("%c%c%c",0xE6,0xB2,0xB3));
p = UTF8AsciiToMBC(va("%c%c%c",0xE8,0xB1,0x9A));
int z=1;
}