forked from phu004/JavaRTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextRenderer.java
More file actions
323 lines (254 loc) · 8.89 KB
/
Copy pathtextRenderer.java
File metadata and controls
323 lines (254 loc) · 8.89 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
package gui;
import java.awt.Image;
import java.awt.image.PixelGrabber;
import javax.imageio.ImageIO;
import core.mainThread;
//handle text rendering
public class textRenderer {
public int[] fontBuffer, menuFontBuffer, star, halfStar;
public int[][] chars, menuChars;
public int[] menuCharsWidth;
public void init(){
fontBuffer = new int[665*16];
menuFontBuffer = new int[789*16];
star = new int[12*12];
halfStar = new int[12*12];
//load font image
Image img = null;
try{
img = ImageIO.read(getClass().getResource("../images/" + "font.jpg"));
}catch(Exception e){
e.printStackTrace();
}
PixelGrabber pg = new PixelGrabber(img, 0, 0, 665, 16, fontBuffer, 0, 665);
try {
pg.grabPixels();
}catch(Exception e){
e.printStackTrace();
}
try{
img = ImageIO.read(getClass().getResource("../images/" + "menuFont.png"));
}catch(Exception e){
e.printStackTrace();
}
pg = new PixelGrabber(img, 0, 0, 789, 16, menuFontBuffer, 0, 789);
try {
pg.grabPixels();
}catch(Exception e){
e.printStackTrace();
}
//create character bitmaps for in game text
chars = new int[93][16*7];
for(int i = 0; i < 93; i++){
for(int j = 0; j < 16; j++){
for(int k = 0; k < 7; k++){
chars[i][k+ j*7] = fontBuffer[i*7 + k + j*665];
}
}
}
//create character bitmaps for menu text
menuChars = new int[95][];
int[] charEndPosition = new int[]{6,9,15,25,34,46,57,61,65,69,76,86,90,95,99, 105,114,120,130,138,146,155, 163,172,180,189,193,197,206,216,225,234,246,259,267,280,291,300,307,320,330,334,340, 350,357,372,382,396,404,419,428,435,443, 451,463,478,487,496,503,507,516,520,530,539,545,555,565,574,585,594,600,610,619,622,625,633,636,650,659,668,679,689,694,700,705,714,722,735,743,750,757,763,770,778,787};
int[] charStartPosition = new int[95];
menuCharsWidth = new int[95];
for(int i = 1; i < 95; i++) {
menuCharsWidth[i] = charEndPosition[i] - charEndPosition[i-1];
charStartPosition[i] = charEndPosition[i-1] + 1;
}
menuCharsWidth[0] = 6;
charStartPosition[0] = 0;
for(int i = 0; i < 95; i++) {
menuChars[i] = new int[menuCharsWidth[i] * 16];
for(int j = 0; j < 16; j++) {
for(int k = 0; k < menuCharsWidth[i]; k++) {
menuChars[i][k+j*menuCharsWidth[i]] = menuFontBuffer[charStartPosition[i] + k + j*789];
}
}
}
//load half star images
try{
img = ImageIO.read(getClass().getResource("../images/" + "84.jpg"));
}catch(Exception e){
e.printStackTrace();
}
pg = new PixelGrabber(img, 0, 0, 12, 12, halfStar, 0, 12);
try {
pg.grabPixels();
}catch(Exception e){
e.printStackTrace();
}
//load star images
try{
img = ImageIO.read(getClass().getResource("../images/" + "85.jpg"));
}catch(Exception e){
e.printStackTrace();
}
pg = new PixelGrabber(img, 0, 0, 12, 12, star, 0, 12);
try {
pg.grabPixels();
}catch(Exception e){
e.printStackTrace();
}
}
public void drawMenuText(int xPos, int yPos, char[] theText, int[] screen, int r, int g, int b, int filterBrightness) {
int pixel, SpriteValue, screenValue, overflow, screenIndex;
int MASK7Bit = 0xFEFEFF;
int xPos_initial = xPos;
for(int i = 0; i < theText.length; i++){
if(theText[i] == 10) {
yPos+=16;
xPos = xPos_initial;
continue;
}
int charIndex = theText[i] - 32;
int w = menuCharsWidth[charIndex];
int pos = 768*yPos + xPos;
for(int j = 0; j < 16; j++){
for(int k = 0; k < w; k++){
screenIndex = pos + k + j*768;
screenValue = screen[screenIndex];
SpriteValue = menuChars[charIndex][k+ j*w]&255;
if(SpriteValue < filterBrightness)
continue;
SpriteValue = (r*SpriteValue/256) << 16 | (g*SpriteValue/256) << 8 | (b*SpriteValue/256);
pixel=(SpriteValue&MASK7Bit)+(screenValue&MASK7Bit);
overflow=pixel&0x1010100;
overflow=overflow-(overflow>>8);
screen[screenIndex] = overflow|pixel;
}
}
xPos+=w;
}
}
public int getMenuTextWidth(char[] theText) {
int w = 0;
for(int i = 0; i< theText.length; i++)
w+=menuCharsWidth[theText[i] - 32];
return w;
}
public void drawFlashingText(int xPos, int yPos, String text, int[] screen){
int pixel, SpriteValue, screenValue, overflow, screenIndex;
int MASK7Bit = 0xFEFEFF;
char[] theText = text.toCharArray();
int t = (int)(35 * Math.sin((double)mainThread.gameFrame/7)) + 35;
if(t > 64)
t = 64;
for(int i = 0; i < theText.length; i++){
for(int j = 0; j < 16; j++){
for(int k = 0; k < 7; k++){
screenIndex = 768*yPos + xPos + i*7 + k + j*768;
screenValue = screen[screenIndex];
SpriteValue = chars[theText[i] - 32][k+ j*7]&255;
SpriteValue = SpriteValue * t / 64;
SpriteValue = SpriteValue << 16 | SpriteValue << 8 | SpriteValue;
pixel=(SpriteValue&MASK7Bit)+(screenValue&MASK7Bit);
overflow=pixel&0x1010100;
overflow=overflow-(overflow>>8);
screen[screenIndex] = overflow|pixel;
}
}
}
}
public void drawText(int xPos, int yPos, String text, int[] screen, int r, int g, int b){
int pixel, SpriteValue, screenValue, overflow, screenIndex;
int MASK7Bit = 0xFEFEFF;
char[] theText = text.toCharArray();
for(int i = 0; i < theText.length; i++){
for(int j = 0; j < 16; j++){
for(int k = 0; k < 7; k++){
screenIndex = 768*yPos + xPos + i*7 + k + j*768;
screenValue = screen[screenIndex];
SpriteValue = chars[theText[i] - 32][k+ j*7]&255;
SpriteValue = (r*SpriteValue/256) << 16 | (g*SpriteValue/256) << 8 | (b*SpriteValue/256);
pixel=(SpriteValue&MASK7Bit)+(screenValue&MASK7Bit);
overflow=pixel&0x1010100;
overflow=overflow-(overflow>>8);
screen[screenIndex] = overflow|pixel;
}
}
}
}
public void drawText_outline(int xPos, int yPos, String text, int[] screen, int insideColor, int outlineColor){
int SpriteValue, screenIndex, width, height;
char[] theText = text.toCharArray();
//draw outline first
for(int i = 0; i < theText.length; i++){
for(int j = 0; j < 16; j++){
for(int k = 0; k < 7; k++){
screenIndex = 768*yPos + xPos + i*7 + k + j*768;
width = xPos + i*7 + k;
height = yPos + j;
if(width < 1 || width > 766 || height < 1 || height > 510)
continue;
SpriteValue = chars[theText[i] - 32][k+ j*7]&255;
if((SpriteValue & 0xff) > 100){
screen[screenIndex+1] = outlineColor;
screen[screenIndex-1] = outlineColor;
screen[screenIndex+768] = outlineColor;
screen[screenIndex-768] = outlineColor;
screen[screenIndex+769] = outlineColor;
screen[screenIndex+767] = outlineColor;
screen[screenIndex-769] = outlineColor;
screen[screenIndex-767] = outlineColor;
}
}
}
}
//draw inside
for(int i = 0; i < theText.length; i++){
for(int j = 0; j < 16; j++){
for(int k = 0; k < 7; k++){
screenIndex = 768*yPos + xPos + i*7 + k + j*768;
width = xPos + i*7 + k;
height = yPos + j;
if(width < 1 || width > 766 || height < 1 || height > 510)
continue;
SpriteValue = chars[theText[i] - 32][k+ j*7]&255;
if((SpriteValue & 0xff) > 100){
screen[screenIndex] = insideColor;
}
}
}
}
}
public void drawStarCharacter(int xPos, int yPos, int starShape, int[] screen, int insideColor, int outlineColor){
//draw outline first
int[] starCharacter = star;
if(starShape == 1)
starCharacter = halfStar;
int SpriteValue, screenIndex, width, height;
for(int j = 0; j < 12; j++){
for(int k = 0; k < 12; k++){
screenIndex = 768*yPos + xPos + k + j*768;
width = xPos + k;
height = yPos + j;
if(width < 1 || width > 766 || height < 1 || height > 510)
continue;
SpriteValue = (starCharacter[k+ j*12]&0xff0000) >> 16;
if((SpriteValue & 0xff) > 30){
screen[screenIndex+1] = outlineColor;
screen[screenIndex-1] = outlineColor;
screen[screenIndex+768] = outlineColor;
screen[screenIndex-768] = outlineColor;
screen[screenIndex+769] = outlineColor;
screen[screenIndex+767] = outlineColor;
screen[screenIndex-769] = outlineColor;
screen[screenIndex-767] = outlineColor;
}
}
}
for(int j = 0; j < 12; j++){
for(int k = 0; k < 12; k++){
screenIndex = 768*yPos + xPos + k + j*768;
width = xPos+ k;
height = yPos + j;
if(width < 1 || width > 766 || height < 1 || height > 510)
continue;
SpriteValue = (starCharacter[k+ j*12]&0xff0000) >> 16;
if((SpriteValue & 0xff) > 30){
screen[screenIndex] = insideColor;
}
}
}
}
}