# About shapes and texture

**URL:** <https://discourse.libsdl.org/t/about-shapes-and-texture/28564>\
**Category:** Game Development\
**Created:** [January 10, 2021, 6:26am UTC](https://discourse.libsdl.org/t/about-shapes-and-texture/28564 "2021-01-10T06:26:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaMoXin](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@DaMoXin](https://discourse.libsdl.org/u/DaMoXin)\
**Post date:** [January 10, 2021, 6:26am UTC](https://discourse.libsdl.org/t/about-shapes-and-texture/28564/1 "2021-01-10T06:26:56Z")

</div>

[https://www.sfml-dev.org/tutorials/2.5/graphics-shape.php](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.

---

<div class="post-metadata">

**Author:** ![theweirdn8](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/76d3ee/32.png) [@theweirdn8](https://discourse.libsdl.org/u/theweirdn8)\
**Post date:** [January 13, 2021, 5:12pm UTC](https://discourse.libsdl.org/t/about-shapes-and-texture/28564/2 "2021-01-13T17:12:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Devsman](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Devsman](https://discourse.libsdl.org/u/Devsman)\
**Post date:** [January 14, 2021, 2:14am UTC](https://discourse.libsdl.org/t/about-shapes-and-texture/28564/3 "2021-01-14T02:14:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DaMoXin](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@DaMoXin](https://discourse.libsdl.org/u/DaMoXin)\
**Post date:** [January 29, 2021, 3:24am UTC](https://discourse.libsdl.org/t/about-shapes-and-texture/28564/4 "2021-01-29T03:24:51Z")

</div>

#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(" ")).

---

<div class="post-metadata">

**Author:** ![DaMoXin](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@DaMoXin](https://discourse.libsdl.org/u/DaMoXin)\
**Post date:** [January 29, 2021, 3:29am UTC](https://discourse.libsdl.org/t/about-shapes-and-texture/28564/5 "2021-01-29T03:29:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Devsman](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Devsman](https://discourse.libsdl.org/u/Devsman)\
**Post date:** [February 1, 2021, 1:21pm UTC](https://discourse.libsdl.org/t/about-shapes-and-texture/28564/6 "2021-02-01T13:21:59Z")

</div>

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.
