Tutorial [Tutorial] Setting up Visual Studio 2017 environment for Nintendo Switch homebrew development

Manurocker95

Game Developer & Pokémon Master
OP
Member
Joined
May 29, 2016
Messages
1,494
Trophies
0
Age
27
Location
Madrid
Website
manuelrodriguezmatesanz.com
XP
2,279
Country
Spain
Hello everyone!

After a whole week fighting with SDL2 I decided to make a similar tutorial to my first one: https://gbatemp.net/threads/tutoria...for-nintendo-3ds-homebrew-development.461083/
so anyone can easily create stuff for Nintendo Switch.

This tutorial is mainly made for Windows, so there are maybe some things you cannot use on MAC/Linux. Just change those as needed.

There's a simple console tutorial made by @WerWolv, so be sure to check it out here.

Introduction:

This guide shows how to set up the initial Nintendo Switch homebrew development environment using Visual Studio 2017 Community Edition. This guide should also apply to Visual Studio 2017 Professional/Enterprise and up. The last version I tested is Visual Studio 2022 (16/02/2022 . This setup will allow you to use Visual Studio's IntelliSense when working with your code, while being able to compile your code into .NRO files for homebrew applications. We can create .NSP o our homebrews with NRO2NSPBuilder.

By following this guide, you will be creating your own project from scratch in C++. However, should you feel like using a pre-made Visual Studio project template, you may use devkitPro Examples or my own template. If you want a game as reference, check any of my games: Good examples are T-REKT NX or TIL NX which has drag functions.

We will call Visual Studio 2017 Community as "VS2017" from here on out. As I mentioned before, Enterprise and Professional editions can be used as well.

Microsoft Visual C/C++ is not bundled with VS2017 by default. You must choose to install this package, as it is required for Nintendo Switch homebrew development. If you cannot find it in the VS2017 Installer, you can follow the instructions to obtain this package below.

Minum Requirements:

- Latest version of Visual Studio 2017 Community: https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx
- Microsoft Visual C/C++ package for Visual Studio 2017 Community.
- Latest stable version of devkitPro: http://devkitpro.org/wiki/Getting_Started
- Switch packages obtained by pacman packages
- Knowledge in C or C++.
- Creativity and time to make good stuff.

Notes:

To explain the inconsistencies of slashes, "\" and "/", used in this guide:

In Windows, of any editions, it uses backslashes "\" as file separators, and not forward slashes "/". This also means in Visual Studio of any editions, it natively uses backslashes "\" as file separators. If you see forward slashes "/", this clearly means it is used in Makefiles.

The usage of backslashes "\" was due to the forward slashes "/" being used as indicators for "switches" or "flags" (cmd.exe /w, or /help) in IBM software, and is not compatible to parse as file separators. MS-DOS adopted this, and to this day, forward slash switches are still used in many places.

Setup:

1) Acquiring Microsoft Visual C/C++ for VS2017:

- Install VS2017. It will install Visual Studio Installer too.

- Run Visual Studio Installer.

- Click on Modify below VS2017.

- Click on Components > Select C++ components > Install.


VS2017 will then install Visual C/C++ packages. Follow the instructions, then continue to the next section.


2) Nintendo Switch Homebrew Development Setup:

-
Install devkitPro, by following the instructions given in the Getting Started article.

- Run Visual Studio 2017 Community.

- On the Start page in VS2017, under the Start section, click "New Project...".

- When in the New Project wizard, click on Installed > Templates > Visual C++ > Makefile Project. It can be in Installed > C++ > MAKE.

- Down at the bottom, choose your project name, your solution location, and the solution action.

NOTE: MAKE SURE your solution location is located in a directory where the file path does NOT contain any WHITESPACES!

- Click OK.

- A new panel will show. Add the commands for Build, Rebuild, and Clean, as follows:

Build-> make
Rebuild-> make clean all
Clean-> make clean


- On the right pane, add the following path to the "Include Search Path":


\path\to\devkitPro\devkitA64\include
\path\to\devkitPro\libnx\include
$(ProjectDir)\include

- Click OK and the project will be setup.


If you want to add more folders to include:

- In the Solution Explorer, right click on your project, and choose Properties.

- On the left pane, click on General.

- Make sure under General, Configuration Type is set to Makefile.

- On the left pane, click on VC++ Directories.

- Under General, click on Include Directories, click on the arrow dropdown button on the right, and select <Edit>.

- Add the desired filepaths.

- Click OK to go back to the VS2017 editor.



- In the Solution Explorer, add a new item under the filter, "Source Files", by right clicking the filter, Add > "New Item...".

- In the Add New Item wizard, click on C++ File.

- Down at the bottom of the wizard, make sure the Location is the following, with the folder, "source" added at the end (And yes, it is all lowercase "source"):

\path\without\whitespace\to\project\source\

- Type your C++ file name (lets call it main.cpp), and click on "Add".
Add the following #if and #defines macros at the top of your CPP file:

Code:
#if __INTELLISENSE__
 typedef unsigned int __SIZE_TYPE__;
 typedef unsigned long __PTRDIFF_TYPE__;
 #define __attribute__(q)
 #define __builtin_strcmp(a,b) 0
 #define __builtin_strlen(a) 0
 #define __builtin_memcpy(a,b) 0
 #define __builtin_va_list void*
 #define __builtin_va_start(a,b)
 #define __extension__
 #endif

 #if defined(_MSC_VER)
 #include <BaseTsd.h>
 typedef SSIZE_T ssize_t;
 #endif

Then add the following code to your C++ file after it:

Code:
#include <switch.h>
#include <iostream>

int main(int argc, char* argv[])
{
    consoleInit(NULL);

    // Other initialization goes here. As a demonstration, we print hello world.
    std::cout<<"Hello World!\n"<<std::endl;

    // Main loop
    while (appletMainLoop())
    {
        // Scan all the inputs. This should be done once for each frame
        hidScanInput();

        // hidKeysDown returns information about which buttons have been
        // just pressed in this frame compared to the previous one
        u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);

    if (kDown & KEY_A)
              std::cout<<"Pressed A button!\n"<<std::endl;

        if (kDown & KEY_PLUS)
            break; // break in order to return to hbmenu

        // Your code goes here

        // Update the console, sending a new frame to the display
        consoleUpdate(NULL);
    }

    // Deinitialize and clean up resources used by the console (important!)
    consoleExit(NULL);
    return 0;
}

This is the part where it gets tricky.

Ignore the errors in the Error List by turning it off or clicking "X of X Errors" once, where X is any given number.

If you see any squiggly lines underneath ANY letter or character in the code provide above, it means you have set your IntelliSense incorrectly. Make sure to double check all of the steps above, to see if you have missed any.


Tricky part is finished. Congratulations!

-
In File Explorer, navigate to the following directory:

\path\to\devkitPro\examples\switch\templates\application

- Copy the Makefile file.

- In File Explorer, navigate back to your project root directory.

- Paste the Makefile file to the project root directory.

- In VS2017, in the Solution Explorer, right click on your project, Add > "Existing Item...".

- Select Makefile.

- Click on "Add".

- In the Solution Explorer, open the Makefile, so the file is opened in VS2017.

- Edit the name of the homebrew, the creator to fit your project.

- Save Makefile.

- In the Solution Explorer, right click on the Makefile, and select "Properties".

- In the Makefile Property Pages, make sure on the right pane, under General, the Item Type is Text, and the rest of the entries are empty.

- Click OK to exit back to VS2017 editor.

- Hit CTRL+SHIFT+B or in the taskbar, "Build > Build Solution" or "Build > Build [project name]", where [project name] is your project name. You can also click on the green button: Windows Debugger and it will compile too.

- In VS2017, in the Output tab, you should see your code being built via Makefile.

And that's it! From this point on, you are free to add anything you want.

Adding more packages for devkitPro:

We will use some libraries in our homebrews but for sure, we need SDL2. Let's install it!

- First we need to open the command window: Windows + R > cmd or Shift + Right Click > Open CMD Prompt Here

- For displaying the package list (to know package names), just type: pacman -Sl

-
Install the packages with: pacman -Syuu [package1] [package2] [every package you want to install...]

Example: pacman -Syuu switch-sdl switch-sdl2_gfx switch-sdl2_image switch-sdl2_ttf switch-sdl2_net switch-sdl2_mixer

You may need to update portlibs by:

pacman -S switch-portlibs

NOTE: You can install as many packages as you need in your homebrew

-
Once the packages are installed, to use SDL we need to add to the makefile this line:
Code:
LIBS    :=    -lSDL2_ttf -lSDL2_gfx -lSDL2_image -lnx

and if you want to use every library as the template (you need every package installed):

Code:
LIBS    :=    -lSDL2_ttf -lSDL2_gfx -lSDL2_image -lSDL2_mixer -lpng -ljpeg -lglad -lEGL -lglapi -ldrm_nouveau -lvorbisidec -logg -lmpg123 -lmodplug -lstdc++ -lavformat -lavcodec -lswresample -lswscale -lavutil -lbz2 -lass -ltheora -lvorbis -lopus `sdl2-config --libs` `freetype-config --libs` -lnx

Using SDL in your project:

We will use NX-Shell's my SDL Helper modification. Let's see how it works:

First, we will download SDL_Helper.hpp, SDL_Helper.cpp, FontCache.c and FontCache.h from my template and place it in our source folder.

Now you can use SDL2. Congrats!

I recommend you to create an SDL_Helper object and send it's pointers.

For that, in the main.cpp copy this at the beggining of the method:

Code:
    plInitialize();
    romfsInit();
    SDL_Helper * helper = new SDL_Helper();
    helper->SDL_HelperInit();

For loading an image we will use:

Code:
SDL_Texture * myTexture;
helper->SDL_LoadImage(&myTexture, "romfs:/textureSprite.png");

in the main loop, when you need to paint your texture just use:

Code:
        helper->SDL_DrawImage(myTexture, x, y);
        helper->SDL_Renderdisplay();

I added rect method for multiple frames:

Code:
        helper->SDL_DrawImageRect(myTexture, x, y, xOffset, yOffset, frameWidth, frameHeight);
        helper->SDL_Renderdisplay();

And opacity methods:

Code:
        helper->SDL_DrawImageOpacity(myTexture, x, y, opacity);
        helper->SDL_Renderdisplay();

Once we exit the program, we need to delete everything:

Code:
    plExit();
    romfsExit();
    helper->SDL_Exit();
    delete(helper);

Testing the homebrew:

YuZu now supports SDL Rendering in latest Canary:

https://github.com/yuzu-emu/yuzu-canary/releases

For testing in Real-Hardware we have two options:

- Copy the generated .nro to the SD/switch folder and test it from the HBMenu.
- Use NX Link:

NXLink is already installed with devkitpro if Switch tools are installed. If you don't have it, install switch tools with pacman.

- Go to the HBMenu in your switch and press Y to start the netloader. A pop-up will appear with the IP of your switch (It must be connected to Internet).

(You can do the commands directly in Windows CMD, but this is more comfy)

- Go back to your computer and create a text file with this commands:

Code:
cd path\to\devkitPro\tools\bin
nxlink -s -a 192.xxx.xxx.xxx path\to\homebrew.nro

Where the numbers are the IP shown in your switch's screen

Save the text file with .bat extension.

Just double click on the bat and it will start sending the HB to your switch. After the copy, the homebrew will run automatically.

Template:

In the template you will find a good example of how to create a game easily with template classes: Splash Screen, Title Screen, Intro Screen (cinematic) and Game Screen.

It has some UI classes for creating sprites, buttons and toggles. Sprites can be animated by frames. Sprites can be draggable with the Switch touch screen.

It has game data and multilanguage support with JSON.

Take a look to it, and if you need help, just ask.

Credits:

SDL2 references:

  • joel16
  • bernardo giordano
  • Cheeze
Help and simple tutorial:
  • Cid2mizard
  • WerWolv
General:
  • Credits for everyone involved in LibNX and Homebrew Development
  • Thanks, SciresM for your awesome work in Atmosphere CFW.
  • Smealum, because you deserve it, dude.

If this tutorial has been useful to you, credits are appreaciated. :D
For anything, just leave a comment below.
 
Last edited by Manurocker95,

Manurocker95

Game Developer & Pokémon Master
OP
Member
Joined
May 29, 2016
Messages
1,494
Trophies
0
Age
27
Location
Madrid
Website
manuelrodriguezmatesanz.com
XP
2,279
Country
Spain
New Additions:

- Added Splash screen, title screen and game screen.
- Added Loading screen and more modifications to load custom fonts in ttf format.
- Added SFX and Music classes.
- Added GUI Classes: Text, Sprite and Button.
- Added Save Data.
- Everything is more noob-friendly.

Check out my last game: https://gbatemp.net/threads/touch-game-til-nx.526417/
 
Last edited by Manurocker95,

Manurocker95

Game Developer & Pokémon Master
OP
Member
Joined
May 29, 2016
Messages
1,494
Trophies
0
Age
27
Location
Madrid
Website
manuelrodriguezmatesanz.com
XP
2,279
Country
Spain
YUZU now renders SDL2 .nro soo it's the best option for testing.
 

Attachments

  • DvrKpcjWoAAcl7W.jpg
    DvrKpcjWoAAcl7W.jpg
    99.7 KB · Views: 302

UnknownProgrammer

New Member
Newbie
Joined
Dec 31, 2018
Messages
1
Trophies
0
Age
24
XP
37
Country
United States
This was incredibly helpful for someone just getting into Switch development. Thank you!!

If you're having issues with BaseTsd.h make sure to install "Windows 8.1 SDK and UCRT SDK" from the Visual Studio Installer.
 
  • Like
Reactions: Manurocker95

Manurocker95

Game Developer & Pokémon Master
OP
Member
Joined
May 29, 2016
Messages
1,494
Trophies
0
Age
27
Location
Madrid
Website
manuelrodriguezmatesanz.com
XP
2,279
Country
Spain
This was incredibly helpful for someone just getting into Switch development. Thank you!!

If you're having issues with BaseTsd.h make sure to install "Windows 8.1 SDK and UCRT SDK" from the Visual Studio Installer.
Thanks man!!

Happy new yeaaaaar!!!!
 

lordelan

Well-Known Member
Member
Joined
Jan 4, 2015
Messages
5,361
Trophies
1
Age
42
XP
5,327
Country
Germany
Thx for the tutorial. Haven't read it yet but I will try it out. I set up an environment on Linux a few month ago and had some issues which lead to a lack of interest but I'll try this. :)
 

Manurocker95

Game Developer & Pokémon Master
OP
Member
Joined
May 29, 2016
Messages
1,494
Trophies
0
Age
27
Location
Madrid
Website
manuelrodriguezmatesanz.com
XP
2,279
Country
Spain
Thx for the tutorial. Haven't read it yet but I will try it out. I set up an environment on Linux a few month ago and had some issues which lead to a lack of interest but I'll try this. :)

you are welcome, m8. This is mainly for windows but it shouldn't be so different to set this up for Linux
 
  • Like
Reactions: lordelan

lordelan

Well-Known Member
Member
Joined
Jan 4, 2015
Messages
5,361
Trophies
1
Age
42
XP
5,327
Country
Germany
you are welcome, m8. This is mainly for windows but it shouldn't be so different to set this up for Linux
No no, I'd try it on Windows, don't worry.
I don't think I would use VS under Linux lol. Even with Wine.

Edit: Btw you should edit your signature and make sure that words like "Nintendo Switch Games" are not inside any color tags.
Because right now those words are completely invisible for users using the gbatemp dark theme (like me). :P

Edit 2: Since you're crediting this guy would you mind to be the person who finally fullfills my wish of a Blobby Volley 2 Switch port? :)

Edit 3: Okay that's not BV2 but "just" a simple rewrite of BV1 but that's something. Maybe I'll try it on my own. :P
 
Last edited by lordelan,

eyeliner

Has an itch needing to be scratched.
Member
Joined
Feb 17, 2006
Messages
2,246
Trophies
1
Age
42
XP
4,008
Country
Portugal
If you do it, I'll give it a try :P I may include physics libs like box2D in my template sometime
Hello, my friend. I am stupid. Please be aware.
I also hada long day, and spend a good amount of time in setting the development environment up.
All is well.

Except when I want to load an image
VS rants that "SDL_Texture is undefined" at this line:
Code:
    SDL_Texture * myTexture;
Can you help?
I just want to load an image. It's my first foray in C++, mind you. Up to that point all was well.
 

Manurocker95

Game Developer & Pokémon Master
OP
Member
Joined
May 29, 2016
Messages
1,494
Trophies
0
Age
27
Location
Madrid
Website
manuelrodriguezmatesanz.com
XP
2,279
Country
Spain
Hello, my friend. I am stupid. Please be aware.
I also hada long day, and spend a good amount of time in setting the development environment up.
All is well.

Except when I want to load an image
VS rants that "SDL_Texture is undefined" at this line:
Code:
    SDL_Texture * myTexture;
Can you help?
I just want to load an image. It's my first foray in C++, mind you. Up to that point all was well.

It seems you didn’t install SDL correctly by pacman (adding packages section). Check with -Syuu which ones you have and which ones you don’t :)

If you have everything correctly setup you maybe are not including the needed headers: sdl.h and sdl_image.h.

If you have the libraries installed, you should be able to compile any of my homebrews by yourself.
 
Last edited by Manurocker95,
General chit-chat
Help Users
  • No one is chatting at the moment.
    KenniesNewName @ KenniesNewName: https://youtube.com/shorts/IgnvdONcqYI?feature=share