forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScenePreviewDlg.cpp
More file actions
200 lines (160 loc) · 4.73 KB
/
ScenePreviewDlg.cpp
File metadata and controls
200 lines (160 loc) · 4.73 KB
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "stdafx.h"
#include "hammer.h"
#include "ScenePreviewDlg.h"
#include "choreoscene.h"
#include "soundsystem.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
#define WM_SCENEPREVIEW_IDLE (WM_USER+1)
BEGIN_MESSAGE_MAP(CScenePreviewDlg, CDialog)
//{{AFX_MSG_MAP(CScenePreviewDlg)
ON_BN_CLICKED(IDCANCEL, OnCancel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CScenePreviewDlg::CScenePreviewDlg( CChoreoScene *pScene, const char *pFilename, CWnd* pParent /*=NULL*/ )
: CDialog(CScenePreviewDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CScenePreviewDlg)
m_pScene = pScene;
m_iLastEventPlayed = -2; // Don't do anything yet.
m_hExitThreadEvent = NULL;
m_hIdleEventHandledEvent = NULL;
m_hIdleThread = NULL;
V_strncpy( m_SceneFilename, pFilename, sizeof( m_SceneFilename ) );
}
CScenePreviewDlg::~CScenePreviewDlg()
{
EndThread();
delete m_pScene;
}
void CScenePreviewDlg::EndThread()
{
if ( m_hIdleThread )
{
SetEvent( m_hExitThreadEvent );
WaitForSingleObject( m_hIdleThread, INFINITE );
CloseHandle( m_hIdleThread );
CloseHandle( m_hExitThreadEvent );
CloseHandle( m_hIdleEventHandledEvent );
m_hIdleThread = m_hExitThreadEvent = m_hIdleEventHandledEvent = NULL;
}
}
BOOL CScenePreviewDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Setup the control showing the scene name.
CString str;
GetDlgItemText( IDC_SCENE_NAME, str );
str = str + " " + m_SceneFilename;
SetDlgItemText( IDC_SCENE_NAME, str );
CString strNone;
strNone.LoadString( IDS_NONE );
SetDlgItemText( IDC_CURRENT_SOUND, strNone );
SetDlgItemText( IDC_NEXT_SOUND, strNone );
m_iLastEventPlayed = -1;
m_flStartTime = Plat_FloatTime();
// Start on the first event..
for ( int i = 0; i < m_pScene->GetNumEvents(); i++ )
{
CChoreoEvent *e = m_pScene->GetEvent( i );
if ( e && e->GetType() == CChoreoEvent::SPEAK )
{
m_flStartTime -= e->GetStartTime();
break;
}
}
// Create our idle thread.
m_hExitThreadEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
m_hIdleEventHandledEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
m_hIdleThread = CreateThread( NULL, 0, &CScenePreviewDlg::StaticIdleThread, this, 0, NULL );
return TRUE;
}
DWORD CScenePreviewDlg::StaticIdleThread( LPVOID pParameter )
{
return ((CScenePreviewDlg*)pParameter)->IdleThread();
}
DWORD CScenePreviewDlg::IdleThread()
{
HANDLE handles[2] = {m_hExitThreadEvent, m_hIdleEventHandledEvent};
while ( 1 )
{
// Send the event to the window.
PostMessage( WM_SCENEPREVIEW_IDLE, 0, 0 );
DWORD ret = WaitForMultipleObjects( ARRAYSIZE( handles ), handles, false, INFINITE );
if ( ret == WAIT_OBJECT_0 || ret == WAIT_TIMEOUT )
return 0;
Sleep( 100 ); // Only handle idle 10x/sec.
}
}
void CScenePreviewDlg::OnIdle()
{
double flElapsed = Plat_FloatTime() - m_flStartTime;
// Find the next sound to play.
int iLastSound = -1;
for ( int i = 0; i < m_pScene->GetNumEvents(); i++ )
{
CChoreoEvent *e = m_pScene->GetEvent( i );
if ( !e || e->GetType() != CChoreoEvent::SPEAK )
continue;
if ( flElapsed > e->GetStartTime() )
iLastSound = i;
}
// Is there another sound to play in here?
if ( iLastSound >= 0 && iLastSound != m_iLastEventPlayed )
{
m_iLastEventPlayed = iLastSound;
// Play the current sound.
CChoreoEvent *pChoreoEvent = m_pScene->GetEvent( iLastSound );
const char *pSoundName = pChoreoEvent->GetParameters();
SoundType_t soundType;
int nIndex;
if ( g_Sounds.FindSoundByName( pSoundName, &soundType, &nIndex ) )
{
bool bRet = g_Sounds.Play( soundType, nIndex );
CString curSound = pSoundName;
if ( !bRet )
{
CString strErrorPlaying;
strErrorPlaying.LoadString( IDS_ERROR_PLAYING );
curSound += " " + strErrorPlaying;
}
SetDlgItemText( IDC_CURRENT_SOUND, curSound );
}
// Find the next sound event.
CString str;
str.LoadString( IDS_NONE );
for ( int i=iLastSound+1; i < m_pScene->GetNumEvents(); i++ )
{
CChoreoEvent *e = m_pScene->GetEvent( i );
if ( e && e->GetType() == CChoreoEvent::SPEAK )
{
str = e->GetParameters();
break;
}
}
SetDlgItemText( IDC_NEXT_SOUND, str );
}
}
LRESULT CScenePreviewDlg::DefWindowProc( UINT message, WPARAM wParam, LPARAM lParam )
{
// We handle the enter key specifically because the default combo box behavior is to
// reset the text and all this stuff we don't want.
if ( message == WM_SCENEPREVIEW_IDLE )
{
OnIdle();
SetEvent( m_hIdleEventHandledEvent );
return 0;
}
return CDialog::DefWindowProc( message, wParam, lParam );
}
void CScenePreviewDlg::OnCancel(void)
{
g_Sounds.StopSound();
EndThread();
EndDialog( 0 );
}