diff --git a/main.cpp b/main.cpp index a481fc1..69fe5c0 100644 --- a/main.cpp +++ b/main.cpp @@ -18,15 +18,17 @@ #include #include #include +#include "RectSaver.hpp" const int WIDTH {700}; const int HEIGHT {700}; const double FPS {50.0}; -const int nanosecs {static_cast(round(1000000000.0 / FPS))}; +const int nanosecs {}; sc::gui::Window* main_window {nullptr}; sc::gui::Image* main_image {nullptr}; -cairo_t* cr {nullptr}; +// cairo_t* cr {nullptr}; +RectSaver* main_saver {nullptr}; bool expired {true}; bool should_run {true}; @@ -46,58 +48,17 @@ void update_frame() { struct timespec delay; delay.tv_sec = 0; - delay.tv_nsec = nanosecs; + delay.tv_nsec = static_cast(round(1000000000.0 / main_saver->fps())); nanosleep(&delay, nullptr); } } void draw() { - static int x = 300; - static int y = 300; - static int dx = 1; - static int dy = 1; - static double r = 0; - static double dr = 0.01; - if (expired) { expired = false; sc::gui::ImageLock lock {*main_image}; - - // black background - cairo_set_source_rgb(cr, 0, 0, 0); - cairo_rectangle(cr, 0, 0, WIDTH, HEIGHT); - cairo_fill(cr); - - // orange rectangle - cairo_rectangle(cr, x, y, 100, 100); - cairo_set_source_rgb(cr, 1, 0.5, 0); - cairo_fill(cr); - - // black rectangle - cairo_rectangle(cr, 200, 200, 100, 100); - cairo_set_source_rgb(cr, r, 0, 0); - cairo_fill(cr); - - // dark green text - cairo_set_source_rgb(cr, 0.2, 0.5, 0); - cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); - cairo_set_font_size(cr, 42); - cairo_move_to(cr, 50, 100); - cairo_show_text(cr, "abcdefghijklmnopqrstuvwxyz"); - - // show + main_saver->draw_frame(); main_window->show_image(*main_image); - - // update - x += dx; - y += dy; - if (x > 400) dx = -1; - if (y > 400) dy = -1; - if (x < 300) dx = 1; - if (y < 300) dy = 1; - r += dr; - if (r > 1) dr = -0.01; - if (r < 0) dr = 0.01; } } @@ -156,7 +117,10 @@ int main(int argc, const char * argv[]) { main_image = ℑ cairo_surface_t* cs {cairo_image_surface_create_for_data(static_cast(s->pixels), CAIRO_FORMAT_RGB24, s->w, s->h, s->pitch)}; - cr = cairo_create(cs); + cairo_t* cr {cairo_create(cs)}; + + RectSaver saver {cr, {0, 0, WIDTH, HEIGHT}}; + main_saver = &saver; // setup thread which periodically sets 'expired' std::thread frame_timer {update_frame};