소스 검색

tried to use double buffers based on the example code, now i get a black screen

tarfeef101 6 년 전
부모
커밋
f3038a47fc
2개의 변경된 파일62개의 추가작업 그리고 36개의 파일을 삭제
  1. BIN
      assignments/a1/snake
  2. 62 36
      assignments/a1/snake.cc

BIN
assignments/a1/snake


+ 62 - 36
assignments/a1/snake.cc

@@ -393,15 +393,12 @@ void initX(int argc, char * argv[], XInfo & xInfo)
 /*
  * Function to repaint a display list
  */
-void repaint( XInfo &xinfo)
+void repaint(XInfo & xinfo)
 {
 	list<Displayable *>::const_iterator begin = dList.begin();
 	list<Displayable *>::const_iterator end = dList.end();
 
-	XClearWindow( xinfo.display, xinfo.window );
-
 	// get height and width of window (might have changed since last repaint)
-
 	XWindowAttributes windowInfo;
 	XGetWindowAttributes(xinfo.display, xinfo.window, &windowInfo);
 	unsigned int height = windowInfo.height;
@@ -423,11 +420,6 @@ void handleKeyPress(XInfo & xinfo, XEvent & event)
 {
 	KeySym key;
 	char text[BufferSize];
-
-	/*
-	 * Exit when 'q' is typed.
-	 * This is a simplified approach that does NOT use localization.
-	 */
 	int i = XLookupString
   (
 		(XKeyEvent *)&event, 	// the keyboard event
@@ -436,7 +428,7 @@ void handleKeyPress(XInfo & xinfo, XEvent & event)
 		&key, 					// workstation-independent key symbol
 		NULL
   );
-  					// pointer to a composeStatus structure (unused)
+
 	if (i == 1)
   {
 		printf("Got key press -- %c\n", text[0]);
@@ -494,39 +486,73 @@ void eventLoop(XInfo & xinfo)
   dList.push_front(&fruity);
 
 	XEvent event;
-	unsigned long lastRepaint = 0;
-	int inside = 0;
 
-	while(1)
+  unsigned long lastRepaint = 0;
+  int inside = 0;
+  XWindowAttributes w;
+  XGetWindowAttributes(xinfo.display, xinfo.window, &w);
+
+  int depth = DefaultDepth(xinfo.display, DefaultScreen(xinfo.display));
+  // represents thing to draw
+  Pixmap buffer = XCreatePixmap(xinfo.display, xinfo.window, w.width, w.height, depth);
+  bool usebuffer = 1;
+
+  while(1)
   {
-		/*
-		 * This is NOT a performant event loop!
-		 * It needs help!
-		 */
+    if (XPending(xinfo.display) > 0)
+    {
+      XNextEvent(xinfo.display, &event);
+
+      switch(event.type)
+      {
+        case KeyPress:
+          handleKeyPress(xinfo, event);
+          break;
+        case EnterNotify:
+          inside = 1;
+          break;
+        case LeaveNotify:
+          inside = 0;
+          break;
+      }
+    }
+
+    unsigned long end = now();
 
-		if (XPending(xinfo.display) > 0)
+    if (end - lastRepaint > 1000000 / FPS)
     {
-			XNextEvent(xinfo.display, &event);
-			cout << "event.type=" << event.type << "\n";
+      Pixmap pixmap; // represents thing in buffer
 
-			switch( event.type )
+      if (usebuffer)
       {
-				case KeyPress:
-					handleKeyPress(xinfo, event);
-					break;
-				case EnterNotify:
-					inside = 1;
-					break;
-				case LeaveNotify:
-					inside = 0;
-					break;
-			}
-		}
+        pixmap = buffer;
+        XSetForeground(xinfo.display, xinfo.gc[0], BlackPixel(xinfo.display, xinfo.screen));
+        XSetBackground(xinfo.display, xinfo.gc[0], WhitePixel(xinfo.display, xinfo.screen));
+        XFillRectangle(xinfo.display, pixmap, xinfo.gc[0], 0, 0, w.width, w.height);
+      }
+      else
+      {
+        pixmap = xinfo.window; // set this to the windowInfo
+        XClearWindow(xinfo.display, pixmap);
+      }
 
-		usleep(1000000/FPS);
-		handleAnimation(xinfo, inside);
-		repaint(xinfo);
-	}
+      //XSetForeground(xinfo.display, xinfo.gc[0], BlackPixel(xinfo.display, xinfo.screen));
+      //XSetBackground(xinfo.display, xinfo.gc[0], WhitePixel(xinfo.display, xinfo.screen));
+      handleAnimation(xinfo, inside);
+      if (usebuffer)
+      {
+        XCopyArea(xinfo.display, pixmap, xinfo.window, xinfo.gc[0], 0, 0 , w.width, w.height, 0, 0);
+      }
+
+      XFlush(xinfo.display);
+      lastRepaint = now();
+    }
+
+    if (XPending(xinfo.display) == 0)
+    {
+      usleep(1000000 / FPS - (end - lastRepaint));
+    }
+  }
 }