Reputation: 610
I am trying to create a image pixel-per-pixel and display it on the screen with SDL. The image has to be refreshed and displayed again ~50 times per second (I am looking for a 50 FPS game). I tried to create a simple program to illustrate what I want to do: I create a 1280 * 720 window and texture which I alternately fill with green and red. The problem is that the code runs very slowly (~8 FPS). Where did I mess up?
Here's my code
SDL_Texture *display;
SDL_Window *window;
SDL_Renderer *renderer;
int x;
int y;
int a = 255;
int b = 0;
window = SDL_CreateWindow(gl_title, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, gl_width, gl_height, 0);
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_TARGETTEXTURE |
SDL_RENDERER_PRESENTVSYNC);
display = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET, gl_width, gl_height);
while (true)
{
SDL_SetRenderTarget(renderer, display);
SDL_SetRenderDrawColor(renderer, a, b, 0, 255);
for (x = 0; x < gl_width; ++x)
{
for (y = 0; y < gl_height; ++y)
{
SDL_RenderDrawPoint(renderer, x, y);
}
}
a = a == 255 ? 0 : 255;
b = b == 255 ? 0 : 255;
SDL_SetRenderTarget(renderer, NULL);
SDL_RenderCopy(renderer, display, NULL, NULL);
SDL_RenderPresent(renderer);
}
Upvotes: 1
Views: 1562
Reputation: 25752
Drawing on a screen using a function for every pixel is going to be slow. You can manually write the pixels in a loop and avoid the cost of calling the function every time or use one of the SDL provided functions that do that for you:
SDL_RenderFillRect
SDL_RenderDrawLines
SDL_RenderDrawPoints
SDL_RenderDrawRect
Upvotes: 2