List<IEvent> using C
By Angus Cheng
Yesterday I was in the mood to go for a hike. The weather is a lot cooler now so it’s actually nice wandering around in the country parks of Hong Kong. I was originally going to go up to Sunset Peak, but then I felt a bit lazy, so I did a much easier flat hike that goes around sunset peak.
Midway through the walk, I think this is Cheung Sha Beach.
This is the way I came.
On the slow ferry from Mui Wo to Central. For some reason walking gives me a lot of ideas. It also helps me plan out what I’m going to do. I was walking along and thinking about how to implement the List C I talked about in a previous blog post.
I got home and started coding. Then I got a message from Toby Lau. He told me he’s in Hong Kong and wanted to hang out immediately. I met him at Tin Hau station and we got some beef noodles.
Then I took him to check out the view from the roof of my building. Technically I’m not allowed up there. We then walked to Causeway Bay and Toby wanted to see if an arcade he used to go to was still in business.
Amazingly it was, and it still reaks of cigarettes. They run a lot of gambling games down there. I didn’t see slot machines, but I did see some sort of horse racing game. We then went to Genki Sushi and I said goodbye to Toby.
Coding Time
The previous blog post and the mental coding while walking gave me a clear idea of how I would implement this List thing.
- Create a PlayMusic event
- Add in a list of EventTypes
- Add in a list of event indices
- When creating new events, write elements to the above lists
typedef struct GamePlayData {
int eventIndex;
ShowText textEvents[100];
PlayMusic playMusicEvents[100];
float elapsedTime;
Music currentSong;
// TODO: Maybe put all event stuff together into it's own struct?
int eventCount;
enum EventType eventTypes[200];
int eventIndices[200];
} GamePlayData;
Added in the two lists.
void add_play_music(GamePlayData *data, int *counter, const char* path ) {
int i = *counter;
data->playMusicEvents[i].path = path;
data->eventTypes[data->eventCount] = PLAY_MUSIC;
data->eventIndices[data->eventCount] = i;
data->eventCount++;
*counter = *counter + 1;
}
When creating an event, I also write into the two lists.
void draw_gameplay(GameData *gameData) {
BeginDrawing();
ClearBackground(BLACK);
GamePlayData data = gameData->gamePlayData;
EventType eventType = data.eventTypes[data.eventIndex];
int eventIndex = data.eventIndices[data.eventIndex];
if (eventType == PLAY_MUSIC) {
PlayMusicStream(data.currentSong);
}
else if (eventType == SHOW_TEXT) {
init_show_text(gameData, eventIndex);
}
DrawFPS(20, 20);
EndDrawing();
}
Then I deal with the events based on what type they are. Looking at this now, it doesn’t really make sense. PlayMusicStream() doesn’t seem like a function you should be calling in a draw() function. Neither does init_show_text(), but it works!
WASM Build Bug
The code I wrote last night works correctly as a local executable. I then built it out to WASM and uploaded it to itch.io
It crashed immediately.
Uncaught RuntimeError: memory access out of bounds
It had a stack trace as well, I clicked into one of the lines but it was web assembly. I do not know how to read web assembly and I don’t plan on learning it. I googled around, but it’s a pretty generic error. The build I made the day before worked okay. It had to be something I introduced while coding on Monday night. I had a feeling it would take me a while to figure this out so I went to sleep.
I woke up this morning, went into the office and tracked down this bug.
Sound sound = LoadSound("resources/sounds/menu_select.mp3");
It turned out to be this line of code. For some reason this was crashing. I changed it to
Sound sound = LoadSound("resources/sounds/menu_select.wav");
All good! This is strange because loading MP3s as music works. It might be a bug in Raylib. I should probably log an issue on the Raylib Github.
You can check out the latest build here.