<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sanyam Sharma 221910304042</title>
    <description>The latest articles on DEV Community by Sanyam Sharma 221910304042 (@sansh).</description>
    <link>https://dev.to/sansh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F627420%2Fa83d5df7-bfc8-4edb-80d9-8cbe2609c99a.png</url>
      <title>DEV Community: Sanyam Sharma 221910304042</title>
      <link>https://dev.to/sansh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sansh"/>
    <language>en</language>
    <item>
      <title>Make a sorting algorithm visualizer using C++ and SFML</title>
      <dc:creator>Sanyam Sharma 221910304042</dc:creator>
      <pubDate>Mon, 25 Oct 2021 16:54:46 +0000</pubDate>
      <link>https://dev.to/sansh/make-a-sorting-algorithm-visualizer-using-c-and-sfml-d61</link>
      <guid>https://dev.to/sansh/make-a-sorting-algorithm-visualizer-using-c-and-sfml-d61</guid>
      <description>&lt;p&gt;This article assumes that you have SFML configured and know how to compile and run it.&lt;/p&gt;

&lt;h3&gt;
  
  
  About SFML
&lt;/h3&gt;

&lt;p&gt;Simple and Fast Multimedia Library or SFML provides a simple interface to the various components of your PC, to ease the development of games and multimedia applications. It is composed of five modules: system, window, graphics, audio and network. It is also cross-platform which means for simple program that you would want to run on different types of operating systems, you can without a lot of effort.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Source: &lt;a href="//www.sfml-dev.org"&gt;sfml-dev.org&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Insertion Sort
&lt;/h3&gt;

&lt;p&gt;To sort an array of size n: &lt;br&gt;
&lt;strong&gt;Step 1&lt;/strong&gt;: Iterate from arr[1] to arr[n] over the array. &lt;br&gt;
&lt;strong&gt;Step 2&lt;/strong&gt;: Compare the current element to its predecessor. &lt;br&gt;
&lt;strong&gt;Step 3&lt;/strong&gt;: If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For more info check out &lt;a href="https://www.geeksforgeeks.org/insertion-sort/" rel="noopener noreferrer"&gt;geeksforgeeks explanation on this&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Libraries Used
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;SFML&lt;/li&gt;
&lt;li&gt;unistd (used for the sleep function, may vary based on one's OS)&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Rendering a new window in SFML
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;SFML/Graphics.hpp&amp;gt;

int main(){

    sf::RenderWindow window(sf::VideoMode(600, 600), "Sample SFML App");

    while (window.isOpen()){ 

        sf::Event event;
        while (window.pollEvent(event)){

            switch (event.type)
            {

                case sf::Event::Closed:
                    window.close();
                    break;

            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Insertion Sort Function:
&lt;/h3&gt;

&lt;p&gt;This function implements insertion sort algorithm and it sorts the recH[] array &lt;em&gt;(initialization in the full code)&lt;/em&gt;&lt;br&gt;
and also calls the dispSort() Function in every iteraton &lt;em&gt;(more on this function below)&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementing Insertion Sort
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void insertionSort()
{
    usleep(microsecond*5);
    int i, key, j;
    for (i = 0; i &amp;lt; n; i++)
    {
        key = recHs[i];
        j = i - 1;

        while (j &amp;gt;= 0 &amp;amp;&amp;amp; recHs[j] &amp;gt; key)
        {
            recHs[j + 1] = recHs[j];
            j = j - 1;
            dispSort(j);
        }
        recHs[j + 1] = key;
    }

    sorted = true;

    dispSort(i);

}

```


###The dispSort() Function
The dispSort() function is called in every iteration of the insertion sort loop, what it does is creates rectangular blocks based on the height data in the recH[] array, the same array being sorted.



```
void dispSort(int index){
    window.clear();
    for(int i=0; i&amp;lt;n; i++){
        sf::RectangleShape block(sf::Vector2f(10, recHs[i]));
        block.setPosition(i*12, 600-recHs[i]);
        block.setFillColor(sorted || i==index? sf::Color::Green : sf::Color::White);
        window.draw(block);
    }
    window.display();
}

```


*Note: the index perimeter is used for the indicator of the current block being sorted.*

###Full Code


```
#include&amp;lt;SFML/Graphics.hpp&amp;gt;
#include&amp;lt;unistd.h&amp;gt;

sf::RenderWindow window(sf::VideoMode(960, 600), "Sorter");
int n=80;
float recHs[80];
unsigned int microsecond = 1000000;
bool sorted=false;

void dispSort(int index){
    window.clear();
    for(int i=0; i&amp;lt;n; i++){
        sf::RectangleShape block(sf::Vector2f(10, recHs[i]));
        block.setPosition(i*12, 600-recHs[i]);
        block.setFillColor(sorted || i==index? sf::Color::Green : sf::Color::White);
        window.draw(block);
    }
    window.display();
}

void insertionSort()
{
    usleep(microsecond*5);
    int i, key, j;
    for (i = 0; i &amp;lt; n; i++)
    {
        key = recHs[i];
        j = i - 1;

        while (j &amp;gt;= 0 &amp;amp;&amp;amp; recHs[j] &amp;gt; key)
        {
            recHs[j + 1] = recHs[j];
            j = j - 1;
            dispSort(j);
        }
        recHs[j + 1] = key;
    }

    sorted = true;

    dispSort(i);

}



int main(){

    for(int i=0; i&amp;lt;n; i++){
        recHs[i]=(rand()%500);
    }
    while(window.isOpen()){
        sf::Event event;


        while (window.pollEvent(event)){
            switch(event.type){

                case sf::Event::Closed:
                    window.close();
            }
        }
        if(!sorted){
            dispSort(0);
            insertionSort();
        }

    }

}
```


###Result

![Insertion Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wumhdo80874dmzrw5lh.GIF)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
    <item>
      <title>Detect Keystrokes in a SFML App C++</title>
      <dc:creator>Sanyam Sharma 221910304042</dc:creator>
      <pubDate>Sat, 08 May 2021 14:56:45 +0000</pubDate>
      <link>https://dev.to/sansh/detect-keystrokes-in-a-sfml-app-c-1127</link>
      <guid>https://dev.to/sansh/detect-keystrokes-in-a-sfml-app-c-1127</guid>
      <description>&lt;p&gt;SFML(Simple and Fast Multimedia Library) provides a simple interface to the various components of your PC, to ease the development of games and multimedia applications.&lt;/p&gt;

&lt;p&gt;This article assumes that you have SFML configured and know how to compile and run it&lt;/p&gt;

&lt;p&gt;Rendering a new window:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;SFML/Graphics.hpp&amp;gt;
#include&amp;lt;iostream&amp;gt;

int main(){

    sf::RenderWindow window(sf::VideoMode(600, 600), "Sample SFML App");

    while (window.isOpen()){ 

        sf::Event event;
        while (window.pollEvent(event)){

            switch (event.type)
            {

                case sf::Event::Closed:
                    window.close();
                    break;

            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This has now rendered a window of 600px height and 600px width and with the title "Sample SFML App".&lt;br&gt;
The &lt;code&gt;windows.isOpen()&lt;/code&gt; lets us stop the window from closing automatically and perform operations on it, and event can be used to detect things happening in the Window. Also we have used a switch case which can be further used to do things when a certain operation takes place on the window. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example when the "Close" button is pressed on the title bar, the window closes, we can also make it print something, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---oCrmK1M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2kxig4e1mjp7sxg70ipl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---oCrmK1M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2kxig4e1mjp7sxg70ipl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now all we need to do is create another case where we will detect any text entered. We will do that by implementing &lt;code&gt;sf::Event::TextEntered&lt;/code&gt; as a second case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;case sf::Event::TextEntered:

                    if(event.text.unicode&amp;lt;128){
                        std::cout&amp;lt;&amp;lt;(char)event.text.unicode&amp;lt;&amp;lt;"\n";
                    }
                    break;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints all the entered keystrokes(the ones that count as characters) onto the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lTAv2QD_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/710xfufdpjaf05csiupx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lTAv2QD_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/710xfufdpjaf05csiupx.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Full code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;SFML/Graphics.hpp&amp;gt;
#include&amp;lt;iostream&amp;gt;

int main(){

    sf::RenderWindow window(sf::VideoMode(600, 600), "Sample SFML App");

    while (window.isOpen()){ 

        sf::Event event;
        while (window.pollEvent(event)){

            switch (event.type)
            {

                case sf::Event::Closed:
                    window.close();
                    break;

                case sf::Event::TextEntered:

                    if(event.text.unicode&amp;lt;128){
                        std::cout&amp;lt;&amp;lt;(char)event.text.unicode&amp;lt;&amp;lt;"\n";
                    }
                    break;
            }
        }

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
