我写了三个demo分别是通过像素点渲染线条 矩形框 文字 ,线条的渲染是正常的 ,矩形框和文字渲染会报错,大神们帮忙看看
初学者,如果有什么不好的地方,清帮忙指正
案例一:线条渲染
int draw_line(const char *group, const char *id)
{
int j;
printf("start draw line\n");
screen_context_t screen_line_ctx;
screen_create_context(&screen_line_ctx, SCREEN_APPLICATION_CONTEXT);
//screen_window_t screen_line_win;
screen_create_window_type(&screen_line_win, screen_line_ctx, SCREEN_CHILD_WINDOW);
screen_join_window_group(screen_line_win, group);
screen_set_window_property_cv(screen_line_win, SCREEN_PROPERTY_ID_STRING, strlen(id), id);
int flag = 1;
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_STATIC, &flag);
int vis = 1;
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_VISIBLE, &vis);
int format = SCREEN_FORMAT_RGBA8888;
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_FORMAT, &format);
int usage = SCREEN_USAGE_WRITE;
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_USAGE, &usage);
int transparency = SCREEN_TRANSPARENCY_SOURCE_OVER;
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_TRANSPARENCY, &transparency);
/* Set the window buffer size for the hourglass. */
int rect[4] = { 0, 100, 1920, 720};
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_BUFFER_SIZE, rect+2);
/* Create the window buffer and then get a handle to this buffer. */
screen_buffer_t screen_buf;
screen_create_window_buffers(screen_line_win, 1);
screen_get_window_property_pv(screen_line_win, SCREEN_PROPERTY_RENDER_BUFFERS, (void **)&screen_buf);
//获取指向该缓冲区的指针,以填充形状。
char *ptr = NULL;
screen_get_buffer_property_pv(screen_buf, SCREEN_PROPERTY_POINTER, (void **)&ptr);
//不同行像素之间的字节数
int stride = 0;
screen_get_buffer_property_iv(screen_buf, SCREEN_PROPERTY_STRIDE, &stride);
//画线 for (i = 0; i < rect[3]; i++, ptr += stride) {
ptr += stride*rect[1];
for (j = 0; j < rect[2]; j++) {
ptr[j*4] = 0xa0;
ptr[j*4+1] = 0xa0;
ptr[j*4+2] = 0xa0;
ptr[j*4+3] = 0xff;
}
/* Post the window. */
screen_post_window(screen_line_win, screen_buf, 1, rect, 0);
return 0;
}
void demo_line(){
int pos[2], size[2];
int vis = 0;
int type;
screen_context_t screen_ctx;
screen_create_context(&screen_ctx, SCREEN_APPLICATION_CONTEXT);
/* 在创建子窗口时指定尺寸. */
/* Get all displays available for the context. */
//获取上下文可用的所有显示
int count = 0;
screen_get_context_property_iv(screen_ctx, SCREEN_PROPERTY_DISPLAY_COUNT, &count);
printf("start demo3 count=%d \n",count);
screen_display_t *screen_disps = (screen_display_t *)calloc(count, sizeof(screen_display_t));
screen_get_context_property_pv(screen_ctx, SCREEN_PROPERTY_DISPLAYS, (void **)screen_disps);
screen_display_t screen_disp = screen_disps[1];
//free(screen_disps);
/* 获取显示器的大小;我们将使用这个作为我们的Windows的尺寸. */
//
int dims[2] = { 0, 0 };
screen_get_display_property_iv(screen_disp, SCREEN_PROPERTY_SIZE, dims);
printf("start screen size w=%d h=%d \n",dims[0],dims[1]);
/* 为窗口组构造一个唯一的名称. */
char str[16];
snprintf(str, sizeof(str), "%d test3", getpid());
/* Create the parent window; in this example the background window is the parent. */
screen_bg_win = create_bag_window(str, dims, screen_ctx);
screen_set_window_property_pv(screen_bg_win,SCREEN_PROPERTY_DISPLAY,(void**)&screen_disps[1]);
/* Create the child windows. */
draw_line(str, "line");
// screen_set_window_property_pv(screen_bg_win,SCREEN_PROPERTY_DISPLAY,(void**)&screen_disps[1]);
//screen_flush_context(screen_ctx, SCREEN_WAIT_IDLE);
vis = 1;
screen_get_window_property_iv(screen_line_win, SCREEN_PROPERTY_BUFFER_SIZE, size);
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_SIZE, size);
pos[0] = pos[1] = 10;
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_POSITION, pos);
size[0] = dims[0];
size[1] = dims[1];
screen_set_window_property_iv(screen_bg_win, SCREEN_PROPERTY_SIZE, size);
int zorder = 7;
screen_set_window_property_iv(screen_bg_win, SCREEN_PROPERTY_ZORDER, &zorder);
zorder++;
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_ZORDER, &zorder);
/* Set all windows visible. */
screen_set_window_property_iv(screen_bg_win, SCREEN_PROPERTY_VISIBLE, &vis);
screen_set_window_property_iv(screen_line_win, SCREEN_PROPERTY_VISIBLE, &vis);
screen_flush_context(screen_ctx, SCREEN_WAIT_IDLE);
sleep(8);
screen_destroy_context(screen_ctx);
}
案例二:矩形框渲染
int draw_rectange(const char *group, const char *id)
{
int i, j;
printf("start draw_rectange\n");
screen_context_t screen_rect_ctx;
screen_create_context(&screen_rect_ctx, SCREEN_APPLICATION_CONTEXT);
printf("draw_rectange 1 \n");
screen_create_window_type(&screen_rect_win, screen_rect_ctx, SCREEN_CHILD_WINDOW);
screen_join_window_group(screen_rect_win, group);
screen_set_window_property_cv(screen_rect_win, SCREEN_PROPERTY_ID_STRING, strlen(id), id);
int flag = 1;
int fill_flag = 1;
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_STATIC, &flag);
int vis = 1;
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_VISIBLE, &vis);
int format = SCREEN_FORMAT_RGBA8888;
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_FORMAT, &format);
int usage = SCREEN_USAGE_WRITE;
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_USAGE, &usage);
int transparency = SCREEN_TRANSPARENCY_SOURCE_OVER;
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_TRANSPARENCY, &transparency);
printf("draw_rectange 2 \n");
int rect[4] = { 100, 100, 960, 360};
printf("draw_rectange 2-1 \n");
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_BUFFER_SIZE, rect+2);
screen_buffer_t screen_buf;
screen_create_window_buffers(screen_rect_win, 1);
screen_get_window_property_pv(screen_rect_win, SCREEN_PROPERTY_RENDER_BUFFERS, (void **)&screen_buf);
//获取指向该缓冲区的指针,以填充形状。
char *ptr = NULL;
screen_get_buffer_property_pv(screen_buf, SCREEN_PROPERTY_POINTER, (void **)&ptr);
//不同行像素之间的字节数
int stride = 0;
screen_get_buffer_property_iv(screen_buf, SCREEN_PROPERTY_STRIDE, &stride);
printf("draw_rectange 3 \n");
//画框
for (i = 0; i <=rect[3]; i++, ptr += stride){
printf("draw_rectange i=%d \n",i);
for (j = 0; j <=rect[2]; j++){
printf("draw_rectange j=%d \n",j);
if(fill_flag>0){//填充框
ptr[j*4] = 0xa0;
ptr[j*4+1] = 0xa0;
ptr[j*4+2] = 0xa0;
ptr[j*4+3] = 0xff;
}
else{
if (i == 0 || i == rect[2] || j == 0 || j == rect[3]){
ptr[j*4] = 0xa0;
ptr[j*4+1] = 0xa0;
ptr[j*4+2] = 0xa0;
ptr[j*4+3] = 0xff;
}
}
}
}
screen_post_window(screen_rect_win, screen_buf, 1, rect, 0);
return 0;
}
void demo_rectange(){
int pos[2], size[2];
int vis = 0;
int type;
screen_context_t screen_ctx;
screen_create_context(&screen_ctx, SCREEN_APPLICATION_CONTEXT);
/* 在创建子窗口时指定尺寸. */
/* Get all displays available for the context. */
//获取上下文可用的所有显示
int count = 0;
screen_get_context_property_iv(screen_ctx, SCREEN_PROPERTY_DISPLAY_COUNT, &count);
printf("start demo count=%d \n",count);
screen_display_t *screen_disps = (screen_display_t *)calloc(count, sizeof(screen_display_t));
screen_get_context_property_pv(screen_ctx, SCREEN_PROPERTY_DISPLAYS, (void **)screen_disps);
screen_display_t screen_disp = screen_disps[1];
//free(screen_disps);
/* 获取显示器的大小;我们将使用这个作为我们的Windows的尺寸. */
//
int dims[2] = { 0, 0 };
screen_get_display_property_iv(screen_disp, SCREEN_PROPERTY_SIZE, dims);
printf("start screen size w=%d h=%d \n",dims[0],dims[1]);
/* 为窗口组构造一个唯一的名称. */
char str[16];
snprintf(str, sizeof(str), "%d test5", getpid());
screen_bg_win = create_bag_window(str, dims, screen_ctx);
screen_set_window_property_pv(screen_bg_win,SCREEN_PROPERTY_DISPLAY,(void**)&screen_disps[1]);
draw_rectange(str, "rectg");
printf("draw_rectange end \n");
// screen_set_window_property_pv(screen_bg_win,SCREEN_PROPERTY_DISPLAY,(void**)&screen_disps[1]);
//screen_flush_context(screen_ctx, SCREEN_WAIT_IDLE);
vis = 1;
//缓冲区大小
screen_get_window_property_iv(screen_rect_win, SCREEN_PROPERTY_BUFFER_SIZE, size);
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_SIZE, size);
pos[0] = pos[1] = 10;
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_POSITION, pos);
size[1] = dims[1];
size[0] = dims[0];
//设置窗口显示大小。窗口显示可以根据应用的显示区域需求进行缩放,默认是全屏
screen_set_window_property_iv(screen_bg_win, SCREEN_PROPERTY_SIZE, size);
int zorder = 12;
screen_set_window_property_iv(screen_bg_win, SCREEN_PROPERTY_ZORDER, &zorder);
zorder++;
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_ZORDER, &zorder);
screen_set_window_property_iv(screen_bg_win, SCREEN_PROPERTY_VISIBLE, &vis);
screen_set_window_property_iv(screen_rect_win, SCREEN_PROPERTY_VISIBLE, &vis);
screen_flush_context(screen_ctx, SCREEN_WAIT_IDLE);
sleep(8);
screen_destroy_context(screen_ctx);
}
案例三:英文字符渲染
int draw_char(const char *group, const char *id)
{
screen_context_t screen_char_ctx;
screen_create_context(&screen_char_ctx, SCREEN_APPLICATION_CONTEXT);
printf("start draw_char\n");
//screen_window_t screen_char_win;
screen_create_window_type(&screen_char_win, screen_char_ctx, SCREEN_CHILD_WINDOW);
screen_join_window_group(screen_char_win, group);
screen_set_window_property_cv(screen_char_win, SCREEN_PROPERTY_ID_STRING, strlen(id), id);
int flag = 1;
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_STATIC, &flag);
int vis = 1;
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_VISIBLE, &vis);
int format = SCREEN_FORMAT_RGBA8888;
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_FORMAT, &format);
int usage = SCREEN_USAGE_WRITE;
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_USAGE, &usage);
int transparency = SCREEN_TRANSPARENCY_SOURCE_OVER;
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_TRANSPARENCY, &transparency);
/* Set the window buffer size for the hourglass. */
int pos[2]={0,0};
int rect[4] = {0, 0, 100, 100};
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_BUFFER_SIZE, rect+2);
/* Create the window buffer and then get a handle to this buffer. */
screen_buffer_t screen_buf;
screen_create_window_buffers(screen_char_win, 1);
screen_get_window_property_pv(screen_char_win, SCREEN_PROPERTY_RENDER_BUFFERS, (void **)&screen_buf);
//获取指向该缓冲区的指针,以填充沙漏形状。
char *ptr = NULL;
screen_get_buffer_property_pv(screen_buf, SCREEN_PROPERTY_POINTER, (void **)&ptr);
//不同行像素之间的字节数
int stride = 0;
screen_get_buffer_property_iv(screen_buf, SCREEN_PROPERTY_STRIDE, &stride);
//字符 完成
unsigned int ch;
unsigned int cl;
char *str="wjb";
unsigned char *codes;
ch=(unsigned int )str[0];
cl=(unsigned int )str[1];
if(( ch >= 0xa1) && (ch < 0xf8) && (cl >= 0xa1) && (cl < 0xff)){
printf("error char\n");
return -1;
}
codes = __ASCII8X16__ + 16*ch;//英文编码组 太长了不贴了
int i = 0;
for (i = 0; i < 16; ++i,ptr += stride)
{
int k = 0;
for (k = 0; k < 8; ++k)
{
// --x;
if ((codes[i] >> k) & 0x1)
{
ptr[pos[0]*4] = 0xa0;
ptr[pos[0]*4+1] = 0xa0;
ptr[pos[0]*4+2] = 0xa0;
ptr[pos[0]*4+3] = 0xff;
}
}
pos[0] += 8;
// ++y;
}
/* Post the window. */
screen_post_window(screen_char_win, screen_buf, 1, rect, 0);
sleep(8);
return 0;
}
void demo_char(){
int pos[2], size[2];
int vis = 0;
int type;
screen_context_t screen_ctx;
screen_create_context(&screen_ctx, SCREEN_APPLICATION_CONTEXT);
/* 在创建子窗口时指定尺寸. */
/* Get all displays available for the context. */
//获取上下文可用的所有显示
int count = 0;
screen_get_context_property_iv(screen_ctx, SCREEN_PROPERTY_DISPLAY_COUNT, &count);
printf("start demo7 count=%d \n",count);
screen_display_t *screen_disps = (screen_display_t *)calloc(count, sizeof(screen_display_t));
screen_get_context_property_pv(screen_ctx, SCREEN_PROPERTY_DISPLAYS, (void **)screen_disps);
screen_display_t screen_disp = screen_disps[1];
//free(screen_disps);
/* 获取显示器的大小;我们将使用这个作为我们的Windows的尺寸. */
//
int dims[2] = { 0, 0 };
screen_get_display_property_iv(screen_disp, SCREEN_PROPERTY_SIZE, dims);
printf("start screen size w=%d h=%d \n",dims[0],dims[1]);
/* 为窗口组构造一个唯一的名称. */
char str[16];
snprintf(str, sizeof(str), "%d test7", getpid());
/* Create the parent window; in this example the background window is the parent. */
screen_bg_win = create_bag_window(str, dims, screen_ctx);
screen_set_window_property_pv(screen_bg_win,SCREEN_PROPERTY_DISPLAY,(void**)&screen_disps[1]);
/* Create the child windows. */
draw_char(str, "rectg");
printf("draw_char end \n");
vis = 1;
//缓冲区大小
screen_get_window_property_iv(screen_char_win, SCREEN_PROPERTY_BUFFER_SIZE, size);
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_SIZE, size);
pos[0] = pos[1] = 10;
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_POSITION, pos);
size[1] = dims[1];
size[0] = dims[0];
//设置窗口显示大小。窗口显示可以根据应用的显示区域需求进行缩放,默认是全屏
screen_set_window_property_iv(screen_bg_win, SCREEN_PROPERTY_SIZE, size);
int zorder = 14;
screen_set_window_property_iv(screen_bg_win, SCREEN_PROPERTY_ZORDER, &zorder);
zorder++;
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_ZORDER, &zorder);
screen_set_window_property_iv(screen_bg_win, SCREEN_PROPERTY_VISIBLE, &vis);
screen_set_window_property_iv(screen_char_win, SCREEN_PROPERTY_VISIBLE, &vis);
screen_flush_context(screen_ctx, SCREEN_WAIT_IDLE);
sleep(8);
screen_destroy_context(screen_ctx);
}