/
app.d
374 lines (275 loc) · 14.3 KB
/
app.d
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
import std.stdio;
import Font = razor_font;
import Window = window.window;
import Camera = camera.camera;
import Shader = shader.shader;
import Texture = texture.texture;
import Math = doml.math;
import mesh.mesh;
import doml.vector_2d;
//! IMPORTANT: If you did not read the simple tutorial, I highly recommend you go and do that!
//! Only NEW pieces will be explained!
//! https://github.com/jordan4ibanez/RazorFontExampleProject
/**
Also another note:
There have been changes to shaders/2d_fragment.fs to allow RazorFont to use
these more advanced features. I suggest you give it a glance. :D
*/
void main()
{
Window.initialize();
Window.setTitle("RazorFont Example Intermediate");
Shader.create("2d", "shaders/2d_vertex.vs", "shaders/2d_fragment.fs");
Shader.createUniform("2d", "cameraMatrix");
Shader.createUniform("2d", "objectMatrix");
Shader.createUniform("2d", "textureSampler");
Font.setRenderTargetAPICallString(
(string input){
Texture.addTexture(input);
}
);
Font.setRenderFunc(
(Font.RazorFontData fontData) {
string fileLocation = Font.getCurrentFontTextureFileLocation();
Mesh tempObject = new Mesh()
.addVertices2d(fontData.vertexPositions)
.addIndices(fontData.indices)
.addTextureCoordinates(fontData.textureCoordinates)
// Note here: We added a new layout storage element into our Mesh class!
.addColors(fontData.colors)
.setTexture(Texture.getTexture(fileLocation))
.finalize();
tempObject.render("2d");
tempObject.cleanUp();
}
);
Font.createFont("example_fonts/totally_original", "mc", true);
// We're only going to use the mc font this time
Font.selectFont("mc");
// You'll find out what this is for later!
double theBeach = 0.0;
double rainbowsX = 0.0;
double rainbowsY = 0.0;
while (!Window.shouldClose()) {
Window.pollEvents();
Camera.clearDepthBuffer();
Window.clear(0.9);
Shader.startProgram("2d");
Font.setCanvasSize(Window.getWidth, Window.getHeight);
Shader.setUniformMatrix4("2d", "cameraMatrix", Camera.updateGuiMatrix());
Shader.setUniformMatrix4("2d", "objectMatrix", Camera.setGuiObjectMatrix() );
{
// So you can set the letter colors before....
Font.setColorChar(0, 1,0,0,0.5);
Font.setColorChar(1, 0,1,0);
Font.setColorChar(2, 0,0,1);
int fontSize = 50;
string textString = "Hello, I'm your test text :)";
Font.RazorTextSize textSize = Font.getTextSize(fontSize, textString);
// Now we're going to move slightly above the center point of the window
double posX = (Window.getWidth / 2.0) - (textSize.width / 2.0);
double posY = (Window.getHeight / 2.0) - (textSize.height / 2.0);
Font.renderToCanvas(posX, posY, fontSize, textString);
/**
...And after rendering to canvas. The color cache is always there.
I recommend doing it after so you can work backwards from the next position/
This is explained more later on!
*/
Font.setColorChar(3, 0.5,0.5,0.5);
/**
Let's do something REALLY fancy!
Let's set the second l and o to a blue to white blend
*/
Font.setColorPoints(4,/*Left points*/[0.,0,1,1],[0.,0,1,1], /*Right points*/[1.,1,1,1],[1.,1,1,1]);
Font.setColorPoints(5,/*Left points*/[0.,0,1,1],[0.,0,1,1], /*Right points*/[1.,1,1,1],[1.,1,1,1]);
/**
Now let's get even crazier! Let's do primary colors on the first 3,
and yellow on the last one, I'm going to use the super verbose version of setColorPoints.
It's overloaded :D
This is going into the letter I in (I'm)
*/
Font.setColorPoints(
6,
1,0,0,1,
0,1,0,1,
0,0,1,1,
1,1,0,1,
);
/**
Looks kinda like a mat4 opengl and vulkan matrix. :P
So this one just uses pcgRandom to make some crazy colors. Pretty standard. :D
You can see that the range func does an inclusive start, exclusive end
just like you're used to in D by the following code:
*/
// This is targeting ('m) in (I'm)
Font.setColorRange(7,9, Math.random(),Math.random(),Math.random(),1);
/**
So now, straight to the insane asylum. Let's animate this text with colors!
It's almost spring at the time of writing this, and I want to go fishing.
So let's make this look like the waves of the ocean.
*/
{
import delta_time;
calculateDelta();
theBeach += getDelta();
if (theBeach > Math.PI) {
theBeach -= Math.PI2;
}
/*
So you know how we rendered to the canvas before hand?
Well, now we have the amount of characters currently on
the canvas, so we can work backwards from that with ease!
You could also use this to set up the next word in a multi-render
to canvas to do some even crazier stuff :)
Keep in mind, your typewriter arm is already above what would be the next character
in the row without a carriage return.
-1 shifts it onto the last currently rendered character.
There's a reason that this isn't a const here, but I recommend you do that to avoid confusion.
*/
int currentIndex = Font.getCurrentCharacterIndex() - 10;
for (int i = currentIndex; i < currentIndex + 10; i++) {
// We want this to be a smooth flowing animation loop
// So we're gonna use some fancy math
const double left = theBeach + cast(double)i;
//! This is what I'm talking about below.
const double right = left + 1.0;
const double foam = 0.5;
double waterFlowLeft = (Math.sin(left) / 2.0) + foam;
double waterFlowRight = (Math.sin(right) / 2.0) + foam;
/**
So what we're doing here is basically creating a fixed,
smooth loop of data that's equal to an oscillating wave
form on a 2d plotter. We're starting from the left point
of that (data + index) to get somewhere on that data stream.
That's the left side done, so the easiest way to make this
look ultra smooth is to just poll to the right to make
the compression of the 1d data stream get automatically mapped by opengl
into emulating the foam of a wave at the beach.
I left a little comment up above where the right half is, try changing
that to 2.5 or 3.5 and see what happens. :)
Now don't read the next part until you've tried that.
What you just did, was you created an overlap of the polling
into the data stream of the next character, looks pretty neat huh?
Makes things act a bit random and I suppose you could say "The sea gets rougher"
*/
Font.setColorPoints(i,
waterFlowLeft,waterFlowLeft,1,1, // left
waterFlowLeft,waterFlowLeft,1,1,
waterFlowRight,waterFlowRight,1,1, //right
waterFlowRight,waterFlowRight,1,1
);
}
/**
Did you notice that "your" is still solid black?
The default buffer color is 0,0,0,1 rgba!
Now remember, colors are in the color cache. If you want to reset the entire cache.
Render, it moves the cursor back to 0, then run:
Font.switchColors(r,g,b,a);
You don't have to do this at all, but I thought someone could use it for something.
So I added it in!
So now I'm going to show you some magical rainbows.
Change this to true, I wanted you to focus on what was
happening on the center text before.
*/
if (false) {
// We're going to use a special library I have for this :)
import fast_noise;
FNLState noise = fnlCreateState(1337);
noise.noise_type = FNLNoiseType.FNL_NOISE_OPENSIMPLEX2S;
noise.frequency = 1.0;
/**
So the noise can go -1, 1 so we're gonna fix that!
Now it can only go from 0.0 to 1.0 to match our RGB :D
*/
double fixNoise(double input) {
input /= 2.0;
return Math.clamp(0.0, 1.0, input + 0.5);
}
// A reminder that if you store this as a string, there are tons of D helper functions!
string myCoolString = "I'm a magical rainbow flying through the sky!";
// Let's render at the top left this time
Font.renderToCanvas(0,0, 62, myCoolString);
/*
Oh look, we're using those two vars from earlier.
Let's send them on a long journey through the data void.
*/
rainbowsX += getDelta();
rainbowsY += getDelta();
// Easily 0 yourself back out to where you want. So here is: I in (I'm)
currentIndex = Font.getCurrentCharacterIndex() - Font.getTextRenderableCharsLength(myCoolString);
// We're going to run this like it's a game, and your text is literally walking through a 2d noise map
for(int i = currentIndex; i < currentIndex + myCoolString.length; i++) {
// I put this here because I don't want to type cast(double) a bunch of times
double rootX = cast(double)i;
/**
Okay, time to get those dang ol positions
*/
double topLeftX = rootX + rainbowsX;
double topLeftY = rainbowsY;
double bottomLeftX = rootX + rainbowsX;
double bottomLeftY = 1 + rainbowsY;
double bottomRightX = 1 + rootX + rainbowsX;
double bottomRightY = 1 + rainbowsY;
double topRightX = 1 + rootX + rainbowsX;
double topRightY = rainbowsY;
/**
Now we have a 2d quad in pure positions! Neat!
So let's get that noise.
What we're doing here is we're polling RGB 100.0 units away from eachother
in the 2d noise map.
I wrote it out VERY verbosely so you can see exactly what this is doing!
*/
// Uncomment this to see the data stream
// if (i == 23) {
// writeln(fixNoise(fnlGetNoise2D(&noise, topLeftX, topLeftY)));
// }
double[4] topLeftRGB = [
fixNoise(fnlGetNoise2D(&noise, topLeftX, topLeftY)),
fixNoise(fnlGetNoise2D(&noise, topLeftX + 100.0, topLeftY)),
fixNoise(fnlGetNoise2D(&noise, topLeftX + 200.0, topLeftY)),
1 // Alpha
];
double[4] bottomLeftRGB = [
fixNoise(fnlGetNoise2D(&noise, bottomLeftX, bottomLeftY)),
fixNoise(fnlGetNoise2D(&noise, bottomLeftX + 100.0, bottomLeftY)),
fixNoise(fnlGetNoise2D(&noise, bottomLeftX + 200.0, bottomLeftY)),
1 // Alpha
];
double[4] bottomRightRGB = [
fixNoise(fnlGetNoise2D(&noise, bottomRightX, bottomRightY)),
fixNoise(fnlGetNoise2D(&noise, bottomRightX + 100.0, bottomRightY)),
fixNoise(fnlGetNoise2D(&noise, bottomRightX + 200.0, bottomRightY)),
1 // Alpha
];
double[4] topRightRGB = [
fixNoise(fnlGetNoise2D(&noise, topRightX, topRightY)),
fixNoise(fnlGetNoise2D(&noise, topRightX + 100.0, topRightY)),
fixNoise(fnlGetNoise2D(&noise, topRightX + 200.0, topRightY)),
1 // Alpha
];
// Now let's apply that to our text
Font.setColorPoints(i, topLeftRGB, bottomLeftRGB, bottomRightRGB, topRightRGB);
/**
Tada! Rainbow text wooo
*/
}
}
}
}
/**
One last thing I have to let you know. Did you notice that we
never set the font during the loop of the program?
If your program has only one font, you only need to set it at the
entry point and never look at it again!
Have fun. :D
*/
// This is our only OpenGL render call in this entire program!
Font.render();
// Update the gl window yada yada
Window.swapBuffers();
}
// Just regular ol opengl cleanup
Shader.deleteShader("2d");
Texture.cleanUp();
Window.destroy();
}