What you will learn here:

Drawing in your display

There are many gui functions. They are listed in lua functions, but I will work through them in a different order than listed there. Each section here is stand-alone, so if you get confused at one section, continue to the next, or just take a break. There's no better way to learn than to actually work these functions as you read, so I recommend you do just that.
You will define a global function named on_paint for nearly everything that you need to draw. If you wish to encode a video with a lua display imposed in it, you will define a global function named on_video which will instead paint the video file with the gui functions rather than the lsnes display you see.
You don't need to stick all your gui function calls in a single on_paint function. You can have other functions with gui calls, and on_paint or on_video can call those to update the display or video as needed.
lsnes passes on_paint a boolean value. It is true if the display was done automatically (such as after a frame advance), and false if done manually by a user command or the lua script called gui.repaint(). The distinction usually isn't important for drawing, but if there's something that needs you to look at the frame count, this is helpful.
on_video does not get this boolean value. There isn't any case where you would be able to update the video manually like you would the lsnes display.

The format of colors

Drawing functions in lsnes use a number to determine color. As follows:
Each pair of hexadecimal digits correspond to transparency (aa) or the intensity of red (RR), green (GG), or blue (BB). Getting other colors is to use a mix of these latter three. Yellow uses 0xFFFF00, cyan uses 0x00FFFF, magenta uses 0xFF00FF, white uses 0xFFFFFF. Understanding why this is requires knowing human color perception.
A larger alpha (aa) will make the color more transparent. An alpha of 255 or 0xFF is almost fully transparent. A fully transparent color uses a special number, -1. Not -0x01000000, but simply -1. If you can't see it, it doesn't matter what color it is anyway.
There are functions that will return an lsnes-readable color, if you prefer that instead of dealing with hexadecimal numbers.
This function takes three numbers, each from 0 to 255. gui.color will then return an lsnes-readable number used as an opaque color. You can specify a fourth number, an alpha of 0 to 256, to determine transparency of this number.
This function takes three numbers. It works in a circle: The second number tells you how big the circle is, and the first number tells you how far you traveled around the circle. One full circle takes you back to the same color. The rotation here is the change in the hue of the color, which you've given as the third number.
The hue is what changes. Increasing your steps will take you: Red -> Yellow -> Green -> Cyan -> Blue -> Magenta -> Red. The saturation (how little grey), luminescence (how bright), and opacity are preserved.

The right coordinates

All gui functions that paint something on the screen ask for coordinates, by pixel, as the first two arguments. Coordinates given to lsnes does not necessarily reflect game coordinates, but whatever drawing area lsnes uses.
TODO: Work out the coordinates using on_paint.

Gaps and their value

These functions produce a black area off the sides of the game area. This area gives you more drawing room for your Lua scripts, and a place to draw useful numbers without covering the game area. They need to be called inside on_paint every time, as lsnes will default to a gap of zero the next time it repaints the display if the gap function isn't called the next time.
Keep in mind that, even with left_gap and top_gap, coordinates 0,0 is still the top-left pixel of the game area, and not the top-left corner of the new drawing area. If you need to draw there, negative coordinates are to be used.
To draw off the right and bottom edges, it's best to use gui.resolution() first to get the position of the right and bottom edges. The right and bottom gap drawing areas will start from the x or y coordinate returned by gui.resolution() and increase the further right or down you go.
The difference between the delta and non-delta versions are that, if you call the same side gap a second time, the non-delta will override the old gap, while the delta will instead add to the old gap. The difference is meaningless if you don't plan on chaining scripts together, but the delta gaps are useful if you wish to produce scripts that use gaps without needing to know what other scripts are doing.
These functions also return a number. It's the size of the gap in pixels before your latest call. It's always zero if it's the first call for that gap in on_paint, as there doesn't exist a gap yet. Subsequent calls return the old size. You'll only ever worry about these numbers when you need to make multiple scripts that use gaps.

A colorful look on gui.text

I have mentioned gui.text in an earlier tutorial, so that you have something to do in your starter scripts. Now I will give further details beyond just the fact it lets you paint text onscreen.
Required are the coordinates and text to paint. The first three parameters specify these. The fourth parameter specifies a color the text itself will be painted in, and fifth will specify the rectangle color surrounding the text. Default colors are white for the text and transparent for the rectangle.
If you aren't placing the text in the gap area as outlined above, it is recommended to try 0x40000000 as the background color to keep your text readable. As lua syntax doesn't allow you to specify a fifth parameter without also specifying a fourth, just use 0xFFFFFF (white) there. An example: gui.text(8,8,"Easily seen.",0xFFFFFF,0x40000000)
There are four versions of gui.text. textH is double the width, and textV is double the height. textHV is double sized text. They are otherwise identical to gui.text.

Various drawing functions

TODO: Explain these. In the meantime, try them out and learn them yourself!

How bitmaps work

All these functions go together:
There are actually three separate object types to learn about here.
BITMAP is a bitmap. Each pixel uses a color index to get the color from an accompanying PALETTE.
PALETTE is the set of colors a bitmap uses. You can have multiple PALETTEs so that the same bitmap can show up with different colors.
DBITMAP is a direct bitmap. Each pixel is represented by an exact color to paint. They can be drawn with gui.bitmap_draw without an accompanying PALETTE.
TODO: Explain these, too. Use the reference until this is updated.

Miscellaneous functions

These functions can be called anywhere. They serve a variety of other purposes that isn't drawing directly to the screen.
A snapshot of the game's current display is stored in a file. The string you provide is the destination file.
Requests lsnes to update the display as soon as convenient. This is usually when the current lua code finishes executing. This will also trigger the on_paint() callback, passing it a false value. Generally a good idea if you use on_keyhook or on_button for various controls to the script. Also good to have outside of any function so that you get immediate feedback upon running your script.
lsnes will refuse to respond to this function in an on_paint() callback, instead throwing a lua error. Otherwise, this would be a fast way to cause a lock-up. If you really need to call on_paint from within on_paint, do so directly, but be sure to have some sort of escape condition for any recursive calls.
If you give it true, lsnes will update the display whenever it would call on_input, passing the on_paint callback a value of false. If you give it false, lsnes will resume default behavior to automatically update display only on frame boundaries.
This will slow down emulation, as now lsnes is busier trying to keep the display updated.
Adds a new item to the status panel. The given name and value will show up on the panel. lsnes will not have duplicate names on the panel, instead updating the existing named item with the new value you pass. Passing an empty string value will remove the named item from the status panel. You can use this as some sort of alternate memory watch, if you really want to avoid on_paint for some reason.
Even a Reset Lua VM won't clear any changes made by gui.status.

Breaking old colors

If you have drawn using lua scripts in other emulators, you may have used these formats:
In some emulators, the number format is broken if the red value is greater than or equal to 0x80, due to the point where a 32-bit signed integer won't fit such a large positive value. The other formats are glitch-free in such emulators.
In lsnes, you can only use a number format, and even there, some differences exist. First, lsnes uses 0xaaRRGGBB, not 0xRRGGBBaa. Second, higher values of alpha makes the color less opaque, with 0xFF000000 being almost fully transparent. This design was made in mind so that, if you didn't want to include alpha in your color, you can type 0xRRGGBB and have a fully opaque color of your choice.
If you need a 100% transparent color, lsnes uses a special number for that: -1
There are functions you can call in lsnes if you prefer it giving you a color to use instead of having to recall lsnes number format. gui.color will return a number that lsnes can use. gui.rainbow will return a number with a different hue but same saturation and opacity as one you put in.

FatRatKnight: I still need to produce examples. I'll fix them up at some point. I lack any encoding experience, so on_video hasn't been touched by me.

EmulatorResources/Lsnes/LuaTutorials/GuiAndDrawing last edited by Darkman425 on 8/20/2023 4:38 PM
Page History Latest diff List referrers View Source