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!