Difference between revisions of "C++ Drawing to the screen"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Using Graphics.h= There are many libraries for drawing graphics to the screen, these examples will show the standard 'Graphics.h' approach. You can the required files from: [...")
 
(Drawing a Rectangle)
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Using Graphics.h=
+
=SFML=
There are many libraries for drawing graphics to the screen, these examples will show the standard 'Graphics.h' approach. You can the required files from: [https://github.com/SagarGaniga/Graphics-Library This Link] . You will need to get 'graphics.h', 'winbgim.h', and the 'libbgi.a' files.
+
This method will require you to install some packages. In Visual Studio, and Project, select 'Manage Nuget Packages'.
  
==Drawing a Line==
+
Click the browse tab and type:
  
// C++ Implementation for drawing line
+
'sfml'
#include <graphics.h>
+
 
#include <winbgim.h>  
+
Now look for the version numbers, each version has a slightly different name. I have found:
 
+
 
// driver code
+
[[File:Sfml.png]]
int main()  
+
 
{  
+
SFML has 5 different components, so click each one and click install.
    // gm is Graphics mode which is a computer display
+
 
    // mode that generates image using pixels.
+
==Creating A Window==
    // DETECT is a macro defined in "graphics.h" header file
+
The code below will create a window, and also provides a loop to keep the window open and to check for window events:
    int gd = DETECT, gm;  
+
 
 
+
<syntaxhighlight lang=c++>
    // initgraph initializes the graphics system
+
#include <SFML/Graphics.hpp>
    // by loading a graphics driver from disk
+
 
    initgraph(&gd, &gm, "");
+
int main()
 
+
{
    // line for x1, y1, x2, y2
+
sf::RenderWindow window{{ 800, 800 }, "Window Title"};
    line(150, 150, 450, 150);
+
window.setFramerateLimit(60);
 
+
 
    getch();  
+
while (window.isOpen())
 
+
{
    // closegraph function closes the graphics
+
sf::Event event;
    // mode and deallocates all memory allocated
+
while (window.pollEvent(event))
    // by graphics system .  
+
{
    closegraph();  
+
if (event.type == sf:: Event::Closed)
 +
{
 +
window.close();
 +
}
 +
}
 +
window.clear();
 +
window.display();
 +
}
 
}
 
}
 +
</syntaxhighlight>
 +
 +
==Drawing a Rectangle==
 +
We can declare a rectangle, so after the 'window.setFramerateLimit(60)' line add:
 +
 +
<syntaxhighlight lang=c++>
 +
sf::RectangleShape rectangle{ { 220.f, 160.f } };
 +
rectangle.setFillColor(sf::Color::White);
 +
rectangle.setPosition({ 150.f, 20.f });
 +
rectangle.rotate(20.f);
 +
</syntaxhighlight>
 +
 +
Now in between the 'window.clear()' and the 'window.display()' enter the following line to draw the rectangle:
 +
 +
<syntaxhighlight lang=c++>
 +
window.draw(rectangle);
 +
</syntaxhighlight>

Revision as of 14:50, 13 June 2019

SFML

This method will require you to install some packages. In Visual Studio, and Project, select 'Manage Nuget Packages'.

Click the browse tab and type:

'sfml'

Now look for the version numbers, each version has a slightly different name. I have found:

Sfml.png

SFML has 5 different components, so click each one and click install.

Creating A Window

The code below will create a window, and also provides a loop to keep the window open and to check for window events:

#include <SFML/Graphics.hpp>

int main()
{
	sf::RenderWindow window{{ 800, 800 }, "Window Title"};
	window.setFramerateLimit(60);

	while (window.isOpen())
	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf:: Event::Closed)
			{
				window.close();
			}
		}
		window.clear();
		window.display();
	}
}

Drawing a Rectangle

We can declare a rectangle, so after the 'window.setFramerateLimit(60)' line add:

		sf::RectangleShape rectangle{ { 220.f, 160.f } }; 
		rectangle.setFillColor(sf::Color::White);
		rectangle.setPosition({ 150.f, 20.f }); 
		rectangle.rotate(20.f);

Now in between the 'window.clear()' and the 'window.display()' enter the following line to draw the rectangle:

window.draw(rectangle);