[Game]Kong II [V1.0]

Highlighting which section of code you’re talking about might help.
(I went looking but I started finding other things to improve.)

You can highlight a section of code by getting a ‘permalink’ (clicking a line number, then selecting ‘Copy permalink’ from the ‘…’ list) and then adding -L followed by the line number you want the section to end at to the URL.


Glad to see you’re using [[fallthrough]].
Attributes are a very underused feature.


Personally I think it’s indicative of the sound library not yet being as user friendly as it could be.
(Unfortunately that’s one where I probably can’t suggest many ways to improve it because of my lack of sound ability.)

Always interested in other improvements. The abundance of memory (compared to another platform I know of) allows you to be a little sloppy.

With regards to the code - it;s probably not a case of a single line of code but a configuration or a missing magic #define or some other black magic that I do not know about or have not picked up on the significance of.

But here you go … the line of code that plays the sound is highlighted. However, the sounds immediately below play properly but not when they are the first in the game.

I even tried to fool the system with a short, silent tune on the splash screen. It didn’t work and interestingly when the next tune played when starting a game it still was faulty. Its like there is a sound buffer or something and, until this is full, the sound sux.

1 Like

@filmote I changed tune 2 to tune 1 and I got a loopy bug in the beginning of the sound

This is caused by the soundstream update not running (buffers not refreshed)

I need to take a look at how you initiate the sound to see what can be done.

The problem is not the sound file

The introduction is not the best quality sound file but I did the same test as you and found it wasn’t the sound files themselves. I vaguely recall a previous conversation about buffers but I cannot recall any detail.

BTW, which bit are you finding hard? I am struggling with the lower line of clappers.

All of it. I got pretty far, then got boxed in by clappers and bird above.

There should be an easy version for us barely multicelled organisms

3 Likes

In the real G&W, multiple clappers and sparks are often on the screen (per line) simultaneously. Mine does that as well but only after your score is 100 or more.

1 Like

There is a #define in the MySettings.h file, called IGNORE_COLLISIONS that may help …

2 Likes

I’m having a dig through the sound stuff despite my dislike of sound stuff.

The sound isn’t the only obstacle though, ‘clever tricks’ like this make my head ache:

uint32_t output = soundbuf[soundbufindex+=Pokitto::streamon];

I’d attempt to make some improvements but I’m not really in a position to be testing sound,
and without testing I’m fairly sure I’ll end up breaking something.

Example improvement

These 4 if statements:

if (currentBuffer==0) fileReadBytes(&buffers[3][0],BUFFER_SIZE);
else if (currentBuffer==1) fileReadBytes(&buffers[0][0],BUFFER_SIZE);
else if (currentBuffer==2) fileReadBytes(&buffers[1][0],BUFFER_SIZE);
else fileReadBytes(&buffers[2][0],BUFFER_SIZE);

Could be rewritten as:

const std::size_t index = (currentBuffer == 0) ? 3 : (currentBuffer - 1);
fileReadBytes(&buffers[index][0], BUFFER_SIZE);

Thus using a single branch and a single function call rather than 3 branches and 4 function calls.
It’s very unlikely that a compiler would be able to figure this out,
particularly because it would have to assume that the else meant >= 3 and not == 3.
This is the kind of thing humans are good at - inferring intent.


Back in the days of yore when games were actually difficult. :P

1 Like

I wouldn’t even touch the sound code myself … its too complex for me!

I thought you might have some code enhancements for my guff.

If you mean your game code,
I only did a few really minor things because I didn’t want to get sidetracked.
I put them in this branch (and here is a permalink for when I delete the branch).

I did spot a few other things:
  • You could introduce a renderClapper function to cut down on duplication because your code for rendering the upper and lower clappers is almost identical.
    • The same applies to the sparks.
    • While you’re at it you could introduce functions for the other things like the bird.
  • std::fill_n might be better than memset. If there’s no extra cost then prefer std::fill_n.
    • You can also probably replace BUFFER_SIZE with std::size(buffers[0]). Alternatively you could try std::fill as std::fill(std::begin(buffers[0]), std::end(buffers[0]), 128).
  • Unless I’m missing something, using &buffers[index][0] should mean you can get rid of the #if POK_HIGH_RAM == HIGH_RAM_MUSIC in main.cpp. The way it works is a bit awkward, but it should work at least.

But as I say, I didn’t spend much time looking,
those are just what I noticed as I was looking for the relevant sound usage.

Quite frankly, I have no idea how any of that is supposed to work. I copied that code from Galaxy Fighters (as the sound works) and it was originally recommended by @FManga when that game had problems.

1 Like

128 is the midpoint for unsigned 8-bit pulse-code modulation.

In this case a picture is worth a thousand words:

So basically a string of 128 should effectively be a sound ‘flatline’,
meaning the transition into the first sound should be smoother.
(255 is max amplitude, 0 is minimum amplitude.)

It’s possible this is related to your problem, but probably unlikely.
You could certainly try filling with 0 instead to see if that makes things better or worse.

Maybe there’s some kind of mixup between signed and unsigned PCM somewhere?
In signed PCM the baseline is 0, 127 is max amplitude and -128 is min amplitude.

(And now you probably know more or less as much about sound programming as I do. :P)


The reason &buffers[index][0] should work (with index being 0, 1, 2 and 3 in your case) in both cases is because of the types involved:
In the high-RAM case decltype(buffers[index]) is unsigned char *,
and in the other case it’s unsigned char (&)[BUFFER_SIZE].
In both of those cases applying [0] retrieves an unsigned char,
thus & retrieves the address of that unsigned char,
so in both cases you get an unsigned char * from the expression despite the type of buffers being different.

Personally I think it would be better if the buffer pointer were retrieved with a function or if buffers had the same type under both conditions

Example code
#if POK_HIGH_RAM == HIGH_RAM_MUSIC
unsigned char * buffers[4]
{
	reinterpret_cast<unsigned char *>(0x20000000),
	reinterpret_cast<unsigned char *>(0x20000400),
	reinterpret_cast<unsigned char *>(0x20004000),
	reinterpret_cast<unsigned char *>(0x20004400),
};
#else
unsigned char bufferMemory[4][BUFFER_SIZE];

unsigned char * buffers[4]
{
	&bufferMemory[0][0],
	&bufferMemory[1][0],
	&bufferMemory[2][0],
	&bufferMemory[3][0],
};
#endif

Or

unsigned char * getBuffer(std::size_t index)
{
	// Could also use a preprocessor '#if'
	// but since C++17 is available I thought I'd demonstrate this
	if constexpr (POK_HIGH_RAM == HIGH_RAM_MUSIC)
		return buffers[index];
	else
		return &buffers[index][0];
}

unsigned char * getBufferSafe(std::size_t index)
{
	return (index > 3) ? nullptr : getBuffer(index);
}

@filmote , Kong II with IGNORE_COLLISIONS is a big hit among 5-year old gamers here in Malaga

EDIT: such a hit, in fact, that I can’t get it back to debug the sound issue

5 Likes

@filmote , here is your fix

kong2_1.raw (24.9 KB)

4 Likes

Thanks Jonne … I haven’t had a chance to test it but what did you do? If you have just fixed the music file then it suggests that the library is correct and that swapping the music assets around would have highlighted this. But I found that the first SD replay was at fault no matter which sound played first.

2 Likes

Theres a weird init glitch. I wasn’t able to locate it. Solution was to add 0.5 sec of silence in beginning of sample

1 Like

OK … excellent. I am looking for a better sound effect and I need to know how to fix it if I do change. So I wasn’t imagining about the init glitch!

Thanks everyone for your assistance with this. I love this community :heart_eyes:

3 Likes

LOL … at least they see Kong being freed!

5 Likes

Version 1.0 has been released!

There is a glitch with the introduction sound that I cannot fathom and are hoping the Pokitto gods might be able to solve it in the library. If so, I will re-release this with whatever fix is needed.

7 Likes

Drat! I am working overtime today and cannot play K2 until very late tonight, but I will! :smiley:

2 Likes