0
\$\begingroup\$

I would like to have an SDL_Surface representing the contents of the window, just like in the old days with SDL1.2. What is the best and fastest way to do it in SDL2?

What I found is that I need an SDL_Window, an SDL_Renderer for that window, an SDL_Texture to render, and an SDL_Surface to create a texture from. This seems a bit too much to me, since I just want to display a single image on the screen.

Not to mention the impact on the performance. On my machine (Lenovo Y510p laptop) this whole procedure takes 9ms, without any memory allocation, only using pre-allocated variables and totally black SDL_Surface.

Is there a way I could speed up things?

\$\endgroup\$
1
\$\begingroup\$

Is there a way I could speed up things?

No, there isn't.

The slowest part of your system is the time it takes for the image to be displayed on the monitor, which presumably is in the vicinity of 24 milliseconds. (The monitor takes 16 milliseconds to draw the frame, plus 8 milliseconds because on average the monitor will be halfway through drawing a frame when your code finishes preparing the window and its image, so there will be an 8ms extra pause while the monitor finishes drawing the previous frame). In practice, the delay will actually be even higher than this due to other sources of latency; often monitors are working an extra frame or two behind their input sources for one reason or another.

The upshot of this is that even if you somehow manage to optimise things so that your code runs instantaneously -- 0 ms instead of 9 ms -- it still won't actually be any faster, since your code wasn't the slow part of the system; the monitor is the slow part. Anything you do to try to "optimise" the code will produce no faster output from the user's point of view, because your code's output is being limited by how quickly the monitor can display it, and the monitor is already substantially slower than your code is now.

So don't worry about trying to speed up your current code. Really. Obsessing over this small stuff is a total waste of your time.

\$\endgroup\$
  • \$\begingroup\$ Thanks for your extensive reply, but I've got a question. Why does the slowness of my monitor slow down my program? It should be independent, or not? Right now I have a loop, which clears then draws on the surface and renders it, and after the termination of the program I print how many frames it created under how many milliseconds. Theoretically, I should be able to produce 200 frames under one second on a 60Hz monitor, what does slow down my program? Transferring the image from RAM to GPU RAM? Sorry, if you don't understand, I'm not native English speaker. \$\endgroup\$ – Semmu Aug 23 '14 at 16:05
  • \$\begingroup\$ Okay, I think I found the "bottleneck". SDL_UpdateTexture is what slows down my latency, from 3ms to 10ms. Thanks for your explanation though! \$\endgroup\$ – Semmu Aug 23 '14 at 16:08
0
\$\begingroup\$

From what I know the fastest way to render something in SDL 2.0 is using textures. You can use SDL_BlitSurface so you don't need to use renderers and convert to textures but that is slower and it is also a bad habit to use it. I would say to stick with textures because it is probably the fastest way to do it. Plus that it is also not so much harder to use textures. It is basicly the same.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.