Loading...
Searching...
No Matches
main.c File Reference

SNES mouse input with cursor sprite, buttons, and sensitivity. More...

#include <snes.h>
#include <snes/input.h>
#include <snes/text.h>
#include <snes/dma.h>

Functions

int main (void)
 Main entry point – mouse detection, initialization, and input loop.
 

Variables

u8 cursor_pal []
 Cursor sprite palette (SNES BGR555 format) defined in data.asm.
 
u8 cursor_pal_end []
 
u8 cursor_tiles []
 Cursor sprite tile data (16x16, 4bpp) defined in data.asm.
 
u8 cursor_tiles_end []
 
static s16 pos_x
 Absolute cursor X position on screen.
 
static s16 pos_y
 Absolute cursor Y position on screen.
 
static const char sens_hi [] = "HIGH"
 Sensitivity label: high speed (largest deltas per physical movement)
 
static const char sens_low [] = "LOW "
 Sensitivity label: low speed (smallest deltas per physical movement)
 
static const char sens_med [] = "MED "
 Sensitivity label: medium speed.
 

Detailed Description

SNES mouse input with cursor sprite, buttons, and sensitivity.

Demonstrates the SNES Mouse peripheral connected to controller port 1. The SNES mouse is a standard serial device that reports relative X/Y deltas and two buttons each frame via the auto-joypad read registers. This example accumulates the deltas into absolute screen coordinates and drives a 16x16 hardware sprite as a cursor.

The mouse supports three sensitivity levels (low, medium, high) that scale the delta values. Sensitivity is toggled by right-clicking. If no mouse is detected at startup, a diagnostic message is shown.

SNES Concepts
  • Mouse peripheral detection and initialization via auto-joypad
  • Relative-to-absolute coordinate accumulation from mouse deltas
  • Sensitivity control (hardware-level scaling of mouse movement)
  • Sprite used as a cursor overlay on a text background
What to Observe
  • A 16x16 cursor sprite follows mouse movement on screen
  • Left click changes the background color to blue
  • Right click cycles sensitivity (LOW / MED / HIGH)
  • Button states ("PRESSED") appear next to L-click / R-click labels
  • Without a mouse connected, a "No mouse detected" message appears
Modules Used
console, input, sprite, dma, text, background
See also
input.h, sprite.h, dma.h, text.h

Function Documentation

◆ main()

int main ( void  )

Main entry point – mouse detection, initialization, and input loop.

Sets up Mode 0 with a text background for status display and a 16x16 hardware sprite as a mouse cursor. The mouse is detected via mouseInit() on port 1. If absent, the program halts with a diagnostic message. Otherwise, the main loop reads relative mouse deltas each frame, accumulates them into absolute screen coordinates, and updates the cursor sprite position and button/sensitivity status on screen.

Returns
0 (never reached – infinite game loop)

Variable Documentation

◆ cursor_pal

u8 cursor_pal[]
extern

Cursor sprite palette (SNES BGR555 format) defined in data.asm.

◆ cursor_pal_end

u8 cursor_pal_end[]

◆ cursor_tiles

u8 cursor_tiles[]
extern

Cursor sprite tile data (16x16, 4bpp) defined in data.asm.

◆ cursor_tiles_end

u8 cursor_tiles_end[]

◆ pos_x

s16 pos_x
static

Absolute cursor X position on screen.

Accumulated from relative mouse X deltas each frame. The SNES mouse reports displacement, not position, so we must integrate the deltas ourselves and clamp to the visible screen range (0-255).

◆ pos_y

s16 pos_y
static

Absolute cursor Y position on screen.

Same accumulation strategy as pos_x, clamped to 0-223 (NTSC visible).

◆ sens_hi

const char sens_hi[] = "HIGH"
static

Sensitivity label: high speed (largest deltas per physical movement)

◆ sens_low

const char sens_low[] = "LOW "
static

Sensitivity label: low speed (smallest deltas per physical movement)

◆ sens_med

const char sens_med[] = "MED "
static

Sensitivity label: medium speed.