Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Documentation] Add setting up a new project docs #6988

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Documentation/config.xml
Expand Up @@ -38,8 +38,10 @@
<topic id="adding_content" name="Adding Content" filename="getting_started/3_adding_content.md"/>
<topic id="adding_basic_code" name="Adding Basic Code" filename="getting_started/4_adding_basic_code.md"/>
</topic>
<topic id="setting_up_project" name="Setting up a new project" filename="setting_up_project/setting_up_project.md">
<topic id="setting_up_project_vscode" name="Visual Studio Code" filename="setting_up_project/setting_up_project_vscode.md"/>
</topic>
<topic id="Tutorials" name="Tutorials" filename="tutorials.md"/>

<topic id="Adding_Art_Sounds_and_More" name="Adding Art, Sounds, and More" filename="content_intro.md">
<topic id="Using_The_Pipeline_Tool" name="Using The Pipeline Tool" filename="using_pipeline_tool.md"/>
<topic id="Custom_Effects" name="Custom Effects" filename="custom_effects.md"/>
Expand Down
37 changes: 37 additions & 0 deletions Documentation/setting_up_project/setting_up_project.md
@@ -0,0 +1,37 @@
# Setting up a new project

In this section, we shall go over some proper ways to set up a new project. MonoGame based projects usually require you to have a shared code and content base that can be used between platforms. As such, it's better to set everything up early on.

There are two common ways to approach this problem, and those are:

## 1. Using a PCL / .NET Standard library project

PCL library projects get compiled into a library that can be referenced by almost any other .NET project. All the platforms support them; however, they have been deprecated and superseded by .NET Standard library projects.

.NET Standard library projects have the same goal as PCL library projects, except they have higher target framework requirements. In case of using them with MonoGame, you would need to use at least .NET Standard 2.0, which requires .NET Framework 4.7.1 or .NET Core 2.0 as the deploy target.

### Positive:
- Great for writing libraries that you want to distribute
- You don't need to worry if the code would fail to compile on platform X because it has a different C# API

### Negative:
- The target framework you select might not work on some platforms, so you have to be careful
- You can't invoke any Native functions, but instead you will need to setup interfaces and assign them from outside the projct if you need to do so

## 2. Using a shared library project

Shared library projects do not get compiled into their own thing. Instead, all the files in them get automatically included during the compilation of the project that references it.

### Positive:
- You don't need to worry about .NET Framework version, but you do need to worry about C# version you are using
- You can easily hack stuff up for specific platforms without using interfaces or anything like that

### Negative:
- Currently, they are in a limbo-like state where some .NET tooling does not work with them, and there is no fix in sight
- Code might not compile on some platforms out of the box

## Conclusion

Now that you know the basic differences between the two, please select the tooling you plan on using, and we will get right into explaining a good way you can set it up:

- [Visual Studio Code](setting_up_project_vscode.md)
136 changes: 136 additions & 0 deletions Documentation/setting_up_project/setting_up_project_vscode.md
@@ -0,0 +1,136 @@
# Setting up a new project with Visual Studio Code

Please read [Setting up a new project](setting_up_project.md) for an explanation of strengths and weaknesses between the different approaches shown below.

For convenience sake, we shall use `Pong` as our project name. Replace all occurrences of it with the name you want.

Visual Studio Code does not have any new project wizard. Instead, we will use the `dotnet` command-line tool to create our project. Follow the shell commands in either `powershell`, `bash` or `zsh`.

No matter which aproach you choose, you will need MonoGame templates installed, so first off run the following command:

```sh
dotnet new --install MonoGame.Templates.CSharp
```

## 1. Using .NET Standard library

First off we need to make a directory for our game:

```sh
mkdir Pong
cd Pong
```

Now that we have our directory, let us create our game library project which will possess all the code:

```sh
dotnet new sln
dotnet new mgnetstandard -n Pong
dotnet sln add Pong/Pong.csproj
```

Next up, we need to make a project for each platform we want our game to run on. The following steps apply to any platform, however for simplicity, let us make a project that can be run on Windows, Linux, and Mac:

```sh
dotnet new mgdesktopgl -n Pong.DesktopGL
dotnet sln add Pong.DesktopGL/Pong.DesktopGL.csproj
```

Now that we have created our platform project, we need to do a few tweaks to it.

First reference our game library from it:

```sh
cd Pong.DesktopGL
dotnet add reference ../Pong/Pong.csproj
```

Next, delete the generated `Game1.cs` and `Content` as they both exist in our library project:

```sh
rm -r Content
rm Game1.cs
```

And finally, we need to fix the link to our content project, simply replace `Content\Content.mgcb` with `..\Pong\Content\Content.mgcb` in `Pong.DesktopGL.csproj`:

Everything is ready, and you can now run the game :)

```
dotnet run
```

## 2. Using a Shared Library

First off we need to make a directory for our game:

```sh
mkdir Pong
cd Pong
```

Now that we have our directory, let us create our shared game library project which will possess all the code:

```sh
dotnet new sln
dotnet new mgshared -n Pong
```

As stated in the previous tutorial, shared project are not working properly with some newer stuff. So instead of using them lets make our own using wildcards:

```
rm Pong/Pong.projitems
rm Pong/Pong.shproj
```

Next up, we need to make a project for each platform we want our game to run on. The following steps apply to any platform, however for simplicity, let us make a project that can be run on Windows, Linux, and Mac:

```sh
dotnet new mgdesktopgl -n Pong.DesktopGL
dotnet sln add Pong.DesktopGL/Pong.DesktopGL.csproj
```

Now that we have created our platform project, we need to do a few tweaks to it.

First reference our game library from it:

```sh
cd Pong.DesktopGL
```

Next, delete the generated `Game1.cs` and `Content` as they both exist in our shared library project:

```sh
rm -r Content
rm Game1.cs
```

And finally, we need to reference our shared project. Simply replace the following text in `Pong.DesktopGL.csproj`:

```xml
<ItemGroup>
<MonoGameContentReference Include="Content\Content.mgcb" Visible="false" />
</ItemGroup>
```

With:

```xml
<ItemGroup>
<Compile Include="..\Pong\**\*.cs">
<Link>Pong\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>
</ItemGroup>

<ItemGroup>
<MonoGameContentReference Include="..\Pong\**\*.mgcb">
<Link>Pong\%(RecursiveDir)%(Filename)%(Extension)</Link>
</MonoGameContentReference>
</ItemGroup>
```

Everything is ready, and you can now run the game :)

```
dotnet run
```