1 package org.newdawn.slick;
2
3 import java.nio.ByteBuffer;
4 import java.nio.DoubleBuffer;
5 import java.nio.FloatBuffer;
6 import java.security.AccessController;
7 import java.security.PrivilegedAction;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import org.lwjgl.BufferUtils;
12 import org.lwjgl.opengl.GL11;
13 import org.newdawn.slick.geom.Rectangle;
14 import org.newdawn.slick.geom.Shape;
15 import org.newdawn.slick.geom.ShapeRenderer;
16 import org.newdawn.slick.opengl.TextureImpl;
17 import org.newdawn.slick.opengl.renderer.LineStripRenderer;
18 import org.newdawn.slick.opengl.renderer.Renderer;
19 import org.newdawn.slick.opengl.renderer.SGL;
20 import org.newdawn.slick.util.FastTrig;
21 import org.newdawn.slick.util.Log;
22
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25
26
27
28
29
30
31 public class Graphics {
32
33 protected static final SGL GL = Renderer.get();
34
35 private static final LineStripRenderer LSR = Renderer.getLineStripRenderer();
36
37
38 private static final int MODE_NORMAL = 1;
39
40
41 private static final int MODE_ALPHA_MAP = 2;
42
43
44 private static final int MODE_ALPHA_BLEND = 3;
45
46
47 private static final int MODE_COLOR_MULTIPLY = 4;
48
49
50 private static final int MODE_ADD = 5;
51
52
53 private static final int MODE_SCREEN = 6;
54
55
56 private static final int MODE_ADD_ALPHA = 7;
57
58
59 private static final int MODE_COLOR_MULTIPLY_ALPHA = 8;
60
61
62 private static final int DEFAULT_SEGMENTS = 50;
63
64
65 @Nullable
66 private static Graphics currentGraphics = null;
67
68
69 private static Font DEFAULT_FONT;
70
71
72 private float sx = 1;
73
74 private float sy = 1;
75
76
77
78
79
80
81
82 protected static void setCurrent(Graphics current) {
83 if (currentGraphics != current) {
84 if (currentGraphics != null) {
85 currentGraphics.disable();
86 }
87 currentGraphics = current;
88 currentGraphics.enable();
89 }
90 }
91
92
93 private Font font;
94
95
96 @Nonnull
97 private Color currentColor = Color.white;
98
99
100 protected int screenWidth;
101
102
103 protected int screenHeight;
104
105
106 private boolean pushed;
107
108
109 @Nullable
110 private Rectangle clip;
111
112
113 private final DoubleBuffer worldClip = BufferUtils.createDoubleBuffer(4);
114
115
116 private final ByteBuffer readBuffer = BufferUtils.createByteBuffer(4);
117
118
119 private boolean antialias;
120
121
122 @Nullable
123 private Rectangle worldClipRecord;
124
125
126 private int currentDrawingMode = MODE_NORMAL;
127
128
129 private float lineWidth = 1;
130
131
132 private final List<FloatBuffer> stack = new ArrayList<>();
133
134 private int stackIndex;
135
136
137
138
139 protected Graphics() {
140 }
141
142
143
144
145
146
147
148
149
150 protected Graphics(int width, int height) {
151 if (DEFAULT_FONT == null) {
152 AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
153 try {
154 DEFAULT_FONT = new AngelCodeFont("org/newdawn/slick/data/defaultfont.fnt", "org/newdawn/slick/data/defaultfont.png");
155 } catch (SlickException e) {
156 Log.error(e);
157 }
158 return null;
159 });
160 }
161
162 this.font = DEFAULT_FONT;
163 screenWidth = width;
164 screenHeight = height;
165 }
166
167
168
169
170
171
172
173
174
175 void setDimensions(int width, int height) {
176 screenWidth = width;
177 screenHeight = height;
178 }
179
180
181
182
183
184
185
186
187
188
189
190 void setDrawMode(int mode) {
191 predraw();
192 currentDrawingMode = mode;
193 if (currentDrawingMode == MODE_NORMAL) {
194 GL.glEnable(SGL.GL_BLEND);
195 GL.glColorMask(true, true, true, true);
196 GL.glBlendFunc(SGL.GL_SRC_ALPHA, SGL.GL_ONE_MINUS_SRC_ALPHA);
197 }
198 if (currentDrawingMode == MODE_ALPHA_MAP) {
199 GL.glDisable(SGL.GL_BLEND);
200 GL.glColorMask(false, false, false, true);
201 }
202 if (currentDrawingMode == MODE_ALPHA_BLEND) {
203 GL.glEnable(SGL.GL_BLEND);
204 GL.glColorMask(true, true, true, false);
205 GL.glBlendFunc(SGL.GL_DST_ALPHA, SGL.GL_ONE_MINUS_DST_ALPHA);
206 }
207 if (currentDrawingMode == MODE_COLOR_MULTIPLY) {
208 GL.glEnable(SGL.GL_BLEND);
209 GL.glColorMask(true, true, true, true);
210 GL.glBlendFunc(SGL.GL_ONE_MINUS_SRC_COLOR, SGL.GL_SRC_COLOR);
211 }
212 if (currentDrawingMode == MODE_ADD) {
213 GL.glEnable(SGL.GL_BLEND);
214 GL.glColorMask(true, true, true, true);
215 GL.glBlendFunc(SGL.GL_ONE, SGL.GL_ONE);
216 }
217 if (currentDrawingMode == MODE_SCREEN) {
218 GL.glEnable(SGL.GL_BLEND);
219 GL.glColorMask(true, true, true, true);
220 GL.glBlendFunc(SGL.GL_ONE, SGL.GL_ONE_MINUS_SRC_COLOR);
221 }
222 if (currentDrawingMode == MODE_ADD_ALPHA) {
223 GL.glEnable(SGL.GL_BLEND);
224 GL.glColorMask(true, true, true, true);
225 GL.glBlendFunc(SGL.GL_SRC_ALPHA, SGL.GL_ONE);
226 }
227 if (currentDrawingMode == MODE_COLOR_MULTIPLY_ALPHA) {
228 GL.glEnable(SGL.GL_BLEND);
229 GL.glColorMask(true, true, true, true);
230 GL.glBlendFunc(SGL.GL_ONE_MINUS_SRC_COLOR, SGL.GL_ONE_MINUS_SRC_ALPHA);
231 }
232 postdraw();
233 }
234
235
236
237
238
239 public void clearAlphaMap() {
240 pushTransform();
241 GL.glLoadIdentity();
242
243 int originalMode = currentDrawingMode;
244 setDrawMode(MODE_ALPHA_MAP);
245 setColor(new Color(0, 0, 0, 0));
246 fillRect(0, 0, screenWidth, screenHeight);
247 setColor(currentColor);
248 setDrawMode(originalMode);
249
250 popTransform();
251 }
252
253
254
255
256 private void predraw() {
257 setCurrent(this);
258 }
259
260
261
262
263 private void postdraw() {
264 }
265
266
267
268
269 protected void enable() {
270 }
271
272
273
274
275 protected void flush() {
276 if (currentGraphics == this) {
277 currentGraphics.disable();
278 currentGraphics = null;
279 }
280 }
281
282
283
284
285 protected void disable() {
286 }
287
288
289
290
291
292
293 public Font getFont() {
294 return font;
295 }
296
297
298
299
300
301
302
303
304 public void setBackground(@Nonnull Color color) {
305 predraw();
306 GL.glClearColor(color.r, color.g, color.b, color.a);
307 postdraw();
308 }
309
310
311
312
313
314
315 @Nonnull
316 public Color getBackground() {
317 predraw();
318 FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
319 GL.glGetFloat(SGL.GL_COLOR_CLEAR_VALUE, buffer);
320 postdraw();
321
322 return new Color(buffer);
323 }
324
325
326
327
328 public void clear() {
329 predraw();
330 GL.glClear(SGL.GL_COLOR_BUFFER_BIT);
331 postdraw();
332 }
333
334
335
336
337 public void resetTransform() {
338 sx = 1;
339 sy = 1;
340
341 if (pushed) {
342 predraw();
343 GL.glPopMatrix();
344 pushed = false;
345 postdraw();
346 }
347 }
348
349
350
351
352 private void checkPush() {
353 if (!pushed) {
354 predraw();
355 GL.glPushMatrix();
356 pushed = true;
357 postdraw();
358 }
359 }
360
361
362
363
364
365
366
367
368
369 public void scale(float sx, float sy) {
370 this.sx = this.sx * sx;
371 this.sy = this.sy * sy;
372
373 checkPush();
374
375 predraw();
376 GL.glScalef(sx, sy, 1);
377 postdraw();
378 }
379
380
381
382
383
384
385
386
387
388
389
390 public void rotate(float rx, float ry, float ang) {
391 checkPush();
392
393 predraw();
394 translate(rx, ry);
395 GL.glRotatef(ang, 0, 0, 1);
396 translate(-rx, -ry);
397 postdraw();
398 }
399
400
401
402
403
404
405
406
407
408 void translate(float x, float y) {
409 checkPush();
410
411 predraw();
412 GL.glTranslatef(x, y, 0);
413 postdraw();
414 }
415
416
417
418
419
420
421
422 public void setFont(Font font) {
423 this.font = font;
424 }
425
426
427
428
429 public void resetFont() {
430 font = DEFAULT_FONT;
431 }
432
433
434
435
436
437
438
439 void setColor(@Nullable Color color) {
440 if (color == null) {
441 return;
442 }
443
444 currentColor = new Color(color);
445 predraw();
446 currentColor.bind();
447 postdraw();
448 }
449
450
451
452
453
454
455 @Nonnull
456 public Color getColor() {
457 return new Color(currentColor);
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471
472 void drawLine(float x1, float y1, float x2, float y2) {
473 float lineWidth = this.lineWidth - 1;
474
475 if (LSR.applyGLLineFixes()) {
476 if (x1 == x2) {
477 if (y1 > y2) {
478 float temp = y2;
479 y2 = y1;
480 y1 = temp;
481 }
482 float step = 1 / sy;
483 lineWidth = lineWidth / sy;
484 fillRect(x1 - (lineWidth / 2.0f), y1 - (lineWidth / 2.0f), lineWidth + step, (y2 - y1) + lineWidth + step);
485 return;
486 } else if (y1 == y2) {
487 if (x1 > x2) {
488 float temp = x2;
489 x2 = x1;
490 x1 = temp;
491 }
492 float step = 1 / sx;
493 lineWidth = lineWidth / sx;
494 fillRect(x1 - (lineWidth / 2.0f), y1 - (lineWidth / 2.0f), (x2 - x1) + lineWidth + step, lineWidth + step);
495 return;
496 }
497 }
498
499 predraw();
500 currentColor.bind();
501 TextureImpl.bindNone();
502
503 LSR.start();
504 LSR.vertex(x1, y1);
505 LSR.vertex(x2, y2);
506 LSR.end();
507
508 postdraw();
509 }
510
511
512
513
514
515
516
517
518
519 public void draw(@Nonnull Shape shape, @Nonnull ShapeFill fill) {
520 predraw();
521 TextureImpl.bindNone();
522
523 ShapeRenderer.draw(shape, fill);
524
525 currentColor.bind();
526 postdraw();
527 }
528
529
530
531
532
533
534
535
536
537 public void fill(@Nonnull Shape shape, @Nonnull ShapeFill fill) {
538 predraw();
539 TextureImpl.bindNone();
540
541 ShapeRenderer.fill(shape, fill);
542
543 currentColor.bind();
544 postdraw();
545 }
546
547
548
549
550
551
552
553 public void draw(@Nonnull Shape shape) {
554 predraw();
555 TextureImpl.bindNone();
556 currentColor.bind();
557
558 ShapeRenderer.draw(shape);
559
560 postdraw();
561 }
562
563
564
565
566
567
568
569 public void fill(@Nonnull Shape shape) {
570 predraw();
571 TextureImpl.bindNone();
572 currentColor.bind();
573
574 ShapeRenderer.fill(shape);
575
576 postdraw();
577 }
578
579
580
581
582
583
584
585
586
587 public void texture(@Nonnull Shape shape, @Nonnull Image image) {
588 texture(shape, image, 0.01f, 0.01f, false);
589 }
590
591
592
593
594
595
596
597
598
599
600
601 public void texture(@Nonnull Shape shape, @Nonnull Image image, @Nonnull ShapeFill fill) {
602 texture(shape, image, 0.01f, 0.01f, fill);
603 }
604
605
606
607
608
609
610
611
612
613
614
615 public void texture(@Nonnull Shape shape, @Nonnull Image image, boolean fit) {
616 if (fit) {
617 texture(shape, image, 1, 1, true);
618 } else {
619 texture(shape, image, 0.01f, 0.01f, false);
620 }
621 }
622
623
624
625
626
627
628
629
630
631
632
633
634
635 public void texture(@Nonnull Shape shape, @Nonnull Image image, float scaleX, float scaleY) {
636 texture(shape, image, scaleX, scaleY, false);
637 }
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653 void texture(@Nonnull Shape shape, @Nonnull Image image, float scaleX, float scaleY, boolean fit) {
654 predraw();
655 TextureImpl.bindNone();
656 currentColor.bind();
657
658 if (fit) {
659 ShapeRenderer.textureFit(shape, image, scaleX, scaleY);
660 } else {
661 ShapeRenderer.texture(shape, image, scaleX, scaleY);
662 }
663
664 postdraw();
665 }
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681 void texture(@Nonnull Shape shape, @Nonnull Image image, float scaleX, float scaleY, @Nonnull ShapeFill fill) {
682 predraw();
683 TextureImpl.bindNone();
684 currentColor.bind();
685
686 ShapeRenderer.texture(shape, image, scaleX, scaleY, fill);
687
688 postdraw();
689 }
690
691
692
693
694
695
696
697
698
699
700
701
702
703 void drawRect(float x1, float y1, float width, float height) {
704 getLineWidth();
705
706 drawLine(x1, y1, x1 + width, y1);
707 drawLine(x1 + width, y1, x1 + width, y1 + height);
708 drawLine(x1 + width, y1 + height, x1, y1 + height);
709 drawLine(x1, y1 + height, x1, y1);
710 }
711
712
713
714
715 void clearClip() {
716 clip = null;
717 predraw();
718 GL.glDisable(SGL.GL_SCISSOR_TEST);
719 postdraw();
720 }
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736 void setWorldClip(float x, float y, float width, float height) {
737 predraw();
738 worldClipRecord = new Rectangle(x, y, width, height);
739
740 GL.glEnable(SGL.GL_CLIP_PLANE0);
741 worldClip.put(1).put(0).put(0).put(-x).flip();
742 GL.glClipPlane(SGL.GL_CLIP_PLANE0, worldClip);
743 GL.glEnable(SGL.GL_CLIP_PLANE1);
744 worldClip.put(-1).put(0).put(0).put(x + width).flip();
745 GL.glClipPlane(SGL.GL_CLIP_PLANE1, worldClip);
746
747 GL.glEnable(SGL.GL_CLIP_PLANE2);
748 worldClip.put(0).put(1).put(0).put(-y).flip();
749 GL.glClipPlane(SGL.GL_CLIP_PLANE2, worldClip);
750 GL.glEnable(SGL.GL_CLIP_PLANE3);
751 worldClip.put(0).put(-1).put(0).put(y + height).flip();
752 GL.glClipPlane(SGL.GL_CLIP_PLANE3, worldClip);
753 postdraw();
754 }
755
756
757
758
759 void clearWorldClip() {
760 predraw();
761 worldClipRecord = null;
762 GL.glDisable(SGL.GL_CLIP_PLANE0);
763 GL.glDisable(SGL.GL_CLIP_PLANE1);
764 GL.glDisable(SGL.GL_CLIP_PLANE2);
765 GL.glDisable(SGL.GL_CLIP_PLANE3);
766 postdraw();
767 }
768
769
770
771
772
773
774
775
776 void setWorldClip(@Nullable Rectangle clip) {
777 if (clip == null) {
778 clearWorldClip();
779 } else {
780 setWorldClip(clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
781 }
782 }
783
784
785
786
787
788
789 @Nullable
790 Rectangle getWorldClip() {
791 return worldClipRecord;
792 }
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807 void setClip(int x, int y, int width, int height) {
808 predraw();
809
810 if (clip == null) {
811 GL.glEnable(SGL.GL_SCISSOR_TEST);
812 clip = new Rectangle(x, y, width, height);
813 } else {
814 clip.setBounds(x, y, width, height);
815 }
816
817 GL.glScissor(x, screenHeight - y - height, width, height);
818 postdraw();
819 }
820
821
822
823
824
825
826
827
828 public void setClip(@Nullable Rectangle rect) {
829 if (rect == null) {
830 clearClip();
831 return;
832 }
833
834 setClip((int) rect.getX(), (int) rect.getY(), (int) rect.getWidth(), (int) rect.getHeight());
835 }
836
837
838
839
840
841
842 @Nullable
843 public Rectangle getClip() {
844 return clip;
845 }
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865 public void fillRect(float x, float y, float width, float height, @Nonnull Image pattern, float offX, float offY) {
866 int cols = ((int) Math.ceil(width / pattern.getWidth())) + 2;
867 int rows = ((int) Math.ceil(height / pattern.getHeight())) + 2;
868
869 Rectangle preClip = getWorldClip();
870 setWorldClip(x, y, width, height);
871
872 predraw();
873
874 for (int c = 0; c < cols; c++) {
875 for (int r = 0; r < rows; r++) {
876 pattern.draw(c * pattern.getWidth() + x - offX, r * pattern.getHeight() + y - offY);
877 }
878 }
879 postdraw();
880
881 setWorldClip(preClip);
882 }
883
884
885
886
887
888
889
890
891
892
893
894
895
896 void fillRect(float x1, float y1, float width, float height) {
897 predraw();
898 TextureImpl.bindNone();
899 currentColor.bind();
900
901 GL.glBegin(SGL.GL_QUADS);
902 GL.glVertex2f(x1, y1);
903 GL.glVertex2f(x1 + width, y1);
904 GL.glVertex2f(x1 + width, y1 + height);
905 GL.glVertex2f(x1, y1 + height);
906 GL.glEnd();
907 postdraw();
908 }
909
910
911
912
913
914
915
916
917
918
919
920
921
922 public void drawOval(float x1, float y1, float width, float height) {
923 drawOval(x1, y1, width, height, DEFAULT_SEGMENTS);
924 }
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940 void drawOval(float x1, float y1, float width, float height, int segments) {
941 drawArc(x1, y1, width, height, segments, 0, 360);
942 }
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960 public void drawArc(float x1, float y1, float width, float height, float start, float end) {
961 drawArc(x1, y1, width, height, DEFAULT_SEGMENTS, start, end);
962 }
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982 void drawArc(float x1, float y1, float width, float height, int segments, float start, float end) {
983 predraw();
984 TextureImpl.bindNone();
985 currentColor.bind();
986
987 while (end < start) {
988 end += 360;
989 }
990
991 float cx = x1 + (width / 2.0f);
992 float cy = y1 + (height / 2.0f);
993
994 LSR.start();
995 int step = 360 / segments;
996
997 for (int a = (int) start; a < (int) (end + step); a += step) {
998 float ang = a;
999 if (ang > end) {
1000 ang = end;
1001 }
1002 float x = (float) (cx + (FastTrig.cos(Math.toRadians(ang)) * width / 2.0f));
1003 float y = (float) (cy + (FastTrig.sin(Math.toRadians(ang)) * height / 2.0f));
1004
1005 LSR.vertex(x, y);
1006 }
1007 LSR.end();
1008 postdraw();
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023 public void fillOval(float x1, float y1, float width, float height) {
1024 fillOval(x1, y1, width, height, DEFAULT_SEGMENTS);
1025 }
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041 void fillOval(float x1, float y1, float width, float height, int segments) {
1042 fillArc(x1, y1, width, height, segments, 0, 360);
1043 }
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061 public void fillArc(float x1, float y1, float width, float height, float start, float end) {
1062 fillArc(x1, y1, width, height, DEFAULT_SEGMENTS, start, end);
1063 }
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083 void fillArc(float x1, float y1, float width, float height, int segments, float start, float end) {
1084 predraw();
1085 TextureImpl.bindNone();
1086 currentColor.bind();
1087
1088 while (end < start) {
1089 end += 360;
1090 }
1091
1092 float cx = x1 + (width / 2.0f);
1093 float cy = y1 + (height / 2.0f);
1094
1095 GL.glBegin(SGL.GL_TRIANGLE_FAN);
1096 int step = 360 / segments;
1097
1098 GL.glVertex2f(cx, cy);
1099
1100 for (int a = (int) start; a < (int) (end + step); a += step) {
1101 float ang = a;
1102 if (ang > end) {
1103 ang = end;
1104 }
1105
1106 float x = (float) (cx + (FastTrig.cos(Math.toRadians(ang)) * width / 2.0f));
1107 float y = (float) (cy + (FastTrig.sin(Math.toRadians(ang)) * height / 2.0f));
1108
1109 GL.glVertex2f(x, y);
1110 }
1111 GL.glEnd();
1112
1113 if (antialias) {
1114 GL.glBegin(SGL.GL_TRIANGLE_FAN);
1115 GL.glVertex2f(cx, cy);
1116 if (end != 360) {
1117 end -= 10;
1118 }
1119
1120 for (int a = (int) start; a < (int) (end + step); a += step) {
1121 float ang = a;
1122 if (ang > end) {
1123 ang = end;
1124 }
1125
1126 float x = (float) (cx + (FastTrig.cos(Math.toRadians(ang + 10)) * width / 2.0f));
1127 float y = (float) (cy + (FastTrig.sin(Math.toRadians(ang + 10)) * height / 2.0f));
1128
1129 GL.glVertex2f(x, y);
1130 }
1131 GL.glEnd();
1132 }
1133
1134 postdraw();
1135 }
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151 public void drawRoundRect(float x, float y, float width, float height, int cornerRadius) {
1152 drawRoundRect(x, y, width, height, cornerRadius, DEFAULT_SEGMENTS);
1153 }
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171 void drawRoundRect(float x, float y, float width, float height, int cornerRadius, int segs) {
1172 if (cornerRadius < 0)
1173 throw new IllegalArgumentException("corner radius must be > 0");
1174 if (cornerRadius == 0) {
1175 drawRect(x, y, width, height);
1176 return;
1177 }
1178
1179 int mr = (int) Math.min(width, height) / 2;
1180
1181 if (cornerRadius > mr) {
1182 cornerRadius = mr;
1183 }
1184
1185 drawLine(x + cornerRadius, y, x + width - cornerRadius, y);
1186 drawLine(x, y + cornerRadius, x, y + height - cornerRadius);
1187 drawLine(x + width, y + cornerRadius, x + width, y + height - cornerRadius);
1188 drawLine(x + cornerRadius, y + height, x + width - cornerRadius, y + height);
1189
1190 float d = cornerRadius * 2;
1191
1192 drawArc(x + width - d, y + height - d, d, d, segs, 0, 90);
1193
1194 drawArc(x, y + height - d, d, d, segs, 90, 180);
1195
1196 drawArc(x + width - d, y, d, d, segs, 270, 360);
1197
1198 drawArc(x, y, d, d, segs, 180, 270);
1199 }
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215 public void fillRoundRect(float x, float y, float width, float height, int cornerRadius) {
1216 fillRoundRect(x, y, width, height, cornerRadius, DEFAULT_SEGMENTS);
1217 }
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235 void fillRoundRect(float x, float y, float width, float height, int cornerRadius, int segs) {
1236 if (cornerRadius < 0)
1237 throw new IllegalArgumentException("corner radius must be > 0");
1238 if (cornerRadius == 0) {
1239 fillRect(x, y, width, height);
1240 return;
1241 }
1242
1243 int mr = (int) Math.min(width, height) / 2;
1244
1245 if (cornerRadius > mr) {
1246 cornerRadius = mr;
1247 }
1248
1249 float d = cornerRadius * 2;
1250
1251 fillRect(x + cornerRadius, y, width - d, cornerRadius);
1252 fillRect(x, y + cornerRadius, cornerRadius, height - d);
1253 fillRect(x + width - cornerRadius, y + cornerRadius, cornerRadius, height - d);
1254 fillRect(x + cornerRadius, y + height - cornerRadius, width - d, cornerRadius);
1255 fillRect(x + cornerRadius, y + cornerRadius, width - d, height - d);
1256
1257
1258 fillArc(x + width - d, y + height - d, d, d, segs, 0, 90);
1259
1260 fillArc(x, y + height - d, d, d, segs, 90, 180);
1261
1262 fillArc(x + width - d, y, d, d, segs, 270, 360);
1263
1264 fillArc(x, y, d, d, segs, 180, 270);
1265 }
1266
1267
1268
1269
1270
1271
1272
1273 public void setLineWidth(float width) {
1274 predraw();
1275 this.lineWidth = width;
1276 LSR.setWidth(width);
1277 GL.glPointSize(width);
1278 postdraw();
1279 }
1280
1281
1282
1283
1284
1285
1286 float getLineWidth() {
1287 return lineWidth;
1288 }
1289
1290
1291
1292
1293 public void resetLineWidth() {
1294 predraw();
1295
1296 Renderer.getLineStripRenderer().setWidth(1.0f);
1297 GL.glLineWidth(1.0f);
1298 GL.glPointSize(1.0f);
1299
1300 postdraw();
1301 }
1302
1303
1304
1305
1306
1307
1308
1309 public void setAntiAlias(boolean anti) {
1310 predraw();
1311 antialias = anti;
1312 LSR.setAntiAlias(anti);
1313 if (anti) {
1314 GL.glEnable(SGL.GL_POLYGON_SMOOTH);
1315 } else {
1316 GL.glDisable(SGL.GL_POLYGON_SMOOTH);
1317 }
1318 postdraw();
1319 }
1320
1321
1322
1323
1324
1325
1326 public boolean isAntiAlias() {
1327 return antialias;
1328 }
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340 public void drawString(String str, float x, float y) {
1341 predraw();
1342 font.drawString(x, y, str, currentColor);
1343 postdraw();
1344 }
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358 void drawImage(@Nonnull Image image, float x, float y, Color col) {
1359 predraw();
1360 image.draw(x, y, col);
1361 currentColor.bind();
1362 postdraw();
1363 }
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375 public void drawAnimation(@Nonnull Animation anim, float x, float y) {
1376 drawAnimation(anim, x, y, Color.white);
1377 }
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391 void drawAnimation(@Nonnull Animation anim, float x, float y, Color col) {
1392 predraw();
1393 anim.draw(x, y, col);
1394 currentColor.bind();
1395 postdraw();
1396 }
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408 protected void drawImage(@Nonnull Image image, float x, float y) {
1409 drawImage(image, x, y, Color.white);
1410 }
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436 void drawImage(@Nonnull Image image, float x, float y, float x2, float y2, float srcx, float srcy, float srcx2, float srcy2) {
1437 predraw();
1438 image.draw(x, y, x2, y2, srcx, srcy, srcx2, srcy2);
1439 currentColor.bind();
1440 postdraw();
1441 }
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463 public void drawImage(@Nonnull Image image, float x, float y, float srcx, float srcy, float srcx2, float srcy2) {
1464 drawImage(image, x, y, x + image.getWidth(), y + image.getHeight(), srcx, srcy, srcx2, srcy2);
1465 }
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478 public void copyArea(@Nonnull Image target, int x, int y) {
1479 copyArea(target, x, y, 0, 0, target.getWidth(), target.getHeight());
1480 }
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498 void copyArea(@Nonnull Image target, int x, int y, int xoff, int yoff, int width, int height) {
1499 predraw();
1500 target.getTexture();
1501 target.bind();
1502 if (isYFlipped()) {
1503 GL11.glCopyTexSubImage2D(SGL.GL_TEXTURE_2D, 0, xoff, yoff, x, y, width, height);
1504 } else {
1505 int yoff2 = target.getHeight()-height-yoff;
1506 GL11.glCopyTexSubImage2D(SGL.GL_TEXTURE_2D, 0, xoff, yoff2, x, screenHeight - (y+height), width, height);
1507 target.ensureInverted();
1508 }
1509 postdraw();
1510 }
1511
1512
1513
1514
1515
1516
1517
1518
1519 private int translate(byte b) {
1520 if (b < 0) {
1521 return 256 + b;
1522 }
1523
1524 return b;
1525 }
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536 @Nonnull
1537 public Color getPixel(int x, int y) {
1538 predraw();
1539 GL.glReadPixels(x, screenHeight - y, 1, 1, SGL.GL_RGBA, SGL.GL_UNSIGNED_BYTE, readBuffer);
1540 postdraw();
1541
1542 return new Color(translate(readBuffer.get(0)), translate(readBuffer.get(1)), translate(readBuffer.get(2)), translate(readBuffer.get(3)));
1543 }
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559 public void getArea(int x, int y, int width, int height, @Nonnull ByteBuffer target) {
1560 if (target.capacity() < width * height * 4) {
1561 throw new IllegalArgumentException("Byte buffer provided to get area is not big enough");
1562 }
1563
1564 predraw();
1565 GL.glReadPixels(x, screenHeight - y - height, width, height, SGL.GL_RGBA, SGL.GL_UNSIGNED_BYTE, target);
1566 postdraw();
1567 }
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595 void drawImage(@Nonnull Image image, float x, float y, float x2, float y2, float srcx, float srcy, float srcx2, float srcy2, Color col) {
1596 predraw();
1597 image.draw(x, y, x2, y2, srcx, srcy, srcx2, srcy2, col);
1598 currentColor.bind();
1599 postdraw();
1600 }
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624 public void drawImage(@Nonnull Image image, float x, float y, float srcx, float srcy, float srcx2, float srcy2, Color col) {
1625 drawImage(image, x, y, x + image.getWidth(), y + image.getHeight(), srcx, srcy, srcx2, srcy2, col);
1626 }
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656 public void drawGradientLine(float x1, float y1, float red1, float green1, float blue1, float alpha1, float x2, float y2, float red2,
1657 float green2, float blue2, float alpha2) {
1658 predraw();
1659
1660 TextureImpl.bindNone();
1661
1662 GL.glBegin(SGL.GL_LINES);
1663
1664 GL.glColor4f(red1, green1, blue1, alpha1);
1665 GL.glVertex2f(x1, y1);
1666
1667 GL.glColor4f(red2, green2, blue2, alpha2);
1668 GL.glVertex2f(x2, y2);
1669
1670 GL.glEnd();
1671
1672 postdraw();
1673 }
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691 public void drawGradientLine(float x1, float y1, @Nonnull Color Color1, float x2, float y2, @Nonnull Color Color2) {
1692 predraw();
1693
1694 TextureImpl.bindNone();
1695
1696 GL.glBegin(SGL.GL_LINES);
1697
1698 Color1.bind();
1699 GL.glVertex2f(x1, y1);
1700
1701 Color2.bind();
1702 GL.glVertex2f(x2, y2);
1703
1704 GL.glEnd();
1705
1706 postdraw();
1707 }
1708
1709
1710
1711
1712
1713
1714 void pushTransform() {
1715 predraw();
1716
1717 FloatBuffer buffer;
1718 if (stackIndex >= stack.size()) {
1719 buffer = BufferUtils.createFloatBuffer(18);
1720 stack.add(buffer);
1721 } else {
1722 buffer = stack.get(stackIndex);
1723 }
1724
1725 GL.glGetFloat(SGL.GL_MODELVIEW_MATRIX, buffer);
1726 buffer.put(16, sx);
1727 buffer.put(17, sy);
1728 stackIndex++;
1729
1730 postdraw();
1731 }
1732
1733
1734
1735
1736
1737 void popTransform() {
1738 if (stackIndex == 0) {
1739 throw new RuntimeException("Attempt to pop a transform that hasn't be pushed");
1740 }
1741
1742 predraw();
1743
1744 stackIndex--;
1745 FloatBuffer oldBuffer = stack.get(stackIndex);
1746 GL.glLoadMatrix(oldBuffer);
1747 sx = oldBuffer.get(16);
1748 sy = oldBuffer.get(17);
1749
1750 postdraw();
1751 }
1752
1753
1754
1755
1756
1757 public void destroy() {
1758
1759 }
1760
1761 protected boolean isYFlipped() {
1762 return false;
1763 }
1764 }