翻蚳è ããïŒãã®èšäºã¯ãSFMLå ¬åŒã©ã€ãã©ãªã¬ã€ãã®ç¿»èš³ãµã€ã¯ã«ã®7çªç®ã§ãã éå»ã®èšäºã¯ãã¡ãã«ãããŸãã ãã®äžé£ã®èšäºã®ç®çã¯ãå ã®èšèªãç¥ããªã人ã«ãã®ã©ã€ãã©ãªã«æ £ããæ©äŒãæäŸããããšã§ãã SFMLã¯ãã·ã³ãã«ã§ã¯ãã¹ãã©ãããã©ãŒã ã®ãã«ãã¡ãã£ã¢ã©ã€ãã©ãªã§ãã SFMLã¯ãã²ãŒã ããã®ä»ã®ãã«ãã¡ãã£ã¢ã¢ããªã±ãŒã·ã§ã³ãéçºããããã®ã·ã³ãã«ãªã€ã³ã¿ãŒãã§ã€ã¹ãæäŸããŸãã å ã®èšäºã¯ãã¡ãã«ãããŸã ã å§ããŸãããã
ç®æ¬¡ïŒ
0.1ã¯ããã«
1.ã¯ããã«
2.ã·ã¹ãã ã¢ãžã¥ãŒã«
3.ãŠã£ã³ããŠã¢ãžã¥ãŒã«
4.ã°ã©ãã£ãã¯ã¢ãžã¥ãŒã«
5.ãªãŒãã£ãªã¢ãžã¥ãŒã«
6.ãããã¯ãŒã¯ã¢ãžã¥ãŒã«
1.ã¯ããã«
- SFMLãšVisual Studio
- SFMLãšã³ãŒã::ãããã¯ïŒMinGWïŒ
- SFMLããã³Linux
- SFMLãšXcodeïŒMac OS XïŒ
- CMakeã§SFMLãã³ã³ãã€ã«ãã
2.ã·ã¹ãã ã¢ãžã¥ãŒã«
3.ãŠã£ã³ããŠã¢ãžã¥ãŒã«
- ãŠã£ã³ããŠãéããŠç®¡çãã
- ã€ãã³ãåŠç
- ããŒããŒããããŠã¹ããžã§ã€ã¹ãã£ãã¯ã䜿çšãã
- OpenGLã䜿çšãã
4.ã°ã©ãã£ãã¯ã¢ãžã¥ãŒã«
- 2Dæç»ãªããžã§ã¯ã
- ã¹ãã©ã€ããšãã¯ã¹ãã£
- ããã¹ããšãã©ã³ã
- ãã©ãŒã
- é ç¹é åã䜿çšããŠç¬èªã®ãªããžã§ã¯ããèšèšãã
- äœçœ®ãå転ãã¹ã±ãŒã«ïŒãªããžã§ã¯ãã®å€æ
- ã·ã§ãŒããŒã§ç¹æ®å¹æãè¿œå ãã
- 2Dã«ã¡ã©ãšãã¥ãŒã³ã³ãããŒã«
5.ãªãŒãã£ãªã¢ãžã¥ãŒã«
- ãµãŠã³ããšé³æ¥œãåçãã
- ãªãŒãã£ãªé²é³
- ã«ã¹ã¿ã ãªãŒãã£ãªã¹ããªãŒã
- 空éåïŒ3DãµãŠã³ã
6.ãããã¯ãŒã¯ã¢ãžã¥ãŒã«
- ãœã±ããéä¿¡
- ããã±ãŒãžã®äœ¿çšãšæ¡åŒµ
- HTTPã䜿çšããWebèŠæ±
- FTPãã¡ã€ã«è»¢é
ã¹ããªãŒã ãšã¯äœã§ããïŒ
ã»ãšãã©ã®äººã¯ãã§ã«ã¹ããªãŒã ãšã¯äœããç¥ã£ãŠããŸããããã®ãããã¯ã§ã¯åå¿è åãã®ã¹ããªãŒã ã«ã€ããŠèª¬æããŸãããã
ã¹ã¬ããã¯ãæ¬è³ªçã«ãä»ã®ã¹ã¬ãããšäžŠè¡ããŠå®è¡ãããäžé£ã®åœä»€ã§ãã åããã°ã©ã ã¯ãå°ãªããšã1ã€ã®ã¹ã¬ãããäœæããŸããã¡ã€ã³ã¹ã¬ããã¯ãmainïŒïŒé¢æ°ãå®è¡ããŸãã ã¡ã€ã³ã¹ã¬ããã®ã¿ã䜿çšããããã°ã©ã ã¯ã·ã³ã°ã«ã¹ã¬ããã§ãã 1ã€ä»¥äžã®ã¹ã¬ãããè¿œå ãããšããã«ãã¹ã¬ããã«ãªããŸãã
ã€ãŸããèŠããã«ãã¹ã¬ããã¯äžåºŠã«ããã€ãã®ããšãè¡ãæ¹æ³ã§ãã ããã¯ãããšãã°ãç»åãé³å£°ã®èªã¿èŸŒã¿äžã«ã¢ãã¡ãŒã·ã§ã³ã衚瀺ãããããŠãŒã¶ãŒå ¥åãåŠçãããããå Žåã«åœ¹ç«ã¡ãŸãã ã¹ããªãŒã ã¯ãããã¯ãŒã¯ããã°ã©ãã³ã°ã§ãåºã䜿çšãããŠããŸãããããŒã¿ã®åä¿¡ãåŸ æ©ããŠããéãã¢ããªã±ãŒã·ã§ã³ã®æŽæ°ãšæç»ã¯ç¶è¡ãããŸãã
SFMLã¹ã¬ãããŸãã¯std ::ã¹ã¬ããïŒ
ææ°ããŒãžã§ã³ïŒ2011ïŒã§ã¯ãC ++æšæºã©ã€ãã©ãªã¯ã¹ããªãŒã ãæäœããããã®äžé£ã®ã¯ã©ã¹ãæäŸããŸã ã SFMLãäœæãããšããC ++ 11æšæºã¯ãŸã äœæãããŠããããã¹ã¬ãããäœæããæšæºçãªæ¹æ³ã¯ãããŸããã§ããã SFML 2.0ããªãªãŒã¹ããããšãããã®æ°ããæšæºããµããŒãããŠããªãå€ãã®ã³ã³ãã€ã©ããããŸããã
æ°ããæšæºããµããŒãããããããŒãã¡ã€ã«ãå«ãã³ã³ãã€ã©ã䜿çšããŠããå Žåã¯ãSFMLã¹ããªãŒã ã¯ã©ã¹ãå¿ããŠã代ããã«æšæºC ++ã¯ã©ã¹ã䜿çšããŠãã ããã ãã ãããã®æšæºããµããŒãããŠããªãã³ã³ãã€ã©ã䜿çšããŠããå ŽåããŸãã¯ã³ãŒããé åžããŠå®å šãªç§»æ€æ§ãå®çŸãããå Žåã¯ãSFMLã¹ããªãŒã ã¯ã©ã¹ãé©ããŠããŸãã
SFMLã䜿çšããŠã¹ã¬ãããäœæãã
ååã«æŽèšããããŠãã³ãŒããèŠãŠã¿ãŸãããã SFMLã䜿çšããŠã¹ã¬ãããäœæã§ããã¯ã©ã¹ã¯sf :: ThreadãšåŒã°ãããããå®éã®åäœïŒã¹ã¬ããäœæïŒã®æ§åã§ãã
#include <SFML/System.hpp> #include <iostream> void func() { // thread.launch() for (int i = 0; i < 10; ++i) std::cout << "I'm thread number one" << std::endl; } int main() { // func sf::Thread thread(&func); // thread.launch(); // ... for (int i = 0; i < 10; ++i) std::cout << "I'm the main thread" << std::endl; return 0; }
ãã®ã³ãŒãã§ã¯ãmainé¢æ°ãšfuncé¢æ°ã¯ãthread.launchïŒïŒãåŒã³åºããåŸã«äžŠåã«å®è¡ãããŸãã ãã®çµæãäž¡æ¹ã®æ©èœã«ããããã¹ãåºåãã³ã³ãœãŒã«ã«æ··åšããŸãã
ã¹ããªãŒã ãžã®ãšã³ããªãã€ã³ããã€ãŸã ã¹ã¬ããã®éå§æã«å®è¡ãããé¢æ°ã¯sf :: Threadã³ã³ã¹ãã©ã¯ã¿ãŒã«æž¡ãå¿ èŠããããŸãã sf ::ã¹ã¬ããã¯æè»ã«ãªããããŸããŸãªãšã³ããªãã€ã³ãïŒéã¡ã³ããŒé¢æ°ãŸãã¯ã¯ã©ã¹ã¡ãœãããåŒæ°ä»ããŸãã¯åŒæ°ãªãã®é¢æ°ããã¡ã³ã¯ã¿ãŒãªã©ïŒãåãå ¥ããããšããŸãã äžèšã®äŸã¯ãã¡ã³ããŒé¢æ°ã®äœ¿çšæ¹æ³ã瀺ããŠããŸããããã«ãä»ã®ããã€ãã®äŸã瀺ããŸãã
- 1ã€ã®åŒæ°ãæã€éã¡ã³ããŒé¢æ°ïŒ
void func(int x) { } sf::Thread thread(&func, 5);
- ã¯ã©ã¹ã¡ãœããïŒ
class MyClass { public: void func() { } }; MyClass object; sf::Thread thread(&MyClass::func, &object);
- ãã¡ã³ã¯ã¿ãŒïŒæ©èœãªããžã§ã¯ãïŒïŒ
struct MyFunctor { void operator()() { } }; sf::Thread thread(MyFunctor());
ãã¡ã³ã¯ã¿ãŒã䜿çšããæåŸã®äŸã¯ãããããã¿ã€ãã®ãã¡ã³ã¯ã¿ãŒãåãå ¥ããããšãã§ãããããæã匷åã§ãããããã£ãŠã sf :: Threadã¯ã©ã¹ã¯ãçŽæ¥ãµããŒããããŠããªãå€ãã®ã¿ã€ãã®é¢æ°ãšäºææ§ããããŸãã ãã®é¢æ°ã¯ãã©ã ãåŒC ++ 11ãŸãã¯std :: bindã§ç¹ã«èå³æ·±ãã§ãã
// - sf::Thread thread([](){ std::cout << "I am in thread!" << std::endl; });
// std::bind void func(std::string, int, double) { } sf::Thread thread(std::bind(&func, "hello", 24, 0.5));
ã¯ã©ã¹å ã§sf :: Threadã䜿çšããå Žåã¯ãæšæºã³ã³ã¹ãã©ã¯ã¿ãŒããªãããšãå¿ããªãã§ãã ããã ãããã£ãŠãåæåãªã¹ãã®ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã§åæåããå¿ èŠããããŸãã
class ClassWithThread { public: ClassWithThread() : m_thread(&ClassWithThread::f, this) { } private: void f() { ... } sf::Thread m_thread; };
ãªããžã§ã¯ããåæåããåŸã«æ¬åœã«sf :: Threadãã€ã³ã¹ã¿ã³ã¹åããå¿ èŠãããå Žåã¯ãããŒãäžã«äœæã§ããŸãã
ã¹ããªãŒã éå§
sf :: Threadã®ã€ã³ã¹ã¿ã³ã¹ãäœæããã ãé¢æ°ãå®è¡ããŠèµ·åããå¿ èŠããããŸãã
sf::Thread thread(&func); thread.launch();
launchã¯ãæ°ããã¹ã¬ããã®ã³ã³ã¹ãã©ã¯ã¿ãŒã«æž¡ããé¢æ°ãåŒã³åºããŠããã«çµäºãããããåŒã³åºãå ã®ã¹ã¬ããã¯ããã«å®è¡ãç¶ç¶ã§ããŸãã
ãããŒã¹ããã
ç¹å®ã®ã¹ã¬ããã®ãšã³ããªãã€ã³ããšããŠæ©èœããé¢æ°ããã®å€ãè¿ããšãã¹ã¬ããã¯èªåçã«çµäºããŸãã ã¹ã¬ãããå¥ã®ã¹ã¬ããããçµäºãããŸã§åŸ æ©ããå Žåã¯ããã®åŸ æ©é¢æ°ãåŒã³åºãããšãã§ããŸãã
sf::Thread thread(&func); // thread.launch(); ... // , thread.wait();
waité¢æ°ãsf :: Threadãã¹ãã©ã¯ã¿ã«ãã£ãŠæé»çã«åŒã³åºããããããã¹ã¬ããã¯sf :: Threadã€ã³ã¹ã¿ã³ã¹ãç Žæ£ãããåŸãå®è¡ãç¶ç¶ã§ããŸããïŒãã§ãã¯ãããŸããïŒã ã¹ã¬ããã管çãããšãã¯ãããèŠããŠãããŠãã ããïŒèšäºã®æåŸã®ã»ã¯ã·ã§ã³ãåç §ïŒã
äžæåæ¢ãããŒ
SFMLã«ã¯ãã¹ããªãŒã ãäžæåæ¢ããæ¹æ³ãæäŸããæ©èœã¯ãããŸããã ã¹ããªãŒã ãäžæåæ¢ããå¯äžã®æ¹æ³ã¯ãã¹ããªãŒã èªäœã®ã³ãŒãããå®è¡ããããšã§ãã ã€ãŸããçŸåšã®ã¹ã¬ããã®ã¿ãäžæåæ¢ã§ããŸãã ãããè¡ãã«ã¯ãsf :: sleepé¢æ°ãåŒã³åºãããšãã§ããŸãïŒ
void func() { ... sf::sleep(sf::milliseconds(10)); ... }
sf :: sleepã«ã¯ãäžæåæ¢æéãšãã1ã€ã®åŒæ°ããããŸãã æéåŠçã«é¢ããèšäºã§ç€ºããããã«ããã®æéã¯ä»»æã®åäœã§è¡šçŸã§ããŸãã
ã¡ã€ã³ã¹ã¬ãããå«ãããã®é¢æ°ã䜿çšããŠä»»æã®ã¹ã¬ãããäžæåæ¢ã§ããããšã«æ³šæããŠãã ããã
sf :: sleepã¯ãã¹ã¬ãããäžæããæãå¹ççãªæ¹æ³ã§ããã¹ã¬ããã®äžæäžãã¹ã¬ããïŒã¹ã¬ããïŒã¯å®è³ªçã«ããã»ããµãªãœãŒã¹ãæ¶è²»ããŸããã 空ã®whileã«ãŒãã®ãããªã¢ã¯ãã£ããªåŸ æ©ã«åºã¥ãäžæã¯ãCPUã100ïŒ æ¶è²»ããäœãããŸããã ãã ããäžæåæ¢ã®æéã¯åãªãæãããã«ãããªãããšã«æ³šæããŠãã ããã äžæåæ¢ã®å®éã®æéïŒæå®ããæéãããé·ããçãïŒã¯OSã«ãã£ãŠç°ãªããŸãã ãããã£ãŠãéåžžã«æ£ç¢ºãªã¿ã€ãã³ã°ã§ãã®æ©èœã«äŸåããªãã§ãã ããã
å ±æããŒã¿ä¿è·
ããã°ã©ã å ã®ãã¹ãŠã®ã¹ã¬ããã¯ã¡ã¢ãªãå ±æããã¹ã³ãŒãå ã®ãã¹ãŠã®å€æ°ã«ã¢ã¯ã»ã¹ã§ããŸãã ããã¯éåžžã«äŸ¿å©ã§ãããå±éºã§ããããŸããã¹ã¬ããã䞊è¡ããŠéå§ãããç¬éãããå€æ°ãŸãã¯é¢æ°ã¯ç°ãªãã¹ã¬ããã§åæã«äœ¿çšã§ããŸãã æäœãã¹ã¬ããã»ãŒãã§ãªãå Žåãããã«ããæªå®çŸ©ã®åäœãçºçããå¯èœæ§ããããŸãïŒã€ãŸããããŒã¿ã®é害ãŸãã¯ç Žæã«ã€ãªããå¯èœæ§ããããŸãïŒã
åæããªããã£ããšåŒã°ãããå ±æããŒã¿ãä¿è·ããã³ãŒããã¹ã¬ããã»ãŒãã«ããããã®ãœãããŠã§ã¢ããŒã«ãããã€ããããŸãã æãäžè¬çãªã®ã¯ããã¥ãŒããã¯ã¹ãã»ããã©ãæ¡ä»¶å€æ°ãã¹ãã³ããã¯ã®ããªããã£ãã§ãã ãããã¯ãã¹ãŠåãæŠå¿µã®ããªã¢ã³ãã§ããã³ãŒãã®äžéšãä¿è·ããç¹å®ã®ã¹ã¬ããã«ã®ã¿ããŒã¿ã«ã¢ã¯ã»ã¹ããæ®ãããããã¯ããŸãã
æãäžè¬çãªïŒãããŠäœ¿çšãããïŒããªããã£ãã¯ãã¥ãŒããã¯ã¹ã§ãã ãã¥ãŒããã¯ã¹ã¯ãçžäºäŸå€ãã®ç¥ã§ãã ããã¯ã1ã€ã®ã¹ã¬ããã®ã¿ãã³ãŒããå®è¡ã§ããããšãä¿èšŒããŸãã 以äžã®äŸã䜿çšããŠããã¥ãŒããã¯ã¹ãã©ã®ããã«æ©èœããããèŠãŠã¿ãŸãããã
#include <SFML/System.hpp> #include <iostream> sf::Mutex mutex; void func() { mutex.lock(); for (int i = 0; i < 10; ++i) std::cout << "I'm thread number one" << std::endl; mutex.unlock(); } int main() { sf::Thread thread(&func); thread.launch(); mutex.lock(); for (int i = 0; i < 10; ++i) std::cout << "I'm the main thread" << std::endl; mutex.unlock(); return 0; }
ãã®ã³ãŒãã¯å ±æãªãœãŒã¹ïŒstd :: coutïŒã䜿çšããŸããããã¯ãèŠãŠã®ãšãããæãŸãããªãçµæã«ã€ãªãããŸãã ã³ã³ãœãŒã«ã§æ··åãããåºåã¹ããªãŒã ã åºåãã©ã³ãã ã«æ··åãããã®ã§ã¯ãªããæ£ããå°å·ãããããã«ããããã«ãã³ãŒãã®å¯Ÿå¿ããé åããã¥ãŒããã¯ã¹ã§ä¿è·ããŸãã
mutex.lockïŒïŒåŒã³åºãã«å°éããæåã®ã¹ã¬ããã¯ãmutexããããã¯ããããã¹ããåºåããã³ãŒãã«ã¢ã¯ã»ã¹ããŸãã ä»ã®ã¹ã¬ãããmutex.lockïŒïŒåŒã³åºãã«å°éãããšãmutexã¯ãã§ã«ããã¯ãããä»ã®ã¹ã¬ããã¯äžæåæ¢ããŠããŸãïŒããã¯sf :: sleepåŒã³åºãã«äŒŒãŠãããã¹ãªãŒãäžã®ã¹ã¬ããã¯CPUæéãæ¶è²»ããŸããïŒã æåã®ã¹ã¬ãããmutexã®ããã¯ã解é€ãããšã2çªç®ã®ã¹ã¬ããã¯å®è¡ãç¶ç¶ããmutexããããã¯ããŠããã¹ããåºåããŸãã ããã«ãããã³ã³ãœãŒã«å ã®ããã¹ããé çªã«å°å·ãããæ··åšããªããšããäºå®ã«ã€ãªãããŸãã
ãã¥ãŒããã¯ã¹ã¯ãå ±æããŒã¿ãä¿è·ããããã«äœ¿çšã§ããããªããã£ãã ãã§ãªããä»ã®å€ãã®å Žåã«äœ¿çšã§ããŸãã ãã ããã¹ããªãŒã ãæäœãããšãã«ã¢ããªã±ãŒã·ã§ã³ãè€éãªããšãè¡ãããã¥ãŒããã¯ã¹ã®æ©èœãååã§ãªããšæããå Žåã¯ãåªããæ©èœãæã€å¥ã®ã©ã€ãã©ãªãæ¢ãããšããããããªãã§ãã ããã
ãã¥ãŒããã¯ã¹ä¿è·
å¿é ããªãã§ãã ããããã¥ãŒããã¯ã¹ã¯ãã§ã«ã¹ã¬ããã»ãŒãã§ãããããããä¿è·ããå¿ èŠã¯ãããŸããã ãããããããã¯äŸå€ã«é¢ããŠå®å šã§ã¯ãããŸããã ãã¥ãŒããã¯ã¹ãããã¯ãããŠãããšãã«äŸå€ãã¹ããŒããããšã©ããªããŸããïŒ ããã¯ã解é€ããããšã¯ã§ãããæ°žä¹ ã«ããã¯ããããŸãŸã«ãªããŸãã ããã¯ããããã¥ãŒããã¯ã¹ãããã¯è§£é€ããããšãããã¹ãŠã®ã¹ã¬ããã¯æ°žä¹ ã«ãããã¯ãããŸãã å Žåã«ãã£ãŠã¯ãã¢ããªã±ãŒã·ã§ã³ãããªãŒãºããŸãã
ãã¥ãŒããã¯ã¹ïŒãã¥ãŒããã¯ã¹ïŒãäŸå€ãã¹ããŒã§ããç°å¢ã§åžžã«ãã¥ãŒããã¯ã¹ãããã¯è§£é€ãããããã«ããããã«ãSFMLã¯ããã¥ãŒããã¯ã¹ãsf :: Lockã¯ã©ã¹ã§ã©ããã§ããRAIIã¯ã©ã¹ãæäŸããŸãã ããã¯ã¯ã³ã³ã¹ãã©ã¯ã¿ãŒã§çºçããããã¯è§£é€ã¯ãã¹ãã©ã¯ã¿ã§çºçããŸãã ã·ã³ãã«ã§å¹æçã
sf::Mutex mutex; void func() { sf::Lock lock(mutex); // mutex.lock() functionThatMightThrowAnException(); // mutex.unlock(), } // mutex.unlock()
sf :: Lockã¯ãå€ãã®æ»ãå€ãæã€é¢æ°ã§ã䜿çšã§ããããšã«æ³šæããŠãã ããã
sf::Mutex mutex; bool func() { sf::Lock lock(mutex); // mutex.lock() if (!image1.loadFromFile("...")) return false; // mutex.unlock() if (!image2.loadFromFile("...")) return false; // mutex.unlock() if (!image3.loadFromFile("...")) return false; // mutex.unlock() return true; } // mutex.unlock()
ãããã誀解
ãã°ãã°èŠèœãšãããŠããããšïŒã¹ã¬ããã¯sf :: Threadã®é©åãªã€ã³ã¹ã¿ã³ã¹ãªãã§ã¯ååšã§ããŸããã 次ã®ã³ãŒãã¯ããã©ãŒã©ã ã§ããèŠãããŸãã
void startThread() { sf::Thread thread(&funcToRunInThread); thread.launch(); } int main() { startThread(); // ... return 0; }
ãã®ã³ãŒããäœæããããã°ã©ããŒã¯ãstartThreadïŒïŒé¢æ°ãããèªäœã§çåããé¢æ°ïŒãšã³ããªãã€ã³ããšããŠæž¡ãããïŒãå®äºãããšç Žæ£ãããã¹ã¬ãããéå§ããããšãæåŸ ããŠããŸãã ããã¯èµ·ããŠããŸããã ã¹ã¬ããé¢æ°ã¯ãããã°ã©ã ãå®è¡ãããŠããªããã®ããã«ã¡ã€ã³ã¹ã¬ããããããã¯ããŸãã
å æ°ïŒ sf :: Threadã€ã³ã¹ã¿ã³ã¹ã¯startThreadïŒïŒé¢æ°ã«å¯ŸããŠããŒã«ã«ã§ãããããé¢æ°ãå€ãè¿ããšããã«ç Žæ£ãããŸãã sf ::ã¹ã¬ãããã¹ãã©ã¯ã¿ãŒãåŒã³åºãããäžèšã®ããã«waitïŒïŒãåŒã³åºãããŸããçµæã¯ã¡ã€ã³ã¹ã¬ããã®ããã¯ã«ãªããã¹ã¬ããã䞊è¡ããŠå®è¡ããã®ã§ã¯ãªããã¹ã¬ãããé¢æ°ãå®äºãããŸã§åŸ æ©ããŸãã
å¿ããªãã§ãã ããïŒã¹ã¬ããé¢æ°ãå¿ èŠãšããéãçãç¶ããããã«sf :: Threadã€ã³ã¹ã¿ã³ã¹ã管çããªããã°ãªããŸããã
次ã®èšäºïŒ ã«ã¹ã¿ã ããŒã¿ã¹ããªãŒã ã®æäœ