Draw to the screen?

Setup

There's quite a bit going on in the setup, but it thankfully is mostly boilerplate.

The functions you want, in order, are:

Putting all that together, a simple (minimal) video setup would look like:

#include <gccore.h>

static void* xfb = nullptr;
static GXRModeObj* rmode = nullptr;

void video_initialise()
{
	// Initialise the video system
	VIDEO_Init();

	// Obtain the preferred video mode from the system
	// This will correspond to the settings in the Wii menu
	rmode = VIDEO_GetPreferredMode(NULL);

	// Allocate memory for the display in the uncached region
	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));

	// Set up the video registers with the chosen mode
	VIDEO_Configure(rmode);

	// Tell the video hardware where our display memory is
	VIDEO_SetNextFramebuffer(xfb);

	// Make the display visible
	VIDEO_SetBlack(FALSE);

	// Flush the video register changes to the hardware
	VIDEO_Flush();

	// Wait for Video setup to complete
	VIDEO_WaitVSync();
	if (rmode->viTVMode & VI_NON_INTERLACE)
		VIDEO_WaitVSync();
}

Text output

Getting console output text out to the screen is quite easy. We need an additional setup function, CON_Init() (sometimes seen with the old name console_init()). This takes as input:

And then that's it! You can then use printf() (from <stdio.h>) normally and it prints to the screen.

The console also accepts VT terminal escape codes, notably the cursorpos one to set the console position. The statement printf("\x1b[2;0H"); sets the cursor to row 2, character 0. Remember the padding border in the init function, a pos of 0,0 is not going to be the top-left of the screen if you have a border set.

An example of printing to the console looks like:

int main()
{
	// Initialise the video interface.
	video_initialise()

	// Initialise the console.
	CON_init(xfb, 20, 20, rmode->fbWidth, rmode->xfbHeight, rmode->fbWidth * VI_DISPLAY_PIX_SZ)

	// Set the cursor to row 2.
	printf("\x1b[%d;%dH", 2, 0);

	// Print the text.
	printf("Hello World!");
}

Drawing to the framebuffer

Drawing to the framebuffer directly is the simplest method of drawing to the screen. All that's required is writing the pixel values to the array directly inbetween clears each frame.


Revision #4
Created 4 July 2021 20:51:46
Updated 5 July 2021 18:07:10