0
\$\begingroup\$

I pulled my SDL2 game out of cold storage and am working to get it running again. I found a curious thing: graphics primitives based on SDL2_gfx and text based on SDL_ttf are displaying way too big, in blocky, oversized pixels. In the code shown below, with the window size at 400, things look pretty decent. But if I change the window size to 800, I don't get more pixels, I get bigger pixels. I've been researching a little, and it seemed that both SDL_RenderSetLogicalSize and SDL_RenderSetScale might have something to do with a situation like this, but a search shows that neither is present in any part of my code.

int main() { //Initialize SDL if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() ); return 1; } gWindow = SDL_CreateWindow("Drawbox", 0, 0, 400, 400, 0); if( gWindow == NULL ) { printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() ); return -1; } //Create renderer for window gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); if( gRenderer == NULL ) { printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); return -1; } /* Create texture for display buffering */ target = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 500, 500); SDL_SetRenderTarget(gRenderer, target); … much more code follows, including drawing circles, lines, and text } 

I did a little experimentation to see if SDL_RenderSetScale could be used to fight back:

SDL_RenderSetScale( gRenderer, 0.5, 0.5 ); circleColor(gRenderer, 100, 100, 50, 0xffff00ff); 

But the graphical result is this, plainly showing the results of trying to draw a skinny little circle with big fat pixels! ! (Please ignore the little spaceship looking thing inside, it's one of my game actors that just happened to land there.) enter image description here

I seem to have defaulted into some odd state. Unfortunately, I don't have a simple example that triggers this behavior. But are there other calls besides SDL_RenderSetLogicalSize and SDL_RenderSetScale that could lead to something like this? Has anyone seen this kind of behavior before?

Incidentally, I really don't want SDL_RenderSetLogicalSize. My game is pixel-savvy, and rather than being targeted to a specific screen size, it will gladly and intelligently make use of all the pixels it can get.

\$\endgroup\$
2
  • \$\begingroup\$ Could this be related to creating a window of size 400x400 but then making a texture of size 500x500 to display onto the window? \$\endgroup\$ Commented Jul 29, 2024 at 8:59
  • \$\begingroup\$ It sure was! Thanks. \$\endgroup\$ Commented Jul 29, 2024 at 16:19

1 Answer 1

2
\$\begingroup\$

The answer to this headbreaking problem, when I saw it, was laughably simple. (And the bit of code I needed to see was at the end of a long line, offscreen. Argh!) Here's the scoop:

You draw to a TEXTURE. The texture is presented (via RenderPresent) to the WINDOW.

The size of the texture determines the number of pixels. The size of the window determines how big the image will be, and therefore how big the pixels will be.

If you want to maximize pixels, the size of the texture and the size of the window should be the same.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.