Hello,
I try to use texture locking to draw single pixels and I get a strange flickering, as if there were 2 “buffers” linked to the window or the texture. In order to improve performance, I would like to only copy the modified pixel when updating the window. Is it possible ?
#include <SDL2/SDL.h>
#include
using namespace std;
int main( int argc, char **argv )
{
SDL_Init( SDL_INIT_EVERYTHING );
atexit( SDL_Quit );
const unsigned int width = 607;
const unsigned int height = 600;
SDL_Window *window = SDL_CreateWindow( “title”, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN );
SDL_Renderer *renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
SDL_RendererInfo info;
SDL_GetRendererInfo( renderer, &info );
cout << "Renderer name: " << info.name << endl;
cout << "Texture formats: " << endl;
for ( Uint32 i = 0; i < info.num_texture_formats; i++ )
cout << SDL_GetPixelFormatName( info.texture_formats[ i ] ) << endl;
// SDL_PIXELFORMAT_ARGB8888 is the first format listed by the above code
SDL_Texture *texture = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height );
// initial clear
unsigned char lockedPixels = nullptr;
int pitch;
SDL_LockTexture( texture, NULL, (void*)&lockedPixels, &pitch );
memset( lockedPixels, 0, height * pitch );
SDL_UnlockTexture( texture );
SDL_RenderCopy( renderer, texture, NULL, NULL );
SDL_RenderPresent( renderer );
// event loop
SDL_Event event;
bool running = true;
int x = 0, y = 0;
while ( running )
{
while ( SDL_PollEvent( &event ) )
if ( (SDL_QUIT == event.type) || (SDL_KEYDOWN == event.type && SDL_SCANCODE_ESCAPE == event.key.keysym.scancode) )
{
running = false;
break;
}
// move pixel
x = (x + 1) % width;
y = (y + 1) % height;
unsigned char *lockedPixels = nullptr;
int pitch;
SDL_LockTexture( texture, NULL, (void**)&lockedPixels, &pitch );
// draw pixel
int ind = y * pitch + x * 4;
lockedPixels[ ind ] = 255;
SDL_UnlockTexture( texture );
// update window
SDL_Rect rect = { x, y, 1, 1 };
SDL_RenderCopy( renderer, texture, &rect, &rect );
SDL_RenderPresent( renderer );
}
SDL_DestroyTexture( texture );
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
}