PolyMarsDev / Twini-Golf

A (broken) SDL2 game made in 48 hours
255 stars 63 forks source link

Some bugfixes to make it work on macOS Big Sur also (most likely fixes crashes on other systems as well!) #12

Open w-A-L-L-e opened 3 years ago

w-A-L-L-e commented 3 years ago

So I compiled this on macos. And it immediately crashed after clicking to start a game. However after some debugging I found the issue is your getStrokeText and getLevelText functions return a pointer to a std::string that is destructed in the function. A quick hack/fix for this, was to define them as globals so they stay in memory. Then the game works also on my macbook on catelina ;)

Here's the patch:

//reserve string in memory that does not get deleted
//as were returning char* from this!
std::string stroke_text="";
std::string level_text="";

const char* getStrokeText()
{
  stroke_text="";
    int biggestStroke = 0;
    if (balls[1].getStrokes() > balls[0].getStrokes())
    {
        biggestStroke = balls[1].getStrokes();
    }
    else
    {
        biggestStroke = balls[0].getStrokes();
    }
  std::string s = std::to_string(biggestStroke);
    stroke_text = "STROKES: " + s;
    return stroke_text.c_str();
}

const char* getLevelText(int side)
{
    int tempLevel = (level + 1)*2 - 1;
    if (side == 1)
    {
        tempLevel++;
    }
    std::string s = std::to_string(tempLevel);
    level_text = "HOLE: " + s;
    return level_text.c_str();
}

Another smaller issue was I was always getting a ttf error because the check did not work correctly. This I patched as follows:

bool init()
{
    if (SDL_Init(SDL_INIT_VIDEO) > 0)
        std::cout << "HEY.. SDL_Init HAS FAILED. SDL_ERROR: " << SDL_GetError() << std::endl;
    if (!(IMG_Init(IMG_INIT_PNG)))
        std::cout << "IMG_init has failed. Error: " << SDL_GetError() << std::endl;
    if (TTF_Init()==-1){
        std::cout << "TTF_init has failed. Error: "<< TTF_GetError() << std::endl;
  }

  std::cout<<"Opening audio now!"<<std::endl;
    Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
    return true;
}

Ideally however you could better return a std::string or a smart pointer instead.

Now everything works except the sounds are not playing. But I do see its a nice game. Well done in 2 days ;).

Screenshot 2021-08-17 at 20 45 30
w-A-L-L-e commented 3 years ago

And after some more digging. Also got sounds to work on macOS. Turns out the SDL2 libs on mac with brew doesn't support mp3 (you don't get any compiler or run errors but there's just no sounds). I converted the 3 samples to .wav files and then changed this code. Now the whole game works perfectly including sound:

Mix_Chunk* chargeSfx = Mix_LoadWAV("res/sfx/charge.wav");
Mix_Chunk* swingSfx = Mix_LoadWAV("res/sfx/swing.wav");
Mix_Chunk* holeSfx = Mix_LoadWAV("res/sfx/hole.wav");

Also a proper makefile would help a lot. Keeping with the 'minimalistic build scripts' This is what I used to install depencencies with homebrew:

brew install sdl2
brew install sdl2_ttf
brew install sdl2_mixer

compile_and_run.sh (to compile, place assets into right place and start game):

echo "cleaning up..."
rm -rf bin
rm -f *.o

echo "compiling..."
g++ -c src/*.cpp -std=c++14 -O3 -Wall -m64 -I include && mkdir -p bin/release && g++ *.o -o bin/release/main -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
cp -r res bin/release/

echo "starting game..."
./bin/release/main

Future work: write a level generator and add more levels ;) Also maybe different obstacles like ramps that allow increasing/decreasing the velocity (basically positive and negative acceleration) that could even make velocity go negative so a ball runs up and down ramps. Your friction variable is already some kind of negative accel that stops when v=0 I guess ;).

Anyway hope this helps you spread it for macos. For me it was nice to see a completed game with gfx+sound in sdl2 (I dabbled a bit with sdl years ago and then randomly saw your youtube video and then tried to make it run on my macbook ;) ). I do see a lot of things I would have done differently c++ style wise but still it seems to work fine now and I really like how it looks and plays smoothly. So good job!

I've attached the macos binary that I compiled after above patches and it includes the wav files. macos_big_sur_binary.zip

TheRealKeto commented 3 years ago

I've opened a PR that makes building easier with a Makefile; I'd be a lot better if you opened a PR with your code changes rather that putting them in an issue.