Crop a texture rectangle

I’m working on a raycasting engine in C. It’s been smooth sailing up until when it came time to draw sprites.

float scale;
sprite skull = {0, {15 * 16 + 8, 5 * 16 + 8}, -1};
SDL_Rect draw = {0, 0, 0, 0};
coordsf temp = {skull.pos.x - posX, skull.pos.y - posY};
coordsf result = {(temp.y * cos(theta)) - (temp.x * sin(theta)), (temp.x * cos(theta)) + (temp.y * sin(theta))};
SDL_Surface *surface = SDL_LoadBMP("skull007.bmp");
SDL_Texture *texture;

scale = 360 / result.y;
skull.pos.x = result.x, skull.pos.y = result.y;
draw.x = (result.x * 1260 / result.y) + (1260 / 2) - (16 * scale);
draw.y = (skull.z * 1260 / result.y) + (720 / 2);
draw.w = draw.h = scale * 32;

texture = SDL_CreateTextureFromSurface(display.renderer, surface);
SDL_SetRenderDrawColor(display.renderer, 255, 255, 255, 0);
SDL_RenderCopy(display.renderer, texture, NULL, &draw);
SDL_RenderDrawRect(display.renderer, &draw);

And it works perfectly
But I need to be able to crop part of the texture, so that only the parts that are visible are rendered.LIke this.
I have tried using SDL_BlitSurface() to copy a part of the sprite onto another surface, that worked but the problem is that the source image is very low res (32 * 32) so I can’t accurately crop the image as there’ll always be a small gap between the wall and the sprite.
My first idea was to first blit the surface onto a scaled surface using SDL_BlitScaled(), then crop the surface by blitting it again onto the final surface which will be drawn, I figured since the resulting surface from the first blit would have high enough resolution that I’ll be able to crop it accurately. this hasn’t worked however Then I decided to try cropping the rectangle itself. I tried using SDL_RenderSetClipRect() , which does select the correct area, but it also fills the entire screen except for that area.
I am at a standstill right now as I have not found anything online regarding my issue, so any help would be appreciated. TIA!

Check the SDL_RenderCopyF and its srcrect argument. The “F” in the function name means that you can use subpixel values (floats).

I don’t think this’ll change anything, the result is the same as SDL_RenderCopy() when I tried it: the sprite doesn’t appear on the screen.

I don’t follow, you didn’t mention that there’s no sprite on the screen. What I understood, you only want to display part of the sprite, which can be achieved with the srcrect parameter.

I realized I made a mistake. in the SDL_RenderCopy() function, I was giving the srcrect the same position as the dstrect.
After changing the srcrect position to 0;0 so that it copies the source texture to the top-left corner of the destination rect, this is what happened:
halfing the width of the destination rect
halfing the width of the source rect
As you can see, in the first picture it squishes the rect instead of cropping it, and in the second picture it doesn’t do anything.

Fill width and height of the srcrect appropriately. If you want to copy left half of the texture with size of 32x32, your srcrect should be .x=0 .y=0 .w=16 .h=32

That works, I can draw half the texture without distorting it, but the SDL_RenderCopyF() doesn’t take an SDL_FRect for the source rect, so I still can only crop along the grid lines of the original texture.

This is fixed in SDL3. The new function is SDL_RenderTexture(), which takes a float rectangle for both the source and destination.