最近高产似那啥,其实就是能玩的东西太多,玩不过来了,虽然话是这么说,但是玩过了还是要写点啥的,不然怎么证明我玩了[捂脸]。

这篇是老任 Switch 的开发入门,甚至连入门都算不上,因为没有 SDK,目前所有的 API 都是一群大佬反编译得到的,属于摸着石头过河的连石头都没摸到的阶段。

废话不多说,先来搭开发环境,一堆命令就完事了(仅限 mac,其他系统暂且无视)

$ brew install python3 cmake llvm squashfs autoconf pkgconfig sdl2 qt5

$ pip3 install pyelftools lz4

$ curl "https://github.com/reswitched/libtransistor/releases/download/v2.1.1/libtransistor_v2.1.1.tar.gz" > libtransistor_v2.1.1.tar.gz

$ sudo mkdir -p /opt/libtransistor

$ sudo chmod -R 777 /opt/libtransistor

$ sudo tar -C /opt/libtransistor -xvzf libtransistor_v2.1.1.tar.gz

$ echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.bash_profile

$ echo 'LIBTRANSISTOR_HOME=/opt/libtransistor' >> ~/.bash_profile

$ . ~/.bash_profile

$ git clone https://github.com/reswitched/sdl-libtransistor.git

$ cd sdl-libtransistor

$ ./autogen.sh

$ make -f switch.mk

完成后就可以折腾代码了,已经有大佬给写好了 Demo(Github),先用这个来进行编译:

$ git clone https://github.com/vgmoose/sdl-hello-world.git

$ cd sdl-hello-world/

$ make

完成后即可看到一个名为 hello.nro 的文件,这就是在 Switch 上的可执行文件了。

接下去就是要运行了,按 Github 上大佬的说法,必须使用自制系统的 Switch,然后将 nro 放到 SD 卡根目录下,然而我却舍不得破解自己的 Switch(废话,被 ban 机了可就啥都没得玩了)。所以比较好的选择是使用模拟器,所幸现在 Switch 模拟器也已经有了。

接下来就自己编译一个模拟器吧, 同样的执行一堆命令,并不麻烦:

$ git clone --recursive https://github.com/yuzu-emu/yuzu

$ cd yuzu

$ git submodule update --init --recursive

$ export Qt5_DIR=/usr/local/opt/qt5

$ export MACOSX_DEPLOYMENT_TARGET=10.12

$ mkdir build

$ cd build

$ cmake .. -DCMAKE_BUILD_TYPE=Release

$ make

完成后即可在 build/bin 下得到一个 yuzu.app,可以直接双击执行之。

用模拟器加载 nro 文件是一个很常规的操作了,从菜单里执行 File | Load File 然后选择 hello.nro 即可。

#include "draw.h"

int main() {

Graphics* g = init();

background(g, 0, 0, 0);

drawColorString(g, 2, 1, "Hello Switch!", 255, 255, 255);

sleep(10);

deinit(g);

return 0;

}

需要注意的是,在模拟器下,背景默认颜色是红色,而且颜色的 RGB 值有偏移,这对于在模拟器下想尝试设置颜色带来非常巨大的麻烦,不过也是没有办法的事情了[汗]。

一些 API 说明

就 sdl-hello-world 下的 Demo 程序而言,提供的 draw.h 含有以下函数,真的是非常的初级,完全不到可用的程度,只能拿来写 Hello World。

函数原型

参数

说明

Graphics* init();

初始化绘图窗口

void deinit(struct Graphics* g);

销毁绘图窗口

void update(struct Graphics* g);

刷新绘图窗口

void sleep(int s);

延迟一定的时间(单位: 秒)

void background(struct Graphics* gr, int r, int g, int b);

RGB

设置背景颜色的 RGB 值

void putAPixel(struct Graphics* gr, int x, int y, int r, int g, int b);

XY坐标,RGB

画一个点

void drawString(struct Graphics* gr, char* string, int xi, int yi);

字符串,XY坐标

画一个字符串

void drawColorString(struct Graphics* gr, int xi, int yi, char * string, int r, int g, int b);

XY坐标,字符串,RGB

画一个带颜色的字符串

总算,到现在为止,Hello World 还是没有让我们失望,借用一位超级大 boss 的话来结束这篇吧[捂脸]

文中参考的内容列表:

https://github.com/vgmoose/sdl-hello-world

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

https://reswitchedweekly.github.io/Development-Setup/#dealing-with-the-deps