本文章将介绍如何在黑莓平台中将图片载入并显示在平板或者是模拟器的屏幕上,以及二维图形的绘制;
第一步: 程序的初始化:
用screen_create_context(&screent_cxt, 0);函数对整个屏幕进行初始化;用bps_initialize();开启bps库;
第二步:载入图片函数的声明
用该函数 int bbutil_load_texture(const char* filename, int*width, int* height, float* tex_x, float *tex_y, unsigned int*tex);进行图片的载入,该函数中的参数width,height代表图片的大小,tex_x,tex_y代表图片的位置,filename代表图片的文件路径以及文件名,这个函数将在下面的步骤继续遇到
第三步:载入图片
//对egl进行初始化 if(EXIT_SUCCESS != bbutil_init_egl(screen_cxt)) { fprintf(stderr,"bbutil_init_egl failed\n"); bbutil_terminate(); screen_destroy_context(screen_cxt); return 0; } //用载入图片函数进行图片载入 if (EXIT_SUCCESS != bbutil_load_texture("app/native/BackGround.png", NULL, NULL, &tex_x,&tex_y, &BackGround_graphic)) { fprintf(stderr,"Unableto load BackGround.png texture\n");
return !EXIT_SUCCESS; } //设置图片的顶点参数 //第一个顶点BackGround_graphic_vertices[0] = 0.0f; BackGround_graphic_vertices[1] = 0.0f; //第二个顶点 BackGround_graphic_vertices[2] = size_x; BackGround_graphic_vertices[3] = 0.0f; //第三个顶点 BackGround_graphic_vertices[4] = 0.0f; BackGround_graphic_vertices[5] = size_y; //第四个顶点 BackGround_graphic_vertices[6] = size_x; BackGround_graphic_vertices[7] = size_y;
//决定图片的绘制方式(如旋转, 90度,倒置) BackGround_graphic_text_coord[0] = 0.0f; BackGround_graphic_text_coord[1] = 0.0f;
BackGround_graphic_text_coord[2] = tex_x; BackGround_graphic_text_coord[3] = 0.0f;
BackGround_graphic_text_coord[4] = 0.0f; BackGround_graphic_text_coord[5] = tex_y;
BackGround_graphic_text_coord[6] = tex_x; BackGround_graphic_text_coord[7] =tex_y; |
第四步:开启opengl支持2D的绘图函数,以及功能函数,这些函数可以在黑莓平台的帮助手册中可以查到,在函数enable_2d();中
void enable_2d(){ glViewport(0, 0,(int)width, (int)height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrthof(0.0f,width / height, 0.0f, 1.0f, -1.0f, 1.0f) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glScalef(1.0f/ height, 1.0f / height, 1.0f); glColor4f(1.0f,0.78f, 0, 1.0f); //支持2D图形 glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); |
第五步:用顶点数组法绘图
定义一个画图函数draw_vertices_array();
void draw_vertices_array() { glVertexPointer(2,GL_FLOAT, 0, BackGround_graphic_vertices); glTexCoordPointer(2,GL_FLOAT, 0, BackGround_graphic_text_coord); glBindTexture(GL_TEXTURE_2D,BackGround_graphic); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } |
结尾的时候要关闭第一步程序初始化时开启的东西比如:bps,screen_create_context();
以这样的函数进行关闭:
bps_shutdown(); screen_destroy_context(screen_cxt); return 0; |
ps:因为本人能力,经验有限,如有错误,欢迎指出,我将加以改正,欢迎提出相关建议,谢谢!