'How to Upscale window or renderer in sdl2 for pixelated look?
I want this pixelated look in sdl2 for all the object in my screen
Solution 1:[1]
To do this, the nearest
scaling must be set (default in SDL2), which does not use antialiasing. If so, you can use SDL_SetHint
by setting hint SDL_HINT_RENDER_SCALE_QUALITY
to nearest
(or 0
). If you now render a small texture in a large enough area (much larger than the texture size), you will see large pixels in the window.
If, on the other hand, you have large textures (just like in the linked thread), or you just want to render the entire frame pixelated, you can do this by rendering the contents of the frame on a low-resolution auxiliary texture (serving as the back buffer), and after rendering the entire frame, rendering the back buffer in the window. The buffer texture will be stretched across the entire window and the pixelation will then be visible.
I used this method for the Fairtris game which renders the image in NES-like resolution. Internal back buffer texture has resolution of 256×240 pixels and is rendered in a window of any size, maintaining the required proportions (4:3
, so slightly stretched horizontally). However, in this game I used linear scaling to make the image smoother.
To do this you need to:
- remember that the
nearest
scaling must be set, - create a renderer with the
SDL_RENDERER_TARGETTEXTURE
flag, - create back buffer texture with low resolution (e.g. 256×240) and with
SDL_TEXTUREACCESS_TARGET
flag.
When rendering a frame, you need to:
- set the renderer target to the backbuffer texture with
SDL_SetRenderTarget
, - render everything the frame should contain using the renderer and back buffer size (e.g. 256×240),
- bring the renderer target back to the window using
SDL_SetRenderTarget
again.
You can resize the back buffer texture at any time if you want a smaller area (zoom in effect, so larger pixels on the screen) or a larger area (zoom out effect, so smaller pixels on the screen) in the frame. To do this, you will most likely have to destroy and recreate the backbuffer texture with a different size. Or you can create a big backbuffer texture with an extra margin and when rendering, use a smaller or bigger area of it — this will avoid redundant memory operations.
At this point, you have the entire frame in an auxiliary texture that you can render in the window. To render it in a window, use the SDL_RenderCopy
function, specifying the renderer handle and back buffer texture handle (rects should not be given so that the texture will be rendered completely over the entire window area), and finally SDL_RenderPresent
.
If you need to render in window the frame respecting the aspect ratio, get the current window size with SDL_GetWindowSize
and calculate the target area taking into account the aspect ratio of the back buffer texture and the window proportions (portrait and landscape). However, before rendering the back buffer texture in the window, first clean the window with SDL_RenderClear
so that the remaining areas of the window (black bars) are filled with black.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |