<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>draw circle | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/draw-circle/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Sun, 28 Jun 2020 19:28:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>draw circle | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>How to Draw a circle in C</title>
		<link>https://learncgames.com/how-to-draw-a-circle-in-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-draw-a-circle-in-c</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Fri, 03 Jul 2020 23:00:17 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[SDL]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[draw circle]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=925</guid>

					<description><![CDATA[<p>In the asteroids game, when you press the s button to put up a shield, it draws a circle.  I must confess, I didn&#8217;t know how to draw a cuircle so looked it up and found an example on StackOverflow. You can use code from StackOverflow, licensed under a MIT license.  I include the link [&#8230;]</p>
The post <a href="https://learncgames.com/how-to-draw-a-circle-in-c/">How to Draw a circle in C</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:640,&quot;href&quot;:&quot;https:\/\/stackoverflow.com\/questions\/38334081\/howto-draw-circles-arcs-and-vector-graphics-in-sdl&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20221007210917\/https:\/\/stackoverflow.com\/questions\/38334081\/howto-draw-circles-arcs-and-vector-graphics-in-sdl&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 17:03:42&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 05:18:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-01 17:54:30&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-08 13:20:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-18 02:18:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-21 20:43:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-29 13:39:12&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-02 16:25:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-09 10:46:00&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-13 07:34:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-17 10:59:40&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-20 19:37:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-24 14:18:03&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-24 14:18:03&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'></div>
<p><img decoding="async" class="alignleft size-full wp-image-926" src="https://learncgames.com/wp-content/uploads/2020/06/ateroids-shield.png" alt="Asteroids-with shield-round-player-ship" width="232" height="114" />In the asteroids game, when you press the s button to put up a shield, it draws a circle.  I must confess, I didn&#8217;t know how to draw a cuircle so looked it up and found an example on StackOverflow. You can use code from <a title="Link to How to Draw a circle on StackOverflow" href="https://stackoverflow.com/questions/38334081/howto-draw-circles-arcs-and-vector-graphics-in-sdl" target="_blank" rel="nofollow noopener noreferrer">StackOverflow</a>, licensed under a MIT license.  I include the link to StackOverflow in the game code (in the chapter 48 zip file) in a comment.</p>
<p>Here&#8217;s the code in the game.</p>
<pre><code class="language-c" data-line="">void DrawCircle(SDL_Renderer *Renderer, int _x, int _y, int radius)
{
	int x = radius - 1;
	int y = 0;
	int tx = 1;
	int ty = 1;
	int err = tx - (radius &lt;&lt; 1); // shifting bits left by 1 effectively
								  // doubles the value. == tx - diameter
	while (x &gt;= y)
	{
		//  Each of the following renders an octant (1/8th) of the circle
		SDL_RenderDrawPoint(Renderer, _x + x, _y - y);
		SDL_RenderDrawPoint(Renderer, _x + x, _y + y);
		SDL_RenderDrawPoint(Renderer, _x - x, _y - y);
		SDL_RenderDrawPoint(Renderer, _x - x, _y + y);
		SDL_RenderDrawPoint(Renderer, _x + y, _y - x);
		SDL_RenderDrawPoint(Renderer, _x + y, _y + x);
		SDL_RenderDrawPoint(Renderer, _x - y, _y - x);
		SDL_RenderDrawPoint(Renderer, _x - y, _y + x);

		if (err &lt;= 0)
		{
			y++;
			err += ty;
			ty += 2;
		}
		else 
		{
			x--;
			tx += 2;
			err += tx - (radius &lt;&lt; 1);
		}
	}
}</code></pre>
<p>it&#8217;s as simple as that! To make it more interesting, it is called each frame with the shield <em>throbbing</em>  by increasing  the radius from 38 to 46 pixels by 2 then restarting at 38 again. Here&#8217;s the code for that. Note that when the shield energy is below 10, it no longer works.</p>
<pre><code class="language-c" data-line="">void DisplayShield(SDL_Rect * target) {
	if (shieldFlag &amp;&amp; shieldStrength &gt;10) {
		SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
		DrawCircle(renderer, target-&gt;x + (SHIPWIDTH/2), target-&gt;y + (SHIPHEIGHT/2), shieldRadius);
		shieldRadius += 2;
		if (shieldRadius == 46) {
			shieldRadius = 38;
		}
	}
	if (shieldStrength &lt; 100) {
		TextAt(target-&gt;x + 10, target-&gt;y + 58, sltoa(shieldStrength), 0.67f);
	}
}</code></pre>
<p>The number under the player ship is the shield energy which drains while the shield is being displayed and recharges back up to 100 when you take your finger off the shield button. The number is only shown when the value is less than 100.</p>The post <a href="https://learncgames.com/how-to-draw-a-circle-in-c/">How to Draw a circle in C</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">925</post-id>	</item>
	</channel>
</rss>
