*work in progress*
authorEleni Maria Stea <elene.mst@gmail.com>
Sat, 16 Feb 2013 16:59:37 +0000 (18:59 +0200)
committerEleni Maria Stea <elene.mst@gmail.com>
Sat, 16 Feb 2013 16:59:37 +0000 (18:59 +0200)
window drawing works xD

.bzrignore [new file with mode: 0644]
src/event.cc
src/geom.cc [new file with mode: 0644]
src/gfx.cc
src/main.cc
src/window.cc
src/winnie.cc [new file with mode: 0644]
src/winnie.h [new file with mode: 0644]
src/wm.cc
src/wm.h

diff --git a/.bzrignore b/.bzrignore
new file mode 100644 (file)
index 0000000..94ca2b4
--- /dev/null
@@ -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
index e5a7182..4b80d04 100644 (file)
@@ -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 (file)
index 0000000..c9d5c51
--- /dev/null
@@ -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;
+}
index 0afa479..7b3513d 100644 (file)
@@ -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<rect.height; i++) {
                for(int j=0; j<rect.width; j++) {
-                       fb[j * 4] = r;
+                       fb[j * 4] = b;
                        fb[j * 4 + 1] = g;
-                       fb[j * 4 + 2] = b;
+                       fb[j * 4 + 2] = r;
                }
                fb += screen_rect.width * 4;
        }
index b4f5f3a..db00cb5 100644 (file)
@@ -1,41 +1,47 @@
 #include <stdio.h>
+#include <stdlib.h>
 
-#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<scrn_sz.height; i++) {
-               for(int j=0; j<scrn_sz.width; j++) {
-                       unsigned char color0[3] = {255, 0, 0};
-                       unsigned char color1[3] = {0, 0, 255};
-                       unsigned char* color;
-
-                       //red blue chessboard 16 pxls size
-                       if(((j >> 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();
 }
index 76c5177..af30e3c 100644 (file)
@@ -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 (file)
index 0000000..4f068ba
--- /dev/null
@@ -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 (file)
index 0000000..1abf460
--- /dev/null
@@ -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
index 16b5a2a..c0fab3c 100644 (file)
--- 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);
+}
index a754186..88a2178 100644 (file)
--- 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;