1 //------------------------------------------------------------------------------
2 //  debugtext.d
3 //
4 //  Test sokol_debugtext.h
5 //------------------------------------------------------------------------------
6 module examples.debugtext;
7 
8 private:
9 
10 import sg = sokol.gfx;
11 import sglue = sokol.glue;
12 import sapp = sokol.app;
13 import sdtx = sokol.debugtext;
14 import log = sokol.log;
15 
16 extern (C):
17 @safe:
18 
19 // Font slots
20 immutable FONT_KC853 = 0;
21 immutable FONT_KC854 = 1;
22 immutable FONT_KC85 = 1;
23 immutable FONT_Z1013 = 2;
24 immutable FONT_CPC = 3;
25 immutable FONT_C64 = 4;
26 immutable FONT_ORIC = 5;
27 
28 struct State
29 {
30     // background color
31     sg.PassAction passAction = {
32         colors: [
33             {
34                 load_action: sg.LoadAction.Clear,
35                 clear_value: {r: 0.0, g: 0.125, b: 0.25, a: 1.0}
36             }
37         ]
38     };
39 }
40 
41 static State state;
42 
43 void init()
44 {
45     sg.Desc gfx = {environment: sglue.environment(),
46     logger: {func: &log.func}};
47     sg.setup(gfx);
48 
49     sdtx.Desc desc = {
50         fonts: [
51             sdtx.fontKc853(),
52             sdtx.fontKc854(),
53             sdtx.fontZ1013(),
54             sdtx.fontCpc(),
55             sdtx.fontC64(),
56             sdtx.fontOric()
57         ],
58         logger: {func: &log.func},
59     };
60     sdtx.setup(desc);
61 }
62 
63 void print_font(uint font_index, string title, ubyte r, ubyte g, ubyte b)
64 {
65     sdtx.font(font_index);
66     sdtx.color3b(r, g, b);
67     sdtx.puts(&title[0]);
68     foreach (c; 32 .. 255)
69     {
70         sdtx.putc(cast(char) c);
71         if (((c + 1) & 63) == 0)
72         {
73             sdtx.crlf();
74         }
75     }
76     sdtx.crlf();
77 }
78 
79 void frame()
80 {
81     sdtx.canvas(sapp.widthf() * 0.5, sapp.heightf() * 0.5);
82     sdtx.origin(0.0, 2.0);
83     sdtx.home();
84 
85     print_font(FONT_KC853, "KC85/3:\n", 0xf4, 0x43, 0x36);
86     print_font(FONT_KC854, "KC85/4:\n", 0x21, 0x96, 0xf3);
87     print_font(FONT_Z1013, "Z1013:\n", 0x4c, 0xaf, 0x50);
88     print_font(FONT_CPC, "Amstrad CPC:\n", 0xff, 0xeb, 0x3b);
89     print_font(FONT_C64, "C64:\n", 0x79, 0x86, 0xcb);
90     print_font(FONT_ORIC, "Oric Atmos:\n", 0xff, 0x98, 0x00);
91 
92     sg.Pass pass = {action: state.passAction, swapchain: sglue.swapchain()};
93     sg.beginPass(pass);
94     sdtx.draw();
95     sg.endPass();
96     sg.commit();
97 }
98 
99 void cleanup()
100 {
101     sdtx.shutdown();
102     sg.shutdown();
103 }
104 
105 void main()
106 {
107     sapp.Desc runner = {
108         window_title: "debugtext.d",
109         init_cb: &init,
110         frame_cb: &frame,
111         cleanup_cb: &cleanup,
112         width: 1024,
113         height: 600,
114         icon: {sokol_default: true},
115         logger: {func: &log.func}
116     };
117     sapp.run(runner);
118 }