微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

开罗C程序不会画到x11窗口

我试图在C上使用Linux上的Cairographics库来制作一个非常轻量级的x11 GUI。

经过艰苦的努力去追踪开罗给x11的悲惨的不完整的指南 ,这是我得到的最好的:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <cairo.h> #include <cairo-xlib.h> #include <X11/Xlib.h> #include <X11/extensions/Xrender.h> #include <X11/extensions/renderproto.h> //This function should give us a new x11 surface to draw on. cairo_surface_t* create_x11_surface(int x,int y) { display* d; Drawable da; int screen; cairo_surface_t* sfc; if((d = XOpendisplay(NULL)) == NULL) { printf("Failed to open displayn"); exit(1); } screen = DefaultScreen(d); da = XCreateSimpleWindow(d,DefaultRootwindow(d),x,y,0); XSelectInput(d,da,ButtonPressMask | KeyPressMask); XMapWindow(d,da); sfc = cairo_xlib_surface_create(d,DefaultVisual(d,screen),y); cairo_xlib_surface_set_size(sfc,y); return sfc; } int main(int argc,char** argv) { //create a new cairo surface in an x11 window as well as a cairo_t* to draw //on the x11 window with. cairo_surface_t* surface = create_x11_surface(300,200); cairo_t* cr = cairo_create(surface); while(1) { //save the empty drawing for the next time through the loop. cairo_push_group(cr); //draw some text cairo_select_font_face(cr,"serif",CAIRO_FONT_SLANT_norMAL,CAIRO_FONT_WEIGHT_BOLD); cairo_set_font_size(cr,32.0); cairo_set_source_rgb(cr,1.0); cairo_move_to(cr,10.0,25.0); if((argc == 2) && (strnlen(argv[1],100) < 50)) cairo_show_text(cr,argv[1]); else cairo_show_text(cr,"usage: ./p1 <string>"); //put the drawn text onto the screen(?) cairo_pop_group_to_source(cr); cairo_paint(cr); cairo_surface_flush(surface); //pause for a little bit. int c = getchar(); //change the text around so we can see the screen update. for(int i = 0; i < strnlen(argv[1],100); i++) { argv[1][i] = argv[1][i + 1]; } if(c == 'q') { break; } } cairo_surface_destroy(surface); return 0; }

在安装了Cairo的Linux系统上,可以使用

gcc -o myprog $(pkg-config --cflags --libs cairo x11) -std=gnu99 main.c

它应该运行一个单一的参数。

赢7 / Python 2.7 32位:OSError:无法加载库pangocairo 1.0:错误0x7e

用PyCairo直接绘制到根窗口

如何在ubuntu中运行和编译cairo文件

如何在Windows上安装节点jscanvas

在Windows 8机器上安装Cairo和Canvas

因为我不明白的理由,插入这行

cairo_pop_group_to_source(cr); cairo_paint(cr); cairo_surface_write_to_png (surface,"hello.png"); //<--------- inserted cairo_surface_flush(surface);

把一些东西放在屏幕上,但有两个问题:

我用这种方法绘制的文本是持久的,产生拖尾效果

我不想在我的程序和x11窗口之间调解一些.png文件。 数据应该直接发送!

如何在linux系统上获得真实types字符的字形轮廓

在x64 CentOS上构build开罗时遇到问题

禁用WebKit中的滚动条(平面框架模式)

在gtk_window中清除cairo文本

如何加快在Opengl窗口与开罗绘图?

几个问题:

在X11中,X11服务器不会保存你画到窗口的内容,而是将一个ExposeEvent发送到你的窗口,告诉它重画。 这意味着你得到一个黑色的窗口,因为你不处理这个事件。

getchar只会在换行后给你一些东西,所以只要输入一些东西就没有帮助。

libX11缓冲区的东西,只有当你等待一个事件(或缓冲区填满)发送到X11服务器。 既然你永远不等待一个事件,它永远不会冲。 调用XFlush显然有帮助。

你推的小组是没用的。 只是摆脱它。

您的代码将字符串向一个方向移动到左侧很容易超出字符串的末尾。 你显然已经知道这一点,因为你用一个strnlen “固定”了这个。

这里有一个更好的解决方案,但它仍然给你一个最初的黑色的窗口,因为你绘制之前,它被映射:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <cairo-xlib.h> #include <X11/Xlib.h> //This function should give us a new x11 surface to draw on. cairo_surface_t* create_x11_surface(display *d,int x,int y) { Drawable da; int screen; cairo_surface_t* sfc; screen = DefaultScreen(d); da = XCreateSimpleWindow(d,char** argv) { display *d = XOpendisplay(NULL); if (d == NULL) { fprintf(stderr,"Failed to open displayn"); return 1; } //create a new cairo surface in an x11 window as well as a cairo_t* to draw //on the x11 window with. cairo_surface_t* surface = create_x11_surface(d,300,200); cairo_t* cr = cairo_create(surface); char *text = argv[1]; size_t text_len = 0; if (argc != 2) text = NULL; else text_len = strlen(text); while(1) { // Clear the background cairo_set_source_rgb(cr,0); cairo_paint(cr); //draw some text cairo_select_font_face(cr,25.0); if (text) cairo_show_text(cr,text); else cairo_show_text(cr,"usage: ./p1 <string>"); cairo_surface_flush(surface); XFlush(d); //pause for a little bit. int c = getchar(); //change the text around so we can see the screen update. memmove(text,&text[1],text_len); if (text_len > 0) text_len--; printf("got char %cn",c); if(c == 'q') { break; } } // XXX: Lots of other stuff isn't properly destroyed here cairo_surface_destroy(surface); return 0; }

编辑:另外,为什么你觉得像开罗只给你一个可悲的不完整的指导? 它告诉你如何使开罗部分工作,它也解释了一些关于X11的部分,即使你已经知道如果你想使用cairo-x11。 这不关它的事。 你甚至提供了一个完整的, 工作和独立的例子: https : //www.cypherpunk.at/files/2014/11/cairo_xlib_simple.c

我已经阅读了这本“不完整的指南”的完整文本,你会看到有一个链接到完整的示例: https ://www.cypherpunk.at/files/2014/11/cairo_xlib_simple.c。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐