citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 40385956f06778ce648630858287d9ef20809cb8
parent d6f534c69d148582e0bea8be79ae48c0d75475a6
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Sun, 20 Mar 2016 15:28:19 -0600

viewer: make struct demo_view_t a class

Diffstat:
Mviewer/demo-view.cxx | 514+++++++++++++++++++++++++++++++------------------------------------------------
Mviewer/demo-view.h | 130++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mviewer/viewer.cxx | 111++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
3 files changed, 362 insertions(+), 393 deletions(-)

diff --git a/viewer/demo-view.cxx b/viewer/demo-view.cxx @@ -17,6 +17,7 @@ */ #include "demo-view.h" +#include "af_unix.h" extern "C" { #include "trackball.h" @@ -25,153 +26,98 @@ extern "C" { #include <sys/time.h> +void start_animation(); -struct demo_view_t { - unsigned int refcount; - - demo_glstate_t *st; - - /* Output */ - GLint vsync; - glyphy_bool_t srgb; - glyphy_bool_t fullscreen; - - /* Mouse handling */ - int buttons; - int modifiers; - bool dragged; - bool click_handled; - double beginx, beginy; - double lastx, lasty, lastt; - double dx,dy, dt; - - /* Transformation */ - float quat[4]; - double scale; - glyphy_point_t translate; - double perspective; - - /* Animation */ - float rot_axis[3]; - float rot_speed; - bool animate; - int num_frames; - long fps_start_time; - long last_frame_time; - bool has_fps_timer; - - /* Window geometry just before going fullscreen */ - int x; - int y; - int width; - int height; -}; - -demo_view_t *static_vu; - -demo_view_t * -demo_view_create (demo_glstate_t *st) +demo_view_t::demo_view_t(demo_glstate_t *st, demo_buffer_t *buf) : + st(st), + buffer(buf), + fullscreen(false), + animate(false), + refcount(1) { TRACE(); - demo_view_t *vu = (demo_view_t *) calloc (1, sizeof (demo_view_t)); - vu->refcount = 1; - - vu->st = st; - demo_view_reset (vu); - - assert (!static_vu); - static_vu = vu; - - return vu; + demo_view_reset(); } +#if 0 demo_view_t * demo_view_reference (demo_view_t *vu) { if (vu) vu->refcount++; return vu; } +#endif -void -demo_view_destroy (demo_view_t *vu) +demo_view_t::~demo_view_t() { - if (!vu || --vu->refcount) - return; - - assert (static_vu == vu); - static_vu = NULL; - - free (vu); + assert (refcount == 1); } #define ANIMATION_SPEED 1. /* Default speed, in radians second. */ void -demo_view_reset (demo_view_t *vu) +demo_view_t::demo_view_reset() { - vu->perspective = 4; - vu->scale = 1; - vu->translate.x = vu->translate.y = 0; - trackball (vu->quat , 0.0, 0.0, 0.0, 0.0); - vset (vu->rot_axis, 0., 0., 1.); - vu->rot_speed = ANIMATION_SPEED / 1000.; + perspective = 4; + scale = 1; + translate.x = translate.y = 0; + trackball (quat , 0.0, 0.0, 0.0, 0.0); + vset (rot_axis, 0., 0., 1.); + rot_speed = ANIMATION_SPEED / 1000.; } - -static void -demo_view_scale_gamma_adjust (demo_view_t *vu, double factor) +void +demo_view_t::demo_view_scale_gamma_adjust(double factor) { - demo_glstate_scale_gamma_adjust (vu->st, factor); + demo_glstate_scale_gamma_adjust(st, factor); } -static void -demo_view_scale_contrast (demo_view_t *vu, double factor) +void +demo_view_t::demo_view_scale_contrast(double factor) { - demo_glstate_scale_contrast (vu->st, factor); + demo_glstate_scale_contrast (st, factor); } -static void -demo_view_scale_perspective (demo_view_t *vu, double factor) +void +demo_view_t::demo_view_scale_perspective(double factor) { - vu->perspective = clamp (vu->perspective * factor, .01, 100.); + perspective = clamp (perspective * factor, .01, 100.); } -static void -demo_view_toggle_outline (demo_view_t *vu) +void +demo_view_t::demo_view_toggle_outline() { - demo_glstate_toggle_outline (vu->st); + demo_glstate_toggle_outline(st); } -static void -demo_view_scale_outline_thickness (demo_view_t *vu, double factor) +void +demo_view_t::demo_view_scale_outline_thickness(double factor) { - demo_glstate_scale_outline_thickness (vu->st, factor); + demo_glstate_scale_outline_thickness(st, factor); } - -static void -demo_view_adjust_boldness (demo_view_t *vu, double factor) +void +demo_view_t::demo_view_adjust_boldness(double factor) { - demo_glstate_adjust_boldness (vu->st, factor); + demo_glstate_adjust_boldness(st, factor); } - -static void -demo_view_scale (demo_view_t *vu, double factor) +void +demo_view_t::demo_view_scale(double factor) { - vu->scale *= factor; + scale *= factor; } -static void -demo_view_translate (demo_view_t *vu, double dx, double dy) +void +demo_view_t::demo_view_translate(double dx, double dy) { - vu->translate.x += dx / vu->scale; - vu->translate.y += dy / vu->scale; + translate.x += dx / scale; + translate.y += dy / scale; } -static void -demo_view_apply_transform (demo_view_t *vu, float *mat) +void +demo_view_t::demo_view_apply_transform(float *mat) { int viewport[4]; glGetIntegerv (GL_VIEWPORT, viewport); @@ -179,13 +125,13 @@ demo_view_apply_transform (demo_view_t *vu, float *mat) GLint height = viewport[3]; // View transform - m4Scale (mat, vu->scale, vu->scale, 1); - m4Translate (mat, vu->translate.x, vu->translate.y, 0); + m4Scale (mat, scale, scale, 1); + m4Translate (mat, translate.x, translate.y, 0); // Perspective { double d = std::max (width, height); - double near = d / vu->perspective; + double near = d / perspective; double far = near + d; double factor = near / (2 * near + d); m4Frustum (mat, -width * factor, width * factor, -height * factor, height * factor, near, far); @@ -194,7 +140,7 @@ demo_view_apply_transform (demo_view_t *vu, float *mat) // Rotate float m[4][4]; - build_rotmatrix (m, vu->quat); + build_rotmatrix (m, quat); m4MultMatrix(mat, &m[0][0]); // Fix 'up' @@ -206,87 +152,32 @@ demo_view_apply_transform (demo_view_t *vu, float *mat) static long current_time (void) { - return glutGet (GLUT_ELAPSED_TIME); + return glutGet(GLUT_ELAPSED_TIME); } -static void -next_frame (demo_view_t *vu) -{ - glutPostRedisplay (); -} - -static void -timed_step (int ms) -{ - demo_view_t *vu = static_vu; - if (vu->animate) { - glutTimerFunc (ms, timed_step, ms); - next_frame (vu); - } -} - -static void -idle_step (void) -{ - demo_view_t *vu = static_vu; - if (vu->animate) { - next_frame (vu); - } - else - glutIdleFunc (NULL); -} - -static void -print_fps (int ms) -{ - demo_view_t *vu = static_vu; - if (vu->animate) { - glutTimerFunc (ms, print_fps, ms); - long t = current_time (); - LOGI ("%gfps\n", vu->num_frames * 1000. / (t - vu->fps_start_time)); - vu->num_frames = 0; - vu->fps_start_time = t; - } else - vu->has_fps_timer = false; -} - -static void -start_animation (demo_view_t *vu) -{ - vu->num_frames = 0; - vu->last_frame_time = vu->fps_start_time = current_time (); - //glutTimerFunc (1000/60, timed_step, 1000/60); - glutIdleFunc (idle_step); - if (!vu->has_fps_timer) { - vu->has_fps_timer = true; - glutTimerFunc (5000, print_fps, 5000); - } -} - -static void -demo_view_toggle_animation (demo_view_t *vu) +void +demo_view_t::demo_view_toggle_animation() { - vu->animate = !vu->animate; - if (vu->animate) - start_animation (vu); + animate = !animate; + if (animate) + start_animation(); } - -static void -demo_view_toggle_vsync (demo_view_t *vu) +void +demo_view_t::demo_view_toggle_vsync() { - vu->vsync = !vu->vsync; - LOGI ("Setting vsync %s.\n", vu->vsync ? "on" : "off"); + vsync = !vsync; + LOGI ("Setting vsync %s.\n", vsync ? "on" : "off"); #if defined(__APPLE__) - CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &vu->vsync); + CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &vsync); #elif defined(__WGLEW__) if (wglewIsSupported ("WGL_EXT_swap_control")) - wglSwapIntervalEXT (vu->vsync); + wglSwapIntervalEXT (vsync); else LOGW ("WGL_EXT_swal_control not supported; failed to set vsync\n"); #elif defined(__GLXEW_H__) if (glxewIsSupported ("GLX_SGI_swap_control")) - glXSwapIntervalSGI (vu->vsync); + glXSwapIntervalSGI (vsync); else LOGW ("GLX_SGI_swap_control not supported; failed to set vsync\n"); #else @@ -294,16 +185,16 @@ demo_view_toggle_vsync (demo_view_t *vu) #endif } -static void -demo_view_toggle_srgb (demo_view_t *vu) +void +demo_view_t::demo_view_toggle_srgb() { - vu->srgb = !vu->srgb; - LOGI ("Setting sRGB framebuffer %s.\n", vu->srgb ? "on" : "off"); + srgb = !srgb; + LOGI ("Setting sRGB framebuffer %s.\n", srgb ? "on" : "off"); #if defined(GL_FRAMEBUFFER_SRGB) && defined(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT) GLboolean available = false; if ((glewIsSupported ("GL_ARB_framebuffer_sRGB") || glewIsSupported ("GL_EXT_framebuffer_sRGB")) && (glGetBooleanv (GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &available), available)) { - if (vu->srgb) + if (srgb) glEnable (GL_FRAMEBUFFER_SRGB); else glDisable (GL_FRAMEBUFFER_SRGB); @@ -312,31 +203,31 @@ demo_view_toggle_srgb (demo_view_t *vu) LOGW ("No sRGB framebuffer extension found; failed to set sRGB framebuffer\n"); } -static void -demo_view_toggle_fullscreen (demo_view_t *vu) -{ - vu->fullscreen = !vu->fullscreen; - if (vu->fullscreen) { - vu->x = glutGet (GLUT_WINDOW_X); - vu->y = glutGet (GLUT_WINDOW_Y); - vu->width = glutGet (GLUT_WINDOW_WIDTH); - vu->height = glutGet (GLUT_WINDOW_HEIGHT); - glutFullScreen (); +void +demo_view_t::demo_view_toggle_fullscreen() +{ + fullscreen = !fullscreen; + if (fullscreen) { + x = glutGet(GLUT_WINDOW_X); + y = glutGet(GLUT_WINDOW_Y); + width = glutGet(GLUT_WINDOW_WIDTH); + height = glutGet(GLUT_WINDOW_HEIGHT); + glutFullScreen(); } else { - glutReshapeWindow (vu->width, vu->height); - glutPositionWindow (vu->x, vu->y); + glutReshapeWindow(width, height); + glutPositionWindow(x, y); } } -static void -demo_view_toggle_debug (demo_view_t *vu) +void +demo_view_t::demo_view_toggle_debug() { - demo_glstate_toggle_debug (vu->st); + demo_glstate_toggle_debug(st); } void -demo_view_reshape_func (demo_view_t *vu, int width, int height) +demo_view_t::demo_view_reshape_func(int width, int height) { glViewport (0, 0, width, height); glutPostRedisplay (); @@ -344,7 +235,7 @@ demo_view_reshape_func (demo_view_t *vu, int width, int height) #define STEP 1.05 void -demo_view_keyboard_func (demo_view_t *vu, unsigned char key, int x, int y) +demo_view_t::demo_view_keyboard_func(unsigned char key, int x, int y) { switch (key) { @@ -354,76 +245,76 @@ demo_view_keyboard_func (demo_view_t *vu, unsigned char key, int x, int y) break; case ' ': - demo_view_toggle_animation (vu); + demo_view_toggle_animation(); break; case 'v': - demo_view_toggle_vsync (vu); + demo_view_toggle_vsync(); break; case 'f': - demo_view_toggle_fullscreen (vu); + demo_view_toggle_fullscreen(); break; case 'd': - demo_view_toggle_debug (vu); + demo_view_toggle_debug(); break; case 'o': - demo_view_toggle_outline (vu); + demo_view_toggle_outline(); break; case 'p': - demo_view_scale_outline_thickness (vu, STEP); + demo_view_scale_outline_thickness(STEP); break; case 'i': - demo_view_scale_outline_thickness (vu, 1. / STEP); + demo_view_scale_outline_thickness(1. / STEP); break; case '0': - demo_view_adjust_boldness (vu, +.01); + demo_view_adjust_boldness(+.01); break; case '9': - demo_view_adjust_boldness (vu, -.01); + demo_view_adjust_boldness(-.01); break; case 'a': - demo_view_scale_contrast (vu, STEP); + demo_view_scale_contrast(STEP); break; case 'z': - demo_view_scale_contrast (vu, 1. / STEP); + demo_view_scale_contrast(1. / STEP); break; case 'g': - demo_view_scale_gamma_adjust (vu, STEP); + demo_view_scale_gamma_adjust(STEP); break; case 'b': - demo_view_scale_gamma_adjust (vu, 1. / STEP); + demo_view_scale_gamma_adjust(1. / STEP); break; case 'c': - demo_view_toggle_srgb (vu); + demo_view_toggle_srgb(); break; case '=': - demo_view_scale (vu, STEP); + demo_view_scale(STEP); break; case '-': - demo_view_scale (vu, 1. / STEP); + demo_view_scale(1. / STEP); break; case 'k': - demo_view_translate (vu, 0, -.1); + demo_view_translate(0, -.1); break; case 'j': - demo_view_translate (vu, 0, +.1); + demo_view_translate(0, +.1); break; case 'h': - demo_view_translate (vu, +.1, 0); + demo_view_translate(+.1, 0); break; case 'l': - demo_view_translate (vu, -.1, 0); + demo_view_translate(-.1, 0); break; case 'r': - demo_view_reset (vu); + demo_view_reset(); break; default: @@ -433,21 +324,21 @@ demo_view_keyboard_func (demo_view_t *vu, unsigned char key, int x, int y) } void -demo_view_special_func (demo_view_t *vu, int key, int x, int y) +demo_view_t::demo_view_special_func(int key, int x, int y) { switch (key) { case GLUT_KEY_UP: - demo_view_translate (vu, 0, -.1); + demo_view_translate(0, -.1); break; case GLUT_KEY_DOWN: - demo_view_translate (vu, 0, +.1); + demo_view_translate(0, +.1); break; case GLUT_KEY_LEFT: - demo_view_translate (vu, +.1, 0); + demo_view_translate(+.1, 0); break; case GLUT_KEY_RIGHT: - demo_view_translate (vu, -.1, 0); + demo_view_translate(-.1, 0); break; default: @@ -457,156 +348,153 @@ demo_view_special_func (demo_view_t *vu, int key, int x, int y) } void -demo_view_mouse_func (demo_view_t *vu, int button, int state, int x, int y) +demo_view_t::demo_view_mouse_func(int button, int state, int x, int y) { if (state == GLUT_DOWN) { - vu->buttons |= (1 << button); - vu->click_handled = false; + buttons |= (1 << button); + click_handled = false; } else - vu->buttons &= !(1 << button); - vu->modifiers = glutGetModifiers (); - - switch (button) - { - case GLUT_RIGHT_BUTTON: - switch (state) { - case GLUT_DOWN: - if (vu->animate) { - demo_view_toggle_animation (vu); - vu->click_handled = true; - } - break; - case GLUT_UP: - if (!vu->animate) - { - if (!vu->dragged && !vu->click_handled) - demo_view_toggle_animation (vu); - else if (vu->dt) { - double speed = hypot (vu->dx, vu->dy) / vu->dt; - if (speed > 0.1) - demo_view_toggle_animation (vu); - } - vu->dx = vu->dy = vu->dt = 0; - } - break; + buttons &= !(1 << button); + modifiers = glutGetModifiers (); + + switch (button) { + case GLUT_RIGHT_BUTTON: + switch (state) { + case GLUT_DOWN: + if (animate) { + demo_view_toggle_animation(); + click_handled = true; + } + break; + case GLUT_UP: + if (!animate) + { + if (!dragged && !click_handled) + demo_view_toggle_animation(); + else if (dt) { + double speed = hypot (dx, dy) / dt; + if (speed > 0.1) + demo_view_toggle_animation(); + } + dx = dy = dt = 0; } break; + } + break; #if !defined(GLUT_WHEEL_UP) #define GLUT_WHEEL_UP 3 #define GLUT_WHEEL_DOWN 4 #endif + case GLUT_WHEEL_UP: + demo_view_scale(STEP); + break; - case GLUT_WHEEL_UP: - demo_view_scale (vu, STEP); - break; - - case GLUT_WHEEL_DOWN: - demo_view_scale (vu, 1. / STEP); - break; + case GLUT_WHEEL_DOWN: + demo_view_scale(1. / STEP); + break; } - vu->beginx = vu->lastx = x; - vu->beginy = vu->lasty = y; - vu->dragged = false; + beginx = lastx = x; + beginy = lasty = y; + dragged = false; glutPostRedisplay (); } void -demo_view_motion_func (demo_view_t *vu, int x, int y) +demo_view_t::demo_view_motion_func(int x, int y) { - vu->dragged = true; + dragged = true; int viewport[4]; glGetIntegerv (GL_VIEWPORT, viewport); GLuint width = viewport[2]; GLuint height = viewport[3]; - if (vu->buttons & (1 << GLUT_LEFT_BUTTON)) + if (buttons & (1 << GLUT_LEFT_BUTTON)) { - if (vu->modifiers & GLUT_ACTIVE_SHIFT) { + if (modifiers & GLUT_ACTIVE_SHIFT) { /* adjust contrast/gamma */ - demo_view_scale_gamma_adjust (vu, 1 - ((y - vu->lasty) / height)); - demo_view_scale_contrast (vu, 1 + ((x - vu->lastx) / width)); + demo_view_scale_gamma_adjust(1 - ((y - lasty) / height)); + demo_view_scale_contrast(1 + ((x - lastx) / width)); } else { /* translate */ - demo_view_translate (vu, - +2 * (x - vu->lastx) / width, - -2 * (y - vu->lasty) / height); + demo_view_translate( + +2 * (x - lastx) / width, + -2 * (y - lasty) / height); } } - if (vu->buttons & (1 << GLUT_RIGHT_BUTTON)) + if (buttons & (1 << GLUT_RIGHT_BUTTON)) { - if (vu->modifiers & GLUT_ACTIVE_SHIFT) { + if (modifiers & GLUT_ACTIVE_SHIFT) { /* adjust perspective */ - demo_view_scale_perspective (vu, 1 - ((y - vu->lasty) / height) * 5); + demo_view_scale_perspective(1 - ((y - lasty) / height) * 5); } else { /* rotate */ float dquat[4]; trackball (dquat, - (2.0*vu->lastx - width) / width, - ( height - 2.0*vu->lasty) / height, - ( 2.0*x - width) / width, - ( height - 2.0*y) / height ); + (2.0*lastx - width) / width, + ( height - 2.0*lasty) / height, + ( 2.0*x - width) / width, + ( height - 2.0*y) / height ); - vu->dx = x - vu->lastx; - vu->dy = y - vu->lasty; - vu->dt = current_time () - vu->lastt; + dx = x - lastx; + dy = y - lasty; + dt = current_time () - lastt; - add_quats (dquat, vu->quat, vu->quat); + add_quats (dquat, quat, quat); - if (vu->dt) { - vcopy (dquat, vu->rot_axis); - vnormal (vu->rot_axis); - vu->rot_speed = 2 * acos (dquat[3]) / vu->dt; + if (dt) { + vcopy (dquat, rot_axis); + vnormal (rot_axis); + rot_speed = 2 * acos (dquat[3]) / dt; } } } - if (vu->buttons & (1 << GLUT_MIDDLE_BUTTON)) + if (buttons & (1 << GLUT_MIDDLE_BUTTON)) { /* scale */ - double factor = 1 - ((y - vu->lasty) / height) * 5; - demo_view_scale (vu, factor); + double factor = 1 - ((y - lasty) / height) * 5; + demo_view_scale(factor); /* adjust translate so we scale centered at the drag-begin mouse position */ - demo_view_translate (vu, - +(2. * vu->beginx / width - 1) * (1 - factor), - -(2. * vu->beginy / height - 1) * (1 - factor)); + demo_view_translate( + +(2. * beginx / width - 1) * (1 - factor), + -(2. * beginy / height - 1) * (1 - factor)); } - vu->lastx = x; - vu->lasty = y; - vu->lastt = current_time (); + lastx = x; + lasty = y; + lastt = current_time (); glutPostRedisplay (); } void -demo_view_print_help (demo_view_t *vu) +demo_view_t::demo_view_print_help() { LOGI ("Welcome to GLyphy demo\n"); } - -static void -advance_frame (demo_view_t *vu, long dtime) +void +demo_view_t::advance_frame(long dtime) { - if (vu->animate) { + if (animate) { float dquat[4]; - axis_to_quat (vu->rot_axis, vu->rot_speed * dtime, dquat); - add_quats (dquat, vu->quat, vu->quat); - vu->num_frames++; + axis_to_quat (rot_axis, rot_speed * dtime, dquat); + add_quats (dquat, quat, quat); + num_frames++; } } void -demo_view_display (demo_view_t *vu, demo_buffer_t *buffer) +demo_view_t::demo_view_display() { long new_time = current_time (); - advance_frame (vu, new_time - vu->last_frame_time); - vu->last_frame_time = new_time; + advance_frame(new_time - last_frame_time); + last_frame_time = new_time; int viewport[4]; glGetIntegerv (GL_VIEWPORT, viewport); @@ -618,7 +506,7 @@ demo_view_display (demo_view_t *vu, demo_buffer_t *buffer) m4LoadIdentity (mat); - demo_view_apply_transform (vu, mat); + demo_view_apply_transform(mat); // Buffer best-fit glyphy_extents_t extents; @@ -631,7 +519,7 @@ demo_view_display (demo_view_t *vu, demo_buffer_t *buffer) -(extents.max_x + extents.min_x) / 2., -(extents.max_y + extents.min_y) / 2., 0); - demo_glstate_set_matrix (vu->st, mat); + demo_glstate_set_matrix(st, mat); glClearColor (1, 1, 1, 1); glClear (GL_COLOR_BUFFER_BIT); @@ -641,12 +529,12 @@ demo_view_display (demo_view_t *vu, demo_buffer_t *buffer) glutSwapBuffers (); } - void -demo_view_setup (demo_view_t *vu) +void +demo_view_t::demo_view_setup() { - if (!vu->vsync) - demo_view_toggle_vsync (vu); - if (!vu->srgb) - demo_view_toggle_srgb (vu); - demo_glstate_setup (vu->st); + if (!vsync) + demo_view_toggle_vsync(); + if (!srgb) + demo_view_toggle_srgb(); + demo_glstate_setup(st); } diff --git a/viewer/demo-view.h b/viewer/demo-view.h @@ -1,21 +1,3 @@ -/* - * Copyright 2012 Google, Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Google Author(s): Behdad Esfahbod - */ - #ifndef DEMO_VIEW_H #define DEMO_VIEW_H @@ -23,44 +5,78 @@ #include "demo-buffer.h" #include "demo-glstate.h" -typedef struct demo_view_t demo_view_t; - -demo_view_t * -demo_view_create (demo_glstate_t *st); - -demo_view_t * -demo_view_reference (demo_view_t *vu); - -void -demo_view_destroy (demo_view_t *vu); - - -void -demo_view_reset (demo_view_t *vu); - -void -demo_view_reshape_func (demo_view_t *vu, int width, int height); - -void -demo_view_keyboard_func (demo_view_t *vu, unsigned char key, int x, int y); - -void -demo_view_special_func (demo_view_t *view, int key, int x, int y); - -void -demo_view_mouse_func (demo_view_t *vu, int button, int state, int x, int y); - -void -demo_view_motion_func (demo_view_t *vu, int x, int y); - -void -demo_view_print_help (demo_view_t *vu); - -void -demo_view_display (demo_view_t *vu, demo_buffer_t *buffer); - -void -demo_view_setup (demo_view_t *vu); - +class demo_view_t { +public: + demo_view_t(demo_glstate_t *, demo_buffer_t *); + ~demo_view_t(); + + void demo_view_reset(); + void demo_view_reshape_func(int, int); + void demo_view_keyboard_func(unsigned char key, int x, int y); + void demo_view_special_func(int key, int x, int y); + void demo_view_mouse_func(int button, int state, int x, int y); + void demo_view_motion_func(int x, int y); + void demo_view_print_help(); + void demo_view_display(); + void demo_view_setup(); + + /* Animation */ + bool animate; + int num_frames; + long fps_start_time; + bool has_fps_timer; + long last_frame_time; +private: + void demo_view_scale_gamma_adjust(double); + void demo_view_scale_contrast(double); + void demo_view_scale_perspective(double); + void demo_view_toggle_outline(); + void demo_view_scale_outline_thickness(double); + void demo_view_adjust_boldness(double); + void demo_view_scale(double); + void demo_view_translate(double, double); + void demo_view_apply_transform(float *); + void demo_view_toggle_animation(); + void demo_view_toggle_vsync(); + void demo_view_toggle_srgb(); + void demo_view_toggle_fullscreen(); + void demo_view_toggle_debug(); + void advance_frame(long); + + unsigned int refcount; + + demo_glstate_t *st; + demo_buffer_t *buffer; + + /* Output */ + GLint vsync; + glyphy_bool_t srgb; + glyphy_bool_t fullscreen; + + /* Mouse handling */ + int buttons; + int modifiers; + bool dragged; + bool click_handled; + double beginx, beginy; + double lastx, lasty, lastt; + double dx,dy, dt; + + /* Transformation */ + float quat[4]; + double scale; + glyphy_point_t translate; + double perspective; + + /* Animation */ + float rot_axis[3]; + float rot_speed; + + /* Window geometry just before going fullscreen */ + int x; + int y; + int width; + int height; +}; #endif /* DEMO_VIEW_H */ diff --git a/viewer/viewer.cxx b/viewer/viewer.cxx @@ -11,32 +11,36 @@ #include "demo-font.h" #include "demo-view.h" -demo_glstate_t *st; -demo_view_t *vu; -static demo_buffer_t *buffer; -static demo_font_t *font; class window { public: window(int argc, char *argv[]); void start(); - void add(drawable &); -private: - static std::vector<drawable*> drawables; + static void idle_step(); + static void print_fps(int); + static void timed_step(int); + static void next_frame(demo_view_t *); + + static demo_view_t *static_vu; static af_unix socket; + static std::vector<drawable*> drawables; +private: static void display(); static void reshape_func(int, int); static void keyboard_func(unsigned char, int, int); static void special_func(int, int, int); static void mouse_func(int, int, int, int); static void motion_func(int, int); - static void idle(); + demo_glstate_t *st; + demo_buffer_t *buffer; + demo_font_t *font; }; std::vector<drawable*> window::drawables; af_unix window::socket; +demo_view_t *window::static_vu; window::window(int argc, char *argv[]) { @@ -58,8 +62,10 @@ window::window(int argc, char *argv[]) errx(1, "No support for OpenGL 2.0 found"); st = demo_glstate_create(); - vu = demo_view_create(st); - demo_view_print_help(vu); + buffer = demo_buffer_create(); + //vu = demo_view_create(st); + static_vu = new demo_view_t(st, buffer); + static_vu->demo_view_print_help(); FT_Library ft_library; FT_Init_FreeType(&ft_library); @@ -69,15 +75,13 @@ window::window(int argc, char *argv[]) font = demo_font_create(ft_face, demo_glstate_get_atlas(st)); - buffer = demo_buffer_create(); - glyphy_point_t top_left = { 0, 0 }; demo_buffer_move_to(buffer, &top_left); demo_buffer_add_text(buffer, default_text, font, 1); demo_font_print_stats(font); - demo_view_setup(vu); + static_vu->demo_view_setup(); // This creates the socket with SOCK_NONBLOCK socket.set_listen(); @@ -86,31 +90,31 @@ window::window(int argc, char *argv[]) void window::reshape_func(int width, int height) { - demo_view_reshape_func(vu, width, height); + static_vu->demo_view_reshape_func(width, height); } void window::keyboard_func(unsigned char key, int x, int y) { - demo_view_keyboard_func(vu, key, x, y); + static_vu->demo_view_keyboard_func(key, x, y); } void window::special_func(int key, int x, int y) { - demo_view_special_func(vu, key, x, y); + static_vu->demo_view_special_func(key, x, y); } void window::mouse_func(int button, int state, int x, int y) { - demo_view_mouse_func(vu, button, state, x, y); + static_vu->demo_view_mouse_func(button, state, x, y); } void window::motion_func(int x, int y) { - demo_view_motion_func(vu, x, y); + static_vu->demo_view_motion_func(x, y); } void @@ -122,18 +126,79 @@ window::start() void window::display(void) { - demo_view_display(vu, buffer); + static_vu->demo_view_display(); +} + + +/* return current time in milli-seconds */ +static long +current_time (void) +{ + return glutGet (GLUT_ELAPSED_TIME); } void -window::idle(void) +window::next_frame(demo_view_t *vu) { - af_unix *temp_socket = socket.accept(); + /* + af_unix *temp_socket = window::socket.accept(); if (temp_socket) - drawables.push_back(new RuntimeClient(temp_socket, buffer, font)); + window::drawables.push_back(new RuntimeClient(temp_socket, buffer, font)); - for (auto &i : drawables) + for (auto &i : window::drawables) i->idle(); + */ + + glutPostRedisplay (); +} + +void +window::timed_step(int ms) +{ + demo_view_t *vu = static_vu; + if (vu->animate) { + glutTimerFunc (ms, timed_step, ms); + next_frame (vu); + } +} + +void +window::idle_step(void) +{ + demo_view_t *vu = static_vu; + if (vu->animate) { + next_frame (vu); + } + else + glutIdleFunc(NULL); +} + +void +window::print_fps(int ms) +{ + demo_view_t *vu = static_vu; + if (vu->animate) { + glutTimerFunc (ms, print_fps, ms); + long t = current_time (); + LOGI ("%gfps\n", vu->num_frames * 1000. / (t - vu->fps_start_time)); + vu->num_frames = 0; + vu->fps_start_time = t; + } else + vu->has_fps_timer = false; +} + +void +start_animation() +{ + demo_view_t *vu = window::static_vu; + vu->num_frames = 0; + vu->last_frame_time = vu->fps_start_time = current_time(); + //glutTimerFunc (1000/60, timed_step, 1000/60); + glutIdleFunc(window::idle_step); + if (!vu->has_fps_timer) { + vu->has_fps_timer = true; + glutTimerFunc (5000, window::print_fps, 5000); + } } int