注意到starfive官方给出了libSDL2-dev的软件包,使用SDL作为中间件可以跨平台适配许多的界面库,使用昉·星光2使用SDL2来绘制一个窗口
之前根据官方文档中的说明,将 libsdl2-dev 软件包安装好
然后编写代码,创建一个窗口并在窗口上显示一个BMP位图,位图和代码如下
#include <SDL.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char *argv[] ) {
int x = 720, y = 256, f = 60;
for ( int i = 1; i < argc; i++ ) {
if ( strcmp( argv[ i ], "-x" ) == 0 && argc > i + 1 ) {
x = (int) strtol( argv[ ++i ], NULL, 10 );
} else if ( strcmp( argv[ i ], "-y" ) == 0 && argc > i + 1 ) {
y = (int) strtol( argv[ ++i ], NULL, 10 );
} else if ( strcmp( argv[ i ], "-f" ) == 0 && argc > i + 1 ) {
f = (int) strtol( argv[ ++i ], NULL, 10 );
}
}
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_EVENTS );
SDL_Window *window = SDL_CreateWindow( "Hello World",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
x, y, SDL_WINDOW_SHOWN );
SDL_Surface *surface = SDL_LoadBMP( "logo.bmp" );
SDL_Renderer *renderer =
SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
SDL_Texture *texture = SDL_CreateTextureFromSurface( renderer, surface );
SDL_FreeSurface( surface );
surface = NULL;
SDL_bool quit = SDL_FALSE;
SDL_Event e = { 0 };
while ( !quit ) {
Uint32 start = SDL_GetTicks();
while ( SDL_PollEvent( &e ) )
switch ( e.type ) {
case SDL_QUIT: quit = SDL_TRUE; break;
default: break;
}
SDL_RenderClear( renderer );
SDL_RenderCopy( renderer, texture, NULL, NULL );
SDL_RenderPresent( renderer );
Uint32 end = SDL_GetTicks();
int wait_for = 1000 / f - ( end - start );
if ( wait_for > 0 ) {
SDL_Delay( wait_for );
}
}
SDL_DestroyTexture( texture );
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
保存以上代码为 hello.c 文件,使用 g++ 来编译
由于用到了 SDL2 库,所以需要链接到 SDL2,并且包含 SDL2 的 include 目录,编译成可执行文件
g++ hello.c -o hello -lSDL2 -I/usr/local/include/SDL2
将前文的图片保存在相同的文件夹,文件名为 logo.bmp,然后运行这个程序
./hello
|