-
-
Notifications
You must be signed in to change notification settings - Fork 76
Input and Output
Output functions explained in this tutorial are only functions for text displaying. Another tutorial will be done for backgrounds.
We will also talk about input which are, well only SNES joypads and multitaps.
The output system is only available for BG_MODE1. If you need it in other mode, you need to develop it.
It is managed via a map of 32x32 characters. The console functions are used to display text.
When you want to display a text, you must specify the x,y coordinates of the text in this map.
You've got two ways to display text on screen :
- using the default map shipped with PVSnesLib (text are sent to display during Vblank)
- using your own map and send it to screen after waiting for vblank with the DMA functions.
The first thing to use the display system is to init the console with the background that will handle the text, and of course, the font to use.
We will see later how to customize the font. PVSneslib is shipped with a default font named pvsneslibfont.bmp. Just take a look at it to know how characters are.
To init display system, just call consoleInitText(0,0, &snesfont) where :
- the first parameter is the background that will handle the text (0..2)
- the second one is the palette number (0..16)
- the last one is the address of graphics for font converted with gfx2snes
.. Somewhere where you initialized variables ...
extern char snesfont;
//---------------------------------------------------------------------------------
.. somewhere in the beginning of your main code ...
// Initialize SNES
consoleInit();
// Initialize text console with our font
consoleInitText(0, 0, &snesfont);
This is done with the consoleDrawText function, just call it with text coordinates and text value. For example consoleDrawText(5,10,"hello world") to display at column 5, row 10 the text **hello world **.
Also, you can format the string to display with generic format options :
For example, %d is used to display integer like consoleDrawText(1,1,"X=%d",varx) that will display X=10 if varx equals 10.
To display a string, you must use %s and %c for a char.
For example :
char name[32];
strcpy(name,"PVSneslib");
consoleDrawText(1,1,"Name of lib=%s",name);
Just take a look at standard printf options of C language to know exactly what you can do.
You can use only two colors, the text and background color with the consoleSetTextCol function. But only text color is important.
Color is designed with the Red, Green and Blue parameters.
Each parameter must be a number between 0 and 31. And you must call function RGB15 to have the final correct color.
So, RGB15(0,0,0) is the black color and RGB15(31,31,31) is the white color.
For example, to use a red text color with black background, just put :
consoleSetTextCol(RGB15(31,0,0), RGB15(0,0,0));
Basically, a custom font is a tiled background, each tile representing a specific character. Here is a basic custom font layout:
The layout is very important, each character must be 8x8 pixels and only from ascii code 32 to 127.
You also have only two colors, the background color (which is the transparent color) and the font color.
We are not going to use the palette for the text font, but you must know that the background color must be the first color (entry #0) and the text color the next one (entry #1 of palette).
For a more accurate editing, I suggest you to display a grid (8×8 pixels) in your favourite graphics editor, like this (zoomed):
One more thing to know about this layout, it must be compiled with the "non optimized" -mR! flag of gfx2snes (to avoid lost of characters).
pvsneslibfont.pic: pvsneslibfont.bmp
@echo convert font with no tile reduction ... $(notdir $@)
$(GFXCONV) -pc16 -n -s8 -o2 -e1 -mR! -m $<
Currently, the only input managed with PVSneslib is the SNES joypad.
__--L--_________________--R--__ Button Colors:
/ _ \ PAL and Japan North America
| _| |_ (X) | X = Blue X = Gray
| |_ _| SLCT STRT (Y) (A) | Y = Green Y = Gray
| |_| (B) | A = Red A = Purple
\_________.-----------._________/ B = Yellow B = Purple
PVSnesLib can handle the two SNES pads, which are identified with number 0 or 1.
To get the values for internal pad management, you must call the scanPads function. It will update the state for the two pads.
Then, let's used the padsCurrent function with the padnumber you want to check. The default one is with number 0.
For example, use a short type variable to do that :
short pad0;
pad0 = padsCurrent(0);
To test is a specific button is pressed, just use the corresponding name :
KEY_A for pad A button.
KEY_B for pad B button.
KEY_X for pad X button.
KEY_Y for pad Y button.
KEY_SELECT for pad SELECT button.
KEY_START for pad START button.
KEY_RIGHT for pad RIGHT button.
KEY_LEFT for pad LEFT button.
KEY_DOWN for pad DOWN button.
KEY_UP for pad UP button.
KEY_R for Right shoulder button.
KEY_L for Left shoulder button.
so, you can use if (pad0 & KEY_A) to know if button A is pressed or if (pad0 & (KEY_A | KEY_X)) to know if button A or button X are pressed. Notice that we used the | operator to add different buttons, not the & operator, this is because | is like the adding operation.
Also, if ( (pad0 & KEY_A) && (pad0 & KEY_X) ) (or if ( (pad0 & (KEY_A | KEY_X) == (KEY_A | KEY_X) )) is used to know if button A and button X are pressed.
At least, the pad is refresh during VBL (thanks to VBlank function), so it is no more needed to use scanPads function to refresh pad values.
// Get current #0 pad
pad0 = padsCurrent(0);
// Update display with current pad
if (pad0 & KEY_A) {
consoleDrawText(12,10,"A PRESSED");
}
- Installation
- PVSneslib and Visual Studio Code
- First steps with No$sns emulator
- Compiling from sources