Hi.
I am learning SDL 2.0 and I have some questions (problems) regarding rendering techniques that I was hoping someone qould help me with.
First of all, I am going to post what my code for rendering looks like right now.
(I will leave out stuff like error checking etc for the sake of readability)
Initialization
Code:
_renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, “linear”);
SDL_RenderSetLogicalSize(
_renderer,
1000,
1000
);
_backbuffer = SDL_CreateTexture(
_renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET | SDL_TEXTUREACCESS_STREAMING,
1000,
1000
);
Drawing
Code:
SDL_SetRenderDrawColor(_renderer, 100, 149, 237, 255);
SDL_RenderClear(_renderer);
SDL_SetRenderTarget(_renderer, _backbuffer);
SDL_RenderCopy(renderer, _texture, nullptr, &dest);
SDL_SetRenderTarget(_renderer, nullptr);
SDL_RenderCopy(_renderer, _backbuffer, 0, 0);
SDL_DestroyTexture(_backbuffer);
SDL_RenderPresent(_renderer)
);
I am basically using this for drawing sprites and a tiles as well as some minor pixel effects.
At the moment I am only using SDL_Textures, not SDL_Surfaces.
The problems and questions I have is…
- I can’t get SDL_RenderSetLogicalSize(_renderer, 1000, 1000); to work as intended.
It says in the imigration guide that…
For 2.0, the render API lets you do this…
Code:
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, “linear”);
SDL_RenderSetLogicalSize(sdlRenderer, 640, 480);…and it will do the right thing for you. This is nice in that you can change the logical rendering size to achieve various effects, but the primary use is this: instead of trying to make the system work with your rendering size, we can now make your rendering size work with the system. On my 1920x1200 monitor, this app thinks it’s talking to a 640x480 resolution now, but SDL is using the GPU to scale it up to use all those pixels. Note that 640x480 and 1920x1200 aren’t the same aspect ratio: SDL takes care of that, too, scaling as much as possible and letterboxing the difference.
…but unfortunately, I am not getting that effect. There is no scaling going on at all.
Is it because I am using SDL_RenderCopy(renderer, _texture, nullptr, &dest) instead of SDL_UpdateTexture(sdlTexture, NULL, myPixels, 640 * sizeof (Uint32)) as the guide suggests?
- Is this method of rendering considered “correct” and is it effective?
As I said, the guide uses SDL_UpdateTexture, which I was not able to get to work. If I understand it correctly you need lo lock the texture to access its pixels, but I was not able to do that, could someone please provide me with a minimal example? And also, on that note, what is the literal integer value 640 representing in the code SDL_UpdateTexture(sdlTexture, NULL, myPixels, 640 * sizeof (Uint32))?
Maybe some of these questions have been answered before, but I have not been able to find an answer to them, so if anyone is able to help me out, thanks in advanced! / AS