aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/IVideoD3DOSD.cpp
blob: 1804a7425b32b88054f905df63e0455d226ee8ee (plain) (blame)
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
959
960
961
962
963
964
965
966
#include "main.h"
#include "IVideoD3DOSD.h"
#include "resource.h"

extern wchar_t FileTitle[];
static HMODULE d3dx_lib = 0;

// For non-debug builds, comment out DXTraceW debug statements to 
// remove them completely
//#ifndef _DEBUG
#define DXTraceW //
//#endif

typedef HRESULT (WINAPI *D3DXCREATESPRITE)(LPDIRECT3DDEVICE9, LPD3DXSPRITE *);
typedef HRESULT (WINAPI *D3DXCREATEFONTW)(LPDIRECT3DDEVICE9, INT, UINT, UINT, UINT, BOOL, DWORD, DWORD, DWORD, DWORD, LPCWSTR, LPD3DXFONT *);
typedef HRESULT (WINAPI *D3DXCREATETEXTUREFROMRESOURCEEXW)(LPDIRECT3DDEVICE9, HMODULE, LPCWSTR, UINT, UINT, UINT, DWORD, D3DFORMAT, D3DPOOL, DWORD, DWORD, D3DCOLOR, D3DXIMAGE_INFO *, PALETTEENTRY *, LPDIRECT3DTEXTURE9 *);
D3DXCREATESPRITE pCreateSprite = NULL;
D3DXCREATEFONTW pCreateFontW = NULL;
D3DXCREATETEXTUREFROMRESOURCEEXW pCreateTextureFromResourceExW = NULL;

HMODULE FindD3DX9()
{
	if (d3dx_lib)
		return d3dx_lib;

	HMODULE d3dx9 = NULL;
	HANDLE hFind;
	WIN32_FIND_DATAW pfiledata;
	wchar_t systemDir[MAX_PATH] = {0};
	wchar_t libPath[MAX_PATH] = {0};
	GetSystemDirectoryW(systemDir, MAX_PATH);
	StringCchCatW(systemDir, MAX_PATH,L"\\d3dx9_");
	StringCchCopyW(libPath, MAX_PATH, systemDir);
	StringCchCatW(systemDir, MAX_PATH,L"*.dll");

	hFind = FindFirstFileW(systemDir,&pfiledata);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		BOOL more = true;
		int iHighVersion = 0;
		while (more)
		{
			wchar_t *start = wcsrchr(pfiledata.cFileName,L'_') + 1;
			int version = _wtoi(start);
			if (version <= 42 && version > iHighVersion)
				iHighVersion = version;
			more = FindNextFileW(hFind,&pfiledata);
		}

		FindClose(hFind);
		if (iHighVersion >= 24)
		{
			wchar_t finalD3DX9LibPath[MAX_PATH] = {0};
			StringCchPrintfW(finalD3DX9LibPath,MAX_PATH,L"%s%d%s",libPath,iHighVersion,L".dll");

			d3dx9 = LoadLibraryW(finalD3DX9LibPath);
		}
	}

	return d3dx9;
}

IVideoD3DOSD::IVideoD3DOSD(void)
{
	osdSprite = NULL;
	osdAtlasTexture = NULL;
	osdTimeFont = NULL;
	osdTitleFont = NULL;
	streaming = 0;
	titleFits = false;

	// Texture Src Coordinates for sprite images
	// Right and Bottom (last two) excluded from image
	SetRect(&osdBkgrndTextSrcCoords, 38, 534, 647, 635);

	SetRect(&osdPrevButtonNormalSrcCoords, 41, 17, 63, 31);
	SetRect(&osdPlayButtonNormalSrcCoords, 145, 14, 161, 35);
	SetRect(&osdPauseButtonNormalSrcCoords, 95, 16, 110, 33);
	SetRect(&osdStopButtonNormalSrcCoords, 195, 16, 210, 33);
	SetRect(&osdNextButtonNormalSrcCoords, 242, 17, 264, 31);
	SetRect(&osdProgressFrameNormalSrcCoords, 41, 226, 606, 235);		  
	SetRect(&osdVolumeFrameNormalSrcCoords, 41, 294, 111, 302);
	SetRect(&osdEndFSButtonNormalSrcCoords, 41, 140, 59, 158);
	SetRect(&osdMuteButtonNormalSrcCoords, 41, 416, 51, 428);
	SetRect(&osdProgressSliderNormalSrcCoords, 41, 343, 57, 361);
	SetRect(&osdVolumeSliderNormalSrcCoords, 41, 343, 57, 361);
	SetRect(&osdProgressProgressSrcCoords, 41, 274, 606, 282);  //hilited progress indicator
	SetRect(&osdVolumeProgressSrcCoords, 41, 314, 111, 322);    //hilited volume indicator

	SetRect(&osdPrevButtonClickSrcCoords, 41, 76, 63, 90);
	SetRect(&osdPlayButtonClickSrcCoords, 145, 73, 161, 94);
	SetRect(&osdPauseButtonClickSrcCoords, 95, 75, 110, 92);
	SetRect(&osdStopButtonClickSrcCoords, 195, 75, 210, 92);
	SetRect(&osdNextButtonClickSrcCoords, 242, 76, 264, 90);
	SetRect(&osdEndFSButtonClickSrcCoords, 41, 192, 59, 210);
	SetRect(&osdProgressSliderClickSrcCoords, 41, 385, 57, 403);
	SetRect(&osdVolumeSliderClickSrcCoords, 41, 385, 57, 403);

	SetRect(&osdPrevButtonDisabledSrcCoords, 41, 106, 63, 120);
	SetRect(&osdNextButtonDisabledSrcCoords, 242, 106, 264, 120);

	SetRect(&osdPrevButtonHiliteSrcCoords, 41, 46, 63, 60);
	SetRect(&osdPlayButtonHiliteSrcCoords, 145, 43, 161, 64);
	SetRect(&osdPauseButtonHiliteSrcCoords, 95, 45, 110, 62);
	SetRect(&osdStopButtonHiliteSrcCoords, 195, 45, 210, 62);
	SetRect(&osdNextButtonHiliteSrcCoords, 242, 46, 264, 60);
	SetRect(&osdEndFSButtonHiliteSrcCoords, 41, 166, 59, 184);
	SetRect(&osdProgressSliderHiliteSrcCoords, 41, 363, 57, 381);
	SetRect(&osdVolumeSliderHiliteSrcCoords, 41, 363, 57, 381);

	xScalingFactor = 1.0f; 
	yScalingFactor = 1.0f;

	for (int i = 0; i < 12; i++)
	{
		bState[i] = NORMAL;
	}

	mouseOver = NO_BUTTON;
	mouseLastOver = NO_BUTTON;
	mousePressed = NO_BUTTON;
	mouseLastPressed = NO_BUTTON;
	mouseDragging = false;

	displayTitle = NULL;
	marqueeTitleSrc = NULL;
	titleRestart = 0;
	dtFormat = 0;

	isInited = false;
	isReadyToDraw = false;
}

RECT IVideoD3DOSD::BuildHitRect(D3DXVECTOR3 position, RECT size)
{
	RECT hitRect;
	// casting float to long since I know the position vector will not be too big.
	hitRect.left = (long)position.x;
	hitRect.top = (long)position.y;
	hitRect.right = (long)position.x + size.right - size.left;
	hitRect.bottom = (long)position.y + size.bottom - size.top;
	return hitRect;
}

IVideoD3DOSD::~IVideoD3DOSD(void)
{
	if (osdSprite)
	{
		osdSprite->Release();
		osdSprite = NULL;
	}
	if (osdAtlasTexture)
	{
		osdAtlasTexture->Release();
		osdAtlasTexture = NULL;
	}
	if (marqueeTitleSrc)
	{
		delete [] marqueeTitleSrc;
		marqueeTitleSrc = NULL;
	}

	if (displayTitle)
	{
		delete [] displayTitle;
		displayTitle = NULL;
	}

	//if (d3dx_lib) 
	//{
	//	FreeLibrary(d3dx_lib);
	//	d3dx_lib = NULL;
	//}
}

void IVideoD3DOSD::SetScalingFactor(float fx, float fy)
{
	xScalingFactor = fx;
	yScalingFactor = fy;
}

void IVideoD3DOSD::CreateOSD(IDirect3DDevice9 * device)
{
	HRESULT hr;

	d3dx_lib = FindD3DX9();
	if (!d3dx_lib)
		return;

	pCreateFontW = (D3DXCREATEFONTW) GetProcAddress(d3dx_lib,"D3DXCreateFontW");
	pCreateSprite = (D3DXCREATESPRITE) GetProcAddress(d3dx_lib,"D3DXCreateSprite");
	pCreateTextureFromResourceExW = (D3DXCREATETEXTUREFROMRESOURCEEXW) GetProcAddress(d3dx_lib,"D3DXCreateTextureFromResourceExW");

	if (!pCreateFontW || !pCreateSprite || !pCreateTextureFromResourceExW)
		return;

	hr = pCreateSprite(device,&osdSprite);
	if (FAILED(hr))
	{
		DXTraceW(__FILE__, __LINE__, hr, L"CreateSprite Error", TRUE);
		return;
	}

	int font_size = -12 ;
	hr = pCreateFontW(
		device, 
		font_size, 
		0,		
		FW_NORMAL,
		1,                       
		0, 
		DEFAULT_CHARSET, 
		OUT_DEFAULT_PRECIS, 
		ANTIALIASED_QUALITY,	//DEFAULT_QUALITY, 
		DEFAULT_PITCH,
		L"Arial", 
		&osdTimeFont);
	if (FAILED(hr))
	{
		DXTraceW(__FILE__, __LINE__, hr, L"CreateFont (Time) Error", TRUE);
		return;
	}

	font_size = -16 ;
	hr = pCreateFontW(
		device, 
		font_size, 
		0,		
		FW_NORMAL,
		1,                       
		0, 
		DEFAULT_CHARSET, 
		OUT_DEFAULT_PRECIS, 
		ANTIALIASED_QUALITY,//DEFAULT_QUALITY, 
		DEFAULT_PITCH,
		L"Trebuchet MS", 
		&osdTitleFont);
	if (FAILED(hr))
	{
		DXTraceW(__FILE__, __LINE__, hr, L"CreateFont (Title) Error", TRUE);
		return;
	}

	ResetOSD(device);

	isInited = true;
}

void IVideoD3DOSD::UpdateOSD(HWND hWnd, VideoOutput *adjuster)
{
	// Position of sprites in screen coordinates
	// Center of sprite (where the position is mapped) is left to default to upper left corner
	// Note the Bkgrnd is positioned and then all other sprites are relative to that
	RECT clientRect;
	GetClientRect(hWnd,&clientRect);
	// Need to adjust the client rect to match the video aspect ration to fit the osd on the video.
	// adjuster->adjustAspect(clientRect);
	// width of the client area - width of the bkgrnd / 2 gives the space on each side
	// add that space to the offset of the left side of the client area.
	float xPosBkg = clientRect.left + 
		(((clientRect.right - clientRect.left) - (osdBkgrndTextSrcCoords.right - osdBkgrndTextSrcCoords.left))/2.0f);
	// width of the client area * .95 give the location of the bottom of the osd (i.e. 5% from bottom)
	// that minus the height of the bkgrnd gives the location of the upper left of background
	// add that space to the offset of the top of the client area.
	float yPosBkg = clientRect.top +
		(((clientRect.bottom - clientRect.top) * 1.0f) - (osdBkgrndTextSrcCoords.bottom - osdBkgrndTextSrcCoords.top));
	osdBkgrndPosition = D3DXVECTOR3(floor(xPosBkg), floor(yPosBkg), 0.0f);

	osdPrevButtonPosition = osdBkgrndPosition + D3DXVECTOR3(191.0f, 75.0f, 0.0f);
	osdPlayButtonPosition = osdBkgrndPosition + D3DXVECTOR3(246.0f, 72.0f, 0.0f);
	osdPauseButtonPosition = osdBkgrndPosition + D3DXVECTOR3(296.0f, 74.0f, 0.0f);
	osdStopButtonPosition = osdBkgrndPosition + D3DXVECTOR3(345.5f, 74.0f, 0.0f);
	osdNextButtonPosition = osdBkgrndPosition + D3DXVECTOR3(392.5f, 75.0f, 0.0f);
	osdProgressFramePosition = osdBkgrndPosition + D3DXVECTOR3(22.0f, 49.0f, 0.0f);
	osdVolumeFramePosition = osdBkgrndPosition + D3DXVECTOR3(518.0f, 76.0f, 0.0f);
	osdEndFSButtonPosition = osdBkgrndPosition + D3DXVECTOR3(583.0f, 19.0f, 0.0f);
	osdMuteButtonPosition = osdVolumeFramePosition + D3DXVECTOR3(-15.0f, -1.0f, 0.0f);
	osdProgressSliderPosition = osdProgressFramePosition + D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	osdVolumeSliderPosition = osdVolumeFramePosition + D3DXVECTOR3(0.0f, 0.0f, 0.0f);

	SetRect(&osdTimeRect,
		(long)osdBkgrndPosition.x + 26,
		(long)osdBkgrndPosition.y + 76,
		(long)osdBkgrndPosition.x + 98, 
		(long)osdBkgrndPosition.y + 85);

	SetRect(&osdTitleRect,
		(long)osdBkgrndPosition.x + 26,
		(long)osdBkgrndPosition.y + 17,
		(long)osdBkgrndPosition.x + 503, 
		(long)osdBkgrndPosition.y + 37);

	// Create Hit Test Rects for ui elements that don't move
	osdPrevButtonHit = BuildHitRect(osdPrevButtonPosition, osdPrevButtonNormalSrcCoords );
	osdPlayButtonHit = BuildHitRect(osdPlayButtonPosition, osdPlayButtonNormalSrcCoords );
	osdPauseButtonHit = BuildHitRect(osdPauseButtonPosition, osdPauseButtonNormalSrcCoords );
	osdStopButtonHit = BuildHitRect(osdStopButtonPosition, osdStopButtonNormalSrcCoords );
	osdNextButtonHit = BuildHitRect(osdNextButtonPosition, osdNextButtonNormalSrcCoords );
	osdEndFSButtonHit = BuildHitRect(osdEndFSButtonPosition, osdEndFSButtonNormalSrcCoords);
	osdProgressFrameHit = BuildHitRect(osdProgressFramePosition, osdProgressFrameNormalSrcCoords);
	osdVolumeFrameHit = BuildHitRect(osdVolumeFramePosition, osdVolumeFrameNormalSrcCoords);

	streaming = (in_getlength() < 0) || !in_mod || !in_mod->is_seekable;
	if (streaming)
	{
		bState[PREV_BUTTON] = DISABLED;
		bState[NEXT_BUTTON] = DISABLED;
		bState[PROGRESS_FRAME] = DISABLED;
		bState[PROGRESS_SLIDER] = DISABLED;
	}

	// Find out if the title will fit in the UI space for it
	RECT tempTitleRect = osdTitleRect;
	osdTitleFont->DrawTextW(NULL, FileTitle, -1, &tempTitleRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOCLIP | DT_CALCRECT, D3DCOLOR_XRGB(255,255,255));
	if (tempTitleRect.right <= osdTitleRect.right)
	{
		// The title fits, just use it
		titleFits = true;
		displayTitle = FileTitle;
		dtFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOCLIP;
	} else 
	{
		// title will not fit, we need to set up a marquee.
		//
		// a string with two copies of the title makes it easier to process
		// sizeNeeded, in chars, includes one space, five dots, 
		//       one space and 1 extra for null.
		size_t sizeNeeded = (lstrlenW(FileTitle)*2) + 8;
		marqueeTitleSrc = new wchar_t[sizeNeeded];
		displayTitle = new wchar_t[sizeNeeded];
		titleRestart = lstrlenW(FileTitle);
		StringCchPrintfW(marqueeTitleSrc, sizeNeeded, L"%s ..... %s", FileTitle, FileTitle);
		titleFits = false;
		dtFormat = DT_RIGHT | DT_TOP | DT_SINGLELINE;
	}

	isReadyToDraw = true;
}
#ifdef _DEBUG
#define DRAW_OSD_SET_ERROR(x) draw_osd_error=x
#else
#define DRAW_OSD_SET_ERROR(x)
#endif

void IVideoD3DOSD::DrawOSD(IDirect3DDevice9 * device)
{
	HRESULT hr;
	D3DXVECTOR3 sliderCenter(8.0f, 8.0f, 0.0f);

	const wchar_t *draw_osd_error;
	hr = osdSprite->Begin(D3DXSPRITE_ALPHABLEND);
	if (FAILED(hr))
	{

		DXTraceW(__FILE__, __LINE__, hr, L"Sprite Begin Error", TRUE);
		return ;
	}

	// Doing Scaling of sprites here
	// If we do translations and/or rotations we'll have to do a more 
	// robust hit test (picking) since the current one assumes a rectangular 
	// shape based on screen coordinates.  Only scaling is currently handled
	// in the hit test.
	//D3DXMATRIX scalingMatrix;
	//osdSprite->SetTransform(D3DXMatrixScaling(&scalingMatrix, 1.0f /*xScalingFactor*/, 1.0f /*yScalingFactor*/, 0.0f));

	hr = osdSprite->Draw(osdAtlasTexture, &osdBkgrndTextSrcCoords, NULL, &osdBkgrndPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Background Sprite Draw Error");
		goto DrawOSD_Error;
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(PREV_BUTTON), NULL, &osdPrevButtonPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Prev Button Sprite Draw Error");
		goto DrawOSD_Error;
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(PLAY_BUTTON), NULL, &osdPlayButtonPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr)) 
	{
		DRAW_OSD_SET_ERROR(L"Play Button Sprite Draw Error");
		goto DrawOSD_Error;
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(PAUSE_BUTTON), NULL, &osdPauseButtonPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Pause Button Sprite Draw Error");
		goto DrawOSD_Error;
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(STOP_BUTTON), NULL, &osdStopButtonPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Stop Button Sprite Draw Error");
		goto DrawOSD_Error;
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(NEXT_BUTTON), NULL, &osdNextButtonPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Next Button Sprite Draw Error");
		goto DrawOSD_Error;
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(PROGRESS_FRAME), NULL, &osdProgressFramePosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Progress Frame Sprite Draw Error");
		goto DrawOSD_Error;
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(VOLUME_FRAME), NULL, &osdVolumeFramePosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Volume Frame Sprite Draw Error");
		goto DrawOSD_Error;
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(ENDFS_BUTTON), NULL, &osdEndFSButtonPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"EndFS Button Sprite Draw Error");
		goto DrawOSD_Error;
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(MUTE_BUTTON), NULL, &osdMuteButtonPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Mute Button Sprite Draw Error");
		goto DrawOSD_Error;
	}

	if (playing && !streaming && !mouseDragging) // if mouseDragging we may be repositioning the slider, don't set it back till Lmouseup
	{
		// calculate the relative position of the slider
		float ppercent = (in_getouttime() / 1000.0f) / in_getlength();
		float sizeOfProgFrame = (float)osdProgressFrameHit.right - osdProgressFrameHit.left - 0;
		// position the progress slider
		osdProgressSliderPosition.x = osdProgressFramePosition.x + (sizeOfProgFrame * ppercent);
		// Now build the hit rect based on the new position.
		osdProgressSliderHit = BuildHitRect(osdProgressSliderPosition + D3DXVECTOR3(0.0f,0.0f,0.0f), osdProgressSliderNormalSrcCoords);
	}


	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(PROGRESS_SLIDER), &sliderCenter, &osdProgressSliderPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Progress Slider Sprite Draw Error");
		goto DrawOSD_Error;
	}

	// Build the progress hilite line by drawing only a certain amount (width) of the texture.
	RECT seekProgress;
	// The progress hilite line goes on top of progress frame
	seekProgress = osdProgressProgressSrcCoords;
	// The width of the progress hilite line is determined by the location of the slider
	seekProgress.right = seekProgress.left + (osdProgressSliderHit.left - osdProgressFrameHit.left + 0);

	hr = osdSprite->Draw(osdAtlasTexture, 
		&seekProgress,
		NULL, 
		&osdProgressFramePosition, 
		D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Seek Progress Sprite Draw Error");
		goto DrawOSD_Error;
	}

	if (!mouseDragging) // if mouseDragging we may be repositioning the slider, don't set it back till Lmouseup
	{
		// calculate the relative position of the slider
		float vpercent = config_volume / 255.0f;
		float sizeOfVolFrame = (float)osdVolumeFrameHit.right - osdVolumeFrameHit.left - 0;
		// position the volume slider
		osdVolumeSliderPosition.x = (osdVolumeFramePosition.x) + (sizeOfVolFrame * vpercent);
		// Now build the hit rect based on the new position.
		osdVolumeSliderHit = BuildHitRect(osdVolumeSliderPosition + D3DXVECTOR3(0.0f,0.0f,0.0f), osdVolumeSliderNormalSrcCoords);
	}
	hr = osdSprite->Draw(osdAtlasTexture, GetTextCoords(VOLUME_SLIDER), &sliderCenter, &osdVolumeSliderPosition, D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Volume SLider Sprite Draw Error");
		goto DrawOSD_Error;
	}

	// Build the volume hilite line by drawing only a certain amount (width) of the texture.
	RECT volProgress;
	// The volume hilite line goes on top of volume frame
	volProgress = osdVolumeProgressSrcCoords;
	// The width of the volume hilite line is determined by the location of the slider
	volProgress.right = volProgress.left + (osdVolumeSliderHit.left - osdVolumeFrameHit.left + 0);

	hr = osdSprite->Draw(osdAtlasTexture, 
		&volProgress,
		NULL, 
		&osdVolumeFramePosition, 
		D3DCOLOR_XRGB(255,255,255));
	if (FAILED(hr))
	{
		DRAW_OSD_SET_ERROR(L"Volume Progress Sprite Draw Error");
		goto DrawOSD_Error;
	}


	if (osdTimeFont)
	{
		int seconds_in = in_getouttime() / 1000;
		int time_to_go = in_getlength();

		wchar_t timerText[256] = {0};
		if (streaming)
			StringCbPrintfW(timerText,sizeof(timerText),L"%.2u:%.2u",seconds_in /60,seconds_in % 60);
		else
			StringCbPrintfW(timerText,sizeof(timerText),L"%.2u:%.2u / %.2u:%.2u",seconds_in /60,seconds_in % 60,time_to_go / 60, time_to_go % 60);
		osdTimeFont->DrawTextW(osdSprite, timerText, -1, &osdTimeRect, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOCLIP, D3DCOLOR_XRGB(163,164,167)); // #A3A4A7
	}

	if (osdTitleFont)
	{
		// found possibility that user can pause in full screen and remove items from playlist
		// This prevents a crash by not trying to display a title.
		if (lstrlenW(FileTitle) > 0)
		{
			if (!titleFits)
			{
				// title does not fit, build marquee
				DWORD now = GetTickCount();
				static DWORD then;
				if (now - then > 250)  // slow it down so people can read it.
				{
					static int charCount = 2; // start with the first char + 1 for null
					lstrcpynW(displayTitle,marqueeTitleSrc,charCount);
					charCount++;
					if (charCount > lstrlenW(marqueeTitleSrc))
						charCount = lstrlenW(FileTitle);
					then = now;
				}
			}

			osdTitleFont->DrawTextW(osdSprite, displayTitle, -1, &osdTitleRect, dtFormat, D3DCOLOR_XRGB(204,204,204)); // #cccccc
		}
	}

	hr = osdSprite->End();
	if (FAILED(hr))
	{
		DXTraceW(__FILE__, __LINE__, hr, L"Sprite End Error", TRUE);
		return;
	}

	return;
DrawOSD_Error:
	DXTraceW(__FILE__, __LINE__, hr, draw_osd_error, TRUE);
	osdSprite->End();
}

RECT *IVideoD3DOSD::GetTextCoords(UI_ELEM item)
{ 
	switch (item) 
	{
	case PREV_BUTTON :
		switch (bState[item]) 
		{
		case NORMAL :
			return &osdPrevButtonNormalSrcCoords;
			break;
		case CLICKED :
			return &osdPrevButtonClickSrcCoords;
			break;
		case HILITE :
			return &osdPrevButtonHiliteSrcCoords;
			break;
		case DISABLED :
			return &osdPrevButtonDisabledSrcCoords;
			break;
		}

		break;
	case PLAY_BUTTON :
		switch (bState[item]) 
		{
		case NORMAL :
		case DISABLED :
			return &osdPlayButtonNormalSrcCoords;
			break;
		case CLICKED :
			return &osdPlayButtonClickSrcCoords;
			break;
		case HILITE :
			return &osdPlayButtonHiliteSrcCoords;
			break;
		}

		break;
	case PAUSE_BUTTON :
		switch (bState[item]) 
		{
		case NORMAL :
		case DISABLED :
			return &osdPauseButtonNormalSrcCoords;
			break;
		case CLICKED :
			return &osdPauseButtonClickSrcCoords;
			break;
		case HILITE :
			return &osdPauseButtonHiliteSrcCoords;
			break;
		}

		break;
	case STOP_BUTTON :
		switch (bState[item]) 
		{
		case NORMAL :
		case DISABLED :
			return &osdStopButtonNormalSrcCoords;
			break;
		case CLICKED :
			return &osdStopButtonClickSrcCoords;
			break;
		case HILITE :
			return &osdStopButtonHiliteSrcCoords;
			break;
		}

		break;
	case NEXT_BUTTON :
		switch (bState[item]) 
		{
		case NORMAL :
			return &osdNextButtonNormalSrcCoords;
			break;
		case CLICKED :
			return &osdNextButtonClickSrcCoords;
			break;
		case HILITE :
			return &osdNextButtonHiliteSrcCoords;
			break;
		case DISABLED :
			return &osdNextButtonDisabledSrcCoords;
			break;
		}

		break;
	case ENDFS_BUTTON :
		switch (bState[item]) 
		{
		case NORMAL :
		case DISABLED :
			return &osdEndFSButtonNormalSrcCoords;
			break;
		case CLICKED :
			return &osdEndFSButtonClickSrcCoords;
			break;
		case HILITE :
			return &osdEndFSButtonHiliteSrcCoords;
			break;
		}

		break;
	case MUTE_BUTTON :
		return &osdMuteButtonNormalSrcCoords;
		//switch (bState[item]) 
		//{
		//case NORMAL :
		//case DISABLED :
		//	return &osdMuteButtonNormalSrcCoords;
		//	break;
		//case CLICKED :
		//	return &osdMuteButtonClickSrcCoords;
		//	break;
		//case HILITE :
		//	return &osdMuteButtonHiliteSrcCoords;
		//	break;
		//}

		break;
	case PROGRESS_FRAME :
		return &osdProgressFrameNormalSrcCoords;

		break;
	case VOLUME_FRAME :
		return &osdVolumeFrameNormalSrcCoords;

		break;
	case PROGRESS_SLIDER :
		switch (bState[item]) 
		{
		case NORMAL :
		case DISABLED :
			return &osdProgressSliderNormalSrcCoords;
			break;
		case CLICKED :
			return &osdProgressSliderClickSrcCoords;
			break;
		case HILITE :
			return &osdProgressSliderHiliteSrcCoords;
			break;
		}

		break;
	case VOLUME_SLIDER :
		switch (bState[item]) 
		{
		case NORMAL :
		case DISABLED :
			return &osdVolumeSliderNormalSrcCoords;
			break;
		case CLICKED :
			return &osdVolumeSliderClickSrcCoords;
			break;
		case HILITE :
			return &osdVolumeSliderHiliteSrcCoords;
			break;
		}

		break;
	}

	return NULL;
}

void IVideoD3DOSD::LostOSD()
{
	if (osdSprite)
		osdSprite->OnLostDevice();
	if (osdTimeFont)
		osdTimeFont->OnLostDevice();
	if (osdTitleFont)
		osdTitleFont->OnLostDevice();
	if (osdAtlasTexture)
	{
		osdAtlasTexture->Release();
		osdAtlasTexture = 0;
	}
}

void IVideoD3DOSD::ResetOSD(IDirect3DDevice9 * device)
{
	if (osdSprite)
		osdSprite->OnResetDevice();
	if (osdTimeFont)
		osdTimeFont->OnResetDevice();
	if (osdTitleFont)
		osdTitleFont->OnResetDevice();

	if (device)
	{
		HRESULT hr;
		hr = pCreateTextureFromResourceExW(
			device,
			NULL,	// HMODULE
			MAKEINTRESOURCEW(IDB_OSD),			// Our texture image atlas
			D3DX_DEFAULT,				// width
			D3DX_DEFAULT,				// height
			1,							// MIP levels
			0,							// usage
			D3DFMT_UNKNOWN,				// get format from file
			D3DPOOL_DEFAULT,			// mem pool
			D3DX_DEFAULT,				// filter
			D3DX_DEFAULT,				// MIP filter
			0,							// transparent color key
			NULL,						// image info struct
			NULL,						// palette
			&osdAtlasTexture);				// the returned texture, if success
		if (FAILED(hr))
		{
			DXTraceW(__FILE__, __LINE__, hr, L"CreateTextureFromFileEx Error", TRUE);
			return;
		}
	}

}

bool IVideoD3DOSD::MouseDown(int xpt, int ypt, WPARAM wParam)
{
	Show();	
	// mouseLastPressed is used during mouse up to verify that the up is on the 
	// same UI element as the mouse down.
	mouseLastPressed = HitTest((float)xpt, (float)ypt);
	bState[mouseLastPressed] = CLICKED;
	return false;
}
bool IVideoD3DOSD::MouseMove(int ixpt, int iypt, WPARAM wParam)
{
	static int saved_ixpt;
	static int saved_iypt;

	// Need to check whether the mouse cursor is still in the same place.
	// Evidently, WM_MOUSEMOVE can get triggered for other reasons than
	// actually moving the mouse, per Microsoft blogs
	// This code was triggering with IM and EMAIL notifications without
	// moving the mouse.
	if (ixpt == saved_ixpt && iypt == saved_iypt)
		return false;

	saved_ixpt = ixpt;
	saved_iypt = iypt;

	Show();	

	// Change input ints to floats so later calculations are more precise.
	float xpt = (float)ixpt;
	float ypt = (float)iypt;

	mouseOver = HitTest((float)xpt, (float)ypt);
	if (wParam & MK_LBUTTON) //dragging
	{
		mouseDragging = true;
		if (mouseLastPressed == VOLUME_SLIDER)
		{
			if (xpt < (osdVolumeFrameHit.left)) xpt = (float) osdVolumeFrameHit.left;
			else if (xpt > (osdVolumeFrameHit.right) - 0) xpt = (float) osdVolumeFrameHit.right - 0;

			//move the volume slider
			osdVolumeSliderPosition.x = xpt; 
			// slider uses center as center
			osdVolumeSliderHit = BuildHitRect(osdVolumeSliderPosition + D3DXVECTOR3(0.0f,0.0f,0.0f),osdVolumeSliderNormalSrcCoords);
		}
		else if (mouseLastPressed == PROGRESS_SLIDER)
		{			
			if (xpt < osdProgressFrameHit.left) xpt = (float)osdProgressFrameHit.left;
			else if (xpt > (osdProgressFrameHit.right)) xpt = (float)osdProgressFrameHit.right;

			//move the progress slider
			osdProgressSliderPosition.x = xpt; 
			osdProgressSliderHit = BuildHitRect(osdProgressSliderPosition + D3DXVECTOR3(0.0f,0.0f,0.0f),osdProgressSliderNormalSrcCoords);
		}
	} else // no click, just mousemove 
	{
		mouseDragging = false;
		if (mouseLastOver != mouseOver)
		{
			if (bState[mouseLastOver] == HILITE) 
				bState[mouseLastOver] = NORMAL;
			if (bState[mouseOver] == NORMAL) 
				bState[mouseOver] = HILITE;
			mouseLastOver = mouseOver;
		}
	}

	return false;
}

bool IVideoD3DOSD::MouseUp(int xpt, int ypt, WPARAM wParam)
{
	mousePressed = HitTest((float)xpt, (float)ypt);

	bState[mouseLastPressed] = NORMAL;

	if (bState[mousePressed] == HILITE) 
		bState[mousePressed] = NORMAL;

	mouseDragging = false;

	switch (mousePressed)
	{
	case ENDFS_BUTTON :
		if (mouseLastPressed == ENDFS_BUTTON)
			return true;  // end full screen
		break;
	case PREV_BUTTON : 
		if (mouseLastPressed == PREV_BUTTON)
			PostMessageW(hMainWindow, WM_COMMAND, WINAMP_BUTTON1, 0);
		break;
	case PLAY_BUTTON :
		if (mouseLastPressed == PLAY_BUTTON)
			PostMessageW(hMainWindow, WM_COMMAND, WINAMP_BUTTON2, 0);
		break;
	case PAUSE_BUTTON :
		if (mouseLastPressed == PAUSE_BUTTON)
			PostMessageW(hMainWindow, WM_COMMAND, WINAMP_BUTTON3, 0);
		break;
	case STOP_BUTTON :
		if (mouseLastPressed == STOP_BUTTON)
			PostMessageW(hMainWindow, WM_COMMAND, WINAMP_BUTTON4, 0);
		break;
	case NEXT_BUTTON :
		if (mouseLastPressed == NEXT_BUTTON)
			PostMessageW(hMainWindow, WM_COMMAND, WINAMP_BUTTON5, 0);
		break;
	default :
		{
			// If not a button, check the sliders
			// The successful use of the sliders should not depend on 
			// releasing the mouse inside the frame, which may be very small.
			switch (mouseLastPressed)
			{
			case PROGRESS_SLIDER :
			case PROGRESS_FRAME :
				{
					float xIntoFrame = (float)xpt - osdProgressFrameHit.left;
					// -8 is half the width of the slider
					float rightMaxOfFrame = (float)osdProgressFrameHit.right - 0;
					float leftMinOfFrame = (float)osdProgressFrameHit.left;
					float sizeOfFrame = rightMaxOfFrame - leftMinOfFrame;
					float t = xIntoFrame / sizeOfFrame;
					if (t < 0) 
						t = 0;
					if (t > 1) 
						t = 1;

					int len = in_getlength();			
					in_seek((int)(t*len*1000));
				}
				break;
			case VOLUME_SLIDER :
			case VOLUME_FRAME :
				{
					float xIntoFrame      = (float)xpt - (osdVolumeFrameHit.left);
					// -8 is half the width of the slider
					float rightMaxOfFrame = (float)osdVolumeFrameHit.right - 0;
					float leftMinOfFrame  = (float)osdVolumeFrameHit.left;
					float sizeOfFrame     = rightMaxOfFrame - leftMinOfFrame;
					float t               = xIntoFrame / sizeOfFrame;

					if (t < 0) 
						t = 0;
					if (t > 1) 
						t = 1;

					unsigned char v = (unsigned char)(t * 255);

					config_volume = v;

					in_setvol(v);
				}
				break;
			}
		}
		break;
	}
	mouseLastPressed = NO_BUTTON;
	return false;
}

IVideoD3DOSD::UI_ELEM IVideoD3DOSD::HitTest(float xpt, float ypt)
{
	if (PointInRect(xpt, ypt, osdPrevButtonHit))
		return PREV_BUTTON;
	else if (PointInRect(xpt, ypt, osdPlayButtonHit))
		return PLAY_BUTTON;
	else if (PointInRect(xpt, ypt, osdPauseButtonHit))
		return PAUSE_BUTTON;
	else if (PointInRect(xpt, ypt, osdStopButtonHit))
		return STOP_BUTTON;
	else if (PointInRect(xpt, ypt, osdNextButtonHit))
		return NEXT_BUTTON;
	else if (PointInRect(xpt, ypt, osdEndFSButtonHit))
		return ENDFS_BUTTON;
	else if (PointInRect(xpt, ypt, osdVolumeSliderHit))
		return VOLUME_SLIDER;
	else if (PointInRect(xpt, ypt, osdProgressSliderHit))
		return PROGRESS_SLIDER;
	else if (PointInRect(xpt, ypt, osdProgressFrameHit))
		return PROGRESS_FRAME;
	else if (PointInRect(xpt, ypt, osdVolumeFrameHit))
		return VOLUME_FRAME;
	else
		return NO_BUTTON;
}

bool IVideoD3DOSD::PointInRect(float x, float y, RECT testRect)
{
	if ((x >= testRect.left) && (x <= testRect.right) &&
		(y >= testRect.top) && (y <= testRect.bottom))
		return true;
	else
		return false;
}