mvbg

VM_LOAD_PALETTE

VM_LOAD_PALETTE is a GBVM instruction used to dynamically load a color palette into the Game Boy’s hardware, thereby changing the colors displayed on the screen.

Purpose: Color palettes are fundamental to the visual presentation of Game Boy games. VM_LOAD_PALETTE allows you to change these palettes at runtime, enabling a wide range of visual effects and dynamic environments:

This instruction is crucial for bringing dynamic and expressive visuals to your Game Boy game, especially on the Game Boy Color (CGB) which supports a much wider range of colors than the original Game Boy (DMG).

Syntax:

VM_LOAD_PALETTE MASK, OPTIONS

Usage Example: Applying a Damage Tint to the Screen

Imagine you want the screen to briefly flash red when the player takes damage. You can define a normal palette and a red-tinted palette, then swap between them.

; Define your normal game background palette (CGB example)
PALETTE_NORMAL_BG:
  .CGB_PAL 31,31,31  ; White
             20,20,20  ; Light Gray
             10,10,10  ; Dark Gray
             0,0,0     ; Black

; Define a red-tinted background palette (CGB example)
PALETTE_DAMAGE_BG:
  .CGB_PAL 31,10,10  ; Light Red
             20,0,0    ; Medium Red
             10,0,0    ; Dark Red
             0,0,0     ; Black

; In your script, when the player takes damage:

PLAYER_TAKES_DAMAGE:
  ; Play damage sound effect
  VM_SFX_PLAY SFX_HIT

  ; Load the red-tinted background palette into background palette 0
  VM_LOAD_PALETTE .PALETTE_BACKGROUND_0, PALETTE_DAMAGE_BG

  VM_IDLE 10 ; Keep red tint for a short duration (e.g., 10 frames)

  ; Load the normal background palette back
  VM_LOAD_PALETTE .PALETTE_BACKGROUND_0, PALETTE_NORMAL_BG

  ; ... continue game logic ...
  VM_RET

; Palette mask constants (these would be defined globally)
.PALETTE_BACKGROUND_0:
  .R_INT8 0 ; Corresponds to BGP0

SFX_HIT:
  ; ... sound effect data ...

In this example, when PLAYER_TAKES_DAMAGE is called, VM_LOAD_PALETTE .PALETTE_BACKGROUND_0, PALETTE_DAMAGE_BG instantly changes the background colors to the red-tinted palette. After a brief pause (VM_IDLE 10), the original PALETTE_NORMAL_BG is loaded back, creating a quick visual flash effect. This demonstrates how VM_LOAD_PALETTE can be used to create dynamic and responsive visual feedback in your game.

Analogy to other programming languages/graphics: This is analogous to swapping out a color lookup table (CLUT) or modifying the color values in a shader program at runtime. In modern game development, you might achieve similar effects with post-processing effects or by manipulating color matrices. On the Game Boy, directly loading palettes is the primary way to achieve such dynamic color changes.