Coding & Programming/C, C++, SFML

[C/C++, SFML] 3. 기본 윈도우(RenderWindow) 출력하기

mainCodes 2021. 3. 26. 16:45

안녕하세요 JollyTree입니다 (•̀ᴗ•́)و

 

SFML의 기본 윈도우(RenderWindow)를 출력해 보겠습니다. RenderWindow는 그래픽 모듈의 메인 클래스이며 부모 클래스는 Window, RenderTarget, 최상위 클래스는 GlResource, NonCopyable 클래스로 구성되어 있습니다. Window 클래스와 같이 RenderWindow 클래스도 OpenGL 항목을 렌더링 할 수 있습니다. 

 

SFML RenderWindow  출력 화면

RenderWindow 클래스의 생성자(Constructor)는 다음과 같습니다.

 

디폴트 생성자

sf::RenderWindow::RenderWindow ( VideoMode  mode, const String &  title, Uint32  style = Style::Default,
const ContextSettings &  settings = ContextSettings() )

기존 컨트롤에 SFML 렌더링을 원할 경우 사용되는 생성자

sf::RenderWindow::RenderWindow ( WindowHandle  handle, const ContextSettings &  settings = ContextSettings() )

 

기본 윈도우를 출력하기 위해서는 SFML 헤더 중 Graphics,hpp를 포함해야 하며, 코드의 간결함과 편의를 위해 namespace를 설정합니다.

 

#include <SFML/Graphics,hpp>
using namespace sf;

기본적인 코드 골격은 새로운 RenderWindow를 생성하고 메인 루프(while(window.isOpen())를 가집니다. 이벤트 처리, 그래픽 개체 그리기, 오디오, 마우스 및 키보드 입력 처리 등의 모든 코드는 이 메인 루프내에 포함됩니다.  

 

SFML RenderWindow 출력 예제:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <SFML/Graphics.hpp>
#include <iostream>
 
using namespace std;
using namespace sf;
 
int main()
{
    RectangleShape rectangle(Vector2f(200200));
    rectangle.setFillColor(Color::Blue);
 
    cout << "프로그램이 시작되었습니다." << endl;
 
    //화면 크기, 캡션 이름 설정
    RenderWindow app(VideoMode(504504), "https://maincodes.tistory.com/");
    app.setFramerateLimit(60);  //프레임 비율 설정
 
    //SFML 메인 루프 - 윈도우가 닫힐때 까지 반복
    while (app.isOpen())
    {
        Event event;
 
        //이벤트 처리
        while (app.pollEvent(event)) 
        {
            //프로그램 종료 이벤트 처리
            if (event.type == Event::EventType::Closed)
            {
                app.close();
                cout << "프로그램이 종료되었습니다." << endl;
            }
        }
 
        //배경화면을 흰색으로 clear
        app.clear(Color::White);

       //사각형 그리기
        app.draw(rectangle);
 
        //프레임을 스크린에 출력
        app.display();
    }
 
    return 0;
}
cs

예제에서는 기본 RenderWindow에 200x200 크기의 사각형을 화면에 출력하는 코드가 포함되어 있습니다. app.display(); 는 draw하기 위해 구현된 내용들을 프레임 단위로 화면에 출력하는 역할을 합니다.  따라서 그리고자 하는 내용이 있다면 app.display() 함수 전에 코드를 추가하면 됩니다.

 

이상 JollyTree였습니다 (•̀ᴗ•́)و