Ver código fonte

Implement pixmap double buffering

rexim 4 anos atrás
pai
commit
57c62be558
4 arquivos alterados com 59 adições e 5 exclusões
  1. 5 1
      .gitignore
  2. 11 2
      Makefile
  3. 39 0
      db_pixmap.c
  4. 4 2
      main.c

+ 5 - 1
.gitignore

@@ -1 +1,5 @@
-main
+main
+main.none
+main.xdbe
+main.pixmap
+main.ximage

+ 11 - 2
Makefile

@@ -1,5 +1,14 @@
 CFLAGS=-Wall -Wextra -std=c11 -pedantic -ggdb
 LIBS=-lX11 -lXext
 
-main: main.c
-	$(CC) $(CFLAGS) -DDB_IMPL=0 -o main main.c $(LIBS)
+.PHONY: all
+all: main.none main.xdbe main.pixmap 
+
+main.none: main.c
+	$(CC) $(CFLAGS) -DDB_IMPL=DB_NONE -o main.none main.c $(LIBS)
+
+main.xdbe: main.c
+	$(CC) $(CFLAGS) -DDB_IMPL=DB_XDBE -o main.xdbe main.c $(LIBS)
+
+main.pixmap: main.c
+	$(CC) $(CFLAGS) -DDB_IMPL=DB_PIXMAP -o main.pixmap main.c $(LIBS)

+ 39 - 0
db_pixmap.c

@@ -0,0 +1,39 @@
+#define DB_IMPL_NAME "Pixmap"
+
+typedef struct {
+    Display *display;
+    Window window;
+    GC gc;
+    Pixmap back_buffer;
+} DB;
+
+void db_init(DB *db, Display *display, Window window)
+{
+    db->display = display;
+    db->window = window;
+    db->gc = XCreateGC(display, window, 0, NULL);
+    db->back_buffer = XCreatePixmap(display, window, WIDTH, HEIGHT, 24);
+}
+
+void db_clear(DB *db)
+{
+    XSetForeground(db->display, db->gc, 0);
+    XFillRectangle(db->display, db->back_buffer, db->gc, 0, 0, WIDTH, HEIGHT);
+}
+
+void db_fill_rect(DB *db, int x, int y, int w, int h)
+{
+    XSetForeground(db->display, db->gc, 0xFF0000);
+    XFillRectangle(db->display, db->back_buffer, db->gc, x, y, w, h);
+}
+
+void db_swap_buffers(DB *db)
+{
+    XCopyArea(db->display,
+              db->back_buffer,
+              db->window,
+              db->gc,
+              0, 0,
+              WIDTH, HEIGHT,
+              0, 0);
+}

+ 4 - 2
main.c

@@ -15,13 +15,15 @@
 
 #define DB_NONE   0
 #define DB_XDBE   1
-#define DB_XIMAGE 2
-#define DB_PIXMAP 3
+#define DB_PIXMAP 2
+#define DB_XIMAGE 3
 
 #if DB_IMPL == DB_NONE
 #  include "./db_none.c"
 #elif DB_IMPL == DB_XDBE
 #  include "./db_xdbe.c"
+#elif DB_IMPL == DB_PIXMAP
+#  include "./db_pixmap.c"
 #else
 #  error "Unsupported Double Buffering approach"
 #endif