1. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    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. 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


    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: Mar 25, 2019
    Assasin1990, mo_v, ioann1s and 14 others like this.
  2. SupImBirDy

    SupImBirDy Member
    Newcomer

    Joined:
    Aug 24, 2018
    Messages:
    24
    Country:
    United States
    I was playing around with this and couldn’t geat it working so thank you for the tutorial!:yayswitch:
     
  3. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    For anything you need, just ask :D
     
  4. SupImBirDy

    SupImBirDy Member
    Newcomer

    Joined:
    Aug 24, 2018
    Messages:
    24
    Country:
    United States
    Stubborness gets in The way my friend
     
  5. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    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: Dec 17, 2018
    lordelan and SupImBirDy like this.
  6. eyeliner

    eyeliner Has an itch needing to be scratched.
    Member

    Joined:
    Feb 17, 2006
    Messages:
    977
    Country:
    Portugal
    Much praise, my hero!
     
  7. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    Updated template to latest scripts from TIL NX.
    Thanks man. Lets do cool stuff :D
     
  8. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    Added Toggle and some changes to Sprite class for animations.
     
  9. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    Added IntroScreen for initial cinematics and JSON Game Data + multilanguage support.
     
  10. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    Added draw functions for rotated images. Drag functions are now in sprite class. Any sprite can be draggable.
     
  11. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    YUZU now renders SDL2 .nro soo it's the best option for testing.
     

    Attached Files:

  12. UnknownProgrammer

    Newcomer

    Joined:
    Dec 31, 2018
    Messages:
    1
    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.
     
    Manurocker95 likes this.
  13. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    Thanks man!!

    Happy new yeaaaaar!!!!
     
  14. lordelan

    lordelan GBAtemp Psycho!
    Member

    Joined:
    Jan 4, 2015
    Messages:
    4,406
    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. :)
     
  15. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    you are welcome, m8. This is mainly for windows but it shouldn't be so different to set this up for Linux
     
    lordelan likes this.
  16. lordelan

    lordelan GBAtemp Psycho!
    Member

    Joined:
    Jan 4, 2015
    Messages:
    4,406
    Country:
    Germany
    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: Jan 3, 2019
  17. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    If you do it, I'll give it a try :P I may include physics libs like box2D in my template sometime
     
    lordelan likes this.
  18. lordelan

    lordelan GBAtemp Psycho!
    Member

    Joined:
    Jan 4, 2015
    Messages:
    4,406
    Country:
    Germany
    Very appreciated! :P
     
  19. eyeliner

    eyeliner Has an itch needing to be scratched.
    Member

    Joined:
    Feb 17, 2006
    Messages:
    977
    Country:
    Portugal
    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.
     
  20. Manurocker95

    OP Manurocker95 Game Developer & Pokémon Master
    Member

    Joined:
    May 29, 2016
    Messages:
    1,302
    Country:
    Spain
    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: Mar 5, 2019
Draft saved Draft deleted
Loading...

Hide similar threads Similar threads with keywords - development, environment, [Tutorial]