Tutorial  Updated

Switch Homebrew development

Switch Homebrew development

I've seen quite a lot questions about how to get into Homebrew development for the switch recently so I decided to share some information I would have found useful when I started.
This Guide is made on Windows 10. Refer to other resources for the installation process on other operating systems. After the setup is done, pretty much everything will be the same.

First of all, here's all the topics I'm going to cover in this Guide:
  • What do I need?
  • Setup and Installation
  • Tools
  • Examples and explanation
  • FAQ
If you find any errors, feel free to report them! So let's get started.



What do I need?Setup and InstallationToolsExamples and explanations


  1. First of all you'll need a Nintendo Switch with a way to get into RCM and some way to start up the custom loader (Hekate for example). There are many tutorials out there on how use those.

    Next, you'll need the following tools:
    • devkitPro : Make sure devkitA64 is selected during installation.
    • hb-menu : You need one of the latest commits to properly use nx-link later so you need to compile it from sources for now.
    • Atom : A nice and advanced text editor. You can use any other text editor you want but I recommend this one.
    • Cmder : An advanced console emulator for Windows. You can use the default Windows cmd but I recommend to switch to better one.
    • (Git : Version control tool. I recommend using it to share your progress and to open source your project so the community can benefit from it. It's not necessary though)
    • ASM/C/C++ knowledge : I'll try to explain everything as easy as possible but don't try getting into homebrew without any prior C programming experience.
    • A lot of patience : We don't have GDB yet so your only way to debug right now is via printf.

    Once you're sure you've got everything above you may continue to the next chapter.

  2. After you've installed devkitPro, Atom Cmder and hopefully git AND you're running the latest version of the hb-menu we can start with the setup process.
    First of all we need to correct the devkitPro environment variables. By default they get set to the paths used on Linux which won't work on Windows.
    To change them, press Win + Pause/Break and click on Advanced system settings in the window that pops up. Now you press on the Environment Variables... button.
    Now search in the System variables list for DEVKITARM, DEVKITPPC and DEVKITPRO. Change them to your devkitPro path. Now if it's not already there, add a DEVKITA64 variable and set the path to the devkitA64 folder path.
    After you're finished, it should look something like this:

    0uJYvpt.png

    Now find the PATH variable and add the path to the ../devkitPro/tools/bin directory to it.

    After this step you're basically done. You don't need any setup for Atom or Cmder. They just work out of the box.​

  3. For compilation and deployment we're going to use two things: make and nxlink. Both get installed by default together with devkitPro.

    If you use the attached template project, all you'll need to do is to type make. This will take care of the whole compilation part. To deploy your compiled project to the Switch now we use nxlink.

    First, enter the hbmenu on your Switch and press the Y Button. This brings you into the nxlink mode. The list of homebrews should disappear now. Now you can run the following command:

    nxlink -s -a 192.168.xxx.xxx out\<homebrew_name>.nro

    This nxlink command some arguments.
    • -s stands for server. It's used for debugging. With this option enabled, printf output can get redirected to our console.
    • -a stands for address. It's the IP address of the Switch. You can look it up either in your router settings or in the Switch settings.
    • out\<homebrew_name>.nro is the path to your homebrew executable. By default it gets built inside the /out folder.


  4. First of all, download the attached sample homebrew and load it up in Atom.
    There are a lot of great examples for many different things inside the /devkitPro/examples/switch folder. Read through those first if you want to start developing. You can also look at the source code of EdiZon, Checkpoint, the hbmenu or any other homebrew to find out how they did certain stuff. We're all a community and learn from each others :). For any other questions consult the official documentation of libnx. You may ask here in this thread as well but I can't promise to be able to help everybody.

    So, let's get started with the examples. More examples will come over time.

    Button detection & console outputGraphics output


    1. Code:
      #include <switch.h>
      #include <stdio.h>
      
      int main(int argc, char** argv) {
          u32 kdown = 0x00000000;
          u32 kdownOld = 0x00000000;
      
          gfxInitDefault();
          consoleInit(nullptr); //Init the console
      
          while(appletMainLoop()) {
              hidScanInput();    //Scan for input       
              kdown = hidKeysDown(CONTROLLER_P1_AUTO); //Read the currently pressed buttons and store that value into kdown
      
               //Edge detection. We don't want to print "Hello World" while A is pressed. We only want to print it once and then again on the next press
              if(kdown > kdownOld) {
                  if(kdown & KEY_A)
                      printf("Hello World\n"); //Prints to the console on screen
              }
      
              kdownOld = kdown;
               
              gfxFlushBuffers();   //Finish this frame
              gfxSwapBuffers();    //Display it on the screen
              gfxWaitForVsync();   //Wait till the last frame finished displaying before updating to avoid flickering
          }
          gfxExit();
          return 0;
      }

    2. Code:
      #include <switch.h>
      
      int main(int argc, char** argv) {
         u8 *framebuffer;
         u32 framebuffer_width;
         u32 framebuffer_height;
      
         gfxInitDefault();
         framebuffer = gfxGetFramebuffer(&framebuffer_width, &framebuffer_height);  //Gets the address of the framebuffer so we can draw to it
      
         while(appletMainLoop()) {
             //The framebuffer is layed out in RGBA8 format. We're coloring it entirely in Magenta (0xFF00FFFF)
              for(u16 x = 0; x < framebuffer_width; x++) {
                  for(u16 y = 0; y < framebuffer_height; y++) {
                       framebuffer[(x + y * framebuffer_width) * 4 + 0] = 0xFF;   //Red
                       framebuffer[(x + y * framebuffer_width) * 4 + 1] = 0x00;   //Green
                       framebuffer[(x + y * framebuffer_width) * 4 + 2] = 0xFF;   //Blue
                       framebuffer[(x + y * framebuffer_width) * 4 + 3] = 0xFF;   //Alpha
                  }
              }
            
              gfxFlushBuffers();   //Finish this frame
              gfxSwapBuffers();    //Display it on the screen
              gfxWaitForVsync();   //Wait till the last frame finished displaying before updating to avoid flickering
          }
      
          gfxExit();
      
         return 0;
      }
 

Attachments

  • HomebrewTemplate.zip
    156.3 KB · Views: 1,734
Last edited by WerWolv,

ozzyOuzo

Active Member
Newcomer
Joined
Mar 30, 2022
Messages
25
Trophies
0
Age
53
Location
earth
XP
134
Country
Greece
any tips on debugging?

I use ryujinx and yuzu to test my nro, but i'd like to see the stdout at least

so finally found out the solution to do so :

call Result svcOutputDebugString(const char *str, u64 size);

from svc.h

(devkitpro)
 

VladDDD

New Member
Newbie
Joined
Dec 28, 2023
Messages
1
Trophies
0
Age
40
XP
23
Country
Mexico
I already managed to get it to work but the basic network "curl" does not work


C:\devkitPro\wifi>make
main.cpp
C:/devkitPro/wifi/source/main.cpp:5:10: fatal error: curl/curl.h: No such file o
r directory
5 | #include <curl/curl.h>
| ^~~~~~~~~~~~~
compilation terminated.
make[1]: *** [/opt/devkitpro/devkitA64/base_rules:16: main.o] Error 1
make: *** [Makefile:166: build] Error 2

how to add the library???
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • AncientBoi @ AncientBoi:
    ooowwww a new way for me to beat NFS 510 :D @SylverReZ
    +1
  • SylverReZ @ SylverReZ:
    @AncientBoi, Yeah, believe you can do PSP games as well. But a Pi5 is much powerful in comparison.
    +2
  • Psionic Roshambo @ Psionic Roshambo:
    Not sure about other models of Pi4 but the Pi 4 B with 8GBs OCed to 2Ghz handles PSP really great except like 1 game I found and it is playable it just looks bad lol Motor Storm Arctic something or other.
  • Psionic Roshambo @ Psionic Roshambo:
    Other games I can have turned up to like 2X and all kinds of enhancements, Motorstorm hmmm nope 1X and no enhancements lol
  • Veho @ Veho:
    Waiting for Anbernic's rg[whatever]SP price announcement, gimme.
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    I will admit that one does seem more interesting than the usual Ambernic ones, and I already liked those.
  • Veho @ Veho:
    I dread the price point.
    +1
  • Veho @ Veho:
    This looks like one of their premium models, so... $150 :glare:
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    To me that seems reasonable.
  • Psionic Roshambo @ Psionic Roshambo:
    I mean since basically all the games are errmmm free lol
  • Veho @ Veho:
    I mean yeah sure but the specs are the same as a $50 model, it's just those pesky "quality of life" things driving up the price, like an actually working speaker, or buttons that don't melt, and stuff like that.
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    I think all in my Pi 4 was well north of 200 bucks 150ish for the Pi 4 the case the fancy cooler, then like 70 for the 500GB MicroSD then like 70 for the Xbox controller. But honestly it's a nice set up I really enjoy and to me was worth every penny. (even bought more controllers for 2 or 4 player games.) hmmm have never played any 2 player games yet :(
  • Veho @ Veho:
    Yeah that's what I hate about the RPi, it's supposedly $30 or something but it takes an additional $200 of accessories to actually turn it into a working something.
  • Psionic Roshambo @ Psionic Roshambo:
    yes that's the expensive part lol
  • Veho @ Veho:
    I mean sure it's flexible and stuff but so is uremum but it's fiddly.
  • Psionic Roshambo @ Psionic Roshambo:
    Yeah a lot of it I consider a hobby, using Batocera I am constantly adjusting the collection adding and removing stuff, scraping the artwork. Haven't even started on some music for the theme... Also way down the road I am considering attempting to do a WiiFlow knock off lol
  • Veho @ Veho:
    I want everything served on a plate plz ktnx, "work" is too much work for me.
  • Veho @ Veho:
    Hmm, with that in mind, maybe a complete out-the-box solution with all the games collected, pacthed and optimized for me would be worth $150 :unsure:
  • Psionic Roshambo @ Psionic Roshambo:
    Yeah it's all choice and that's a good thing :)
  • Bunjolio @ Bunjolio:
    animal crossing new leaf 11pm music
  • Bunjolio @ Bunjolio:
    avatars-kKKZnC8XiW7HEUw0-KdJMsw-t1080x1080.jpg
    wokey d pronouns
  • SylverReZ @ SylverReZ:
    What its like to do online shopping in 1998: https://www.youtube.com/watch?v=vwag5XE8oJo
    SylverReZ @ SylverReZ: What its like to do online shopping in 1998: https://www.youtube.com/watch?v=vwag5XE8oJo