From f71618aebfa6b8754dd056689a6c5821b755972c Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Sat, 16 Feb 2013 18:59:37 +0200 Subject: [PATCH] *work in progress* window drawing works xD --- .bzrignore | 9 ++++++++ src/event.cc | 4 ++-- src/geom.cc | 33 +++++++++++++++++++++++++++++ src/gfx.cc | 6 +++--- src/main.cc | 66 +++++++++++++++++++++++++++++++-------------------------- src/window.cc | 22 +++++++++++++++++++ src/winnie.cc | 16 ++++++++++++++ src/winnie.h | 13 ++++++++++++ src/wm.cc | 5 +++++ src/wm.h | 2 ++ 10 files changed, 141 insertions(+), 35 deletions(-) create mode 100644 .bzrignore create mode 100644 src/geom.cc create mode 100644 src/winnie.cc create mode 100644 src/winnie.h diff --git a/.bzrignore b/.bzrignore new file mode 100644 index 0000000..94ca2b4 --- /dev/null +++ b/.bzrignore @@ -0,0 +1,9 @@ +src/event.d +src/geom.d +src/gfx.d +src/keyboard.d +src/main.d +src/mouse.d +src/window.d +src/winnie.d +src/wm.d diff --git a/src/event.cc b/src/event.cc index e5a7182..4b80d04 100644 --- a/src/event.cc +++ b/src/event.cc @@ -12,6 +12,8 @@ void process_events() int mouse_fd = get_mouse_fd(); for(;;) { + wm->process_windows(); + fd_set read_set; FD_ZERO(&read_set); @@ -28,7 +30,5 @@ void process_events() if(FD_ISSET(mouse_fd, &read_set)) { process_mouse_event(); } - - wm->process_windows(); } } diff --git a/src/geom.cc b/src/geom.cc new file mode 100644 index 0000000..c9d5c51 --- /dev/null +++ b/src/geom.cc @@ -0,0 +1,33 @@ +#include "geom.h" + +static inline int min(int x, int y) +{ + return x < y ? x : y; +} + +static inline int max(int x, int y) +{ + return x > y ? x : y; +} + +Rect rect_union(const Rect &a, const Rect &b) +{ + Rect uni; + uni.x = min(a.x, b.x); + uni.y = min(a.y, b.y); + uni.width = max(a.x + a.width, b.x + b.width) - uni.x; + uni.height = max(a.y + a.height, b.y + b.height) - uni.y; + + return uni; +} + +Rect rect_intersection(const Rect &a, const Rect &b) +{ + Rect intersect; + intersect.x = max(a.x, b.x); + intersect.y = max(a.y, b.y); + intersect.width = max(min(a.x + a.width, b.x + b.width) - intersect.x, 0); + intersect.height = max(min(a.y + a.height, b.y + b.height) - intersect.y, 0); + + return intersect; +} diff --git a/src/gfx.cc b/src/gfx.cc index 0afa479..7b3513d 100644 --- a/src/gfx.cc +++ b/src/gfx.cc @@ -87,12 +87,12 @@ void clear_screen(int r, int g, int b) void fill_rect(const Rect &rect, int r, int g, int b) { - unsigned char *fb = framebuffer + rect.x + screen_rect.width * rect.y; + unsigned char *fb = framebuffer + (rect.x + screen_rect.width * rect.y) * 4; for(int i=0; i +#include -#include "gfx.h" +#include "winnie.h" + +static void display(Window *win); +static void keyboard(Window *win, int key, bool pressed); +static void cleanup(); int main() { - if(!init_gfx()) { - return 1; - } + winnie_init(); + atexit(cleanup); + + Window *win1 = new Window; + win1->set_title("title1"); + win1->move(5, 10); + win1->resize(600, 800); + win1->set_display_callback(display); + win1->set_keyboard_callback(keyboard); + + wm->add_window(win1); - set_cursor_visibility(false); - - unsigned char* fb = get_framebuffer(); - Rect scrn_sz = get_screen_size(); - - for(int i=0; i> 4) & 1) == ((i >> 4) & 1)) { - color = color0; - } - else { - color = color1; - } - - *fb++ = color[0]; - *fb++ = color[1]; - *fb++ = color[2]; - fb++; - } + while(1) { + process_events(); } - getchar(); - clear_screen(0, 0, 0); + winnie_shutdown(); +} +static void display(Window *win) +{ + fill_rect(win->get_rect(), 0, 0, 0); +} + +static void keyboard(Window *win, int key, bool pressed) +{ + switch(key) { + case 'q': + exit(0); + } +} + +static void cleanup() +{ destroy_gfx(); } diff --git a/src/window.cc b/src/window.cc index 76c5177..af30e3c 100644 --- a/src/window.cc +++ b/src/window.cc @@ -57,6 +57,8 @@ void Window::invalidate() void Window::draw() { + //TODO + //titlebar, frame callbacks.display(this); dirty = false; } @@ -71,3 +73,23 @@ int Window::get_scanline_width() { return get_screen_size().x; } + +void Window::set_display_callback(DisplayFuncType func) +{ + callbacks.display = func; +} + +void Window::set_keyboard_callback(KeyboardFuncType func) +{ + callbacks.keyboard = func; +} + +void Window::set_mouse_button_callback(MouseButtonFuncType func) +{ + callbacks.button = func; +} + +void Window::set_mouse_motion_callback(MouseMotionFuncType func) +{ + callbacks.motion = func; +} diff --git a/src/winnie.cc b/src/winnie.cc new file mode 100644 index 0000000..4f068ba --- /dev/null +++ b/src/winnie.cc @@ -0,0 +1,16 @@ +#include "winnie.h" + +bool winnie_init() +{ + if(!init_gfx()) { + return false; + } + + wm->invalidate_region(get_screen_size()); + return true; +} + +void winnie_shutdown() +{ + destroy_gfx(); +} diff --git a/src/winnie.h b/src/winnie.h new file mode 100644 index 0000000..1abf460 --- /dev/null +++ b/src/winnie.h @@ -0,0 +1,13 @@ +#ifndef WINNIE_H_ +#define WINNIE_H_ + +#include "event.h" +#include "geom.h" +#include "gfx.h" +#include "window.h" +#include "wm.h" + +bool winnie_init(); +void winnie_shutdown(); + +#endif diff --git a/src/wm.cc b/src/wm.cc index 16b5a2a..c0fab3c 100644 --- a/src/wm.cc +++ b/src/wm.cc @@ -47,3 +47,8 @@ void WindowManager::process_windows() } } } + +void WindowManager::add_window(Window *win) +{ + windows.push_back(win); +} diff --git a/src/wm.h b/src/wm.h index a754186..88a2178 100644 --- a/src/wm.h +++ b/src/wm.h @@ -18,6 +18,8 @@ public: void invalidate_region(const Rect &rect); void process_windows(); + + void add_window(Window *win); }; extern WindowManager *wm; -- 1.7.10.4