From 4efe56141a36d3119c86d6caced43f9881027188 Mon Sep 17 00:00:00 2001 From: alexeyha Date: Wed, 18 May 2022 21:44:11 +0300 Subject: [PATCH 01/44] Create background structure --- core/CMakeLists.txt | 4 +++ core/loader/LevelLoaderFromFile.cpp | 26 ++++++++++++++ core/loader/LevelLoaderFromFile.hpp | 2 ++ core/visual/Camera.hpp | 2 ++ core/visual/image/Image.hpp | 3 +- .../image/background/BackgroundImage.cpp | 27 ++++++++++++++ .../image/background/BackgroundImage.hpp | 27 ++++++++++++++ .../image/background/RenderableBackground.cpp | 24 +++++++++++++ .../image/background/RenderableBackground.hpp | 33 ++++++++++++++++++ game/resources/background/background_01.png | Bin 0 -> 2025 bytes game/resources/background/background_02.png | Bin 0 -> 1093 bytes game/resources/background/background_03.png | Bin 0 -> 4729 bytes game/resources/background/background_04.png | Bin 0 -> 6703 bytes game/resources/background/background_05.png | Bin 0 -> 4500 bytes game/resources/levels/level_01/config.json | 4 +++ 15 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 core/visual/image/background/BackgroundImage.cpp create mode 100644 core/visual/image/background/BackgroundImage.hpp create mode 100644 core/visual/image/background/RenderableBackground.cpp create mode 100644 core/visual/image/background/RenderableBackground.hpp create mode 100644 game/resources/background/background_01.png create mode 100644 game/resources/background/background_02.png create mode 100644 game/resources/background/background_03.png create mode 100644 game/resources/background/background_04.png create mode 100644 game/resources/background/background_05.png diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 5e3f53c..a648fe4 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -77,11 +77,15 @@ set( visual/image/shape/square/RenderableSquare.cpp visual/image/shape/square/RenderableSquare.hpp visual/image/animated/RenderableAnimatedOneFile.cpp visual/image/animated/RenderableAnimatedOneFile.hpp visual/image/animated/RenderableAnimatedSeveralFiles.cpp visual/image/animated/RenderableAnimatedSeveralFiles.hpp + visual/image/background/RenderableBackground.cpp visual/image/background/RenderableBackground.hpp + visual/image/shape/Shape.cpp visual/image/shape/Shape.hpp visual/image/shape/square/Square.cpp visual/image/shape/square/Square.hpp visual/image/static/StaticImage.cpp visual/image/static/StaticImage.hpp visual/image/animated/AnimatedImageOneFile.cpp visual/image/animated/AnimatedImageOneFile.hpp visual/image/animated/AnimatedImageSeveralFiles.cpp visual/image/animated/AnimatedImageSeveralFiles.hpp + visual/image/background/BackgroundImage.cpp visual/image/background/BackgroundImage.hpp + visual/image/storage/ImageStorage.cpp visual/image/storage/ImageStorage.hpp visual/Renderable.hpp visual/Camera.cpp visual/Camera.hpp diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index 282b5d8..0fc0946 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -81,6 +81,8 @@ namespace mad::core { float current_position_y = object_size / 2; Entity::Id hero_id = 0; std::string map_line; + + create_background(world); while (std::getline(m_level_map, map_line)) { for (char object: map_line) { switch (m_objects[object]) { @@ -194,4 +196,28 @@ namespace mad::core { return hero_id; } + void LevelLoaderFromFile::create_background(std::shared_ptr world) { + std::filesystem::path source(m_config_json["background"]["source"]); + + std::shared_ptr image_storage; + std::vector parallax_ratios = m_config_json["background"]["a"]; + + image_storage = std::make_shared( + std::unordered_map>( + {{ImageStorage::TypeAction::Idle, + std::make_shared( + source, + parallax_ratios + ) + }} + ) + ); + world->create_viewable_entity( + -1, + {0, 0}, + 0, + image_storage + ); + } + } \ No newline at end of file diff --git a/core/loader/LevelLoaderFromFile.hpp b/core/loader/LevelLoaderFromFile.hpp index e12e665..1683b8e 100644 --- a/core/loader/LevelLoaderFromFile.hpp +++ b/core/loader/LevelLoaderFromFile.hpp @@ -99,6 +99,8 @@ namespace mad::core { Entity::Id create_hero(std::shared_ptr world, Vec2d position); + void create_background(std::shared_ptr world); + private: enum class Objects { UnstableBlock, diff --git a/core/visual/Camera.hpp b/core/visual/Camera.hpp index 28b4b7a..fd8da6d 100644 --- a/core/visual/Camera.hpp +++ b/core/visual/Camera.hpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/core/visual/image/Image.hpp b/core/visual/image/Image.hpp index 37c7e02..9a78594 100644 --- a/core/visual/image/Image.hpp +++ b/core/visual/image/Image.hpp @@ -13,7 +13,8 @@ namespace mad::core { Shape, Static, AnimatedOneFile, - AnimatedSeveralFiles + AnimatedSeveralFiles, + Background }; enum class Orientation { diff --git a/core/visual/image/background/BackgroundImage.cpp b/core/visual/image/background/BackgroundImage.cpp new file mode 100644 index 0000000..9d96b8a --- /dev/null +++ b/core/visual/image/background/BackgroundImage.cpp @@ -0,0 +1,27 @@ +#include "BackgroundImage.hpp" + +#include + +namespace mad::core { + + + BackgroundImage::BackgroundImage(std::filesystem::path path, float parallax_ratio) + : Image(Image::Type::Background), m_path(std::move(path)), + m_parallax_ratio(parallax_ratio) { + } + + std::filesystem::path BackgroundImage::get_path() const noexcept { + return m_path; + } + + float BackgroundImage::get_parallax_ratio() const noexcept { + return m_parallax_ratio; + } + + b2PolygonShape BackgroundImage::as_fixture() { + return {}; + } + + +} + diff --git a/core/visual/image/background/BackgroundImage.hpp b/core/visual/image/background/BackgroundImage.hpp new file mode 100644 index 0000000..984b736 --- /dev/null +++ b/core/visual/image/background/BackgroundImage.hpp @@ -0,0 +1,27 @@ +#ifndef MAD_BACKGROUNDIMAGE_HPP +#define MAD_BACKGROUNDIMAGE_HPP + +#include + +#include + +namespace mad::core { + + class BackgroundImage : public Image { + public: + BackgroundImage(std::filesystem::path path, float parallax_ratio); + + [[nodiscard]] std::filesystem::path get_path() const noexcept; + + [[nodiscard]] float get_parallax_ratio() const noexcept; + + b2PolygonShape as_fixture() override; + private: + std::filesystem::path m_path; + + float m_parallax_ratio; + }; + +} + +#endif //MAD_BACKGROUNDIMAGE_HPP diff --git a/core/visual/image/background/RenderableBackground.cpp b/core/visual/image/background/RenderableBackground.cpp new file mode 100644 index 0000000..906fb04 --- /dev/null +++ b/core/visual/image/background/RenderableBackground.cpp @@ -0,0 +1,24 @@ +#include "RenderableBackground.hpp" + +#include + +namespace mad::core { + + + RenderableBackground::RenderableBackground(const std::shared_ptr &background, + std::shared_ptr position, std::shared_ptr rotation) : + m_position(std::move(position)), m_rotation(std::move(rotation)), + m_parallax_ratio(background->get_parallax_ratio()) { + is_active = background->is_active; + + CHECK_THROW(m_texture.loadFromFile(background->get_path()), + FileDoesNotExist, "Background file does not exist"); + + m_texture.setRepeated(true); + } + + bool RenderableBackground::render(sf::RenderWindow &window) { + return false; + } +} + diff --git a/core/visual/image/background/RenderableBackground.hpp b/core/visual/image/background/RenderableBackground.hpp new file mode 100644 index 0000000..e68778e --- /dev/null +++ b/core/visual/image/background/RenderableBackground.hpp @@ -0,0 +1,33 @@ +#ifndef MAD_RENDERABLEBACKGROUND_HPP +#define MAD_RENDERABLEBACKGROUND_HPP + +#include +#include + +#include + +#include + +namespace mad::core { + + class RenderableBackground : public Renderable { + public: + RenderableBackground(const std::shared_ptr &background, std::shared_ptr position, + std::shared_ptr rotation); + + bool render(sf::RenderWindow &window) override; + + + private: + sf::Texture m_texture; + + std::shared_ptr m_position; + + std::shared_ptr m_rotation; + + float m_parallax_ratio; + }; + +} + +#endif //MAD_RENDERABLEBACKGROUND_HPP diff --git a/game/resources/background/background_01.png b/game/resources/background/background_01.png new file mode 100644 index 0000000000000000000000000000000000000000..a4a480568fa798de5792566ace4239acc6467159 GIT binary patch literal 2025 zcmd6o={FmQ8phL_(KO=PN@=5N*V-j?LL*aq8pN)#M1xREY_XT9T8nZjMj1;=ql#8* zZ^bf}AfmLEiLsuhh@nFTHA=X8&&>aDzxO}LCA;{6E@1iWutIFoQ4063G4 zGP~gvJM*~=F}e&p_cJ$J;RnU%SU{y8G!x_oQmc7>1#tGJ)IFHwYnj0Q)6Z`RR3={o zHCl@svDsZm55_(WH5&5{A+v*QTVC%(9yMWGV?;ZN&j-7G*;s$|0Vvz}o;ZD)k@|;W z-cg^_^sVL2As$%nc13kit;xkvaeRXRGm!q4yjCd4so1^<)%-{#1opGEDbb{_PGV1* zAKg!neNyO!7H*u75;HKXA&8`dm6%&r_?V@f=C8d@&%+&r`iZf!^BRk zHfJELTv~GDymOe-iyXk64HRW~UBg8B%}do@N3`A<{QVR~WbS!wQOsvsmuVxQ1Ox(i zHNhZf-y$igd8JmE<{W}hF5 z1oEq?#s?L_`p=>UHt*Xh_R|B1`hTmu?R#FR6nRT5E~||+CYWyP!UkftsA_G2vWleI z-E{&#t#q~nMndLK(BYhPoq#%xs82YYZd=oj z{v1TzLQT40Uo2YBQ(`s5*muLSEzf;cZ+m9soA_in0IWDd+-ONWn$I}N-j>D`(HAJK z6VD2uo%7gwq_{06nizHSC3$f1tQYxPrQgz(sqkMmK`@A5#RS&9OfnDfHSE-HtExS4 z5`ko0uIw|{Bs_Gyh9+?~m14m2jtP>Zds=$)um9a zpPoMz)WMp4yml>VY|bbVv5f|qkvx13MyH0L5`dytiML4%no(8nmQ@kS3xQ9--%y&O*$mz0|nU^G{#x2cJ z;AFCE%7jluZkyI%yY;$ZYVP9#9~)bQ;z8_jQ(m7zT*Rn)Ku~iwbg#dsy|FskS^ zB>Y;b(^?~H6jB}oXb`48ZxZ`a7g;sh zv$ow{x(yltnU|Dom^!StGKAAK=}?|!N0|1xnriw?;iU2LY~SAj{N2t<8*O%v{+<$i zyP<=>oJgy~sM)+6`*Trlh4%I7ZjrTS(%F`RkWr&~pVF1(^%)wR*}@Co`OJgg&8O|V zFbbX8g8gK83X&EpStrU-;%!;^cv zcsx>1q4V<6=<1y6L5k`kiX5&Ul{BS=rOWL}K3ZQr@wR*&)grIQ@yeFGJY5RkEPMB= zSloz_tLx$r@>N3%yi!B=a9(o=quyV}C$58R&k1g(%az=?Td zuHc{HeJe*+h8}?kV~m3J&<9A2GSx;C*<{U3d#Dn1De*t8(nx;um_iI}(39!!$Qrxi z7%S9`Dzl`fXbxbIe^ELz-Y({v%i?aE6i(n_rZ{g_Fwx{l50fh2dT;G&aevb%5`gD( zlaJ^w7kB0m+?rFO?yf8;FA@^1ZM&@;K5juRqyb-bmY7W$r=;UWOv?|K;uOI50!0M= rZS(&seS&}WpOXJS*~)xx2CJN`ORLV){+(jiNa$dl}w4u6HX?&=_QiYGtT$}kzGcG(!5rN^6JH#9XV7M7IWuL_|3A+w54e@-YpaWruzkSnOIk)))sMW~y{+EkW> z@-uJUpAz=>ee20Db$lN^geNXCo%FEN_o}Smtfvlc$?V^xxRlsM6+%hUI}+o~u9 zF?QXxlcb!4>=fjVU#jv^YFEi|s4-*LZC&zT{P-auLlwpMjV^ud5d~~_1AKl3c8W|g z;8sa8InwmPea_nCO_dJYxY{ig)I`}+&K7jAu05Kv_QD#u38se>CAKX1lFYg)Ug3FP zTY`ehNv5PshkqS9ViUH$%j{q~(s}YtLxj|YJP$vbP0l+XkK>7kEq literal 0 HcmV?d00001 diff --git a/game/resources/background/background_03.png b/game/resources/background/background_03.png new file mode 100644 index 0000000000000000000000000000000000000000..90ec81645704b2862f1fa5dd16c88cfe16c1818c GIT binary patch literal 4729 zcmai0XH-+|vVJj0=mDilFDfEcihzLtK|&Lx8G2QMB2|Ok-t-bfmerD#G^3IPp=Ato*h4C080DwhbPsa>^L&8)& zi=K}9RVw(rOce}1de%1pU^;s6(ty--9sn3kt|E~aFS>jCd*5*P_7T=cB87eYyxp!| zzXAYuB*PqIVZO+vzCE#_ZAidBGW0g%q!%{RzK7?CKOrf?z-B-Y$sXo3ZDBuuo|dmW zivWp@#oy;Nm14fjI0T&)$%zfmCER_pHx-=g@u_k9YtR1k&l>Y(>lrnD&}Mq(L_Gyl zB|LMnwjleL@UHgGsaYA#P)2bdz(!wlS>y&;j0WuB;PBI;&Cr*C2K%0g9yDJ_YnJ1W zrofi8Q|xH)5j4&I@hXP&cn+W$6suhTG<9k48ObNiK^84=dh6;k11?Gfr;~TSz6bb> zos?i2VEagflO`)32p_#d&;jn}Kw00VyXS$G9N=&>Y*z(C(m+lh?WPB+YC&@^8&fr) zX9jYngve8XHW)Z{i-}=DL<->0T|>iHPgk(bOHwD5QVp*cS3MtUOE2vUMWbZ|C3^L_ zPje_cA97CBlxYo2;XMY`Bj(R5XoYZdD$=F`jir!x3SQ4wF^wKLjil~dr%>G?;xV zyOxRaTj0HDyS_`kl_bZE=@H*poz(m5XYxg3v>%@8I6g?ZJUuD32ho^R0#(;EXoBOV zi|kz?TZSjuto)m&w*gpd@NW4aK@Y*ZhfnndP<|lDsI21v@23CI7XSwxaT$v@HHa>H z0CciKC9AafHd=V3n(6pk4v)1k{dQ80&=zlR)n?OXyo~4fb2?QPrY#ZGQzanjB>O>A zK(X1>Ju1P6Ri@Rlj#b?Uw(Crv*&@*x%0R2tO@Gu`WQ0KDa@R^MmYzE$az~^^kM_tt zapBhl4pULXcqKh~v%40;mU;qf>VAh*qwREG#GmU1XTp9*HKODng;txYe_(!&tS>ry zEJ8PjssFOSd@7MEE3Mz}*)jF|%GvY%rS7m1q6V@{u6cn6s?dI2PNuU>`>Bc)LxStu zr=n%drJcvBE|ozVVm4j-o(9k!2|eG;piCQ}E$l3)rDtViWjv%;B+M_a$UeewmH`rB z+Imtnp~&#ri5Kj@+Id{g$VXf0Nr`#m8BZInw zUS?v8QpIVCddI{PUXNV%sD`ROk`g}M9XXpZ+cv8(%Qq{sX3Lo2qUoErV?Hfm_U4)a z?;P8l;vC(TrD=50QN^37sy*Rc`iA-CIgj5TbLV#7 zzFN89|AKw|v7m8MKYH-(B5EON0kRRm%o4>J-bKJj@TT!Vc`&>IJeA2t1-$R?@pf6r zot6KVJf8wf@wT**BuU=pjDN%U*x>Pr$9$5q7R4n6B@-n~mg|;pEr`Vq<{=fjmI~%G z#S9-3OD#(>E`BrTGshI4D~c|`6nR@9@^IGV7&_}Vqc#&{o@v%o$C<`XVJ27J8myFv z2TN(x%}}NG6Hj}FZ{Lz^NG1z3+*2dA)qtKzB}pu#tickg-PqU zyAvEgPEDWUD^N75m%n1?QIeRHXjQ6LYFdi=)}&mqT$oYNbiToQFO*fQ%iyHlNfd_I zdpKj>eAs-vkg<^Kg1Xvfg-nS@K3{f&PQWX#!jZX|zLlkoeT{B;M)~ni4do4m?1lBl3C5+FpD=Az ztEAVY;#%ISxgomI3X&%Mqljj8RZXj@5WN(gxo@_?)kWr-eUEfug4z}l;#-Asu~ts{Lw)dD?pvh8t6VBYd53FUBzJtxS@JOd4P3 zcz4sRf&Zqj{Z!E4W=2{~TI+JrXyuCX%GH(Vu_x~opPb6HR=CmM){XpPIyZn|dh%G# zW3miqf;0)mJf9bnb(M`rmw&FR%?~KqcK*Swv8CbnWJY5KesWglmSqPKTd;z#Dr>v+8)~53A!!vDi)zOdS$D6cvtucrGSAk z^fs&*~iOeqcZDSFy|yDYZmRBZ9HYqn}!Al0{tikFOmKI3CLw z*ecM95tSWK9v}=M?IZ<_*YqSW1U>v7->4KQ-l32$HCG>TvN*2Mh{b3;O)Zlv^_H;b z8FjTL`S6GFalYCmA{P=!m?g|tw_9(^u0&SvcX-{al@)vpeeF>bS9aK1nvtYkuwY>$ z@mUgDjk$t$d({*3Mm4cC^K{ShyP7wnac9$iJfSQfX+1Lakdz-~h?W+x`r1@>O|=^C z8#u~1!+H30)Frwyc->u+q~}MI(q(i~1+M8t&;^fBhn6>CEzfB@zA;%c&P9fN8~eO$ zZ(=2DJ^9>uH!6cqgEQuC1;;3N66c*Ei81UPU$pm|S1DtUo)wq2*I({)8+2RzIw75D z%bHk{^figqi)DD^8O!9QXUm&g=SO9edL%|0!b!;b+*k0r#w&%ddwb3yEb=TiEOsrF zR|dzQJAdBs8d%{AZroD*{&Cc{aht61`sJu4jpc=pZXd7g>F?ESBPwD_7IzZ&JnhSk zJ$;wEAcGJxRE(AMx&FgPfkAZ(GvcSy-%zYwLNE6-egsIfD6?2TNbj5$^U(>MUEF4v z+!ka}8@Y8qxNHjgUg(wpMnLwOcIsNHZkl=Oo~?`R3zK&m1D}>Af_|e$g<$E5aO*&W zb&J!2sC%MLPekaMjZySaKPOg+Unlw6YXfA6FBgdmZB5e1^hJK9Qxz-$z zwLUP0+NO&P@7s^xB*Kt~i7CXlQ7lp4GF3EGRi_btaC6Dx zc0PXl+&hpwke#%al<>;)c0PHj`5SNLXlHAu;LF!o1$l>!OMA;L0b>CIKXNBwCbPGN z??_PGe?8qa?=Rd-;7v%N&_a^ef4eG3k5ccO!mfH|7XS!44gfwJfL|1fC)ZI?2%Mn#?_1>Gft9uh8N#U>^qI zu40vXrzr>9t`FXu49Vn$Zt1zwTQ%+qfQPOIR{@Y%oR$HAnGG}p8X!l#NI_`-s8r-Y z{b%Iw?gu`9M-I6D5%~}0zl~J!zmR|D`#&lFsaPHnYJ|@BXd3Mg_{$Ga#c^YkMB7&92x=|tPlYrh3>1quC_gvnq%TCy?&f1r zaNq4tNIy}p@sReT)5-QzLpKiBY?ZhzU2~ZVNnG(--}$1PerxU!wN*NLb=tfVY#Gbz zO@^0WGz=ALgVPxaB4M}%)H58GDJnI8#QV$U+(#w(;Z@9qCy}!nlDhN@s0iGSwcLke zu^h{~uUxYqxUJ6ZwIVTv&W(x{FNSA%mTi=wEf@M$= z@M%C5vXlFX1oBTt`U)%Kx*XM?eQEqc8%Tuc*37d$eEQDKFNV18ewBc|Ub{|~&fKe& zvffvF0w;Y)#=lfKSRAyzMN`XzLGn70LF>)=^|H>_q)Kv3V+8KV-#n{L4lpC*<#d#L{mgMkCW>*f>dB|CsENNT>|cpZ!RQnt8I8M zp|AweP|7W)gP_*F^aIs1c~^aI*CHSkLRx&kLWAgVuvhGGwPA7;$UVrsypj6?t(p1c zWq}aIYNKL^-tYJye3#u1Kc*wLmn~K2V;l0&au4ITIXF*00HY2&nXED=oqQy_=qLUG z(+c+#8&OpB)n9a;8@WS3_4F}LfvO^!;(bXY_!cT}OC;w{s!g$ZP&^$nWB~ijBP=Z9 zu+K?!L;a~KH<)YUjM;ncwxgO%fbc;A&epElRAKm>_7ZsSYyISXHxBTK=|1eC(T6OiCD`Wtk6dIFm4PM_iU|MO=mMo{Ci*g&$=$c>xoxIR3m&PWP8SN#m5fTmN?nw zHmy~xK-!f(eZc6`Spg9@gnPllupCW648f!7Tg#Ly`3J+a3t@+?N(e;vc@=g|{Q5vW z78vC6`qY>oJ%2iB=IQL}O}ccG1CE8haiP2qm4mJ?`s2UrOa%s54sX7;cPH!;mv@Z_#G`c5e)rfS(V_5 z6{~y%$N-X;faSm0G43!;5LCf!WvL5s(AAyB$asK+ZyQ z2ZUn5E)5+43l^X~>s&@HSbAud&`+UCJcpd2!EBo00x9JpcfVx;mQ1 z0C0wn@;#T1hVpr{0KZ0gocGbO^alV2*3&l?kdeg&0Q5#~Q0TpTuHFIO{;u9We7aC5 zpO2rni<^ft06d;RnZQj=mN_7#>1_?Y=aFypyp7rE_>47PMY1IciSeIjx%-?yZ=C)9 z=ZkmloC9^|K0g~D9~sAfU;M&L`Z3xC{&(?D-#>r(^>7yX-hH%@^t11{2CuSIx`o2v zXj|znr0B@qzZrRaU8f3OgUx__oUeQRmp^AJ))f$j=TaPZ14NybGYZL%#T2zs!G$9ip_Z;xB*U@1f zxF-QT6pr{g3`C;#(vehv^&5V6s@x=ikJb6PCg7?Bl;R$|yaQNB18gpO9SXph1R$+z z?xF*HtOZ*8Ss1DSx(k5x{pZo5z&RxFuv zJ5knj627$N=90W3{koht*ubzeunaZHwvcpg*(iDDA6wrjHA!Np>~@R%xSy%KzaJcr zWwMu8X`wl}=HM`Yyof{lr~$xiK=|OXxD@7cq!LY}=kYti^>eOv47q`%S1wh|YBfOa z@`U-8&nY)|@)GOD#}^kC#_o1#Si`=WhaJP)>|4#r9{b_SN2Hw}&2!hIWNo9g&+Ywa z`?js0e+BpY{4ECO2RNC2?e;Qjf#2;JF8*Qc|%;FAqivUaW@ z5b2`(+7|%qG_OmVe#NME(E)&FZj{(ZbZbC+IS=j(aScK@1Q(oG#1xyn&ex~(~VZbRsCL@6(H*>(R`Z6RAoVPT9c4eMOP=R(y zxBlXymFw`3mh5cTsJtzHKH0Ii@>=Nybm!%d4@%EAyx4KXRR*8C6m_TdJosF&1|N)9 zUB^P-!f;Hdi0{gE`HK_hZ=FB;?0%c@wd5kbYN7gz`yE^kvNEw2I^qJOml9Z|Sdh=5 z9k;GudI6Ocu1h?lTlv9s=+cnZke9K*vUqW(yv}8T26TgPQM$J(D>RorX%wlfJU*D{kcnJ+&7Jq2x6g1SdE0hz#ym5O3Z;6>i1svp8F$uDRu z+x6Rxpdaq%R@%=ucJeVea$_Eg^Sd*mV*;aYb_iAWjfVz_HKdVw8eTz4it`kpa(Cs- zF~(bOTvTc2(|q@`0u0YDi4>-8g}?VHA^k*Ii*7VQqLqKQ(H}WZYz`kiL+^0HU~4E?MH956WxM3~uo%Tu;kx zwu{Wx_b!|cX<9nC5++49C3_we?r2}Iput?YPD8lR&!zn3rmwd$k5m`o&wZ$EyBahf z_7;{p`z)z$Q2G6c)<0Uzac-S9^VjD+J3Tw!3(*Mi3+WlG7_4U)8`Kqu7AWZ}>38*g z>wDKXntn4AB7>J*%3R9CXU^0Ee*T2Z+i!ata z6E03{)MwP6%2dqe$Oz)|l!YiZ$vl0Xl<2Eb!f%j_g|EPWx&-x>I!9L>fAM-%E5-Yk zw#OZlSW0avL66la_+@G(f)}H$f;*eLwDi6Bs*r-tzR|b#4fBT zqdV%Ha4thG%Ri?jvaOj?N>YEOGI=qMPgFC`Jg8pV*}X%MO6?ONG(5#Z>)y90e`<6t z?CI}QQZ@Zxx@~%33SJ+Ys)6D6yav}n$i`jy)e(YqBZ;ii(@Zd%Nch3_(nd6Gm?kwVArEHXoxlf+ZNijAs92 zb+v$VrkgLvRPQjat;HZ!y`O)?Ix9jxn(zNrk3u1q72 zvhG?N3s3liPb4puSuN=+J}D9?xlG;JwbdT7ujwn z0)ov^<$2b7T+PuHvmkv3H6|hHEH3)$7Xs~9kEm;+8W48{!I@6CdK`!$k)PvE;_Aom z93PZ9m{l_In*xBzLAy2ph*0_}YC0hD%sJp*McH~`w?{_qrUN)q$l^f z3TQ5X`=#Q*;dwJz_s}@J=a7G~?I#6@TM0smxrL|QxFLRNSbR;P&DU#vekLpX%9LkX zv`2~{$mywvh1{9VGx?+th45wa)M0Gb%Fpc&2fz2x%rq3NE`2oA-M1lGU!*$_HfwbT zjR{wUIqY)E=H8F4Y5uZ#%~%CX@kna|UeHcBe?o6~h5LhD5q{Hl*_>O`j?BU|`pU~g zsf=7v+0h zam{#h9s*=PuqlzWPRTQYX?hs(;AmBBda0kL_94@ahzD)gg!c2Bcu3)?Gue!gtUc;2 zr>Di$U9bFg!Qw5Dt)7+!p)dU?Mhpkv#&ol8Fkb1Uwo2EudS25r#W4FnPQ9^KcnH6} z;bJWuJwh}eS=m7w5W04o8|uf7reksT=wR;|)BHZ_B*rRr+(|s^2Q1I1T&*laM*+_9iAzKym}lE6297VfwY~b;f97$7P_6{ z;kuXp-l@GyY6j8wR7?;gB%wA}ph4tE)t4Nn2R0l7D6XJQmQ}~~ zL-&906O(%kdg3O1f&(jy`eKLX?}7sR#}9jwC7_5(58|Tyvt9?`jIe`aN4ZG_r>zfp zYjqpMc;D3!LP1_g7?z$W7=odkV0G{X(%zPNq2Oe`bGx_zA0Cbt#!P~Oe%v_bW^Z7V zwsgmIlq^5dJwchbEbG>Nr@i+@S|@&fyqO{F=cNEooNa|tSEV;`L19kBL^h{#*b6szTa>o~PHN?G;pBvtKI3dOyM33H8 zj3X0k4gCD!S>+IBgIbBvhzCPgPBr*1LfeXqJklJ6zbj79+u~mu#T>up$NNz1^+p3C z4kf@m=8vrM`0Zu{^LH4PYphtI6ud{bnaJxk%2651Itr^vi|oNKn~#VwDd7dMOD-;< z2x2ag7$36gYrVDlY0l=AgV4LLr>1vs_L^+4&o%$10KW{XgMs z>5Tp!U+my|Vb}+6^)eRT^a~j!ICka-1)AKh4x8?(>f7YswdESh-hbVx(CmukK@xnB zt4ULK*|C&cQGwy&-zqVDjj_z8#x&!sYgg;dQ^CrOXxK1p{`R1V*DquCYL{^QJhtIz zN|5?E@f6I`Rj_X!Y0gYKJ4kL5SH-pGH+9ewe1>iJv_E;1sVfwkj_~*()#4yz-ZM zP+?E~7u)QFe|gdeV+?H5Ogh+{(>jNS3*=AUxaXX}AX%51oD;WPflg1ESfm>M-CR$j zmxK#<-9O`PU1*;$mdy`|9hT}-$(f$0qS+H`O{FV-Gtt>=b(Tvr+x21b{HPJ$VDPH!aMRkDQn)YP)?V+_K=?qrH+!xm z-hK+MI8%%IJx(t9c+4%BLxAB_bE@IvW>6`(FU@Wm$qNT1eLqymc}GO*AUrXlFaFN8 z^8&|NMRsswman5!ghP%ZMtdVPden#EH2ZJzYTa!ktdZh@bL0O~h~p}0U`I5yVH%oB zEZ=bNIZf`=uAktaOpPkY&|LgB#Xw$9JdLnThsc$QF?75M*X7L(1kz8Fyku2prQ4pE7;s;YqaG5)0PrBex)6o zegd7qh_+r14~JBfhPE(ch}EzbF5Zaa66aH&sDhPxq&ab~CvsQoh3pN+Ppd7p_&@jQ zIV%LI%*>XY$j8f%9A15ouU#AK^1wCcY?bd`sj7GEt`vsk)o1uvp7woJvx_e6Ijr`H zD50!Hv-!2f5Qw(X(mhC)$IO!hmZ+iKac2U9>ONUTIPADJKl)ijYT&^R-I~H)U~QO8 zumUrsVRkbTU44XOf)ISduo*{)t0IC&UK7AXr>y@vy${1&V%gQ zj-erkjqCggq(4g)eY}9}OnW(OG)DDmS=nFerIbKgJX8ulUZd(iI0v0VuKzj(2Tdtf zm@=F0#M*-Bc8Wuj?S?TC<~=U=1h@T4;`IQ0SK_YU?ju}zj_2^OEc5N*CR}ALHkd=} zy$$jd7^Sb+y^>ge&?GnAJYp;2T-*Nm8j~_VF_5{?@Wu%A3V~vGJ}1Aqf~J;0viHSN zPAOA|D97^F@~tv_Gs7X4^5?_CA;d{;R;ejg5+v%ZD*RV`PVHRdzgwsPl~IN&6vFx% QKzZrjG1M&Ac=+VM0C|+3W&i*H literal 0 HcmV?d00001 diff --git a/game/resources/background/background_05.png b/game/resources/background/background_05.png new file mode 100644 index 0000000000000000000000000000000000000000..936c5f7a1a85aa049912c375811ce26feab4f47c GIT binary patch literal 4500 zcma)8XH=8R)_wu$U79o{Qbl?drH9_Dbm;^{q$)@W0i+3nfOG?>hh9W_DAEZC1nC{b z1dy&215$FM=bm-fy5D#1y+7V}_Uzf^+0UL?Gx0_Sn$#3*6aWBFYip^Q001EuUM?Ub z!T+U;rg!iIxsR5WKLAiN{Q3xhoL5%>fWpuN3^p=y_YUy(clY+;(guUMeEhuKJRZ0L zK+qiA6k=w&!K{E?I#kt-gT2)CHen*;GEq%{F{bbdg2?G~;y|xwnT$K>)zvSt_ZP$w zB_+WUnT&;~o>9z@VnA<_qTa?m`*^w%^44>z6}vWkQ9rG?UU2}g9U<)?qsq{dG?s=@ zm8xE){~U$v>07~wC`VB6_yBZdwJsq4b8Z6Q7%DG+^LhvA2Y?`GoRSRa(9i7<Tml>hot@Btkuc!EAHFsYz~IN( zAq0TUOAr%5K?=ad;2Ng}xZeRPM(#dS2Q0+^MmOCaIbcQ@5Yx7B(*mj+fesWMWeq?^ z1&A5PMc)7}g#Zry+}uIH(`l{!i2B(;5-OLQ-wmwBsI|33vafQ;I2D4p)XcP z%6*gpU?l)LdLblQ%Lcnc0`t0f!@GOQ{T^k(BW!|O4XtuLP_Qv)ap3cd8}-*O5VNzZ z7|e`LkE)I1M~kovNSA$w#rcC1sQelB=<|m!JP{Ih5t^5dKX(ls>J@T~q>?{%T3Acg zJZq%HopFv|@6mQbSPC+tjW7EqspTXdNEBX=S53Xq%R7d1`LZl@N~E|f4OHJ(BnU|n zF0pea`k~8DXBp7(1q%TCE#93I0%Sxm_o$VTK-`|vxn=<`0CUq$^#y=?YCIxlA8VD6 zWB{O65FuEt!hYC!MW};>qmy`{lk&_#=BX-APnRm4DuoM-!_VPHMWm`g%y2cQpo8dy zGN)9Bv3pFK4~(j-a_X0;gw|+(~3C+0n!wI+iJLq#<1*Lc+*j};l#8|1XimA)4@g9w#k6XQzl^9cR+ylw2O~;k z*`8{=q5R|$AfA)VT#)<8uZ~S2QRemfr*e1NxnxB!Qmo_a6;jEb2Vx?9-Ky_z36ZBc z55B)%K~>(zR(-dEs3rc$dE|ZIrOOfO9po~X0#&&juc~NS>RB4hXq9kr@JP|mk;{@3 zJvHv)zn)g2TgTT-f6{ZsNkTlT)uJC|yV#xJQ=fay(dqAMt`v`~4qgW0%J? z#vYn*ZwQs()_@D&@8$1o@rQ>hPk9E zx|Dn(qujgN7y*;|MkBicowKhly zzmJ z&}U1GdpEw^U@wx=Lx{WDdX{BmW>}VMl^d69Zneo&?G(d{+SFUDP9tbkkUIRf{F;zt z6fu0=bk=mSn4*|jU*Yyql}MRqA^YnVwZQg=#k*N~AMU~O^t_9gp0ut1sAIAI;J~jt~^aajLN^A)OfBzo9NMJ zi{?Rl^?CKZhI>5=p%}56y4FgqlX+G8h$f8l`WmCAU-Xzp1Ynqox9xV zU>#&F)KJpUhRu`aUOZD@TMZ7pY2XlZV3SF2G8ujHL-scb2xFGd)o8If% ze;Y<8;Kfmoe@H6-MxK?V|EKPL@Mq)IQ6shE2lm|_;5%lZSCsM~X$AoSu6@PSWv_*_kl24o^+o*V^bl~U$*ne5= zr&@;EQZ7W?Q0|?uj;sZOSq->16>X|5y2{%hYvn1pY0sP zeu00XM2E0siT2z1Jb8`kI*cx6E)}^fbUQ;?FV5iB%Wo=lf+fs}y!#A+yh#*Kx;VQa z*F{HVM&o9{wt`m;_O%4{gHyLtTBV=x^hy>Ats+(u!Lt%`rg)pb(T3f|^xf*%jC-W3& zCgPhhcS$Pbo1QfYdQBRZ?^tA3LEHF(^*tl*b$*QOtS9i?qBN&ijSkydnBK87v=p^k zu6O(y17}xcihowcIM0&F6h0%c5VXo3>;17kd*NkWX?YLAWyEdFZDVaoIM0SAqbzeR zljb4y>|7o7^4+?fqaW(?qM5@2^DR*gV8q*Y`KDIa;sMm~9VN3r%nr?dn#t^rE!I0u zA3q%3We;inA+K@yD- zos)9YG)yBVCOO9x%uJG}ZfG-wBoDfdY`>HwnIU2d9Ij1&GHqC}ZilbATOTlNF^Veu!Ny%G1(K#?1@R-|( z98ZJnk0ip=;bBmblLd$QHw_`{&Zy=4wBCEobO9@4>bQjUJFRz6w);&1`!(~c3>Hlm zIqz=Udu;}6%3q|78zNg?wd7PGY&{XX!EN;+ZrBa>JXzG@VcV1M%($t^JUNBMwu4}A zY%lg`nRyZF$A69*Y?%$@->qylY_&gB-cgwaFUC$P*<38l>@9@f?+7e77+uiBl0-+1 zT%;T&(}IbUvy%s7sAIPBZYj#ieNpl|-#9A*~AuVIW9S{NCZ+)1A)1g+R`|x68DKm{6{80i65E`y_04xdsPH^~f0{|Y00l=mM0Nl<302c35 z`(6zIAZgcDQ!xvg+g`LmrE4*g7(G*a%kb%hc7#6bxaPE!!I@@)Nx-3!CG7{+)?s?x zg2N#9x)T9#YkZRq|Ar+43Sa;rzy-hv34e=t%`fr))%=?|-saylzheETkbf=yE6HD) zf9Ct!kpC6a--i5enEr3!|0|}yW$v#4e~alqmT7zQ{P;(&G3=zLMrgnh$jiA_1^c!} zeEZasvX~r$G(7+ltPHP+ZxQO_&M5kmk*l$UUF@9imC-PyAMqs&(iv~iNmHc6lVts^ z3I;n1dH@yRvR#}S30k113=ri}+=+Yhy-i~c;*{!4x(~)L(%v&NypMZ(4ZBU;%yJoF zZ?o(vc;*kW^)vV3usH!K#h{u9CH5k7CXHopO>PrbSr+huQb;)7wqe_knlzUK*76wf zRv6?rR(tOTt>wCcCC02m%DUAo&EA8eB=y##Io;&xdt7#HQHayTL0Y^m-%#>VA<=2i zwCvz|zVS)3AEgx2)q~Xa8;jr7&?g4ZCHHUi{D3O%aI^%iu)Vy7N4X%Tv}>en9Y`j* z{375{-1DG zv$`6x02&GfkI@r^OswZ48ZNNT=?jZ?`wQ`GoY9OnAF=K0K(d`I zVPb#beT#tEh7D8&fL^r~++UuCE8-SfI&Zi>2GvI`G_$v!TCk26U?+aEhVCSa;#tAx z8qK-piB7nKzG;XUbT79^X?~sJl|j2nzT~#19RPAxybBuw!T^xv&~T96uI~f3_74k&iP)O zKi8k!sQXvDSwjbDiXgFK;}Q4mj1+JNopGE)#tGqvu7-{Q?VS4B8o&33K+bnY`oWPs zdnIYiSB|{8i5)2FDrO0Z*gPsm(fOXFIv(tAVe@(Lsq2OYrcR8}r;hmDMUd?% zYPaq Date: Wed, 18 May 2022 22:56:56 +0300 Subject: [PATCH 02/44] Change camera position --- core/loader/LevelLoaderFromFile.cpp | 2 +- core/visual/Camera.cpp | 30 ++++++++++++----- core/visual/Camera.hpp | 2 +- .../image/background/BackgroundImage.cpp | 8 ++--- .../image/background/BackgroundImage.hpp | 7 ++-- .../image/background/RenderableBackground.cpp | 32 ++++++++++++++++--- .../image/background/RenderableBackground.hpp | 6 ++-- game/resources/levels/level_01/config.json | 2 +- 8 files changed, 64 insertions(+), 25 deletions(-) diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index 0fc0946..0c5fe09 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -200,7 +200,7 @@ namespace mad::core { std::filesystem::path source(m_config_json["background"]["source"]); std::shared_ptr image_storage; - std::vector parallax_ratios = m_config_json["background"]["a"]; + std::vector parallax_ratios = m_config_json["background"]["parallax_ratios"]; image_storage = std::make_shared( std::unordered_map>( diff --git a/core/visual/Camera.cpp b/core/visual/Camera.cpp index ed92d84..0fb8c1a 100644 --- a/core/visual/Camera.cpp +++ b/core/visual/Camera.cpp @@ -93,6 +93,20 @@ namespace mad::core { }); break; } + case Image::Type::Background: { + std::shared_ptr background_image = + pointer_cast_to(image); + RenderableBackground renderable_background(background_image, + m_position, + positional_appearance.get_rotation()); + + insert_renderable_to_scene({positional_appearance.get_z_index(), + RenderableWithId(std::make_shared(renderable_background), + positional_appearance.get_entity_id()) + }); + + break; + } } } } @@ -102,7 +116,7 @@ namespace mad::core { } Camera::Camera(Vec2d initial_position, std::shared_ptr world, bool is_debug_mode) - : m_position(initial_position), + : m_position(std::make_shared(initial_position)), m_world(std::move(world)), m_view(initial_position, {640, 480}), m_is_debug_mode(is_debug_mode) { @@ -117,15 +131,15 @@ namespace mad::core { } switch (m_type) { case FollowType::Forward: { - float move_x = (position.get_x() - m_position.get_x()) * (2 - m_smoothness); - float move_y = (position - m_position).get_y(); + float move_x = (position.get_x() - m_position->get_x()) * (2 - m_smoothness); + float move_y = (position - *m_position).get_y(); m_view.move(move_x, move_y); - m_position += {move_x, move_y}; + *m_position += {move_x, move_y}; break; } case FollowType::Backward : { - m_view.move((position - m_position) * m_smoothness); - m_position += (position - m_position) * m_smoothness; + m_view.move((position - *m_position) * m_smoothness); + *m_position += (position - *m_position) * m_smoothness; break; } } @@ -138,8 +152,8 @@ namespace mad::core { } void Camera::set_position(const Vec2d &position) { - m_position = position; - m_view.setCenter(m_position); + *m_position = position; + m_view.setCenter(*m_position); } void Camera::set_rotation(float angle) { diff --git a/core/visual/Camera.hpp b/core/visual/Camera.hpp index fd8da6d..5e5bf3f 100644 --- a/core/visual/Camera.hpp +++ b/core/visual/Camera.hpp @@ -80,7 +80,7 @@ namespace mad::core { sf::View m_view; - Vec2d m_position; + std::shared_ptr m_position; std::optional m_last_position; diff --git a/core/visual/image/background/BackgroundImage.cpp b/core/visual/image/background/BackgroundImage.cpp index 9d96b8a..f383ede 100644 --- a/core/visual/image/background/BackgroundImage.cpp +++ b/core/visual/image/background/BackgroundImage.cpp @@ -5,17 +5,17 @@ namespace mad::core { - BackgroundImage::BackgroundImage(std::filesystem::path path, float parallax_ratio) + BackgroundImage::BackgroundImage(std::filesystem::path path, std::vector parallax_ratios) : Image(Image::Type::Background), m_path(std::move(path)), - m_parallax_ratio(parallax_ratio) { + m_parallax_ratios(std::move(parallax_ratios)) { } std::filesystem::path BackgroundImage::get_path() const noexcept { return m_path; } - float BackgroundImage::get_parallax_ratio() const noexcept { - return m_parallax_ratio; + std::vector BackgroundImage::get_parallax_ratios() const noexcept { + return m_parallax_ratios; } b2PolygonShape BackgroundImage::as_fixture() { diff --git a/core/visual/image/background/BackgroundImage.hpp b/core/visual/image/background/BackgroundImage.hpp index 984b736..ceb78c0 100644 --- a/core/visual/image/background/BackgroundImage.hpp +++ b/core/visual/image/background/BackgroundImage.hpp @@ -4,22 +4,23 @@ #include #include +#include namespace mad::core { class BackgroundImage : public Image { public: - BackgroundImage(std::filesystem::path path, float parallax_ratio); + BackgroundImage(std::filesystem::path path, std::vector parallax_ratios); [[nodiscard]] std::filesystem::path get_path() const noexcept; - [[nodiscard]] float get_parallax_ratio() const noexcept; + [[nodiscard]] std::vector get_parallax_ratios() const noexcept; b2PolygonShape as_fixture() override; private: std::filesystem::path m_path; - float m_parallax_ratio; + std::vector m_parallax_ratios; }; } diff --git a/core/visual/image/background/RenderableBackground.cpp b/core/visual/image/background/RenderableBackground.cpp index 906fb04..bab27c7 100644 --- a/core/visual/image/background/RenderableBackground.cpp +++ b/core/visual/image/background/RenderableBackground.cpp @@ -1,6 +1,7 @@ #include "RenderableBackground.hpp" #include +#include namespace mad::core { @@ -8,17 +9,38 @@ namespace mad::core { RenderableBackground::RenderableBackground(const std::shared_ptr &background, std::shared_ptr position, std::shared_ptr rotation) : m_position(std::move(position)), m_rotation(std::move(rotation)), - m_parallax_ratio(background->get_parallax_ratio()) { + m_parallax_ratios(background->get_parallax_ratios()) { is_active = background->is_active; - CHECK_THROW(m_texture.loadFromFile(background->get_path()), - FileDoesNotExist, "Background file does not exist"); + std::set sorted_files; - m_texture.setRepeated(true); + for (const auto &file : std::filesystem::directory_iterator{background->get_path()}) { + sorted_files.insert(file.path()); + } + + for (const auto &layer : sorted_files) { + sf::Texture texture; + CHECK_THROW(texture.loadFromFile(layer), + FileDoesNotExist, "Background file does not exist"); + texture.setRepeated(true); + m_layers.push_back(texture); + } } bool RenderableBackground::render(sf::RenderWindow &window) { - return false; + for (const auto ¤t_layer : m_layers) { + sf::Sprite background; + background.setTexture(current_layer); + background.setPosition(*m_position); + background.setScale({0.5, 0.5}); + background.setOrigin(background.getLocalBounds().width / 2, background.getLocalBounds().height / 2); + window.draw(background); + } + return true; + } + + sf::RectangleShape RenderableBackground::get_physical_shape() noexcept { + return sf::RectangleShape(); } } diff --git a/core/visual/image/background/RenderableBackground.hpp b/core/visual/image/background/RenderableBackground.hpp index e68778e..ebc7391 100644 --- a/core/visual/image/background/RenderableBackground.hpp +++ b/core/visual/image/background/RenderableBackground.hpp @@ -7,6 +7,7 @@ #include #include +#include namespace mad::core { @@ -17,15 +18,16 @@ namespace mad::core { bool render(sf::RenderWindow &window) override; + sf::RectangleShape get_physical_shape() noexcept override; private: - sf::Texture m_texture; + std::vector m_layers; std::shared_ptr m_position; std::shared_ptr m_rotation; - float m_parallax_ratio; + std::vector m_parallax_ratios; }; } diff --git a/game/resources/levels/level_01/config.json b/game/resources/levels/level_01/config.json index 549dfdc..60a8c70 100644 --- a/game/resources/levels/level_01/config.json +++ b/game/resources/levels/level_01/config.json @@ -12,7 +12,7 @@ "zoom": 10.5 }, "background" : { - "source" : "background", + "source" : "../../game/resources/background", "parallax_ratios" : [1, 1, 1, 1, 1] }, "texture" : { From 6165a9c94f3f46cb3e5e673b57739060e2543d18 Mon Sep 17 00:00:00 2001 From: alexeyha Date: Sun, 22 May 2022 14:10:01 +0300 Subject: [PATCH 03/44] Complete background --- core/loader/LevelLoaderFromFile.cpp | 2 +- .../image/background/RenderableBackground.cpp | 24 +++++++++++++------ .../image/background/RenderableBackground.hpp | 8 ++++++- game/game_with_default_loader_example.cpp | 2 +- game/resources/levels/level_01/config.json | 2 +- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index 0c5fe09..e9177f8 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -24,7 +24,7 @@ namespace mad::core { Vec2d camera_position = {m_config_json["camera"]["position"]["x"], m_config_json["camera"]["position"]["y"]}; - auto camera = std::make_shared(camera_position, world, true); + auto camera = std::make_shared(camera_position, world); Entity::Id hero_id = create_world(world); diff --git a/core/visual/image/background/RenderableBackground.cpp b/core/visual/image/background/RenderableBackground.cpp index bab27c7..b6ed068 100644 --- a/core/visual/image/background/RenderableBackground.cpp +++ b/core/visual/image/background/RenderableBackground.cpp @@ -8,8 +8,9 @@ namespace mad::core { RenderableBackground::RenderableBackground(const std::shared_ptr &background, std::shared_ptr position, std::shared_ptr rotation) : - m_position(std::move(position)), m_rotation(std::move(rotation)), - m_parallax_ratios(background->get_parallax_ratios()) { + m_camera_position(std::move(position)), m_rotation(std::move(rotation)), + m_parallax_ratios(background->get_parallax_ratios()), + m_last_camera_position(*m_camera_position) { is_active = background->is_active; std::set sorted_files; @@ -28,14 +29,23 @@ namespace mad::core { } bool RenderableBackground::render(sf::RenderWindow &window) { - for (const auto ¤t_layer : m_layers) { + for (int i = 0; i < static_cast(m_layers.size()); i++) { sf::Sprite background; - background.setTexture(current_layer); - background.setPosition(*m_position); - background.setScale({0.5, 0.5}); - background.setOrigin(background.getLocalBounds().width / 2, background.getLocalBounds().height / 2); + background.setTexture(m_layers[i]); + float ratio = m_parallax_ratios[i]; + background.setScale(m_scale); + if (m_layers_positions.size() == i) { + m_layers_positions.push_back(*m_camera_position); + background.setPosition(m_layers_positions[i]); + } else { + background.setPosition(m_layers_positions[i].get_x() + (*m_camera_position - m_last_camera_position).get_x() * ratio, m_camera_position->get_y()); + } + m_layers_positions[i] += {(*m_camera_position - m_last_camera_position).get_x() * ratio, (*m_camera_position - m_layers_positions[i]).get_y()}; + background.setTextureRect({0, 0, static_cast(window.getSize().x) * 3, static_cast(window.getSize().y)}); + background.setOrigin(static_cast(window.getSize().x) / 2, static_cast(window.getSize().y) / 4); //static_cast(window.getSize().y) / 2); window.draw(background); } + m_last_camera_position = *m_camera_position; return true; } diff --git a/core/visual/image/background/RenderableBackground.hpp b/core/visual/image/background/RenderableBackground.hpp index ebc7391..e09e602 100644 --- a/core/visual/image/background/RenderableBackground.hpp +++ b/core/visual/image/background/RenderableBackground.hpp @@ -23,10 +23,16 @@ namespace mad::core { private: std::vector m_layers; - std::shared_ptr m_position; + std::shared_ptr m_camera_position; + + std::vector m_layers_positions; + + Vec2d m_last_camera_position; std::shared_ptr m_rotation; + Vec2d m_scale = {2.8, 2.8}; + std::vector m_parallax_ratios; }; diff --git a/game/game_with_default_loader_example.cpp b/game/game_with_default_loader_example.cpp index 6a43d9d..d8ef7ea 100644 --- a/game/game_with_default_loader_example.cpp +++ b/game/game_with_default_loader_example.cpp @@ -28,7 +28,7 @@ int main() { #endif spdlog::set_level(log_level); - auto window = std::make_shared(sf::VideoMode(640, 480), "MAD"); + auto window = std::make_shared(sf::VideoMode(2560, 1600), "MAD"); ImGui::SFML::Init(*window); window->setFramerateLimit(120); diff --git a/game/resources/levels/level_01/config.json b/game/resources/levels/level_01/config.json index 60a8c70..4bd8662 100644 --- a/game/resources/levels/level_01/config.json +++ b/game/resources/levels/level_01/config.json @@ -13,7 +13,7 @@ }, "background" : { "source" : "../../game/resources/background", - "parallax_ratios" : [1, 1, 1, 1, 1] + "parallax_ratios" : [0.95, 0.9, 0.85, 0.8, 0.75] }, "texture" : { "stable" : "brick.png", From b71d7d9f2c9a4732f0b9f440737b1a02e9c86f08 Mon Sep 17 00:00:00 2001 From: alexeyha Date: Fri, 27 May 2022 11:50:11 +0300 Subject: [PATCH 04/44] Merge main --- core/loader/LevelLoaderFromFile.cpp | 3 +++ core/loader/LevelLoaderFromFile.hpp | 2 ++ core/visual/Camera.cpp | 4 ++-- game/resources/levels/level_with_finish/config.json | 4 ++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index e8fb9a7..269068a 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -82,6 +82,9 @@ namespace mad::core { float current_position_y = object_size / 2; std::unordered_map keys; std::string map_line; + + create_background(world); + while (std::getline(m_level_map, map_line)) { for (char object: map_line) { switch (m_objects[object]) { diff --git a/core/loader/LevelLoaderFromFile.hpp b/core/loader/LevelLoaderFromFile.hpp index aa5f1e4..4994a81 100644 --- a/core/loader/LevelLoaderFromFile.hpp +++ b/core/loader/LevelLoaderFromFile.hpp @@ -106,6 +106,8 @@ namespace mad::core { Entity::Id create_hero(std::shared_ptr world, Vec2d position); + void create_background(std::shared_ptr world); + Entity::Id create_finish_block(std::shared_ptr world, Vec2d position, float block_size); private: diff --git a/core/visual/Camera.cpp b/core/visual/Camera.cpp index c724f02..e7a4e1d 100644 --- a/core/visual/Camera.cpp +++ b/core/visual/Camera.cpp @@ -128,8 +128,8 @@ namespace mad::core { Vec2d position = entity.get_image_position(); if (!m_last_position.has_value()) { m_last_position = position; - m_position = position; - m_view.setCenter(m_position); + *m_position = position; + m_view.setCenter(*m_position); } switch (m_type) { case FollowType::Forward: { diff --git a/game/resources/levels/level_with_finish/config.json b/game/resources/levels/level_with_finish/config.json index 6482a2a..ce6782f 100644 --- a/game/resources/levels/level_with_finish/config.json +++ b/game/resources/levels/level_with_finish/config.json @@ -11,6 +11,10 @@ "smoothness": 0.6, "zoom": 10.5 }, + "background" : { + "source" : "../../game/resources/background", + "parallax_ratios" : [0.95, 0.9, 0.85, 0.8, 0.75] + }, "texture" : { "stable" : "brick.png", "unstable" : "brick.png", From ce4ef0f03d72cb5faa21d4c1992a6133cd6597d6 Mon Sep 17 00:00:00 2001 From: denis Date: Sat, 28 May 2022 16:05:17 +0300 Subject: [PATCH 05/44] Add httplib.h in submodule, move resources, add network directory and cmake --- .gitmodules | 3 ++ CMakeLists.txt | 16 +++++++ core/loader/LevelLoaderFromFile.cpp | 4 +- deps/cpp-httplib | 1 + game/game_database_example.cpp | 4 +- game/game_with_default_loader_example.cpp | 2 +- network/CMakeLists.txt | 41 ++++++++++++++++++ network/client/client.cpp | 6 +++ network/server/server.cpp | 6 +++ .../animated/helicopter.png | Bin .../hero/idle/Chara - BlueIdle00000.png | Bin .../hero/idle/Chara - BlueIdle00001.png | Bin .../hero/idle/Chara - BlueIdle00002.png | Bin .../hero/idle/Chara - BlueIdle00003.png | Bin .../hero/idle/Chara - BlueIdle00004.png | Bin .../hero/idle/Chara - BlueIdle00005.png | Bin .../hero/idle/Chara - BlueIdle00006.png | Bin .../hero/idle/Chara - BlueIdle00007.png | Bin .../hero/idle/Chara - BlueIdle00008.png | Bin .../hero/idle/Chara - BlueIdle00009.png | Bin .../hero/idle/Chara - BlueIdle00010.png | Bin .../hero/idle/Chara - BlueIdle00011.png | Bin .../hero/idle/Chara - BlueIdle00012.png | Bin .../hero/idle/Chara - BlueIdle00013.png | Bin .../hero/idle/Chara - BlueIdle00014.png | Bin .../hero/idle/Chara - BlueIdle00015.png | Bin .../hero/idle/Chara - BlueIdle00016.png | Bin .../hero/idle/Chara - BlueIdle00017.png | Bin .../hero/idle/Chara - BlueIdle00018.png | Bin .../hero/idle/Chara - BlueIdle00019.png | Bin .../animated/hero/run/Chara_BlueWalk00000.png | Bin .../animated/hero/run/Chara_BlueWalk00001.png | Bin .../animated/hero/run/Chara_BlueWalk00002.png | Bin .../animated/hero/run/Chara_BlueWalk00003.png | Bin .../animated/hero/run/Chara_BlueWalk00004.png | Bin .../animated/hero/run/Chara_BlueWalk00005.png | Bin .../animated/hero/run/Chara_BlueWalk00006.png | Bin .../animated/hero/run/Chara_BlueWalk00007.png | Bin .../animated/hero/run/Chara_BlueWalk00008.png | Bin .../animated/hero/run/Chara_BlueWalk00009.png | Bin .../animated/hero/run/Chara_BlueWalk00010.png | Bin .../animated/hero/run/Chara_BlueWalk00011.png | Bin .../animated/hero/run/Chara_BlueWalk00012.png | Bin .../animated/hero/run/Chara_BlueWalk00013.png | Bin .../animated/hero/run/Chara_BlueWalk00014.png | Bin .../animated/hero/run/Chara_BlueWalk00015.png | Bin .../animated/hero/run/Chara_BlueWalk00016.png | Bin .../animated/hero/run/Chara_BlueWalk00017.png | Bin .../animated/hero/run/Chara_BlueWalk00018.png | Bin .../animated/hero/run/Chara_BlueWalk00019.png | Bin .../animated/runner_new.png | Bin .../levels/level_01/config.json | 2 +- .../levels/level_01/map | 0 .../levels/level_with_finish/config.json | 2 +- .../levels/level_with_finish/map | 0 .../resources => resources}/static/18plus.png | Bin .../resources => resources}/static/brick.png | Bin {game/resources => resources}/static/exit.png | Bin 58 files changed, 80 insertions(+), 7 deletions(-) create mode 160000 deps/cpp-httplib create mode 100644 network/CMakeLists.txt create mode 100644 network/client/client.cpp create mode 100644 network/server/server.cpp rename {game/resources => resources}/animated/helicopter.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00000.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00001.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00002.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00003.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00004.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00005.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00006.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00007.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00008.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00009.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00010.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00011.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00012.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00013.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00014.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00015.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00016.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00017.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00018.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00019.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00000.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00001.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00002.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00003.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00004.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00005.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00006.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00007.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00008.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00009.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00010.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00011.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00012.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00013.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00014.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00015.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00016.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00017.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00018.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00019.png (100%) rename {game/resources => resources}/animated/runner_new.png (100%) rename {game/resources => resources}/levels/level_01/config.json (92%) rename {game/resources => resources}/levels/level_01/map (100%) rename {game/resources => resources}/levels/level_with_finish/config.json (92%) rename {game/resources => resources}/levels/level_with_finish/map (100%) rename {game/resources => resources}/static/18plus.png (100%) rename {game/resources => resources}/static/brick.png (100%) rename {game/resources => resources}/static/exit.png (100%) diff --git a/.gitmodules b/.gitmodules index c319e98..952393c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "deps/libpqxx"] path = deps/libpqxx url = https://github.com/jtv/libpqxx +[submodule "deps/cpp-httplib"] + path = deps/cpp-httplib + url = https://github.com/yhirose/cpp-httplib diff --git a/CMakeLists.txt b/CMakeLists.txt index 928f804..1c50a15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,13 @@ add_subdirectory(${DIRECTORY_PQXX}) message(STATUS "PostgreSQL include: ${INCLUDE_PQXX}") message(STATUS "PostgreSQL library: ${LIBRARY_PQXX}") +# HTTP library +set(DIRECTORY_HTTP ${PROJECT_SOURCE_DIR}/deps/cpp-httplib) +set(INCLUDE_HTTP ${DIRECTORY_HTTP}) + +add_subdirectory(${DIRECTORY_HTTP}) +message(STATUS "HTTP include: ${INCLUDE_HTTP}") + # Core library set(DIRECTORY_CORE ${PROJECT_SOURCE_DIR}/core) set(INCLUDE_CORE ${DIRECTORY_CORE}) @@ -105,6 +112,15 @@ add_subdirectory(${DIRECTORY_GAME}) message(STATUS "Game executable: ${EXECUTABLE_GAME}") message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") +# Network binaries +set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) +set(EXECUTABLE_SERVER server) +set(EXECUTABLE_CLIENT client) + +add_subdirectory(${DIRECTORY_NETWORK}) +message(STATUS "Server executable: ${EXECUTABLE_SERVER}") +message(STATUS "Client executable ${EXECUTABLE_GAME_CLIENT}") + # Tests set(DIRECTORY_TEST ${PROJECT_SOURCE_DIR}/test) set(EXECUTABLE_TEST test) diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index 2201916..324c61a 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -133,7 +133,7 @@ namespace mad::core { void LevelLoaderFromFile::create_block(std::shared_ptr world, Vec2d position, float block_size, bool is_stable) { - std::filesystem::path source("../../game/resources/static/"); + std::filesystem::path source("../../resources/static/"); if (is_stable) { source /= static_cast(m_config_json["texture"]["stable"]); } else { @@ -203,7 +203,7 @@ namespace mad::core { } Entity::Id LevelLoaderFromFile::create_finish_block(std::shared_ptr world, Vec2d position, float block_size) { - std::filesystem::path source("../../game/resources/static/"); + std::filesystem::path source("../../resources/static/"); source /= static_cast(m_config_json["texture"]["finish"]); auto image_storage = std::make_shared( diff --git a/deps/cpp-httplib b/deps/cpp-httplib new file mode 160000 index 0000000..df20c27 --- /dev/null +++ b/deps/cpp-httplib @@ -0,0 +1 @@ +Subproject commit df20c27696c6dcb2b9ccecf3c4f9c3d06c1ecf8c diff --git a/game/game_database_example.cpp b/game/game_database_example.cpp index 48e0fe2..586b030 100644 --- a/game/game_database_example.cpp +++ b/game/game_database_example.cpp @@ -45,8 +45,8 @@ int main() { auto database_storage_driver = std::make_shared(database); std::vector> level_loaders{ - std::make_shared("../../game/resources/levels/level_with_finish"), - std::make_shared("../../game/resources/levels/level_01") + std::make_shared("../../resources/levels/level_with_finish"), + std::make_shared("../../resources/levels/level_01") }; auto game_runner = std::make_unique( diff --git a/game/game_with_default_loader_example.cpp b/game/game_with_default_loader_example.cpp index 337426b..a285992 100644 --- a/game/game_with_default_loader_example.cpp +++ b/game/game_with_default_loader_example.cpp @@ -42,7 +42,7 @@ int main() { auto offline_storage_driver = std::make_shared(); std::vector> level_loaders{ - std::make_shared("../../game/resources/levels/level_01") + std::make_shared("../../resources/levels/level_01") }; auto game_runner = std::make_unique( diff --git a/network/CMakeLists.txt b/network/CMakeLists.txt new file mode 100644 index 0000000..35f74e8 --- /dev/null +++ b/network/CMakeLists.txt @@ -0,0 +1,41 @@ +function(network_executable EXECUTABLE_NETWORK_NAME EXECUTABLE_SOURCES) + message(STATUS "Network executable: '${EXECUTABLE_NETWORK_NAME}' is built with ${EXECUTABLE_SOURCES}") + + add_executable( + ${EXECUTABLE_NETWORK_NAME} + ${EXECUTABLE_SOURCES} + ${SOURCES_IMGUI} + ${SOURCES_IMGUI_SFML} + ) + + if (CMAKE_BUILD_TYPE MATCHES Debug) + target_compile_definitions(${EXECUTABLE_NETWORK_NAME} PUBLIC SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE) + message(STATUS "Enable debug logs for ${EXECUTABLE_NETWORK_NAME}") + endif() + + target_include_directories( + ${EXECUTABLE_NETWORK_NAME} PUBLIC + + ${PROJECT_SOURCE_DIR} + ${INCLUDE_SPDLOG} + ${INCLUDE_BOX2D} + ${INCLUDE_IMGUI} + ${INCLUDE_IMGUI_SFML} + ${INCLUDE_HTTP} + ) + + target_link_libraries( + ${EXECUTABLE_NETWORK_NAME} + + ${LIBRARY_SFML} + ${LIBRARY_CORE} + ${LIBRARY_SPDLOG} + ${LIBRARY_BOX2D} + ${LIBRARY_OPENGL} + ${LIBRARY_PQXX} + ) + +endfunction() + +network_executable(${EXECUTABLE_SERVER} server/server.cpp) +network_executable(${EXECUTABLE_CLIENT} client/client.cpp) diff --git a/network/client/client.cpp b/network/client/client.cpp new file mode 100644 index 0000000..b4b83b3 --- /dev/null +++ b/network/client/client.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from client!" << '\n'; +} diff --git a/network/server/server.cpp b/network/server/server.cpp new file mode 100644 index 0000000..d60b858 --- /dev/null +++ b/network/server/server.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from server!" << '\n'; +} diff --git a/game/resources/animated/helicopter.png b/resources/animated/helicopter.png similarity index 100% rename from game/resources/animated/helicopter.png rename to resources/animated/helicopter.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00000.png b/resources/animated/hero/idle/Chara - BlueIdle00000.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00000.png rename to resources/animated/hero/idle/Chara - BlueIdle00000.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00001.png b/resources/animated/hero/idle/Chara - BlueIdle00001.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00001.png rename to resources/animated/hero/idle/Chara - BlueIdle00001.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00002.png b/resources/animated/hero/idle/Chara - BlueIdle00002.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00002.png rename to resources/animated/hero/idle/Chara - BlueIdle00002.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00003.png b/resources/animated/hero/idle/Chara - BlueIdle00003.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00003.png rename to resources/animated/hero/idle/Chara - BlueIdle00003.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00004.png b/resources/animated/hero/idle/Chara - BlueIdle00004.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00004.png rename to resources/animated/hero/idle/Chara - BlueIdle00004.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00005.png b/resources/animated/hero/idle/Chara - BlueIdle00005.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00005.png rename to resources/animated/hero/idle/Chara - BlueIdle00005.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00006.png b/resources/animated/hero/idle/Chara - BlueIdle00006.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00006.png rename to resources/animated/hero/idle/Chara - BlueIdle00006.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00007.png b/resources/animated/hero/idle/Chara - BlueIdle00007.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00007.png rename to resources/animated/hero/idle/Chara - BlueIdle00007.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00008.png b/resources/animated/hero/idle/Chara - BlueIdle00008.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00008.png rename to resources/animated/hero/idle/Chara - BlueIdle00008.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00009.png b/resources/animated/hero/idle/Chara - BlueIdle00009.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00009.png rename to resources/animated/hero/idle/Chara - BlueIdle00009.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00010.png b/resources/animated/hero/idle/Chara - BlueIdle00010.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00010.png rename to resources/animated/hero/idle/Chara - BlueIdle00010.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00011.png b/resources/animated/hero/idle/Chara - BlueIdle00011.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00011.png rename to resources/animated/hero/idle/Chara - BlueIdle00011.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00012.png b/resources/animated/hero/idle/Chara - BlueIdle00012.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00012.png rename to resources/animated/hero/idle/Chara - BlueIdle00012.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00013.png b/resources/animated/hero/idle/Chara - BlueIdle00013.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00013.png rename to resources/animated/hero/idle/Chara - BlueIdle00013.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00014.png b/resources/animated/hero/idle/Chara - BlueIdle00014.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00014.png rename to resources/animated/hero/idle/Chara - BlueIdle00014.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00015.png b/resources/animated/hero/idle/Chara - BlueIdle00015.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00015.png rename to resources/animated/hero/idle/Chara - BlueIdle00015.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00016.png b/resources/animated/hero/idle/Chara - BlueIdle00016.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00016.png rename to resources/animated/hero/idle/Chara - BlueIdle00016.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00017.png b/resources/animated/hero/idle/Chara - BlueIdle00017.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00017.png rename to resources/animated/hero/idle/Chara - BlueIdle00017.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00018.png b/resources/animated/hero/idle/Chara - BlueIdle00018.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00018.png rename to resources/animated/hero/idle/Chara - BlueIdle00018.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00019.png b/resources/animated/hero/idle/Chara - BlueIdle00019.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00019.png rename to resources/animated/hero/idle/Chara - BlueIdle00019.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00000.png b/resources/animated/hero/run/Chara_BlueWalk00000.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00000.png rename to resources/animated/hero/run/Chara_BlueWalk00000.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00001.png b/resources/animated/hero/run/Chara_BlueWalk00001.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00001.png rename to resources/animated/hero/run/Chara_BlueWalk00001.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00002.png b/resources/animated/hero/run/Chara_BlueWalk00002.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00002.png rename to resources/animated/hero/run/Chara_BlueWalk00002.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00003.png b/resources/animated/hero/run/Chara_BlueWalk00003.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00003.png rename to resources/animated/hero/run/Chara_BlueWalk00003.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00004.png b/resources/animated/hero/run/Chara_BlueWalk00004.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00004.png rename to resources/animated/hero/run/Chara_BlueWalk00004.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00005.png b/resources/animated/hero/run/Chara_BlueWalk00005.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00005.png rename to resources/animated/hero/run/Chara_BlueWalk00005.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00006.png b/resources/animated/hero/run/Chara_BlueWalk00006.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00006.png rename to resources/animated/hero/run/Chara_BlueWalk00006.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00007.png b/resources/animated/hero/run/Chara_BlueWalk00007.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00007.png rename to resources/animated/hero/run/Chara_BlueWalk00007.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00008.png b/resources/animated/hero/run/Chara_BlueWalk00008.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00008.png rename to resources/animated/hero/run/Chara_BlueWalk00008.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00009.png b/resources/animated/hero/run/Chara_BlueWalk00009.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00009.png rename to resources/animated/hero/run/Chara_BlueWalk00009.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00010.png b/resources/animated/hero/run/Chara_BlueWalk00010.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00010.png rename to resources/animated/hero/run/Chara_BlueWalk00010.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00011.png b/resources/animated/hero/run/Chara_BlueWalk00011.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00011.png rename to resources/animated/hero/run/Chara_BlueWalk00011.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00012.png b/resources/animated/hero/run/Chara_BlueWalk00012.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00012.png rename to resources/animated/hero/run/Chara_BlueWalk00012.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00013.png b/resources/animated/hero/run/Chara_BlueWalk00013.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00013.png rename to resources/animated/hero/run/Chara_BlueWalk00013.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00014.png b/resources/animated/hero/run/Chara_BlueWalk00014.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00014.png rename to resources/animated/hero/run/Chara_BlueWalk00014.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00015.png b/resources/animated/hero/run/Chara_BlueWalk00015.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00015.png rename to resources/animated/hero/run/Chara_BlueWalk00015.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00016.png b/resources/animated/hero/run/Chara_BlueWalk00016.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00016.png rename to resources/animated/hero/run/Chara_BlueWalk00016.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00017.png b/resources/animated/hero/run/Chara_BlueWalk00017.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00017.png rename to resources/animated/hero/run/Chara_BlueWalk00017.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00018.png b/resources/animated/hero/run/Chara_BlueWalk00018.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00018.png rename to resources/animated/hero/run/Chara_BlueWalk00018.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00019.png b/resources/animated/hero/run/Chara_BlueWalk00019.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00019.png rename to resources/animated/hero/run/Chara_BlueWalk00019.png diff --git a/game/resources/animated/runner_new.png b/resources/animated/runner_new.png similarity index 100% rename from game/resources/animated/runner_new.png rename to resources/animated/runner_new.png diff --git a/game/resources/levels/level_01/config.json b/resources/levels/level_01/config.json similarity index 92% rename from game/resources/levels/level_01/config.json rename to resources/levels/level_01/config.json index 4594a7c..e03da1a 100644 --- a/game/resources/levels/level_01/config.json +++ b/resources/levels/level_01/config.json @@ -1,6 +1,6 @@ { "name" : "level_01", - "animated_resources" : "../../game/resources/animated/", + "animated_resources" : "../../resources/animated/", "block" : 50.0, "camera": { "position" : { diff --git a/game/resources/levels/level_01/map b/resources/levels/level_01/map similarity index 100% rename from game/resources/levels/level_01/map rename to resources/levels/level_01/map diff --git a/game/resources/levels/level_with_finish/config.json b/resources/levels/level_with_finish/config.json similarity index 92% rename from game/resources/levels/level_with_finish/config.json rename to resources/levels/level_with_finish/config.json index 6482a2a..edfee15 100644 --- a/game/resources/levels/level_with_finish/config.json +++ b/resources/levels/level_with_finish/config.json @@ -1,6 +1,6 @@ { "name" : "level_with_finish", - "animated_resources" : "../../game/resources/animated/", + "animated_resources" : "../../resources/animated/", "block" : 50.0, "camera": { "position" : { diff --git a/game/resources/levels/level_with_finish/map b/resources/levels/level_with_finish/map similarity index 100% rename from game/resources/levels/level_with_finish/map rename to resources/levels/level_with_finish/map diff --git a/game/resources/static/18plus.png b/resources/static/18plus.png similarity index 100% rename from game/resources/static/18plus.png rename to resources/static/18plus.png diff --git a/game/resources/static/brick.png b/resources/static/brick.png similarity index 100% rename from game/resources/static/brick.png rename to resources/static/brick.png diff --git a/game/resources/static/exit.png b/resources/static/exit.png similarity index 100% rename from game/resources/static/exit.png rename to resources/static/exit.png From 628113e3d94f3b5efe832583ffff407f5f326caa Mon Sep 17 00:00:00 2001 From: denis Date: Sat, 28 May 2022 16:12:17 +0300 Subject: [PATCH 06/44] Fix PauseMenu render --- core/menu/PauseMenu.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/menu/PauseMenu.cpp b/core/menu/PauseMenu.cpp index f9b2c3a..f34ee62 100644 --- a/core/menu/PauseMenu.cpp +++ b/core/menu/PauseMenu.cpp @@ -23,6 +23,7 @@ namespace mad::core { } ImGui::End(); ImGui::SFML::Render(window); + return true; } } From 6ee5a4b04f9a7cf591f96d7c136578c97b1ae000 Mon Sep 17 00:00:00 2001 From: denis Date: Sat, 28 May 2022 16:05:17 +0300 Subject: [PATCH 07/44] Add httplib.h in submodule, move resources, add network directory and cmake --- .gitmodules | 3 ++ CMakeLists.txt | 16 +++++++ core/loader/LevelLoaderFromFile.cpp | 4 +- deps/cpp-httplib | 1 + game/game_database_example.cpp | 4 +- game/game_with_default_loader_example.cpp | 2 +- network/CMakeLists.txt | 41 ++++++++++++++++++ network/client/client.cpp | 6 +++ network/server/server.cpp | 6 +++ .../animated/helicopter.png | Bin .../hero/idle/Chara - BlueIdle00000.png | Bin .../hero/idle/Chara - BlueIdle00001.png | Bin .../hero/idle/Chara - BlueIdle00002.png | Bin .../hero/idle/Chara - BlueIdle00003.png | Bin .../hero/idle/Chara - BlueIdle00004.png | Bin .../hero/idle/Chara - BlueIdle00005.png | Bin .../hero/idle/Chara - BlueIdle00006.png | Bin .../hero/idle/Chara - BlueIdle00007.png | Bin .../hero/idle/Chara - BlueIdle00008.png | Bin .../hero/idle/Chara - BlueIdle00009.png | Bin .../hero/idle/Chara - BlueIdle00010.png | Bin .../hero/idle/Chara - BlueIdle00011.png | Bin .../hero/idle/Chara - BlueIdle00012.png | Bin .../hero/idle/Chara - BlueIdle00013.png | Bin .../hero/idle/Chara - BlueIdle00014.png | Bin .../hero/idle/Chara - BlueIdle00015.png | Bin .../hero/idle/Chara - BlueIdle00016.png | Bin .../hero/idle/Chara - BlueIdle00017.png | Bin .../hero/idle/Chara - BlueIdle00018.png | Bin .../hero/idle/Chara - BlueIdle00019.png | Bin .../animated/hero/run/Chara_BlueWalk00000.png | Bin .../animated/hero/run/Chara_BlueWalk00001.png | Bin .../animated/hero/run/Chara_BlueWalk00002.png | Bin .../animated/hero/run/Chara_BlueWalk00003.png | Bin .../animated/hero/run/Chara_BlueWalk00004.png | Bin .../animated/hero/run/Chara_BlueWalk00005.png | Bin .../animated/hero/run/Chara_BlueWalk00006.png | Bin .../animated/hero/run/Chara_BlueWalk00007.png | Bin .../animated/hero/run/Chara_BlueWalk00008.png | Bin .../animated/hero/run/Chara_BlueWalk00009.png | Bin .../animated/hero/run/Chara_BlueWalk00010.png | Bin .../animated/hero/run/Chara_BlueWalk00011.png | Bin .../animated/hero/run/Chara_BlueWalk00012.png | Bin .../animated/hero/run/Chara_BlueWalk00013.png | Bin .../animated/hero/run/Chara_BlueWalk00014.png | Bin .../animated/hero/run/Chara_BlueWalk00015.png | Bin .../animated/hero/run/Chara_BlueWalk00016.png | Bin .../animated/hero/run/Chara_BlueWalk00017.png | Bin .../animated/hero/run/Chara_BlueWalk00018.png | Bin .../animated/hero/run/Chara_BlueWalk00019.png | Bin .../animated/runner_new.png | Bin .../levels/level_01/config.json | 2 +- .../levels/level_01/map | 0 .../levels/level_with_finish/config.json | 2 +- .../levels/level_with_finish/map | 0 .../resources => resources}/static/18plus.png | Bin .../resources => resources}/static/brick.png | Bin {game/resources => resources}/static/exit.png | Bin 58 files changed, 80 insertions(+), 7 deletions(-) create mode 160000 deps/cpp-httplib create mode 100644 network/CMakeLists.txt create mode 100644 network/client/client.cpp create mode 100644 network/server/server.cpp rename {game/resources => resources}/animated/helicopter.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00000.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00001.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00002.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00003.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00004.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00005.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00006.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00007.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00008.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00009.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00010.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00011.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00012.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00013.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00014.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00015.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00016.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00017.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00018.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00019.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00000.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00001.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00002.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00003.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00004.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00005.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00006.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00007.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00008.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00009.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00010.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00011.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00012.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00013.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00014.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00015.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00016.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00017.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00018.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00019.png (100%) rename {game/resources => resources}/animated/runner_new.png (100%) rename {game/resources => resources}/levels/level_01/config.json (92%) rename {game/resources => resources}/levels/level_01/map (100%) rename {game/resources => resources}/levels/level_with_finish/config.json (92%) rename {game/resources => resources}/levels/level_with_finish/map (100%) rename {game/resources => resources}/static/18plus.png (100%) rename {game/resources => resources}/static/brick.png (100%) rename {game/resources => resources}/static/exit.png (100%) diff --git a/.gitmodules b/.gitmodules index c319e98..952393c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "deps/libpqxx"] path = deps/libpqxx url = https://github.com/jtv/libpqxx +[submodule "deps/cpp-httplib"] + path = deps/cpp-httplib + url = https://github.com/yhirose/cpp-httplib diff --git a/CMakeLists.txt b/CMakeLists.txt index 928f804..1c50a15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,13 @@ add_subdirectory(${DIRECTORY_PQXX}) message(STATUS "PostgreSQL include: ${INCLUDE_PQXX}") message(STATUS "PostgreSQL library: ${LIBRARY_PQXX}") +# HTTP library +set(DIRECTORY_HTTP ${PROJECT_SOURCE_DIR}/deps/cpp-httplib) +set(INCLUDE_HTTP ${DIRECTORY_HTTP}) + +add_subdirectory(${DIRECTORY_HTTP}) +message(STATUS "HTTP include: ${INCLUDE_HTTP}") + # Core library set(DIRECTORY_CORE ${PROJECT_SOURCE_DIR}/core) set(INCLUDE_CORE ${DIRECTORY_CORE}) @@ -105,6 +112,15 @@ add_subdirectory(${DIRECTORY_GAME}) message(STATUS "Game executable: ${EXECUTABLE_GAME}") message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") +# Network binaries +set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) +set(EXECUTABLE_SERVER server) +set(EXECUTABLE_CLIENT client) + +add_subdirectory(${DIRECTORY_NETWORK}) +message(STATUS "Server executable: ${EXECUTABLE_SERVER}") +message(STATUS "Client executable ${EXECUTABLE_GAME_CLIENT}") + # Tests set(DIRECTORY_TEST ${PROJECT_SOURCE_DIR}/test) set(EXECUTABLE_TEST test) diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index 2201916..324c61a 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -133,7 +133,7 @@ namespace mad::core { void LevelLoaderFromFile::create_block(std::shared_ptr world, Vec2d position, float block_size, bool is_stable) { - std::filesystem::path source("../../game/resources/static/"); + std::filesystem::path source("../../resources/static/"); if (is_stable) { source /= static_cast(m_config_json["texture"]["stable"]); } else { @@ -203,7 +203,7 @@ namespace mad::core { } Entity::Id LevelLoaderFromFile::create_finish_block(std::shared_ptr world, Vec2d position, float block_size) { - std::filesystem::path source("../../game/resources/static/"); + std::filesystem::path source("../../resources/static/"); source /= static_cast(m_config_json["texture"]["finish"]); auto image_storage = std::make_shared( diff --git a/deps/cpp-httplib b/deps/cpp-httplib new file mode 160000 index 0000000..df20c27 --- /dev/null +++ b/deps/cpp-httplib @@ -0,0 +1 @@ +Subproject commit df20c27696c6dcb2b9ccecf3c4f9c3d06c1ecf8c diff --git a/game/game_database_example.cpp b/game/game_database_example.cpp index 48e0fe2..586b030 100644 --- a/game/game_database_example.cpp +++ b/game/game_database_example.cpp @@ -45,8 +45,8 @@ int main() { auto database_storage_driver = std::make_shared(database); std::vector> level_loaders{ - std::make_shared("../../game/resources/levels/level_with_finish"), - std::make_shared("../../game/resources/levels/level_01") + std::make_shared("../../resources/levels/level_with_finish"), + std::make_shared("../../resources/levels/level_01") }; auto game_runner = std::make_unique( diff --git a/game/game_with_default_loader_example.cpp b/game/game_with_default_loader_example.cpp index 337426b..a285992 100644 --- a/game/game_with_default_loader_example.cpp +++ b/game/game_with_default_loader_example.cpp @@ -42,7 +42,7 @@ int main() { auto offline_storage_driver = std::make_shared(); std::vector> level_loaders{ - std::make_shared("../../game/resources/levels/level_01") + std::make_shared("../../resources/levels/level_01") }; auto game_runner = std::make_unique( diff --git a/network/CMakeLists.txt b/network/CMakeLists.txt new file mode 100644 index 0000000..35f74e8 --- /dev/null +++ b/network/CMakeLists.txt @@ -0,0 +1,41 @@ +function(network_executable EXECUTABLE_NETWORK_NAME EXECUTABLE_SOURCES) + message(STATUS "Network executable: '${EXECUTABLE_NETWORK_NAME}' is built with ${EXECUTABLE_SOURCES}") + + add_executable( + ${EXECUTABLE_NETWORK_NAME} + ${EXECUTABLE_SOURCES} + ${SOURCES_IMGUI} + ${SOURCES_IMGUI_SFML} + ) + + if (CMAKE_BUILD_TYPE MATCHES Debug) + target_compile_definitions(${EXECUTABLE_NETWORK_NAME} PUBLIC SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE) + message(STATUS "Enable debug logs for ${EXECUTABLE_NETWORK_NAME}") + endif() + + target_include_directories( + ${EXECUTABLE_NETWORK_NAME} PUBLIC + + ${PROJECT_SOURCE_DIR} + ${INCLUDE_SPDLOG} + ${INCLUDE_BOX2D} + ${INCLUDE_IMGUI} + ${INCLUDE_IMGUI_SFML} + ${INCLUDE_HTTP} + ) + + target_link_libraries( + ${EXECUTABLE_NETWORK_NAME} + + ${LIBRARY_SFML} + ${LIBRARY_CORE} + ${LIBRARY_SPDLOG} + ${LIBRARY_BOX2D} + ${LIBRARY_OPENGL} + ${LIBRARY_PQXX} + ) + +endfunction() + +network_executable(${EXECUTABLE_SERVER} server/server.cpp) +network_executable(${EXECUTABLE_CLIENT} client/client.cpp) diff --git a/network/client/client.cpp b/network/client/client.cpp new file mode 100644 index 0000000..b4b83b3 --- /dev/null +++ b/network/client/client.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from client!" << '\n'; +} diff --git a/network/server/server.cpp b/network/server/server.cpp new file mode 100644 index 0000000..d60b858 --- /dev/null +++ b/network/server/server.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from server!" << '\n'; +} diff --git a/game/resources/animated/helicopter.png b/resources/animated/helicopter.png similarity index 100% rename from game/resources/animated/helicopter.png rename to resources/animated/helicopter.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00000.png b/resources/animated/hero/idle/Chara - BlueIdle00000.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00000.png rename to resources/animated/hero/idle/Chara - BlueIdle00000.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00001.png b/resources/animated/hero/idle/Chara - BlueIdle00001.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00001.png rename to resources/animated/hero/idle/Chara - BlueIdle00001.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00002.png b/resources/animated/hero/idle/Chara - BlueIdle00002.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00002.png rename to resources/animated/hero/idle/Chara - BlueIdle00002.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00003.png b/resources/animated/hero/idle/Chara - BlueIdle00003.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00003.png rename to resources/animated/hero/idle/Chara - BlueIdle00003.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00004.png b/resources/animated/hero/idle/Chara - BlueIdle00004.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00004.png rename to resources/animated/hero/idle/Chara - BlueIdle00004.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00005.png b/resources/animated/hero/idle/Chara - BlueIdle00005.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00005.png rename to resources/animated/hero/idle/Chara - BlueIdle00005.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00006.png b/resources/animated/hero/idle/Chara - BlueIdle00006.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00006.png rename to resources/animated/hero/idle/Chara - BlueIdle00006.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00007.png b/resources/animated/hero/idle/Chara - BlueIdle00007.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00007.png rename to resources/animated/hero/idle/Chara - BlueIdle00007.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00008.png b/resources/animated/hero/idle/Chara - BlueIdle00008.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00008.png rename to resources/animated/hero/idle/Chara - BlueIdle00008.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00009.png b/resources/animated/hero/idle/Chara - BlueIdle00009.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00009.png rename to resources/animated/hero/idle/Chara - BlueIdle00009.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00010.png b/resources/animated/hero/idle/Chara - BlueIdle00010.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00010.png rename to resources/animated/hero/idle/Chara - BlueIdle00010.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00011.png b/resources/animated/hero/idle/Chara - BlueIdle00011.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00011.png rename to resources/animated/hero/idle/Chara - BlueIdle00011.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00012.png b/resources/animated/hero/idle/Chara - BlueIdle00012.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00012.png rename to resources/animated/hero/idle/Chara - BlueIdle00012.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00013.png b/resources/animated/hero/idle/Chara - BlueIdle00013.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00013.png rename to resources/animated/hero/idle/Chara - BlueIdle00013.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00014.png b/resources/animated/hero/idle/Chara - BlueIdle00014.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00014.png rename to resources/animated/hero/idle/Chara - BlueIdle00014.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00015.png b/resources/animated/hero/idle/Chara - BlueIdle00015.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00015.png rename to resources/animated/hero/idle/Chara - BlueIdle00015.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00016.png b/resources/animated/hero/idle/Chara - BlueIdle00016.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00016.png rename to resources/animated/hero/idle/Chara - BlueIdle00016.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00017.png b/resources/animated/hero/idle/Chara - BlueIdle00017.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00017.png rename to resources/animated/hero/idle/Chara - BlueIdle00017.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00018.png b/resources/animated/hero/idle/Chara - BlueIdle00018.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00018.png rename to resources/animated/hero/idle/Chara - BlueIdle00018.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00019.png b/resources/animated/hero/idle/Chara - BlueIdle00019.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00019.png rename to resources/animated/hero/idle/Chara - BlueIdle00019.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00000.png b/resources/animated/hero/run/Chara_BlueWalk00000.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00000.png rename to resources/animated/hero/run/Chara_BlueWalk00000.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00001.png b/resources/animated/hero/run/Chara_BlueWalk00001.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00001.png rename to resources/animated/hero/run/Chara_BlueWalk00001.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00002.png b/resources/animated/hero/run/Chara_BlueWalk00002.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00002.png rename to resources/animated/hero/run/Chara_BlueWalk00002.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00003.png b/resources/animated/hero/run/Chara_BlueWalk00003.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00003.png rename to resources/animated/hero/run/Chara_BlueWalk00003.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00004.png b/resources/animated/hero/run/Chara_BlueWalk00004.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00004.png rename to resources/animated/hero/run/Chara_BlueWalk00004.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00005.png b/resources/animated/hero/run/Chara_BlueWalk00005.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00005.png rename to resources/animated/hero/run/Chara_BlueWalk00005.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00006.png b/resources/animated/hero/run/Chara_BlueWalk00006.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00006.png rename to resources/animated/hero/run/Chara_BlueWalk00006.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00007.png b/resources/animated/hero/run/Chara_BlueWalk00007.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00007.png rename to resources/animated/hero/run/Chara_BlueWalk00007.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00008.png b/resources/animated/hero/run/Chara_BlueWalk00008.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00008.png rename to resources/animated/hero/run/Chara_BlueWalk00008.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00009.png b/resources/animated/hero/run/Chara_BlueWalk00009.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00009.png rename to resources/animated/hero/run/Chara_BlueWalk00009.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00010.png b/resources/animated/hero/run/Chara_BlueWalk00010.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00010.png rename to resources/animated/hero/run/Chara_BlueWalk00010.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00011.png b/resources/animated/hero/run/Chara_BlueWalk00011.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00011.png rename to resources/animated/hero/run/Chara_BlueWalk00011.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00012.png b/resources/animated/hero/run/Chara_BlueWalk00012.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00012.png rename to resources/animated/hero/run/Chara_BlueWalk00012.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00013.png b/resources/animated/hero/run/Chara_BlueWalk00013.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00013.png rename to resources/animated/hero/run/Chara_BlueWalk00013.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00014.png b/resources/animated/hero/run/Chara_BlueWalk00014.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00014.png rename to resources/animated/hero/run/Chara_BlueWalk00014.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00015.png b/resources/animated/hero/run/Chara_BlueWalk00015.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00015.png rename to resources/animated/hero/run/Chara_BlueWalk00015.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00016.png b/resources/animated/hero/run/Chara_BlueWalk00016.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00016.png rename to resources/animated/hero/run/Chara_BlueWalk00016.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00017.png b/resources/animated/hero/run/Chara_BlueWalk00017.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00017.png rename to resources/animated/hero/run/Chara_BlueWalk00017.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00018.png b/resources/animated/hero/run/Chara_BlueWalk00018.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00018.png rename to resources/animated/hero/run/Chara_BlueWalk00018.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00019.png b/resources/animated/hero/run/Chara_BlueWalk00019.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00019.png rename to resources/animated/hero/run/Chara_BlueWalk00019.png diff --git a/game/resources/animated/runner_new.png b/resources/animated/runner_new.png similarity index 100% rename from game/resources/animated/runner_new.png rename to resources/animated/runner_new.png diff --git a/game/resources/levels/level_01/config.json b/resources/levels/level_01/config.json similarity index 92% rename from game/resources/levels/level_01/config.json rename to resources/levels/level_01/config.json index 4594a7c..e03da1a 100644 --- a/game/resources/levels/level_01/config.json +++ b/resources/levels/level_01/config.json @@ -1,6 +1,6 @@ { "name" : "level_01", - "animated_resources" : "../../game/resources/animated/", + "animated_resources" : "../../resources/animated/", "block" : 50.0, "camera": { "position" : { diff --git a/game/resources/levels/level_01/map b/resources/levels/level_01/map similarity index 100% rename from game/resources/levels/level_01/map rename to resources/levels/level_01/map diff --git a/game/resources/levels/level_with_finish/config.json b/resources/levels/level_with_finish/config.json similarity index 92% rename from game/resources/levels/level_with_finish/config.json rename to resources/levels/level_with_finish/config.json index 6482a2a..edfee15 100644 --- a/game/resources/levels/level_with_finish/config.json +++ b/resources/levels/level_with_finish/config.json @@ -1,6 +1,6 @@ { "name" : "level_with_finish", - "animated_resources" : "../../game/resources/animated/", + "animated_resources" : "../../resources/animated/", "block" : 50.0, "camera": { "position" : { diff --git a/game/resources/levels/level_with_finish/map b/resources/levels/level_with_finish/map similarity index 100% rename from game/resources/levels/level_with_finish/map rename to resources/levels/level_with_finish/map diff --git a/game/resources/static/18plus.png b/resources/static/18plus.png similarity index 100% rename from game/resources/static/18plus.png rename to resources/static/18plus.png diff --git a/game/resources/static/brick.png b/resources/static/brick.png similarity index 100% rename from game/resources/static/brick.png rename to resources/static/brick.png diff --git a/game/resources/static/exit.png b/resources/static/exit.png similarity index 100% rename from game/resources/static/exit.png rename to resources/static/exit.png From 84d9c73128a3791faf4760346452e213c40bdbf3 Mon Sep 17 00:00:00 2001 From: alexeyha Date: Sun, 29 May 2022 14:33:10 +0300 Subject: [PATCH 08/44] Update config --- game/resources/levels/level_with_finish/config.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/game/resources/levels/level_with_finish/config.json b/game/resources/levels/level_with_finish/config.json index ce6782f..4276712 100644 --- a/game/resources/levels/level_with_finish/config.json +++ b/game/resources/levels/level_with_finish/config.json @@ -13,7 +13,8 @@ }, "background" : { "source" : "../../game/resources/background", - "parallax_ratios" : [0.95, 0.9, 0.85, 0.8, 0.75] + "parallax_ratios" : [0.95, 0.9, 0.85, 0.8, 0.75], + "scale" : 2.8 }, "texture" : { "stable" : "brick.png", From 703b6a6d1211ca2ca0e69fb6327ff19e9975d20a Mon Sep 17 00:00:00 2001 From: alexeyha Date: Sun, 29 May 2022 15:29:13 +0300 Subject: [PATCH 09/44] Add scale to arguments --- core/loader/LevelLoaderFromFile.cpp | 3 ++- core/visual/image/background/BackgroundImage.cpp | 8 ++++++-- core/visual/image/background/BackgroundImage.hpp | 6 +++++- core/visual/image/background/RenderableBackground.cpp | 11 ++++++----- core/visual/image/background/RenderableBackground.hpp | 6 +++--- game/resources/levels/level_01/config.json | 3 ++- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index 269068a..1909ea8 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -237,7 +237,8 @@ namespace mad::core { {{ImageStorage::TypeAction::Idle, std::make_shared( source, - parallax_ratios + parallax_ratios, + m_config_json["background"]["scale"] ) }} ) diff --git a/core/visual/image/background/BackgroundImage.cpp b/core/visual/image/background/BackgroundImage.cpp index f383ede..13e5786 100644 --- a/core/visual/image/background/BackgroundImage.cpp +++ b/core/visual/image/background/BackgroundImage.cpp @@ -5,9 +5,9 @@ namespace mad::core { - BackgroundImage::BackgroundImage(std::filesystem::path path, std::vector parallax_ratios) + BackgroundImage::BackgroundImage(std::filesystem::path path, std::vector parallax_ratios, float scale) : Image(Image::Type::Background), m_path(std::move(path)), - m_parallax_ratios(std::move(parallax_ratios)) { + m_parallax_ratios(std::move(parallax_ratios)), m_scale(scale) { } std::filesystem::path BackgroundImage::get_path() const noexcept { @@ -22,6 +22,10 @@ namespace mad::core { return {}; } + float BackgroundImage::get_scale() const noexcept { + return m_scale; + } + } diff --git a/core/visual/image/background/BackgroundImage.hpp b/core/visual/image/background/BackgroundImage.hpp index ceb78c0..88b5bbb 100644 --- a/core/visual/image/background/BackgroundImage.hpp +++ b/core/visual/image/background/BackgroundImage.hpp @@ -10,17 +10,21 @@ namespace mad::core { class BackgroundImage : public Image { public: - BackgroundImage(std::filesystem::path path, std::vector parallax_ratios); + BackgroundImage(std::filesystem::path path, std::vector parallax_ratios, float scale); [[nodiscard]] std::filesystem::path get_path() const noexcept; [[nodiscard]] std::vector get_parallax_ratios() const noexcept; + [[nodiscard]] float get_scale() const noexcept; + b2PolygonShape as_fixture() override; private: std::filesystem::path m_path; std::vector m_parallax_ratios; + + float m_scale; }; } diff --git a/core/visual/image/background/RenderableBackground.cpp b/core/visual/image/background/RenderableBackground.cpp index b6ed068..65c91e5 100644 --- a/core/visual/image/background/RenderableBackground.cpp +++ b/core/visual/image/background/RenderableBackground.cpp @@ -6,11 +6,12 @@ namespace mad::core { - RenderableBackground::RenderableBackground(const std::shared_ptr &background, - std::shared_ptr position, std::shared_ptr rotation) : + RenderableBackground::RenderableBackground(const std::shared_ptr &background, std::shared_ptr position, + std::shared_ptr rotation) : m_camera_position(std::move(position)), m_rotation(std::move(rotation)), m_parallax_ratios(background->get_parallax_ratios()), - m_last_camera_position(*m_camera_position) { + m_last_camera_position(*m_camera_position), + m_scale({background->get_scale(), background->get_scale()}) { is_active = background->is_active; std::set sorted_files; @@ -41,8 +42,8 @@ namespace mad::core { background.setPosition(m_layers_positions[i].get_x() + (*m_camera_position - m_last_camera_position).get_x() * ratio, m_camera_position->get_y()); } m_layers_positions[i] += {(*m_camera_position - m_last_camera_position).get_x() * ratio, (*m_camera_position - m_layers_positions[i]).get_y()}; - background.setTextureRect({0, 0, static_cast(window.getSize().x) * 3, static_cast(window.getSize().y)}); - background.setOrigin(static_cast(window.getSize().x) / 2, static_cast(window.getSize().y) / 4); //static_cast(window.getSize().y) / 2); + background.setTextureRect({0, 0, static_cast(background.getLocalBounds().width) * 100, static_cast(background.getLocalBounds().height)}); + background.setOrigin(background.getLocalBounds().width / 2, background.getLocalBounds().height / 2); //static_cast(window.getSize().y) / 2); window.draw(background); } m_last_camera_position = *m_camera_position; diff --git a/core/visual/image/background/RenderableBackground.hpp b/core/visual/image/background/RenderableBackground.hpp index e09e602..df95e9c 100644 --- a/core/visual/image/background/RenderableBackground.hpp +++ b/core/visual/image/background/RenderableBackground.hpp @@ -13,8 +13,8 @@ namespace mad::core { class RenderableBackground : public Renderable { public: - RenderableBackground(const std::shared_ptr &background, std::shared_ptr position, - std::shared_ptr rotation); + RenderableBackground(const std::shared_ptr &background, + std::shared_ptr position, std::shared_ptr rotation); bool render(sf::RenderWindow &window) override; @@ -31,7 +31,7 @@ namespace mad::core { std::shared_ptr m_rotation; - Vec2d m_scale = {2.8, 2.8}; + Vec2d m_scale = {1, 1}; std::vector m_parallax_ratios; }; diff --git a/game/resources/levels/level_01/config.json b/game/resources/levels/level_01/config.json index c8fd830..250584b 100644 --- a/game/resources/levels/level_01/config.json +++ b/game/resources/levels/level_01/config.json @@ -13,7 +13,8 @@ }, "background" : { "source" : "../../game/resources/background", - "parallax_ratios" : [0.95, 0.9, 0.85, 0.8, 0.75] + "parallax_ratios" : [0.95, 0.9, 0.85, 0.8, 0.75], + "scale" : 2.8 }, "texture" : { "stable" : "brick.png", From fe60003eb2eb892521004afb35abfae2df269540 Mon Sep 17 00:00:00 2001 From: denis Date: Sun, 29 May 2022 19:06:49 +0300 Subject: [PATCH 10/44] Test for httplib --- test/CMakeLists.txt | 1 + test/deps/CMakeLists.txt | 2 + test/deps/network/client.cpp | 22 +++++ test/deps/network/server.cpp | 155 +++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 test/deps/network/client.cpp create mode 100644 test/deps/network/server.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 07bb365..bc2aa5e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,6 +18,7 @@ function(test_executable EXECUTABLE_TEST_NAME EXECUTABLE_SOURCES) ${INCLUDE_IMGUI} ${INCLUDE_IMGUI_SFML} ${INCLUDE_PQXX} + ${INCLUDE_HTTP} ) target_link_libraries( diff --git a/test/deps/CMakeLists.txt b/test/deps/CMakeLists.txt index 3078da8..817960a 100644 --- a/test/deps/CMakeLists.txt +++ b/test/deps/CMakeLists.txt @@ -1,3 +1,5 @@ test_executable(test-box2d TestBox2d.cpp) test_executable(test-imgui TestImGui.cpp) test_executable(test-pqxx TestPQXX.cpp) +test_executable(test-server network/server.cpp) +test_executable(test-client network/client.cpp) diff --git a/test/deps/network/client.cpp b/test/deps/network/client.cpp new file mode 100644 index 0000000..198b44a --- /dev/null +++ b/test/deps/network/client.cpp @@ -0,0 +1,22 @@ +#include +#include + +int main() { + httplib::Client cli("localhost", 8080); + + auto res1 = cli.Post("/user/login", httplib::Params{ + {"username", "Denis"} + }); + std::cout << res1->status << ' ' << res1->body << '\n'; + + + auto res2 = cli.Post("/user/login", httplib::Params{ + {"username", "NoDenis"} + }); + std::cout << res2->status << ' ' << res2->body << '\n'; + + auto res3 = cli.Post("/user/login", httplib::Params{ + {"name", "Denis"} + }); + std::cout << res3->status << ' ' << res3->body << '\n'; +} diff --git a/test/deps/network/server.cpp b/test/deps/network/server.cpp new file mode 100644 index 0000000..e4038c3 --- /dev/null +++ b/test/deps/network/server.cpp @@ -0,0 +1,155 @@ +#include +#include +#include + +namespace mad::core { + class Database { + public: + Database(); + + bool is_user_exists(const std::string &username); + + void registry_user(const std::string &username); + + std::size_t get_id(const std::string &username); + + std::size_t get_progress(std::size_t id); + + std::size_t get_progress(const std::string &username); + + void increment_progress(std::size_t id); + + void increment_progress(const std::string &username); + + void reset_progress(std::size_t id); + + void reset_progress(const std::string &username); + + private: + pqxx::connection m_connector; + std::string m_query; + + }; +} + +namespace mad::core { + + Database::Database() : m_connector("dbname = test-network") { + try { + if (m_connector.is_open()) { + SPDLOG_DEBUG("Database mad opened successfully"); + } else { + SPDLOG_DEBUG("Can't open database mad"); + } + + pqxx::work w(m_connector); + + m_query = "CREATE TABLE IF NOT EXISTS users(" + "id SMALLINT PRIMARY KEY," + "name TEXT NOT NULL UNIQUE);"; + w.exec(m_query); + + m_query = "CREATE TABLE IF NOT EXISTS progress(" + "id SMALLINT PRIMARY KEY REFERENCES users (id)," + "levels_completed SMALLINT NOT NULL);"; + w.exec(m_query); + + w.commit(); + + SPDLOG_DEBUG("Tables created successfully"); + } catch (std::exception &exc) { + SPDLOG_INFO(exc.what()); + } + } + + bool Database::is_user_exists(const std::string &username) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM users WHERE name = '" + W.esc(username) + "';"; + pqxx::result rows_found = W.exec(m_query); + return !rows_found.empty(); + } + + void Database::registry_user(const std::string &username) { + pqxx::work W(m_connector); + + m_query = "SELECT id FROM users"; + pqxx::result total_rows = W.exec(m_query); + std::size_t id = total_rows.size(); + + m_query = "INSERT INTO users(id, name) VALUES(" + std::to_string(id) + ", '" + W.esc(username) + "');"; + W.exec(m_query); + + m_query = "INSERT INTO progress(id, levels_completed) VALUES(" + std::to_string(id) + ", 0);"; + W.exec(m_query); + + W.commit(); + } + + std::size_t Database::get_id(const std::string &username) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM users WHERE name = '" + W.esc(username) + "';"; + auto found_row = W.exec1(m_query); + return found_row["id"].as(); + } + + std::size_t Database::get_progress(std::size_t id) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM progress WHERE id = " + std::to_string(id) + ";"; + auto found_row = W.exec1(m_query); + return found_row["levels_completed"].as(); + } + + std::size_t Database::get_progress(const std::string &username) { + return get_progress(get_id(username)); + } + + void Database::increment_progress(std::size_t id) { + pqxx::work W(m_connector); + m_query = "UPDATE progress " + "SET levels_completed = levels_completed + 1 " + "WHERE id = " + std::to_string(id) + ";"; + W.exec(m_query); + W.commit(); + } + + void Database::increment_progress(const std::string &username) { + increment_progress(get_id(username)); + } + + void Database::reset_progress(std::size_t id) { + pqxx::work W(m_connector); + m_query = "UPDATE progress " + "SET levels_completed = 0 " + "WHERE id = " + std::to_string(id) + ";"; + W.exec(m_query); + W.commit(); + } + + void Database::reset_progress(const std::string &username) { + reset_progress(get_id(username)); + } +} + +int main() { + httplib::Server svr; + mad::core::Database db; + + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = "OK"; + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + + svr.listen("localhost", 8080); +} \ No newline at end of file From cf3727ac0ce0983b90d3bc4e222b30be8727a13b Mon Sep 17 00:00:00 2001 From: denis Date: Sun, 29 May 2022 20:01:02 +0300 Subject: [PATCH 11/44] Add simple client and server realisation --- CMakeLists.txt | 4 +- network/CMakeLists.txt | 4 +- network/client/client.cpp | 6 -- network/client/simple-client.cpp | 108 +++++++++++++++++++++++++++++++ network/server/server.cpp | 6 -- network/server/simple-server.cpp | 77 ++++++++++++++++++++++ 6 files changed, 189 insertions(+), 16 deletions(-) delete mode 100644 network/client/client.cpp create mode 100644 network/client/simple-client.cpp delete mode 100644 network/server/server.cpp create mode 100644 network/server/simple-server.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c50a15..01687c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,8 +114,8 @@ message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") # Network binaries set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) -set(EXECUTABLE_SERVER server) -set(EXECUTABLE_CLIENT client) +set(EXECUTABLE_SIMPLE_SERVER simple-server) +set(EXECUTABLE_SIMPLE_CLIENT simple-client) add_subdirectory(${DIRECTORY_NETWORK}) message(STATUS "Server executable: ${EXECUTABLE_SERVER}") diff --git a/network/CMakeLists.txt b/network/CMakeLists.txt index 35f74e8..69e16eb 100644 --- a/network/CMakeLists.txt +++ b/network/CMakeLists.txt @@ -37,5 +37,5 @@ function(network_executable EXECUTABLE_NETWORK_NAME EXECUTABLE_SOURCES) endfunction() -network_executable(${EXECUTABLE_SERVER} server/server.cpp) -network_executable(${EXECUTABLE_CLIENT} client/client.cpp) +network_executable(${EXECUTABLE_SIMPLE_SERVER} server/simple-server.cpp) +network_executable(${EXECUTABLE_SIMPLE_CLIENT} client/simple-client.cpp) diff --git a/network/client/client.cpp b/network/client/client.cpp deleted file mode 100644 index b4b83b3..0000000 --- a/network/client/client.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int main() { - std::cout << "Hello from client!" << '\n'; -} diff --git a/network/client/simple-client.cpp b/network/client/simple-client.cpp new file mode 100644 index 0000000..26049c9 --- /dev/null +++ b/network/client/simple-client.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace mad::core { + + class NetworkClientStorageDriver : public ClientStorageDriver { + public: + explicit NetworkClientStorageDriver() : m_client("localhost", 8080) { } + + bool log_in(const std::string &username) const override { + auto res = m_client.Post("/user/login", httplib::Params{ + {"username", username} + }); + SPDLOG_DEBUG("Login request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + if (res->status == 200) { + m_username = username; + } + return res->status == 200; + } + + bool sign_up(const std::string &username) override { + auto res = m_client.Post("/user/signup", httplib::Params{ + {"username", username} + }); + SPDLOG_DEBUG("Sign up request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + return res->status == 200; + } + + std::size_t get_progress() const override { + auto res = m_client.Post("/user/progress", httplib::Params{ + {"username", m_username} + }); + SPDLOG_DEBUG("Get progress request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + return std::stoi(res->body); + } + + void update_progress() override { + auto res = m_client.Post("/user/increment-progress", httplib::Params{ + {"username", m_username} + }); + SPDLOG_DEBUG("Update request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + } + + private: + mutable std::string m_username; + mutable httplib::Client m_client; + + }; + +} + +int main() { +#ifndef NDEBUG + auto log_level = spdlog::level::trace; +#else + auto log_level = spdlog::level::info; +#endif + spdlog::set_level(log_level); + + auto window = std::make_shared(sf::VideoMode(640, 480), "MAD"); + ImGui::SFML::Init(*window); + window->setFramerateLimit(120); + + auto global_dispatcher = std::make_shared(); + + auto system_listener = std::make_shared(window); + + std::vector> level_loaders{ + std::make_shared("../../resources/levels/level_with_finish"), + std::make_shared("../../resources/levels/level_01") + }; + + auto network_storage_driver = std::make_shared(); + + auto game_runner = std::make_unique( + level_loaders, + global_dispatcher, + std::make_unique(), + std::make_unique(network_storage_driver), + system_listener, + network_storage_driver + ); + + global_dispatcher->registry(std::make_shared(*window)); + global_dispatcher->registry(std::make_shared(*game_runner)); + global_dispatcher->registry(std::make_shared(*game_runner, network_storage_driver)); + global_dispatcher->registry(std::make_shared(*game_runner)); + + game_runner->run(*window); + + ImGui::SFML::Shutdown(); +} diff --git a/network/server/server.cpp b/network/server/server.cpp deleted file mode 100644 index d60b858..0000000 --- a/network/server/server.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int main() { - std::cout << "Hello from server!" << '\n'; -} diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp new file mode 100644 index 0000000..5c85588 --- /dev/null +++ b/network/server/simple-server.cpp @@ -0,0 +1,77 @@ +#include + +#include +#include + +int main() { + httplib::Server svr; + mad::core::Database db; + + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = "OK"; + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.Post("/user/signup", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 404; + res.body = "User already exists"; + } else { + db.registry_user(username); + res.status = 200; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.Post("/user/progress", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = std::to_string(db.get_progress(username)); + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.Post("/user/increment-progress", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = "OK"; + db.increment_progress(username); + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.listen("localhost", 8080); +} From 2d1ea084e696f64ce55222ed94f0c5ea4703f964 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 11:56:34 +0300 Subject: [PATCH 12/44] Chain server and window --- network/client/simple-client.cpp | 5 +++- network/server/simple-server.cpp | 45 +++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/network/client/simple-client.cpp b/network/client/simple-client.cpp index 26049c9..d35f011 100644 --- a/network/client/simple-client.cpp +++ b/network/client/simple-client.cpp @@ -21,7 +21,10 @@ namespace mad::core { class NetworkClientStorageDriver : public ClientStorageDriver { public: - explicit NetworkClientStorageDriver() : m_client("localhost", 8080) { } + explicit NetworkClientStorageDriver() : m_client("localhost", 8080) { + auto res = m_client.Get("/connection"); + SPDLOG_DEBUG("Connection request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + } bool log_in(const std::string &username) const override { auto res = m_client.Post("/user/login", httplib::Params{ diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 5c85588..d51b1a6 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -2,11 +2,25 @@ #include #include +#include + +#include +#include + +#include +#include +#include + int main() { httplib::Server svr; mad::core::Database db; + svr.Get("/connection", [](const httplib::Request &req, httplib::Response &res) { + res.status = 200; + res.body = "Connected user port -- " + std::to_string(req.remote_port); + }); + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); @@ -73,5 +87,34 @@ int main() { } }); - svr.listen("localhost", 8080); + std::thread([&svr]() mutable { + svr.listen("localhost", 8080); + }).detach(); + + sf::RenderWindow window(sf::VideoMode(640, 480), "MAD Server"); + ImGui::SFML::Init(window); + window.setFramerateLimit(120); + sf::Clock clock; + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + ImGui::SFML::ProcessEvent(event); + + if (event.type == sf::Event::Closed) { + window.close(); + svr.stop(); + } + } + + ImGui::SFML::Update(window, clock.restart()); + + ImGui::Begin("Window"); + ImGui::Text("Hello"); + ImGui::End(); + + window.clear(); + ImGui::SFML::Render(window); + window.display(); + } + ImGui::SFML::Shutdown(); } From 20f95183c90a0dfb73c925158e256de60e37bb9a Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 12:42:20 +0300 Subject: [PATCH 13/44] Add server control buttons --- network/server/simple-server.cpp | 38 +++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index d51b1a6..2f1ae43 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -46,7 +46,7 @@ int main() { } else { db.registry_user(username); res.status = 200; - res.body = "User doesn\'t exists"; + res.body = "User " + username + " is registered"; } } else { res.status = 404; @@ -87,10 +87,6 @@ int main() { } }); - std::thread([&svr]() mutable { - svr.listen("localhost", 8080); - }).detach(); - sf::RenderWindow window(sf::VideoMode(640, 480), "MAD Server"); ImGui::SFML::Init(window); window.setFramerateLimit(120); @@ -102,14 +98,40 @@ int main() { if (event.type == sf::Event::Closed) { window.close(); - svr.stop(); + if (svr.is_running()) { + svr.stop(); + } } } ImGui::SFML::Update(window, clock.restart()); - ImGui::Begin("Window"); - ImGui::Text("Hello"); + ImGui::SetNextWindowSize(ImVec2(window.getSize().x, window.getSize().y)); + ImGui::SetNextWindowPos({0, 0}); + ImGui::Begin("Server util", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); + + + if (ImGui::Button("Start server")) { + if (!svr.is_running()) { + std::thread([&svr]() mutable { + svr.listen("localhost", 8080); + }).detach(); + } + } + + if (ImGui::Button("Stop server")) { + if (svr.is_running()) { + svr.stop(); + } + } + + if (ImGui::Button("Quit")) { + window.close(); + if (svr.is_running()) { + svr.stop(); + } + } + ImGui::End(); window.clear(); From 04e24ce49bdd7a09997f66d086f3718bae882f63 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 15:59:14 +0300 Subject: [PATCH 14/44] Add server logs --- network/server/simple-server.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 2f1ae43..c8934f1 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -16,17 +17,22 @@ int main() { httplib::Server svr; mad::core::Database db; - svr.Get("/connection", [](const httplib::Request &req, httplib::Response &res) { + std::vector logs; + const char *log; + + svr.Get("/connection", [&logs](const httplib::Request &req, httplib::Response &res) { res.status = 200; res.body = "Connected user port -- " + std::to_string(req.remote_port); + logs.push_back(res.body); }); - svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/login", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; + logs.push_back("User " + std::to_string(req.remote_port) + " login as " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -37,12 +43,13 @@ int main() { } }); - svr.Post("/user/signup", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/signup", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 404; res.body = "User already exists"; + logs.push_back("Register new user " + username + " from port " + std::to_string(req.remote_port)); } else { db.registry_user(username); res.status = 200; @@ -54,12 +61,13 @@ int main() { } }); - svr.Post("/user/progress", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = std::to_string(db.get_progress(username)); + logs.push_back("Send progress to user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -70,13 +78,14 @@ int main() { } }); - svr.Post("/user/increment-progress", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/increment-progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; db.increment_progress(username); + logs.push_back("Increment progress for user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -116,12 +125,14 @@ int main() { std::thread([&svr]() mutable { svr.listen("localhost", 8080); }).detach(); + logs.emplace_back("Server has started"); } } if (ImGui::Button("Stop server")) { if (svr.is_running()) { svr.stop(); + logs.emplace_back("Server has stopped"); } } @@ -132,6 +143,15 @@ int main() { } } + { + ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; + ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); + for (int i = 0; i < logs.size(); ++i) { + ImGui::Text(logs[i].c_str(), i); + } + ImGui::EndChild(); + } + ImGui::End(); window.clear(); From 87dc43482903a2fa97ba4e310e3e9d9f0c2dba4c Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 16:32:47 +0300 Subject: [PATCH 15/44] Add mutex for logs and db --- network/server/simple-server.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index c8934f1..1fef0fe 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -1,9 +1,9 @@ #include -#include #include #include #include +#include #include #include @@ -17,16 +17,18 @@ int main() { httplib::Server svr; mad::core::Database db; + std::mutex locker; std::vector logs; - const char *log; - svr.Get("/connection", [&logs](const httplib::Request &req, httplib::Response &res) { + svr.Get("/connection", [&logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); res.status = 200; - res.body = "Connected user port -- " + std::to_string(req.remote_port); - logs.push_back(res.body); + res.body = "Connection successful"; + logs.push_back("Connected user port -- " + std::to_string(req.remote_port)); }); - svr.Post("/user/login", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/login", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -43,7 +45,8 @@ int main() { } }); - svr.Post("/user/signup", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/signup", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -61,7 +64,8 @@ int main() { } }); - svr.Post("/user/progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -78,7 +82,8 @@ int main() { } }); - svr.Post("/user/increment-progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/increment-progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -144,6 +149,7 @@ int main() { } { + std::unique_lock lock(locker); ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); for (int i = 0; i < logs.size(); ++i) { From 84d2adf2166f92b973c216ac9e4919a859bb1996 Mon Sep 17 00:00:00 2001 From: denis Date: Sat, 28 May 2022 16:05:17 +0300 Subject: [PATCH 16/44] Add httplib.h in submodule, move resources, add network directory and cmake --- .gitmodules | 3 ++ CMakeLists.txt | 16 +++++++ core/loader/LevelLoaderFromFile.cpp | 4 +- deps/cpp-httplib | 1 + game/game_database_example.cpp | 4 +- game/game_with_default_loader_example.cpp | 2 +- network/CMakeLists.txt | 41 ++++++++++++++++++ network/client/client.cpp | 6 +++ network/server/server.cpp | 6 +++ .../animated/helicopter.png | Bin .../hero/idle/Chara - BlueIdle00000.png | Bin .../hero/idle/Chara - BlueIdle00001.png | Bin .../hero/idle/Chara - BlueIdle00002.png | Bin .../hero/idle/Chara - BlueIdle00003.png | Bin .../hero/idle/Chara - BlueIdle00004.png | Bin .../hero/idle/Chara - BlueIdle00005.png | Bin .../hero/idle/Chara - BlueIdle00006.png | Bin .../hero/idle/Chara - BlueIdle00007.png | Bin .../hero/idle/Chara - BlueIdle00008.png | Bin .../hero/idle/Chara - BlueIdle00009.png | Bin .../hero/idle/Chara - BlueIdle00010.png | Bin .../hero/idle/Chara - BlueIdle00011.png | Bin .../hero/idle/Chara - BlueIdle00012.png | Bin .../hero/idle/Chara - BlueIdle00013.png | Bin .../hero/idle/Chara - BlueIdle00014.png | Bin .../hero/idle/Chara - BlueIdle00015.png | Bin .../hero/idle/Chara - BlueIdle00016.png | Bin .../hero/idle/Chara - BlueIdle00017.png | Bin .../hero/idle/Chara - BlueIdle00018.png | Bin .../hero/idle/Chara - BlueIdle00019.png | Bin .../animated/hero/run/Chara_BlueWalk00000.png | Bin .../animated/hero/run/Chara_BlueWalk00001.png | Bin .../animated/hero/run/Chara_BlueWalk00002.png | Bin .../animated/hero/run/Chara_BlueWalk00003.png | Bin .../animated/hero/run/Chara_BlueWalk00004.png | Bin .../animated/hero/run/Chara_BlueWalk00005.png | Bin .../animated/hero/run/Chara_BlueWalk00006.png | Bin .../animated/hero/run/Chara_BlueWalk00007.png | Bin .../animated/hero/run/Chara_BlueWalk00008.png | Bin .../animated/hero/run/Chara_BlueWalk00009.png | Bin .../animated/hero/run/Chara_BlueWalk00010.png | Bin .../animated/hero/run/Chara_BlueWalk00011.png | Bin .../animated/hero/run/Chara_BlueWalk00012.png | Bin .../animated/hero/run/Chara_BlueWalk00013.png | Bin .../animated/hero/run/Chara_BlueWalk00014.png | Bin .../animated/hero/run/Chara_BlueWalk00015.png | Bin .../animated/hero/run/Chara_BlueWalk00016.png | Bin .../animated/hero/run/Chara_BlueWalk00017.png | Bin .../animated/hero/run/Chara_BlueWalk00018.png | Bin .../animated/hero/run/Chara_BlueWalk00019.png | Bin .../animated/runner_new.png | Bin .../levels/level_01/config.json | 2 +- .../levels/level_01/map | 0 .../levels/level_with_finish/config.json | 2 +- .../levels/level_with_finish/map | 0 .../resources => resources}/static/18plus.png | Bin .../resources => resources}/static/brick.png | Bin {game/resources => resources}/static/exit.png | Bin 58 files changed, 80 insertions(+), 7 deletions(-) create mode 160000 deps/cpp-httplib create mode 100644 network/CMakeLists.txt create mode 100644 network/client/client.cpp create mode 100644 network/server/server.cpp rename {game/resources => resources}/animated/helicopter.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00000.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00001.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00002.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00003.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00004.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00005.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00006.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00007.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00008.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00009.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00010.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00011.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00012.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00013.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00014.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00015.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00016.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00017.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00018.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00019.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00000.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00001.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00002.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00003.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00004.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00005.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00006.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00007.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00008.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00009.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00010.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00011.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00012.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00013.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00014.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00015.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00016.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00017.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00018.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00019.png (100%) rename {game/resources => resources}/animated/runner_new.png (100%) rename {game/resources => resources}/levels/level_01/config.json (92%) rename {game/resources => resources}/levels/level_01/map (100%) rename {game/resources => resources}/levels/level_with_finish/config.json (92%) rename {game/resources => resources}/levels/level_with_finish/map (100%) rename {game/resources => resources}/static/18plus.png (100%) rename {game/resources => resources}/static/brick.png (100%) rename {game/resources => resources}/static/exit.png (100%) diff --git a/.gitmodules b/.gitmodules index c319e98..952393c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "deps/libpqxx"] path = deps/libpqxx url = https://github.com/jtv/libpqxx +[submodule "deps/cpp-httplib"] + path = deps/cpp-httplib + url = https://github.com/yhirose/cpp-httplib diff --git a/CMakeLists.txt b/CMakeLists.txt index 928f804..1c50a15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,13 @@ add_subdirectory(${DIRECTORY_PQXX}) message(STATUS "PostgreSQL include: ${INCLUDE_PQXX}") message(STATUS "PostgreSQL library: ${LIBRARY_PQXX}") +# HTTP library +set(DIRECTORY_HTTP ${PROJECT_SOURCE_DIR}/deps/cpp-httplib) +set(INCLUDE_HTTP ${DIRECTORY_HTTP}) + +add_subdirectory(${DIRECTORY_HTTP}) +message(STATUS "HTTP include: ${INCLUDE_HTTP}") + # Core library set(DIRECTORY_CORE ${PROJECT_SOURCE_DIR}/core) set(INCLUDE_CORE ${DIRECTORY_CORE}) @@ -105,6 +112,15 @@ add_subdirectory(${DIRECTORY_GAME}) message(STATUS "Game executable: ${EXECUTABLE_GAME}") message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") +# Network binaries +set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) +set(EXECUTABLE_SERVER server) +set(EXECUTABLE_CLIENT client) + +add_subdirectory(${DIRECTORY_NETWORK}) +message(STATUS "Server executable: ${EXECUTABLE_SERVER}") +message(STATUS "Client executable ${EXECUTABLE_GAME_CLIENT}") + # Tests set(DIRECTORY_TEST ${PROJECT_SOURCE_DIR}/test) set(EXECUTABLE_TEST test) diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index 2201916..324c61a 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -133,7 +133,7 @@ namespace mad::core { void LevelLoaderFromFile::create_block(std::shared_ptr world, Vec2d position, float block_size, bool is_stable) { - std::filesystem::path source("../../game/resources/static/"); + std::filesystem::path source("../../resources/static/"); if (is_stable) { source /= static_cast(m_config_json["texture"]["stable"]); } else { @@ -203,7 +203,7 @@ namespace mad::core { } Entity::Id LevelLoaderFromFile::create_finish_block(std::shared_ptr world, Vec2d position, float block_size) { - std::filesystem::path source("../../game/resources/static/"); + std::filesystem::path source("../../resources/static/"); source /= static_cast(m_config_json["texture"]["finish"]); auto image_storage = std::make_shared( diff --git a/deps/cpp-httplib b/deps/cpp-httplib new file mode 160000 index 0000000..df20c27 --- /dev/null +++ b/deps/cpp-httplib @@ -0,0 +1 @@ +Subproject commit df20c27696c6dcb2b9ccecf3c4f9c3d06c1ecf8c diff --git a/game/game_database_example.cpp b/game/game_database_example.cpp index 48e0fe2..586b030 100644 --- a/game/game_database_example.cpp +++ b/game/game_database_example.cpp @@ -45,8 +45,8 @@ int main() { auto database_storage_driver = std::make_shared(database); std::vector> level_loaders{ - std::make_shared("../../game/resources/levels/level_with_finish"), - std::make_shared("../../game/resources/levels/level_01") + std::make_shared("../../resources/levels/level_with_finish"), + std::make_shared("../../resources/levels/level_01") }; auto game_runner = std::make_unique( diff --git a/game/game_with_default_loader_example.cpp b/game/game_with_default_loader_example.cpp index 337426b..a285992 100644 --- a/game/game_with_default_loader_example.cpp +++ b/game/game_with_default_loader_example.cpp @@ -42,7 +42,7 @@ int main() { auto offline_storage_driver = std::make_shared(); std::vector> level_loaders{ - std::make_shared("../../game/resources/levels/level_01") + std::make_shared("../../resources/levels/level_01") }; auto game_runner = std::make_unique( diff --git a/network/CMakeLists.txt b/network/CMakeLists.txt new file mode 100644 index 0000000..35f74e8 --- /dev/null +++ b/network/CMakeLists.txt @@ -0,0 +1,41 @@ +function(network_executable EXECUTABLE_NETWORK_NAME EXECUTABLE_SOURCES) + message(STATUS "Network executable: '${EXECUTABLE_NETWORK_NAME}' is built with ${EXECUTABLE_SOURCES}") + + add_executable( + ${EXECUTABLE_NETWORK_NAME} + ${EXECUTABLE_SOURCES} + ${SOURCES_IMGUI} + ${SOURCES_IMGUI_SFML} + ) + + if (CMAKE_BUILD_TYPE MATCHES Debug) + target_compile_definitions(${EXECUTABLE_NETWORK_NAME} PUBLIC SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE) + message(STATUS "Enable debug logs for ${EXECUTABLE_NETWORK_NAME}") + endif() + + target_include_directories( + ${EXECUTABLE_NETWORK_NAME} PUBLIC + + ${PROJECT_SOURCE_DIR} + ${INCLUDE_SPDLOG} + ${INCLUDE_BOX2D} + ${INCLUDE_IMGUI} + ${INCLUDE_IMGUI_SFML} + ${INCLUDE_HTTP} + ) + + target_link_libraries( + ${EXECUTABLE_NETWORK_NAME} + + ${LIBRARY_SFML} + ${LIBRARY_CORE} + ${LIBRARY_SPDLOG} + ${LIBRARY_BOX2D} + ${LIBRARY_OPENGL} + ${LIBRARY_PQXX} + ) + +endfunction() + +network_executable(${EXECUTABLE_SERVER} server/server.cpp) +network_executable(${EXECUTABLE_CLIENT} client/client.cpp) diff --git a/network/client/client.cpp b/network/client/client.cpp new file mode 100644 index 0000000..b4b83b3 --- /dev/null +++ b/network/client/client.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from client!" << '\n'; +} diff --git a/network/server/server.cpp b/network/server/server.cpp new file mode 100644 index 0000000..d60b858 --- /dev/null +++ b/network/server/server.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from server!" << '\n'; +} diff --git a/game/resources/animated/helicopter.png b/resources/animated/helicopter.png similarity index 100% rename from game/resources/animated/helicopter.png rename to resources/animated/helicopter.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00000.png b/resources/animated/hero/idle/Chara - BlueIdle00000.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00000.png rename to resources/animated/hero/idle/Chara - BlueIdle00000.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00001.png b/resources/animated/hero/idle/Chara - BlueIdle00001.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00001.png rename to resources/animated/hero/idle/Chara - BlueIdle00001.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00002.png b/resources/animated/hero/idle/Chara - BlueIdle00002.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00002.png rename to resources/animated/hero/idle/Chara - BlueIdle00002.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00003.png b/resources/animated/hero/idle/Chara - BlueIdle00003.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00003.png rename to resources/animated/hero/idle/Chara - BlueIdle00003.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00004.png b/resources/animated/hero/idle/Chara - BlueIdle00004.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00004.png rename to resources/animated/hero/idle/Chara - BlueIdle00004.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00005.png b/resources/animated/hero/idle/Chara - BlueIdle00005.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00005.png rename to resources/animated/hero/idle/Chara - BlueIdle00005.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00006.png b/resources/animated/hero/idle/Chara - BlueIdle00006.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00006.png rename to resources/animated/hero/idle/Chara - BlueIdle00006.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00007.png b/resources/animated/hero/idle/Chara - BlueIdle00007.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00007.png rename to resources/animated/hero/idle/Chara - BlueIdle00007.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00008.png b/resources/animated/hero/idle/Chara - BlueIdle00008.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00008.png rename to resources/animated/hero/idle/Chara - BlueIdle00008.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00009.png b/resources/animated/hero/idle/Chara - BlueIdle00009.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00009.png rename to resources/animated/hero/idle/Chara - BlueIdle00009.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00010.png b/resources/animated/hero/idle/Chara - BlueIdle00010.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00010.png rename to resources/animated/hero/idle/Chara - BlueIdle00010.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00011.png b/resources/animated/hero/idle/Chara - BlueIdle00011.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00011.png rename to resources/animated/hero/idle/Chara - BlueIdle00011.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00012.png b/resources/animated/hero/idle/Chara - BlueIdle00012.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00012.png rename to resources/animated/hero/idle/Chara - BlueIdle00012.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00013.png b/resources/animated/hero/idle/Chara - BlueIdle00013.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00013.png rename to resources/animated/hero/idle/Chara - BlueIdle00013.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00014.png b/resources/animated/hero/idle/Chara - BlueIdle00014.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00014.png rename to resources/animated/hero/idle/Chara - BlueIdle00014.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00015.png b/resources/animated/hero/idle/Chara - BlueIdle00015.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00015.png rename to resources/animated/hero/idle/Chara - BlueIdle00015.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00016.png b/resources/animated/hero/idle/Chara - BlueIdle00016.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00016.png rename to resources/animated/hero/idle/Chara - BlueIdle00016.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00017.png b/resources/animated/hero/idle/Chara - BlueIdle00017.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00017.png rename to resources/animated/hero/idle/Chara - BlueIdle00017.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00018.png b/resources/animated/hero/idle/Chara - BlueIdle00018.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00018.png rename to resources/animated/hero/idle/Chara - BlueIdle00018.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00019.png b/resources/animated/hero/idle/Chara - BlueIdle00019.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00019.png rename to resources/animated/hero/idle/Chara - BlueIdle00019.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00000.png b/resources/animated/hero/run/Chara_BlueWalk00000.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00000.png rename to resources/animated/hero/run/Chara_BlueWalk00000.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00001.png b/resources/animated/hero/run/Chara_BlueWalk00001.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00001.png rename to resources/animated/hero/run/Chara_BlueWalk00001.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00002.png b/resources/animated/hero/run/Chara_BlueWalk00002.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00002.png rename to resources/animated/hero/run/Chara_BlueWalk00002.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00003.png b/resources/animated/hero/run/Chara_BlueWalk00003.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00003.png rename to resources/animated/hero/run/Chara_BlueWalk00003.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00004.png b/resources/animated/hero/run/Chara_BlueWalk00004.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00004.png rename to resources/animated/hero/run/Chara_BlueWalk00004.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00005.png b/resources/animated/hero/run/Chara_BlueWalk00005.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00005.png rename to resources/animated/hero/run/Chara_BlueWalk00005.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00006.png b/resources/animated/hero/run/Chara_BlueWalk00006.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00006.png rename to resources/animated/hero/run/Chara_BlueWalk00006.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00007.png b/resources/animated/hero/run/Chara_BlueWalk00007.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00007.png rename to resources/animated/hero/run/Chara_BlueWalk00007.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00008.png b/resources/animated/hero/run/Chara_BlueWalk00008.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00008.png rename to resources/animated/hero/run/Chara_BlueWalk00008.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00009.png b/resources/animated/hero/run/Chara_BlueWalk00009.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00009.png rename to resources/animated/hero/run/Chara_BlueWalk00009.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00010.png b/resources/animated/hero/run/Chara_BlueWalk00010.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00010.png rename to resources/animated/hero/run/Chara_BlueWalk00010.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00011.png b/resources/animated/hero/run/Chara_BlueWalk00011.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00011.png rename to resources/animated/hero/run/Chara_BlueWalk00011.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00012.png b/resources/animated/hero/run/Chara_BlueWalk00012.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00012.png rename to resources/animated/hero/run/Chara_BlueWalk00012.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00013.png b/resources/animated/hero/run/Chara_BlueWalk00013.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00013.png rename to resources/animated/hero/run/Chara_BlueWalk00013.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00014.png b/resources/animated/hero/run/Chara_BlueWalk00014.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00014.png rename to resources/animated/hero/run/Chara_BlueWalk00014.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00015.png b/resources/animated/hero/run/Chara_BlueWalk00015.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00015.png rename to resources/animated/hero/run/Chara_BlueWalk00015.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00016.png b/resources/animated/hero/run/Chara_BlueWalk00016.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00016.png rename to resources/animated/hero/run/Chara_BlueWalk00016.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00017.png b/resources/animated/hero/run/Chara_BlueWalk00017.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00017.png rename to resources/animated/hero/run/Chara_BlueWalk00017.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00018.png b/resources/animated/hero/run/Chara_BlueWalk00018.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00018.png rename to resources/animated/hero/run/Chara_BlueWalk00018.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00019.png b/resources/animated/hero/run/Chara_BlueWalk00019.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00019.png rename to resources/animated/hero/run/Chara_BlueWalk00019.png diff --git a/game/resources/animated/runner_new.png b/resources/animated/runner_new.png similarity index 100% rename from game/resources/animated/runner_new.png rename to resources/animated/runner_new.png diff --git a/game/resources/levels/level_01/config.json b/resources/levels/level_01/config.json similarity index 92% rename from game/resources/levels/level_01/config.json rename to resources/levels/level_01/config.json index 4594a7c..e03da1a 100644 --- a/game/resources/levels/level_01/config.json +++ b/resources/levels/level_01/config.json @@ -1,6 +1,6 @@ { "name" : "level_01", - "animated_resources" : "../../game/resources/animated/", + "animated_resources" : "../../resources/animated/", "block" : 50.0, "camera": { "position" : { diff --git a/game/resources/levels/level_01/map b/resources/levels/level_01/map similarity index 100% rename from game/resources/levels/level_01/map rename to resources/levels/level_01/map diff --git a/game/resources/levels/level_with_finish/config.json b/resources/levels/level_with_finish/config.json similarity index 92% rename from game/resources/levels/level_with_finish/config.json rename to resources/levels/level_with_finish/config.json index 6482a2a..edfee15 100644 --- a/game/resources/levels/level_with_finish/config.json +++ b/resources/levels/level_with_finish/config.json @@ -1,6 +1,6 @@ { "name" : "level_with_finish", - "animated_resources" : "../../game/resources/animated/", + "animated_resources" : "../../resources/animated/", "block" : 50.0, "camera": { "position" : { diff --git a/game/resources/levels/level_with_finish/map b/resources/levels/level_with_finish/map similarity index 100% rename from game/resources/levels/level_with_finish/map rename to resources/levels/level_with_finish/map diff --git a/game/resources/static/18plus.png b/resources/static/18plus.png similarity index 100% rename from game/resources/static/18plus.png rename to resources/static/18plus.png diff --git a/game/resources/static/brick.png b/resources/static/brick.png similarity index 100% rename from game/resources/static/brick.png rename to resources/static/brick.png diff --git a/game/resources/static/exit.png b/resources/static/exit.png similarity index 100% rename from game/resources/static/exit.png rename to resources/static/exit.png From 995cbe3b6cbd8e030d8fe067d876b92902ec81a9 Mon Sep 17 00:00:00 2001 From: denis Date: Sun, 29 May 2022 19:06:49 +0300 Subject: [PATCH 17/44] Test for httplib --- test/CMakeLists.txt | 1 + test/deps/CMakeLists.txt | 2 + test/deps/network/client.cpp | 22 +++++ test/deps/network/server.cpp | 155 +++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 test/deps/network/client.cpp create mode 100644 test/deps/network/server.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 07bb365..bc2aa5e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,6 +18,7 @@ function(test_executable EXECUTABLE_TEST_NAME EXECUTABLE_SOURCES) ${INCLUDE_IMGUI} ${INCLUDE_IMGUI_SFML} ${INCLUDE_PQXX} + ${INCLUDE_HTTP} ) target_link_libraries( diff --git a/test/deps/CMakeLists.txt b/test/deps/CMakeLists.txt index 3078da8..817960a 100644 --- a/test/deps/CMakeLists.txt +++ b/test/deps/CMakeLists.txt @@ -1,3 +1,5 @@ test_executable(test-box2d TestBox2d.cpp) test_executable(test-imgui TestImGui.cpp) test_executable(test-pqxx TestPQXX.cpp) +test_executable(test-server network/server.cpp) +test_executable(test-client network/client.cpp) diff --git a/test/deps/network/client.cpp b/test/deps/network/client.cpp new file mode 100644 index 0000000..198b44a --- /dev/null +++ b/test/deps/network/client.cpp @@ -0,0 +1,22 @@ +#include +#include + +int main() { + httplib::Client cli("localhost", 8080); + + auto res1 = cli.Post("/user/login", httplib::Params{ + {"username", "Denis"} + }); + std::cout << res1->status << ' ' << res1->body << '\n'; + + + auto res2 = cli.Post("/user/login", httplib::Params{ + {"username", "NoDenis"} + }); + std::cout << res2->status << ' ' << res2->body << '\n'; + + auto res3 = cli.Post("/user/login", httplib::Params{ + {"name", "Denis"} + }); + std::cout << res3->status << ' ' << res3->body << '\n'; +} diff --git a/test/deps/network/server.cpp b/test/deps/network/server.cpp new file mode 100644 index 0000000..e4038c3 --- /dev/null +++ b/test/deps/network/server.cpp @@ -0,0 +1,155 @@ +#include +#include +#include + +namespace mad::core { + class Database { + public: + Database(); + + bool is_user_exists(const std::string &username); + + void registry_user(const std::string &username); + + std::size_t get_id(const std::string &username); + + std::size_t get_progress(std::size_t id); + + std::size_t get_progress(const std::string &username); + + void increment_progress(std::size_t id); + + void increment_progress(const std::string &username); + + void reset_progress(std::size_t id); + + void reset_progress(const std::string &username); + + private: + pqxx::connection m_connector; + std::string m_query; + + }; +} + +namespace mad::core { + + Database::Database() : m_connector("dbname = test-network") { + try { + if (m_connector.is_open()) { + SPDLOG_DEBUG("Database mad opened successfully"); + } else { + SPDLOG_DEBUG("Can't open database mad"); + } + + pqxx::work w(m_connector); + + m_query = "CREATE TABLE IF NOT EXISTS users(" + "id SMALLINT PRIMARY KEY," + "name TEXT NOT NULL UNIQUE);"; + w.exec(m_query); + + m_query = "CREATE TABLE IF NOT EXISTS progress(" + "id SMALLINT PRIMARY KEY REFERENCES users (id)," + "levels_completed SMALLINT NOT NULL);"; + w.exec(m_query); + + w.commit(); + + SPDLOG_DEBUG("Tables created successfully"); + } catch (std::exception &exc) { + SPDLOG_INFO(exc.what()); + } + } + + bool Database::is_user_exists(const std::string &username) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM users WHERE name = '" + W.esc(username) + "';"; + pqxx::result rows_found = W.exec(m_query); + return !rows_found.empty(); + } + + void Database::registry_user(const std::string &username) { + pqxx::work W(m_connector); + + m_query = "SELECT id FROM users"; + pqxx::result total_rows = W.exec(m_query); + std::size_t id = total_rows.size(); + + m_query = "INSERT INTO users(id, name) VALUES(" + std::to_string(id) + ", '" + W.esc(username) + "');"; + W.exec(m_query); + + m_query = "INSERT INTO progress(id, levels_completed) VALUES(" + std::to_string(id) + ", 0);"; + W.exec(m_query); + + W.commit(); + } + + std::size_t Database::get_id(const std::string &username) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM users WHERE name = '" + W.esc(username) + "';"; + auto found_row = W.exec1(m_query); + return found_row["id"].as(); + } + + std::size_t Database::get_progress(std::size_t id) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM progress WHERE id = " + std::to_string(id) + ";"; + auto found_row = W.exec1(m_query); + return found_row["levels_completed"].as(); + } + + std::size_t Database::get_progress(const std::string &username) { + return get_progress(get_id(username)); + } + + void Database::increment_progress(std::size_t id) { + pqxx::work W(m_connector); + m_query = "UPDATE progress " + "SET levels_completed = levels_completed + 1 " + "WHERE id = " + std::to_string(id) + ";"; + W.exec(m_query); + W.commit(); + } + + void Database::increment_progress(const std::string &username) { + increment_progress(get_id(username)); + } + + void Database::reset_progress(std::size_t id) { + pqxx::work W(m_connector); + m_query = "UPDATE progress " + "SET levels_completed = 0 " + "WHERE id = " + std::to_string(id) + ";"; + W.exec(m_query); + W.commit(); + } + + void Database::reset_progress(const std::string &username) { + reset_progress(get_id(username)); + } +} + +int main() { + httplib::Server svr; + mad::core::Database db; + + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = "OK"; + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + + svr.listen("localhost", 8080); +} \ No newline at end of file From 72b1e90989879330ea2ba6309d460223351623ef Mon Sep 17 00:00:00 2001 From: denis Date: Sun, 29 May 2022 20:01:02 +0300 Subject: [PATCH 18/44] Add simple client and server realisation --- CMakeLists.txt | 4 +- network/CMakeLists.txt | 4 +- network/client/client.cpp | 6 -- network/client/simple-client.cpp | 108 +++++++++++++++++++++++++++++++ network/server/server.cpp | 6 -- network/server/simple-server.cpp | 77 ++++++++++++++++++++++ 6 files changed, 189 insertions(+), 16 deletions(-) delete mode 100644 network/client/client.cpp create mode 100644 network/client/simple-client.cpp delete mode 100644 network/server/server.cpp create mode 100644 network/server/simple-server.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c50a15..01687c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,8 +114,8 @@ message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") # Network binaries set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) -set(EXECUTABLE_SERVER server) -set(EXECUTABLE_CLIENT client) +set(EXECUTABLE_SIMPLE_SERVER simple-server) +set(EXECUTABLE_SIMPLE_CLIENT simple-client) add_subdirectory(${DIRECTORY_NETWORK}) message(STATUS "Server executable: ${EXECUTABLE_SERVER}") diff --git a/network/CMakeLists.txt b/network/CMakeLists.txt index 35f74e8..69e16eb 100644 --- a/network/CMakeLists.txt +++ b/network/CMakeLists.txt @@ -37,5 +37,5 @@ function(network_executable EXECUTABLE_NETWORK_NAME EXECUTABLE_SOURCES) endfunction() -network_executable(${EXECUTABLE_SERVER} server/server.cpp) -network_executable(${EXECUTABLE_CLIENT} client/client.cpp) +network_executable(${EXECUTABLE_SIMPLE_SERVER} server/simple-server.cpp) +network_executable(${EXECUTABLE_SIMPLE_CLIENT} client/simple-client.cpp) diff --git a/network/client/client.cpp b/network/client/client.cpp deleted file mode 100644 index b4b83b3..0000000 --- a/network/client/client.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int main() { - std::cout << "Hello from client!" << '\n'; -} diff --git a/network/client/simple-client.cpp b/network/client/simple-client.cpp new file mode 100644 index 0000000..26049c9 --- /dev/null +++ b/network/client/simple-client.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace mad::core { + + class NetworkClientStorageDriver : public ClientStorageDriver { + public: + explicit NetworkClientStorageDriver() : m_client("localhost", 8080) { } + + bool log_in(const std::string &username) const override { + auto res = m_client.Post("/user/login", httplib::Params{ + {"username", username} + }); + SPDLOG_DEBUG("Login request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + if (res->status == 200) { + m_username = username; + } + return res->status == 200; + } + + bool sign_up(const std::string &username) override { + auto res = m_client.Post("/user/signup", httplib::Params{ + {"username", username} + }); + SPDLOG_DEBUG("Sign up request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + return res->status == 200; + } + + std::size_t get_progress() const override { + auto res = m_client.Post("/user/progress", httplib::Params{ + {"username", m_username} + }); + SPDLOG_DEBUG("Get progress request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + return std::stoi(res->body); + } + + void update_progress() override { + auto res = m_client.Post("/user/increment-progress", httplib::Params{ + {"username", m_username} + }); + SPDLOG_DEBUG("Update request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + } + + private: + mutable std::string m_username; + mutable httplib::Client m_client; + + }; + +} + +int main() { +#ifndef NDEBUG + auto log_level = spdlog::level::trace; +#else + auto log_level = spdlog::level::info; +#endif + spdlog::set_level(log_level); + + auto window = std::make_shared(sf::VideoMode(640, 480), "MAD"); + ImGui::SFML::Init(*window); + window->setFramerateLimit(120); + + auto global_dispatcher = std::make_shared(); + + auto system_listener = std::make_shared(window); + + std::vector> level_loaders{ + std::make_shared("../../resources/levels/level_with_finish"), + std::make_shared("../../resources/levels/level_01") + }; + + auto network_storage_driver = std::make_shared(); + + auto game_runner = std::make_unique( + level_loaders, + global_dispatcher, + std::make_unique(), + std::make_unique(network_storage_driver), + system_listener, + network_storage_driver + ); + + global_dispatcher->registry(std::make_shared(*window)); + global_dispatcher->registry(std::make_shared(*game_runner)); + global_dispatcher->registry(std::make_shared(*game_runner, network_storage_driver)); + global_dispatcher->registry(std::make_shared(*game_runner)); + + game_runner->run(*window); + + ImGui::SFML::Shutdown(); +} diff --git a/network/server/server.cpp b/network/server/server.cpp deleted file mode 100644 index d60b858..0000000 --- a/network/server/server.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int main() { - std::cout << "Hello from server!" << '\n'; -} diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp new file mode 100644 index 0000000..5c85588 --- /dev/null +++ b/network/server/simple-server.cpp @@ -0,0 +1,77 @@ +#include + +#include +#include + +int main() { + httplib::Server svr; + mad::core::Database db; + + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = "OK"; + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.Post("/user/signup", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 404; + res.body = "User already exists"; + } else { + db.registry_user(username); + res.status = 200; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.Post("/user/progress", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = std::to_string(db.get_progress(username)); + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.Post("/user/increment-progress", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = "OK"; + db.increment_progress(username); + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.listen("localhost", 8080); +} From 9b3b98fa8b9c5a990898da0e256d43cdd213a9ae Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 11:56:34 +0300 Subject: [PATCH 19/44] Chain server and window --- network/client/simple-client.cpp | 5 +++- network/server/simple-server.cpp | 45 +++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/network/client/simple-client.cpp b/network/client/simple-client.cpp index 26049c9..d35f011 100644 --- a/network/client/simple-client.cpp +++ b/network/client/simple-client.cpp @@ -21,7 +21,10 @@ namespace mad::core { class NetworkClientStorageDriver : public ClientStorageDriver { public: - explicit NetworkClientStorageDriver() : m_client("localhost", 8080) { } + explicit NetworkClientStorageDriver() : m_client("localhost", 8080) { + auto res = m_client.Get("/connection"); + SPDLOG_DEBUG("Connection request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + } bool log_in(const std::string &username) const override { auto res = m_client.Post("/user/login", httplib::Params{ diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 5c85588..d51b1a6 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -2,11 +2,25 @@ #include #include +#include + +#include +#include + +#include +#include +#include + int main() { httplib::Server svr; mad::core::Database db; + svr.Get("/connection", [](const httplib::Request &req, httplib::Response &res) { + res.status = 200; + res.body = "Connected user port -- " + std::to_string(req.remote_port); + }); + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); @@ -73,5 +87,34 @@ int main() { } }); - svr.listen("localhost", 8080); + std::thread([&svr]() mutable { + svr.listen("localhost", 8080); + }).detach(); + + sf::RenderWindow window(sf::VideoMode(640, 480), "MAD Server"); + ImGui::SFML::Init(window); + window.setFramerateLimit(120); + sf::Clock clock; + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + ImGui::SFML::ProcessEvent(event); + + if (event.type == sf::Event::Closed) { + window.close(); + svr.stop(); + } + } + + ImGui::SFML::Update(window, clock.restart()); + + ImGui::Begin("Window"); + ImGui::Text("Hello"); + ImGui::End(); + + window.clear(); + ImGui::SFML::Render(window); + window.display(); + } + ImGui::SFML::Shutdown(); } From 205ff02f1f0e19f9fe482092accda9eefe664c17 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 12:42:20 +0300 Subject: [PATCH 20/44] Add server control buttons --- network/server/simple-server.cpp | 38 +++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index d51b1a6..2f1ae43 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -46,7 +46,7 @@ int main() { } else { db.registry_user(username); res.status = 200; - res.body = "User doesn\'t exists"; + res.body = "User " + username + " is registered"; } } else { res.status = 404; @@ -87,10 +87,6 @@ int main() { } }); - std::thread([&svr]() mutable { - svr.listen("localhost", 8080); - }).detach(); - sf::RenderWindow window(sf::VideoMode(640, 480), "MAD Server"); ImGui::SFML::Init(window); window.setFramerateLimit(120); @@ -102,14 +98,40 @@ int main() { if (event.type == sf::Event::Closed) { window.close(); - svr.stop(); + if (svr.is_running()) { + svr.stop(); + } } } ImGui::SFML::Update(window, clock.restart()); - ImGui::Begin("Window"); - ImGui::Text("Hello"); + ImGui::SetNextWindowSize(ImVec2(window.getSize().x, window.getSize().y)); + ImGui::SetNextWindowPos({0, 0}); + ImGui::Begin("Server util", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); + + + if (ImGui::Button("Start server")) { + if (!svr.is_running()) { + std::thread([&svr]() mutable { + svr.listen("localhost", 8080); + }).detach(); + } + } + + if (ImGui::Button("Stop server")) { + if (svr.is_running()) { + svr.stop(); + } + } + + if (ImGui::Button("Quit")) { + window.close(); + if (svr.is_running()) { + svr.stop(); + } + } + ImGui::End(); window.clear(); From acc028ebb4b48585989858ca91ff9fd3b883deca Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 15:59:14 +0300 Subject: [PATCH 21/44] Add server logs --- network/server/simple-server.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 2f1ae43..c8934f1 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -16,17 +17,22 @@ int main() { httplib::Server svr; mad::core::Database db; - svr.Get("/connection", [](const httplib::Request &req, httplib::Response &res) { + std::vector logs; + const char *log; + + svr.Get("/connection", [&logs](const httplib::Request &req, httplib::Response &res) { res.status = 200; res.body = "Connected user port -- " + std::to_string(req.remote_port); + logs.push_back(res.body); }); - svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/login", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; + logs.push_back("User " + std::to_string(req.remote_port) + " login as " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -37,12 +43,13 @@ int main() { } }); - svr.Post("/user/signup", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/signup", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 404; res.body = "User already exists"; + logs.push_back("Register new user " + username + " from port " + std::to_string(req.remote_port)); } else { db.registry_user(username); res.status = 200; @@ -54,12 +61,13 @@ int main() { } }); - svr.Post("/user/progress", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = std::to_string(db.get_progress(username)); + logs.push_back("Send progress to user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -70,13 +78,14 @@ int main() { } }); - svr.Post("/user/increment-progress", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/increment-progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; db.increment_progress(username); + logs.push_back("Increment progress for user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -116,12 +125,14 @@ int main() { std::thread([&svr]() mutable { svr.listen("localhost", 8080); }).detach(); + logs.emplace_back("Server has started"); } } if (ImGui::Button("Stop server")) { if (svr.is_running()) { svr.stop(); + logs.emplace_back("Server has stopped"); } } @@ -132,6 +143,15 @@ int main() { } } + { + ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; + ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); + for (int i = 0; i < logs.size(); ++i) { + ImGui::Text(logs[i].c_str(), i); + } + ImGui::EndChild(); + } + ImGui::End(); window.clear(); From 2af56987bb6d04ffad18683d94d45883a3df3511 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 16:32:47 +0300 Subject: [PATCH 22/44] Add mutex for logs and db --- network/server/simple-server.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index c8934f1..1fef0fe 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -1,9 +1,9 @@ #include -#include #include #include #include +#include #include #include @@ -17,16 +17,18 @@ int main() { httplib::Server svr; mad::core::Database db; + std::mutex locker; std::vector logs; - const char *log; - svr.Get("/connection", [&logs](const httplib::Request &req, httplib::Response &res) { + svr.Get("/connection", [&logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); res.status = 200; - res.body = "Connected user port -- " + std::to_string(req.remote_port); - logs.push_back(res.body); + res.body = "Connection successful"; + logs.push_back("Connected user port -- " + std::to_string(req.remote_port)); }); - svr.Post("/user/login", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/login", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -43,7 +45,8 @@ int main() { } }); - svr.Post("/user/signup", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/signup", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -61,7 +64,8 @@ int main() { } }); - svr.Post("/user/progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -78,7 +82,8 @@ int main() { } }); - svr.Post("/user/increment-progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/increment-progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -144,6 +149,7 @@ int main() { } { + std::unique_lock lock(locker); ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); for (int i = 0; i < logs.size(); ++i) { From fe12638dc0e31cd360cf00b413ef28fa09eb1493 Mon Sep 17 00:00:00 2001 From: denis Date: Mon, 30 May 2022 20:46:24 +0300 Subject: [PATCH 23/44] Provide rebase --- network/server/simple-server.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 1fef0fe..611561e 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -164,5 +164,6 @@ int main() { ImGui::SFML::Render(window); window.display(); } + ImGui::SFML::Shutdown(); } From 8560e00499f55533195c231c1ab7a9a3247e05da Mon Sep 17 00:00:00 2001 From: denis Date: Sat, 28 May 2022 16:05:17 +0300 Subject: [PATCH 24/44] Add httplib.h in submodule, move resources, add network directory and cmake --- .gitmodules | 3 ++ CMakeLists.txt | 16 +++++++ core/loader/LevelLoaderFromFile.cpp | 4 +- deps/cpp-httplib | 1 + game/game_database_example.cpp | 4 +- game/game_with_default_loader_example.cpp | 2 +- network/CMakeLists.txt | 41 ++++++++++++++++++ network/client/client.cpp | 6 +++ network/server/server.cpp | 6 +++ .../animated/helicopter.png | Bin .../hero/idle/Chara - BlueIdle00000.png | Bin .../hero/idle/Chara - BlueIdle00001.png | Bin .../hero/idle/Chara - BlueIdle00002.png | Bin .../hero/idle/Chara - BlueIdle00003.png | Bin .../hero/idle/Chara - BlueIdle00004.png | Bin .../hero/idle/Chara - BlueIdle00005.png | Bin .../hero/idle/Chara - BlueIdle00006.png | Bin .../hero/idle/Chara - BlueIdle00007.png | Bin .../hero/idle/Chara - BlueIdle00008.png | Bin .../hero/idle/Chara - BlueIdle00009.png | Bin .../hero/idle/Chara - BlueIdle00010.png | Bin .../hero/idle/Chara - BlueIdle00011.png | Bin .../hero/idle/Chara - BlueIdle00012.png | Bin .../hero/idle/Chara - BlueIdle00013.png | Bin .../hero/idle/Chara - BlueIdle00014.png | Bin .../hero/idle/Chara - BlueIdle00015.png | Bin .../hero/idle/Chara - BlueIdle00016.png | Bin .../hero/idle/Chara - BlueIdle00017.png | Bin .../hero/idle/Chara - BlueIdle00018.png | Bin .../hero/idle/Chara - BlueIdle00019.png | Bin .../animated/hero/run/Chara_BlueWalk00000.png | Bin .../animated/hero/run/Chara_BlueWalk00001.png | Bin .../animated/hero/run/Chara_BlueWalk00002.png | Bin .../animated/hero/run/Chara_BlueWalk00003.png | Bin .../animated/hero/run/Chara_BlueWalk00004.png | Bin .../animated/hero/run/Chara_BlueWalk00005.png | Bin .../animated/hero/run/Chara_BlueWalk00006.png | Bin .../animated/hero/run/Chara_BlueWalk00007.png | Bin .../animated/hero/run/Chara_BlueWalk00008.png | Bin .../animated/hero/run/Chara_BlueWalk00009.png | Bin .../animated/hero/run/Chara_BlueWalk00010.png | Bin .../animated/hero/run/Chara_BlueWalk00011.png | Bin .../animated/hero/run/Chara_BlueWalk00012.png | Bin .../animated/hero/run/Chara_BlueWalk00013.png | Bin .../animated/hero/run/Chara_BlueWalk00014.png | Bin .../animated/hero/run/Chara_BlueWalk00015.png | Bin .../animated/hero/run/Chara_BlueWalk00016.png | Bin .../animated/hero/run/Chara_BlueWalk00017.png | Bin .../animated/hero/run/Chara_BlueWalk00018.png | Bin .../animated/hero/run/Chara_BlueWalk00019.png | Bin .../animated/runner_new.png | Bin .../levels/level_01/config.json | 2 +- .../levels/level_01/map | 0 .../levels/level_with_finish/config.json | 2 +- .../levels/level_with_finish/map | 0 .../resources => resources}/static/18plus.png | Bin .../resources => resources}/static/brick.png | Bin {game/resources => resources}/static/exit.png | Bin 58 files changed, 80 insertions(+), 7 deletions(-) create mode 160000 deps/cpp-httplib create mode 100644 network/CMakeLists.txt create mode 100644 network/client/client.cpp create mode 100644 network/server/server.cpp rename {game/resources => resources}/animated/helicopter.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00000.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00001.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00002.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00003.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00004.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00005.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00006.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00007.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00008.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00009.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00010.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00011.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00012.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00013.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00014.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00015.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00016.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00017.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00018.png (100%) rename {game/resources => resources}/animated/hero/idle/Chara - BlueIdle00019.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00000.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00001.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00002.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00003.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00004.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00005.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00006.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00007.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00008.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00009.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00010.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00011.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00012.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00013.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00014.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00015.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00016.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00017.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00018.png (100%) rename {game/resources => resources}/animated/hero/run/Chara_BlueWalk00019.png (100%) rename {game/resources => resources}/animated/runner_new.png (100%) rename {game/resources => resources}/levels/level_01/config.json (93%) rename {game/resources => resources}/levels/level_01/map (100%) rename {game/resources => resources}/levels/level_with_finish/config.json (93%) rename {game/resources => resources}/levels/level_with_finish/map (100%) rename {game/resources => resources}/static/18plus.png (100%) rename {game/resources => resources}/static/brick.png (100%) rename {game/resources => resources}/static/exit.png (100%) diff --git a/.gitmodules b/.gitmodules index c319e98..952393c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "deps/libpqxx"] path = deps/libpqxx url = https://github.com/jtv/libpqxx +[submodule "deps/cpp-httplib"] + path = deps/cpp-httplib + url = https://github.com/yhirose/cpp-httplib diff --git a/CMakeLists.txt b/CMakeLists.txt index 928f804..1c50a15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,13 @@ add_subdirectory(${DIRECTORY_PQXX}) message(STATUS "PostgreSQL include: ${INCLUDE_PQXX}") message(STATUS "PostgreSQL library: ${LIBRARY_PQXX}") +# HTTP library +set(DIRECTORY_HTTP ${PROJECT_SOURCE_DIR}/deps/cpp-httplib) +set(INCLUDE_HTTP ${DIRECTORY_HTTP}) + +add_subdirectory(${DIRECTORY_HTTP}) +message(STATUS "HTTP include: ${INCLUDE_HTTP}") + # Core library set(DIRECTORY_CORE ${PROJECT_SOURCE_DIR}/core) set(INCLUDE_CORE ${DIRECTORY_CORE}) @@ -105,6 +112,15 @@ add_subdirectory(${DIRECTORY_GAME}) message(STATUS "Game executable: ${EXECUTABLE_GAME}") message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") +# Network binaries +set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) +set(EXECUTABLE_SERVER server) +set(EXECUTABLE_CLIENT client) + +add_subdirectory(${DIRECTORY_NETWORK}) +message(STATUS "Server executable: ${EXECUTABLE_SERVER}") +message(STATUS "Client executable ${EXECUTABLE_GAME_CLIENT}") + # Tests set(DIRECTORY_TEST ${PROJECT_SOURCE_DIR}/test) set(EXECUTABLE_TEST test) diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index 1909ea8..45bfe37 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -136,7 +136,7 @@ namespace mad::core { void LevelLoaderFromFile::create_block(std::shared_ptr world, Vec2d position, float block_size, bool is_stable) { - std::filesystem::path source("../../game/resources/static/"); + std::filesystem::path source("../../resources/static/"); if (is_stable) { source /= static_cast(m_config_json["texture"]["stable"]); } else { @@ -206,7 +206,7 @@ namespace mad::core { } Entity::Id LevelLoaderFromFile::create_finish_block(std::shared_ptr world, Vec2d position, float block_size) { - std::filesystem::path source("../../game/resources/static/"); + std::filesystem::path source("../../resources/static/"); source /= static_cast(m_config_json["texture"]["finish"]); auto image_storage = std::make_shared( diff --git a/deps/cpp-httplib b/deps/cpp-httplib new file mode 160000 index 0000000..df20c27 --- /dev/null +++ b/deps/cpp-httplib @@ -0,0 +1 @@ +Subproject commit df20c27696c6dcb2b9ccecf3c4f9c3d06c1ecf8c diff --git a/game/game_database_example.cpp b/game/game_database_example.cpp index 48e0fe2..586b030 100644 --- a/game/game_database_example.cpp +++ b/game/game_database_example.cpp @@ -45,8 +45,8 @@ int main() { auto database_storage_driver = std::make_shared(database); std::vector> level_loaders{ - std::make_shared("../../game/resources/levels/level_with_finish"), - std::make_shared("../../game/resources/levels/level_01") + std::make_shared("../../resources/levels/level_with_finish"), + std::make_shared("../../resources/levels/level_01") }; auto game_runner = std::make_unique( diff --git a/game/game_with_default_loader_example.cpp b/game/game_with_default_loader_example.cpp index cce779a..37cafe8 100644 --- a/game/game_with_default_loader_example.cpp +++ b/game/game_with_default_loader_example.cpp @@ -42,7 +42,7 @@ int main() { auto offline_storage_driver = std::make_shared(); std::vector> level_loaders{ - std::make_shared("../../game/resources/levels/level_01") + std::make_shared("../../resources/levels/level_01") }; auto game_runner = std::make_unique( diff --git a/network/CMakeLists.txt b/network/CMakeLists.txt new file mode 100644 index 0000000..35f74e8 --- /dev/null +++ b/network/CMakeLists.txt @@ -0,0 +1,41 @@ +function(network_executable EXECUTABLE_NETWORK_NAME EXECUTABLE_SOURCES) + message(STATUS "Network executable: '${EXECUTABLE_NETWORK_NAME}' is built with ${EXECUTABLE_SOURCES}") + + add_executable( + ${EXECUTABLE_NETWORK_NAME} + ${EXECUTABLE_SOURCES} + ${SOURCES_IMGUI} + ${SOURCES_IMGUI_SFML} + ) + + if (CMAKE_BUILD_TYPE MATCHES Debug) + target_compile_definitions(${EXECUTABLE_NETWORK_NAME} PUBLIC SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE) + message(STATUS "Enable debug logs for ${EXECUTABLE_NETWORK_NAME}") + endif() + + target_include_directories( + ${EXECUTABLE_NETWORK_NAME} PUBLIC + + ${PROJECT_SOURCE_DIR} + ${INCLUDE_SPDLOG} + ${INCLUDE_BOX2D} + ${INCLUDE_IMGUI} + ${INCLUDE_IMGUI_SFML} + ${INCLUDE_HTTP} + ) + + target_link_libraries( + ${EXECUTABLE_NETWORK_NAME} + + ${LIBRARY_SFML} + ${LIBRARY_CORE} + ${LIBRARY_SPDLOG} + ${LIBRARY_BOX2D} + ${LIBRARY_OPENGL} + ${LIBRARY_PQXX} + ) + +endfunction() + +network_executable(${EXECUTABLE_SERVER} server/server.cpp) +network_executable(${EXECUTABLE_CLIENT} client/client.cpp) diff --git a/network/client/client.cpp b/network/client/client.cpp new file mode 100644 index 0000000..b4b83b3 --- /dev/null +++ b/network/client/client.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from client!" << '\n'; +} diff --git a/network/server/server.cpp b/network/server/server.cpp new file mode 100644 index 0000000..d60b858 --- /dev/null +++ b/network/server/server.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from server!" << '\n'; +} diff --git a/game/resources/animated/helicopter.png b/resources/animated/helicopter.png similarity index 100% rename from game/resources/animated/helicopter.png rename to resources/animated/helicopter.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00000.png b/resources/animated/hero/idle/Chara - BlueIdle00000.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00000.png rename to resources/animated/hero/idle/Chara - BlueIdle00000.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00001.png b/resources/animated/hero/idle/Chara - BlueIdle00001.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00001.png rename to resources/animated/hero/idle/Chara - BlueIdle00001.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00002.png b/resources/animated/hero/idle/Chara - BlueIdle00002.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00002.png rename to resources/animated/hero/idle/Chara - BlueIdle00002.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00003.png b/resources/animated/hero/idle/Chara - BlueIdle00003.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00003.png rename to resources/animated/hero/idle/Chara - BlueIdle00003.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00004.png b/resources/animated/hero/idle/Chara - BlueIdle00004.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00004.png rename to resources/animated/hero/idle/Chara - BlueIdle00004.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00005.png b/resources/animated/hero/idle/Chara - BlueIdle00005.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00005.png rename to resources/animated/hero/idle/Chara - BlueIdle00005.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00006.png b/resources/animated/hero/idle/Chara - BlueIdle00006.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00006.png rename to resources/animated/hero/idle/Chara - BlueIdle00006.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00007.png b/resources/animated/hero/idle/Chara - BlueIdle00007.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00007.png rename to resources/animated/hero/idle/Chara - BlueIdle00007.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00008.png b/resources/animated/hero/idle/Chara - BlueIdle00008.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00008.png rename to resources/animated/hero/idle/Chara - BlueIdle00008.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00009.png b/resources/animated/hero/idle/Chara - BlueIdle00009.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00009.png rename to resources/animated/hero/idle/Chara - BlueIdle00009.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00010.png b/resources/animated/hero/idle/Chara - BlueIdle00010.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00010.png rename to resources/animated/hero/idle/Chara - BlueIdle00010.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00011.png b/resources/animated/hero/idle/Chara - BlueIdle00011.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00011.png rename to resources/animated/hero/idle/Chara - BlueIdle00011.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00012.png b/resources/animated/hero/idle/Chara - BlueIdle00012.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00012.png rename to resources/animated/hero/idle/Chara - BlueIdle00012.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00013.png b/resources/animated/hero/idle/Chara - BlueIdle00013.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00013.png rename to resources/animated/hero/idle/Chara - BlueIdle00013.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00014.png b/resources/animated/hero/idle/Chara - BlueIdle00014.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00014.png rename to resources/animated/hero/idle/Chara - BlueIdle00014.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00015.png b/resources/animated/hero/idle/Chara - BlueIdle00015.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00015.png rename to resources/animated/hero/idle/Chara - BlueIdle00015.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00016.png b/resources/animated/hero/idle/Chara - BlueIdle00016.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00016.png rename to resources/animated/hero/idle/Chara - BlueIdle00016.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00017.png b/resources/animated/hero/idle/Chara - BlueIdle00017.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00017.png rename to resources/animated/hero/idle/Chara - BlueIdle00017.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00018.png b/resources/animated/hero/idle/Chara - BlueIdle00018.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00018.png rename to resources/animated/hero/idle/Chara - BlueIdle00018.png diff --git a/game/resources/animated/hero/idle/Chara - BlueIdle00019.png b/resources/animated/hero/idle/Chara - BlueIdle00019.png similarity index 100% rename from game/resources/animated/hero/idle/Chara - BlueIdle00019.png rename to resources/animated/hero/idle/Chara - BlueIdle00019.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00000.png b/resources/animated/hero/run/Chara_BlueWalk00000.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00000.png rename to resources/animated/hero/run/Chara_BlueWalk00000.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00001.png b/resources/animated/hero/run/Chara_BlueWalk00001.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00001.png rename to resources/animated/hero/run/Chara_BlueWalk00001.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00002.png b/resources/animated/hero/run/Chara_BlueWalk00002.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00002.png rename to resources/animated/hero/run/Chara_BlueWalk00002.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00003.png b/resources/animated/hero/run/Chara_BlueWalk00003.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00003.png rename to resources/animated/hero/run/Chara_BlueWalk00003.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00004.png b/resources/animated/hero/run/Chara_BlueWalk00004.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00004.png rename to resources/animated/hero/run/Chara_BlueWalk00004.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00005.png b/resources/animated/hero/run/Chara_BlueWalk00005.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00005.png rename to resources/animated/hero/run/Chara_BlueWalk00005.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00006.png b/resources/animated/hero/run/Chara_BlueWalk00006.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00006.png rename to resources/animated/hero/run/Chara_BlueWalk00006.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00007.png b/resources/animated/hero/run/Chara_BlueWalk00007.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00007.png rename to resources/animated/hero/run/Chara_BlueWalk00007.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00008.png b/resources/animated/hero/run/Chara_BlueWalk00008.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00008.png rename to resources/animated/hero/run/Chara_BlueWalk00008.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00009.png b/resources/animated/hero/run/Chara_BlueWalk00009.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00009.png rename to resources/animated/hero/run/Chara_BlueWalk00009.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00010.png b/resources/animated/hero/run/Chara_BlueWalk00010.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00010.png rename to resources/animated/hero/run/Chara_BlueWalk00010.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00011.png b/resources/animated/hero/run/Chara_BlueWalk00011.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00011.png rename to resources/animated/hero/run/Chara_BlueWalk00011.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00012.png b/resources/animated/hero/run/Chara_BlueWalk00012.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00012.png rename to resources/animated/hero/run/Chara_BlueWalk00012.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00013.png b/resources/animated/hero/run/Chara_BlueWalk00013.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00013.png rename to resources/animated/hero/run/Chara_BlueWalk00013.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00014.png b/resources/animated/hero/run/Chara_BlueWalk00014.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00014.png rename to resources/animated/hero/run/Chara_BlueWalk00014.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00015.png b/resources/animated/hero/run/Chara_BlueWalk00015.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00015.png rename to resources/animated/hero/run/Chara_BlueWalk00015.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00016.png b/resources/animated/hero/run/Chara_BlueWalk00016.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00016.png rename to resources/animated/hero/run/Chara_BlueWalk00016.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00017.png b/resources/animated/hero/run/Chara_BlueWalk00017.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00017.png rename to resources/animated/hero/run/Chara_BlueWalk00017.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00018.png b/resources/animated/hero/run/Chara_BlueWalk00018.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00018.png rename to resources/animated/hero/run/Chara_BlueWalk00018.png diff --git a/game/resources/animated/hero/run/Chara_BlueWalk00019.png b/resources/animated/hero/run/Chara_BlueWalk00019.png similarity index 100% rename from game/resources/animated/hero/run/Chara_BlueWalk00019.png rename to resources/animated/hero/run/Chara_BlueWalk00019.png diff --git a/game/resources/animated/runner_new.png b/resources/animated/runner_new.png similarity index 100% rename from game/resources/animated/runner_new.png rename to resources/animated/runner_new.png diff --git a/game/resources/levels/level_01/config.json b/resources/levels/level_01/config.json similarity index 93% rename from game/resources/levels/level_01/config.json rename to resources/levels/level_01/config.json index 250584b..c9c4e6d 100644 --- a/game/resources/levels/level_01/config.json +++ b/resources/levels/level_01/config.json @@ -1,6 +1,6 @@ { "name" : "level_01", - "animated_resources" : "../../game/resources/animated/", + "animated_resources" : "../../resources/animated/", "block" : 50.0, "camera": { "position" : { diff --git a/game/resources/levels/level_01/map b/resources/levels/level_01/map similarity index 100% rename from game/resources/levels/level_01/map rename to resources/levels/level_01/map diff --git a/game/resources/levels/level_with_finish/config.json b/resources/levels/level_with_finish/config.json similarity index 93% rename from game/resources/levels/level_with_finish/config.json rename to resources/levels/level_with_finish/config.json index 4276712..4d9f9fb 100644 --- a/game/resources/levels/level_with_finish/config.json +++ b/resources/levels/level_with_finish/config.json @@ -1,6 +1,6 @@ { "name" : "level_with_finish", - "animated_resources" : "../../game/resources/animated/", + "animated_resources" : "../../resources/animated/", "block" : 50.0, "camera": { "position" : { diff --git a/game/resources/levels/level_with_finish/map b/resources/levels/level_with_finish/map similarity index 100% rename from game/resources/levels/level_with_finish/map rename to resources/levels/level_with_finish/map diff --git a/game/resources/static/18plus.png b/resources/static/18plus.png similarity index 100% rename from game/resources/static/18plus.png rename to resources/static/18plus.png diff --git a/game/resources/static/brick.png b/resources/static/brick.png similarity index 100% rename from game/resources/static/brick.png rename to resources/static/brick.png diff --git a/game/resources/static/exit.png b/resources/static/exit.png similarity index 100% rename from game/resources/static/exit.png rename to resources/static/exit.png From 1f9e1fb90fbdd09c298adeee3e6ea899216705b3 Mon Sep 17 00:00:00 2001 From: denis Date: Sun, 29 May 2022 19:06:49 +0300 Subject: [PATCH 25/44] Test for httplib --- test/CMakeLists.txt | 1 + test/deps/CMakeLists.txt | 2 + test/deps/network/client.cpp | 22 +++++ test/deps/network/server.cpp | 155 +++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 test/deps/network/client.cpp create mode 100644 test/deps/network/server.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 07bb365..bc2aa5e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,6 +18,7 @@ function(test_executable EXECUTABLE_TEST_NAME EXECUTABLE_SOURCES) ${INCLUDE_IMGUI} ${INCLUDE_IMGUI_SFML} ${INCLUDE_PQXX} + ${INCLUDE_HTTP} ) target_link_libraries( diff --git a/test/deps/CMakeLists.txt b/test/deps/CMakeLists.txt index 3078da8..817960a 100644 --- a/test/deps/CMakeLists.txt +++ b/test/deps/CMakeLists.txt @@ -1,3 +1,5 @@ test_executable(test-box2d TestBox2d.cpp) test_executable(test-imgui TestImGui.cpp) test_executable(test-pqxx TestPQXX.cpp) +test_executable(test-server network/server.cpp) +test_executable(test-client network/client.cpp) diff --git a/test/deps/network/client.cpp b/test/deps/network/client.cpp new file mode 100644 index 0000000..198b44a --- /dev/null +++ b/test/deps/network/client.cpp @@ -0,0 +1,22 @@ +#include +#include + +int main() { + httplib::Client cli("localhost", 8080); + + auto res1 = cli.Post("/user/login", httplib::Params{ + {"username", "Denis"} + }); + std::cout << res1->status << ' ' << res1->body << '\n'; + + + auto res2 = cli.Post("/user/login", httplib::Params{ + {"username", "NoDenis"} + }); + std::cout << res2->status << ' ' << res2->body << '\n'; + + auto res3 = cli.Post("/user/login", httplib::Params{ + {"name", "Denis"} + }); + std::cout << res3->status << ' ' << res3->body << '\n'; +} diff --git a/test/deps/network/server.cpp b/test/deps/network/server.cpp new file mode 100644 index 0000000..e4038c3 --- /dev/null +++ b/test/deps/network/server.cpp @@ -0,0 +1,155 @@ +#include +#include +#include + +namespace mad::core { + class Database { + public: + Database(); + + bool is_user_exists(const std::string &username); + + void registry_user(const std::string &username); + + std::size_t get_id(const std::string &username); + + std::size_t get_progress(std::size_t id); + + std::size_t get_progress(const std::string &username); + + void increment_progress(std::size_t id); + + void increment_progress(const std::string &username); + + void reset_progress(std::size_t id); + + void reset_progress(const std::string &username); + + private: + pqxx::connection m_connector; + std::string m_query; + + }; +} + +namespace mad::core { + + Database::Database() : m_connector("dbname = test-network") { + try { + if (m_connector.is_open()) { + SPDLOG_DEBUG("Database mad opened successfully"); + } else { + SPDLOG_DEBUG("Can't open database mad"); + } + + pqxx::work w(m_connector); + + m_query = "CREATE TABLE IF NOT EXISTS users(" + "id SMALLINT PRIMARY KEY," + "name TEXT NOT NULL UNIQUE);"; + w.exec(m_query); + + m_query = "CREATE TABLE IF NOT EXISTS progress(" + "id SMALLINT PRIMARY KEY REFERENCES users (id)," + "levels_completed SMALLINT NOT NULL);"; + w.exec(m_query); + + w.commit(); + + SPDLOG_DEBUG("Tables created successfully"); + } catch (std::exception &exc) { + SPDLOG_INFO(exc.what()); + } + } + + bool Database::is_user_exists(const std::string &username) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM users WHERE name = '" + W.esc(username) + "';"; + pqxx::result rows_found = W.exec(m_query); + return !rows_found.empty(); + } + + void Database::registry_user(const std::string &username) { + pqxx::work W(m_connector); + + m_query = "SELECT id FROM users"; + pqxx::result total_rows = W.exec(m_query); + std::size_t id = total_rows.size(); + + m_query = "INSERT INTO users(id, name) VALUES(" + std::to_string(id) + ", '" + W.esc(username) + "');"; + W.exec(m_query); + + m_query = "INSERT INTO progress(id, levels_completed) VALUES(" + std::to_string(id) + ", 0);"; + W.exec(m_query); + + W.commit(); + } + + std::size_t Database::get_id(const std::string &username) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM users WHERE name = '" + W.esc(username) + "';"; + auto found_row = W.exec1(m_query); + return found_row["id"].as(); + } + + std::size_t Database::get_progress(std::size_t id) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM progress WHERE id = " + std::to_string(id) + ";"; + auto found_row = W.exec1(m_query); + return found_row["levels_completed"].as(); + } + + std::size_t Database::get_progress(const std::string &username) { + return get_progress(get_id(username)); + } + + void Database::increment_progress(std::size_t id) { + pqxx::work W(m_connector); + m_query = "UPDATE progress " + "SET levels_completed = levels_completed + 1 " + "WHERE id = " + std::to_string(id) + ";"; + W.exec(m_query); + W.commit(); + } + + void Database::increment_progress(const std::string &username) { + increment_progress(get_id(username)); + } + + void Database::reset_progress(std::size_t id) { + pqxx::work W(m_connector); + m_query = "UPDATE progress " + "SET levels_completed = 0 " + "WHERE id = " + std::to_string(id) + ";"; + W.exec(m_query); + W.commit(); + } + + void Database::reset_progress(const std::string &username) { + reset_progress(get_id(username)); + } +} + +int main() { + httplib::Server svr; + mad::core::Database db; + + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = "OK"; + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + + svr.listen("localhost", 8080); +} \ No newline at end of file From cf8a92f5540dd6f211239861fb31ba13538bd22f Mon Sep 17 00:00:00 2001 From: denis Date: Sun, 29 May 2022 20:01:02 +0300 Subject: [PATCH 26/44] Add simple client and server realisation --- CMakeLists.txt | 4 +- network/CMakeLists.txt | 4 +- network/client/client.cpp | 6 -- network/client/simple-client.cpp | 108 +++++++++++++++++++++++++++++++ network/server/server.cpp | 6 -- network/server/simple-server.cpp | 77 ++++++++++++++++++++++ 6 files changed, 189 insertions(+), 16 deletions(-) delete mode 100644 network/client/client.cpp create mode 100644 network/client/simple-client.cpp delete mode 100644 network/server/server.cpp create mode 100644 network/server/simple-server.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c50a15..01687c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,8 +114,8 @@ message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") # Network binaries set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) -set(EXECUTABLE_SERVER server) -set(EXECUTABLE_CLIENT client) +set(EXECUTABLE_SIMPLE_SERVER simple-server) +set(EXECUTABLE_SIMPLE_CLIENT simple-client) add_subdirectory(${DIRECTORY_NETWORK}) message(STATUS "Server executable: ${EXECUTABLE_SERVER}") diff --git a/network/CMakeLists.txt b/network/CMakeLists.txt index 35f74e8..69e16eb 100644 --- a/network/CMakeLists.txt +++ b/network/CMakeLists.txt @@ -37,5 +37,5 @@ function(network_executable EXECUTABLE_NETWORK_NAME EXECUTABLE_SOURCES) endfunction() -network_executable(${EXECUTABLE_SERVER} server/server.cpp) -network_executable(${EXECUTABLE_CLIENT} client/client.cpp) +network_executable(${EXECUTABLE_SIMPLE_SERVER} server/simple-server.cpp) +network_executable(${EXECUTABLE_SIMPLE_CLIENT} client/simple-client.cpp) diff --git a/network/client/client.cpp b/network/client/client.cpp deleted file mode 100644 index b4b83b3..0000000 --- a/network/client/client.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int main() { - std::cout << "Hello from client!" << '\n'; -} diff --git a/network/client/simple-client.cpp b/network/client/simple-client.cpp new file mode 100644 index 0000000..26049c9 --- /dev/null +++ b/network/client/simple-client.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace mad::core { + + class NetworkClientStorageDriver : public ClientStorageDriver { + public: + explicit NetworkClientStorageDriver() : m_client("localhost", 8080) { } + + bool log_in(const std::string &username) const override { + auto res = m_client.Post("/user/login", httplib::Params{ + {"username", username} + }); + SPDLOG_DEBUG("Login request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + if (res->status == 200) { + m_username = username; + } + return res->status == 200; + } + + bool sign_up(const std::string &username) override { + auto res = m_client.Post("/user/signup", httplib::Params{ + {"username", username} + }); + SPDLOG_DEBUG("Sign up request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + return res->status == 200; + } + + std::size_t get_progress() const override { + auto res = m_client.Post("/user/progress", httplib::Params{ + {"username", m_username} + }); + SPDLOG_DEBUG("Get progress request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + return std::stoi(res->body); + } + + void update_progress() override { + auto res = m_client.Post("/user/increment-progress", httplib::Params{ + {"username", m_username} + }); + SPDLOG_DEBUG("Update request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + } + + private: + mutable std::string m_username; + mutable httplib::Client m_client; + + }; + +} + +int main() { +#ifndef NDEBUG + auto log_level = spdlog::level::trace; +#else + auto log_level = spdlog::level::info; +#endif + spdlog::set_level(log_level); + + auto window = std::make_shared(sf::VideoMode(640, 480), "MAD"); + ImGui::SFML::Init(*window); + window->setFramerateLimit(120); + + auto global_dispatcher = std::make_shared(); + + auto system_listener = std::make_shared(window); + + std::vector> level_loaders{ + std::make_shared("../../resources/levels/level_with_finish"), + std::make_shared("../../resources/levels/level_01") + }; + + auto network_storage_driver = std::make_shared(); + + auto game_runner = std::make_unique( + level_loaders, + global_dispatcher, + std::make_unique(), + std::make_unique(network_storage_driver), + system_listener, + network_storage_driver + ); + + global_dispatcher->registry(std::make_shared(*window)); + global_dispatcher->registry(std::make_shared(*game_runner)); + global_dispatcher->registry(std::make_shared(*game_runner, network_storage_driver)); + global_dispatcher->registry(std::make_shared(*game_runner)); + + game_runner->run(*window); + + ImGui::SFML::Shutdown(); +} diff --git a/network/server/server.cpp b/network/server/server.cpp deleted file mode 100644 index d60b858..0000000 --- a/network/server/server.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int main() { - std::cout << "Hello from server!" << '\n'; -} diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp new file mode 100644 index 0000000..5c85588 --- /dev/null +++ b/network/server/simple-server.cpp @@ -0,0 +1,77 @@ +#include + +#include +#include + +int main() { + httplib::Server svr; + mad::core::Database db; + + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = "OK"; + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.Post("/user/signup", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 404; + res.body = "User already exists"; + } else { + db.registry_user(username); + res.status = 200; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.Post("/user/progress", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = std::to_string(db.get_progress(username)); + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.Post("/user/increment-progress", [&db](const httplib::Request &req, httplib::Response &res) { + if (req.has_param("username")) { + auto username = req.get_param_value("username"); + if (db.is_user_exists(username)) { + res.status = 200; + res.body = "OK"; + db.increment_progress(username); + } else { + res.status = 404; + res.body = "User doesn\'t exists"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } + }); + + svr.listen("localhost", 8080); +} From cb7cdac64555bef6c69266c3f3bf59e0e956ebf7 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 11:56:34 +0300 Subject: [PATCH 27/44] Chain server and window --- network/client/simple-client.cpp | 5 +++- network/server/simple-server.cpp | 45 +++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/network/client/simple-client.cpp b/network/client/simple-client.cpp index 26049c9..d35f011 100644 --- a/network/client/simple-client.cpp +++ b/network/client/simple-client.cpp @@ -21,7 +21,10 @@ namespace mad::core { class NetworkClientStorageDriver : public ClientStorageDriver { public: - explicit NetworkClientStorageDriver() : m_client("localhost", 8080) { } + explicit NetworkClientStorageDriver() : m_client("localhost", 8080) { + auto res = m_client.Get("/connection"); + SPDLOG_DEBUG("Connection request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); + } bool log_in(const std::string &username) const override { auto res = m_client.Post("/user/login", httplib::Params{ diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 5c85588..d51b1a6 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -2,11 +2,25 @@ #include #include +#include + +#include +#include + +#include +#include +#include + int main() { httplib::Server svr; mad::core::Database db; + svr.Get("/connection", [](const httplib::Request &req, httplib::Response &res) { + res.status = 200; + res.body = "Connected user port -- " + std::to_string(req.remote_port); + }); + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); @@ -73,5 +87,34 @@ int main() { } }); - svr.listen("localhost", 8080); + std::thread([&svr]() mutable { + svr.listen("localhost", 8080); + }).detach(); + + sf::RenderWindow window(sf::VideoMode(640, 480), "MAD Server"); + ImGui::SFML::Init(window); + window.setFramerateLimit(120); + sf::Clock clock; + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + ImGui::SFML::ProcessEvent(event); + + if (event.type == sf::Event::Closed) { + window.close(); + svr.stop(); + } + } + + ImGui::SFML::Update(window, clock.restart()); + + ImGui::Begin("Window"); + ImGui::Text("Hello"); + ImGui::End(); + + window.clear(); + ImGui::SFML::Render(window); + window.display(); + } + ImGui::SFML::Shutdown(); } From 7a9ec4af71d7e4820b76586d9d1844c789dc5778 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 12:42:20 +0300 Subject: [PATCH 28/44] Add server control buttons --- network/server/simple-server.cpp | 38 +++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index d51b1a6..2f1ae43 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -46,7 +46,7 @@ int main() { } else { db.registry_user(username); res.status = 200; - res.body = "User doesn\'t exists"; + res.body = "User " + username + " is registered"; } } else { res.status = 404; @@ -87,10 +87,6 @@ int main() { } }); - std::thread([&svr]() mutable { - svr.listen("localhost", 8080); - }).detach(); - sf::RenderWindow window(sf::VideoMode(640, 480), "MAD Server"); ImGui::SFML::Init(window); window.setFramerateLimit(120); @@ -102,14 +98,40 @@ int main() { if (event.type == sf::Event::Closed) { window.close(); - svr.stop(); + if (svr.is_running()) { + svr.stop(); + } } } ImGui::SFML::Update(window, clock.restart()); - ImGui::Begin("Window"); - ImGui::Text("Hello"); + ImGui::SetNextWindowSize(ImVec2(window.getSize().x, window.getSize().y)); + ImGui::SetNextWindowPos({0, 0}); + ImGui::Begin("Server util", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); + + + if (ImGui::Button("Start server")) { + if (!svr.is_running()) { + std::thread([&svr]() mutable { + svr.listen("localhost", 8080); + }).detach(); + } + } + + if (ImGui::Button("Stop server")) { + if (svr.is_running()) { + svr.stop(); + } + } + + if (ImGui::Button("Quit")) { + window.close(); + if (svr.is_running()) { + svr.stop(); + } + } + ImGui::End(); window.clear(); From 3e4ee47c4a62056c097e7008a23fa39e2599db84 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 15:59:14 +0300 Subject: [PATCH 29/44] Add server logs --- network/server/simple-server.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 2f1ae43..c8934f1 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -16,17 +17,22 @@ int main() { httplib::Server svr; mad::core::Database db; - svr.Get("/connection", [](const httplib::Request &req, httplib::Response &res) { + std::vector logs; + const char *log; + + svr.Get("/connection", [&logs](const httplib::Request &req, httplib::Response &res) { res.status = 200; res.body = "Connected user port -- " + std::to_string(req.remote_port); + logs.push_back(res.body); }); - svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/login", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; + logs.push_back("User " + std::to_string(req.remote_port) + " login as " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -37,12 +43,13 @@ int main() { } }); - svr.Post("/user/signup", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/signup", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 404; res.body = "User already exists"; + logs.push_back("Register new user " + username + " from port " + std::to_string(req.remote_port)); } else { db.registry_user(username); res.status = 200; @@ -54,12 +61,13 @@ int main() { } }); - svr.Post("/user/progress", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = std::to_string(db.get_progress(username)); + logs.push_back("Send progress to user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -70,13 +78,14 @@ int main() { } }); - svr.Post("/user/increment-progress", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/increment-progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; db.increment_progress(username); + logs.push_back("Increment progress for user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -116,12 +125,14 @@ int main() { std::thread([&svr]() mutable { svr.listen("localhost", 8080); }).detach(); + logs.emplace_back("Server has started"); } } if (ImGui::Button("Stop server")) { if (svr.is_running()) { svr.stop(); + logs.emplace_back("Server has stopped"); } } @@ -132,6 +143,15 @@ int main() { } } + { + ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; + ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); + for (int i = 0; i < logs.size(); ++i) { + ImGui::Text(logs[i].c_str(), i); + } + ImGui::EndChild(); + } + ImGui::End(); window.clear(); From 4c81fe7772506e9b5504b3e16315560ac463736b Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 16:32:47 +0300 Subject: [PATCH 30/44] Add mutex for logs and db --- network/server/simple-server.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index c8934f1..1fef0fe 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -1,9 +1,9 @@ #include -#include #include #include #include +#include #include #include @@ -17,16 +17,18 @@ int main() { httplib::Server svr; mad::core::Database db; + std::mutex locker; std::vector logs; - const char *log; - svr.Get("/connection", [&logs](const httplib::Request &req, httplib::Response &res) { + svr.Get("/connection", [&logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); res.status = 200; - res.body = "Connected user port -- " + std::to_string(req.remote_port); - logs.push_back(res.body); + res.body = "Connection successful"; + logs.push_back("Connected user port -- " + std::to_string(req.remote_port)); }); - svr.Post("/user/login", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/login", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -43,7 +45,8 @@ int main() { } }); - svr.Post("/user/signup", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/signup", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -61,7 +64,8 @@ int main() { } }); - svr.Post("/user/progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -78,7 +82,8 @@ int main() { } }); - svr.Post("/user/increment-progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/increment-progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -144,6 +149,7 @@ int main() { } { + std::unique_lock lock(locker); ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); for (int i = 0; i < logs.size(); ++i) { From 9ab7131a9214d2770dae718d7d867f8fd04067ae Mon Sep 17 00:00:00 2001 From: denis Date: Mon, 30 May 2022 20:46:24 +0300 Subject: [PATCH 31/44] Provide rebase --- network/server/simple-server.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 1fef0fe..611561e 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -164,5 +164,6 @@ int main() { ImGui::SFML::Render(window); window.display(); } + ImGui::SFML::Shutdown(); } From 7a893839825c4858d8693c8b43c1c5245a36dd80 Mon Sep 17 00:00:00 2001 From: denis Date: Sat, 28 May 2022 16:05:17 +0300 Subject: [PATCH 32/44] Add httplib.h in submodule, move resources, add network directory and cmake --- CMakeLists.txt | 9 --------- network/client/client.cpp | 6 ++++++ network/server/server.cpp | 6 ++++++ 3 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 network/client/client.cpp create mode 100644 network/server/server.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 01687c8..be9e12e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,15 +112,6 @@ add_subdirectory(${DIRECTORY_GAME}) message(STATUS "Game executable: ${EXECUTABLE_GAME}") message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") -# Network binaries -set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) -set(EXECUTABLE_SIMPLE_SERVER simple-server) -set(EXECUTABLE_SIMPLE_CLIENT simple-client) - -add_subdirectory(${DIRECTORY_NETWORK}) -message(STATUS "Server executable: ${EXECUTABLE_SERVER}") -message(STATUS "Client executable ${EXECUTABLE_GAME_CLIENT}") - # Tests set(DIRECTORY_TEST ${PROJECT_SOURCE_DIR}/test) set(EXECUTABLE_TEST test) diff --git a/network/client/client.cpp b/network/client/client.cpp new file mode 100644 index 0000000..b4b83b3 --- /dev/null +++ b/network/client/client.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from client!" << '\n'; +} diff --git a/network/server/server.cpp b/network/server/server.cpp new file mode 100644 index 0000000..d60b858 --- /dev/null +++ b/network/server/server.cpp @@ -0,0 +1,6 @@ +#include +#include + +int main() { + std::cout << "Hello from server!" << '\n'; +} From 9aaded5f8570c2b0ea2e915b1d4f2a045fd1a757 Mon Sep 17 00:00:00 2001 From: denis Date: Sat, 28 May 2022 16:05:17 +0300 Subject: [PATCH 33/44] Add httplib.h in submodule, move resources, add network directory and cmake --- CMakeLists.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index be9e12e..1c50a15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,6 +112,15 @@ add_subdirectory(${DIRECTORY_GAME}) message(STATUS "Game executable: ${EXECUTABLE_GAME}") message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") +# Network binaries +set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) +set(EXECUTABLE_SERVER server) +set(EXECUTABLE_CLIENT client) + +add_subdirectory(${DIRECTORY_NETWORK}) +message(STATUS "Server executable: ${EXECUTABLE_SERVER}") +message(STATUS "Client executable ${EXECUTABLE_GAME_CLIENT}") + # Tests set(DIRECTORY_TEST ${PROJECT_SOURCE_DIR}/test) set(EXECUTABLE_TEST test) From 0a95808eadbb7b69f822ddabc1829375345802b1 Mon Sep 17 00:00:00 2001 From: denis Date: Sun, 29 May 2022 20:01:02 +0300 Subject: [PATCH 34/44] Add simple client and server realisation --- CMakeLists.txt | 4 ++-- network/client/client.cpp | 6 ------ network/server/server.cpp | 6 ------ 3 files changed, 2 insertions(+), 14 deletions(-) delete mode 100644 network/client/client.cpp delete mode 100644 network/server/server.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c50a15..01687c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,8 +114,8 @@ message(STATUS "Game-runner executable ${EXECUTABLE_GAME_RUNNER}") # Network binaries set(DIRECTORY_NETWORK ${PROJECT_SOURCE_DIR}/network) -set(EXECUTABLE_SERVER server) -set(EXECUTABLE_CLIENT client) +set(EXECUTABLE_SIMPLE_SERVER simple-server) +set(EXECUTABLE_SIMPLE_CLIENT simple-client) add_subdirectory(${DIRECTORY_NETWORK}) message(STATUS "Server executable: ${EXECUTABLE_SERVER}") diff --git a/network/client/client.cpp b/network/client/client.cpp deleted file mode 100644 index b4b83b3..0000000 --- a/network/client/client.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int main() { - std::cout << "Hello from client!" << '\n'; -} diff --git a/network/server/server.cpp b/network/server/server.cpp deleted file mode 100644 index d60b858..0000000 --- a/network/server/server.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -int main() { - std::cout << "Hello from server!" << '\n'; -} From 35b01e902be6bde96662b09d02cada15d9ce1868 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 12:42:20 +0300 Subject: [PATCH 35/44] Add server control buttons --- network/server/simple-server.cpp | 41 ++++++-------------------------- 1 file changed, 7 insertions(+), 34 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 611561e..2f1ae43 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -1,9 +1,8 @@ #include +#include #include #include -#include -#include #include #include @@ -17,24 +16,17 @@ int main() { httplib::Server svr; mad::core::Database db; - std::mutex locker; - std::vector logs; - - svr.Get("/connection", [&logs, &locker](const httplib::Request &req, httplib::Response &res) { - std::unique_lock lock(locker); + svr.Get("/connection", [](const httplib::Request &req, httplib::Response &res) { res.status = 200; - res.body = "Connection successful"; - logs.push_back("Connected user port -- " + std::to_string(req.remote_port)); + res.body = "Connected user port -- " + std::to_string(req.remote_port); }); - svr.Post("/user/login", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { - std::unique_lock lock(locker); + svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; - logs.push_back("User " + std::to_string(req.remote_port) + " login as " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -45,14 +37,12 @@ int main() { } }); - svr.Post("/user/signup", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { - std::unique_lock lock(locker); + svr.Post("/user/signup", [&db](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 404; res.body = "User already exists"; - logs.push_back("Register new user " + username + " from port " + std::to_string(req.remote_port)); } else { db.registry_user(username); res.status = 200; @@ -64,14 +54,12 @@ int main() { } }); - svr.Post("/user/progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { - std::unique_lock lock(locker); + svr.Post("/user/progress", [&db](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = std::to_string(db.get_progress(username)); - logs.push_back("Send progress to user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -82,15 +70,13 @@ int main() { } }); - svr.Post("/user/increment-progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { - std::unique_lock lock(locker); + svr.Post("/user/increment-progress", [&db](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; db.increment_progress(username); - logs.push_back("Increment progress for user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -130,14 +116,12 @@ int main() { std::thread([&svr]() mutable { svr.listen("localhost", 8080); }).detach(); - logs.emplace_back("Server has started"); } } if (ImGui::Button("Stop server")) { if (svr.is_running()) { svr.stop(); - logs.emplace_back("Server has stopped"); } } @@ -148,22 +132,11 @@ int main() { } } - { - std::unique_lock lock(locker); - ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; - ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); - for (int i = 0; i < logs.size(); ++i) { - ImGui::Text(logs[i].c_str(), i); - } - ImGui::EndChild(); - } - ImGui::End(); window.clear(); ImGui::SFML::Render(window); window.display(); } - ImGui::SFML::Shutdown(); } From 765788e6eaf292185ba76e9a7026da6f3977fc9f Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 15:59:14 +0300 Subject: [PATCH 36/44] Add server logs --- network/server/simple-server.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 2f1ae43..c8934f1 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -16,17 +17,22 @@ int main() { httplib::Server svr; mad::core::Database db; - svr.Get("/connection", [](const httplib::Request &req, httplib::Response &res) { + std::vector logs; + const char *log; + + svr.Get("/connection", [&logs](const httplib::Request &req, httplib::Response &res) { res.status = 200; res.body = "Connected user port -- " + std::to_string(req.remote_port); + logs.push_back(res.body); }); - svr.Post("/user/login", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/login", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; + logs.push_back("User " + std::to_string(req.remote_port) + " login as " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -37,12 +43,13 @@ int main() { } }); - svr.Post("/user/signup", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/signup", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 404; res.body = "User already exists"; + logs.push_back("Register new user " + username + " from port " + std::to_string(req.remote_port)); } else { db.registry_user(username); res.status = 200; @@ -54,12 +61,13 @@ int main() { } }); - svr.Post("/user/progress", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = std::to_string(db.get_progress(username)); + logs.push_back("Send progress to user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -70,13 +78,14 @@ int main() { } }); - svr.Post("/user/increment-progress", [&db](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/increment-progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { res.status = 200; res.body = "OK"; db.increment_progress(username); + logs.push_back("Increment progress for user " + username); } else { res.status = 404; res.body = "User doesn\'t exists"; @@ -116,12 +125,14 @@ int main() { std::thread([&svr]() mutable { svr.listen("localhost", 8080); }).detach(); + logs.emplace_back("Server has started"); } } if (ImGui::Button("Stop server")) { if (svr.is_running()) { svr.stop(); + logs.emplace_back("Server has stopped"); } } @@ -132,6 +143,15 @@ int main() { } } + { + ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; + ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); + for (int i = 0; i < logs.size(); ++i) { + ImGui::Text(logs[i].c_str(), i); + } + ImGui::EndChild(); + } + ImGui::End(); window.clear(); From 7021ea2169902bcc57a55fce1708762fb908b74c Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Mon, 30 May 2022 16:32:47 +0300 Subject: [PATCH 37/44] Add mutex for logs and db --- network/server/simple-server.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index c8934f1..1fef0fe 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -1,9 +1,9 @@ #include -#include #include #include #include +#include #include #include @@ -17,16 +17,18 @@ int main() { httplib::Server svr; mad::core::Database db; + std::mutex locker; std::vector logs; - const char *log; - svr.Get("/connection", [&logs](const httplib::Request &req, httplib::Response &res) { + svr.Get("/connection", [&logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); res.status = 200; - res.body = "Connected user port -- " + std::to_string(req.remote_port); - logs.push_back(res.body); + res.body = "Connection successful"; + logs.push_back("Connected user port -- " + std::to_string(req.remote_port)); }); - svr.Post("/user/login", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/login", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -43,7 +45,8 @@ int main() { } }); - svr.Post("/user/signup", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/signup", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -61,7 +64,8 @@ int main() { } }); - svr.Post("/user/progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -78,7 +82,8 @@ int main() { } }); - svr.Post("/user/increment-progress", [&db, &logs](const httplib::Request &req, httplib::Response &res) { + svr.Post("/user/increment-progress", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); if (req.has_param("username")) { auto username = req.get_param_value("username"); if (db.is_user_exists(username)) { @@ -144,6 +149,7 @@ int main() { } { + std::unique_lock lock(locker); ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); for (int i = 0; i < logs.size(); ++i) { From 69c2c455a6a16ffd4ffa68c194b6948b30134a9d Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Wed, 1 Jun 2022 13:34:17 +0300 Subject: [PATCH 38/44] Implement LevelLoaderFromServer --- CMakeLists.txt | 1 + core/CMakeLists.txt | 1 + core/loader/LevelLoaderFromServer.cpp | 250 ++++++++++++++++++ core/loader/LevelLoaderFromServer.hpp | 142 ++++++++++ game/CMakeLists.txt | 2 +- game/game_loader_from_server_example.cpp | 71 +++++ resources/levels/level_01/config.json | 1 + .../levels/level_with_finish/config.json | 1 + 8 files changed, 468 insertions(+), 1 deletion(-) create mode 100644 core/loader/LevelLoaderFromServer.cpp create mode 100644 core/loader/LevelLoaderFromServer.hpp create mode 100644 game/game_loader_from_server_example.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 01687c8..b76a67f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,7 @@ set(DIRECTORY_GAME ${PROJECT_SOURCE_DIR}/game) set(EXECUTABLE_GAME example) set(EXECUTABLE_GAME_RUNNER game_runner_example) set(EXECUTABLE_GAME_DATABASE game_database_example) +set(EXECUTABLE_LOADER_FROM_SERVER game_loader_from_server_example) add_subdirectory(${DIRECTORY_GAME}) message(STATUS "Game executable: ${EXECUTABLE_GAME}") diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 1d87b87..a3599f0 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -78,6 +78,7 @@ set( runner/GameRunner.hpp runner/GameRunner.cpp loader/LevelLoader.hpp loader/LevelLoaderFromFile.cpp loader/LevelLoaderFromFile.hpp + loader/LevelLoaderFromServer.hpp loader/LevelLoaderFromServer.cpp visual/image/Image.hpp visual/image/static/RenderableStatic.cpp visual/image/static/RenderableStatic.hpp diff --git a/core/loader/LevelLoaderFromServer.cpp b/core/loader/LevelLoaderFromServer.cpp new file mode 100644 index 0000000..0cfd1ee --- /dev/null +++ b/core/loader/LevelLoaderFromServer.cpp @@ -0,0 +1,250 @@ +#include "LevelLoaderFromServer.hpp" + +#include +#include "event/management/condition/KeyDownCondition.hpp" +#include "event/management/condition/KeyPressedCondition.hpp" +#include "event/management/condition/TimerCondition.hpp" +#include "event/management/condition/TrueCondition.hpp" +#include "event/management/controller/statemachine/StateMachine.hpp" + +#include + +namespace mad::core { + + LevelLoaderFromServer::LevelLoaderFromServer(std::string map, json config) : m_level_map(std::move(map)), m_config_json(std::move(config)) { } + + std::unique_ptr LevelLoaderFromServer::load(std::shared_ptr global_dispatcher, + std::shared_ptr system_listener) { + auto level_dispatcher = std::make_shared(); + + auto world = std::make_shared(*level_dispatcher); + + Vec2d camera_position = {m_config_json["camera"]["position"]["x"], + m_config_json["camera"]["position"]["y"]}; + auto camera = std::make_shared(camera_position, world); + + auto keys = create_world(world); + + camera->turn_on(*level_dispatcher, keys[LevelLoaderFromServer::IdKeys::Hero]); + level_dispatcher->registry(camera); + level_dispatcher->registry(std::make_shared(world, keys[LevelLoaderFromServer::IdKeys::Hero])); + + /* std::vector> controllers { + std::make_shared(camera) + };*/ + + ///State Machine + struct C1 : mad::core::Controller { + void control() override { + //SPDLOG_DEBUG("controller 1"); + }; + }; + struct C2 : mad::core::Controller { + void control() override { + //SPDLOG_DEBUG("controller 2"); + }; + }; + auto machine = std::make_shared( + std::shared_ptr(level_dispatcher)); + machine->add_state(std::make_shared()); + machine->add_state(std::make_shared()); + machine->add_transition(0, 1, std::make_shared(1)); + machine->add_transition(1, 0, std::make_shared(2)); + machine->set_initial_state(0); + std::vector> controllers{machine, + std::make_shared( + camera)}; + + auto level_runner = std::make_unique( + system_listener, + std::make_unique(), + camera, + global_dispatcher, + level_dispatcher, + world, + controllers + ); + + level_dispatcher->registry(std::make_shared(*level_runner, std::make_shared(keys[LevelLoaderFromServer::IdKeys::Hero], keys[LevelLoaderFromServer::IdKeys::FinishBlock]))); + level_dispatcher->registry(std::make_shared(*level_runner)); + + return level_runner; + } + + std::unordered_map LevelLoaderFromServer::create_world(std::shared_ptr world) { + float object_size = m_config_json["block"]; + float current_position_x = object_size / 2; + float current_position_y = object_size / 2; + std::size_t width = m_config_json["width"]; + std::unordered_map keys; + + create_background(world); + + for (std::size_t i = 0; i < m_level_map.size();) { + for (std::size_t j = 0; j < width; ++j, ++i) { + char object = m_level_map[i]; + switch (m_objects[object]) { + case Objects::UnstableBlock: { + create_block(world, + {current_position_x, + current_position_y}, + object_size, false); + break; + } + case Objects::StableBlock: { + create_block(world, + {current_position_x, + current_position_y}, + object_size, true); + break; + } + case Objects::FinishBlock: { + keys[LevelLoaderFromServer::IdKeys::FinishBlock] = create_finish_block( + world, + {current_position_x, current_position_y}, + object_size); + break; + } + case Objects::Hero: { + keys[LevelLoaderFromServer::IdKeys::Hero] = create_hero(world, + {current_position_x, + current_position_y}); + break; + } + case Objects::Enemy1: { + break; + } + case Objects::Enemy2: { + break; + } + case Objects::Empty: { + break; + } + } + current_position_x += object_size; + } + current_position_y += object_size; + current_position_x = object_size / 2; + } + return keys; + } + + void LevelLoaderFromServer::create_block(std::shared_ptr world, + Vec2d position, float block_size, bool is_stable) { + + std::filesystem::path source("../../resources/static/"); + if (is_stable) { + source /= static_cast(m_config_json["texture"]["stable"]); + } else { + source /= static_cast(m_config_json["texture"]["unstable"]); + } + + auto image_storage = std::make_shared( + std::unordered_map>( + {{ImageStorage::TypeAction::Idle, + std::make_shared(source, block_size, + block_size, + StaticImage::TransformType::Tile) + }})); + + Entity::Id square_id = world->create_physical_entity( + 0, + position, + 0, + image_storage, + is_stable + ); + } + + Entity::Id LevelLoaderFromServer::create_hero(std::shared_ptr world, Vec2d position) { + std::filesystem::path source(m_config_json["animated_resources"]); + source /= m_config_json["hero"]["source"]; + + Entity::Id hero_id = 0; + + std::shared_ptr image_storage; + + float physical_size_width = m_config_json["hero"]["animated"]["size_width"]; + float physical_size_height = m_config_json["hero"]["animated"]["size_height"]; + float size_scale = m_config_json["hero"]["animated"]["size_scale"]; + float delta_x = m_config_json["hero"]["animated"]["delta_x"]; + float delta_y = m_config_json["hero"]["animated"]["delta_y"]; + + + image_storage = std::make_shared( + std::unordered_map>( + {{ImageStorage::TypeAction::Idle, + std::make_shared( + source / m_config_json["hero"]["animated"]["actions"]["idle"]["source"], + m_config_json["hero"]["animated"]["actions"]["idle"]["delta_time"], + physical_size_width, physical_size_height, size_scale, + delta_x, delta_y) + }, + {ImageStorage::TypeAction::Run, + std::make_shared( + source / m_config_json["hero"]["animated"]["actions"]["run"]["source"], + m_config_json["hero"]["animated"]["actions"]["run"]["delta_time"], + physical_size_width, physical_size_height, size_scale, + delta_x, delta_y) + }} + ) + ); + + hero_id = world->create_physical_entity( + 0, + position, + 0, + image_storage, + false, false + ); + + return hero_id; + } + + Entity::Id LevelLoaderFromServer::create_finish_block(std::shared_ptr world, Vec2d position, float block_size) { + std::filesystem::path source("../../resources/static/"); + source /= static_cast(m_config_json["texture"]["finish"]); + + auto image_storage = std::make_shared( + std::unordered_map>( + {{ImageStorage::TypeAction::Idle, + std::make_shared(source, block_size, + block_size, + StaticImage::TransformType::Fit) + }})); + + return world->create_physical_entity( + 0, + position, + 0, + image_storage, + true + ); + } + + void LevelLoaderFromServer::create_background(std::shared_ptr world) { + std::filesystem::path source(m_config_json["background"]["source"]); + + std::shared_ptr image_storage; + std::vector parallax_ratios = m_config_json["background"]["parallax_ratios"]; + + image_storage = std::make_shared( + std::unordered_map>( + {{ImageStorage::TypeAction::Idle, + std::make_shared( + source, + parallax_ratios, + m_config_json["background"]["scale"] + ) + }} + ) + ); + world->create_viewable_entity( + -1, + {0, 0}, + 0, + image_storage + ); + } + +} \ No newline at end of file diff --git a/core/loader/LevelLoaderFromServer.hpp b/core/loader/LevelLoaderFromServer.hpp new file mode 100644 index 0000000..04e310a --- /dev/null +++ b/core/loader/LevelLoaderFromServer.hpp @@ -0,0 +1,142 @@ +#ifndef MAD_LEVELLOADERFROMSERVER_HPP +#define MAD_LEVELLOADERFROMSERVER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include + +using json = nlohmann::json; + + +namespace mad::core { + + class ArrowController : public mad::core::EventHandler { + public: + explicit ArrowController(std::shared_ptr world, + mad::core::Entity::Id entity_id) + : m_world(std::move(world)), + m_entity_id(entity_id) {} + + void handle(const mad::core::Event &event) override { + //SPDLOG_INFO("handle arrow event"); + + auto make_move_intent = [](mad::core::Vec2d dir) { + return mad::core::LambdaIntent( + [=](mad::core::Entity &entity, mad::core::EventDispatcher &event_dispatcher) { + mad::core::cast_to(entity).move(dir); + }); + }; + + auto impulse = [](mad::core::Vec2d dir) { + return mad::core::LambdaIntent( + [=](mad::core::Entity &entity, mad::core::EventDispatcher &event_dispatcher) { + mad::core::cast_to(entity).apply_linear_impulse_to_center(dir, event_dispatcher); + }); + }; + + auto force = [](mad::core::Vec2d dir) { + return mad::core::LambdaIntent( + [=](mad::core::Entity &entity, mad::core::EventDispatcher &event_dispatcher) { + mad::core::cast_to(entity).apply_force_to_center(dir, event_dispatcher); + }); + }; + + if (event.type == mad::core::Event::Type::KeyPressed) { + const auto &keystroke = mad::core::const_cast_to(event); + if (keystroke.key_id == sf::Keyboard::Key::Space) { + m_world->manipulate_entity_id(m_entity_id, impulse(mad::core::Vec2d{0.0f, -200000.0f})); + } + } else if (event.type == mad::core::Event::Type::KeyHeld) { + const auto &keystroke = mad::core::const_cast_to(event); + if (keystroke.key_id == sf::Keyboard::Key::Right) { + m_world->manipulate_entity_id(m_entity_id, force(mad::core::Vec2d{100000.0f, 0.0f})); + } + if (keystroke.key_id == sf::Keyboard::Key::Left) { + m_world->manipulate_entity_id(m_entity_id, force(mad::core::Vec2d{-100000.0f, 0.0f})); + } + } + } + + std::unordered_set handled_types() override { + return {mad::core::Event::Type::KeyPressed, mad::core::Event::Type::KeyHeld}; + } + + private: + std::shared_ptr m_world; + mad::core::Entity::Id m_entity_id; + }; + + class LevelLoaderFromServer : public LevelLoader { + private: + enum class IdKeys { + Hero, + FinishBlock, + }; + + public: + explicit LevelLoaderFromServer(std::string map, json config); + + std::unique_ptr load( + std::shared_ptr global_dispatcher, + std::shared_ptr system_listener) override; + + std::unordered_map create_world(std::shared_ptr world); + + void create_block(std::shared_ptr world, Vec2d position, + float block_size, bool is_stable); + + Entity::Id create_hero(std::shared_ptr world, Vec2d position); + + void create_background(std::shared_ptr world); + + Entity::Id create_finish_block(std::shared_ptr world, Vec2d position, float block_size); + + private: + enum class Objects { + UnstableBlock, + StableBlock, + FinishBlock, + Hero, + Enemy1, + Enemy2, + Empty + }; + + json m_config_json; + + std::string m_level_map; + + std::unordered_map m_objects = { + {'.', Objects::Empty}, + {'#', Objects::StableBlock}, + {'@', Objects::UnstableBlock}, + {'F', Objects::FinishBlock}, + {'H', Objects::Hero}, + {'Z', Objects::Enemy1}, + {'E', Objects::Enemy2} + }; + + }; + +} + +#endif//MAD_LEVELLOADERFROMSERVER_HPP diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 1bbf356..80ef2dc 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -37,6 +37,6 @@ function(game_executable EXECUTABLE_GAME_NAME EXECUTABLE_SOURCES) endfunction() game_executable(${EXECUTABLE_GAME} example.cpp) -#game_executable(${EXECUTABLE_GAME_RUNNER} game_runner_example.cpp) +game_executable(${EXECUTABLE_LOADER_FROM_SERVER} game_loader_from_server_example.cpp) game_executable(${EXECUTABLE_GAME_RUNNER} game_with_default_loader_example.cpp) game_executable(${EXECUTABLE_GAME_DATABASE} game_database_example.cpp) diff --git a/game/game_loader_from_server_example.cpp b/game/game_loader_from_server_example.cpp new file mode 100644 index 0000000..0d42e9d --- /dev/null +++ b/game/game_loader_from_server_example.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +int main() { +#ifndef NDEBUG + auto log_level = spdlog::level::trace; +#else + auto log_level = spdlog::level::info; +#endif + spdlog::set_level(log_level); + + auto window = std::make_shared(sf::VideoMode(640, 480), "MAD"); + ImGui::SFML::Init(*window); + window->setFramerateLimit(120); + + auto global_dispatcher = std::make_shared(); + + auto system_listener = std::make_shared(window); + + auto database = std::make_shared(); + auto database_storage_driver = std::make_shared(database); + + std::string level_map; + json level_config; + + std::ifstream input_config("../../resources/levels/level_with_finish/config.json"); + input_config >> level_config; + std::ifstream map_file("../../resources/levels/level_with_finish/map"); + std::string map_line; + while (std::getline(map_file, map_line)) { + level_map += map_line; + } + + std::vector> level_loaders{ + std::make_shared(level_map, level_config) + }; + + auto game_runner = std::make_unique( + level_loaders, + global_dispatcher, + std::make_unique(), + std::make_unique(database_storage_driver), + system_listener, + database_storage_driver + ); + + global_dispatcher->registry(std::make_shared(*window)); + global_dispatcher->registry(std::make_shared(*game_runner)); + global_dispatcher->registry(std::make_shared(*game_runner, database_storage_driver)); + global_dispatcher->registry(std::make_shared(*game_runner)); + + game_runner->run(*window); + + ImGui::SFML::Shutdown(); +} diff --git a/resources/levels/level_01/config.json b/resources/levels/level_01/config.json index c9c4e6d..d3d1309 100644 --- a/resources/levels/level_01/config.json +++ b/resources/levels/level_01/config.json @@ -1,6 +1,7 @@ { "name" : "level_01", "animated_resources" : "../../resources/animated/", + "width" : 14, "block" : 50.0, "camera": { "position" : { diff --git a/resources/levels/level_with_finish/config.json b/resources/levels/level_with_finish/config.json index 4d9f9fb..de9320e 100644 --- a/resources/levels/level_with_finish/config.json +++ b/resources/levels/level_with_finish/config.json @@ -1,6 +1,7 @@ { "name" : "level_with_finish", "animated_resources" : "../../resources/animated/", + "width" : 18, "block" : 50.0, "camera": { "position" : { From 6e90660b9f01424c7c8a1d068be9183c6aedd92b Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Thu, 2 Jun 2022 17:27:24 +0300 Subject: [PATCH 39/44] Level-table in database --- core/database/database/Database.cpp | 21 +++++++++++++++++++++ core/database/database/Database.hpp | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/core/database/database/Database.cpp b/core/database/database/Database.cpp index 98eb45c..403f25d 100644 --- a/core/database/database/Database.cpp +++ b/core/database/database/Database.cpp @@ -25,6 +25,11 @@ namespace mad::core { "levels_completed SMALLINT NOT NULL);"; w.exec(m_query); + m_query = "CREATE TABLE IF NOT EXISTS levels(" + "id SERIAL PRIMARY KEY," + "name TEXT NOT NULL UNIQUE);"; + w.exec(m_query); + w.commit(); SPDLOG_DEBUG("Tables created successfully"); @@ -56,6 +61,15 @@ namespace mad::core { W.commit(); } + void Database::append_level(const std::string &levelname) { + pqxx::work W(m_connector); + + m_query = "INSERT INTO levels(name) VALUES('" + levelname + "');"; + W.exec(m_query); + + W.commit(); + } + std::size_t Database::get_id(const std::string &username) { pqxx::work W(m_connector); m_query = "SELECT * FROM users WHERE name = '" + W.esc(username) + "';"; @@ -99,4 +113,11 @@ namespace mad::core { void Database::reset_progress(const std::string &username) { reset_progress(get_id(username)); } + + std::string Database::get_levelname(std::size_t id) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM levels WHERE id = " + std::to_string(id) + ";"; + auto found_row = W.exec1(m_query); + return found_row["name"].as(); + } } diff --git a/core/database/database/Database.hpp b/core/database/database/Database.hpp index 5db0168..dcc2833 100644 --- a/core/database/database/Database.hpp +++ b/core/database/database/Database.hpp @@ -15,11 +15,15 @@ namespace mad::core { void registry_user(const std::string &username); + void append_level(const std::string &levelname); + std::size_t get_id(const std::string &username); std::size_t get_progress(std::size_t id); std::size_t get_progress(const std::string &username); + std::string get_levelname(std::size_t id); + void increment_progress(std::size_t id); void increment_progress(const std::string &username); From 6f710e2a1f0b835537c96ee620b4dbd625e4b36e Mon Sep 17 00:00:00 2001 From: denis Date: Fri, 3 Jun 2022 00:19:01 +0300 Subject: [PATCH 40/44] Load levels from server --- core/database/database/Database.cpp | 9 +++- core/database/database/Database.hpp | 3 ++ core/loader/LevelLoaderFromFile.cpp | 7 +-- network/client/simple-client.cpp | 26 +++++++--- network/server/simple-server.cpp | 45 +++++++++++++++++- .../background/background_01.png | Bin .../background/background_02.png | Bin .../background/background_03.png | Bin .../background/background_04.png | Bin .../background/background_05.png | Bin resources/levels/level_01/config.json | 2 +- .../levels/level_with_finish/config.json | 2 +- 12 files changed, 79 insertions(+), 15 deletions(-) rename {game/resources => resources}/background/background_01.png (100%) rename {game/resources => resources}/background/background_02.png (100%) rename {game/resources => resources}/background/background_03.png (100%) rename {game/resources => resources}/background/background_04.png (100%) rename {game/resources => resources}/background/background_05.png (100%) diff --git a/core/database/database/Database.cpp b/core/database/database/Database.cpp index 403f25d..5bac71f 100644 --- a/core/database/database/Database.cpp +++ b/core/database/database/Database.cpp @@ -48,7 +48,7 @@ namespace mad::core { void Database::registry_user(const std::string &username) { pqxx::work W(m_connector); - m_query = "SELECT id FROM users"; + m_query = "SELECT id FROM users;"; pqxx::result total_rows = W.exec(m_query); std::size_t id = total_rows.size(); @@ -120,4 +120,11 @@ namespace mad::core { auto found_row = W.exec1(m_query); return found_row["name"].as(); } + + std::size_t Database::get_levels_total() { + pqxx::work W(m_connector); + m_query = "SELECT id FROM levels;"; + auto total_rows = W.exec(m_query); + return total_rows.size(); + } } diff --git a/core/database/database/Database.hpp b/core/database/database/Database.hpp index dcc2833..b41c16d 100644 --- a/core/database/database/Database.hpp +++ b/core/database/database/Database.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace mad::core { @@ -24,6 +25,8 @@ namespace mad::core { std::string get_levelname(std::size_t id); + std::size_t get_levels_total(); + void increment_progress(std::size_t id); void increment_progress(const std::string &username); diff --git a/core/loader/LevelLoaderFromFile.cpp b/core/loader/LevelLoaderFromFile.cpp index 45bfe37..460f637 100644 --- a/core/loader/LevelLoaderFromFile.cpp +++ b/core/loader/LevelLoaderFromFile.cpp @@ -1,10 +1,7 @@ #include "LevelLoaderFromFile.hpp" #include -#include "event/management/condition/KeyDownCondition.hpp" -#include "event/management/condition/KeyPressedCondition.hpp" -#include "event/management/condition/TimerCondition.hpp" -#include "event/management/condition/TrueCondition.hpp" -#include "event/management/controller/statemachine/StateMachine.hpp" +#include +#include #include diff --git a/network/client/simple-client.cpp b/network/client/simple-client.cpp index d35f011..a7977b3 100644 --- a/network/client/simple-client.cpp +++ b/network/client/simple-client.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include @@ -60,6 +60,23 @@ namespace mad::core { SPDLOG_DEBUG("Update request result:\n\tStatus: " + std::to_string(res->status) + "\n\tMessage: " + res->body); } + std::vector> get_levels() { + auto res = m_client.Get("/level/total"); + std::size_t level_count = std::stoi(res->body); + std::vector> loaders; + for (std::size_t i = 1; i <= level_count; ++i) { + auto res1 = m_client.Post("/level/load", httplib::Params{ + {"number", std::to_string(i)} + }); + json config; + std::stringstream ss; + ss << res1->body; + ss >> config; + loaders.push_back(std::make_shared(config["map"], config)); + } + return loaders; + } + private: mutable std::string m_username; mutable httplib::Client m_client; @@ -84,13 +101,10 @@ int main() { auto system_listener = std::make_shared(window); - std::vector> level_loaders{ - std::make_shared("../../resources/levels/level_with_finish"), - std::make_shared("../../resources/levels/level_01") - }; - auto network_storage_driver = std::make_shared(); + std::vector> level_loaders = network_storage_driver->get_levels(); + auto game_runner = std::make_unique( level_loaders, global_dispatcher, diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 1fef0fe..d8ff77c 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include @@ -12,10 +14,15 @@ #include #include +#include + +using json = nlohmann::json; + int main() { httplib::Server svr; mad::core::Database db; + std::filesystem::path levels_directory = "../../resources/levels"; std::mutex locker; std::vector logs; @@ -24,7 +31,43 @@ int main() { std::unique_lock lock(locker); res.status = 200; res.body = "Connection successful"; - logs.push_back("Connected user port -- " + std::to_string(req.remote_port)); + logs.push_back("Connected user port " + std::to_string(req.remote_port)); + }); + + svr.Get("/level/total", [&logs, &locker, &db](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); + res.status = 200; + res.body = std::to_string(db.get_levels_total()); + logs.push_back("Send total level counter to port " + std::to_string(req.remote_port)); + }); + + svr.Post("/level/load", [&logs, &locker, &db, &levels_directory](const httplib::Request &req, httplib::Response &res) { + std::unique_lock lock(locker); + if (req.has_param("number")) { + auto number = std::stoi(req.get_param_value("number")); + if (number <= db.get_levels_total()) { + auto levelname = db.get_levelname(number); + auto cur_level_directory = levels_directory / levelname; + std::ifstream input_config(cur_level_directory / "config.json"); + std::ifstream input_map(cur_level_directory / "map"); + json config; + std::string map, map_line; + input_config >> config; + while (input_map >> map_line) { + map += map_line; + } + config["map"] = map; + res.status = 200; + res.body = to_string(config); + logs.push_back("Send level " + levelname + " to port " + std::to_string(req.remote_port)); + } else { + res.status = 404; + res.body = "Invalid number of level"; + } + } else { + res.status = 404; + res.body = "Invalid params of request"; + } }); svr.Post("/user/login", [&db, &logs, &locker](const httplib::Request &req, httplib::Response &res) { diff --git a/game/resources/background/background_01.png b/resources/background/background_01.png similarity index 100% rename from game/resources/background/background_01.png rename to resources/background/background_01.png diff --git a/game/resources/background/background_02.png b/resources/background/background_02.png similarity index 100% rename from game/resources/background/background_02.png rename to resources/background/background_02.png diff --git a/game/resources/background/background_03.png b/resources/background/background_03.png similarity index 100% rename from game/resources/background/background_03.png rename to resources/background/background_03.png diff --git a/game/resources/background/background_04.png b/resources/background/background_04.png similarity index 100% rename from game/resources/background/background_04.png rename to resources/background/background_04.png diff --git a/game/resources/background/background_05.png b/resources/background/background_05.png similarity index 100% rename from game/resources/background/background_05.png rename to resources/background/background_05.png diff --git a/resources/levels/level_01/config.json b/resources/levels/level_01/config.json index d3d1309..d6ff91f 100644 --- a/resources/levels/level_01/config.json +++ b/resources/levels/level_01/config.json @@ -13,7 +13,7 @@ "zoom": 10.5 }, "background" : { - "source" : "../../game/resources/background", + "source" : "../../resources/background", "parallax_ratios" : [0.95, 0.9, 0.85, 0.8, 0.75], "scale" : 2.8 }, diff --git a/resources/levels/level_with_finish/config.json b/resources/levels/level_with_finish/config.json index de9320e..c0f4a17 100644 --- a/resources/levels/level_with_finish/config.json +++ b/resources/levels/level_with_finish/config.json @@ -13,7 +13,7 @@ "zoom": 10.5 }, "background" : { - "source" : "../../game/resources/background", + "source" : "../../resources/background", "parallax_ratios" : [0.95, 0.9, 0.85, 0.8, 0.75], "scale" : 2.8 }, From 7cef2f07a4f4c3d882e959f6333cf12067d11199 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Fri, 3 Jun 2022 18:42:47 +0300 Subject: [PATCH 41/44] something --- core/loader/LevelLoaderFromServer.cpp | 7 ++--- network/server/simple-server.cpp | 37 +++++++++++++++------------ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/core/loader/LevelLoaderFromServer.cpp b/core/loader/LevelLoaderFromServer.cpp index 0cfd1ee..7b143c1 100644 --- a/core/loader/LevelLoaderFromServer.cpp +++ b/core/loader/LevelLoaderFromServer.cpp @@ -1,11 +1,8 @@ #include "LevelLoaderFromServer.hpp" #include -#include "event/management/condition/KeyDownCondition.hpp" -#include "event/management/condition/KeyPressedCondition.hpp" -#include "event/management/condition/TimerCondition.hpp" -#include "event/management/condition/TrueCondition.hpp" -#include "event/management/controller/statemachine/StateMachine.hpp" +#include +#include #include diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index d8ff77c..54e80f3 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -167,28 +167,31 @@ int main() { ImGui::SetNextWindowPos({0, 0}); ImGui::Begin("Server util", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); - - if (ImGui::Button("Start server")) { - if (!svr.is_running()) { - std::thread([&svr]() mutable { - svr.listen("localhost", 8080); - }).detach(); - logs.emplace_back("Server has started"); + { + ImGui::BeginChild("Tool buttons", ImVec2(ImGui::GetContentRegionAvail().x * 0.3f, 80), true); + if (ImGui::Button("Start server")) { + if (!svr.is_running()) { + std::thread([&svr]() mutable { + svr.listen("localhost", 8080); + }).detach(); + logs.emplace_back("Server has started"); + } } - } - if (ImGui::Button("Stop server")) { - if (svr.is_running()) { - svr.stop(); - logs.emplace_back("Server has stopped"); + if (ImGui::Button("Stop server")) { + if (svr.is_running()) { + svr.stop(); + logs.emplace_back("Server has stopped"); + } } - } - if (ImGui::Button("Quit")) { - window.close(); - if (svr.is_running()) { - svr.stop(); + if (ImGui::Button("Quit")) { + window.close(); + if (svr.is_running()) { + svr.stop(); + } } + ImGui::EndChild(); } { From 7d466e6913cffcbb58c2ce2ae7eb6ded465e9138 Mon Sep 17 00:00:00 2001 From: denis Date: Fri, 3 Jun 2022 19:50:49 +0300 Subject: [PATCH 42/44] Input levelname --- core/database/database/Database.cpp | 7 +++++++ core/database/database/Database.hpp | 8 +++++--- network/server/simple-server.cpp | 22 ++++++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/core/database/database/Database.cpp b/core/database/database/Database.cpp index 5bac71f..8009717 100644 --- a/core/database/database/Database.cpp +++ b/core/database/database/Database.cpp @@ -45,6 +45,13 @@ namespace mad::core { return !rows_found.empty(); } + bool Database::is_level_exists(const std::string &levelname) { + pqxx::work W(m_connector); + m_query = "SELECT * FROM levels WHERE name = '" + W.esc(levelname) + "';"; + pqxx::result rows_found = W.exec(m_query); + return !rows_found.empty(); + } + void Database::registry_user(const std::string &username) { pqxx::work W(m_connector); diff --git a/core/database/database/Database.hpp b/core/database/database/Database.hpp index b41c16d..d1fb612 100644 --- a/core/database/database/Database.hpp +++ b/core/database/database/Database.hpp @@ -14,9 +14,7 @@ namespace mad::core { bool is_user_exists(const std::string &username); - void registry_user(const std::string &username); - - void append_level(const std::string &levelname); + bool is_level_exists(const std::string &levelname); std::size_t get_id(const std::string &username); @@ -27,6 +25,10 @@ namespace mad::core { std::size_t get_levels_total(); + void registry_user(const std::string &username); + + void append_level(const std::string &levelname); + void increment_progress(std::size_t id); void increment_progress(const std::string &username); diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 54e80f3..060d06b 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -147,6 +147,7 @@ int main() { sf::RenderWindow window(sf::VideoMode(640, 480), "MAD Server"); ImGui::SFML::Init(window); window.setFramerateLimit(120); + char input_levelname[255] = ""; sf::Clock clock; while (window.isOpen()) { sf::Event event; @@ -168,7 +169,9 @@ int main() { ImGui::Begin("Server util", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); { - ImGui::BeginChild("Tool buttons", ImVec2(ImGui::GetContentRegionAvail().x * 0.3f, 80), true); + std::unique_lock lock(locker); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; + ImGui::BeginChild("Tool buttons", ImVec2(ImGui::GetContentRegionAvail().x * 0.3f, 82), true, window_flags); if (ImGui::Button("Start server")) { if (!svr.is_running()) { std::thread([&svr]() mutable { @@ -194,10 +197,25 @@ int main() { ImGui::EndChild(); } + ImGui::SameLine(); + { + std::unique_lock lock(locker); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; + ImGui::BeginChild("Input levelname", ImVec2(ImGui::GetContentRegionAvail().x, 82), true, window_flags); + ImGui::InputText("##", input_levelname, 255); + ImGui::Text("new level name"); + if (ImGui::Button("Enter")) { + if (!std::string(input_levelname).empty()) { + + } + } + ImGui::EndChild(); + } + { std::unique_lock lock(locker); ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; - ImGui::BeginChild("Child", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); + ImGui::BeginChild("Logs", ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y), true, window_flags); for (int i = 0; i < logs.size(); ++i) { ImGui::Text(logs[i].c_str(), i); } From 39d234d750d86c6998426906a3524756cbfc1123 Mon Sep 17 00:00:00 2001 From: MhlvDenis Date: Sat, 4 Jun 2022 12:49:15 +0300 Subject: [PATCH 43/44] Input levelname frontend --- network/server/simple-server.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 060d06b..13210a0 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -147,7 +147,8 @@ int main() { sf::RenderWindow window(sf::VideoMode(640, 480), "MAD Server"); ImGui::SFML::Init(window); window.setFramerateLimit(120); - char input_levelname[255] = ""; + char input_levelname[255] = "\0"; + std::string text_hint = "new level name"; sf::Clock clock; while (window.isOpen()) { sf::Event event; @@ -203,10 +204,15 @@ int main() { ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; ImGui::BeginChild("Input levelname", ImVec2(ImGui::GetContentRegionAvail().x, 82), true, window_flags); ImGui::InputText("##", input_levelname, 255); - ImGui::Text("new level name"); + ImGui::Text(text_hint.c_str()); if (ImGui::Button("Enter")) { if (!std::string(input_levelname).empty()) { - + if (!db.is_level_exists(input_levelname)) { + db.append_level(input_levelname); + text_hint = "new level name"; + } else { + text_hint = "level name is already used"; + } } } ImGui::EndChild(); From 33ef74092bcb599c7fa5a3026504f75a6e9103cd Mon Sep 17 00:00:00 2001 From: denis Date: Sat, 4 Jun 2022 16:52:33 +0300 Subject: [PATCH 44/44] Input levelname with checking --- network/server/simple-server.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/network/server/simple-server.cpp b/network/server/simple-server.cpp index 13210a0..cc77ab4 100644 --- a/network/server/simple-server.cpp +++ b/network/server/simple-server.cpp @@ -207,12 +207,21 @@ int main() { ImGui::Text(text_hint.c_str()); if (ImGui::Button("Enter")) { if (!std::string(input_levelname).empty()) { - if (!db.is_level_exists(input_levelname)) { - db.append_level(input_levelname); - text_hint = "new level name"; + std::filesystem::path level_container = levels_directory / input_levelname; + if (std::filesystem::is_directory(level_container) && + std::filesystem::exists(level_container / "config.json") && + std::filesystem::exists(level_container / "map")) { + if (!db.is_level_exists(input_levelname)) { + db.append_level(input_levelname); + text_hint = "new level name"; + } else { + text_hint = "level name is already used"; + } } else { - text_hint = "level name is already used"; + text_hint = "wrong directory format"; } + } else { + text_hint = "string is empty"; } } ImGui::EndChild();