e3aa0263dfcf954a48035f0c43b9023126d9ef72
[nexus3d] / test.c
1 #include <stdio.h>
2 #include "nexus3d.h"
3
4 static int init(void);
5 static void cleanup(void);
6
7 static void display(void *cls);
8 static void reshape(int x, int y, void *cls);
9 static void keyb(int key, int pressed, void *cls);
10 static void mbutton(int bn, int pressed, int x, int y, void *cls);
11 static void mmove(int x, int y, void *cls);
12
13 #define ATTR_POS        0
14 #define ATTR_COL        1
15
16 static const float vdata[] = {
17         -0.25f, -0.25f, 0.25f, 0.25f, -0.25f, 0.25f, 0.25f, -0.25f, -0.25f, -0.25f, -0.25f, -0.25f,
18         -0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, -0.25f, -0.25f, 0.25f, -0.25f
19 };
20 static const float vcolors[] = {
21         1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0,
22         0, 1, 1, 1, 0, 1, 1, 0.5, 0, 0.5, 0.5, 0.5
23 };
24
25 static const unsigned int idata[] = {
26         0, 1, 5,        0, 5, 4,
27         1, 2, 6,        1, 6, 5,
28         2, 3, 7,        2, 7, 6,
29         3, 0, 4,        3, 4, 7,
30         4, 5, 6,        4, 6, 7,
31         1, 0, 3,        1, 3, 2
32 };
33
34
35 static int quit;
36 static nex_buffer *vbuf, *ibuf, *cbuf;
37 static nex_geometry *geom;
38 static nex_sdrprog *sdrprog;
39
40 int main(void)
41 {
42         nex_gfxapi_opengl(3, 3, NEX_OPENGL_DEBUG);
43         if(nex_initgfx(1280, 800, 0) == -1) {
44                 return 1;
45         }
46
47         nex_cbdisplay(display, 0);
48         nex_cbreshape(reshape, 0);
49         nex_cbkey(keyb, 0);
50         nex_cbmousebn(mbutton, 0);
51         nex_cbmousemove(mmove, 0);
52
53         if(init() == -1) {
54                 goto end;
55         }
56
57         while(nex_evloop_wait() && !quit);
58
59 end:
60         cleanup();
61         nex_closegfx();
62         return 0;
63 }
64
65 static int init(void)
66 {
67         nex_clearcolor(0.1, 0.12, 0.2);
68
69         vbuf = nex_alloc_buffer(sizeof vdata, vdata);
70         cbuf = nex_alloc_buffer(sizeof vcolors, vcolors);
71         ibuf = nex_alloc_buffer(sizeof idata, idata);
72         geom = nex_alloc_geometry();
73         nex_geom_vbuffer(geom, 0, vbuf, 3 * sizeof(float));
74         nex_geom_vbuffer(geom, 1, cbuf, 3 * sizeof(float));
75         nex_geom_vattr(geom, ATTR_POS, NEX_VEC3, 0, 0);
76         nex_geom_vattr(geom, ATTR_COL, NEX_COL3, 1, 0);
77         nex_geom_ibuffer(geom, ibuf);
78
79         if(!(sdrprog = nex_load_sdrprog("test.v.spv", "test.p.spv"))) {
80                 return -1;
81         }
82         return 0;
83 }
84
85 static void cleanup(void)
86 {
87         nex_free_buffer(vbuf);
88         nex_free_buffer(cbuf);
89         nex_free_buffer(ibuf);
90         nex_free_geometry(geom);
91         nex_free_sdrprog(sdrprog);
92 }
93
94 static void display(void *cls)
95 {
96         nex_clear();
97
98         nex_bind_sdrprog(sdrprog);
99         nex_draw_geometry(geom, NEX_TRIANGLES, 0);
100
101         nex_swap_buffers();
102 }
103
104 static void reshape(int x, int y, void *cls)
105 {
106         nex_viewport(0, 0, x, y);
107 }
108
109 static void keyb(int key, int pressed, void *cls)
110 {
111         if(!pressed) return;
112
113         switch(key) {
114         case 27:
115                 quit = 1;
116                 break;
117         }
118 }
119
120 static void mbutton(int bn, int pressed, int x, int y, void *cls)
121 {
122 }
123
124 static void mmove(int x, int y, void *cls)
125 {
126 }