Added some sound effects

This commit is contained in:
Luxferre
2026-01-09 20:27:05 +02:00
parent 063c2eef99
commit ab324f915f
+93 -2
View File
@@ -11,6 +11,8 @@
#include <stdlib.h>
#include <string.h>
/* 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<dl;++i);
play_pulse(0, n2f1, 2);
play_pulse(1, n2f2, 2);
for(i=0;i<dl;++i);
play_pulse(0, n3f1, 2);
play_pulse(1, n3f2, 2);
}
void init_sound() {
APU.status = 3;
APU.pulse[0].ramp = 0x8;
APU.pulse[1].ramp = 0x8;
APU.pulse[0].control = 0x9F;
APU.pulse[1].control = 0x9F;
waitvsync();
}
void beep_start() {
play_jingle(220, 440, 440, 660, 660, 880);
}
void beep_interact() {
play_jingle(440, 660, 220, 880, 440, 660);
}
void beep_enemy() {
play_jingle(440, 220, 220, 110, 110, 55);
}
void beep_weapon() {
play_jingle(660, 990, 440, 880, 660, 990);
}
void beep_potion() {
play_jingle(440, 660, 660, 990, 660, 880);
}
void beep_run() {
play_jingle(660, 880, 440, 660, 220, 440);
}
void beep_win() {
unsigned short i;
play_jingle(55, 110, 110, 220, 220, 440);
for(i=0;i<4000;++i);
play_jingle(440, 660, 660, 880, 880, 1760);
for(i=0;i<4000;++i);
play_jingle(880, 660, 660, 880, 880, 1760);
}
void beep_lose() {
play_jingle(110, 55, 220, 110, 110, 55);
}
void beep_err() {
play_jingle(330, 220, 330, 220, 330, 220);
}
/* Main game logic */
void game_startup() {
unsigned int entropy;
unsigned char joy, i, j;
@@ -94,6 +175,7 @@ void game_startup() {
_randomize();
#endif
entropy = rand();
init_sound();
bgcolor(COLOR_BLACK);
textcolor(0);
init_palette();
@@ -139,6 +221,7 @@ void game_startup() {
waitvsync();
}
srand(entropy);
beep_start();
clrscr();
}
@@ -165,6 +248,8 @@ void win_screen(int score) {
chlinexy(0, 23, SCREEN_WIDTH);
cputsxy(10, 25, "Press Start");
ppu_on();
waitvsync();
beep_win();
while(joy_input_wait() != KEY_ST);
}
@@ -221,7 +306,6 @@ void render_room_item(short i, short v) {
topleft_x, topleft_y,
topright_x, bottomleft_y,
text_x, text_y, cheight=7, cwidth=8, left_offset = 9;
if(i == 0) {
topleft_x = 8;
topleft_y = 0;
@@ -325,7 +409,6 @@ unsigned char confirm(char *prompt) {
cprintf("A=YES");
gotoxy(statbase, SCREEN_HEIGHT-4);
cprintf("B=NO");
while(res > 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;
}