From ab324f915f32aad60c2c20cf632161410f9b30b9 Mon Sep 17 00:00:00 2001 From: Luxferre Date: Fri, 9 Jan 2026 20:27:05 +0200 Subject: [PATCH] Added some sound effects --- nes/nescoundrel.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/nes/nescoundrel.c b/nes/nescoundrel.c index 710098a..c196787 100644 --- a/nes/nescoundrel.c +++ b/nes/nescoundrel.c @@ -11,6 +11,8 @@ #include #include +/* Constants section */ + #define SCREEN_WIDTH 32 #define SCREEN_HEIGHT 28 #define SCREEN_CENTER_X (SCREEN_WIDTH >> 1) @@ -30,6 +32,8 @@ #define GAME_YELLOW 2 #define GAME_GREEN 3 +/* Graphics section */ + void ppu_off() { PPU.mask &= 0xE7; } @@ -87,6 +91,83 @@ void colorxy(unsigned char x, unsigned char y, unsigned char palette) { PPU.vram.data = attr_byte; } +/* Sound section */ + +#define CPU 1789773 /* NTSC */ + +unsigned int get_timer(int freq) { + return (unsigned int) ((CPU / 16 / freq) - 1); +} + +void play_pulse(unsigned char idx, unsigned int freq, unsigned char len) { + unsigned int tm = get_timer(freq); + if(len > 31) len = 31; + if(idx > 1) idx = 0; + APU.pulse[idx].period_low = tm & 255; + APU.pulse[idx].len_period_high = (tm >> 8) | (len << 3); +} + +void play_jingle(unsigned int n1f1, + unsigned int n1f2, + unsigned int n2f1, + unsigned int n2f2, + unsigned int n3f1, + unsigned int n3f2) { + unsigned int i, dl = 2000; + play_pulse(0, n1f1, 2); + play_pulse(1, n1f2, 2); + for(i=0;i 1) { switch(joy_input_wait()) { case KEY_A: res = 1; break; @@ -333,6 +416,7 @@ unsigned char confirm(char *prompt) { default: res = 2; } } + beep_interact(); return res; } @@ -361,6 +445,7 @@ void shuffle(char *array, unsigned int n) { void handle_monster(int val) { int healthlost = val; char monster_msg[(SCREEN_WIDTH-1)]; + beep_enemy(); if(weapon > 0 && val <= durability) { int weapondiff = val - weapon; if(weapondiff < 0) weapondiff = 0; @@ -384,11 +469,13 @@ void handle_weapon(int val) { weapon = val; snprintf(status_msg, sizeof(status_msg), "You equip a %d-level weapon.", weapon); durability = 14; /* reset durability to max limit */ + beep_weapon(); } void handle_potion(unsigned char val) { if(potion_drank) { print_msg("Already drank a potion here."); + beep_err(); } else { potion_drank = 1; hp += val; @@ -396,6 +483,7 @@ void handle_potion(unsigned char val) { snprintf(status_msg, sizeof(status_msg), "You drink a %d-level potion.", val); if(deck_idx >= sizeof(deck) || deck[deck_idx] == 0) last_potion_val = val; + beep_potion(); } } @@ -460,6 +548,7 @@ int game() { break; } else if(ch == CH_INVAL) { print_msg("Invalid input!"); + beep_err(); continue; } else if(ch == CH_R) { /* run mechanics */ /* Compact and rearrange the deck */ @@ -490,6 +579,7 @@ int game() { engaged = 0; last_room = 0; /* Since we added cards, it's not the last room anymore */ print_msg("You ran from the room."); + beep_run(); } else { /* engagement mechanics */ engaged++; /* increase the amount of engaged cards */ can_run = 0; /* Cannot run once engaged */ @@ -553,6 +643,7 @@ int main() { win_screen(score); break; } else { + beep_lose(); snprintf(status_msg, sizeof(status_msg), "RIP! Score: %d. Restart?", score); if(!confirm(status_msg)) break; }