From a12da132d3d8b42d5474a3102d485026a4c62e1c Mon Sep 17 00:00:00 2001 From: James Whiteman Date: Tue, 1 Feb 2022 14:32:50 -0800 Subject: [PATCH] Redraw screen at 120fps --- compile/draw/opengl_service.cpp | 69 +++++++++--- compile/draw/opengl_service.h | 3 +- compile/target/linux/draw/window_service.cpp | 109 +++++++++++++------ 3 files changed, 131 insertions(+), 50 deletions(-) diff --git a/compile/draw/opengl_service.cpp b/compile/draw/opengl_service.cpp index bafd31e..cbbc6ad 100644 --- a/compile/draw/opengl_service.cpp +++ b/compile/draw/opengl_service.cpp @@ -8,25 +8,65 @@ opengl_service_t::opengl_service_t( configuration_m = configuration_p; } -void_t opengl_service_t::draw_quad( +void_t opengl_service_t::draw( + int cycle_p ) { glClearColor(1.0, 1.0, 1.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glMatrixMode(GL_PROJECTION); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); - glOrtho(-1., 1., -1., 1., 1., 20.); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.); + double rotate_x = static_cast(cycle_p * 1); + double rotate_y = static_cast(cycle_p * 1); + glRotatef( rotate_x, 1.0, 0.0, 0.0 ); + glRotatef( rotate_y, 0.0, 1.0, 0.0 ); - glBegin(GL_QUADS); - glColor3f(1., 0., 0.); glVertex3f(-.75, -.75, 0.); - glColor3f(0., 1., 0.); glVertex3f( .75, -.75, 0.); - glColor3f(0., 0., 1.); glVertex3f( .75, .75, 0.); - glColor3f(1., 1., 0.); glVertex3f(-.75, .75, 0.); + glBegin(GL_POLYGON); + + glColor3f( 1.0, 1.0, 1.0 ); + glVertex3f( 0.5, -0.5, 0.5 ); + glVertex3f( 0.5, 0.5, 0.5 ); + glVertex3f( -0.5, 0.5, 0.5 ); + glVertex3f( -0.5, -0.5, 0.5 ); glEnd(); + + // Purple side - RIGHT + glBegin(GL_POLYGON); + glColor3f( 1.0, 0.0, 1.0 ); + glVertex3f( 0.5, -0.5, -0.5 ); + glVertex3f( 0.5, 0.5, -0.5 ); + glVertex3f( 0.5, 0.5, 0.5 ); + glVertex3f( 0.5, -0.5, 0.5 ); + glEnd(); + + // Green side - LEFT + glBegin(GL_POLYGON); + glColor3f( 0.0, 1.0, 0.0 ); + glVertex3f( -0.5, -0.5, 0.5 ); + glVertex3f( -0.5, 0.5, 0.5 ); + glVertex3f( -0.5, 0.5, -0.5 ); + glVertex3f( -0.5, -0.5, -0.5 ); + glEnd(); + + // Blue side - TOP + glBegin(GL_POLYGON); + glColor3f( 0.0, 0.0, 1.0 ); + glVertex3f( 0.5, 0.5, 0.5 ); + glVertex3f( 0.5, 0.5, -0.5 ); + glVertex3f( -0.5, 0.5, -0.5 ); + glVertex3f( -0.5, 0.5, 0.5 ); + glEnd(); + + // Red side - BOTTOM + glBegin(GL_POLYGON); + glColor3f( 1.0, 0.0, 0.0 ); + glVertex3f( 0.5, -0.5, -0.5 ); + glVertex3f( 0.5, -0.5, 0.5 ); + glVertex3f( -0.5, -0.5, 0.5 ); + glVertex3f( -0.5, -0.5, -0.5 ); + + glEnd(); + glFlush(); + void_t void_l; void_l.error_m = opengl_service_t::error_t::none; return void_l; @@ -35,13 +75,14 @@ void_t opengl_service_t::draw_quad( void_t opengl_service_t::enable_depth_test( ) { glEnable( - GL_DEPTH_TEST + GL_DEPTH_TEST ); void_t void_l; void_l.error_m = opengl_service_t::error_t::none; return void_l; } + void_t opengl_service_t::resize_viewport( int x, int y, diff --git a/compile/draw/opengl_service.h b/compile/draw/opengl_service.h index ebaeede..81ffbaf 100644 --- a/compile/draw/opengl_service.h +++ b/compile/draw/opengl_service.h @@ -20,7 +20,8 @@ class opengl_service_t { }; struct configuration_t { }; - void_t draw_quad( + void_t draw( + int cycle_p ); void_t enable_depth_test( ); diff --git a/compile/target/linux/draw/window_service.cpp b/compile/target/linux/draw/window_service.cpp index 1eea0d9..95fedb6 100644 --- a/compile/target/linux/draw/window_service.cpp +++ b/compile/target/linux/draw/window_service.cpp @@ -4,6 +4,7 @@ #include "target/linux/draw/opengl_x11_service.h" #include #include +#include #include #include #include @@ -67,6 +68,11 @@ void_t window_service_t::create_window( CWColormap | CWEventMask, &set_window_attributes_l ); + XSelectInput( + display_l, + window_l, + ExposureMask | KeyPressMask + ); XMapWindow( display_l, window_l @@ -93,44 +99,77 @@ void_t window_service_t::create_window( opengl_service_l->enable_depth_test( ); - XEvent event_l; + int display_fd_l = ConnectionNumber(display_l); + fd_set in_fds; + + int cycle_l = 0; XWindowAttributes window_attributes_l; - while(true) { - XNextEvent( - display_l, - &event_l + XEvent event_l; + while (true) { + FD_ZERO(&in_fds); + FD_SET(display_fd_l, &in_fds); + + // Set our timer. + timeval timeval_l; + timeval_l.tv_usec = 8333; + timeval_l.tv_sec = 0; + + // Wait for X Event or a Timer + int select_l = select( + display_fd_l + 1, + &in_fds, + 0, + 0, + &timeval_l ); - if (event_l.type == Expose) { - XGetWindowAttributes( - display_l, - window_l, - &window_attributes_l - ); - opengl_service_l->resize_viewport( - 0, - 0, - window_attributes_l.width, - window_attributes_l.height - ); - opengl_service_l->draw_quad( - ); - opengl_x11_service_l->swap_buffers( - display_l, - window_l - ); - } else if (event_l.type == KeyPress) { - opengl_x11_service_l->dispose( - display_l - ); - XDestroyWindow( - display_l, - window_l - ); - XCloseDisplay( - display_l - ); - break; + if (select_l >= 0) { + // event received + switch (event_l.type) { + case KeyPress: + opengl_x11_service_l->dispose( + display_l + ); + XDestroyWindow( + display_l, + window_l + ); + XCloseDisplay( + display_l + ); + break; + case Expose: + XGetWindowAttributes( + display_l, + window_l, + &window_attributes_l + ); + opengl_service_l->resize_viewport( + 0, + 0, + window_attributes_l.width, + window_attributes_l.height + ); + opengl_service_l->draw( + cycle_l + ); + opengl_x11_service_l->swap_buffers( + display_l, + window_l + ); + break; + default: + break; + } + } else { + // timeout } + while (XPending(display_l)) { + XNextEvent( + display_l, + &event_l + ); + } + cycle_l += 1; } void_l.error_m = window_service_t::error_t::none; return void_l;