One of the most frustrating problems I’ve run in to date on my Water Drum game! Take a look at the output in the terminal:
alaskalinuxuser@alaskalinuxuser-OptiPlex-7010:~/Documents/c++/sfml_projects/sfml_water_drumming$ ./build.sh
DrumGame.cpp: In function ‘int main()’:
DrumGame.cpp:428:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (roundNum == demoMode.length()) {
^
DrumGame.cpp:432:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
} else if (roundNum < demoMode.length()) {
^
DrumGame.cpp:506:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (currentDemo == demoMode.length()) {
^
./build.sh: line 14: 3281 Segmentation fault (core dumped) ./DrumGame-app
type ./DrumGame-app to launch
A segmentation fault! But why?! It doesn’t really mention it here, so I tried to pull an strace on the program. Amongst pages and pages of gibbly-gook, there was this little gem about the exitButton being out of range.
DOH! I can be really, really, really dumb sometimes!
Here was my code:
window.draw(exitButton[numExitButtons].getSprite());
That should have been written as:
window.draw(exitButton[0].getSprite());
You see, there is only 1 exit button, so the numExitButtons variable was set to just 1. However, in an array (the []) the first (and only) exitButton starts with numeral zero! Boy, I can be a real dunce sometimes! All that time spent working on the issue that was a coder induced numerical error!
But, praise God, the DrumGame-app, Water Drumming! is complete! You can check the latest commits on GitLab, and download the source code as well! Be sure, once you extract it, to run the ./build.sh script. This game does require libsfml. If you try it out, you should drop a line to say what you think of it!
Linux – keep it simple.
Could you not just do: window.draw(exitButton[numExitButtons-1].getSprite());
This is a classic off-by-one error. Thank you, computer scientists of the past. Everybody runs into this, so don’t beat yourself up about it.
Good point! That would work better, in the event that there was more than one exit button. Thanks!