forked from YoYoGames/GameMaker-HTML5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyyGraphics.js
More file actions
2030 lines (1741 loc) · 65.7 KB
/
Copy pathyyGraphics.js
File metadata and controls
2030 lines (1741 loc) · 65.7 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
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// **********************************************************************************************************************
//
// Copyright (c)2011, YoYo Games Ltd. All Rights reserved.
//
// File: yyGraphics.js
// Created: 19/02/2011
// Author: Mike
// Project: HTML5
// Description: A set of global functions to control graphics. (no longer a class)
//
// Date Version BY Comment
// ----------------------------------------------------------------------------------------------------------------------
// 19/02/2011 V1.0 MJD 1st version
// 20/06/2011 V1.1 MJD Removed all "class-ness" of it due to javascript speed issues - doesnt like with().
//
// **********************************************************************************************************************
var g_Canvas;
var g_Textures;
var g_pTextureOffsets = null;
var g_DisplayWidth = 1024;
var g_DisplayHeight = 768;
var g_DisplayScaleX = 1;
var g_DisplayScaleY = 1;
var g_clipx = 0;
var g_clipy = 0;
var g_clipw = 0;
var g_cliph = 0;
var g_worldx = 0;
var g_worldy = 0;
var g_worldw = 0;
var g_worldh = 0;
var g_transform = [];
var g_GlobalFrameCount = 0;
var g_CacheWhite = 0xffffff;
var Graphics_TextureDrawSimple;
var Graphics_TextureDrawTiled;
var Graphics_SetViewPort;
var Graphics_SetViewArea;
var Graphics_SetTransform;
var Graphics_ClearScreen;
var Graphics_PushTransform;
var Graphics_PushMatrix;
var Graphics_DrawPart;
var Graphics_DrawComment;
var Graphics_StartFrame;
var Graphics_Restore;
var Graphics_Save;
var Graphics_SetViewAreaTransform;
var Graphics_TextureDraw;
var Graphics_TextureDrawWH;
var Graphics_EndFrame;
var Graphics_SWFDraw;
var Graphics_SWFDrawObject;
var Graphics_VectorSpriteDraw;
var Graphics_VectorSpriteDrawObject;
var Graphics_DrawText;
// whether to use texture interpolation in 2d context
var Graphics_Interpolation = true;
function DisplayWidth(){ return g_DisplayWidth; }
function DisplayHeight(){ return g_DisplayHeight; }
// #############################################################################################
/// Constructor: <summary>
/// Add "our" canvas functions.
/// This helps obfuscation, and will shrink the code base.
/// </summary>
///
/// In: <param name="_canvas">The canvas to "update"</param>
///
// #############################################################################################
function Graphics_AddCanvasFunctions(_graphics)
{
if( !_graphics ) return;
_graphics._transform = _graphics.transform;
_graphics._setTransform = _graphics.setTransform;
_graphics._save = _graphics.save;
_graphics._restore = _graphics.restore;
_graphics._fillRect = _graphics.fillRect;
_graphics._strokeRect = _graphics.strokeRect;
_graphics._beginPath = _graphics.beginPath;
_graphics._arc = _graphics.arc;
_graphics._stroke = _graphics.stroke;
_graphics._closePath = _graphics.closePath;
_graphics.lineWidth = _graphics.lineWidth;
_graphics._moveTo = _graphics.moveTo;
_graphics._lineTo = _graphics.lineTo;
_graphics._fill = _graphics.fill;
_graphics._drawImage = _graphics.drawImage;
_graphics._getImageData = _graphics.getImageData;
_graphics._createImageData = _graphics.createImageData;
_graphics._putImageData = _graphics.putImageData;
_graphics._clip = _graphics.clip;
_graphics._rect = _graphics.rect;
/*_graphics._ = _graphics.;
_graphics._ = _graphics.;
_graphics._ = _graphics.;
_graphics._ = _graphics.;
_graphics._ = _graphics.;
_graphics._ = _graphics.;
_graphics._ = _graphics.;
_graphics._ = _graphics.;
_graphics._ = _graphics.;
_graphics._ = _graphics.;*/
}
// #############################################################################################
/// Function:<summary>
/// As far as possible, prevent anti-aliasing on a canvas context
/// </summary>
// #############################################################################################
function Graphics_SetInterpolation(_graphics, _enable) {
if ((_graphics.imageSmoothingEnabled == _enable) &&
(_graphics.msImageSmoothingEnabled == _enable)) // IE11-specific kludge - modifying some canvas parameters (i.e. the width) changes the interpolation state of the canvas rendering context :/
return;
//
_graphics.imageSmoothingEnabled = _enable;
_graphics.webkitImageSmoothingEnabled = _enable;
_graphics.mozImageSmoothingEnabled = _enable;
_graphics.msImageSmoothingEnabled = _enable;
_graphics.oImageSmoothingEnabled = _enable;
// Adaptation of old code, but there are no more browsers that are that broken:
/*var pCanvas = _graphics.canvas;
if (pCanvas) {
var pStyle = pCanvas.style;
pStyle.msInterpolationMode = (_enable ? "bicubic" : "nearest-neighbor");
if (_enable) {
pStyle.setProperty("image-rendering", "optimizeQuality");
} else {
pStyle.setProperty("image-rendering", "pixelated");
if (pStyle.getPropertyValue("image-rendering") == "") {
pStyle.setProperty("image-rendering", "optimizeSpeed");
}
}
}*/
}
function Graphics_SetInterpolation_Auto(_graphics) {
if (!g_webGL) Graphics_SetInterpolation(_graphics, Graphics_Interpolation);
}
function texture_set_interpolation(_linear) {
_linear = _linear > 0.5;
if (Graphics_Interpolation != _linear) {
Graphics_Interpolation = _linear;
// this here is graphics from Function_Surface.js, for clarity
Graphics_SetInterpolation(graphics, _linear);
}
}
// #############################################################################################
/// Function:<summary>
/// Main graphics code. globals "canvas" and "graphics" must have been initialised.
/// </summary>
// #############################################################################################
function Graphics_Init( _canvas )
{
g_Textures = [];
g_pTextureOffsets = null;
g_clipx = 0;
g_clipy = 0;
g_clipw = 0;
g_cliph = 0;
g_worldx = 0;
g_worldy = 0;
g_worldw = 0;
g_worldh = 0;
g_transform = [];
g_transform[0] = 1;
g_transform[1] = 0;
g_transform[2] = 0;
g_transform[3] = 0;
g_transform[4] = 1;
g_transform[5] = 0;
if( !g_webGL ){
// @if feature("2d")
// Fill in RELEASE function pointers.
if (CACHE_SINGLE_IMAGE)
{
Graphics_TextureDrawSimple = Graphics_TextureDrawSimple_Cache;
} else
{
Graphics_TextureDrawSimple = Graphics_TextureDrawSimple_NoCache;
}
Graphics_TextureDrawTiled = Graphics_TextureDrawTiled_RELEASE;
Graphics_TextureDraw = Graphics_TextureDraw_RELEASE;
Graphics_TextureDrawWH = Graphics_TextureDrawWH_RELEASE;
Graphics_SetViewPort = Graphics_SetViewPort_RELEASE;
Graphics_SetViewArea = Graphics_SetViewArea_RELEASE;
Graphics_SetViewAreaTransform = Graphics_SetViewAreaTransform_RELEASE;
Graphics_SetTransform = Graphics_SetTransform_RELEASE;
Graphics_ClearScreen = Graphics_ClearScreen_RELEASE;
Graphics_PushTransform = Graphics_PushTransform_RELEASE;
Graphics_PushMatrix = Graphics_PushMatrix_RELEASE;
Graphics_DrawPart = Graphics_DrawPart_RELEASE;
Graphics_Save = Graphics_Save_RELEASE;
Graphics_Restore = Graphics_Restore_RELEASE;
// @if feature("fonts")
Graphics_DrawText = Graphics_DrawText_RELEASE;
// @endif fonts
Graphics_StartFrame = Graphics_StartFrame_RELEASE;
Graphics_EndFrame = Graphics_EndFrame_RELEASE;
Graphics_DrawComment = Graphics_DrawComment_RELEASE;
Graphics_SWFDraw = function () {};
Graphics_SWFDrawObject = function () {};
Graphics_VectorSpriteDraw = function () {};
Graphics_VectorSpriteDrawObject = function () {};
Graphics_Interpolation = !(g_pGMFile.Options && !g_pGMFile.Options.interpolatePixels);
// Fill in DEBUG function pointers.
if(DEBUG_MODE)
{
if (CACHE_SINGLE_IMAGE)
{
// Make the white value slightly OFF white, so it caches a white image as well...
g_CacheWhite = 0x1ffffff;
Graphics_TextureDrawSimple = Graphics_TextureDrawSimple_Cache_DEBUG;
} else
{
Graphics_TextureDrawSimple = Graphics_TextureDrawSimple_NoCache_DEBUG;
}
Graphics_TextureDrawTiled = Graphics_TextureDrawTiled_RELEASE;
Graphics_TextureDraw = Graphics_TextureDraw_DEBUG;
}
// @endif
}else{
// @if feature("gl")
InitWebGLFunctions();
// @endif gl
}
Graphics_SetViewPort(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT);
Graphics_SetViewArea(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT,0);
}
var g_Graphics_Save_Stack = [];
var g_GraphicsCounter = 0;
// #############################################################################################
/// Function:<summary>
/// Save graphics context
/// </summary>
// #############################################################################################
function Graphics_Save_RELEASE() {
graphics._save();
// keep track of save/restores. Should be 0 entries in the stack at the end of the draw
g_Graphics_Save_Stack.push(
{
counter: g_GraphicsCounter++,
clipx : g_clipx,
clipy: g_clipy,
clipw: g_clipw,
cliph: g_cliph
}
);
}
// #############################################################################################
/// Function:<summary>
/// Restore graphics context
/// </summary>
// #############################################################################################
function Graphics_Restore_RELEASE() {
graphics._restore();
// Pop last one off the stack
var g = g_Graphics_Save_Stack.pop();
g_clipx = g.clipx;
g_clipy = g.clipy;
g_clipw = g.clipw;
g_cliph = g.cliph;
--g_GraphicsCounter;
}
// #############################################################################################
/// Function:<summary>
/// Setup caching entries for an individual texture offset object
/// </summary>
// #############################################################################################
function Graphics_SetupTPECaching(_pTPE) {
_pTPE.cache = []; // clear colour cache
_pTPE.count = 0;
_pTPE.maxcache = 4; // Max number of times to cache this image.
_pTPE.vh_tile = 0; // How is it tiled?
_pTPE.hvcached = null; // tiling cache.
_pTPE.singleimage = null; // clear colour cache
_pTPE.texture = g_Textures[_pTPE.tp]; // get raw pointe to texture.
}
// #############################################################################################
/// Function:<summary>
/// Setup the texture offset array
/// </summary>
///
/// In: <param name="_pTable">Pointer to the texture table</param>
/// Out: <returns>
///
/// </returns>
// #############################################################################################
function Graphics_SetEntryTable(_pTable)
{
g_pTextureOffsets = _pTable;
for (var i = 0; i < _pTable.length; i++)
{
var pTPE = _pTable[i];
Graphics_SetupTPECaching(pTPE);
}
}
// #############################################################################################
/// Function:<summary>
/// Create a cacheblock
/// </summary>
///
/// Out: <returns>
///
/// </returns>
// #############################################################################################
/** @constructor */
function yyCacheBlock()
{
this.pImage = null;
this.lastused = 0;
}
function Graphics_CacheBlock_Do(_texture, _pCacheObject, _x, _y, _w, _h, _colour, _undoPremultiply)
{
var undoPremultiply = false;
if (_undoPremultiply != undefined)
{
undoPremultiply = _undoPremultiply;
}
var cachekey = _colour & 0xffffff; // the alpha component isn't used by Graphics_ColouriseImage anyway
if (undoPremultiply)
{
cachekey |= 1 << 24; // use space that used to contain alpha
}
var pCacheBlock = null;
if (_pCacheObject.cache != undefined) {
pCacheBlock = _pCacheObject.cache[cachekey];
} // end if
else {
_pCacheObject.cache = [];
_pCacheObject.maxcache = 4;
_pCacheObject.count = 0;
} // end else
if(pCacheBlock != null)
{
pCacheBlock.lastused = g_GlobalFrameCount;
return pCacheBlock.pImage;
}
var usetime = -9999999999;
pCacheBlock = null;
if (_pCacheObject.count < _pCacheObject.maxcache)
{
pCacheBlock = new yyCacheBlock();
_pCacheObject.count++;
}
else {
// Eject the least recently used cache block
var FoundColour = -1;
for (var i in _pCacheObject.cache)
{
if (!_pCacheObject.cache.hasOwnProperty(i)) continue;
var pBlock = _pCacheObject.cache[i];
if (pBlock != null)
{
var t = g_GlobalFrameCount - pBlock.lastused;
if (t > usetime)
{
FoundColour = i;
pCacheBlock = pBlock;
usetime = t;
}
}
}
if (FoundColour >= 0) delete _pCacheObject.cache[FoundColour];
}
_pCacheObject.cache[cachekey] = pCacheBlock;
pCacheBlock.lastused = g_GlobalFrameCount;
pCacheBlock.pImage = Graphics_ColouriseImage(_texture, _x, _y, _w, _h, _colour, _undoPremultiply);
return pCacheBlock.pImage;
}
// #############################################################################################
/// Function:<summary>
///
/// </summary>
///
/// In: <param name="_pTPE"></param>
/// <param name="_colour"></param>
/// Out: <returns>
///
/// </returns>
// #############################################################################################
function Graphics_CacheBlock(_pTPE, _colour, _undoPremultiply)
{
return Graphics_CacheBlock_Do( _pTPE.texture, _pTPE, _pTPE.x, _pTPE.y, _pTPE.w, _pTPE.h, _colour, _undoPremultiply );
}
// #############################################################################################
/// Function:<summary>
/// "Get" an offset
/// </summary>
// #############################################################################################
function Graphics_GetTextureEntry( _index )
{
return g_pTextureOffsets[_index];
}
// #############################################################################################
/// Function:<summary>
/// Sets the current g_transform...
/// </summary>
// #############################################################################################
function Graphics_SetTransform_RELEASE()
{
graphics._setTransform(g_transform[0], g_transform[3], g_transform[1], g_transform[4], g_transform[2], g_transform[5]);
}
// #############################################################################################
/// Function:<summary>
/// Clear the region in the indicated color
/// </summary>
///
/// In: <param name="col">Colour to clear the screen with</param>
// #############################################################################################
function Graphics_ClearScreen_RELEASE(_col)
{
Graphics_Save();
var trans = [];
trans[0] = 1;
trans[1] = 0;
trans[2] = 0;
trans[3] = 1;
trans[4] = 0;
trans[5] = 0;
graphics._setTransform(trans[0], trans[1], trans[2], trans[3], trans[4], trans[5]);
graphics.fillStyle = GetHTMLRGB(_col|0xff000000);
graphics._fillRect(g_clipx, g_clipy, g_clipw, g_cliph);
Graphics_Restore();
}
// #############################################################################################
/// Function:<summary>
/// This specifies the CLIP region, and is in screen pixels.
/// </summary>
///
/// In: <param name="_portx"></param>
/// <param name="_porty"></param>
/// <param name="_portw"></param>
/// <param name="_porth"></param>
///
// #############################################################################################
function Graphics_SetViewPort_RELEASE(_portx, _porty, _portw, _porth)
{
g_clipx = _portx;
g_clipy = _porty;
g_clipw = _portw;
g_cliph = _porth;
if (g_isZeus)
{
if ((g_clipx === 0 && g_clipy === 0) && (g_clipw == graphics.canvas.width && g_cliph === graphics.canvas.height)) {
} else {
g_transform[0] = 1;
g_transform[1] = 0;
g_transform[2] = 0;
g_transform[3] = 1;
g_transform[4] = 0;
g_transform[5] = 0;
graphics._setTransform(g_transform[0], g_transform[1], g_transform[2], g_transform[3], g_transform[4], g_transform[5]);
graphics.beginPath();
graphics.moveTo(g_clipx, g_clipy);
graphics.lineTo(g_clipx + g_clipw, g_clipy);
graphics.lineTo(g_clipx + g_clipw, g_clipy + g_cliph);
graphics.lineTo(g_clipx, g_clipy + g_cliph);
// default android browsers has a bug with "clip"
if (!(g_OSBrowser == BROWSER_DEFAULT_ANDROID && g_OSVersion==4.0))
{
graphics.clip();
}
}
}
}
// #############################################################################################
/// Function:<summary>
/// Sets the correct view area in the world angle is the rotation
/// in degrees counter-clockwise
/// </summary>
///
/// In: <param name="x"></param>
/// <param name="y"></param>
/// <param name="w"></param>
/// <param name="h"></param>
/// <param name="angle"></param>
///
// #############################################################################################
function GR_D3D_Set_View_Area(_x, _y, _w, _h, _angle) {
var V1 = new Vector3((_x + _w / 2.0), (_y + _h / 2.0), -16000.0);
var V2 = new Vector3((_x + _w / 2.0), (_y + _h / 2.0), 0.0);
var V3 = new Vector3(Math.sin(-_angle * (Math.PI / 180.0)), Math.cos(-_angle * (Math.PI / 180.0)), 0.0);
g_pView.LookAtLH(V1, V2, V3);
g_pProjection.OrthoLH(_w, -_h * g_RenderTargetActive, 1.0, 32000.0);
}
// #############################################################################################
/// Function:<summary>
/// Create a "full" matrix and use "push" it.
/// </summary>
///
/// In: <param name="_x">X location</param>
/// <param name="_y">Y location</param>
/// <param name="_xs">X scale</param>
/// <param name="_ys">Y scale</param>
/// <param name="_angle">angle in radians/</param>
/// Out: <returns>
///
/// </returns>
// #############################################################################################
function Graphics_PushTransform_RELEASE( _x,_y, _xs,_ys, _angle )
{
var trans = [];
trans[0] = Math.cos(_angle);
trans[3] = Math.sin(_angle);
trans[1] = -trans[3];
trans[4] = trans[0];
trans[0] *= _xs;
trans[3] *= _xs;
trans[1] *= _ys;
trans[4] *= _ys;
trans[2] = _x;
trans[5] = _y;
graphics._transform( trans[0], trans[3], trans[1], trans[4], trans[2], trans[5] );
}
function Graphics_PushMatrix_RELEASE(_matrix)
{
graphics._transform(_matrix.m[0], _matrix.m[1], _matrix.m[4], _matrix.m[5], _matrix.m[12], _matrix.m[13]);
}
function Graphics_SetViewAreaTransform_RELEASE(_sx, _sy, _tx, _ty)
{
if ((g_clipx === 0 && g_clipy === 0) && (g_clipw == graphics.canvas.width && g_cliph === graphics.canvas.height)) {
} else {
//set clip region
g_transform[0] = 1;
g_transform[1] = 0;
g_transform[2] = 0;
g_transform[3] = 1;
g_transform[4] = 0;
g_transform[5] = 0;
graphics._setTransform(g_transform[0], g_transform[1], g_transform[2], g_transform[3], g_transform[4], g_transform[5]);
// graphics.beginPath();
//graphics._rect(0, 0, 255, 255); //g_clipx, g_clipy, g_clipw, g_cliph);
graphics.moveTo(g_clipx, g_clipy);
graphics.lineTo(g_clipx + g_clipw, g_clipy);
graphics.lineTo(g_clipx + g_clipw, g_clipy + g_cliph);
graphics.lineTo(g_clipx, g_clipy + g_cliph);
//graphics.lineTo(g_clipx, g_clipy);
//graphics._closePath();
// graphics.rect(g_clipx, g_clipy, g_clipx + g_clipw, g_clipy + g_cliph);
// graphics.stroke();
// default android browsers has a bug with "clip"
if (!(g_OSBrowser == BROWSER_DEFAULT_ANDROID && g_OSVersion == 4.0)) {
graphics.clip();
}
}
//set view transform
g_transform[0] = _sx;
g_transform[1] = 0;
g_transform[2] = _tx;
g_transform[3] = 0;
g_transform[4] = _sy;
g_transform[5] = _ty;
graphics._setTransform(g_transform[0], g_transform[3], g_transform[1], g_transform[4], g_transform[2], g_transform[5]);
}
// #############################################################################################
/// Function:<summary>
/// Specify a "region" to look at in the world.
/// </summary>
///
/// In: <param name="_wolrdx"></param>
/// <param name="_worldy"></param>
/// <param name="_worldw"></param>
/// <param name="_worldh"></param>
///
// #############################################################################################
function Graphics_SetViewArea_RELEASE(_worldx, _worldy, _worldw, _worldh, _angle)
{
if ((g_clipx === 0 && g_clipy === 0) && (g_clipw == graphics.canvas.width && g_cliph === graphics.canvas.height)) {
} else {
g_transform[0] = 1;
g_transform[1] = 0;
g_transform[2] = 0;
g_transform[3] = 1;
g_transform[4] = 0;
g_transform[5] = 0;
graphics._setTransform(g_transform[0], g_transform[1], g_transform[2], g_transform[3], g_transform[4], g_transform[5]);
graphics.beginPath();
graphics.moveTo(g_clipx, g_clipy);
graphics.lineTo(g_clipx + g_clipw, g_clipy);
graphics.lineTo(g_clipx + g_clipw, g_clipy + g_cliph);
graphics.lineTo(g_clipx, g_clipy + g_cliph);
// default android browsers has a bug with "clip"
if (!(g_OSBrowser == BROWSER_DEFAULT_ANDROID && g_OSVersion==4.0))
{
graphics.clip();
}
}
var w = g_clipw / _worldw;
var h = g_cliph / _worldh;
g_worldx = _worldx;
g_worldy = _worldy;
g_worldw = _worldw;
g_worldh = _worldh;
g_transform[0] = w;
g_transform[1] = 0;
g_transform[2] = -(_worldx * w) + g_clipx;
g_transform[3] = 0;
g_transform[4] = h;
g_transform[5] = -(_worldy * h) + g_clipy;
graphics._setTransform(g_transform[0], g_transform[3], g_transform[1], g_transform[4], g_transform[2], g_transform[5]);
}
// #############################################################################################
/// Function:<summary>
/// Add a texture to the "pool"
/// </summary>
///
/// In: <param name="_name">Name+path of texture to load</param>
/// Out: <returns>
/// The index it's assigned to.
/// </returns>
// #############################################################################################
function Graphics_AddTexture( _name )
{
var i = g_Textures.length;
var texture = new Image();
_name = CheckWorkingDirectory(_name);
texture.crossOrigin = g_HttpRequestCrossOriginType;
texture.src = set_load_location(null,null,_name);
g_Textures[i] = texture;
return i;
}
function Graphics_AddTextureWH( _width, _height )
{
var c = document.createElement( "canvas" );
c.width = _width;
c.height = _height;
var i = g_Textures.length;
var texture = new Image(_width, _height);
texture.src = set_load_location(null,null,c.toDataURL());
g_Textures[i] = texture;
return i;
}
function Graphics_AddActualTexture( _image )
{
var i = g_Textures.length;
g_Textures[i] = _image;
return i;
}
// #############################################################################################
/// Function:<summary>
/// Add a texture to the "pool"
/// </summary>
///
/// In: <param name="_name">Name+path of texture to load</param>
/// Out: <returns>
/// The index it's assigned to.
/// </returns>
// #############################################################################################
function Graphics_UpdateTexture( _texture, _x, _y, _w,_h,_canvas )
{
var context = _texture.getContext('2d');
context.globalCompositeOperation = 'copy';
context.drawImage( _canvas, _x, _y );
}
// #############################################################################################
/// Function:<summary>
/// Adds an IMAGE to the texture pool.
/// </summary>
///
/// In: <param name="_pImage">dynamically created image to add</param>
/// Out: <returns>
/// The "index" it's assigned to.
/// </returns>
// #############################################################################################
function Graphics_AddImage(_pImage)
{
var i = g_Textures.length;
g_Textures[i] = _pImage;
return i;
}
// #############################################################################################
/// Function:<summary>
/// Begin rendering
/// </summary>
// #############################################################################################
function Graphics_StartFrame_RELEASE()
{
g_GlobalFrameCount++;
}
// #############################################################################################
/// Function:<summary>
/// End rendering
/// </summary>
// #############################################################################################
function Graphics_EndFrame_RELEASE()
{
}
// #############################################################################################
/// Function:<summary>
/// Extract and image from a texture page into it's "own" single image.
/// </summary>
///
/// In: <param name="_pTPE">Texture page entry to extract</param>
/// Out: <returns>
/// A "singleimage" to use.
/// </returns>
// #############################################################################################
function Graphics_ExtractImage(_pTPE)
{
var singleimage = document.createElement(g_CanvasName);
var pImg = singleimage.getContext('2d');
Graphics_AddCanvasFunctions(pImg);
// TODO: use Graphics_DisableInterpolation(pImg);
singleimage.width = _pTPE.w;
singleimage.height = _pTPE.h;
pImg._drawImage(_pTPE.texture, _pTPE.x, _pTPE.y, _pTPE.w, _pTPE.h, 0, 0, _pTPE.w, _pTPE.h);
singleimage.complete = true;
return singleimage;
}
// #############################################################################################
/// Function:<summary>
/// Extract and image into a RAW byte array (ARGB format)
/// </summary>
///
/// In: <param name="_pTPE">Texture page entry to extract</param>
/// Out: <returns>
/// A "singleimage" to use.
/// </returns>
// #############################################################################################
function Graphics_ExtractImageBytes(_pTPE) {
// First allocate and fill the TOTAL size of the original image.
var pData = [];
var tot = _pTPE.ow * _pTPE.oh * 4;
var pSrcData;
for(var i=0;i<tot;i++) pData[i]=0;
if (_pTPE.texture.webgl_textureid)
pSrcData = g_webGL.ExtractWebGLPixels(_pTPE);
else
{
var singleimage = document.createElement(g_CanvasName);
var pImg = singleimage.getContext('2d');
Graphics_AddCanvasFunctions(pImg);
// TODO: use Graphics_DisableInterpolation(pImg);
singleimage.width = _pTPE.w;
singleimage.height = _pTPE.h;
pImg._drawImage(_pTPE.texture, _pTPE.x, _pTPE.y, _pTPE.w, _pTPE.h, 0, 0, _pTPE.w, _pTPE.h);
singleimage.complete = true;
var data, sdata, imagedata, ddata;
// This function cannot be called if the image is not from the same domain. You'll get security error if you do. ?!?!?
try
{
data = pImg.getImageData(0, 0, _pTPE.w, _pTPE.h);
} catch (ex)
{
return pData;
}
pSrcData = data.data;
}
var baseindex = (_pTPE.XOffset + (_pTPE.YOffset * _pTPE.ow)) * 4;
var ww = _pTPE.w * 4;
var x, y;
for (y = 0; y < _pTPE.h; y++)
{
var bindex = baseindex;
var imageindex = y*ww;
for (x = 0; x < ww; x++)
{
pData[bindex++] = pSrcData[imageindex++];
}
baseindex += _pTPE.ow*4;
}
return pData;
}
// #############################################################################################
/// Function:<summary>
/// Draw a simple texture map
/// </summary>
///
/// In: <param name="id"></param>
/// <param name="pTPE"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// Out: <returns>
///
/// </returns>
// #############################################################################################
function Graphics_TextureDrawSimple_Cache(_pTPE, _x, _y, _alpha)
{
// Need these checks here in case "dynamic loading" has forced an unload of the image.
if (!_pTPE) return;
if (!_pTPE.texture) return;
if (!_pTPE.texture.complete) return;
if (_pTPE.singleimage == null) _pTPE.singleimage = Graphics_ExtractImage(_pTPE);
_x += _pTPE.XOffset;
_y += _pTPE.YOffset;
graphics.globalAlpha = _alpha;
graphics._drawImage(_pTPE.singleimage, ~ ~_x, ~~_y);
}
// Cache the image - debug mode
function Graphics_TextureDrawSimple_Cache_DEBUG(_pTPE, _x, _y, _alpha)
{
Graphics_TextureDrawSimple_Cache(_pTPE, _x, _y, _alpha);
}
// Non-cached, release mode
function Graphics_TextureDrawSimple_NoCache(_pTPE, _x, _y, _alpha)
{
// Need these checks here in case "dynamic loading" has forced an unload of the image.
if (!_pTPE) return;
if (!_pTPE.texture) return;
if (!_pTPE.texture.complete) return;
graphics.globalAlpha = _alpha;
graphics._drawImage(_pTPE.texture, ~ ~_pTPE.x, ~ ~_pTPE.y, _pTPE.w, _pTPE.h, Math.floor(_x) + _pTPE.XOffset, Math.floor(_y) + _pTPE.YOffset, _pTPE.CropWidth, _pTPE.CropHeight);
}
// No cached, debug mode
function Graphics_TextureDrawSimple_NoCache_DEBUG(_pTPE, _x, _y, _alpha)
{
Graphics_TextureDrawSimple_NoCache(_pTPE, _x, _y, _alpha);
}
// https://developer.apple.com/library/safari/documentation/appleapplications/reference/safariwebcontent/creatingcontentforsafarioniphone/creatingcontentforsafarioniphone.html
// #############################################################################################
/// Function:<summary>
/// 1) The iOS Safari browser (and others on iOS, seemingly) has an upper limit on
/// Images of 3 * 1024 * 1024 for devices with <256MB of RAM and 5 * 1024 * 1024 for
/// devices with >256MB of RAM. When a canvas is used that's larger than this there
/// won't be any exceptions thrown, it'll just carry on oblivious and simply not
/// draw anything to the canvas. Therefore we can just try drawing to the canvas
/// and retrieve the data to see if it succeeded and if not.
/// 2) SurfaceRT (Win8(JS)) machines seem only capable of <= 2048x2048 canvases.
/// However, they will let you try and work with one bigger and all that happens is
/// that if you draw outwith the size limit then nothing gets drawn, so if you try to
/// use this canvas as a texture there will be big blank spaces past the 2048 limit.
/// The only way to find out if the canvas size in use is too large is by trying to
/// explicitly draw the full buffer width/height somewhere to get it to trip an
/// "IndexSizeError" exception.
/// </summary>
// #############################################################################################
function Graphics_CanvasSizeSupported( _canvas ) {
// Win8(JS) approach
try {
graphics._drawImage(_canvas, 0, 0, _canvas.width, _canvas.height, canvas.width, canvas.height, 0, 0);
}
catch (e) {
return false;
}
// iOS approach
var ctx = _canvas.getContext('2d');
ctx.fillStyle = "white";
ctx.fillRect(0, 0, 1, 1);
var imgData = ctx.getImageData(0, 0, 1, 1);
return (imgData.data[0] === 255);
}
// #############################################################################################
/// Function:<summary>
/// Draws the texture tiled to cover at least the given area (xr,yr,wr,hr)
/// No clipping is performed so a slightly larger area might be filled
/// Its origin is placed on position x,y and it is scaled as indicated
/// htiled indicates whether to use horizontal tiling, vtiled for vertical tiling
/// col is the blend color, alpha is the alpha transparency value (0-1)
/// NB: Because of speed issues, the non-WebGL version of this explicitly ignores the
/// scaling values.
/// </summary>
///
/// In: <param name="id"></param>
/// <param name="xorig"></param>
/// <param name="yorig"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="xsc">IGNORED</param>
/// <param name="ysc">IGNORED</param>
/// <param name="htiled"></param>
/// <param name="vtiled"></param>
/// <param name="xr"></param>
/// <param name="yr"></param>
/// <param name="wr"></param>
/// <param name="hr"></param>
/// <param name="col"></param>
/// <param name="alpha"></param>
/// Out: <returns>
///
/// </returns>
// #############################################################################################
function Graphics_TextureDrawTiled_RELEASE( _pTPE, _xorig, _yorig, _x, _y, _xsc, _ysc, htiled, vtiled, _xr, _yr, _wr, _hr, _col, _alpha )
{
var pTexture = _pTPE.texture;
if (!pTexture) return;
if (!pTexture.complete) return;