#include <stdlib.h>
#include <string.h>
#include <math.h>
+#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include "demo.h"
++nframes;
}
-void demo_keyboard(int key, int state)
+static void change_screen(int idx)
{
- if(state) {
+ printf("change screen %d\n", idx);
+ scr_change(scr_screen(idx), 4000);
+}
+
+#define CBUF_SIZE 64
+#define CBUF_MASK (CBUF_SIZE - 1)
+void demo_keyboard(int key, int press)
+{
+ static char cbuf[CBUF_SIZE];
+ static int rd, wr;
+ char inp[CBUF_SIZE + 1], *dptr;
+ int i, nscr;
+
+ if(press) {
switch(key) {
case 27:
demo_quit();
break;
+ case '\n':
+ case '\r':
+ dptr = inp;
+ while(rd != wr) {
+ *dptr++ = cbuf[rd];
+ rd = (rd + 1) & CBUF_MASK;
+ }
+ *dptr = 0;
+ if(inp[0]) {
+ printf("trying to match: %s\n", inp);
+ nscr = scr_num_screens();
+ for(i=0; i<nscr; i++) {
+ if(strstr(scr_screen(i)->name, inp)) {
+ change_screen(i);
+ break;
+ }
+ }
+ }
+ break;
+
default:
- if(key >= '1' && key <= '1' + scr_num_screens()) {
- int idx = key - '1';
- printf("change screen %d\n", idx);
- scr_change(scr_screen(idx), 4000);
+ if(key >= '1' && key <= '9' && key <= '1' + scr_num_screens()) {
+ change_screen(key - '1');
+ } else if(key == '0' && scr_num_screens() >= 10) {
+ change_screen(9);
+ }
+
+ if(key < 256 && isprint(key)) {
+ cbuf[wr] = key;
+ wr = (wr + 1) & CBUF_MASK;
+ if(wr == rd) { /* overflow */
+ rd = (rd + 1) & CBUF_MASK;
+ }
}
break;
}
}
}
+
+
+void mouse_orbit_update(float *theta, float *phi, float *dist)
+{
+ static int prev_mx, prev_my;
+ static unsigned int prev_bmask;
+
+ if(mouse_bmask) {
+ if((mouse_bmask ^ prev_bmask) == 0) {
+ int dx = mouse_x - prev_mx;
+ int dy = mouse_y - prev_my;
+
+ if(dx || dy) {
+ if(mouse_bmask & 1) {
+ float p = *phi;
+ *theta += dx * 1.0;
+ p += dy * 1.0;
+
+ if(p < -90) p = -90;
+ if(p > 90) p = 90;
+ *phi = p;
+ }
+ if(mouse_bmask & 4) {
+ *dist += dy * 0.5;
+
+ if(*dist < 0) *dist = 0;
+ }
+ }
+ }
+ }
+ prev_mx = mouse_x;
+ prev_my = mouse_y;
+ prev_bmask = mouse_bmask;
+}
void demo_draw(void);
-void demo_keyboard(int key, int state);
+void demo_keyboard(int key, int press);
/* defined in main_*.c */
*/
void swap_buffers(void *pixels);
+/* call each frame to get 3D viewing spherical coordinates */
+void mouse_orbit_update(float *theta, float *phi, float *dist);
+
#endif /* DEMO_H_ */
g3d_matrix_mode(G3D_MODELVIEW);
g3d_load_identity();
g3d_translate(0, 0, -cam_dist);
+ g3d_rotate(cam_phi, 1, 0, 0);
+ g3d_rotate(cam_theta, 0, 1, 0);
if(opt.sball) {
g3d_mult_matrix(sball_matrix);
- } else {
- g3d_rotate(cam_phi, 1, 0, 0);
- g3d_rotate(cam_theta, 0, 1, 0);
}
memcpy(cur_smokebuf, prev_smokebuf, smokebuf_size);
--- /dev/null
+#include <stdio.h>
+#include <math.h>
+#include "demo.h"
+#include "3dgfx.h"
+#include "screen.h"
+#include "cfgopt.h"
+
+static int init(void);
+static void destroy(void);
+static void start(long trans_time);
+static void draw(void);
+static void draw_cube(void);
+
+static struct screen scr = {
+ "infcubes",
+ init,
+ destroy,
+ start, 0,
+ draw
+};
+
+static float cam_theta, cam_phi;
+static float cam_dist = 5;
+
+struct screen *infcubes_screen(void)
+{
+ return &scr;
+}
+
+
+static int init(void)
+{
+ return 0;
+}
+
+static void destroy(void)
+{
+}
+
+static void start(long trans_time)
+{
+ g3d_matrix_mode(G3D_PROJECTION);
+ g3d_load_identity();
+ g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
+
+ g3d_enable(G3D_LIGHTING);
+ g3d_enable(G3D_LIGHT0);
+}
+
+static void update(void)
+{
+ mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
+}
+
+static void draw(void)
+{
+ update();
+
+ g3d_matrix_mode(G3D_MODELVIEW);
+ g3d_load_identity();
+ g3d_translate(0, 0, -cam_dist);
+ g3d_rotate(cam_phi, 1, 0, 0);
+ g3d_rotate(cam_theta, 0, 1, 0);
+ if(opt.sball) {
+ g3d_mult_matrix(sball_matrix);
+ }
+
+ memset(fb_pixels, 0, fb_width * fb_height * 2);
+
+ draw_cube();
+
+ swap_buffers(fb_pixels);
+}
+
+static void draw_cube(void)
+{
+ g3d_begin(G3D_QUADS);
+ g3d_normal(0, 0, 1);
+ g3d_vertex(-1, -1, 1);
+ g3d_vertex(1, -1, 1);
+ g3d_vertex(1, 1, 1);
+ g3d_vertex(-1, 1, 1);
+ g3d_normal(1, 0, 0);
+ g3d_vertex(1, -1, 1);
+ g3d_vertex(1, -1, -1);
+ g3d_vertex(1, 1, -1);
+ g3d_vertex(1, 1, 1);
+ g3d_normal(0, 0, -1);
+ g3d_vertex(1, -1, -1);
+ g3d_vertex(-1, -1, -1);
+ g3d_vertex(-1, 1, -1);
+ g3d_vertex(1, 1, -1);
+ g3d_normal(-1, 0, 0);
+ g3d_vertex(-1, -1, -1);
+ g3d_vertex(-1, -1, 1);
+ g3d_vertex(-1, 1, 1);
+ g3d_vertex(-1, 1, -1);
+ g3d_normal(0, 1, 0);
+ g3d_vertex(-1, 1, 1);
+ g3d_vertex(1, 1, 1);
+ g3d_vertex(1, 1, -1);
+ g3d_vertex(-1, 1, -1);
+ g3d_normal(0, -1, 0);
+ g3d_vertex(1, -1, 1);
+ g3d_vertex(-1, -1, 1);
+ g3d_vertex(-1, -1, -1);
+ g3d_vertex(1, -1, -1);
+ g3d_end();
+}
struct screen *thunder_screen(void);
struct screen *metaballs_screen(void);
struct screen *greets_screen(void);
+struct screen *infcubes_screen(void);
#define NUM_SCR 32
static struct screen *scr[NUM_SCR];
if(!(scr[idx++] = greets_screen())) {
return -1;
}
+ if(!(scr[idx++] = infcubes_screen())) {
+ return -1;
+ }
num_screens = idx;
assert(num_screens <= NUM_SCR);