제7장 SFML으로 작성해보는프로젝트 I
1. SFML 라이브러리의 기초 사항을 살펴본다.
2. SFML을 이용하여 몇 가지의 클래식 게임을 작성해본다.
이번장에서만들어볼프로그램
SFML
SFML은 PC의애플리케이션에간단한멀티미디어인터페이스를제공하여게임및멀티미디어애플리케이션개발을용이하게한다. 시스템, 윈도우, 그래픽, 오디오및네트워크의 5개모듈로구성된다.
SFML 설치
SFML 설치
SFML 설치
SFML 프로젝트만들기
SFML 프로젝트
SFML 프로젝트
SFML 프로젝트
SFML 프로젝트
예제프로그램#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow
window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
실행결과
SFML 프로그램의구조
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(600, 480), "LUNARLANDER");
window.setFramerateLimit(60);
//게임에필요한스프라이트를생성한다.Texture t2;
Sprite lander;
t2.loadFromFile("images/spaceship.png");
lander.setTexture(t2);
SFML 프로그램의구조//여기서부터게임루프이다.
while (window.isOpen())
{
//이벤트검사및처리Event e;
while (window.pollEvent(e)) {
if (e.type == Event::Closed)
window.close();
}
//화면을지운다.window.clear();
//화면에스프라이트를그린다.window.draw(lander);
//화면을표시한다.window.display();
}
return 0;
}
실행결과
게임루프
스프라이트를움직여보자.
if (Keyboard::isKeyPressed(Keyboard::Left))
lander.move(-10.0, 0.0);
if (Keyboard::isKeyPressed(Keyboard::Right))
lander.move(10.0, 0.0);
배경화면생성
Texture t1; //텍스처객체Sprite background; //스프라이트객체t1.loadFromFile("images/background.png");
background.setTexture(t1);
//화면에스프라이트를그린다.window.draw(background);
window.draw(lander);
실행결과
Lunar Lander 게임
우리의 Lunar Lander
전체소스#include <SFML/Graphics.hpp>
#include <windows.h>
#include <string>
#include <vector>
#include <iostream>
using namespace sf;
using namespace std;
class LunarLander {
private:
double x, y; //현재위치double velocity; //속도double fuel; //연료string status; //현재상태Texture t1, t2; //텍스처객체Sprite spaceship, burst; //스프라이트객체Font font; //폰트객체Text text; //텍스트객체
전체소스public:
LunarLander(double h, double v, double f); //생성자bool checkLanded();
void update(double rate);
void draw(RenderWindow &window);
};
//생성자함수LunarLander::LunarLander(double h, double v, double f) {
x = 300;
y = h;
velocity = v;
fuel = f;
t1.loadFromFile("images/spaceship.png");
t2.loadFromFile("images/burst.png");
spaceship.setTexture(t1);
burst.setTexture(t2);
spaceship.setPosition(x, y);
burst.setPosition(x + 20, y + 50);
if (!font.loadFromFile("OpenSans-Bold.ttf")) {
cout << "폰트파일을오픈할수없음!" << endl;}
text.setFont(font);
}
전체소스//착륙했는지를검사하는함수.만약높이가 0보다작으면착륙한것이다.bool LunarLander::checkLanded() {
if (y <= 0)
return true;
return false;
}
//게임상태를업데이트한다.void LunarLander::update(double amount) {
if (fuel <= 0) {
fuel = 0;
amount = 0;
}
fuel = fuel - amount;
velocity = velocity - amount + 0.8;
y = y + velocity;
if (y > 450) y = 450;
spaceship.setPosition(x, y);
burst.setPosition(x + 20, y + 50);
status = "Press up key!\nheight: " + to_string(y) + "\nspeed: " +to_string(velocity) + "\nfuel: " + to_string(fuel);
}
전체소스
//화면에착륙선과불꽃,현재상태를그린다.void LunarLander::draw(RenderWindow &window) {
window.draw(spaceship);
window.draw(burst);
text.setString(status);
text.setCharacterSize(20);
text.setPosition(10, 100);
window.draw(text);
}
전체소스
int main()
{
RenderWindow window(VideoMode(600, 480), "LUNARLANDER");
window.setFramerateLimit(60);
Texture t;
Sprite background;
t.loadFromFile("images/background.png");
background.setTexture(t);
LunarLander lander(300.0, 1.0, 100.0); // 착륙선 객체생성
전체소스while (window.isOpen()) {
Event e;
while (window.pollEvent(e)) {
if (e.type == Event::Closed)
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::Up))
lander.update(3.0);
else
lander.update(0.0);
window.clear();
window.draw(background);
lander.draw(window);
window.display();
Sleep(100); // 0.1초동안잠재운다.}
return 0;
}
Q & A