我正在寻找最快的方法来在SDL中将单个像素绘制到屏幕上。当前,我正在使用一个数组到SDL_Texture中,然后使用SDL_RenderCopy复制它。我怀疑这是最有效的方法,因为绘制960x540屏幕需要6毫秒。.我想尽可能地坚持使用软件,并且不想使用OpenGL或Vulkan。
这是我的代码:
#include <SDL2/SDL.h>
#include <stdbool.h>
#define RESX 960
#define RESY 540
#define CRESX RESY*0xFF
#define CRESY RESY*0xFF
#define N 10
void main() {
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
bool running = true;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
running = false;
}
window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, RESX, RESY, SDL_WINDOW_SHOWN );
if( window==NULL) {
printf( "SDL_Error: %s\n", SDL_GetError() );
running = false;
}
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
if(renderer==NULL) {
printf("Renderer error: %s\n", SDL_GetError() );
running = false;
}
SDL_Texture * texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, RESX,RESY);
Uint32 pixels[RESX*RESY];
while( running ) {
clock_t t1, t2;
t1 = SDL_GetTicks();
SDL_Event e;
running = !(SDL_PollEvent( &e ) && e.type == SDL_QUIT);
for( int n = 0; n < N; n++) {
for( int x = 0; x < RESX; x++) {
for( int y = 0 ; y < RESY; y++) {
pixels[y*RESX+x] = 0xFFFFFF;
}
}
SDL_UpdateTexture(texture, NULL, pixels, RESX * sizeof(Uint32));
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent( renderer );
}
t2 = SDL_GetTicks();
printf("%ldms to draw %dx%d screen %d times.\n", t2-t1, RESX, RESY, N );
}
SDL_DestroyWindow( window );
SDL_DestroyRenderer( renderer );
SDL_DestroyTexture( texture );
SDL_Quit();
}