17 #define MOUSE_TIMEOUT 1200
22 int fb_width, fb_height, fb_bpp, fb_scan_size;
24 long fb_size, fb_buf_size;
25 uint16_t *fb_pixels, *vmem;
28 unsigned long time_msec;
30 unsigned int mouse_bmask;
32 static struct au_module *mod;
34 float sball_matrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
36 static unsigned long last_mouse_move;
37 static int prev_mx, prev_my, mouse_dx, mouse_dy;
38 static unsigned int bmask_diff, prev_bmask;
40 static unsigned long nframes;
41 static int con_active;
43 extern uint16_t loading_pixels[]; /* data.asm */
46 int demo_init(int argc, char **argv)
51 if(load_config("demo.cfg") == -1) {
54 if((env = getenv("START_SCR"))) {
57 if(parse_args(argc, argv) == -1) {
61 /* reuse the loading image as our back buffer.
62 * adjust fb_pixels to leave 4 pixels guard band top/bottom. We have enough
63 * space since the loading image is 8 pixels taller.
65 fb_pixels = loading_pixels + 320 * 4;
70 if(g3d_init() == -1) {
73 g3d_framebuffer(FB_WIDTH, FB_HEIGHT, fb_pixels);
76 if(!(mod = au_load_module("data/test.mod"))) {
81 if(scr_init() == -1) {
85 scr = scr_lookup(opt.start_scr);
90 if(!scr || scr_change(scr, 4000) == -1) {
91 fprintf(stderr, "screen %s not found\n", opt.start_scr ? opt.start_scr : "0");
95 /* clear the framebuffer at least once */
96 memset(fb_pixels, 0, FB_WIDTH * FB_HEIGHT * FB_BPP / CHAR_BIT);
104 void demo_cleanup(void)
113 float fps = (float)nframes / ((float)time_msec / 1000.0f);
114 printf("average framerate: %.1f\n", fps);
118 int demo_resizefb(int width, int height, int bpp)
120 int newsz, new_scansz;
122 if(!width || !height || !bpp) {
124 fb_buf = fb_pixels = 0;
125 fb_size = fb_buf_size = fb_scan_size = 0;
126 fb_width = fb_height = fb_bpp = 0;
130 new_scansz = ((width + GUARD_XPAD * 2) * bpp + 7) / 8;
131 newsz = (height + GUARD_YPAD * 2) * new_scansz;
133 if(!fb_buf || newsz > fb_buf_size) {
134 void *tmp = malloc(newsz);
142 fb_scan_size = new_scansz;
143 fb_pixels = (uint16_t*)((char*)fb_buf + GUARD_YPAD * fb_scan_size + (GUARD_XPAD * bpp + 7) / 8);
147 fb_size = fb_scan_size * fb_height;
149 fb_aspect = (float)fb_width / (float)fb_height;
158 mouse_dx = mouse_x - prev_mx;
159 mouse_dy = mouse_y - prev_my;
162 bmask_diff = mouse_bmask ^ prev_bmask;
163 prev_bmask = mouse_bmask;
164 if(mouse_dx | mouse_dy) {
165 last_mouse_move = time_msec;
178 /* called by swap_buffers just before the actual swap */
179 void demo_post_draw(void *pixels)
184 if(dbg_curscr_name) {
185 cs_dputs(pixels, dbg_curscr_name_pos, 240 - 16, dbg_curscr_name);
193 if(opt.mouse && time_msec - last_mouse_move < MOUSE_TIMEOUT) {
194 cs_mouseptr(pixels, mouse_x, mouse_y);
198 void cs_puts_font(cs_font_func csfont, int sz, void *fb, int x, int y, const char *str)
203 if(c > ' ' && c < 128) {
204 csfont(fb, x, y, c - ' ');
210 void change_screen(int idx)
212 printf("change screen %d\n", idx);
213 scr_change(scr_screen(idx), 4000);
216 void demo_keyboard(int key, int press)
236 con_active = !con_active;
247 con_active = con_input('/');
253 con_active = con_input(key);
257 if(key >= '1' && key <= '9' && key <= '1' + scr_num_screens()) {
258 change_screen(key - '1');
260 } else if(key == '0' && scr_num_screens() >= 10) {
271 void mouse_orbit_update(float *theta, float *phi, float *dist)
274 if(bmask_diff == 0) {
276 if(mouse_dx | mouse_dy) {
277 if(mouse_bmask & MOUSE_BN_LEFT) {
279 *theta += mouse_dx * 1.0;
286 if(mouse_bmask & MOUSE_BN_RIGHT) {
287 *dist += mouse_dy * 0.5;
289 if(*dist < 0) *dist = 0;