From 14138f15e9e90a7855812caadaa8311867d961c8 Mon Sep 17 00:00:00 2001 From: chenhuan Date: Tue, 10 Dec 2019 22:39:12 +0100 Subject: [PATCH] refresh frequency set --- src/GUI.cpp | 21 ++++++++++++--------- src/GUI.hpp | 22 ++++++++++++++-------- src/GUI_Window.cpp | 2 +- src/Image.cpp | 12 +++++++++--- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/GUI.cpp b/src/GUI.cpp index b104a23..c8260c4 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -12,19 +12,22 @@ auto GUI_run(int argc, char **argv) -> int { window_width = vkvm::getWidth(); char *resolusion = get_resolusion(window_height, window_width); std::cout << resolusion << std::endl; - GUI_Window *window = new GUI_Window(window_width, window_height + 30, "example"); + auto *window = new GUI_Window(window_width, window_height + 30, "example"); Statusbar *status[5]; //Dummy-Values TBD window->begin(); - Image *image = new Image(0, 30, window_width, window_height); + auto *image = new Image(0, 30, window_width, window_height); status[0] = new Statusbar(0, 0, 300, 30, resolusion); status[1] = new Statusbar(300, 0, 170, 30, "Event:"); status[2] = new Statusbar(470, 0, 200, 30, "Mouse Position:"); - vkvm::registerEvent(vkvm::EventType::Redraw, [image](){ + vkvm::registerEvent(vkvm::EventType::Redraw, [image]() { redraw(image); }); - Fl::repeat_timeout(0.5, get_new_image, image); + vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [image]() { + image->setDelay(vkvm::getTimerInterruptInterval()); + }); + Fl::repeat_timeout(image->getDelay(), get_new_image, image); window->end(); window->show(argc, argv); return Fl::run(); @@ -42,20 +45,20 @@ char *get_resolusion(int window_height, int window_width) { return resolusion; } -void redraw(Image* image){ +void redraw(Image *image) { image->getPixels(); - sleep(0.05); image->redraw(); } void get_new_image(void *pointer) { - ((Image *) pointer)->getPixels(); - Fl::repeat_timeout(0.5, refresh_image, pointer); + Image *image = ((Image *) pointer); + image->getPixels(); + Fl::repeat_timeout(image->getDelay(), refresh_image, pointer); } void refresh_image(void *pointer) { ((Image *) pointer)->redraw(); - Fl::repeat_timeout(0.5, get_new_image, pointer); + get_new_image(pointer); } diff --git a/src/GUI.hpp b/src/GUI.hpp index 47dfe9e..bd8c34d 100644 --- a/src/GUI.hpp +++ b/src/GUI.hpp @@ -1,7 +1,7 @@ #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -20,12 +20,12 @@ * @param button The button that was pushed last. */ class GUI_Window : public Fl_Window { - int x, y, button; + int x{}, y{}, button{}; vkvm::KeyCode keyCode; - int handle(int e); + int handle(int e) override; - char *position_to_string(int x, int y); + static char *position_to_string(int x, int y); public: GUI_Window(int x, int y, const char *l); @@ -40,12 +40,18 @@ public: class Image : public Fl_Widget { uchar *buf; + double delay; + public: Image(int x, int y, int w, int h); - void draw(); + void draw() override; void getPixels(); + + void setDelay(double delay); + + double getDelay(); }; /** Statusbar @@ -53,7 +59,7 @@ public: * @param text: A pointer to a char-array, that can contain a text like a status or whatever you want. */ class Statusbar : public Fl_Box { - char *text; + char* text; public: Statusbar(int x, int y, int w, int h, char *text); }; diff --git a/src/GUI_Window.cpp b/src/GUI_Window.cpp index da0d5f2..5fb0c53 100644 --- a/src/GUI_Window.cpp +++ b/src/GUI_Window.cpp @@ -80,7 +80,7 @@ auto GUI_Window::handle(int e) -> int { GUI_Window::GUI_Window(int x, int y, const char *l) : Fl_Window(x, y, l) {} auto GUI_Window::position_to_string(int x,int y)->char*{ - char *str = new char[25]; + auto *str = new char[25]; std::string str_temp; str = strcpy(str,"Mouse Position X:"); str_temp = std::to_string(x); diff --git a/src/Image.cpp b/src/Image.cpp index c4284df..1e5ec56 100644 --- a/src/Image.cpp +++ b/src/Image.cpp @@ -10,10 +10,10 @@ auto Image::draw() -> void { fl_draw_image(buf, x(), y(), w(), h()); } - Image::Image(int x, int y, int w, int h) : - Fl_Widget(x, y, w, h, 0) { + Fl_Widget(x, y, w, h, nullptr) { buf = new uchar[w * h * 3]; + delay = ((double)vkvm::getTimerInterruptInterval())/1000; /*Just an example.*/ for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { @@ -32,6 +32,12 @@ auto Image::getPixels() -> void { buf[(i * w() + j) * 3 + 2] = c.getBlue(); } } -}; +} +auto Image::getDelay() -> double { + return delay; +} +auto Image::setDelay(double delay) -> void { + this->delay = delay; +}