about summary refs log tree commit diff
path: root/pkgs/games/humblebundle/trine2.nix
blob: a6cef22dca04b5965722c1d6668275b6684040d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
{ buildGame, fetchHumbleBundle, makeWrapper, runCommandCC, writeText
, coreutils, openal, libvorbis, libGLU, SDL2, freetype, alsaLib
}:

buildGame rec {
  name = "trine2-${version}";
  version = "2.01";

  src = fetchHumbleBundle {
    machineName = "trine2complete_linux";
    suffix = "zip";
    md5 = "82049b65c1bce6841335935bc05139c8";
  };

  nativeBuildInputs = [ makeWrapper ];
  buildInputs = [ openal libvorbis libGLU freetype alsaLib ];

  patchPhase = ''
    patchelf --replace-needed libSDL-1.3.so.0 libSDL.so \
      bin/trine2_linux_32bit
    patchelf --replace-needed libPhysXLoader.so.1 libPhysXLoader.so \
      bin/trine2_linux_32bit
  '';

  getResolutionArgs = runCommandCC "get-resolution-args" {
    buildInputs = [ SDL2 ];
    src = writeText "get-resolution-args.c" ''
      #include <SDL.h>

      int main(void)
      {
        int width = 0, height = 0;
        SDL_DisplayMode current;

        SDL_Init(SDL_INIT_VIDEO);

        int displays = SDL_GetNumVideoDisplays();

        for (int i = 0; i < displays; ++i) {
          if (SDL_GetCurrentDisplayMode(i, &current) != 0)
            goto err;

          if (current.w * current.h > width * height) {
            width = current.w;
            height = current.h;
          }
        }

        if (width == 0 && height == 0)
          goto err;

        SDL_Quit();
        printf("-RenderingModule:DetectedFullscreenWidth=%d\n", width);
        printf("-RenderingModule:DetectedFullscreenHeight=%d\n", height);
        return EXIT_SUCCESS;

      err:
        fputs("Unable to get current display mode.\n", stderr);
        SDL_Quit();
        return EXIT_FAILURE;
      }
    '';
  } "gcc -Wall $(sdl2-config --cflags --libs) -o \"$out\" \"$src\"";

  installPhase = ''
    for name in Cg CgGL PhysXCooking PhysXCore PhysXLoader; do
      install -vD "lib/lib32/lib$name.so" "$out/libexec/trine2/lib$name.so"
    done

    install -vD lib/lib32/libSDL-1.3.so.0 "$out/libexec/trine2/libSDL.so"

    mkdir -p "$out/share/trine2"
    cp -rvt "$out/share/trine2" *.fbq trine2.png data

    install -vD bin/trine2_linux_32bit "$out/libexec/trine2/trine2"

    rtDataPath="\''${XDG_DATA_HOME:-\$HOME/.local/share}"
    makeWrapper "$out/libexec/trine2/trine2" "$out/bin/trine2" \
      --run "cd '$out/share/trine2'" \
      --run '${coreutils}/bin/ln -s "'"$rtDataPath"'" "$HOME/.frozenbyte"' \
      --prefix LD_LIBRARY_PATH : "$out/libexec/trine2" \
      --add-flags "\$($getResolutionArgs)"

    mkdir -p "$out/share/applications"
    cat > "$out/share/applications/trine2.desktop" <<EOF
    [Desktop Entry]
    Name=Trine 2
    Type=Application
    Version=1.1
    Exec=$out/bin/trine2
    Icon=$out/share/trine2/trine2.png
    Categories=Game
    EOF
  '';

  sandbox.paths.required = [ "$XDG_DATA_HOME/Trine2" ];
}