erebus

changeset 9:d38e13d6063c

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 May 2014 17:22:53 +0300
parents e2d9bf168a41
children 506e114b7ca2
files liberebus/src/erebus.cc liberebus/src/erebus.h src/main.cc
diffstat 3 files changed, 78 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/liberebus/src/erebus.cc	Sat May 24 06:12:57 2014 +0300
     1.2 +++ b/liberebus/src/erebus.cc	Sat May 24 17:22:53 2014 +0300
     1.3 @@ -34,6 +34,11 @@
     1.4  	int cur_pixel_x, cur_pixel_y;
     1.5  	Rect cur_rect;
     1.6  	int cur_sample;
     1.7 +
     1.8 +	// interactive input
     1.9 +	std::vector<bool> keystate;
    1.10 +	std::vector<bool> bnstate;
    1.11 +	int mouse_pos[2];
    1.12  };
    1.13  
    1.14  static void render_pixel(struct erebus *ctx, int x, int y, int sample);
    1.15 @@ -205,6 +210,30 @@
    1.16  	return 0;
    1.17  }
    1.18  
    1.19 +bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed)
    1.20 +{
    1.21 +	if(!ctx) return false;
    1.22 +	if(ctx->keystate.size() <= key) ctx->keystate.resize(key < 256 ? 256 : key + 1);
    1.23 +
    1.24 +	ctx->keystate[key] = pressed;
    1.25 +	return false;
    1.26 +}
    1.27 +
    1.28 +bool erb_input_mouse_button(struct erebus *ctx, int bn, bool pressed, int x, int y)
    1.29 +{
    1.30 +	if(!ctx) return false;
    1.31 +	if(ctx->bnstate.size() <= bn) ctx->bnstate.resize(bn < 32 ? 32 : bn + 1);
    1.32 +
    1.33 +	ctx->bnstate[bn] = pressed;
    1.34 +	ctx->mouse_pos[0] = x;
    1.35 +	ctx->mouse_pos[1] = y;
    1.36 +}
    1.37 +
    1.38 +bool erb_input_mouse_motion(struct erebus *ctx, int x, int y);
    1.39 +bool erb_input_6dof_button(struct erebus *ctx, int bn, bool pressed);
    1.40 +bool erb_input_6dof_motion(struct erebus *ctx, float x, float y, float z);
    1.41 +
    1.42 +
    1.43  }	// extern "C"
    1.44  
    1.45  float randf(float low, float high)
     2.1 --- a/liberebus/src/erebus.h	Sat May 24 06:12:57 2014 +0300
     2.2 +++ b/liberebus/src/erebus.h	Sat May 24 17:22:53 2014 +0300
     2.3 @@ -39,6 +39,12 @@
     2.4  
     2.5  int erb_load_scene(struct erebus *ctx, const char *fname);
     2.6  
     2.7 +bool erb_input_keyboard(struct erebus *ctx, int key, bool pressed);
     2.8 +bool erb_input_mouse_button(struct erebus *ctx, int bn, bool pressed, int x, int y);
     2.9 +bool erb_input_mouse_motion(struct erebus *ctx, int x, int y);
    2.10 +bool erb_input_6dof_button(struct erebus *ctx, int bn, bool pressed);
    2.11 +bool erb_input_6dof_motion(struct erebus *ctx, float x, float y, float z);
    2.12 +
    2.13  #ifdef __cplusplus
    2.14  }
    2.15  #endif
     3.1 --- a/src/main.cc	Sat May 24 06:12:57 2014 +0300
     3.2 +++ b/src/main.cc	Sat May 24 17:22:53 2014 +0300
     3.3 @@ -12,7 +12,11 @@
     3.4  static void display();
     3.5  static void reshape(int x, int y);
     3.6  static void keyb(unsigned char key, int x, int y);
     3.7 +static void keyb_up(unsigned char key, int x, int y);
     3.8  static void mouse(int bn, int st, int x, int y);
     3.9 +static void motion(int x, int y);
    3.10 +static void sball_button(int bn, int st);
    3.11 +static void sball_motion(int x, int y, int z);
    3.12  static int next_pow2(int x);
    3.13  
    3.14  static int width, height, rtex_width, rtex_height;
    3.15 @@ -32,7 +36,11 @@
    3.16  	glutDisplayFunc(display);
    3.17  	glutReshapeFunc(reshape);
    3.18  	glutKeyboardFunc(keyb);
    3.19 +	glutKeyboardUpFunc(keyb_up);
    3.20  	glutMouseFunc(mouse);
    3.21 +	glutMotionFunc(motion);
    3.22 +	glutSpaceballButtonFunc(sball_button);
    3.23 +	glutSpaceballMotionFunc(sball_motion);
    3.24  
    3.25  	if(!init()) {
    3.26  		return 1;
    3.27 @@ -169,10 +177,45 @@
    3.28  		erb_begin_frame(erb, 0);
    3.29  		break;
    3.30  	}
    3.31 +
    3.32 +	if(erb_input_keyboard(key, true)) {
    3.33 +		glutPostRedisplay();
    3.34 +	}
    3.35 +}
    3.36 +
    3.37 +static void keyb_up(unsigned char key, int x, int y)
    3.38 +{
    3.39 +	if(erb_input_keyboard(key, false)) {
    3.40 +		glutPostRedisplay();
    3.41 +	}
    3.42  }
    3.43  
    3.44  static void mouse(int bn, int st, int x, int y)
    3.45  {
    3.46 +	if(erb_input_mouse_button(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y)) {
    3.47 +		glutPostRedisplay();
    3.48 +	}
    3.49 +}
    3.50 +
    3.51 +static void motion(int x, int y)
    3.52 +{
    3.53 +	if(erb_input_mouse_move(x, y)) {
    3.54 +		glutPostRedisplay();
    3.55 +	}
    3.56 +}
    3.57 +
    3.58 +static void sball_button(int bn, int state)
    3.59 +{
    3.60 +	if(erb_input_6dof_button(bn, state == GLUT_DOWN)) {
    3.61 +		glutPostRedisplay();
    3.62 +	}
    3.63 +}
    3.64 +
    3.65 +static void sball_motion(int x, int y, int z)
    3.66 +{
    3.67 +	if(erb_input_6dof_motion(x / 65536.0, y / 65536.0, z / 65536.0)) {
    3.68 +		glutPostRedisplay();
    3.69 +	}
    3.70  }
    3.71  
    3.72  static int next_pow2(int x)