From 5449bf8fbca1d2cfbefa0386df7109afb2a5aa34 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Wed, 20 Feb 2013 01:44:03 +0200 Subject: [PATCH] * in progress * added blit and blit_key functions to the graphics code added mouse cursor drawing --- src/gfx.cc | 94 ++++++++++++++++++++++++++++++++++++++++++++++++---- src/gfx.h | 6 +++- src/mouse.cc | 4 +-- src/mouse_cursor.h | 28 ++++++++++++++++ src/pixmap.cc | 86 +++++++++++++++++++++++++++++++++++++++++++++++ src/pixmap.h | 27 +++++++++++++++ src/window.cc | 9 ++--- src/wm.cc | 26 +++++++++++++++ src/wm.h | 3 ++ 9 files changed, 266 insertions(+), 17 deletions(-) create mode 100644 src/mouse_cursor.h create mode 100644 src/pixmap.cc create mode 100644 src/pixmap.h diff --git a/src/gfx.cc b/src/gfx.cc index 226d269..5c04d1e 100644 --- a/src/gfx.cc +++ b/src/gfx.cc @@ -113,17 +113,97 @@ void set_cursor_visibility(bool visible) } } -void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, int dest_x, int dest_y) +void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, + const Rect &dest_rect, int dest_x, int dest_y) { - Rect dest_rect; + int width = src_rect.width; + int height = src_rect.height; - if(dest_x < screen_rect.x) { - dest_rect.x = screen_rect.x; + int xoffs = dest_x - dest_rect.x; + if(xoffs < 0) { + dest_x = dest_rect.x; + width += xoffs; } - if(dest_y < screen_rect.y) { - dest_rect.y = screen_rect.y; + int yoffs = dest_y - dest_rect.y; + if(yoffs < 0) { + dest_y = dest_rect.y; + height += yoffs; } - //TODO :p zzz + int xend = dest_x + width; + if(xend >= dest_rect.width) { + width -= xend - dest_rect.width; + } + + int yend = dest_y + height; + if(yend >= dest_rect.height) { + height -= yend - dest_rect.height; + } + + if(width <= 0 || height <= 0) { + return; + } + + unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4; + unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4; + + for(int i=0; i= dest_rect.width) { + width -= xend - dest_rect.width; + } + + int yend = dest_y + height; + if(yend >= dest_rect.height) { + height -= yend - dest_rect.height; + } + + if(width <= 0 || height <= 0) { + return; + } + + unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4; + unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4; + + for(int i=0; i +#include +#include +#include "pixmap.h" + +Pixmap::Pixmap() +{ + width = height = 0; + pixels = 0; +} + +Pixmap::~Pixmap() +{ + if(pixels) { + delete [] pixels; + } +} + +int Pixmap::get_width() const +{ + return width; +} + +int Pixmap::get_height() const +{ + return height; +} + +Rect Pixmap::get_rect() const +{ + Rect rect = {0, 0, width, height}; + return rect; +} + +bool Pixmap::set_image(int x, int y, unsigned char *pix) +{ + delete [] pixels; + + pixels = new unsigned char[x * y * 4]; + width = x; + height = y; + + if(pix) { + memcpy(pixels, pix, x * y * 4); + } + return true; +} + +const unsigned char *Pixmap::get_image() const +{ + return pixels; +} + +unsigned char *Pixmap::get_image() +{ + return pixels; +} + +bool Pixmap::load(const char *fname) +{ + return false; // TODO +} + +bool Pixmap::save(const char *fname) const +{ + if(!pixels) { + return false; + } + + FILE *fp = fopen(fname, "wb"); + if(!fp) { + fprintf(stderr, "failed to save pixmap: %s: %s\n", fname, strerror(errno)); + return false; + } + + fprintf(fp, "P6\n%d %d\n255\n", width, height); + + for(int i=0; i= ptr_x)) { - if((rect.y <= ptr_y) && (ptr_y <= (rect.y + rect.height))) { - return true; - } - } - - return false; + return ptr_x >= rect.x && ptr_x < rect.x + rect.width && + ptr_y >= rect.y && ptr_y < rect.y + rect.height; } void Window::move(int x, int y) diff --git a/src/wm.cc b/src/wm.cc index f65c9c2..b661234 100644 --- a/src/wm.cc +++ b/src/wm.cc @@ -2,6 +2,8 @@ #include "gfx.h" #include "wm.h" #include "window.h" +#include "mouse.h" +#include "mouse_cursor.h" WindowManager *wm; static WindowManager wminst; @@ -19,6 +21,19 @@ WindowManager::WindowManager() bg_color[0] = 210; bg_color[1] = 106; bg_color[2] = 106; + + mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height); + unsigned char *pixels = mouse_cursor.get_image(); + + for(int i=0; i #include "geom.h" +#include "pixmap.h" class Window; @@ -14,6 +15,8 @@ private: int bg_color[3]; Window *focused_win; + Pixmap mouse_cursor; + public: WindowManager(); -- 1.7.10.4