我正在尝试在SDL2中为嵌入式项目创建动态绘图仪。当前,代码可以在x86和ARM两种体系结构上执行。在x86上,我得到了一个平稳运行的绘图仪,但是,在ARM上,它的运行速度非常慢,仅占我在x86上获得的帧的一小部分。我非常确定这是因为我重新渲染了表面上的每个像素,因为这会导致嵌入式设备过热。
我尝试将新内容渲染为纹理,将其复制到渲染器,然后进行渲染,但这根本不起作用。
由于双重缓冲,我必须清除每一帧。否则,我将“丢弃”更改。但是我还需要渲染旧的数据点,并仅在绘图仪再次到达它们时才覆盖它们。
SDL2中是否有一种方法可以将这些数据点保存到某种画布上,而仅添加(重画)新添加的画布?
这里是我的源代码:
Plotter.cpp
#include <SDL2/SDL.h>
#include <stdio.h>
#include "PlotterHelper.h"
/*Implementation*/
int main(int arhc, char * argv[])
{
//Top and bottom viewport
SDL_Rect topViewport;
SDL_Rect bottomViewport;
topViewport = CreateViewPort(0,0, SCREEN_WIDTH, (SCREEN_HEIGHT/2));
bottomViewport = CreateViewPort(0,(SCREEN_HEIGHT/2), SCREEN_WIDTH, SCREEN_HEIGHT/2);
float timeFrame = 4.0;
int updateWidth = round(SCREEN_WIDTH/(60.0 * timeFrame));
uint8_t backgroundColor = 0xff;
int delayTime = 0;
int sinusScale = 0;
int rectWidth = RECT_WIDTH;
bool rectEnd = false;
SDL_Point points[SCREEN_WIDTH] = {0,0};
int pointPosition = 0;
if (!init())
{
printf("Init failed!\n");
}
else
{
SDL_ShowCursor(SDL_DISABLE);
//Main lopp flag
bool quit = false;
//Event handler
SDL_Event event;
//While application is running
while(!quit)
{
//Handle events on queue
while (SDL_PollEvent( &event) != 0)
{
//User requests quit
if(event.type == SDL_QUIT)
{
quit = true;
}
else if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_w:
delayTime += 50;
if(delayTime > 5000)
delayTime = 5000;
break;
case SDLK_s:
delayTime -= 50;
if(delayTime < 0)
delayTime = 0;
break;
case SDLK_d:
timeFrame -= 1;
if(timeFrame < 1)
timeFrame = 1.0;
updateWidth = round(SCREEN_WIDTH/(60.0 * timeFrame));
printf("timeFrame = %lf\n", timeFrame);
break;
case SDLK_a:
timeFrame += 1;
if(timeFrame > 44)
timeFrame = 44.0;
updateWidth = round(SCREEN_WIDTH/(60.0 * timeFrame));
printf("timeFrame = %lf\n", timeFrame);
break;
case SDLK_r:
if(backgroundColor == 0x3f)
break;
else
{
++backgroundColor;
break;
}
case SDLK_f:
if(backgroundColor == 0x00)
break;
else
{
--backgroundColor;
break;
}
}
}
}
//Reset Plotter when the end of the window was reached
if(pointPosition > SCREEN_WIDTH-1)
{
pointPosition = 0;
sinusScale = (rand() % 100 + 1) - 50;
rectWidth = RECT_WIDTH;
rectEnd = false;
}
//Handler eraser when he reaches end of window
if(((SCREEN_WIDTH-1) - pointPosition) < RECT_WIDTH)
{
rectWidth = (SCREEN_WIDTH -1) - pointPosition;
rectEnd = true;
}
//Clear screen
SDL_SetRenderDrawColor(gRenderer, backgroundColor, backgroundColor, backgroundColor, 0xff);
SDL_RenderClear(gRenderer);
//Draw top viewport
SDL_RenderSetViewport( gRenderer, &topViewport );
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x66, 0x00, 0xff);
for(int iterator = 0; iterator <= SCREEN_WIDTH -1; ++iterator)
{
SDL_RenderDrawLine(
gRenderer,
points[iterator].x,
0,
points[iterator].x,
SCREEN_HEIGHT/2);
}
PlotEraser(rectEnd, backgroundColor, rectWidth,points[pointPosition].x );
//raw bottom viewport
SDL_RenderSetViewport( gRenderer, &bottomViewport );
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x66, 0x00, 0xff);
for(int iterator = 0; iterator <= SCREEN_WIDTH -1; ++iterator)
{
SDL_RenderDrawLine(
gRenderer,
points[iterator].x,
SCREEN_HEIGHT/2,
points[iterator].x,
points[iterator].y);
}
PlotEraser(rectEnd, backgroundColor, rectWidth,points[pointPosition].x );
for(int iterator = pointPosition; iterator <= pointPosition + updateWidth; ++iterator)
{
points[iterator].x = iterator;
points[iterator].y = round(((SCREEN_HEIGHT/4 )* sin(iterator/(100.0+sinusScale))) + SCREEN_HEIGHT/4);
}
pointPosition += updateWidth;
//Update Screen
SDL_RenderPresent(gRenderer);
SDL_Delay(delayTime);
}
}
//Free resources and close SDL
close();
return 0;
}
/*End of File*/
PlotterHelper.cpp
/*Includes*/
#include "PlotterHelper.h"
SDL_Window * gWindow = NULL;
SDL_Renderer * gRenderer = NULL;
SDL_Renderer * intermediate = NULL;
/*Implementation*/
/*********************************************************************
*********************************************************************
*********************************************************************
*/
bool init()
{
//Init flag
bool success = true;
//Init SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "Warning: Linear texture filtering not enabled!" );
}
//Set VSYNC
if( !SDL_SetHint( SDL_HINT_RENDER_VSYNC, "1" ) )
{
printf( "Warning: VSYNC not enabled!" );
}
//Create window
gWindow = SDL_CreateWindow(
"SDL Plotter",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN );
if (NULL == gWindow)
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED );
if(NULL == gRenderer)
{
printf("Renderer could not be created! SDLError: %s\n", SDL_GetError());
success = false;
}
else
{
//Initialise renderer colour
SDL_SetRenderDrawColor(gRenderer, 0xff, 0xff, 0xff, 0xff);
}
}
}
return success;
}
/*********************************************************************
*********************************************************************
*********************************************************************
*/
void close()
{
//Destroy window
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gRenderer = NULL;
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
/*********************************************************************
*********************************************************************
*********************************************************************
*/
SDL_Rect CreateViewPort(int x, int y, int w, int h)
{
SDL_Rect Viewport;
Viewport.x = x;
Viewport.y = y;
Viewport.w = w;
Viewport.h = h;
return Viewport;
}
/*********************************************************************
*********************************************************************
*********************************************************************
*/
void PlotEraser(bool rectEnd, uint8_t backgroundColor, int rectWidth, int x)
{
SDL_Rect fillRectBot = {x, 0, rectWidth, SCREEN_HEIGHT/2};
SDL_SetRenderDrawColor(gRenderer, backgroundColor, backgroundColor, backgroundColor, 0xff);
SDL_RenderFillRect(gRenderer, &fillRectBot);
if(rectEnd)
{
int startRecWidth = RECT_WIDTH - rectWidth;
SDL_Rect startRec = {0, 0, startRecWidth, SCREEN_HEIGHT/2};
SDL_RenderFillRect(gRenderer, &startRec);
}
}
/*End of File*/
PlotterHelper.h
#ifndef PLOTTER_HELPER_H_
#define PLOTTER_HELPER_H_
#include <SDL2/SDL.h>
#include <stdio.h>
#include <cmath>
#include <string>
/*Globals*/
//Screen constants
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
const int RECT_WIDTH = 10;
//The window we'll be rendering to
extern SDL_Window * gWindow;
//The window renderer
extern SDL_Renderer * gRenderer;
extern SDL_Renderer * intermediate;
/*Prototypes*/
/*********************************************************************
*********************************************************************
*********************************************************************
*/
//Starts up SDL and creates window
bool init();
/*********************************************************************
*********************************************************************
*********************************************************************
*/
//Free media and shut down SDL
void close();
/*********************************************************************
*********************************************************************
*********************************************************************
*/
//CreateVieport
SDL_Rect CreateViewPort(int x, int y, int w, int h);
/*********************************************************************
*********************************************************************
*********************************************************************
*/
//Plot Eraser
void PlotEraser(bool rectEnd, uint8_t backgroundColor, int rectWidth, int x);
#endif /* PLOTTER_HELPER_H_ */
/*End of File*/
答案 0 :(得分:3)
您可能有两种选择可以帮助您:
您应该能够使用“渲染目标纹理”来完成此操作。这就是您使用SDL_Texture
标志创建的SDL_CreateTexture
(请参见SDL_TEXTUREACCESS_TARGET
)。
您可以通过调用SDL_SetRenderTarget
并在渲染点之前传递该纹理来为该纹理绘制新点。
然后,您的主循环将需要调用SDL_SetRenderTarget
并传递nullptr
来将窗口还原为渲染目标。然后,每帧将纹理渲染到窗口。
SDL documentation上有一个小样,演示了如何使用渲染目标纹理。
如果您设置SDL_HINT_RENDER_BATCHING
(SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
),则最新版本的SDL 2支持渲染批处理。您应使用SDL_CreateWindowAndRenderer()
来确保已启用批处理。
如果您的平台不支持硬件加速,则可能无法执行任何操作。您可以将其与渲染目标一起使用。了解有关SDL渲染批处理here的信息。
这与渲染批处理是分开的,并且可以在任何版本的SDL 2上使用。
如果可以的话,将所有矩形/点收集到可以使用的数组中
SDL_RenderDrawPoints
或SDL_RenderDrawRects
让SDL一次完成所有操作,即使没有硬件加速,也应提高性能。可以将其与渲染目标结合使用。
如果您的设备缺乏硬件加速,则抛弃SDL_Renderer可能更快一些,而是使用SDL_GetWindowSurface
获取窗口的SDL_Surface
并使用SDL_BlitSurface
(或通过手动设置像素surface->pixels
)直接绘制到该位置,然后使用SDL_UpdateWindowSurface
更新窗口。另请参见SDL_UpdateWindowSurfaceRects
,了解如何仅更新您要更改的矩形以获得更好的性能。
您将需要familiarize yourself和SDL_Surface
一起使用,因为您需要检查窗口表面的像素格式,以便在您选择直接操作像素时正确更新它。