├── DxLibAnswerExample ├── .gitattributes ├── .gitignore ├── CppDxLibLecture.sln ├── Vol_00 │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Vol_00.vcxproj │ ├── Vol_00.vcxproj.filters │ └── main.cpp ├── Vol_01 │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Vol_01.vcxproj │ ├── Vol_01.vcxproj.filters │ └── main.cpp ├── Vol_02 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Player.cpp │ ├── Player.h │ ├── Vol_02.vcxproj │ ├── Vol_02.vcxproj.filters │ └── main.cpp ├── Vol_03 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── EnemyManager.cpp │ ├── EnemyManager.h │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Vol_03.vcxproj │ ├── Vol_03.vcxproj.filters │ └── main.cpp ├── Vol_04 │ ├── Main.cpp │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Vol_04.vcxproj │ └── Vol_04.vcxproj.filters ├── Vol_05 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Main.cpp │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Player.cpp │ ├── Player.h │ ├── Vol_05.vcxproj │ └── Vol_05.vcxproj.filters ├── Vol_06 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Vol_06.vcxproj │ ├── Vol_06.vcxproj.filters │ └── main.cpp ├── Vol_07 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Vol_07.vcxproj │ ├── Vol_07.vcxproj.filters │ └── main.cpp ├── Vol_08 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── EnemyManager.cpp │ ├── EnemyManager.h │ ├── GameManager.cpp │ ├── GameManager.h │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Player.cpp │ ├── Player.h │ ├── Vol_08.vcxproj │ ├── Vol_08.vcxproj.filters │ └── main.cpp ├── Vol_08_FirstHalfTask │ ├── Effect.cpp │ ├── Effect.h │ ├── EffectManager.cpp │ ├── EffectManager.h │ ├── Enemy.cpp │ ├── Enemy.h │ ├── EnemyBullet.cpp │ ├── EnemyBullet.h │ ├── EnemyBulletManager.cpp │ ├── EnemyBulletManager.h │ ├── EnemyManager.cpp │ ├── EnemyManager.h │ ├── GameManager.cpp │ ├── GameManager.h │ ├── Main.cpp │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Player.cpp │ ├── Player.h │ ├── PlayerBullet.cpp │ ├── PlayerBullet.h │ ├── PlayerBulletManager.cpp │ ├── PlayerBulletManager.h │ ├── ScoreManager.cpp │ ├── ScoreManager.h │ ├── Vol_08_FirstHalfTask.vcxproj │ └── Vol_08_FirstHalfTask.vcxproj.filters ├── Vol_09 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Vol_09.vcxproj │ ├── Vol_09.vcxproj.filters │ └── main.cpp ├── Vol_10 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Vol_10.vcxproj │ ├── Vol_10.vcxproj.filters │ └── main.cpp ├── Vol_11 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Vol_11.vcxproj │ ├── Vol_11.vcxproj.filters │ └── main.cpp └── Vol_11_FinalTask │ ├── EffectManager.cpp │ ├── EffectManager.h │ ├── EnemyBullet.cpp │ ├── EnemyBullet.h │ ├── EnemyBulletManager.cpp │ ├── EnemyBulletManager.h │ ├── EnemyManager.cpp │ ├── EnemyManager.h │ ├── EnemyRotation.cpp │ ├── EnemyRotation.h │ ├── EnemyStalker.cpp │ ├── EnemyStalker.h │ ├── EnemyStop.cpp │ ├── EnemyStop.h │ ├── GameManager.cpp │ ├── GameManager.h │ ├── IBullet.cpp │ ├── IBullet.h │ ├── IEffect.cpp │ ├── IEffect.h │ ├── IEnemy.cpp │ ├── IEnemy.h │ ├── MyGlobal.cpp │ ├── MyGlobal.h │ ├── Player.cpp │ ├── Player.h │ ├── PlayerBullet.cpp │ ├── PlayerBullet.h │ ├── PlayerBulletManager.cpp │ ├── PlayerBulletManager.h │ ├── ScoreManager.cpp │ ├── ScoreManager.h │ ├── Vol_11_FinalTask.vcxproj │ ├── Vol_11_FinalTask.vcxproj.filters │ ├── WhiteCircleEffect.cpp │ ├── WhiteCircleEffect.h │ └── main.cpp ├── DxLibTemplate2017.zip ├── vol_00_はじめに.md ├── vol_01_クラスの基本.md ├── vol_02_ファイル分け・コンポジション.md ├── vol_03_vectorの基本.md ├── vol_04_string・bool・関数オーバーロード.md ├── vol_05_参照・クラスのポインタ.md ├── vol_06_イテレータ・vector要素の削除.md ├── vol_07_remove_if・ラムダ式.md ├── vol_08_GameManagerクラス・前半課題.md ├── vol_09_new・delete・基底・派生クラス.md ├── vol_10_仮想関数・ポリモーフィズム.md ├── vol_11_スマートポインタ・最終課題.md └── vol_12_演習問題(コンソール)の解答.md /DxLibAnswerExample/.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /DxLibAnswerExample/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_00/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_00/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_00/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_00/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_00/Vol_00.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {d9525416-bb88-41f5-a0b9-fd8b875b9456} 24 | Vol_00 25 | 10.0.15063.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v141 32 | MultiByte 33 | 34 | 35 | Application 36 | false 37 | v141 38 | true 39 | MultiByte 40 | 41 | 42 | Application 43 | true 44 | v141 45 | MultiByte 46 | 47 | 48 | Application 49 | false 50 | v141 51 | true 52 | MultiByte 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | Disabled 77 | true 78 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 79 | MultiThreadedDebug 80 | 81 | 82 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 83 | 84 | 85 | 86 | 87 | Level3 88 | Disabled 89 | true 90 | 91 | 92 | 93 | 94 | Level3 95 | MaxSpeed 96 | true 97 | true 98 | true 99 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 100 | MultiThreaded 101 | 102 | 103 | true 104 | true 105 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 106 | 107 | 108 | 109 | 110 | Level3 111 | MaxSpeed 112 | true 113 | true 114 | true 115 | 116 | 117 | true 118 | true 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_00/Vol_00.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | 26 | 27 | ヘッダー ファイル 28 | 29 | 30 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_00/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_00/main.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_01/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_01/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_01/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_01/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_01/Vol_01.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {65c07226-c099-4515-a490-10ad05dcc7d8} 24 | Vol_01 25 | 10.0.15063.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v141 32 | MultiByte 33 | 34 | 35 | Application 36 | false 37 | v141 38 | true 39 | MultiByte 40 | 41 | 42 | Application 43 | true 44 | v141 45 | MultiByte 46 | 47 | 48 | Application 49 | false 50 | v141 51 | true 52 | MultiByte 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | Disabled 77 | true 78 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 79 | MultiThreadedDebug 80 | 81 | 82 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 83 | 84 | 85 | 86 | 87 | Level3 88 | Disabled 89 | true 90 | 91 | 92 | 93 | 94 | Level3 95 | MaxSpeed 96 | true 97 | true 98 | true 99 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 100 | MultiThreaded 101 | 102 | 103 | true 104 | true 105 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 106 | 107 | 108 | 109 | 110 | Level3 111 | MaxSpeed 112 | true 113 | true 114 | true 115 | 116 | 117 | true 118 | true 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_01/Vol_01.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | 26 | 27 | ヘッダー ファイル 28 | 29 | 30 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_01/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_01/main.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_02/Enemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_02/Enemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_02/Enemy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "DxLib.h" 3 | 4 | class Enemy { 5 | public: 6 | double x, y; 7 | 8 | Enemy(double _x, double _y); 9 | void update(); 10 | void draw(); 11 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_02/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_02/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_02/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_02/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_02/Player.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_02/Player.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_02/Player.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_02/Player.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_02/Vol_02.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 15.0 34 | {133e6f12-451c-4984-a308-bf2d4f1a6189} 35 | Vol_02 36 | 10.0.15063.0 37 | 38 | 39 | 40 | Application 41 | true 42 | v141 43 | MultiByte 44 | 45 | 46 | Application 47 | false 48 | v141 49 | true 50 | MultiByte 51 | 52 | 53 | Application 54 | true 55 | v141 56 | MultiByte 57 | 58 | 59 | Application 60 | false 61 | v141 62 | true 63 | MultiByte 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | Level3 87 | Disabled 88 | true 89 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 90 | MultiThreadedDebug 91 | 92 | 93 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 94 | 95 | 96 | 97 | 98 | Level3 99 | Disabled 100 | true 101 | 102 | 103 | 104 | 105 | Level3 106 | MaxSpeed 107 | true 108 | true 109 | true 110 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 111 | MultiThreaded 112 | 113 | 114 | true 115 | true 116 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 117 | 118 | 119 | 120 | 121 | Level3 122 | MaxSpeed 123 | true 124 | true 125 | true 126 | 127 | 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_02/Vol_02.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | ソース ファイル 29 | 30 | 31 | 32 | 33 | ヘッダー ファイル 34 | 35 | 36 | ヘッダー ファイル 37 | 38 | 39 | ヘッダー ファイル 40 | 41 | 42 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_02/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_02/main.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_03/Enemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_03/Enemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_03/Enemy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "DxLib.h" 3 | 4 | class Enemy { 5 | public: 6 | double x, y; 7 | double vx, vy; 8 | Enemy(double _x, double _y); 9 | void update(); 10 | void draw(); 11 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_03/EnemyManager.cpp: -------------------------------------------------------------------------------- 1 | #include "EnemyManager.h" 2 | 3 | void EnemyManager::update() 4 | { 5 | for (int i = 0; i < enemies.size(); i++) { 6 | enemies[i].update(); 7 | } 8 | } 9 | 10 | void EnemyManager::draw() 11 | { 12 | for (int i = 0; i < enemies.size(); i++) { 13 | enemies[i].draw(); 14 | } 15 | } 16 | 17 | void EnemyManager::add(Enemy enemy) 18 | { 19 | enemies.emplace_back(enemy); 20 | } 21 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_03/EnemyManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "DxLib.h" 4 | #include "Enemy.h" 5 | 6 | class EnemyManager { 7 | public: 8 | std::vector enemies; 9 | void update(); 10 | void draw(); 11 | void add(Enemy enemy); 12 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_03/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_03/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_03/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_03/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_03/Vol_03.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 15.0 34 | {f210dd2c-bed5-46ec-8fae-d52c5a700f63} 35 | Vol_03 36 | 10.0.15063.0 37 | 38 | 39 | 40 | Application 41 | true 42 | v141 43 | MultiByte 44 | 45 | 46 | Application 47 | false 48 | v141 49 | true 50 | MultiByte 51 | 52 | 53 | Application 54 | true 55 | v141 56 | MultiByte 57 | 58 | 59 | Application 60 | false 61 | v141 62 | true 63 | MultiByte 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | Level3 87 | Disabled 88 | true 89 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 90 | MultiThreadedDebug 91 | 92 | 93 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 94 | 95 | 96 | 97 | 98 | Level3 99 | Disabled 100 | true 101 | 102 | 103 | 104 | 105 | Level3 106 | MaxSpeed 107 | true 108 | true 109 | true 110 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 111 | MultiThreaded 112 | 113 | 114 | true 115 | true 116 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 117 | 118 | 119 | 120 | 121 | Level3 122 | MaxSpeed 123 | true 124 | true 125 | true 126 | 127 | 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_03/Vol_03.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ヘッダー ファイル 20 | 21 | 22 | ヘッダー ファイル 23 | 24 | 25 | ヘッダー ファイル 26 | 27 | 28 | 29 | 30 | ソース ファイル 31 | 32 | 33 | ソース ファイル 34 | 35 | 36 | ソース ファイル 37 | 38 | 39 | ソース ファイル 40 | 41 | 42 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_03/main.cpp: -------------------------------------------------------------------------------- 1 | #include "DxLib.h" 2 | #include "Enemy.h" 3 | #include "EnemyManager.h" 4 | #include "MyGlobal.h" 5 | 6 | #include 7 | 8 | 9 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 10 | { 11 | SetOutApplicationLogValidFlag(FALSE);//Log.txtを生成しないように設定 12 | ChangeWindowMode(TRUE);//非全画面にセット 13 | SetGraphMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32);//画面サイズ指定 14 | if (DxLib_Init() == 1) { return -1; }//初期化に失敗時にエラーを吐かせて終了 15 | SetDrawScreen(DX_SCREEN_BACK);//描画先を裏画面に 16 | 17 | fontHandle = CreateFontToHandle("Segoe UI", 20, 5, DX_FONTTYPE_ANTIALIASING_4X4);//フォントを読み込み 18 | 19 | EnemyManager enemyManager; // EnemyManagerのインスタンスを生成 20 | 21 | while (ProcessMessage() == 0) 22 | { 23 | ClearDrawScreen();//裏画面消す 24 | 25 | GetMousePoint(&mouseX, &mouseY); //マウス座標更新 26 | keyUpdate();//(自作関数)キー更新 27 | 28 | // Zキーが押されたら敵を生成 29 | if (keyState[KEY_INPUT_Z] == 1) { 30 | enemyManager.add(Enemy(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 31 | } 32 | 33 | enemyManager.update(); 34 | enemyManager.draw(); 35 | 36 | DrawFormatStringToHandle(20, 40, 0xFFFFFF, fontHandle, "敵の数:%3d", enemyManager.enemies.size()); 37 | 38 | ScreenFlip();//裏画面を表画面にコピー 39 | } 40 | 41 | DxLib_End(); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_04/Main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_04/Main.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_04/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_04/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_04/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_04/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_04/Vol_04.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 15.0 30 | {0724ee25-907b-44bd-8aed-c164f3872a3d} 31 | Vol_04 32 | 10.0.15063.0 33 | 34 | 35 | 36 | Application 37 | true 38 | v141 39 | MultiByte 40 | 41 | 42 | Application 43 | false 44 | v141 45 | true 46 | MultiByte 47 | 48 | 49 | Application 50 | true 51 | v141 52 | MultiByte 53 | 54 | 55 | Application 56 | false 57 | v141 58 | true 59 | MultiByte 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | Level3 83 | Disabled 84 | true 85 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 86 | MultiThreadedDebug 87 | 88 | 89 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 90 | 91 | 92 | 93 | 94 | Level3 95 | Disabled 96 | true 97 | 98 | 99 | 100 | 101 | Level3 102 | MaxSpeed 103 | true 104 | true 105 | true 106 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 107 | MultiThreaded 108 | 109 | 110 | true 111 | true 112 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 113 | 114 | 115 | 116 | 117 | Level3 118 | MaxSpeed 119 | true 120 | true 121 | true 122 | 123 | 124 | true 125 | true 126 | 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_04/Vol_04.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | 26 | 27 | ヘッダー ファイル 28 | 29 | 30 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_05/Enemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_05/Enemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_05/Enemy.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_05/Enemy.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_05/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DxLib.h" 3 | 4 | # include "Player.h" 5 | # include "Enemy.h" 6 | #include "MyGlobal.h" 7 | 8 | 9 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 10 | { 11 | SetOutApplicationLogValidFlag(FALSE);//Log.txtを生成しないように設定 12 | ChangeWindowMode(TRUE);//非全画面にセット 13 | SetGraphMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32);//画面サイズ指定 14 | if (DxLib_Init() == 1) { return -1; }//初期化に失敗時にエラーを吐かせて終了 15 | SetDrawScreen(DX_SCREEN_BACK);//描画先を裏画面に 16 | 17 | 18 | fontHandle = CreateFontToHandle("Segoe UI", 20, 5, DX_FONTTYPE_ANTIALIASING_4X4);//フォントを読み込み 19 | 20 | Player player; // プレイヤーをインスタンス化 21 | Enemy enemy(320.0, 120.0); // 敵をインスタンス化 22 | 23 | // プレイヤーへのポインタを取得 24 | enemy.setPlayerPtr(&player); 25 | 26 | while (ProcessMessage() == 0) 27 | { 28 | ClearDrawScreen();//裏画面消す 29 | 30 | GetMousePoint(&mouseX, &mouseY); //マウス座標更新 31 | keyUpdate();//(自作関数)キー更新 32 | 33 | player.update(); 34 | enemy.update(); 35 | 36 | player.draw(); 37 | enemy.draw(); 38 | 39 | ScreenFlip();//裏画面を表画面にコピー 40 | } 41 | 42 | DxLib_End(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_05/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_05/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_05/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_05/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_05/Player.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_05/Player.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_05/Player.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "DxLib.h" 3 | 4 | class Player { 5 | public: 6 | double x, y; 7 | double speed; 8 | Player(); 9 | void update(); 10 | void draw(); 11 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_05/Vol_05.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 15.0 34 | {d5e79b50-bb62-4332-8bde-a5067082c506} 35 | Vol_05 36 | 10.0.15063.0 37 | 38 | 39 | 40 | Application 41 | true 42 | v141 43 | MultiByte 44 | 45 | 46 | Application 47 | false 48 | v141 49 | true 50 | MultiByte 51 | 52 | 53 | Application 54 | true 55 | v141 56 | MultiByte 57 | 58 | 59 | Application 60 | false 61 | v141 62 | true 63 | MultiByte 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | Level3 87 | Disabled 88 | true 89 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 90 | MultiThreadedDebug 91 | 92 | 93 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 94 | 95 | 96 | 97 | 98 | Level3 99 | Disabled 100 | true 101 | 102 | 103 | 104 | 105 | Level3 106 | MaxSpeed 107 | true 108 | true 109 | true 110 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 111 | MultiThreaded 112 | 113 | 114 | true 115 | true 116 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 117 | 118 | 119 | 120 | 121 | Level3 122 | MaxSpeed 123 | true 124 | true 125 | true 126 | 127 | 128 | true 129 | true 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_05/Vol_05.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | ソース ファイル 29 | 30 | 31 | 32 | 33 | ヘッダー ファイル 34 | 35 | 36 | ヘッダー ファイル 37 | 38 | 39 | ヘッダー ファイル 40 | 41 | 42 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_06/Enemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_06/Enemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_06/Enemy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "DxLib.h" 3 | 4 | class Enemy { 5 | public: 6 | double x, y; 7 | double vx, vy; 8 | Enemy(double _x, double _y); 9 | void update(); 10 | void draw(); 11 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_06/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_06/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_06/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_06/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_06/Vol_06.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 15.0 32 | {4a3088fb-3038-4352-9e6f-51856345ddc0} 33 | Vol_06 34 | 10.0.15063.0 35 | 36 | 37 | 38 | Application 39 | true 40 | v141 41 | MultiByte 42 | 43 | 44 | Application 45 | false 46 | v141 47 | true 48 | MultiByte 49 | 50 | 51 | Application 52 | true 53 | v141 54 | MultiByte 55 | 56 | 57 | Application 58 | false 59 | v141 60 | true 61 | MultiByte 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Level3 85 | Disabled 86 | true 87 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 88 | MultiThreadedDebug 89 | 90 | 91 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 92 | 93 | 94 | 95 | 96 | Level3 97 | Disabled 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | MaxSpeed 105 | true 106 | true 107 | true 108 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 109 | MultiThreaded 110 | 111 | 112 | true 113 | true 114 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 115 | 116 | 117 | 118 | 119 | Level3 120 | MaxSpeed 121 | true 122 | true 123 | true 124 | 125 | 126 | true 127 | true 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_06/Vol_06.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | 29 | 30 | ヘッダー ファイル 31 | 32 | 33 | ヘッダー ファイル 34 | 35 | 36 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_06/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "DxLib.h" 5 | 6 | #include "MyGlobal.h" 7 | #include "Enemy.h" 8 | 9 | 10 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 11 | { 12 | SetOutApplicationLogValidFlag(FALSE);//Log.txtを生成しないように設定 13 | ChangeWindowMode(TRUE);//非全画面にセット 14 | SetGraphMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32);//画面サイズ指定 15 | if (DxLib_Init() == 1) { return -1; }//初期化に失敗時にエラーを吐かせて終了 16 | SetDrawScreen(DX_SCREEN_BACK);//描画先を裏画面に 17 | 18 | 19 | fontHandle = CreateFontToHandle("Segoe UI", 20, 5, DX_FONTTYPE_ANTIALIASING_4X4);//フォントを読み込み 20 | 21 | std::vector vec; 22 | 23 | while (ProcessMessage() == 0) 24 | { 25 | ClearDrawScreen();//裏画面消す 26 | 27 | GetMousePoint(&mouseX, &mouseY); //マウス座標更新 28 | keyUpdate();//(自作関数)キー更新 29 | 30 | // Zキーが押されたらランダムな座標に敵を生成 31 | if (keyState[KEY_INPUT_Z] == 1) { 32 | vec.emplace_back(Enemy(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); // 敵をインスタンス化 33 | } 34 | 35 | // 画面外の敵を削除 36 | auto iter = vec.begin(); 37 | while (iter != vec.end()) { 38 | if (iter->x < 0 || iter->x > SCREEN_WIDTH || iter->y < 0 || iter->y > SCREEN_HEIGHT) { //画面外に出ているか確認 39 | iter = vec.erase(iter); 40 | } 41 | else { 42 | iter++; 43 | } 44 | } 45 | 46 | for (auto i = vec.begin(); i < vec.end(); i++) { 47 | i->update(); 48 | } 49 | for (auto i = vec.begin(); i < vec.end(); i++) { 50 | i->draw(); 51 | } 52 | 53 | DrawFormatStringToHandle(20, 20, GetColor(255, 255, 255), fontHandle, "Zキーで敵を生成 敵の数:%d", vec.size()); 54 | 55 | ScreenFlip();//裏画面を表画面にコピー 56 | } 57 | 58 | DxLib_End(); 59 | return 0; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_07/Enemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_07/Enemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_07/Enemy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "DxLib.h" 3 | 4 | class Enemy { 5 | public: 6 | double x, y; 7 | double vx, vy; 8 | Enemy(double _x, double _y); 9 | void update(); 10 | void draw(); 11 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_07/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_07/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_07/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_07/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_07/Vol_07.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 15.0 32 | {65a3454d-0e8e-4b45-a32e-0bcf51b12f91} 33 | Vol_07 34 | 10.0.15063.0 35 | 36 | 37 | 38 | Application 39 | true 40 | v141 41 | MultiByte 42 | 43 | 44 | Application 45 | false 46 | v141 47 | true 48 | MultiByte 49 | 50 | 51 | Application 52 | true 53 | v141 54 | MultiByte 55 | 56 | 57 | Application 58 | false 59 | v141 60 | true 61 | MultiByte 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Level3 85 | Disabled 86 | true 87 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 88 | MultiThreadedDebug 89 | 90 | 91 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 92 | 93 | 94 | 95 | 96 | Level3 97 | Disabled 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | MaxSpeed 105 | true 106 | true 107 | true 108 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 109 | MultiThreaded 110 | 111 | 112 | true 113 | true 114 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 115 | 116 | 117 | 118 | 119 | Level3 120 | MaxSpeed 121 | true 122 | true 123 | true 124 | 125 | 126 | true 127 | true 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_07/Vol_07.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | 29 | 30 | ヘッダー ファイル 31 | 32 | 33 | ヘッダー ファイル 34 | 35 | 36 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_07/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "DxLib.h" 6 | 7 | #include "Enemy.h" 8 | #include "MyGlobal.h" 9 | 10 | 11 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 12 | { 13 | SetOutApplicationLogValidFlag(FALSE);//Log.txtを生成しないように設定 14 | ChangeWindowMode(TRUE);//非全画面にセット 15 | SetGraphMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32);//画面サイズ指定 16 | if (DxLib_Init() == 1) { return -1; }//初期化に失敗時にエラーを吐かせて終了 17 | SetDrawScreen(DX_SCREEN_BACK);//描画先を裏画面に 18 | 19 | 20 | fontHandle = CreateFontToHandle("Segoe UI", 20, 5, DX_FONTTYPE_ANTIALIASING_4X4);//フォントを読み込み 21 | 22 | std::vector vec; 23 | 24 | while (ProcessMessage() == 0) 25 | { 26 | ClearDrawScreen();//裏画面消す 27 | 28 | GetMousePoint(&mouseX, &mouseY); //マウス座標更新 29 | keyUpdate();//(自作関数)キー更新 30 | 31 | // Zキーが押されたらランダムな座標に敵を生成 32 | if (keyState[KEY_INPUT_Z] == 1) { 33 | vec.emplace_back(Enemy(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); // 敵をインスタンス化 34 | } 35 | 36 | auto rmvIter = std::remove_if(vec.begin(), vec.end(), [](const Enemy& i) { 37 | return i.x < 0 || i.x > SCREEN_WIDTH || i.y < 0 || i.y > SCREEN_HEIGHT; 38 | }); 39 | 40 | vec.erase(rmvIter, vec.end()); 41 | 42 | for (auto i = vec.begin(); i < vec.end(); i++) { 43 | i->update(); 44 | } 45 | for (auto i = vec.begin(); i < vec.end(); i++) { 46 | i->draw(); 47 | } 48 | 49 | // 敵の数を描画 50 | DrawFormatStringToHandle(20, 20, GetColor(255, 255, 255), fontHandle, "敵の数:%d", vec.size()); 51 | 52 | ScreenFlip();//裏画面を表画面にコピー 53 | } 54 | 55 | DxLib_End(); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include "DxLib.h" 2 | #include "MyGlobal.h" 3 | #include "Enemy.h" 4 | #include "GameManager.h" 5 | 6 | const double Enemy::Radius = 24.0; 7 | 8 | Enemy::Enemy(double _x, double _y, Kind _kind) : 9 | x(_x), y(_y), 10 | vx(0.0), vy(0.0), 11 | kind(_kind), 12 | elapsedFrame(0) 13 | { 14 | } 15 | 16 | void Enemy::update() { 17 | if (kind == Stalker) { 18 | Player& player = gameManager.player; 19 | const double Speed = 1.5; 20 | normalize(x, y, player.x, player.y, &vx, &vy); 21 | vx *= Speed; 22 | vy *= Speed; 23 | x += vx; 24 | y += vy; 25 | } 26 | else if (kind == Rotation) { 27 | double angle = elapsedFrame * 0.02; 28 | vx = 1.0 * cos(angle); 29 | vy = 1.0 * sin(angle); 30 | } 31 | else if (kind == Straight) { 32 | vx = 0.0; 33 | vy = 1.0; 34 | } 35 | x += vx; 36 | y += vy; 37 | elapsedFrame++; 38 | } 39 | 40 | void Enemy::draw() { 41 | if (kind == Stalker) { 42 | DrawCircle(x, y, static_cast(Radius), GetColor(255, 0, 0)); 43 | } 44 | else if (kind == Rotation) { 45 | DrawCircle(x, y, static_cast(Radius), GetColor(255, 128, 0)); 46 | } 47 | else if (kind == Straight) { 48 | DrawCircle(x, y, static_cast(Radius), GetColor(255, 0, 128)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/Enemy.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08/Enemy.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/EnemyManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08/EnemyManager.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/EnemyManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08/EnemyManager.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/GameManager.cpp: -------------------------------------------------------------------------------- 1 | #include "GameManager.h" 2 | 3 | GameManager::GameManager() 4 | { 5 | } 6 | 7 | void GameManager::update() 8 | { 9 | player.update(); 10 | enemyManager.update(); 11 | } 12 | 13 | void GameManager::draw() 14 | { 15 | player.draw(); 16 | enemyManager.draw(); 17 | } 18 | 19 | 20 | GameManager gameManager; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/GameManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Player.h" 3 | #include "EnemyManager.h" 4 | 5 | class GameManager 6 | { 7 | public: 8 | GameManager(); 9 | Player player; 10 | EnemyManager enemyManager; 11 | void update(); 12 | void draw(); 13 | }; 14 | 15 | extern GameManager gameManager; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/Player.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08/Player.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/Player.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "DxLib.h" 3 | 4 | class Player { 5 | public: 6 | static const double Speed; 7 | double x, y; 8 | Player(); 9 | void update(); 10 | void draw(); 11 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/Vol_08.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 15.0 38 | {11cbbc6d-072f-42e5-b1e9-97ed5c838f4a} 39 | Vol_08 40 | 10.0.15063.0 41 | 42 | 43 | 44 | Application 45 | true 46 | v141 47 | MultiByte 48 | 49 | 50 | Application 51 | false 52 | v141 53 | true 54 | MultiByte 55 | 56 | 57 | Application 58 | true 59 | v141 60 | MultiByte 61 | 62 | 63 | Application 64 | false 65 | v141 66 | true 67 | MultiByte 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | Level3 91 | Disabled 92 | true 93 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 94 | MultiThreadedDebug 95 | 96 | 97 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 98 | 99 | 100 | 101 | 102 | Level3 103 | Disabled 104 | true 105 | 106 | 107 | 108 | 109 | Level3 110 | MaxSpeed 111 | true 112 | true 113 | true 114 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 115 | MultiThreaded 116 | 117 | 118 | true 119 | true 120 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 121 | 122 | 123 | 124 | 125 | Level3 126 | MaxSpeed 127 | true 128 | true 129 | true 130 | 131 | 132 | true 133 | true 134 | 135 | 136 | 137 | 138 | 139 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/Vol_08.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | ソース ファイル 29 | 30 | 31 | ソース ファイル 32 | 33 | 34 | ソース ファイル 35 | 36 | 37 | 38 | 39 | ヘッダー ファイル 40 | 41 | 42 | ヘッダー ファイル 43 | 44 | 45 | ヘッダー ファイル 46 | 47 | 48 | ヘッダー ファイル 49 | 50 | 51 | ヘッダー ファイル 52 | 53 | 54 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08/main.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/Effect.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/Effect.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/Effect.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/Effect.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/EffectManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/EffectManager.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/EffectManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/EffectManager.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/Enemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/Enemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/Enemy.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/Enemy.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/EnemyBullet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DxLib.h" 3 | #include "EnemyBullet.h" 4 | 5 | const double EnemyBullet::Radius = 8.0; 6 | const unsigned int EnemyBullet::Color = 0xFFFFFF; 7 | const int EnemyBullet::LifeTime = 900; 8 | 9 | EnemyBullet::EnemyBullet(double _x, double _y, double _vx, double _vy) : 10 | x(_x), y(_y), 11 | vx(_vx), vy(_vy), 12 | elapsedFrame(0), 13 | isDead(false) 14 | { 15 | } 16 | 17 | void EnemyBullet::update() { 18 | checkDead(); 19 | move(); 20 | elapsedFrame++; 21 | } 22 | 23 | void EnemyBullet::move() { 24 | x += vx; y+= vy; 25 | } 26 | 27 | void EnemyBullet::checkDead() { 28 | if (elapsedFrame > LifeTime) { 29 | isDead = true; 30 | } 31 | } 32 | 33 | void EnemyBullet::draw() const { 34 | DrawCircle(x, y, Radius, Color); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/EnemyBullet.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/EnemyBullet.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/EnemyBulletManager.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DxLib.h" 3 | #include "EnemyBulletManager.h" 4 | 5 | EnemyBulletManager::EnemyBulletManager() { 6 | } 7 | 8 | void EnemyBulletManager::add(const EnemyBullet& ins) { 9 | enemyBullets.emplace_back(ins); 10 | } 11 | 12 | void EnemyBulletManager::update() { 13 | for (auto&& enemyBullet : enemyBullets) { 14 | enemyBullet.update(); 15 | } 16 | 17 | auto iter = std::remove_if(enemyBullets.begin(), enemyBullets.end(), [](const EnemyBullet& ins) { 18 | return ins.isDead; 19 | }); 20 | enemyBullets.erase(iter, enemyBullets.end()); 21 | } 22 | 23 | void EnemyBulletManager::draw() const { 24 | for (auto&& enemyBullet : enemyBullets) { 25 | enemyBullet.draw(); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/EnemyBulletManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/EnemyBulletManager.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/EnemyManager.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "EnemyManager.h" 3 | #include "MyGlobal.h" 4 | 5 | void EnemyManager::update() { 6 | for (auto&& enemy : enemies) { 7 | enemy.update(); 8 | } 9 | checkDelete(); 10 | } 11 | 12 | void EnemyManager::draw() const{ 13 | for (const auto& enemy : enemies) { 14 | enemy.draw(); 15 | } 16 | } 17 | 18 | void EnemyManager::add(const Enemy& ins) 19 | { 20 | enemies.emplace_back(ins); 21 | } 22 | 23 | size_t EnemyManager::getEnemyNum(){ 24 | return enemies.size(); 25 | } 26 | 27 | void EnemyManager::checkDelete() { 28 | auto it = enemies.begin(); 29 | 30 | auto rmvIter = std::remove_if(enemies.begin(), enemies.end(), [](const Enemy& enemy) { 31 | return enemy.isDead; 32 | }); 33 | 34 | enemies.erase(rmvIter, enemies.end()); 35 | } -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/EnemyManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/EnemyManager.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/GameManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/GameManager.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/GameManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "DxLib.h" 3 | #include "Player.h" 4 | #include "Player.h" 5 | #include "PlayerBulletManager.h" 6 | #include "EnemyManager.h" 7 | #include "EnemyBulletManager.h" 8 | #include "EffectManager.h" 9 | #include "ScoreManager.h" 10 | 11 | class GameManager{ 12 | public: 13 | Player player; 14 | PlayerBulletManager pBulletManager; 15 | EnemyManager enemyManager; 16 | EnemyBulletManager eBulletManager; 17 | EffectManager effectManager; 18 | ScoreManager scoreManager; 19 | int fontHandle; 20 | 21 | GameManager(); 22 | void update(); 23 | void draw() const; 24 | void load(); 25 | }; 26 | 27 | extern GameManager gameManager; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/Main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/Main.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/Player.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/Player.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/Player.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | class Player{ 5 | public: 6 | static const double Radius; 7 | static const size_t FireRate; 8 | static const double MoveSpeed; 9 | static const double BulletFirstVelocityX; 10 | static const double BulletFirstVelocityY; 11 | static const double BulletFirstDeltaX; 12 | static const double BulletFirstDeltaY; 13 | static const size_t EffectNum; 14 | static const double EffectRange; 15 | static const unsigned int Color; 16 | static const double FirstPosX; 17 | static const double FirstPosY; 18 | 19 | double x, y; 20 | int fireCool; 21 | 22 | Player(); 23 | void update(); 24 | void draw() const; 25 | void move(); 26 | void shot(); 27 | void checkHit(); 28 | }; 29 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/PlayerBullet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DxLib.h" 3 | #include "PlayerBullet.h" 4 | 5 | const double PlayerBullet::Radius = 4.0; 6 | const double PlayerBullet::Attack = 1.0; 7 | const unsigned int PlayerBullet::Color = 0x0000AA; 8 | const int PlayerBullet::LifeTime = 60; 9 | 10 | PlayerBullet::PlayerBullet(double _x, double _y, double _vx, double _vy): 11 | x(_x), y(_y), 12 | vx(_vx), vy(_vy), 13 | elapsedFrame(0), 14 | isDead(false) 15 | { 16 | } 17 | 18 | void PlayerBullet::update() { 19 | checkDead(); 20 | move(); 21 | elapsedFrame++; 22 | } 23 | 24 | void PlayerBullet::move() { 25 | x += vx; y+= vy; 26 | } 27 | 28 | void PlayerBullet::checkDead() { 29 | if (elapsedFrame > LifeTime) { 30 | isDead = true; 31 | } 32 | } 33 | 34 | void PlayerBullet::draw() const { 35 | DrawCircle(x, y, Radius, Color); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/PlayerBullet.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/PlayerBullet.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/PlayerBulletManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/PlayerBulletManager.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/PlayerBulletManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "PlayerBullet.h" 4 | 5 | class PlayerBulletManager { 6 | public: 7 | std::vector playerBullets; 8 | PlayerBulletManager(); 9 | void add(const PlayerBullet& ins); 10 | void update(); 11 | void draw() const; 12 | }; 13 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/ScoreManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_08_FirstHalfTask/ScoreManager.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/ScoreManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "DxLib.h" 3 | 4 | class ScoreManager{ 5 | private: 6 | int score; 7 | int damagedNum; 8 | public: 9 | ScoreManager(); 10 | void addScore(int n); 11 | int getScore(); 12 | void addDamagedNum(); 13 | int getDamagedNum(); 14 | void update(); 15 | void draw() const; 16 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_08_FirstHalfTask/Vol_08_FirstHalfTask.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | ソース ファイル 29 | 30 | 31 | ソース ファイル 32 | 33 | 34 | ソース ファイル 35 | 36 | 37 | ソース ファイル 38 | 39 | 40 | ソース ファイル 41 | 42 | 43 | ソース ファイル 44 | 45 | 46 | ソース ファイル 47 | 48 | 49 | ソース ファイル 50 | 51 | 52 | ソース ファイル 53 | 54 | 55 | ソース ファイル 56 | 57 | 58 | 59 | 60 | ヘッダー ファイル 61 | 62 | 63 | ヘッダー ファイル 64 | 65 | 66 | ヘッダー ファイル 67 | 68 | 69 | ヘッダー ファイル 70 | 71 | 72 | ヘッダー ファイル 73 | 74 | 75 | ヘッダー ファイル 76 | 77 | 78 | ヘッダー ファイル 79 | 80 | 81 | ヘッダー ファイル 82 | 83 | 84 | ヘッダー ファイル 85 | 86 | 87 | ヘッダー ファイル 88 | 89 | 90 | ヘッダー ファイル 91 | 92 | 93 | ヘッダー ファイル 94 | 95 | 96 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_09/Enemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_09/Enemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_09/Enemy.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_09/Enemy.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_09/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_09/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_09/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_09/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_09/Vol_09.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 15.0 32 | {d0ba0ccc-7c04-4c0f-a01e-8f11de200031} 33 | Vol_09 34 | 10.0.15063.0 35 | 36 | 37 | 38 | Application 39 | true 40 | v141 41 | MultiByte 42 | 43 | 44 | Application 45 | false 46 | v141 47 | true 48 | MultiByte 49 | 50 | 51 | Application 52 | true 53 | v141 54 | MultiByte 55 | 56 | 57 | Application 58 | false 59 | v141 60 | true 61 | MultiByte 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Level3 85 | Disabled 86 | true 87 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 88 | MultiThreadedDebug 89 | 90 | 91 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 92 | 93 | 94 | 95 | 96 | Level3 97 | Disabled 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | MaxSpeed 105 | true 106 | true 107 | true 108 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 109 | MultiThreaded 110 | 111 | 112 | true 113 | true 114 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 115 | 116 | 117 | 118 | 119 | Level3 120 | MaxSpeed 121 | true 122 | true 123 | true 124 | 125 | 126 | true 127 | true 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_09/Vol_09.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | 29 | 30 | ヘッダー ファイル 31 | 32 | 33 | ヘッダー ファイル 34 | 35 | 36 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_09/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DxLib.h" 3 | #include "Enemy.h" 4 | #include "MyGlobal.h" 5 | 6 | 7 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 8 | { 9 | SetOutApplicationLogValidFlag(FALSE);//Log.txtを生成しないように設定 10 | ChangeWindowMode(TRUE);//非全画面にセット 11 | SetGraphMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32);//画面サイズ指定 12 | if (DxLib_Init() == 1) { return -1; }//初期化に失敗時にエラーを吐かせて終了 13 | SetDrawScreen(DX_SCREEN_BACK);//描画先を裏画面に 14 | 15 | fontHandle = CreateFontToHandle("Segoe UI", 20, 5, DX_FONTTYPE_ANTIALIASING_4X4);//フォントを読み込み 16 | 17 | std::vector vec; 18 | 19 | while (ProcessMessage() == 0) 20 | { 21 | ClearDrawScreen();//裏画面消す 22 | 23 | GetMousePoint(&mouseX, &mouseY); //マウス座標更新 24 | keyUpdate();//(自作関数)キー更新 25 | 26 | // Z, X, Cキーが押されたらランダムな座標に敵を生成 27 | if (keyState[KEY_INPUT_Z] == 1) { 28 | vec.emplace_back(new EnemySinMove(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 29 | } 30 | if (keyState[KEY_INPUT_X] == 1) { 31 | vec.emplace_back(new EnemyRotation(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 32 | } 33 | if (keyState[KEY_INPUT_C] == 1) { 34 | vec.emplace_back(new EnemyStraight(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 35 | } 36 | 37 | // 画面外の敵を削除 38 | auto it = vec.begin(); 39 | while (it != vec.end()) { 40 | // 画面外に出ているか確認 41 | // 敵の座標と画面の長方形との当たり判定を取り、その否定を条件にすることで画面外に出ているか確認している 42 | if (!checkHitPointRect((*it)->x, (*it)->y, 0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT)) { 43 | // メモリ解放 44 | delete (*it); 45 | it = vec.erase(it); 46 | } 47 | else { 48 | it++; 49 | } 50 | } 51 | 52 | for (const auto& enemy : vec) { 53 | enemy->update(); 54 | } 55 | for (const auto& enemy : vec) { 56 | enemy->draw(); 57 | } 58 | 59 | // 敵の数を描画 60 | DrawFormatStringToHandle(20, 40, 0xFFFFFF, fontHandle, "Z, X, Cで敵生成 敵の数:%3d", vec.size()); 61 | 62 | ScreenFlip();//裏画面を表画面にコピー 63 | } 64 | 65 | DxLib_End(); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_10/Enemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_10/Enemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_10/Enemy.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_10/Enemy.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_10/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_10/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_10/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_10/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_10/Vol_10.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 15.0 32 | {46d982f5-ba74-44bb-8acb-daf8f0bff7ec} 33 | Vol_10 34 | 10.0.15063.0 35 | 36 | 37 | 38 | Application 39 | true 40 | v141 41 | MultiByte 42 | 43 | 44 | Application 45 | false 46 | v141 47 | true 48 | MultiByte 49 | 50 | 51 | Application 52 | true 53 | v141 54 | MultiByte 55 | 56 | 57 | Application 58 | false 59 | v141 60 | true 61 | MultiByte 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Level3 85 | Disabled 86 | true 87 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 88 | MultiThreadedDebug 89 | 90 | 91 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 92 | 93 | 94 | 95 | 96 | Level3 97 | Disabled 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | MaxSpeed 105 | true 106 | true 107 | true 108 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 109 | MultiThreaded 110 | 111 | 112 | true 113 | true 114 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 115 | 116 | 117 | 118 | 119 | Level3 120 | MaxSpeed 121 | true 122 | true 123 | true 124 | 125 | 126 | true 127 | true 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_10/Vol_10.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | 29 | 30 | ヘッダー ファイル 31 | 32 | 33 | ヘッダー ファイル 34 | 35 | 36 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_10/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DxLib.h" 3 | #include "MyGlobal.h" 4 | #include "Enemy.h" 5 | 6 | 7 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 8 | { 9 | SetOutApplicationLogValidFlag(FALSE);//Log.txtを生成しないように設定 10 | ChangeWindowMode(TRUE);//非全画面にセット 11 | SetGraphMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32);//画面サイズ指定 12 | if (DxLib_Init() == 1) { return -1; }//初期化に失敗時にエラーを吐かせて終了 13 | SetDrawScreen(DX_SCREEN_BACK);//描画先を裏画面に 14 | 15 | fontHandle = CreateFontToHandle("Segoe UI", 20, 5, DX_FONTTYPE_ANTIALIASING_4X4);//フォントを読み込み 16 | 17 | std::vector vec; 18 | 19 | while (ProcessMessage() == 0) 20 | { 21 | ClearDrawScreen();//裏画面消す 22 | 23 | GetMousePoint(&mouseX, &mouseY); //マウス座標更新 24 | keyUpdate();//(自作関数)キー更新 25 | 26 | // Z, X, Cキーが押されたらランダムな座標に敵を生成 27 | if (keyState[KEY_INPUT_Z] == 1) { 28 | vec.emplace_back(new EnemySinMove(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 29 | } 30 | if (keyState[KEY_INPUT_X] == 1) { 31 | vec.emplace_back(new EnemyRotation(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 32 | } 33 | if (keyState[KEY_INPUT_C] == 1) { 34 | vec.emplace_back(new EnemyStraight(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 35 | } 36 | 37 | // 画面外の敵を削除 38 | auto it = vec.begin(); 39 | while (it != vec.end()) { 40 | // 画面外に出ているか確認 41 | // 敵の座標点と画面の長方形との当たり判定を取り、その否定を条件にすることで画面外に出ているか確認している 42 | if (!checkHitPointRect((*it)->x, (*it)->y, 0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT)) { 43 | // メモリ解放 44 | delete (*it); 45 | it = vec.erase(it); 46 | } 47 | else { 48 | it++; 49 | } 50 | } 51 | 52 | for (const auto& enemy : vec) { 53 | enemy->update(); 54 | } 55 | for (const auto& enemy : vec) { 56 | enemy->draw(); 57 | } 58 | 59 | DrawFormatStringToHandle(20, 40, 0xFFFFFF, fontHandle, "Z, X, Cで敵生成 敵の数:%3d", vec.size()); 60 | 61 | ScreenFlip();//裏画面を表画面にコピー 62 | } 63 | 64 | DxLib_End(); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11/Enemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11/Enemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11/Enemy.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11/Enemy.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11/Vol_11.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 15.0 32 | {88ca773b-81a9-4666-8d63-cfbf6a92dbc1} 33 | Vol_11 34 | 10.0.15063.0 35 | 36 | 37 | 38 | Application 39 | true 40 | v141 41 | MultiByte 42 | 43 | 44 | Application 45 | false 46 | v141 47 | true 48 | MultiByte 49 | 50 | 51 | Application 52 | true 53 | v141 54 | MultiByte 55 | 56 | 57 | Application 58 | false 59 | v141 60 | true 61 | MultiByte 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | Level3 85 | Disabled 86 | true 87 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 88 | MultiThreadedDebug 89 | 90 | 91 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 92 | 93 | 94 | 95 | 96 | Level3 97 | Disabled 98 | true 99 | 100 | 101 | 102 | 103 | Level3 104 | MaxSpeed 105 | true 106 | true 107 | true 108 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 109 | MultiThreaded 110 | 111 | 112 | true 113 | true 114 | c:\DxLib_VC\プロジェクトに追加すべきファイル_VC用 115 | 116 | 117 | 118 | 119 | Level3 120 | MaxSpeed 121 | true 122 | true 123 | true 124 | 125 | 126 | true 127 | true 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11/Vol_11.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | 29 | 30 | ヘッダー ファイル 31 | 32 | 33 | ヘッダー ファイル 34 | 35 | 36 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "DxLib.h" 5 | #include "MyGlobal.h" 6 | #include "Enemy.h" 7 | 8 | 9 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 10 | { 11 | SetOutApplicationLogValidFlag(FALSE);//Log.txtを生成しないように設定 12 | ChangeWindowMode(TRUE);//非全画面にセット 13 | SetGraphMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32);//画面サイズ指定 14 | if (DxLib_Init() == 1) { return -1; }//初期化に失敗時にエラーを吐かせて終了 15 | SetDrawScreen(DX_SCREEN_BACK);//描画先を裏画面に 16 | 17 | fontHandle = CreateFontToHandle("Segoe UI", 20, 5, DX_FONTTYPE_ANTIALIASING_4X4);//フォントを読み込み 18 | 19 | std::vector> vec; 20 | 21 | while (ProcessMessage() == 0) 22 | { 23 | ClearDrawScreen();//裏画面消す 24 | 25 | GetMousePoint(&mouseX, &mouseY); //マウス座標更新 26 | keyUpdate();//(自作関数)キー更新 27 | 28 | // Z, X, Cキーが押されたらランダムな座標に敵を生成 29 | if (keyState[KEY_INPUT_Z] == 1) { 30 | vec.emplace_back(std::make_shared(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 31 | } 32 | if (keyState[KEY_INPUT_X] == 1) { 33 | vec.emplace_back(std::make_shared(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 34 | } 35 | if (keyState[KEY_INPUT_C] == 1) { 36 | vec.emplace_back(std::make_shared(GetRand(SCREEN_WIDTH), GetRand(SCREEN_HEIGHT))); 37 | } 38 | 39 | // 画面外の敵を削除 40 | auto rmvIter = std::remove_if(vec.begin(), vec.end(), [](const std::shared_ptr& enemy) { 41 | return !checkHitPointRect(enemy->x, enemy->y, 0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT); 42 | }); 43 | 44 | vec.erase(rmvIter, vec.end()); 45 | 46 | for (const auto& enemy : vec) { 47 | enemy->update(); 48 | } 49 | 50 | for (const auto& enemy : vec) { 51 | enemy->draw(); 52 | } 53 | 54 | DrawFormatStringToHandle(20, 40, 0xFFFFFF, fontHandle, "Z, X, Cで敵生成 敵の数:%3d", vec.size()); 55 | 56 | ScreenFlip();//裏画面を表画面にコピー 57 | } 58 | 59 | DxLib_End(); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EffectManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/EffectManager.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EffectManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/EffectManager.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyBullet.cpp: -------------------------------------------------------------------------------- 1 | #include "EnemyBullet.h" 2 | 3 | const double EnemyBullet::Radius = 8.0; 4 | const double EnemyBullet::Attack = 1.0; 5 | const unsigned int EnemyBullet::Color = 0xFFFFFF; 6 | const int EnemyBullet::LifeTime = 900; 7 | 8 | EnemyBullet::EnemyBullet(double _x, double _y, double _vx, double _vy) : 9 | IBullet(_x, _y, _vx, _vy, Radius, Attack, Color, LifeTime) 10 | { 11 | } 12 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyBullet.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/EnemyBullet.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyBulletManager.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "EnemyBulletManager.h" 3 | 4 | EnemyBulletManager::EnemyBulletManager() { 5 | } 6 | 7 | void EnemyBulletManager::add(const std::shared_ptr& ptr) { 8 | enemyBullets.emplace_back(ptr); 9 | } 10 | 11 | void EnemyBulletManager::update() { 12 | for (const auto& enemyBullet : enemyBullets) { 13 | enemyBullet->update(); 14 | } 15 | 16 | auto iter = std::remove_if(enemyBullets.begin(), enemyBullets.end(), [](const std::shared_ptr& ptr) { 17 | return ptr->isDead; 18 | }); 19 | enemyBullets.erase(iter, enemyBullets.end()); 20 | } 21 | 22 | void EnemyBulletManager::draw() const { 23 | for (const auto& enemyBullet : enemyBullets) { 24 | enemyBullet->draw(); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyBulletManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/EnemyBulletManager.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyManager.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "EnemyManager.h" 3 | 4 | EnemyManager::EnemyManager() { 5 | } 6 | 7 | void EnemyManager::add(const std::shared_ptr& ptr) { 8 | enemies.emplace_back(ptr); 9 | } 10 | 11 | void EnemyManager::update() { 12 | for (const auto& enemy : enemies) { 13 | enemy->update(); 14 | } 15 | 16 | auto iter = std::remove_if(enemies.begin(), enemies.end(), [](const std::shared_ptr& ptr) { 17 | return ptr->isDead; 18 | }); 19 | enemies.erase(iter, enemies.end()); 20 | } 21 | 22 | void EnemyManager::draw() const { 23 | for (const auto& enemy : enemies) { 24 | enemy->draw(); 25 | } 26 | } -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/EnemyManager.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyRotation.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/EnemyRotation.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyRotation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "IEnemy.h" 3 | 4 | class EnemyRotation : public IEnemy { 5 | public: 6 | static const size_t EnemyshotRate; 7 | static const size_t EnemyshotNum; 8 | static const double EnemyBulletSpeed; 9 | static const double EnemyOmega; 10 | static const double Radius; 11 | static const double Hp; 12 | static const int Score; 13 | static const unsigned int Color; 14 | 15 | EnemyRotation(double _x, double _y); 16 | ~EnemyRotation() = default; 17 | void move() override; 18 | void shot() override; 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyStalker.cpp: -------------------------------------------------------------------------------- 1 | #include "MyGlobal.h" 2 | #include "GameManager.h" 3 | #include "EnemyStalker.h" 4 | 5 | const double EnemyStalker::StalkerEnemySpeed = 1.5; 6 | const double EnemyStalker::Radius = 24.0; 7 | const double EnemyStalker::Hp = 5.0; 8 | const int EnemyStalker::Score = 100; 9 | const unsigned int EnemyStalker::Color = 0xFF0000; 10 | 11 | EnemyStalker::EnemyStalker(double _x, double _y) : 12 | IEnemy(_x, _y, 0.0, 0.0, Radius, Hp, Score, Color) 13 | { 14 | } 15 | 16 | void EnemyStalker::move() { 17 | Player& player = gameManager.player; 18 | normalize(x, y, player.x, player.y, &vx, &vy); 19 | vx *= StalkerEnemySpeed; 20 | vy *= StalkerEnemySpeed; 21 | x += vx; 22 | y += vy; 23 | } 24 | 25 | void EnemyStalker::shot() { 26 | } 27 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyStalker.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "IEnemy.h" 3 | 4 | class EnemyStalker : public IEnemy { 5 | public: 6 | static const double StalkerEnemySpeed; 7 | static const double Radius; 8 | static const double Hp; 9 | static const int Score; 10 | static const unsigned int Color; 11 | 12 | EnemyStalker(double _x, double _y); 13 | ~EnemyStalker() = default; 14 | void move() override; 15 | void shot() override; 16 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyStop.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/EnemyStop.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/EnemyStop.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "IEnemy.h" 3 | 4 | class EnemyStop : public IEnemy { 5 | public: 6 | static const size_t EnemyshotRate; 7 | static const double BulletSpeed; 8 | static const double Radius; 9 | static const double Hp; 10 | static const int Score; 11 | static const unsigned int Color; 12 | 13 | EnemyStop(double _x, double _y); 14 | ~EnemyStop() = default; 15 | void move() override; 16 | void shot() override; 17 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/GameManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/GameManager.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/GameManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Player.h" 3 | #include "Player.h" 4 | #include "PlayerBulletManager.h" 5 | #include "EnemyManager.h" 6 | #include "EnemyBulletManager.h" 7 | #include "EffectManager.h" 8 | #include "ScoreManager.h" 9 | 10 | class GameManager { 11 | public: 12 | Player player; 13 | PlayerBulletManager playerBulletManager; 14 | EnemyManager enemyManager; 15 | EnemyBulletManager enemyBulletManager; 16 | EffectManager effectManager; 17 | ScoreManager scoreManager; 18 | int fontHandle; 19 | 20 | GameManager(); 21 | void update(); 22 | void draw() const; 23 | void load(); 24 | }; 25 | 26 | extern GameManager gameManager; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/IBullet.cpp: -------------------------------------------------------------------------------- 1 | #include "DxLib.h" 2 | #include "IBullet.h" 3 | 4 | 5 | IBullet::IBullet(double _x, double _y, double _vx, double _vy, double _radius, double _attack, unsigned int _color, int _lifeTime) : 6 | x(_x), y(_y), 7 | vx(_vx), vy(_vy), 8 | radius(_radius), 9 | attack(_attack), 10 | color(_color), 11 | lifeTime(_lifeTime), 12 | elapsedFrame(0), 13 | isDead(false) 14 | { 15 | } 16 | 17 | void IBullet::update() { 18 | checkDead(); 19 | move(); 20 | elapsedFrame++; 21 | } 22 | 23 | void IBullet::move() { 24 | x += vx; y+= vy; 25 | } 26 | 27 | void IBullet::checkDead() { 28 | if (elapsedFrame > lifeTime) { 29 | isDead = true; 30 | } 31 | } 32 | 33 | void IBullet::draw() const { 34 | DrawCircle(x, y, radius, color); 35 | } 36 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/IBullet.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/IBullet.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/IEffect.cpp: -------------------------------------------------------------------------------- 1 | #include "IEffect.h" 2 | 3 | 4 | IEffect::IEffect(double _x, double _y, int _lifeTime) : 5 | x(_x), y(_y), 6 | lifeTime(_lifeTime), 7 | elapsedFrame(0), 8 | isDead(false) 9 | { 10 | } 11 | 12 | void IEffect::checkDead() { 13 | if (elapsedFrame > lifeTime) { 14 | isDead = true; 15 | } 16 | } 17 | 18 | void IEffect::update() { 19 | checkDead(); 20 | elapsedFrame++; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/IEffect.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/IEffect.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/IEnemy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/IEnemy.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/IEnemy.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/IEnemy.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/MyGlobal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/MyGlobal.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/MyGlobal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/MyGlobal.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/Player.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/Player.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/Player.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Player { 4 | public: 5 | static const double Radius; 6 | static const size_t FireRate; 7 | static const double MoveSpeed; 8 | static const double BulletFirstVelocityX; 9 | static const double BulletFirstVelocityY; 10 | static const double BulletFirstDeltaX; 11 | static const double BulletFirstDeltaY; 12 | static const size_t EffectNum; 13 | static const double EffectRange; 14 | static const unsigned int Color; 15 | static const double FirstPosX; 16 | static const double FirstPosY; 17 | 18 | double x, y; 19 | int fireCool; 20 | 21 | Player(); 22 | void update(); 23 | void draw() const; 24 | void move(); 25 | void shot(); 26 | void checkHit(); 27 | }; 28 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/PlayerBullet.cpp: -------------------------------------------------------------------------------- 1 | #include "PlayerBullet.h" 2 | 3 | const double PlayerBullet::Radius = 4.0; 4 | const double PlayerBullet::Attack = 1.0; 5 | const unsigned int PlayerBullet::Color = 0x0000AA; 6 | const int PlayerBullet::LifeTime = 60; 7 | 8 | 9 | PlayerBullet::PlayerBullet(double _x, double _y, double _vx, double _vy) : 10 | IBullet(_x, _y, _vx, _vy, Radius, Attack, Color, LifeTime) 11 | { 12 | } 13 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/PlayerBullet.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/PlayerBullet.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/PlayerBulletManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/PlayerBulletManager.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/PlayerBulletManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/PlayerBulletManager.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/ScoreManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/ScoreManager.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/ScoreManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "DxLib.h" 4 | 5 | class ScoreManager { 6 | private: 7 | int score; 8 | int damagedNum; 9 | public: 10 | ScoreManager(); 11 | void addScore(int n); 12 | int getScore(); 13 | void addDamagedNum(); 14 | int getDamagedNum(); 15 | void update(); 16 | void draw() const; 17 | }; -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/Vol_11_FinalTask.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | ソース ファイル 20 | 21 | 22 | ソース ファイル 23 | 24 | 25 | ソース ファイル 26 | 27 | 28 | ソース ファイル 29 | 30 | 31 | ソース ファイル 32 | 33 | 34 | ソース ファイル 35 | 36 | 37 | ソース ファイル 38 | 39 | 40 | ソース ファイル 41 | 42 | 43 | ソース ファイル 44 | 45 | 46 | ソース ファイル 47 | 48 | 49 | ソース ファイル 50 | 51 | 52 | ソース ファイル 53 | 54 | 55 | ソース ファイル 56 | 57 | 58 | ソース ファイル 59 | 60 | 61 | ソース ファイル 62 | 63 | 64 | ソース ファイル 65 | 66 | 67 | ソース ファイル 68 | 69 | 70 | ソース ファイル 71 | 72 | 73 | 74 | 75 | ヘッダー ファイル 76 | 77 | 78 | ヘッダー ファイル 79 | 80 | 81 | ヘッダー ファイル 82 | 83 | 84 | ヘッダー ファイル 85 | 86 | 87 | ヘッダー ファイル 88 | 89 | 90 | ヘッダー ファイル 91 | 92 | 93 | ヘッダー ファイル 94 | 95 | 96 | ヘッダー ファイル 97 | 98 | 99 | ヘッダー ファイル 100 | 101 | 102 | ヘッダー ファイル 103 | 104 | 105 | ヘッダー ファイル 106 | 107 | 108 | ヘッダー ファイル 109 | 110 | 111 | ヘッダー ファイル 112 | 113 | 114 | ヘッダー ファイル 115 | 116 | 117 | ヘッダー ファイル 118 | 119 | 120 | ヘッダー ファイル 121 | 122 | 123 | ヘッダー ファイル 124 | 125 | 126 | -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/WhiteCircleEffect.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/WhiteCircleEffect.cpp -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/WhiteCircleEffect.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/WhiteCircleEffect.h -------------------------------------------------------------------------------- /DxLibAnswerExample/Vol_11_FinalTask/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibAnswerExample/Vol_11_FinalTask/main.cpp -------------------------------------------------------------------------------- /DxLibTemplate2017.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yagiri000/CppDxLibLecture/8257da4a2684b18088844339f7435bc65aea6a10/DxLibTemplate2017.zip -------------------------------------------------------------------------------- /vol_00_はじめに.md: -------------------------------------------------------------------------------- 1 | # DXライブラリで学ぶC++ 2 | 3 | ## はじめに 4 | 5 | 本資料は、やぎりが作成した、C++初学者向け資料です。 6 | C++とDXライブラリで実際にシューティングゲームを作り、「C++の機能(クラス, vector, 継承)をゲームを製作に実際どう使うか」を理解することが目的になっています。 7 | 8 | ## 開発環境 9 | 10 | 開発環境はWindows, Visual Studio 2017、DXライブラリのバージョンはDxLib 3.18eを想定しています。 11 | 12 | ## 謝辞 13 | 14 | 本資料の一部は、[いかろちゃんのサークル用C++講座資料](https://turarasoft.com/main/ccs/index.html)を参考に作りました。ありがとうございました。 15 | 16 | ## 演習問題について 17 | 18 | DxLib.hをインクルードしているプログラムはDXライブラリを使ったアプリケーションを、そうでないプログラムはコンソールアプリケーション(文字だけ出てくる黒画面のやつ)で解くことを想定しています。 19 | 演習問題(コンソール)の解答は、「vol_12_演習問題(コンソール)」の解答の解答に書いてあります。 20 | 演習問題(DxLib)の解答例はDxLibAnsweExampleフォルダ内に入っています。 21 | 22 | ## ファイルについて 23 | 24 | * DxLibAnsweExampleフォルダ 25 | 各回の演習問題(DxLib)の解答例です。 26 | 27 | * SampleGame.exe 28 | 前半課題、最終課題ではこれを作ります。 29 | 30 | * DxLibTemplate2017.zip 31 | DxLibの初期設定済みのVisual Studio2017プロジェクトです。これを改造して演習問題を解くことをおすすめします。 32 | 33 | ## 今回やること 34 | 35 | 今回はコンソールの問題を解く際の豆知識や、本資料でよく使うDxLibの関数についての解説を行います。 36 | 37 | ## \#if~\#endif 38 | 39 | \#ifと\#endifで囲んだ部分は、\#ifの右に来る条件式がtrue(=0以外)の場合はコンパイルされ、false(=0)の場合はコンパイルされない。 40 | コメントアウトのように使うことができる。 41 | 講座中の演習問題(コンソール)では、一問解くたびに新しいプロジェクトを作るのは手間がかかるので、\#if-\#endifを使って一つのプロジェクト内に複数の演習問題の解答のコードを作っていくとよい。 42 | 43 | ```cpp 44 | #if 1 45 | 46 | #include 47 | 48 | int main(){ 49 | std::cout << "aaa" << std::endl; 50 | return 0; 51 | } 52 | 53 | #endif 54 | 55 | #if 0 56 | 57 | #include 58 | 59 | int main(){ 60 | std::cout << "bbb" << std::endl; 61 | return 0; 62 | } 63 | 64 | #endif 65 | 66 | #if 0 67 | 68 | #include 69 | 70 | int main(){ 71 | std::cout << "ccc" << std::endl; 72 | return 0; 73 | } 74 | 75 | #endif 76 | ``` 77 | 78 | 79 | ## DXライブラリコード例 80 | 81 | この講座では演習にDxLibを使う。以下ではDxLibの基本的なコードや、私が作ったDxLibのひな形のコードについて説明している。 82 | 画面中央に円を描画するだけの基本的なコード。 83 | 84 | 85 | ```cpp 86 | #include 87 | #include 88 | 89 | 90 | int WINAPI WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow ) 91 | { 92 | SetOutApplicationLogValidFlag( FALSE ) ;//Log.txtを生成しないように設定 93 | ChangeWindowMode( TRUE );//非全画面にセット 94 | SetGraphMode( 640 , 480 , 32 );//画面サイズ指定 95 | if(DxLib_Init() == 1){return -1;}//初期化に失敗時にエラーを吐かせて終了 96 | SetDrawScreen( DX_SCREEN_BACK ) ;//描画先を裏画面に 97 | 98 | // 99 | //ここで敵やプレイヤーのオブジェクトの実体を作る 100 | // 101 | 102 | while( ProcessMessage()==0 ) 103 | { 104 | ClearDrawScreen();//裏画面消す 105 | 106 | // 107 | // ここに敵やプレイヤーを更新、描画するコードを書く 108 | // 109 | 110 | DrawCircle(400, 320, 100, GetColor(255, 255, 255), 1); 111 | 112 | ScreenFlip();//裏画面を表画面にコピー 113 | } 114 | 115 | DxLib_End(); 116 | return 0; 117 | } 118 | ``` 119 | 120 | 121 | 122 | ## DXライブラリコード例 その2 123 | 124 | 下の例では、キーの状態を記録する配列とそれを更新する関数を用意して、フォントを指定して文字を描画(DrawFormatStringToHandle)している。 125 | 変数にexternをつけて宣言することで複数のcppやヘッダーからその変数にアクセスできるようになる。 126 | 127 | > MyGlobal.h 128 | 129 | ```cpp 130 | #pragma once 131 | #include "DxLib.h" 132 | 133 | extern const int SCREEN_WIDTH; 134 | extern const int SCREEN_HEIGHT; 135 | 136 | extern int fontHandle; 137 | extern int mouseX , mouseY;//マウス座標 138 | 139 | //キー取得マウス付き 140 | extern char buf[256]; 141 | extern int keyState[256]; 142 | 143 | //キー入力状態を更新する関数 144 | void keyUpdate(); 145 | ``` 146 | 147 | 148 | > MyGlobal.cpp 149 | 150 | ```cpp 151 | #pragma once 152 | 153 | #include "MyGlobal.h" 154 | 155 | const int SCREEN_WIDTH = 640; 156 | const int SCREEN_HEIGHT = 480; 157 | 158 | int fontHandle;//フォント読み込み用変数 159 | int mouseX=0 , mouseY=0;//マウス座標 160 | 161 | //キー取得用の配列 162 | char buf[256] = {0}; 163 | int keyState[256] = {0}; 164 | 165 | //キー入力状態を更新する関数 166 | void keyUpdate() 167 | { 168 | GetHitKeyStateAll(buf); 169 | for(int i = 0; i< 256; i++) 170 | { 171 | if(buf[i] == 1) keyState[i]++; 172 | else keyState[i] = 0; 173 | } 174 | } 175 | ``` 176 | 177 | 178 | > main.cpp 179 | 180 | ```cpp 181 | #include 182 | #include 183 | 184 | #include "MyGlobal.h" 185 | 186 | 187 | int WINAPI WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow ) 188 | { 189 | SetOutApplicationLogValidFlag( FALSE ) ;//Log.txtを生成しないように設定 190 | ChangeWindowMode( TRUE );//非全画面にセット 191 | SetGraphMode( SCREEN_WIDTH , SCREEN_HEIGHT , 32 );//画面サイズ指定 192 | if(DxLib_Init() == 1){return -1;}//初期化に失敗時にエラーを吐かせて終了 193 | SetDrawScreen( DX_SCREEN_BACK ) ;//描画先を裏画面に 194 | 195 | 196 | fontHandle = CreateFontToHandle( "Segoe UI" , 20 , 5 ,DX_FONTTYPE_ANTIALIASING_4X4) ;//フォントを読み込み 197 | 198 | // 199 | //ここで敵やプレイヤーのオブジェクトの実体を作る 200 | // 201 | 202 | while( ProcessMessage()==0 ) 203 | { 204 | ClearDrawScreen();//裏画面消す 205 | 206 | GetMousePoint( &mouseX, &mouseY ); //マウス座標更新 207 | keyUpdate();//(自作関数)キー更新 208 | 209 | //説明用・文字を描画:keyState配列(自作)には、各キーが何フレーム押され続けているかが入っている 210 | DrawFormatStringToHandle(320, 320, GetColor(255, 255, 255), fontHandle , "Z KEY %d", keyState[KEY_INPUT_Z]) ; 211 | 212 | // 213 | // ここに敵やプレイヤーのupdate,drawを書く 214 | // 215 | 216 | //左上に文字(マウスの座標)を描画 217 | DrawFormatStringToHandle(20, 20, GetColor(255, 255, 255), fontHandle , "MX:%3d MY:%3d", mouseX,mouseY) ; 218 | 219 | ScreenFlip();//裏画面を表画面にコピー 220 | } 221 | 222 | DxLib_End(); 223 | return 0; 224 | } 225 | ``` 226 | 227 | 228 | 229 | ## キーが何フレーム押されているかを取得 230 | 231 | GetHitKeyStateAll関数は引数にとった配列に各キーの状態(押されていれば1, そうでなければ0)を入れる。 232 | 下のような関数を自分で作れば、どのキーが何フレーム押されているかを配列に入れることができる。(この関数は毎フレーム呼ばなければいけない) 233 | 234 | ```cpp 235 | //キー取得用の配列 236 | char buf[256] = {0}; 237 | int keyState[256] = {0}; 238 | 239 | //キー入力状態を更新する関数 240 | void keyUpdate() 241 | { 242 | GetHitKeyStateAll(buf); 243 | for(int i = 0; i< 256; i++) 244 | { 245 | if(buf[i] == 1) keyState[i]++; 246 | else keyState[i] = 0; 247 | } 248 | } 249 | ``` 250 | 251 | ## フォントを指定して文字を描画 252 | 253 | DrawFormatStringToHandle関数を使えば、座標、色、フォントを指定して文字を描画できる。printfと同じように%dなどが使える。 254 | 255 | ```cpp 256 | DrawFormatStringToHandle(20, 20, GetColor(255, 255, 255), fontHandle , "MX:%3d MY:%3d", mouseX,mouseY) ; 257 | ``` 258 | 259 | ## 演習問題 260 | 261 | 1. double型の変数x, yを用意し、DrawCircleを用いてx,yに半径20pxの赤い円を描画せよ。 262 | 263 | 1. 矢印キーを押したとき上記で描画した円が動くようにせよ。(xやyに数字を足せばよい) 264 | 265 | 1. Zキーを押したフレームにx座標にGetRand(100)を加算せよ。 266 | ヒント:GetRandはDXライブラリの関数で、乱数を返す。GetRand(100)で0~100の数のうちどれかが返ってくる。(0~99でないことに注意!) -------------------------------------------------------------------------------- /vol_01_クラスの基本.md: -------------------------------------------------------------------------------- 1 | # DXライブラリで学ぶC++ 2 | 3 | 今回はC++でのHello World、クラスの基本的な使い方について学ぶ。 4 | 演習では、実際にDxLibを用いてPlayer,Enemyを生成し、動かす。 5 | 6 | ## Hello World! 7 | 8 | C++流のHello World! 9 | C++ではstd::coutが用意されており、それを使って出力ができる。 10 | std:: とは、「stdという名前空間にある」という意味。 11 | 12 | ```cpp 13 | #include 14 | 15 | int main(){ 16 | 17 | std::cout << "Hello World!!" << std::endl; 18 | 19 | return 0; 20 | } 21 | ``` 22 | 23 | ## いろいろ出力 24 | 25 | Cのprintf関数では%dや%sなどを使って型を指定する必要があったが、std::coutでは型を指定しなくても自動的に型を判定・表示してくれる。 26 | 27 | ```cpp 28 | #include 29 | 30 | int main(){ 31 | 32 | int a = 12; 33 | double b = 3.14; 34 | char str[100] = "abcde"; 35 | 36 | std::cout << a << " " << b << " " << str << std::endl; 37 | 38 | return 0; 39 | } 40 | ``` 41 | 42 | ## 入力 43 | 44 | std::cinを使うと入力されたデータを変数に入れられる。 45 | 46 | ```cpp 47 | #include 48 | 49 | int main(){ 50 | 51 | int input; 52 | std::cout << "年齢を入力してください" << std::endl; 53 | std::cin >> input;//inputに入力された数値を代入 54 | 55 | std::cout << "あなたは"<< input << "歳なんですね。" << std::endl; 56 | 57 | return 0; 58 | } 59 | ``` 60 | 61 | ## クラスの基本的な書き方 62 | 63 | クラスは以下のように定義でき、インスタンス(実体)化して使う。 64 | クラスの要素には```.```(コンマ)を使ってアクセスできる。 65 | クラスは変数を要素に持つことができ、メンバ変数という。 66 | 67 | ```cpp 68 | #include 69 | 70 | class MyClass{ 71 | public: 72 | int a; 73 | int b; 74 | }; 75 | 76 | int main(){ 77 | 78 | MyClass obj; // インスタンス化 79 | obj.a = 10; 80 | obj.b = 20; 81 | std::cout << obj.a << "," << obj.b << std::endl; 82 | 83 | return 0; 84 | } 85 | ``` 86 | 87 | 二次元上の座標や、2次元ベクトルを表すクラス、Vector2を作った。 88 | クラスの要素には```.```(コンマ)を使ってアクセスできる。 89 | クラスは関数を要素に持つことができ、メンバ関数という。(下の例ではsetData関数がメンバ関数) 90 | 91 | ```cpp 92 | #include 93 | 94 | class Vector2{ 95 | public: 96 | int x; 97 | int y; 98 | void setData(int _x, int _y){ 99 | x = _x; 100 | y = _y; 101 | } 102 | }; 103 | 104 | int main(){ 105 | 106 | Vector2 obj; 107 | obj.setData(13, 29); 108 | std::cout << obj.x << " " << obj.y << std::endl; 109 | 110 | return 0; 111 | } 112 | ``` 113 | 114 | ## コンストラクタ 115 | 116 | さっきの例ではVector2クラスを作った後、setData関数をいちいち呼ばなければならない。 117 | 初期化をいちいち行うのは手間がかかるので、C++ではクラス生成時に呼び出されるコンストラクタというものを定義できる。 118 | 119 | > コンストラクタの書き方 120 | 121 | ```cpp 122 | クラスの名前(引数){ 123 | 初期化処理... 124 | } 125 | ``` 126 | 127 | > Vector2クラスのコンストラクタ例 128 | 129 | ```cpp 130 | #include 131 | 132 | class Vector2{ 133 | public: 134 | int x; 135 | int y; 136 | 137 | Vector2(int _x, int _y){ 138 | x = _x; 139 | y = _y; 140 | } 141 | }; 142 | 143 | int main(){ 144 | 145 | Vector2 obj(1, 2); 146 | std::cout << obj.x << " " << obj.y << std::endl; 147 | 148 | return 0; 149 | } 150 | ``` 151 | 152 | 153 | ## デストラクタ 154 | 155 | オブジェクトが破棄されるときにデストラクタが呼ばれる。オブジェクトを破棄する時に実行したい処理をここに書く。(ex. リソース解放) 156 | 157 | > デストラクタの書き方。~(チルダ)をつける 158 | 159 | ```cpp 160 | ~クラスの名前(){ 161 | 処理... 162 | } 163 | ``` 164 | 165 | > メイン関数を抜ける時オブジェクトが破棄されるのでデストラクタが呼ばれる。 166 | 167 | ```cpp 168 | #include 169 | 170 | class MyClass{ 171 | public: 172 | ~MyClass(){ 173 | std::cout << "デストラクタが呼ばれました" << std::endl; 174 | } 175 | }; 176 | 177 | int main(){ 178 | std::cout << "メイン関数に入りました" << std::endl; 179 | 180 | MyClass obj; 181 | 182 | std::cout << "メイン関数を抜けました" << std::endl; 183 | 184 | return 0; 185 | } 186 | ``` 187 | 188 | 189 | ## アクセス修飾子 190 | 191 | publicやprivateをアクセス修飾子と言う。 publicの要素はクラス外からアクセスでき(公開)、 privateの要素はクラス外からアクセスできない(非公開)。 何も指定されていなければprivateメンバになる。 192 | privateメンバ変数を用いることで、外から直接書き換えられたくないメンバ変数を書き換えることが出来ないようにできる。 193 | privateは隠蔽、カプセル化などにおいて重要な機能だが、この講座では割愛する(基本的に全てpublicを使い、privateはあまり使用しない)。 194 | 195 | ```cpp 196 | #include 197 | 198 | class MyClass 199 | { 200 | int a; 201 | public: 202 | int b; 203 | private: 204 | int c; 205 | }; 206 | 207 | int main(){ 208 | 209 | MyClass obj; 210 | 211 | //コメントを外すとコンパイルエラー 212 | 213 | //obj.a = 100; 214 | //obj.c = 100; 215 | 216 | obj.b = 100; 217 | 218 | return 0; 219 | } 220 | ``` 221 | 222 | > 以下のようにすると、クラスの外から値を参照できるが、書き換えることは出来ないようにできる。 223 | 224 | ```cpp 225 | #include 226 | 227 | class MyClass 228 | { 229 | private: 230 | int data; 231 | public: 232 | MyClass(){ 233 | data = 3; 234 | } 235 | int getData(){ 236 | return data; 237 | } 238 | }; 239 | 240 | int main(){ 241 | 242 | MyClass obj; 243 | 244 | // コメントを外すとコンパイルエラー 245 | // obj.data = 100; 246 | 247 | std::cout << obj.getData() << std::endl; 248 | 249 | return 0; 250 | } 251 | ``` 252 | 253 | ## const変数 254 | 255 | constをつけると値を変更不可能な変数を作ることができる。#defineの代わりに使って、定数を設定する時などに使える。 256 | 257 | ```cpp 258 | #include 259 | 260 | int main(){ 261 | 262 | const int a = 100; 263 | 264 | //コメントを外すとコンパイルエラー 265 | //a = 200; 266 | 267 | std::cout << "aの値は:" << a << std::endl; 268 | 269 | return 0; 270 | } 271 | ``` 272 | 273 | ## 演習問題(コンソール) 274 | 275 | 1. 以下のプログラムをstd::coutとstd::cinを使って書き直せ。 276 | 277 | #include 278 | 279 | int main() { 280 | int input; //入力用 281 | 282 | printf("整数値を入力してください:"); 283 | scanf("%d", &input); 284 | 285 | printf("入力された値は、%dです。\n", input); 286 | 287 | return 0; 288 | } 289 | 1. 以下の様なクラスVector3を作った。 290 | 適当なコンストラクタを作れ。 291 | x,y,zの値を表示するメンバ関数showを作り、動作を確認せよ。 292 | x\*y\*zの値を表示するメンバ関数showMultipleを作り、動作を確認せよ。 293 | 294 | class Vector3{ 295 | public: 296 | int x, y, z; 297 | }; 298 | 299 | 300 | ## 演習問題(DxLib) 301 | 302 | 1. 以下の様なPlayerクラスを作り、矢印キーで動くようにせよ。 303 | 304 | class Player{ 305 | public: 306 | double x, y, speed; 307 | 308 | Player(){ 309 | x = 320.0; 310 | y = 240.0; 311 | speed = 5.0; 312 | } 313 | void update(){ 314 | //ここを実装 315 | //矢印キーで移動 316 | } 317 | //自機(円)を描画 318 | void draw(){ 319 | DrawCircle(x, y, 30.0, 0x0000FF, 1); 320 | } 321 | }; 322 | 323 | 1. Playerクラスの実体を作り、メインループ内でupdate関数とdraw関数を呼び出してPlayerクラスを動作させよ。(ヒント:while内でプレイヤーのインスタンスを作成すると、毎フレーム破棄されてしまう) 324 | 325 | 1. 以下の様なEnemyクラスを作り、Playerクラスと同様に実体を作り動作を確認せよ。 326 | 今回作るEnemyはただ下に移動するだけでよい。 327 | 328 | class Enemy{ 329 | public: 330 | double x, y; 331 | 332 | //ここに座標を指定できるようなコンストラクタを実装 333 | 334 | void update(){ 335 | //ここを実装 336 | //下方向に移動 337 | } 338 | //敵(円)を描画 339 | void draw(){ 340 | DrawCircle(x, y, 30.0, 0xFF0000, 1); 341 | } 342 | }; 343 | 344 | -------------------------------------------------------------------------------- /vol_02_ファイル分け・コンポジション.md: -------------------------------------------------------------------------------- 1 | # DXライブラリで学ぶC++ 2 | 3 | 実際にゲームを作る時、ファイル分けをすることが必要になる。 4 | 今回はクラスの内容を.hと.cppに分ける。 5 | 6 | ## ファイル分け 7 | 8 | 以下のように、.hと.cppにクラスの定義を分けて書くことが出来る。今後は基本的に.hと.cppにクラスの定義を分けて書くこと。(ただし、解答例ではファイル数が多くなるのを避けるためすべてMain.cppに書いている場合がある) 9 | .cppには、名前空間を指定するため、クラス名::関数名と書く必要がある。以下は、コンソールでのプレイヤー例。 10 | > Player.h 11 | 12 | ```cpp 13 | #include 14 | 15 | class Player{ 16 | public: 17 | int x, y; 18 | Player(int _x, int _y); 19 | void update(); 20 | void draw(); 21 | void showXY(); 22 | }; 23 | ``` 24 | 25 | > Player.cpp 26 | 27 | ```cpp 28 | #include "Player.h" 29 | 30 | Player::Player(int _x, int _y) : 31 | x(_x), 32 | y(_y) 33 | { 34 | } 35 | 36 | void Player::update(){ 37 | std::cout << "Playerのupdateが呼ばれました。" << std::endl; 38 | } 39 | 40 | void Player::draw(){ 41 | std::cout << "Playerのdrawが呼ばれました。" << std::endl; 42 | } 43 | 44 | void Player::showXY(){ 45 | std::cout << "PlayerのshowXYが呼ばれました。 x:" << x << " y:" << y << std::endl; 46 | } 47 | ``` 48 | 49 | > main.cpp 50 | 51 | ```cpp 52 | #include 53 | #include "Player.h" 54 | 55 | int main(){ 56 | 57 | Player player(20, 30); 58 | player.update(); 59 | player.draw(); 60 | player.showXY(); 61 | 62 | return 0; 63 | } 64 | ``` 65 | 66 | 67 | ## Tips : ショートカット 68 | 69 | 複数のファイルに分けると関数やクラスの定義を見に行くのに時間がかかるが、Visual Studioでは、関数、クラスにカーソルを合わせた状態で```F12```を押すと定義に移動できる。もとのカーソル位置に戻りたい場合は、```Ctrl + -```でもとの位置に戻ることができる。```Ctrl + ,```でクラス、関数を検索して移動できる。他にも様々なショートカットがあるので、インターネットで調べるか、コード上で右クリックして出るダイアログを見て調べてみよう。 70 | 71 | 72 | ## メンバイニシャライザ 73 | 74 | コンストラクタの数値の初期化は以下のようにも書くことができる。自分で定義したクラスをなど初期化する場合、こう書いたほうが、インスタンスのコピーが発生しなくなるため処理が早くなる。以後はこの書き方、メンバイニシャライザを使って初期化を行う。 75 | 76 | ```cpp 77 | #include 78 | 79 | class Vector2{ 80 | public: 81 | int x; 82 | int y; 83 | 84 | Vector2(int _x, int _y): 85 | x(_x), 86 | y(_y) 87 | { 88 | } 89 | }; 90 | 91 | int main(){ 92 | 93 | Vector2 obj(1, 2); 94 | std::cout << obj.x << " " << obj.y << std::endl; 95 | 96 | return 0; 97 | } 98 | ``` 99 | 100 | 101 | ## コンポジション 102 | 103 | クラスをクラスのメンバにすることが出来る。これをコンポジションと呼ぶ。 104 | 以下の例では、Vector2クラスをまず定義し、PlayerクラスでVector2クラスのメンバを持っている。 105 | 106 | ```cpp 107 | #include 108 | 109 | class Vector2{ 110 | public: 111 | int x, y; 112 | Vector2(int _x, int _y) : 113 | x(_x), 114 | y(_y) 115 | { 116 | } 117 | }; 118 | 119 | class Player{ 120 | public: 121 | Vector2 pos; 122 | int hp; 123 | 124 | Player(int _x, int _y, int _hp) : 125 | pos(_x, _y), 126 | hp(_hp) 127 | { 128 | } 129 | }; 130 | 131 | 132 | int main(){ 133 | Player player(100, 200, 64); 134 | 135 | std::cout << "x:" << player.pos.x << ", y:" << player.pos.y << std::endl; 136 | std::cout << "hp:" << player.hp << std::endl; 137 | 138 | return 0; 139 | } 140 | 141 | ``` 142 | 143 | コンポジションをしている場合、コンストラクタは以下のように書く。引数を受け渡すと考えれば良い。 144 | ClassAがClassBを持つ場合 145 | 146 | ```cpp 147 | ClassA(引数1,引数2,引数3...) : 148 | ClassB(引数1,引数2,引数3...) 149 | { 150 | } 151 | ``` 152 | 153 | クラスをコンポジションした場合、クラスに含まれているクラスのコンストラクタが先に呼ばれる。デストラクタは逆の順で呼ばれる。デストラクタは、コンストラクタがクラス生成時に呼び出される処理であるように、オブジェクトが破棄される時に呼び出される処理を指す。 154 | HogeやPiyoは日本では、「特に意味のない名前」を表す。 155 | 156 | ```cpp 157 | #include 158 | 159 | class Hoge{ 160 | public: 161 | int x; 162 | 163 | Hoge(int _x): 164 | x(_x) 165 | { 166 | std::cout << "Hogeのコンストラクタが呼ばれました" << std::endl; 167 | } 168 | 169 | ~Hoge(){ 170 | std::cout << "Hogeのデストラクタが呼ばれました" << std::endl; 171 | } 172 | }; 173 | 174 | class Piyo{ 175 | public: 176 | Hoge Hoge_; 177 | int y; 178 | 179 | Piyo(int _x, int _y): 180 | Hoge_(_x), 181 | y(_y) 182 | { 183 | std::cout << "Piyoのコンストラクタが呼ばれました" << std::endl; 184 | } 185 | 186 | ~Piyo(){ 187 | std::cout << "Piyoのデストラクタが呼ばれました" << std::endl; 188 | } 189 | }; 190 | 191 | 192 | int main(){ 193 | 194 | Piyo piyo(100, 200); 195 | 196 | std::cout << piyo.Hoge_.x << " " << piyo.y << std::endl; 197 | 198 | return 0; 199 | } 200 | ``` 201 | 202 | 203 | ## インクルード 204 | 205 | \#includeを使うと自分が定義したヘッダーをインクルードできる。インクルードすると、その部分にヘッダーの中身のコードが展開される。 206 | 207 | ## \#pragma once 208 | 209 | Visual Studioのコンパイラでは、```#pragma once```を使うと一度読み込まれたヘッダーは読み込まれないようになる。複数のファイルから読み込まれるヘッダーの一番上につけることで、多重定義を防ぐことが出来る。 210 | 以下の例では、PlayerクラスとEnemyクラスで共通で使うVector2クラスを作り、それをどちらのファイルからも読み込むコンソールプログラムの例である。よって、Vector2クラスの```#pragma once```を外すと、コンパイルエラーが起きる。 211 | 212 | > Vector2.h 213 | 214 | ```cpp 215 | #pragma once 216 | #include 217 | 218 | class Vector2{ 219 | public: 220 | int x, y; 221 | Vector2(int _x, int _y) : 222 | x(_x), 223 | y(_y) 224 | { 225 | } 226 | }; 227 | ``` 228 | 229 | > Enemy.h 230 | 231 | ```cpp 232 | #include 233 | #include "Vector2.h" 234 | 235 | class Enemy{ 236 | public: 237 | Vector2 pos; 238 | 239 | Enemy(int x, int y) : 240 | pos(x, y) 241 | { 242 | } 243 | }; 244 | ``` 245 | 246 | > Player.h 247 | 248 | ```cpp 249 | #include 250 | #include "Vector2.h" 251 | 252 | class Player{ 253 | public: 254 | Vector2 pos; 255 | 256 | Player(int x, int y) : 257 | pos(x, y) 258 | { 259 | } 260 | }; 261 | ``` 262 | 263 | > main関数内 264 | 265 | ```cpp 266 | #include 267 | #include "Player.h" 268 | #include "Enemy.h" 269 | 270 | int main(){ 271 | 272 | Player player(100, 200); 273 | Enemy enemy(320, 400); 274 | 275 | std::cout << player.pos.x << ", " << player.pos.y << std::endl; 276 | std::cout << enemy.pos.x << ", " << enemy.pos.y << std::endl; 277 | 278 | return 0; 279 | } 280 | ``` 281 | 282 | 283 | 284 | ## まとめ 285 | 286 | * クラスが他のクラスを実体としてメンバとして持つ、または他のクラスのメンバにアクセスするときは、他のクラスの定義がクラスの前方になければならない。 287 | * 実際にゲームを作るときは、とりあえず#pragma onceをつけよう。#pragma onceは特に害はない。 288 | 289 | 290 | 291 | ## 演習問題(DxLib) 292 | 293 | 1. 前回DxLibで作ったプロジェクト上で、Player.h, Player.cpp, Enemy.h, Enemy.cppを作り、PlayerクラスとEnemyクラスをファイル分けせよ。 294 | 295 | 296 | 297 | 298 | ## Tips : クラス内定数 299 | 300 | 以下のようにすると、クラス内に定数を用意することができる。以下の例ではMyClassにint型のHoge定数、を用意している。これらは、アクセス属性がpublicなので、外からもアクセスできるが、その場合は```Myclass::Hoge```のようにアクセスする。MyClass.cppで```const int MyClass::Hoge = 3;```のように書いているが、関数やコンストラクタ同様に、クラスの名前空間を```MyClass::```のように指定し忘れないように注意!また、MyClass.cppの数値を定義する部分では、staticは必要ない。 301 | 302 | > MyClass.h 303 | 304 | ```cpp 305 | class MyClass { 306 | public: 307 | static const int Hoge; 308 | void func(); 309 | }; 310 | ``` 311 | 312 | > MyClass.cpp 313 | 314 | ```cpp 315 | const int MyClass::Hoge = 3; 316 | 317 | void MyClass::func(){ 318 | std::cout << "aaa" << std::endl; 319 | } 320 | ``` -------------------------------------------------------------------------------- /vol_03_vectorの基本.md: -------------------------------------------------------------------------------- 1 | # DXライブラリで学ぶC++ 2 | 3 | 今回は動的配列vectorの基本について学ぶ。 4 | 演習では、DxLibを用いて複数の敵を生成する。 5 | 6 | 7 | ## vectorの基本的な使い方 8 | 9 | C言語の配列は固定長だったが、C++には必要に応じて自動的にサイズを変更してくれる配列(のようなもの)、vectorがある。 10 | vectorの要素には配列と同じように[]でアクセス出来る。 11 | vectorを使うときはvectorヘッダをインクルードする。(vector.hでないことに注意!) 12 | 13 | ```cpp 14 | #include 15 | #include 16 | 17 | int main(){ 18 | 19 | std::vector vec;//int型の動的配列 20 | 21 | // 10個の要素を追加していく 22 | for(int i = 0; i < 10; i++ ){ 23 | vec.emplace_back(i); 24 | } 25 | 26 | //出力 27 | for(int i = 0; i < vec.size(); i++ ){ 28 | std::cout << vec[i] << " "; 29 | } 30 | std::cout << std::endl; 31 | 32 | std::cout << "現在のサイズ : " << vec.size() << std::endl; 33 | 34 | return 0; 35 | } 36 | ``` 37 | 38 | > 書き方 39 | 40 | ``` 41 | std::vector<動的配列の中に入れる型> 動的配列の変数名; 42 | 43 | 例:std::vector ary1; 44 | ``` 45 | 46 | > 追加の仕方 47 | 以下の例では、Point2(x,y)でPoint2の実体をつくり、emplace\_backによってvectorの末尾に追加している。 48 | 49 | ``` 50 | 動的配列名.(追加するデータ); 51 | 52 | 例:Point2型の動的配列aryに Point2(x,y)を入れるとしたら 53 | ary.emplace_back(Point2(x,y)) 54 | ``` 55 | 56 | > 配列のサイズ取得(返り値はunsigned int型) 57 | 58 | ``` 59 | 動的配列名.size() 60 | ``` 61 | 62 | > クラスのvector例 63 | 64 | ```cpp 65 | #include 66 | #include 67 | 68 | class MyClass{ 69 | public: 70 | int x; 71 | 72 | MyClass(int _x): 73 | x(_x) 74 | { 75 | } 76 | }; 77 | 78 | 79 | int main(){ 80 | 81 | std::vector vec; 82 | 83 | //要素を追加 84 | for(int i = 0; i < 10; i++){ 85 | vec.emplace_back(MyClass(rand()%10)); 86 | } 87 | 88 | //要素MyClassのxを表示 89 | for(int i = 0; i < vec.size(); i++){ 90 | std::cout << vec[i].x << " "; 91 | } 92 | 93 | std::cout << std::endl;//改行 94 | 95 | return 0; 96 | } 97 | ``` 98 | 99 | ## 演習問題(コンソール) 100 | 101 | 1. int型のvectorを用意し、ランダムに0~9の数を10個入れて、すべて表示せよ。 102 | 上記のvectorの中で5以下の要素を表示せよ。 103 | > ヒント:ランダムな0~9の数を生成するには```rand() % 10```とすればよい。今回はsrand等を使わなくてもよい。 104 | rand()は乱数に偏りがあるので、本来は使わないことを推奨されているが、短く書けるので本講座ではrand()を使う。偏りのない乱数を生成できるrandomに関してはこちらを参照→ [cpprefjp - C++日本語リファレンス random/uniform_real_distribution](https://cpprefjp.github.io/reference/random/uniform_real_distribution.html) 105 | 1. 以下の様なクラスMyClassを用意した。MyClassのvectorを用意し、vectorにMyClassを10個追加し、すべての要素のshow()関数を呼び出せ。xの値はランダムな数でよい。 106 | 107 | class MyClass{ 108 | public: 109 | int x; 110 | 111 | MyClass(int _x): 112 | x(_x) 113 | { 114 | } 115 | 116 | void show(){ 117 | std::cout << "xは:" << x << std::endl; 118 | } 119 | }; 120 | 121 | 122 | 1. 以下の様なPoint2クラスを作った。 123 | Point2クラスのvectorを用意し、rand()などを使って適当な座標を10個格納し、表示せよ。 124 | 125 | class Point2{ 126 | public: 127 | int x; 128 | int y; 129 | 130 | Point2(int _x, int _y): 131 | x(_x), 132 | y(_y) 133 | { 134 | } 135 | }; 136 | 137 | ## 演習問題(DxLib) 138 | 139 | 今回は、前回のプロジェクトからそのままEnemyクラスを書き換えてもいいし、1から作り直しても良い。 140 | 141 | 1. 以下の様なEnemyクラスを用意した。Enemyクラスのインスタンスを作り、動作を確認せよ。 142 | 143 | > Enemy.h 144 | 145 | class Enemy { 146 | public: 147 | double x, y; 148 | double vx, vy; 149 | Enemy(double x, double y); 150 | void update(); 151 | void draw(); 152 | }; 153 | 154 | > Enemy.cpp 155 | 156 | Enemy::Enemy(double _x, double _y): 157 | x(_x), 158 | y(_y), 159 | vx(0.0), 160 | vy(1.0) 161 | { 162 | } 163 | 164 | void Enemy::update() { 165 | x += vx; 166 | y += vy; 167 | } 168 | 169 | //エネミー(円)を描画 170 | void Enemy::draw() { 171 | DrawCircle(x, y, 30.0, 0xFF0000, 1); 172 | } 173 | 174 | 175 | 176 | 177 | 1. Enemyのvectorを作り、Enemyが複数出られるようにせよ。 178 | 179 | 1. Zキーが押されたらランダムな座標に敵が出るようにせよ。 180 | 181 | 1. 現在、メインループ内でEnemyのvectorをfor文で回していると思う。 182 | メインループ内はできるだけすっきりさせたい。そこで、Enemyのvectorをメンバに持ち、updateを呼ぶとメンバのEnemyのupdateをすべて呼ぶEnemyManagerクラスを作り、メインループ内ではEnemyManagerのupdateを呼ぶだけでEnemyすべてが更新され、EnemyManagerのdrawを呼ぶだけですべての敵が描画されるようにする。また、add関数を持ち、それが呼ばれると敵を追加する。 183 | EnemyManagerクラスを作り、敵を管理せよ。 184 | 185 | > EnemyManager.h 186 | 187 | #pragma once 188 | #include 189 | #include 190 | #include "Enemy.h" 191 | 192 | class EnemyManager { 193 | public: 194 | std::vector enemies; 195 | void update(); 196 | void draw(); 197 | void add(Enemy enemy); 198 | }; 199 | 200 | > EnemyManager.cpp 201 | 202 | #include "EnemyManager.h" 203 | 204 | void EnemyManager::update() 205 | { 206 | // ここを実装 207 | } 208 | 209 | void EnemyManager::draw() 210 | { 211 | // ここを実装 212 | } 213 | 214 | void EnemyManager::add(Enemy enemy) 215 | { 216 | // ここを実装 217 | } 218 | 219 | 220 | 221 | 1. Enemy.h, Enemy.cppとは別にEnemyManager.h, EnemyManager.cppを作り、適切にファイル分けをせよ。 222 | 223 | 1. 現在の敵の数を画面左上に表示せよ。ヒント:vectorのsize関数 224 | 225 | 226 | 227 | ## Tips : emplace\_backとpush\_back 228 | 229 | vectorに要素を追加する関数は```emplace_back```と```push_back```がある。 230 | ```emplace_back```はC++11から追加された関数で、```push_back```に比べ、要素追加時にコピーが発生せず処理が効率的なので、こちらを紹介した。 -------------------------------------------------------------------------------- /vol_06_イテレータ・vector要素の削除.md: -------------------------------------------------------------------------------- 1 | # DXライブラリで学ぶC++ 2 | 3 | 今回はvectorの要素を削除する方法を学ぶ。DxLibを用いた演習では、画面外に出た敵を削除する。 4 | C++にはSTLという便利なライブラリが標準でついている。STLにはコンテナが含まれ、コンテナには便利な配列やリスト構造などが用意されている。vectorはコンテナの一種である。 5 | 6 | ## イテレータ 7 | 8 | ポインタを抽象化してstd::vectorや他のコンテナでも似た形で使えるようにしたイテレータというものが存在する。 9 | ポインタが変数をさすのに対して、イテレータはvectorの要素をさす。 10 | begin()関数は、先頭の要素へのイテレータを返す関数、end()関数は、最後の要素の次へのイテレータを返す関数。 11 | 下の例では、これらを用いてfor文を回している。 12 | 13 | ```cpp 14 | #include 15 | #include 16 | 17 | int main(){ 18 | 19 | std::vector vec; 20 | 21 | // 10個の要素を追加していく 22 | for(int i = 0; i < 10; i++ ){ 23 | vec.emplace_back(i); 24 | } 25 | 26 | //イテレータを用いて出力 27 | for(std::vector::iterator iter = vec.begin(); iter != vec.end(); iter++){ 28 | std::cout << *iter << " "; 29 | } 30 | std::cout << std::endl; 31 | 32 | 33 | //begin()関数は、先頭の要素へのイテレータを返す関数 34 | std::vector::iterator iter2 = vec.begin(); 35 | std::cout << *iter2 << std::endl; 36 | 37 | //イテレータはポインタと同じく+することで指す要素を進められる 38 | //はじめの要素を指すイテレータを4つ進めたので、5番目の要素が出力される 39 | iter2 += 4; 40 | std::cout << *iter2 << std::endl; 41 | 42 | return 0; 43 | } 44 | ``` 45 | 46 | std::vector<int>::iteratorは長いので、C++の機能である型推論を使い表記を省略できる。 47 | 型名の代わりに、autoと書くことで、コンパイラが型を推測してくれる。 48 | また、型推論は初期化する値から型を推論するので、auto a;のようには宣言できない。 49 | 50 | ```cpp 51 | #include 52 | #include 53 | 54 | int main(){ 55 | 56 | std::vector vec; 57 | 58 | // 10個の要素を追加していく 59 | for(int i = 0; i < 10; i++ ){ 60 | vec.emplace_back(i); 61 | } 62 | 63 | //イテレータを用いて出力 64 | for(auto iter = vec.begin(); iter != vec.end(); iter++){ 65 | std::cout << *iter << " "; 66 | } 67 | std::cout << std::endl; 68 | 69 | return 0; 70 | } 71 | ``` 72 | 73 | ## クラスのvectorのイテレータ 74 | 75 | イテレータをiterとすると、下のように書けばよい。 76 | 77 | ```cpp 78 | iter->x 79 | ``` 80 | 81 | > Vector2クラスのvectorへのイテレータを使ってVector2クラスのx, yを出力 82 | 83 | ```cpp 84 | #include 85 | #include 86 | 87 | class Vector2{ 88 | public: 89 | int x; 90 | int y; 91 | 92 | Vector2(int _x, int _y) : 93 | x(_x), 94 | y(_y) 95 | { 96 | } 97 | }; 98 | 99 | int main(){ 100 | 101 | std::vector vec; 102 | 103 | //適当な値のVector2を5個入れる 104 | for (int i = 0; i < 5; i++){ 105 | vec.emplace_back(Vector2(rand() % 100, rand() % 100)); 106 | } 107 | 108 | //出力 109 | for (auto iter = vec.begin(); iter != vec.end(); iter++){ 110 | std::cout << iter->x << " " << iter->y << std::endl; 111 | } 112 | 113 | return 0; 114 | } 115 | ``` 116 | 117 | ## vectorの要素の削除 118 | 119 | erase関数で要素を削除できる。引数には削除する要素のイテレータを入れる。 120 | 121 | ```cpp 122 | #include 123 | #include 124 | 125 | int main(){ 126 | 127 | std::vector vec;//int型の動的配列 128 | 129 | // 10個の要素を追加していく 130 | for(int i = 0; i < 10; i++ ){ 131 | vec.emplace_back(i); 132 | } 133 | 134 | //イテレータを用いて出力 135 | for(auto iter = vec.begin(); iter != vec.end(); iter++){ 136 | std::cout << *iter << " "; 137 | } 138 | std::cout << std::endl; 139 | 140 | 141 | std::cout << "現在のサイズ : " << vec.size() << std::endl; 142 | 143 | //4つめの要素の消去 144 | vec.erase(vec.begin() + 3); 145 | 146 | //イテレータを用いて出力 147 | for(auto iter =vec.begin(); iter !=vec.end(); iter++){ 148 | std::cout << *iter << " "; 149 | } 150 | std::cout << std::endl; 151 | 152 | std::cout << "現在のサイズ : " << vec.size() << std::endl; 153 | 154 | return 0; 155 | } 156 | ``` 157 | 158 | 159 | ## 条件にあった要素の削除 160 | 161 | erase関数を使って削除をすると、返り値として削除した要素の次の要素へのイテレータが返ってくるので、それをループ用のイテレータに入れる。 162 | (削除用の関数、remove_ifは次回紹介する。) 163 | 164 | ```cpp 165 | #include 166 | #include 167 | 168 | int main(){ 169 | 170 | std::vector vec;//int型の動的配列 171 | 172 | // 10個の要素を追加していく 173 | for(int i = 0; i < 10; i++ ){ 174 | vec.emplace_back(i); 175 | } 176 | 177 | //出力 178 | for(auto iter =vec.begin(); iter !=vec.end(); iter++){ 179 | std::cout << *iter << " "; 180 | } 181 | std::cout << std::endl; 182 | 183 | std::cout << "現在のサイズ : " << vec.size() << std::endl << std::endl; 184 | 185 | //要素の消去(偶数を消去) 186 | auto it = vec.begin(); 187 | while(it != vec.end()){ 188 | if(*it % 2 == 0){//偶数だったら 189 | std::cout << *it << "を消去" << std::endl; 190 | //eraseの返り値をitで受ける(返り値は削除したものの次につながっていた要素へのイテレータ) 191 | it = vec.erase(it); 192 | }else{ 193 | it++; 194 | } 195 | } 196 | std::cout << std::endl; 197 | 198 | //出力 199 | for(auto iter =vec.begin(); iter !=vec.end(); iter++){ 200 | std::cout << *iter << " "; 201 | } 202 | std::cout << std::endl; 203 | 204 | std::cout << "現在のサイズ : " << vec.size() << std::endl << std::endl; 205 | 206 | return 0; 207 | } 208 | ``` 209 | 210 | ## 演習問題(コンソール) 211 | 212 | 1. 以下のようなクラスを用意した。MyClassのvectorを作り、適当なa,bの値(0~100ぐらい)をもつデータを10個ほど格納し、イテレータを用いたfor文によって表示せよ。 213 | #include 214 | 215 | class MyClass{ 216 | public: 217 | int a; 218 | int b; 219 | 220 | MyClass(int _a, int _b): 221 | a(_a), 222 | b(_b) 223 | { 224 | } 225 | }; 226 | 1. aがbより小さいデータを削除し、再度表示せよ。 227 | 228 | 229 | 230 | ## 演習問題(DxLib) 231 | 232 | 1. 以下のようなEnemyクラスを用意した。Enemyのvectorを作り、敵を複数出せ。forループは、イテレータを用いて書くこと。 233 | 234 | > Enemy.h 235 | 236 | #pragma once 237 | #include 238 | 239 | class Enemy { 240 | public: 241 | double x, y; 242 | double vx, vy; 243 | Enemy(double _x, double _y); 244 | void update(); 245 | void draw(); 246 | }; 247 | 248 | > Enemy.cpp 249 | 250 | # include "Enemy.h" 251 | 252 | Enemy::Enemy(double _x, double _y): 253 | x(_x), 254 | y(_y), 255 | vx(0.0), 256 | vy(0.0) 257 | { 258 | // 適当な速度を設定 259 | vx = (double)(rand() % 10 - 5); 260 | vy = (double)(rand() % 10 - 5); 261 | } 262 | 263 | void Enemy::update() { 264 | x += vx; y += vy; 265 | } 266 | 267 | void Enemy::draw() { 268 | DrawCircle(x, y, 30.0, 0xFF0000, 1); 269 | } 270 | 271 | 1. 前回の課題で、y座標がSCREEN_HEIGHTを超えた敵(下の方の画面外に出た敵)を削除するようにせよ。 272 | 1. 画面外に出た敵を削除するようにせよ。 273 | 274 | -------------------------------------------------------------------------------- /vol_07_remove_if・ラムダ式.md: -------------------------------------------------------------------------------- 1 | # DXライブラリで学ぶC++ 2 | 3 | 今回は前回とは別の、vectorの要素を削除する方法を学ぶ。演習問題では前回と同様敵を削除する。 4 | vectorはコンテナの一種である。algorithmヘッダーにコンテナに対して削除・ソートなどを行う事ができる。 5 | 今回はコンテナに対し条件に一致する要素を削除する関数、remove_ifを使い、要素を削除する。また条件の指定にラムダ式を用いる。 6 | 7 | ## 要素を削除 std::remove_if 8 | 9 | remove_ifは条件にあった要素を削除する。 10 | 正確には、条件にあった要素を、vectorの後ろに配置しなおし、削除したように見せかけた後のvectorの最後のイテレータを返す。 つまり、remove_if関数を呼んだだけでは要素は削除されておらず、その後にerase関数を呼ぶ必要がある。 11 | remove_ifを使うには、algorithmヘッダーをインクルードする。 12 | 13 | > remove_ifの使い方 14 | 15 | ```cpp 16 | //remove_ifを実行し、条件にあった要素を後ろに詰める。 17 | auto rmvIter = std::remove_if(vec.begin(),vec.end(), 関数オブジェクト); 18 | 19 | //後ろの方に詰められた要素を削除 20 | vec.erase( rmvIter, vec.end() ); 21 | ``` 22 | 23 | remove\_ifの第一引数には削除するかを判定する範囲の先頭、第二引数には終端を書く。 24 | remove\_ifの第三引数には削除の条件を返す関数名を書く。(正確には関数オブジェクト) 25 | 26 | > 3の倍数を削除する例 27 | 28 | ```cpp 29 | #include 30 | #include 31 | #include 32 | 33 | //3の倍数かどうかをたしかめて返す叙述関数 34 | bool isMultipleThree(int x){ 35 | return (x%3) == 0; 36 | } 37 | 38 | int main(){ 39 | 40 | std::vector vec;//int型の動的配列 41 | 42 | //0から数を入れる 43 | for(int i = 0; i < 10; i++){ 44 | vec.emplace_back(i); 45 | } 46 | 47 | //表示 48 | for(auto i = vec.begin(); i < vec.end(); ++i){ 49 | std::cout << *i << " "; 50 | } 51 | std::cout << std::endl; 52 | 53 | 54 | //vecの中から3の倍数を後ろに詰める 55 | //3の倍数かどうかの判定は3番目のパラメータで渡された関数オブジェクトで判定 56 | auto rmvIter = std::remove_if(vec.begin(), vec.end(), isMultipleThree); 57 | 58 | //実際に削除 59 | vec.erase( rmvIter, vec.end() ); 60 | 61 | //表示 62 | for(auto i = vec.begin(); i < vec.end(); ++i){ 63 | std::cout << *i << " "; 64 | } 65 | std::cout << std::endl; 66 | 67 | return 0; 68 | } 69 | ``` 70 | 71 | > remove\_ifの第三引数には、remove\_ifが対象にするvectorの中身を引数に取り、bool型(trueかfalse)を返す関数を入れる。以下が例。 72 | 73 | ```cpp 74 | bool Func(配列の要素の型 x){ 75 | return (x%3) == 0;//条件に一致してるか確認してbool型を返す 76 | } 77 | ``` 78 | 79 | 80 | ## ラムダ式 81 | 本項目ではC++の中でも特に書き方が変態的なラムダ式を扱う。 82 | remove\_ifを使う時、大きなプログラムになると削除するかを判定する関数の定義と使う部分が離れてしまう。そこでC++11からはラムダ式というものが導入された。 例えば2で割り切れるかを返す関数オブジェクトを生成する場合 83 | 84 | ```cpp 85 | #include 86 | 87 | int main(){ 88 | 89 | //2で割り切れるかどうかを返す関数オブジェクトを生成 90 | auto isMultiple2 = [](int num){return (num % 2) == 0;}; 91 | 92 | std::cout << isMultiple2(3) << std::endl; 93 | std::cout << isMultiple2(4) << std::endl; 94 | 95 | return 0; 96 | 97 | } 98 | ``` 99 | 100 | 101 | 上のコードの 102 | 103 | ```cpp 104 | [](int num){return (num % 2) == 0;} 105 | ``` 106 | 107 | の部分がラムダ式だ。 ラムダ式の型はautoで推論させている。 108 | 109 | 110 | ラムダ式の書き方 111 | 112 | ```cpp 113 | [](引数){処理}; 114 | ``` 115 | 116 | ところで返り値の型を指定していないことに気づいただろうか? これは多くの場合指定しなくてよいものだ。 117 | ラムダ式の返り値を明示的に指定する場合は 118 | 119 | ```cpp 120 | [](引数)->返り値の型{ 121 | 処理 122 | }; 123 | ``` 124 | 125 | のように()のあとに「->(返り値の型)」という形式にする。 126 | 127 | 128 | ## ラムダ式とremove_if 129 | 130 | ラムダ式を用いると、remove_if関数の近くに条件を判定する関数を書くことが出来る。 131 | 132 | ```cpp 133 | #include 134 | #include 135 | #include 136 | 137 | int main(){ 138 | 139 | std::vector vec;//int型の動的配列 140 | 141 | for(int i = 0; i < 10; i++){ 142 | vec.emplace_back(i); 143 | } 144 | 145 | //表示 146 | for(auto i = vec.begin(); i < vec.end(); ++i){ 147 | std::cout << *i << " "; 148 | } 149 | std::cout << std::endl; 150 | 151 | 152 | //3の倍数かどうかを返す関数 153 | auto isMulThree = 154 | [](int x){ 155 | return (x % 3) == 0; 156 | }; 157 | 158 | //vecの中から3の倍数を後ろに詰める 159 | auto rmvIter = std::remove_if(vec.begin(), vec.end(), isMulThree); 160 | 161 | //実際に削除 162 | vec.erase( rmvIter, vec.end() ); 163 | 164 | 165 | //表示 166 | for(auto i = vec.begin(); i < vec.end(); ++i){ 167 | std::cout << *i << " "; 168 | } 169 | std::cout << std::endl; 170 | 171 | return 0; 172 | } 173 | ``` 174 | 175 | 176 | 177 | remove_if関数の中に条件を判定する関数を直接書くことが出来る。 178 | 179 | 下の例では、第三引数に直接ラムダ式を書いている。 180 | 181 | ```cpp 182 | #include 183 | #include 184 | #include 185 | 186 | int main(){ 187 | 188 | std::vector vec;//int型の動的配列 189 | 190 | for(int i = 0; i < 10; i++){ 191 | vec.emplace_back(i); 192 | } 193 | 194 | //表示 195 | for(auto i = vec.begin(); i < vec.end(); ++i){ 196 | std::cout << *i << " "; 197 | } 198 | std::cout << std::endl; 199 | 200 | 201 | 202 | 203 | //vecの中から3の倍数を後ろに詰める 204 | auto rmvIter = std::remove_if(vec.begin(),vec.end(), 205 | [](int x){ 206 | return (x % 3) == 0; 207 | } 208 | ); 209 | 210 | //実際に削除 211 | vec.erase( rmvIter, vec.end() ); 212 | 213 | 214 | //表示 215 | for(auto i = vec.begin(); i < vec.end(); ++i){ 216 | std::cout << *i << " "; 217 | } 218 | std::cout << std::endl; 219 | 220 | return 0; 221 | } 222 | ``` 223 | 224 | ## 演習問題(コンソール) 225 | 226 | 1. int型の変数を引数にとり、2倍にして返す関数をラムダ式を用いて作成せよ。 227 | 1. int型のvectorに0から9を入れ、remove_ifとラムダ式を用いて2の倍数を削除せよ。 228 | 229 | ## 演習問題(DxLib) 230 | 231 | 1. 前回同様にEnemyクラスを用意した。Enemyのvectorを作り、敵を複数生成せよ。 232 | 233 | > Enemy.h 234 | 235 | #pragma once 236 | #include 237 | 238 | class Enemy { 239 | public: 240 | double x, y; 241 | double vx, vy; 242 | Enemy(double _x, double _y); 243 | void update(); 244 | void draw(); 245 | }; 246 | 247 | > Enemy.cpp 248 | 249 | # include "Enemy.h" 250 | 251 | Enemy::Enemy(double _x, double _y): 252 | x(_x), 253 | y(_y), 254 | vx(0.0), 255 | vy(0.0) 256 | { 257 | // 適当な速度を設定 258 | vx = (double)(rand() % 10 - 5); 259 | vy = (double)(rand() % 10 - 5); 260 | } 261 | 262 | void Enemy::update() { 263 | x += vx; y += vy; 264 | } 265 | 266 | void Enemy::draw() { 267 | DrawCircle(x, y, 30.0, 0xFF0000, 1); 268 | } 269 | 270 | 1. 画面外に出た敵を削除せよ。要素の削除にはalgorithmのremove\_ifを使い、その条件の指定にはラムダ式を使用せよ。 271 | -------------------------------------------------------------------------------- /vol_08_GameManagerクラス・前半課題.md: -------------------------------------------------------------------------------- 1 | # DXライブラリで学ぶC++   2 | 3 | ## GameManagerクラス 4 | 5 | 実際ゲームを作る時、PlayerやEnemyの配列、Bulletの配列、Effectの配列等をメンバに持ち、それらを管理するManagerクラスを作ると便利である。 6 | 今回は、PlayerとEnemyを持つManagerクラスの実体をグローバル変数にすることで、EnemyがPlayerが相互にアクセスする例を示す。 7 | (Vol.04では、EnemyクラスがPlayerへのポインタを持つことで、Playerクラスにアクセスすることができたが、今回はEnemyクラスがPlayerクラスへのポインタを持つ必要はない) 8 | PlayerがEnemyの情報に、EnemyがPlayerの情報に相互にアクセス出来るようにするため、上手くファイルをインクルードしなければならない。 9 | 以下の例では、PlayerはshowEnemyX関数でEnemyのxにアクセスし、EnemyはshowPlayerX関数でPlayerのxにアクセスしている。 10 | 11 | 12 | > Player.h 13 | 14 | ```cpp 15 | #pragma once 16 | #include 17 | #include "Manager.h" 18 | 19 | class Player { 20 | public: 21 | int x; 22 | Player(int _x); 23 | void update(); 24 | void showEnemyX(); 25 | }; 26 | ``` 27 | 28 | > Player.cpp 29 | 30 | ```cpp 31 | #include "Manager.h" 32 | 33 | Player::Player(int _x) : 34 | x(_x) 35 | { 36 | } 37 | 38 | void Player::update() { 39 | std::cout << "Player内のupdateが呼ばれました。xは:" << x << std::endl; 40 | } 41 | 42 | void Player::showEnemyX() { 43 | std::cout << "Player内のshowEnemyXが呼ばれました。" << std::endl; 44 | std::cout << "Enemyのxは:" << manager.enemy.x << std::endl; 45 | } 46 | ``` 47 | 48 | > Enemy.h 49 | 50 | ```cpp 51 | #pragma once 52 | #include 53 | #include "Manager.h" 54 | 55 | class Enemy { 56 | public: 57 | int x; 58 | Enemy(int _x); 59 | void update(); 60 | void showPlayerX(); 61 | }; 62 | ``` 63 | 64 | > Enemy.cpp 65 | 66 | ```cpp 67 | #include "Manager.h" 68 | 69 | Enemy::Enemy(int _x) : 70 | x(_x) 71 | { 72 | } 73 | 74 | void Enemy::update() { 75 | std::cout << "Enemy内のupdateが呼ばれました。xは:" << x << std::endl; 76 | } 77 | 78 | void Enemy::showPlayerX() { 79 | std::cout << "Enemy内のshowPlayerXが呼ばれました。" << std::endl; 80 | std::cout << "Playerのxは:" << manager.player.x << std::endl; 81 | } 82 | ``` 83 | 84 | 85 | > Manager.h 86 | 87 | ```cpp 88 | #pragma once 89 | #include "Player.h" 90 | #include "Enemy.h" 91 | 92 | class Manager { 93 | public: 94 | Player player; 95 | Enemy enemy; 96 | 97 | Manager(); 98 | }; 99 | 100 | extern Manager manager; 101 | ``` 102 | 103 | > Manager.cpp 104 | 105 | ```cpp 106 | #include "Manager.h" 107 | 108 | Manager::Manager() : 109 | player(100), 110 | enemy(200) 111 | { 112 | }; 113 | 114 | Manager manager; 115 | ``` 116 | 117 | > main.cpp 118 | 119 | ```cpp 120 | #include 121 | #include "Manager.h" 122 | 123 | int main() { 124 | 125 | manager.player.update(); 126 | manager.enemy.update(); 127 | 128 | manager.player.showEnemyX(); 129 | manager.enemy.showPlayerX(); 130 | 131 | return 0; 132 | } 133 | ``` 134 | 135 | 136 | ## Tips : range-based-for 137 | 138 | これまでvectorに対して、for文で用いる時、毎回始めのイテレータと終わりのイテレータを書いていたが、これは面倒である。C++にはrange-based-forがありこれを用いると、配列のそれぞれの要素に対して行う処理が簡単に書ける。以下に書き方を示す。 139 | 140 | ```cpp 141 | for(配列の要素の型 受け取る変数 : 配列名){ 142 | 143 | } 144 | ``` 145 | 146 | 以下にサンプルコードを用意した。今回は、自分で用意したMyClass型のvectorのそれぞれの要素のaを表示している。配列の要素の型はautoで推論している。constを使うことで値を間違って変更することを防いでおり、参照を用いて、無駄なコピーをが発生しないようにしている。 147 | 148 | ```cpp 149 | #include 150 | #include 151 | 152 | class MyClass { 153 | public: 154 | int a; 155 | MyClass(int _a) : 156 | a(_a) 157 | { 158 | } 159 | }; 160 | 161 | int main() { 162 | 163 | std::vector vec; 164 | for (int i = 0; i < 5; i++) { 165 | vec.emplace_back(MyClass(i)); 166 | } 167 | 168 | for (const auto& i : vec) { 169 | std::cout << i.a << std::endl; 170 | 171 | // コメントを外すと値を書き換えることになるのでコンパイルエラー 172 | // i.a = 99; 173 | } 174 | 175 | return 0; 176 | } 177 | ``` 178 | 179 | 以下はMyClassのaを全て2倍にする例。参照を用いて、vectorの中身を書き換えている。```auto&&```と、&が1つではなく2つあるがタイプミスではない。今回の場合```auto&```でも問題ないが、```auto&&```を用いると右辺値にも対応できるようになり、より汎用的な書き方になるので、range-based-forを用いる際は基本的にこちらを推奨する。(ちなみに、VisualStudio上でマウスカーソルを```i```の上に持っていくとわかるが、```i```はちゃんとMyClass&と推論されていることがわかる)詳しくは、[range-based for loopsの要素の型について](http://qiita.com/rinse_/items/cdfce8aa6a685a8ebe0c)参照。 180 | 181 | ```cpp 182 | #include 183 | #include 184 | 185 | class MyClass { 186 | public: 187 | int a; 188 | MyClass(int _a) : 189 | a(_a) 190 | { 191 | } 192 | }; 193 | 194 | int main() { 195 | 196 | std::vector vec; 197 | for (int i = 0; i < 5; i++) { 198 | vec.emplace_back(MyClass(i)); 199 | } 200 | 201 | // 参照を用いてそれぞれのMyClassのaを2倍に 202 | for (auto&& i : vec) { 203 | i.a *= 2; 204 | } 205 | 206 | // 中身を表示 207 | for (const auto& i : vec) { 208 | std::cout << i.a << std::endl; 209 | } 210 | 211 | return 0; 212 | } 213 | 214 | ``` 215 | 216 | 217 | 218 | 219 | ## 演習問題(DxLib) 220 | 221 | 1. GameManagerクラスを作れ。 222 | GameManagerクラスは、PlayerやEnemyManager(Enemyのvectorを管理するクラス)をメンバに持ち、それらを管理するクラスである。 223 | GameManagerのインスタンスをグローバル変数にすることで、GameManagerを介してPlayerやEnemyの情報にアクセスできるようにせよ。 224 | GameManagerクラスのupdateを呼べば、PlayerとすべてのEnemyのupdateが呼ばれるようにせよ。 225 | EnemyクラスがPlayerクラスへのポインタを持っていた場合、それを消して、Playerクラスへのアクセスはグローバル空間に存在するGameManagerクラスのインスタンスを介して行うようにせよ。 226 | 227 | 1. Enemyが自機を追うようにせよ。 228 | 229 | 1. Enemyクラス内にint型のkind変数を作り、kind変数によって敵が多様な動きをするようにせよ。 230 | (kind==1なら自機に直進、kind==2ならsin軌道、kind==3なら出た位置で円運動…など) 231 | 232 | 1. kindによって敵の色が変わるようにせよ。 233 | 234 | 1. kindをenum型にせよ。クラス内で定義されたenumは、クラス外部からは(クラス名)::(要素の名前)でenumを指定できる。 235 | 236 | 237 | ## ヒント:enum型 238 | 239 | クラス内でenumを定義しコンストラクタで指定する例。 240 | 241 | ```cpp 242 | #include 243 | 244 | class Hoge { 245 | public: 246 | enum Kind { 247 | A, 248 | B, 249 | C, 250 | }; 251 | 252 | Kind kind; 253 | 254 | //コンストラクタ kindを指定 255 | Hoge(Kind _kind) : 256 | kind(_kind) 257 | { 258 | } 259 | }; 260 | 261 | int main() { 262 | // コンストラクタでkindを指定 263 | Hoge ins(Hoge::B); 264 | 265 | // 出力。整数が出力される(0から始まる) 266 | std::cout << ins.kind << std::endl; 267 | 268 | return 0; 269 | } 270 | ``` 271 | 272 | 273 | ## 前半課題 274 | 275 | これまでやったことを用いて、添付資料のサンプルゲームと似たようなものを作れ。今回は練習として、GameManagerクラスが他のクラスを持つ形にすること。 276 | 277 | > ヒント 278 | 279 | 解答例は以下のようなクラス構成にした。 280 | 281 | GameManagerクラス(他のクラスを所持し、管理する) 282 | |- Playerクラス 283 | |- PlayerBulletManagerクラス(PlayerBulletクラスのvectorを所持) 284 | |- EnemyManagerクラス(Enemyクラスのvectorを所持) 285 | |- EnemyBulletManagerクラス(EnemyBulletクラスのvectorを所持) 286 | |- EffectManagerクラス(Effectクラスのvectorを所持) 287 | |- ScoreManagerクラス(スコアを管理するクラス) 288 | -------------------------------------------------------------------------------- /vol_09_new・delete・基底・派生クラス.md: -------------------------------------------------------------------------------- 1 | # DXライブラリで学ぶC++ 2 | 3 | ## newとdelete 4 | 5 | newを使うと、プログラムの実行中にメモリを確保し、オブジェクトを生成できる。 6 | newすると生成したオブジェクトへのポインタが返ってくる。生成したオブジェクトを見失わないように、ポインタを格納する変数を用意する必要がある。 7 | 8 | ```cpp 9 | #include 10 | 11 | class MyClass{ 12 | public: 13 | int x; 14 | 15 | MyClass(int _x): 16 | x(_x) 17 | { 18 | } 19 | 20 | }; 21 | 22 | int main(){ 23 | 24 | MyClass* ptr = new MyClass(32); 25 | 26 | std::cout << "MyClassのxの値 : " << ptr->x << std::endl; 27 | 28 | return 0; 29 | } 30 | ``` 31 | 32 | deleteを使うと、プログラム実行中にnewで確保された領域を開放することが出来る。 33 | deleteには開放する領域のポインタを渡す。 34 | 下の例では、xの値を表示した後deleteするので、メイン関数を抜ける前にデストラクタが呼ばれる。 35 | deleteされた後にptrのxの値を表示するとカオスな値が入っている(動作未定義)。見てみよう。 36 | deleteしなかったらデストラクタがいつ呼ばれるか確認してみよう。 37 | 38 | ```cpp 39 | #include 40 | 41 | class MyClass{ 42 | public: 43 | int x; 44 | 45 | MyClass(int _x): 46 | x(_x) 47 | { 48 | std::cout << "コンストラクタが呼ばれました" << std::endl; 49 | } 50 | 51 | ~MyClass(){ 52 | std::cout << "デストラクタが呼ばれました" << std::endl; 53 | } 54 | }; 55 | 56 | int main(){ 57 | std::cout << "メイン関数に入りました" << std::endl; 58 | 59 | MyClass* ptr = new MyClass(12); 60 | 61 | std::cout << "MyClassのxの値 : " << ptr->x << std::endl; 62 | 63 | delete ptr; 64 | 65 | std::cout << "メイン関数を抜けました" << std::endl; 66 | 67 | return 0; 68 | } 69 | ``` 70 | 71 | ## 演習問題(コンソール) 72 | 73 | 1. 以下のようなクラスを定義した。MyClassへのポインタのvectorを用意し、適当な値のa(0~10ぐらい)を持ったデータ10個をnewで生成・格納し、表示した後にaが5以下の要素を削除せよ。 74 | 75 | ```cpp 76 | #include 77 | 78 | class MyClass{ 79 | public: 80 | int a; 81 | 82 | MyClass(int _a): 83 | a(_a) 84 | { 85 | } 86 | }; 87 | ``` 88 | 89 | 90 | > ヒント:vectorの要素のMyClassへのポインタを削除しただけでは動的に確保した領域は解放されていない。remove_ifを用いた形式だと、削除する要素それぞれに対してdeleteを行えないので、Vol6で紹介したwhileループを用いた形式でvectorの要素を削除するとよい。 91 | 92 | auto it = vec.begin(); 93 | while(it != vec.end()){ 94 | if(条件){ 95 | it = vec.erase(it); 96 | }else{ 97 | it++; 98 | } 99 | } 100 | 101 | 102 | ## 基底クラスと派生クラス 103 | 104 | クラスを継承することで、新しいクラスを定義することが出来る。 105 | 継承元のクラスを基底クラス、継承先のクラスを派生クラスという。 106 | 派生クラスは基底クラスのメンバを持つ。 107 | 108 | 下の例の場合DerivedクラスがBaseクラスを継承している。 109 | 110 | ```cpp 111 | #include 112 | 113 | class Base{ 114 | public: 115 | Base(int _x): 116 | x(_x) 117 | { 118 | } 119 | int x; 120 | void show(){ 121 | std::cout << "x:" << x << std::endl; 122 | } 123 | }; 124 | 125 | class Derived : public Base{ 126 | public: 127 | Derived(int _x): 128 | Base(_x) 129 | { 130 | } 131 | }; 132 | 133 | 134 | int main(){ 135 | 136 | Base obj_b(12); 137 | obj_b.show(); 138 | 139 | Derived obj_d(23); 140 | obj_d.show(); 141 | 142 | return 0; 143 | } 144 | ``` 145 | 146 | > 書き方 147 | 148 | ```cpp 149 | class 派生クラス名 : アクセス 基底クラス名{ 150 | public: 151 | 派生クラス名(引数1,引数2, ...): 152 | 基底クラス名(引数1,引数2, ...) 153 | { 154 | } 155 | }; 156 | ``` 157 | 158 | 上記の基底クラス名(引数1,引数2, ...) で基底クラスのコンストラクタを呼ぶことが出来る。 159 | 基底クラス名(引数1,引数2, ...) には基底クラスに渡す引数を書く。 160 | アクセスがpublicだと基本クラスのpublicメンバが派生クラスのpublicメンバに、privateだと基本クラスのpublicメンバが派生クラスのprivateメンバになる。 161 | 162 | ## 演習問題(DxLib) 163 | 今回はEnemyを複数出すだけなので、前半課題とは別に、サンプルプロジェクトを元にを作った方が楽かもしれない。 164 | 以下のようなEnenyクラスを用意した。 165 | 166 | 167 | > Enemy.h 168 | 169 | ```cpp 170 | #pragma once 171 | #include 172 | 173 | class Enemy { 174 | public: 175 | static const double Radius; 176 | 177 | double x, y; 178 | double vx, vy; 179 | Enemy(double _x, double _y); 180 | void update(); 181 | void draw(); 182 | }; 183 | ``` 184 | 185 | > Enemy.cpp 186 | 187 | ```cpp 188 | # include "Enemy.h" 189 | 190 | const double Enemy::Radius = 30.0; 191 | 192 | Enemy::Enemy(double _x, double _y): 193 | x(_x), 194 | y(_y), 195 | vx(0.0), 196 | vy(0.0) 197 | { 198 | // 適当な速度を設定 199 | vx = (double)(rand() % 10 - 5); 200 | vy = (double)(rand() % 10 - 5); 201 | } 202 | 203 | void Enemy::update() { 204 | x += vx; y += vy; 205 | } 206 | 207 | void Enemy::draw() { 208 | DrawCircle(x, y, 30.0, 0xFF0000, 1); 209 | } 210 | 211 | ``` 212 | 213 | 1. Enemy型へのポインタを要素に持つvectorを用意し、newを使って敵を複数生成せよ。 214 | 215 | 1. 画面外に行った敵を削除せよ。newで動的確保した領域もdeleteで解放すること。 216 | 217 | -------------------------------------------------------------------------------- /vol_11_スマートポインタ・最終課題.md: -------------------------------------------------------------------------------- 1 | # DXライブラリで学ぶC++ 2 | 3 | ## スコープ 4 | 5 | 下の例では、aは{}内で宣言されているので、{}を抜けるとaが破棄され、aにアクセスできなくなる。 6 | 7 | ```cpp 8 | #include 9 | 10 | int main(){ 11 | 12 | { 13 | int a = 30; 14 | std::cout << "aの値は:" << a << std::endl; 15 | } 16 | 17 | //コメントを外すとコンパイルエラー 18 | //a = 60; 19 | 20 | return 0; 21 | } 22 | ``` 23 | 24 | 下の例では、objは{}内で宣言されているので、{}を抜けるとobjが破棄され、デストラクタが呼ばれる。 25 | 26 | ```cpp 27 | #include 28 | 29 | class MyClass{ 30 | public: 31 | int x; 32 | MyClass(int _x): 33 | x(_x) 34 | { 35 | std::cout << "コンストラクタが呼ばれました" << std::endl; 36 | } 37 | 38 | ~MyClass(){ 39 | std::cout << "デストラクタが呼ばれました" << std::endl; 40 | } 41 | }; 42 | 43 | 44 | int main(){ 45 | 46 | std::cout << "メイン関数に入りました" << std::endl; 47 | 48 | { 49 | std::cout << "スコープに入りました" << std::endl; 50 | MyClass obj(100); 51 | std::cout << "objのxの値:" << obj.x << std::endl; 52 | std::cout << "スコープを抜けます" << std::endl; 53 | } 54 | 55 | //コメントを外すとコンパイルエラー 56 | //obj.x = 200; 57 | 58 | std::cout << "メイン関数を抜けます" << std::endl; 59 | 60 | return 0; 61 | } 62 | ``` 63 | 64 | 下の例では、ptrは{}内で宣言され、オブジェクトは{}内でnewされているが、{}を抜ける時破棄されるのがptrなので、{}を抜ける時にオブジェクトのデストラクタが呼ばれない。 65 | つまり、newで確保された領域はdeleteしていないので、開放されていない。 66 | 67 | ```cpp 68 | #include 69 | 70 | class MyClass{ 71 | public: 72 | int x; 73 | MyClass(int _x): 74 | x(_x) 75 | { 76 | std::cout << "コンストラクタが呼ばれました" << std::endl; 77 | } 78 | 79 | ~MyClass(){ 80 | std::cout << "デストラクタが呼ばれました" << std::endl; 81 | } 82 | }; 83 | 84 | 85 | int main(){ 86 | 87 | std::cout << "メイン関数に入りました" << std::endl; 88 | 89 | { 90 | std::cout << "スコープに入りました" << std::endl; 91 | MyClass *ptr = new MyClass(100); 92 | std::cout << "objのxの値:" << ptr->x << std::endl; 93 | std::cout << "スコープを抜けます" << std::endl; 94 | } 95 | 96 | std::cout << "メイン関数を抜けます" << std::endl; 97 | 98 | return 0; 99 | } 100 | ``` 101 | 102 | ## スマートポインタ std::shared_ptr 103 | 104 | newしたポインタは必ずdeleteしなくてはいけない。これは非常に面倒だし、忘れるとメモリリークの原因になる。そんな時に便利なのがスマートポインタだ。 105 | スマートポインタはほぼ通常のポインタと同じ使い方でしかも適切なタイミングで自動的にdeleteされる。 106 | スマートポインタを使用するにはmemoryヘッダーをインクルードする。 107 | スコープを抜ける時自動的にsptrが破棄され、newで確保したMyClassを指すスマートポインタがなくなるので、自動的にnewで確保した領域が開放される。 108 | 109 | ```cpp 110 | #include 111 | #include 112 | 113 | class MyClass{ 114 | public: 115 | int x; 116 | MyClass(int _x): 117 | x(_x) 118 | { 119 | std::cout << "コンストラクタが呼ばれました" << std::endl; 120 | } 121 | 122 | ~MyClass(){ 123 | std::cout << "デストラクタが呼ばれました" << std::endl; 124 | } 125 | }; 126 | 127 | 128 | int main(){ 129 | 130 | std::cout << "メイン関数に入りました" << std::endl; 131 | 132 | { 133 | std::cout << "スコープに入りました" << std::endl; 134 | std::shared_ptr sptr( new MyClass(100) ); 135 | std::cout << "objのxの値:" << sptr->x << std::endl; 136 | std::cout << "スコープを抜けます" << std::endl; 137 | } 138 | 139 | std::cout << "メイン関数を抜けます" << std::endl; 140 | 141 | return 0; 142 | } 143 | ``` 144 | 145 | 146 | 上の例ではshared_ptrに直接newのポインタを渡して、 147 | 148 | ```cpp 149 | std::shared_ptr sptr( new MyClass(100) ); 150 | ``` 151 | 152 | のように書いているが、これだと例外に対して脆弱なので、std::make_shared関数を使って、 153 | 154 | ```cpp 155 | std::shared_ptr sptr = std::make_shared(100); 156 | ``` 157 | 158 | と書いたほうが良い。 159 | 160 | 161 | > 書き方 162 | 163 | ```cpp 164 | std::shared_ptr<クラス名> 変数名 = std::make_shared<クラス名>(引数1, 引数2, ...); 165 | ``` 166 | 167 | ## Tips : std::shared_ptr以外のスマートポインタ 168 | スマートポインタは,他にも,コピー出来ないが```std::shared_ptr```に比べ高速に動作する```std::unique_ptr```や,```std::shared_ptr```が指すメモリを参照し,[循環参照問題](https://qiita.com/hmito/items/db3b14917120b285112f#%E5%BE%AA%E7%92%B0%E5%8F%82%E7%85%A7)を防ぐことができる```std::weak_ptr```などがある.```std::shared_ptr```に慣れたらこちらも使っていくとよい. 169 | 170 | ## 演習問題(コンソール) 171 | 172 | 1. 以下のような2次元座標を表すVector2クラスを用意した。Vector2クラスへのスマートポインタを要素に持つvectorを用意し、適当な座標を10個ほどmake_sharedで動的確保して追加し、表示せよ。 173 | 174 | class Vector2{ 175 | public: 176 | int x, y; 177 | Vector2(int _x, int _y) 178 | :x(_x), 179 | y(_y) 180 | { 181 | } 182 | ~Vector2() { 183 | std::cout << "Vector2のデストラクタが呼ばれました" << std::endl; 184 | } 185 | }; 186 | 187 | 1. 上記のvectorの中でx > yとなっている要素を削除せよ。 188 | 189 | 190 | ## 演習問題(DxLib) 191 | 192 | 1. 前回の課題を、Enemyのクラスへのスマートポインタを要素に持つvectorを使って書き直せ。 193 | 194 | ## 最終課題 195 | 196 | 前半課題をポリモーフィズムを用いて書き直せ。練習として、敵の基底クラスへのスマートポインタのvectorに派生クラスを入れ、敵の削除にはremove_ifとラムダ式を使うこと。解答例は例のごとく添付資料内にある。 197 | --------------------------------------------------------------------------------