-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhello_render_to_texture.cpp
More file actions
81 lines (66 loc) · 3.02 KB
/
Copy pathhello_render_to_texture.cpp
File metadata and controls
81 lines (66 loc) · 3.02 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
#include <nothofagus.h>
#include <cmath>
int main()
{
// Canvas: 128x128 logical pixels, 6px scale.
Nothofagus::Canvas canvas({128, 128}, "Hello Render To Texture", {0.15f, 0.15f, 0.15f}, 6);
// --- Source sprites (rendered into the render target each frame) ---
Nothofagus::ColorPallete redPallete{
{0.0f, 0.0f, 0.0f, 0.0f}, // 0: transparent
{0.8f, 0.1f, 0.1f, 1.0f}, // 1: red
{1.0f, 0.5f, 0.5f, 1.0f}, // 2: light red
};
Nothofagus::IndirectTexture redTexture({8, 8}, {0.0f, 0.0f, 0.0f, 0.0f});
redTexture.setPallete(redPallete).setPixels({
0,0,1,1,1,1,0,0,
0,1,2,2,2,2,1,0,
1,2,2,1,1,2,2,1,
1,2,1,2,2,1,2,1,
1,2,1,2,2,1,2,1,
1,2,2,1,1,2,2,1,
0,1,2,2,2,2,1,0,
0,0,1,1,1,1,0,0,
});
Nothofagus::TextureId redTextureId = canvas.addTexture(redTexture);
Nothofagus::ColorPallete bluePallete{
{0.0f, 0.0f, 0.0f, 0.0f}, // 0: transparent
{0.1f, 0.1f, 0.8f, 1.0f}, // 1: blue
{0.5f, 0.5f, 1.0f, 1.0f}, // 2: light blue
};
Nothofagus::IndirectTexture blueTexture({8, 8}, {0.0f, 0.0f, 0.0f, 0.0f});
blueTexture.setPallete(bluePallete).setPixels({
0,1,1,1,1,1,1,0,
1,2,2,2,2,2,2,1,
1,2,0,0,0,0,2,1,
1,2,0,1,1,0,2,1,
1,2,0,1,1,0,2,1,
1,2,0,0,0,0,2,1,
1,2,2,2,2,2,2,1,
0,1,1,1,1,1,1,0,
});
Nothofagus::TextureId blueTextureId = canvas.addTexture(blueTexture);
// Source bellotas — positioned inside the 64x64 render target coordinate space.
Nothofagus::BellotaId redBellotaId = canvas.addBellota({{{22.0f, 32.0f}}, redTextureId});
Nothofagus::BellotaId blueBellotaId = canvas.addBellota({{{42.0f, 32.0f}}, blueTextureId});
// Main canvas bellota — rendered on top of the display bellota, not inside the render target.
Nothofagus::BellotaId blueBellota2Id = canvas.addBellota({{{62.0f, 32.0f}}, blueTextureId, 1});
// --- Render target: 64x64 pixels ---
Nothofagus::RenderTargetId renderTargetId = canvas.addRenderTarget({64, 64});
Nothofagus::TextureId renderTargetTextureId = canvas.renderTargetTexture(renderTargetId);
canvas.setRenderTargetClearColor(renderTargetId, {0.0f, 0.0f, 0.0f, 0.5f});
// Display bellota — samples the render target and shows it on the main canvas.
Nothofagus::BellotaId displayBellotaId = canvas.addBellota({{{64.0f, 64.0f}}, renderTargetTextureId});
float time = 0.0f;
canvas.run([&](float deltaTime)
{
time += deltaTime;
// Rotate source sprites over time.
canvas.bellota(redBellotaId).transform().angle() = 0.05f * time;
canvas.bellota(blueBellotaId).transform().angle() = -0.05f * time;
// Gently bob the display bellota.
canvas.bellota(displayBellotaId).transform().location().y = 64.0f + 8.0f * std::sin(0.002f * time);
// Schedule both source sprites to be rendered into the render target this frame.
canvas.renderTo(renderTargetId, {redBellotaId, blueBellotaId});
});
return 0;
}