183 lines
5.8 KiB
C++
183 lines
5.8 KiB
C++
|
|
#include <iostream>
|
|
#include <Fl/Fl.H>
|
|
#include <Fl/Fl_Box.H>
|
|
#include <FL/Fl_Window.H>
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include "vkvm.hpp"
|
|
|
|
|
|
#define window_height 600
|
|
#define window_width 800
|
|
|
|
/**************************************[[l***************************************/
|
|
|
|
//Es ist Class fuer Status
|
|
class Status_Leiste : public Fl_Box {
|
|
char *text;
|
|
public:
|
|
//Konstruktor
|
|
Status_Leiste(int x, int y, int w, int h, char *text) :
|
|
Fl_Box(x, y, w, h,text) {
|
|
this->text = text;
|
|
};
|
|
void set_text(char *text) {
|
|
this->text = text;
|
|
}
|
|
//nur zu Beispiel, zu zeigen, dass der Inhalt von Status einfach geaendert werden kann.
|
|
void change_text(){
|
|
if(text=="status0")
|
|
set_text("status1");
|
|
else
|
|
set_text("status0");
|
|
}
|
|
//der neue Inhalt von Status zeigen
|
|
void refresh_label(){
|
|
this->label(text);
|
|
}
|
|
};
|
|
|
|
//Pixel ist ein neue Class fuer ein Pixel
|
|
class Bild : public Fl_Widget {
|
|
uchar *buf;//Bitmap fuer das Bild, es braucht 3 char um ein Pixel zu erklaren(RGB), deshalb die Groesse von buf ist window_height * window_width * 3
|
|
|
|
//buf ist das Bitmap fuer das Bild, x,y fuer Postion in Window, w, h, bedeutet height und width
|
|
void draw() {
|
|
fl_draw_image(buf, x(), y(), w(), h());
|
|
}
|
|
|
|
public:
|
|
//Konstruktor
|
|
Bild(int x, int y, int w, int h) :
|
|
Fl_Widget(x, y, w, h, 0) {
|
|
buf = new uchar[w * h * 3];
|
|
//nur als Beispiel
|
|
for (int i = 0; i < h; i++) {
|
|
for (int j = 0; j < w; j++) {
|
|
buf[(j + (i * w)) * 3 + 1] = 0xff;
|
|
}
|
|
}
|
|
}
|
|
|
|
//die Farbe vom Bild aendern(Die Reihenfolge: gruen, rot, blau, gruen )
|
|
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();
|
|
}
|
|
}
|
|
/*
|
|
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;
|
|
*/
|
|
}
|
|
};
|
|
|
|
|
|
class My_Window : public Fl_Window {
|
|
int x, y, button;
|
|
|
|
//die Inputs von Maus und Tastatur behandln
|
|
int handle(int e) {
|
|
switch (e) {
|
|
//Druck vom Maus behandln
|
|
case FL_PUSH:
|
|
if (Fl::event_button() == FL_LEFT_MOUSE) {
|
|
std::cout << "Mouse:left" << std::endl;
|
|
} else if (Fl::event_button() == FL_RIGHT_MOUSE) {
|
|
std::cout << "Mouse:right" << std::endl;
|
|
} else {
|
|
std::cout << "Mouse:middle" << std::endl;
|
|
}
|
|
return 1;
|
|
//Druck und Bewegung vom Maus behandln
|
|
case FL_DRAG:
|
|
x = Fl::event_x();
|
|
y = Fl::event_y();
|
|
std::cout << "Postion X:" << x << " Postion Y:" << y << std::endl;
|
|
if (Fl::event_button() == FL_LEFT_MOUSE) {
|
|
std::cout << "Mouse:left" << std::endl;
|
|
} else if (Fl::event_button() == FL_RIGHT_MOUSE) {
|
|
std::cout << "Mouse:right" << std::endl;
|
|
} else {
|
|
std::cout << "Mouse:middle" << std::endl;
|
|
}
|
|
return 1;
|
|
//Bewegung vom Maus behandln
|
|
case FL_MOVE:
|
|
x = Fl::event_x();
|
|
y = Fl::event_y();
|
|
std::cout << "Postion X:" << x << " Postion Y:" << y << std::endl;
|
|
return 1;
|
|
//Druck von der Tastator behandln
|
|
case FL_KEYBOARD:
|
|
button = Fl::event_button();
|
|
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:
|
|
//Konstruktor
|
|
My_Window(int x, int y, const char *l) : Fl_Window(x, y, l) {}
|
|
};
|
|
|
|
//draw() wird regelmaessig anrufen
|
|
void refresh_bild(void *pointer) {
|
|
((Bild *) pointer)->redraw();
|
|
Fl::repeat_timeout(0.5, refresh_bild, pointer);//nach 0.5 sec refresh_bild(pointer) anrufen. Here pointer ist wie ein Parameter
|
|
}
|
|
|
|
//die Farbe von Pixels regekmaessig aendern
|
|
void change_color(void *pointer) {
|
|
((Bild *) pointer)->change_color();
|
|
Fl::repeat_timeout(1, change_color, pointer);
|
|
}
|
|
|
|
//refresh_status() von Status0 wird regelmaessig anrufen
|
|
void refresh_status_leiste(void *pointer) {
|
|
((Status_Leiste **) pointer)[0]->refresh_label();
|
|
Fl::repeat_timeout(0.5, refresh_status_leiste, pointer);
|
|
}
|
|
|
|
//der Inhalt von Status0 regekmaessig aendern
|
|
void change_status(void *pointer) {
|
|
((Status_Leiste **) pointer)[0]->change_text();
|
|
Fl::repeat_timeout(1, change_status, pointer);
|
|
}
|
|
// main
|
|
int main(int argc, char **argv) {
|
|
vkvm::initialize(0);
|
|
|
|
My_Window *window = new My_Window(window_width, window_height, "example");
|
|
Status_Leiste *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);
|
|
Fl::repeat_timeout(1, change_status, status);
|
|
window->end();
|
|
window->show(argc, argv);
|
|
return Fl::run();
|
|
} |