experiment #1: writing some pixels in the framebuffer device and cls
authorEleni Maria Stea <elene.mst@gmail.com>
Sun, 3 Feb 2013 20:30:45 +0000 (22:30 +0200)
committerEleni Maria Stea <elene.mst@gmail.com>
Sun, 3 Feb 2013 20:30:45 +0000 (22:30 +0200)
Makefile [new file with mode: 0644]
src/event.cc [new file with mode: 0644]
src/event.h [new file with mode: 0644]
src/gfx.cc [new file with mode: 0644]
src/gfx.h [new file with mode: 0644]
src/main.cc [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..3ced08b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,25 @@
+src = $(wildcard src/*.cc)
+obj = $(src:.cc=.o)
+dep = $(obj:.o=.d)
+bin = winnie
+
+
+dbg = -g
+opt = -O0
+inc = -Isrc -Isrc/shaders -Isrc/math
+
+CXX = g++
+CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(inc)
+#LDFLAGS = 
+
+$(bin): $(obj)
+       $(CXX) -o $@ $(obj) $(LDFLAGS)
+
+-include $(dep)
+
+%.d: %.cc
+       @$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(bin) $(dep)
diff --git a/src/event.cc b/src/event.cc
new file mode 100644 (file)
index 0000000..4656575
--- /dev/null
@@ -0,0 +1,46 @@
+#include "event.h"
+
+static DisplayFuncType display_func;
+static KeyboardFuncType keyboard_func;
+static MouseButtonFuncType mouse_button_func;
+static MouseMotionFuncType mouse_motion_func;
+
+void set_display_callback(DisplayFuncType display)
+{
+       display_func = display;
+}
+
+void set_keyboard_callback(KeyboardFuncType keyboard)
+{
+       keyboard_func = keyboard;
+}
+
+void set_mouse_button_callback(MouseButtonFuncType mouse_button)
+{
+       mouse_button_func = mouse_button;
+}
+
+void set_mouse_motion_callback(MouseMotionFuncType mouse_motion)
+{
+       mouse_motion_func = mouse_motion;
+}
+
+DisplayFuncType get_display_callback()
+{
+       return display_func;
+}
+
+KeyboardFuncType get_keyboard_callback()
+{
+       return keyboard_func;
+}
+
+MouseButtonFuncType get_mouse_button_callback()
+{
+       return mouse_button_func;
+}
+
+MouseMotionFuncType get_mouse_motion_callback()
+{
+       return mouse_motion_func;
+}
diff --git a/src/event.h b/src/event.h
new file mode 100644 (file)
index 0000000..0c2ccd2
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef EVENT_H_
+#define EVENT_H_
+
+typedef void (*DisplayFuncType)();
+typedef void (*KeyboardFuncType)(int key, bool pressed);
+typedef void (*MouseButtonFuncType)(int bn, bool pressed);
+typedef void (*MouseMotionFuncType)(int x, int y);
+
+void set_display_callback(DisplayFuncType display);
+void set_keyboard_callback(KeyboardFuncType keyboard);
+void set_mouse_button_callback(MouseButtonFuncType mouse_button);
+void set_mouse_motion_callback(MouseMotionFuncType mouse_motion);
+
+DisplayFuncType get_display_callback();
+KeyboardFuncType get_keyboard_callback();
+MouseButtonFuncType get_mouse_button_callback();
+MouseMotionFuncType get_mouse_motion_callback();
+
+#endif
diff --git a/src/gfx.cc b/src/gfx.cc
new file mode 100644 (file)
index 0000000..54ed7f9
--- /dev/null
@@ -0,0 +1,102 @@
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <linux/fb.h>
+
+#include "gfx.h"
+
+#define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
+
+static unsigned char* framebuffer;
+static int dev_fd = -1;
+
+static Rect screen_rect;
+static int color_depth; //bits per pixel
+
+bool init_gfx()
+{
+       if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
+               fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
+               return false;
+       }
+
+       fb_var_screeninfo sinfo;
+       if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
+               close(dev_fd);
+               dev_fd = -1;
+               fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
+               return false;
+       }
+
+       printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
+       printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
+
+       screen_rect.x = screen_rect.y = 0;
+       screen_rect.width = sinfo.xres_virtual;
+       screen_rect.height = sinfo.yres_virtual;
+       color_depth = sinfo.bits_per_pixel;
+
+       int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth);
+       framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
+
+       if(framebuffer == (void*)-1) {
+               close(dev_fd);
+               dev_fd = -1;
+               fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
+               return false;
+       }
+
+       return true;
+}
+
+void destroy_gfx()
+{
+       close(dev_fd);
+       dev_fd = -1;
+
+       munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth));
+       framebuffer = 0;
+}
+
+unsigned char* get_framebuffer()
+{
+       return framebuffer;
+}
+
+Rect get_screen_size()
+{
+       return screen_rect;
+}
+
+int get_color_depth()
+{
+       return color_depth;
+}
+
+void clear_screen(int r, int g, int b)
+{
+       unsigned char* fb = framebuffer;
+       for(int i=0; i<screen_rect.width * screen_rect.height; i++) {
+               *fb++ = r;
+               *fb++ = g;
+               *fb++ = b;
+               fb++;
+       }
+}
+
+void set_cursor_visibility(bool visible)
+{
+       fb_cursor curs;
+       curs.enable = visible ? 1 : 0;
+
+       if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
+               fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
+       }
+}
diff --git a/src/gfx.h b/src/gfx.h
new file mode 100644 (file)
index 0000000..7868b01
--- /dev/null
+++ b/src/gfx.h
@@ -0,0 +1,22 @@
+#ifndef GFX_H_
+#define GFX_H_
+
+struct Rect {
+       int x;
+       int y;
+       int width;
+       int height;
+};
+
+bool init_gfx();
+void destroy_gfx();
+
+unsigned char* get_framebuffer();
+Rect get_screen_size();
+int get_color_depth();
+
+void clear_screen(int r, int g, int b);
+
+void set_cursor_visibility(bool visible);
+
+#endif //GFX_H_
diff --git a/src/main.cc b/src/main.cc
new file mode 100644 (file)
index 0000000..b4f5f3a
--- /dev/null
@@ -0,0 +1,41 @@
+#include <stdio.h>
+
+#include "gfx.h"
+
+int main()
+{
+       if(!init_gfx()) {
+               return 1;
+       }
+
+       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++;
+               }
+       }
+
+       getchar();
+       clear_screen(0, 0, 0);
+
+       destroy_gfx();
+}