【发布时间】:2019-08-27 10:20:54
【问题描述】:
我希望开发一些使用 SDL2 在 7" RPi 触摸屏上显示图形的代码,但我宁愿不安装完整的桌面操作系统。我已经安装了 Raspbian Buster Lite。一些简单的测试代码得到当我尝试运行它时出错:
user@rpi4:~/01_hello_SDL $ ./hw
Window could not be created! SDL_Error: Could not initialize EGL
user@rpi4:~/01_hello_SDL $ sudo ./hw
error: XDG_RUNTIME_DIR not set in the environment.
Window could not be created! SDL_Error: Could not initialize EGL
我正在尝试创建窗口
SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL )
我找到了一个 post,它引用了有关如何在没有 X 的情况下构建 SDL2 的说明,但我希望有人能告诉我更多关于 SDL 如何在各种环境中找到显示的信息,以及是否有可能做我想做的事情想做。
几年前,我使用 SDL 1.2 在运行 Debian 版本的 Beaglebone Black 上制作全屏图形,但我似乎丢失了该安装,并且不记得它是如何设置的。我隐约记得 fbdev 的一些问题,它是非加速图形,但这在当时并不重要(虽然我现在想获得加速图形,但这并不重要)。
示例代码:
/*This source code copyrighted by Lazy Foo' Productions (2004-2019)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 480;
int main( int argc, char* args[] )
{
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL );
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
//Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
//Update the surface
SDL_UpdateWindowSurface( window );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}
【问题讨论】:
-
如果您只想在没有鼠标或菜单的情况下将视频/图形输出到屏幕,您可以使用
fb设备并在 1 毫秒内更新整个 1024x768 屏幕...在 Raspi 4 上,fb设备以 "DRM emulated" 的形式出现,没有做任何特别的事情。
标签: c++ linux raspbian sdl-2 framebuffer