#define GFX_H_
#include "geom.h"
+#include "pixmap.h"
bool init_gfx();
void destroy_gfx();
unsigned char *get_framebuffer();
+Pixmap *get_framebuffer_pixmap();
+
Rect get_screen_size();
int get_color_depth();
Window *win1 = new Window;
win1->set_title("red");
- win1->move(5, 10);
+ win1->move(200, 100);
win1->resize(200, 300);
win1->set_display_callback(display);
win1->set_keyboard_callback(keyboard);
Window *win2 = new Window;
win2->set_title("yellow");
- win2->move(150, 10);
+ win2->move(300, 100);
win2->resize(200, 300);
win2->set_display_callback(display);
win2->set_keyboard_callback(keyboard);
#include "geom.h"
class Pixmap {
-private:
+public:
int width, height;
unsigned char *pixels;
-public:
Pixmap();
~Pixmap();
static Rect screen_rect = {0, 0, 1024, 768};
static int color_depth = 32; // bits per pixel
+static Pixmap *pixmap;
+
bool init_gfx()
{
if(SDL_Init(SDL_INIT_VIDEO) == -1) {
}
SDL_ShowCursor(0);
+ pixmap = new Pixmap;
+
+ pixmap->width = screen_rect.width;
+ pixmap->height = screen_rect.height;
+
+ pixmap->pixels = (unsigned char*)fbsurf->pixels;
+
return true;
}
void destroy_gfx()
{
+ pixmap->pixels = 0;
+ delete pixmap;
SDL_Quit();
}
return (unsigned char*)fbsurf->pixels;
}
+Pixmap *get_framebuffer_pixmap()
+{
+ return pixmap;
+}
+
Rect get_screen_size()
{
return screen_rect;
--- /dev/null
+#include <ft2build.h>
+#include <freetype/freetype.h>
+
+#include "gfx.h"
+#include "text.h"
+
+#define DPI 72
+#define FONT_PATH "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"
+#define FONT_SIZE 16
+
+static int draw_glyph(Pixmap *pixmap, int x, int y, char c);
+
+static FT_Library ft_lib;
+static FT_Face ft_face;
+
+static int text_x, text_y;
+static int text_color[3] = {255, 255, 255};
+
+bool init_text()
+{
+ if(FT_Init_FreeType(&ft_lib)) {
+ fprintf(stderr, "Failed to initialize the FreeType library!\n");
+ return false;
+ }
+
+ if(FT_New_Face(ft_lib, FONT_PATH, 0, &ft_face)) {
+ fprintf(stderr, "Failed to load font: %s\n", FONT_PATH);
+ return false;
+ }
+
+ if(FT_Set_Char_Size(ft_face, 0, FONT_SIZE * 64, DPI, DPI)) {
+ fprintf(stderr, "Failed to set font size\n");
+ return false;
+ }
+
+ return true;
+}
+
+void draw_text(const char *txt, Pixmap *pixmap)
+{
+ if(!pixmap) {
+ pixmap = get_framebuffer_pixmap();
+ }
+
+ while(*txt != 0) {
+ text_x += draw_glyph(pixmap, text_x, text_y, *txt);
+ txt++;
+ }
+}
+
+void set_text_position(int x, int y)
+{
+ text_x = x;
+ text_y = y;
+
+}
+
+void set_text_color(int r, int g, int b)
+{
+ text_color[0] = r;
+ text_color[1] = g;
+ text_color[2] = b;
+}
+
+static int draw_glyph(Pixmap *pixmap, int x, int y, char c)
+{
+ if(FT_Load_Char(ft_face, c, FT_LOAD_RENDER)) {
+ return 0;
+ }
+
+ x += ft_face->glyph->bitmap_left;
+ y -= ft_face->glyph->bitmap_top;
+
+ FT_Bitmap *ft_bmp = &ft_face->glyph->bitmap;
+ unsigned char *bmp_ptr = ft_bmp->buffer;
+ unsigned char *pxm_ptr = pixmap->get_image() + (pixmap->get_width() * y + x) * 4;
+
+ for(int i=0; i<ft_bmp->rows; i++) {
+ for(int j=0; j<ft_bmp->width; j++) {
+ if(bmp_ptr[j]) {
+ int a = (int)bmp_ptr[j];
+ pxm_ptr[4 * j] = (a * text_color[0] + pxm_ptr[4 * j] * (255 - a)) / 255;
+ pxm_ptr[4 * j + 1] = (a * text_color[1] + pxm_ptr[4 * j + 1] * (255 - a)) / 255;
+ pxm_ptr[4 * j + 2] = (a * text_color[2] + pxm_ptr[4 * j + 2] * (255 - a)) / 255;
+ }
+ }
+ pxm_ptr += 4 * pixmap->get_width();
+ bmp_ptr += ft_bmp->pitch;
+ }
+
+ return ft_face->glyph->advance.x >> 6;
+}
--- /dev/null
+#ifndef TEXT_H_
+#define TEXT_H_
+
+bool init_text();
+
+void draw_text(const char *txt, Pixmap *pixmap = 0);
+void set_text_position(int x, int y);
+void set_text_color(int r, int g, int b);
+
+#endif // TEXT_H_
return false;
}
+ if(!init_text()) {
+ return false;
+ }
+
wm->invalidate_region(get_screen_size());
return true;
}
#include "event.h"
#include "geom.h"
#include "gfx.h"
+#include "text.h"
#include "window.h"
#include "wm.h"
#include <stdio.h> // TODO
#include "gfx.h"
-#include "wm.h"
-#include "window.h"
#include "mouse.h"
#include "mouse_cursor.h"
+#include "text.h"
+#include "wm.h"
+#include "window.h"
WindowManager *wm;
static WindowManager wminst;
frame_thickness = 8;
titlebar_thickness = 16;
- frame_fcolor[0] = frame_fcolor[1] = frame_fcolor[2] = 0;
- frame_ucolor[0] = frame_ucolor[1] = frame_ucolor[2] = 255;
+ set_focused_frame_color(36, 59, 98);
+ set_unfocused_frame_color(80, 129, 162);
mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height);
unsigned char *pixels = mouse_cursor.get_image();
frame_fcolor[2] = b;
}
-void WindowManager::get_focused_frame_color(int *r, int *g, int *b)
+void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
{
*r = frame_fcolor[0];
*g = frame_fcolor[1];
frame_ucolor[2] = b;
}
+void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
+{
+ *r = frame_ucolor[0];
+ *g = frame_ucolor[1];
+ *b = frame_ucolor[2];
+}
+
Window *WindowManager::get_grab_window() const
{
return grab_win;
static void display(Window *win)
{
//frame display:
- Window **children = win->get_children();
- for(int i=0; i<win->get_children_count(); i++) {
- if(children[0] == wm->get_focused_window()) {
- int r, g, b;
- wm->get_focused_frame_color(&r, &g, &b);
- fill_rect(win->get_absolute_rect(), r, g, b);
- return;
- }
+ Window *child = win->get_children()[0];
+ int r, g, b;
+ Rect abs_rect = win->get_absolute_rect();
+
+ //TODO 5 not hardcoded
+ set_text_position(abs_rect.x + 5, abs_rect.y + 15);
+ set_text_color(255, 255, 255);
+
+ if(child == wm->get_focused_window()) {
+ wm->get_focused_frame_color(&r, &g, &b);
+ fill_rect(abs_rect, r, g, b);
+ }
+ else {
+ wm->get_unfocused_frame_color(&r, &g, &b);
+ fill_rect(win->get_absolute_rect(), r, g, b);
}
- fill_rect(win->get_absolute_rect(), 74, 175, 198);
+ draw_text(child->get_title());
}
static int prev_x, prev_y;
Window *get_root_window() const;
void set_focused_frame_color(int r, int g, int b);
- void get_focused_frame_color(int *r, int *g, int *b);
+ void get_focused_frame_color(int *r, int *g, int *b) const;
void set_unfocused_frame_color(int r, int g, int b);
+ void get_unfocused_frame_color(int *r, int *g, int *b) const;
Window *get_grab_window() const;