TI-S2

Opdrachten Classes C++

Inhoud

Deze opdracht kan nog veranderen!

Opdracht OO1.51 SFML window

Installeer SFML met de packetmanager (dankzij Lia E.):

Zelf installeren:

SFML voorbeeld

Opdracht OO1.52 Rechthoek

Tips

Hieronder staat voorbeeldcode die misschien behulpzaam is.

// incomplete code
// exercise: draw a SFML rectangle
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>

int main ()
{
    // the window in which we want to print the line
    sf::RenderWindow window (sf::VideoMode (800, 600), "My window", sf::Style::Default, sf::ContextSettings (0, 0, 2));

    // Todo: define a sfml rectangle, e.g. named rshape, located at (4, 2) with a size of 120x50
    // ... rshape ...;

    // run the program as long as the window is open
    while (window.isOpen ())
	{
	    // check all the window's events that were triggered since the last iteration of the loop
	    sf::Event event;
	    // window.display ();
	    while (window.pollEvent (event))
		{
		    // "close requested" event: we close the window
		    if (event.type == sf::Event::Closed)
			window.close ();
		}

	    // clear the window
	    window.clear ();
	    // Todo: draw the sfml rectangle - uncomment the next line
	    // window.draw (rshape);

	    window.display ();
	    // voorkom gebibber op het scherm
	    sf::sleep (sf::milliseconds (20));
	}
    return 0;
}

main.cpp

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: SFML g++.exe build AND RUN active file",
            "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${fileDirname}\\*.cpp",
		"-lsfml-graphics",
        "-lsfml-window",
                "-lsfml-system",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&&",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\msys64\\ucrt64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Voorbeeld tasks.json file voor VScode, bij gebruik van SFML. Let op de -l flags (-lsfml-system en -lsfml-window)Bron: Lia Engelchor en Tobias Bosch

Uitleg van Tobias Toelichting door Tobias: als het goed is kun je met bovenstaande tasks.json simpelweg de build task runnen via Terminal -> Run Build Task of de shortcut Ctrl + Shift + B gebruiken. De task compilet de binary, en runt die gelijk daarna, het is niet nodig om die zelf nog te runnen.