linux 图形编程rect,第一个QNX图形程序

理清了思路以后开始通过实例测试GF的使用过程,为了简化测试环境,这次使用的是QNX Momentics环境和QNX虚拟机环境,没有使用BeagleBoard作为运行目标,主要是怕直接在板子上运行会有其他因素影响测试。

在QNX momentics环境中创建了一个新的QNX C Project, 在main.c里先加上gf头文件的引用:

#include

然后在main方法里加上第一步操作,就是通过fg_dev_attach()方法连接到设备上去,代码如下:

if (gf_dev_attach(&gdev, GF_DEVICE_INDEX(0), &gdev_info) != GF_ERR_OK)          {

printf("gf_dev_attach() failed\n");

return (-1);

}

不管后面是否成功,先测试一下能否连接设备,很气馁地发现编译都不通过,方法gf_dev_attach的引用有问题。

不是已经将fg.h引入了吗?

在网上找了一轮,发现是链接配置有问题,需要在链接选项里加上gf库的引用,配置在项目属性的“QNX c/c++”一栏,在“Linker”页面的“Other Options”项中填写“-lgf”,如下图:

4549e73c52e61b97261f0f41b21ee644.gif

然后的测试就一路顺风顺水:

连接设备成功!

连接显示器成功!

连接显示层成功!

具体的代码在文章后面有,这里就不贴了。

到了显示表面的地方卡了,有点问题,首先是使用那个方法有点搞不清,看了文档才发现应该是调用

gf_surface_create_layer()方法创建显示表面,然后才调用gf_layer_set_surfaces()将显示表面和显示层连接起来。

然后就是一直无法创建显示表面,测试的时候总是报错。

最后从openqnx网站上找到答案,去年的贴子,和我的问题一模一样,贴子链接如下:

贴子中提到问题的原因是QNX虚拟机启动Photon界面后将资源占住了,我们的测试程序无法使用。

解决方法是从新启动QNX虚拟机,在登录界面选择“ShutDown”按钮,然后在关机选择中选择“Exit to text mode”如下图,就是退出到文本界面的意思:

c68aa09f0cfa66b9e29174a763e708fd.gif

然后要注意,退出到文本界面后login字样直接出现在登录背景图上,登录背景图没有清空,这时不要以为有什么问题,直接输入用户名,回车,密码,回车,就可以进入文本界面了。

进入文本界面后回到QNX Momentics上测试应用,果然可以解决问题,成功创建显示表面。

后面就真的是一路顺风了,最后很没追求地在QNX上显示了一个无聊的方框:

c276338ee4f6eda5f9c06953c62594be.gif

下面是完整代码,供参考:

#include

#include

#include

int main(int argc, char *argv[]) {

printf("Starting Graphics test\n");

gf_dev_t gdev;

gf_dev_info_t gdev_info;

gf_display_t display;

gf_display_info_t display_info;

gf_layer_t layer;

gf_surface_t surface;

gf_context_t context;

if (gf_dev_attach(&gdev, GF_DEVICE_INDEX(0), &gdev_info) != GF_ERR_OK) {

printf("gf_dev_attach() failed\n");

return (-1);

}

printf("device attached\n");

if (gf_display_attach(&display, gdev, 0, &display_info) != GF_ERR_OK) {

printf("gf_display_attach() failed\n");

}

printf("display attached\n");

if (display != NULL) {

unsigned main_layer_index = display_info.main_layer_index;

if (gf_layer_attach(&layer, display, main_layer_index,

GF_LAYER_ATTACH_PASSIVE) != GF_ERR_OK) {

printf("gf_layer_attach() failed\n");

return EXIT_SUCCESS;

}

} else {

printf("Display is null \n");

return EXIT_SUCCESS;

}

printf("Layer attached \n");

//gf_surface_t surface;

int width = display_info.xres;

int height = display_info.yres;

int create_surface_result = gf_surface_create_layer(&surface, &layer, 1, 0,

width, height, display_info.format, NULL, 0);

if (create_surface_result == GF_ERR_OK) {

gf_layer_set_surfaces(layer, &surface, 1);

printf("attach to the surface , Done\n");

} else {

printf("attach to the surface , failed. Error: %d\n",

create_surface_result);

return EXIT_SUCCESS;

}

if (gf_context_create(&context) != GF_ERR_OK) {

fprintf(stderr, "gf_context_create failed\n");

}

if (gf_context_set_surface(context, surface) != GF_ERR_OK) {

fprintf(stderr, "gf_context_set_surface failed\n");

}

gf_draw_begin(context);

gf_context_set_fgcolor(context, 0xFFFFFF);

gf_draw_rect(context, 10, 10, 50, 50);

gf_context_set_fgcolor(context, 0x000000);

gf_draw_rect(context, 20, 20, 40, 40);

gf_draw_flush(context);

gf_draw_end(context);

gf_dev_detach(gdev);

return EXIT_SUCCESS;

}0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值