Aktuellste Version der GUI-main.cpp. Farben werden nun von der

Mamory ausgelesen.
This commit is contained in:
Edgar Schkrob 2019-11-14 12:40:34 +01:00
parent 9bd85c5273
commit 5d55c2ec95

View File

@ -2,10 +2,11 @@
#include <iostream>
#include <Fl/Fl.H>
#include <Fl/Fl_Box.H>
#include <Fl/Fl_Output.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
#include "vkvm.hpp"
#define window_height 600
#define window_width 800
@ -15,6 +16,7 @@
/** The class inherits a char *text from the Fl_Box. It also shows the window-resolution and the mouse-position.
* @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;
public:
@ -38,54 +40,12 @@ public:
else
set_text("status0");
}
//This function refreshes the statusbar
//This function refreshes the statusbar.
void refresh_label(){
this->label(text);
}
};
//Image**************************************/
/** The Image-class draws the bitmap that it get from the Shared Memory.
* @param *buf: A pointer to the bitmap, that the image-class has to draw. Three chars are needed to get the RGB-value of a pixel, so the size equals window_height * window_width * 3.
*/
class Image : public Fl_Widget {
uchar *buf;
//Function to draw a bitmap
void draw() {
fl_draw_image(buf, x(), y(), w(), h());
}
/** The constructor of the image class, get additional
* @param x: The mouse-position on the x-axis.
* @param y: The mouse-position on the y-axis:
* @param w: The width of a single pixel,
* @param h: The height of a single pixel.
*/
public:
Image(int x, int y, int w, int h) :
Fl_Widget(x, y, w, h, 0) {
buf = new uchar[w * h * 3];
/*Just an example.*/
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
buf[(j + (i * w)) * 3 + 1] = 0xff;
}
}
}
/*A function to change the colors of the image-class. The colors are changing in this order: green, blue, red*/
void change_color() {
for (int j = 1; j < w() * h() * 3; j++) {
if (buf[j] == 0xff) {
buf[j] = 0;
buf[j - 1] = 0xff;
}
}
if (buf[2] == 0xff)
buf[w() * h() * 3 - 1] = 0xff;
}
};
//My_Window*************************************/
/** The My_Windows-class generates a window, within the window it recognizes the curretn mouse-position.
@ -95,6 +55,7 @@ public:
* @parame y The mouse-position on the y-axis.
* @parame button The button that was pushed last.
*/
class My_Window : public Fl_Window {
int x, y, button;
@ -133,21 +94,69 @@ class My_Window : public Fl_Window {
/*keyboardbutton*/
case FL_KEYBOARD:
button = Fl::event_button();
std::cout << "Keyboard:" << (unsigned short) button << std::endl;
std::cout << "Keyboard:" << (unsigned short) button << " down"<<std::endl;
return 1;
case FL_KEYUP:
button = Fl::event_button();
std::cout << "Keyboard:" << (unsigned short) button << " up"<<std::endl;
return 1;
}
}
public:
/*Constructor*/
My_Window(int x, int y, const char *l) : Fl_Window(x, y, l) {}
};
//Image**************************************/
/** The Image-class draws the bitmap that it get from the Shared Memory.
* @param *buf: A pointer to the bitmap, that the image-class has to draw. Three chars are needed to get the RGB-value of a pixel, so the size equals window_height * window_width * 3.
*/
class Image : public Fl_Widget {
uchar *buf;
//Function to draw a bitmap
void draw() {
fl_draw_image(buf, x(), y(), w(), h());
}
/** The constructor of the image class, get additional
* @param x: The mouse-position on the x-axis.
* @param y: The mouse-position on the y-axis:
* @param w: The width of a single pixel,
* @param h: The height of a single pixel.
*/
public:
Image(int x, int y, int w, int h) :
Fl_Widget(x, y, w, h, 0) {
buf = new uchar[w * h * 3];
/*Just an example.*/
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
buf[(j + (i * w)) * 3 + 1] = 0xff;
}
}
}
/*A function to change the colors of the image-class. It reads the colors from the Shared Memory-Class*/
void change_color() {
for (int i = 0; i < h(); i++) {
for (int j = 0; j < w(); j++) {
vkvm::Color c = vkvm::getPixel(j,i);
buf[(i * w() + j) * 3 + 0] = c.getRed();
buf[(i * w() + j) * 3 + 1] = c.getGreen();
buf[(i * w() + j) * 3 + 2] = c.getBlue();
}
}
}
};
/*Functions to refresh the image.*/
void refresh_image(void *pointer) {
((Image *) pointer)->redraw();
Fl::repeat_timeout(0.5, refresh_image, pointer);//nach 0.5 sec refresh_bild(pointer) anrufen. Here pointer ist wie ein Parameter
Fl::repeat_timeout(0.5, refresh_image, pointer);//nach 0.5 sec refresh_image(pointer) anrufen. Here pointer ist wie ein Parameter
}
void change_color(void *pointer) {
@ -161,29 +170,30 @@ void refresh_statusbar(void *pointer) {
}
void change_status(void *pointer) {
((Status_Leiste **) pointer)[0]->change_text();
((Statusbar **) pointer)[0]->change_text();
Fl::repeat_timeout(1, change_status, pointer);
}
//main*************************************/
/**
* The main function initializes all needed classes and executes the functions.
*/
int main(int argc, char **argv) {
vkvm::initialize(0);
My_Window *window = new My_Window(window_width, window_height, "example");
Status_Leiste *status[5];
Statusbar *status[5];
window->begin();
Bild *bild = new Bild(0, 30, window_width, window_height - 30);
status[0] = new Status_Leiste(0, 0, 60, 30, "status0");
status[1] = new Status_Leiste(60, 0, 60, 30, "status1");
status[2] = new Status_Leiste(120, 0, 60, 30, "status2");
status[3] = new Status_Leiste(180, 0, 60, 30, "status3");
status[4] = new Status_Leiste(240, 0, 60, 30, "status4");
Fl::repeat_timeout(0.5, refresh_bild, bild);
Fl::repeat_timeout(1, change_color, bild);
Fl::repeat_timeout(0.5, refresh_status_leiste, status);
Image *image = new Image(0, 30, window_width, window_height - 30);
status[0] = new Statusbar(0, 0, 60, 30, "status0");
status[1] = new Statusbar(60, 0, 60, 30, "status1");
status[2] = new Statusbar(120, 0, 60, 30, "status2");
status[3] = new Statusbar(180, 0, 60, 30, "status3");
status[4] = new Statusbar(240, 0, 60, 30, "status4");
Fl::repeat_timeout(0.5, refresh_image, image);
Fl::repeat_timeout(1, change_color, image);
Fl::repeat_timeout(0.5, refresh_statusbar, status);
Fl::repeat_timeout(1, change_status, status);
window->end();
window->show(argc, argv);
return Fl::run();
}main
}