About shapes and texture

https://www.sfml-dev.org/tutorials/2.5/graphics-shape.php Can SDL achieve the effect of the third picture in the URL link,shapes can be textured.It still uses the rect function to display, but the image displayed is circular or other shape.

No, you can pre-render shapes though, but real time will be very costly.

I wonder if a custom blendmode could be used here, just make a simple alpha mask in your desired shape, and blend them so that it uses your texture for color and your mask for alpha.

At that point, though, you wouldn’t be gaining much advantage over just rendering the texture in the desired shape in an image editor anyway.

#include <SDL2/SDL.h>

#include <SDL2/SDL_image.h>

int main(int argc,char**argv)

{

SDL_Init(SDL_INIT_VIDEO);

SDL_Window*window=SDL_CreateWindow(“window1”, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 720, 1280, SDL_WINDOW_SHOWN);

SDL_Renderer*renderer=SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);

SDL_Surface*surface=IMG_Load("/Pictures/1.jpg");

SDL_Rect box={0, 0, 500, 500};

SDL_Texture*texture=SDL_CreateTextureFromSurface(renderer, surface);

SDL_RenderCopy(renderer, texture, NULL, &box);
int x,y;
for(x=0;x<=500;x++){
for(y=0;y<=500;y++){
if(((x-250)(x-250)+(y-250)(y-250))>250*250){
SDL_RenderDrawPoint(renderer,x,y); } } }

SDL_RenderPresent(renderer);

SDL_Delay(12000);

SDL_FreeSurface(surface);

SDL_DestroyTexture(texture);

SDL_DestroyRenderer(renderer);

SDL_DestroyWindow(window);

SDL_Quit();

}
I use the above code to achieve a circular image display, still using the rect function. If you want to try, just change the image path (IMG_ Load(" ")).

It uses the equations of the circle and some knowledge of geometry(x-a)²+(y-b)²=r².Draw the area outside the circle in the rectangle as the background color(The area where the rect(SDL_Rect rect) and polygons within the rect do not intersect).Through this method, I can also realize the ellipse display of the picture (x²/a²+y²/b²=c²(a>b>0)), but I have no way for the figure with more than three vertices、triangle(the rectangle should be simpler).And this method is not efficient.Can anyone solve these two problems.

I may be taking the example you provided too literally, but it’s also possible to use some draw order tricks, if your “background” is simpler than the texture you want to draw.

For example, you can provide the illusion of a circle on a black background by drawing a large black shape with a hole cut out of it on top of your texture.