From 9ed338a5e703fc819cede5cb797e82e08046ac17 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Tue, 13 Feb 2018 20:47:25 +0200 Subject: [PATCH] starting infcubes part --- src/demo.c | 88 +++++++++++++++++++++++++++++++++++++++++---- src/demo.h | 5 ++- src/greets.c | 5 ++- src/infcubes.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/screen.c | 4 +++ 5 files changed, 201 insertions(+), 10 deletions(-) create mode 100644 src/infcubes.c diff --git a/src/demo.c b/src/demo.c index f25610a..6abcf67 100644 --- a/src/demo.c +++ b/src/demo.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include "demo.h" @@ -99,21 +100,96 @@ void demo_draw(void) ++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; iname, 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; +} diff --git a/src/demo.h b/src/demo.h index 60c7646..fdb238b 100644 --- a/src/demo.h +++ b/src/demo.h @@ -20,7 +20,7 @@ void demo_cleanup(void); void demo_draw(void); -void demo_keyboard(int key, int state); +void demo_keyboard(int key, int press); /* defined in main_*.c */ @@ -34,4 +34,7 @@ void set_palette(int idx, int r, int g, int b); */ 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_ */ diff --git a/src/greets.c b/src/greets.c index 57f85e8..353d29c 100644 --- a/src/greets.c +++ b/src/greets.c @@ -211,11 +211,10 @@ static void draw(void) 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); diff --git a/src/infcubes.c b/src/infcubes.c new file mode 100644 index 0000000..5fe7df9 --- /dev/null +++ b/src/infcubes.c @@ -0,0 +1,109 @@ +#include +#include +#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(); +} diff --git a/src/screen.c b/src/screen.c index fb89206..5ba6d40 100644 --- a/src/screen.c +++ b/src/screen.c @@ -14,6 +14,7 @@ struct screen *bump_screen(void); 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]; @@ -53,6 +54,9 @@ int scr_init(void) if(!(scr[idx++] = greets_screen())) { return -1; } + if(!(scr[idx++] = infcubes_screen())) { + return -1; + } num_screens = idx; assert(num_screens <= NUM_SCR); -- 1.7.10.4