Example #1
0
        public void GEALOSinit()
        {
            if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) < 0)
            {
                Console.WriteLine($"There was an issue initilizing SDL. {SDL.SDL_GetError()}");
                Environment.Exit(-1);
            }

            if (SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_PNG |
                                   SDL_image.IMG_InitFlags.IMG_INIT_TIF | SDL_image.IMG_InitFlags.IMG_INIT_WEBP) < 0)
            {
                Console.WriteLine($"There was an issue initilizing SDL_image. {SDL.SDL_GetError()}");
                Environment.Exit(-1);
            }

            if (SDL_ttf.TTF_Init() < 0)
            {
                Console.WriteLine($"There was an issue initilizing SDL_ttf. {SDL.SDL_GetError()}");
                Environment.Exit(-1);
            }

            if (SDL_mixer.Mix_OpenAudio(22050, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 4096) < 0 ||
                SDL_mixer.Mix_Init(SDL_mixer.MIX_InitFlags.MIX_INIT_FLAC | SDL_mixer.MIX_InitFlags.MIX_INIT_MID |
                                   SDL_mixer.MIX_InitFlags.MIX_INIT_MOD | SDL_mixer.MIX_InitFlags.MIX_INIT_MP3 |
                                   SDL_mixer.MIX_InitFlags.MIX_INIT_OGG | SDL_mixer.MIX_InitFlags.MIX_INIT_OPUS) < 0)
            {
                Console.WriteLine($"There was an issue initilizing SDL_mixer. {SDL.SDL_GetError()}");
                Environment.Exit(-1);
            }

            running = true;

            GEALOSprog();
        }
Example #2
0
    private static void Start()
    {
        // ======================================================================================
        // Initialize SDL
        // ======================================================================================

        if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) != 0)
        {
            throw new Exception("Failed to initialize SDL.");
        }

        if (SDL_ttf.TTF_Init() != 0)
        {
            throw new Exception("Failed to initialize SDL_ttf.");
        }

        SDL_mixer.MIX_InitFlags mixInitFlags = SDL_mixer.MIX_InitFlags.MIX_INIT_MP3 | SDL_mixer.MIX_InitFlags.MIX_INIT_OGG | SDL_mixer.MIX_InitFlags.MIX_INIT_FLAC;
        if (((SDL_mixer.MIX_InitFlags)SDL_mixer.Mix_Init(mixInitFlags) & mixInitFlags) != mixInitFlags)
        {
            throw new Exception("Failed to initialize SDL_mixer.");
        }

        if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 1024) != 0)
        {
            throw new Exception("Failed to initialize SDL_mixer.");
        }

        SDL_mixer.Mix_AllocateChannels(AudioChannelCount);

        Window = SDL.SDL_CreateWindow(
            Game.Title,
            SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(0),
            SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(0),
            (int)Game.Resolution.X,
            (int)Game.Resolution.Y,
            0);

        if (Window == IntPtr.Zero)
        {
            throw new Exception("Failed to create window.");
        }

        Renderer = SDL.SDL_CreateRenderer(Window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC | SDL.SDL_RendererFlags.SDL_RENDERER_TARGETTEXTURE);

        if (Renderer == IntPtr.Zero)
        {
            throw new Exception("Failed to create renderer.");
        }

        IntPtr renderTargetHandle = SDL.SDL_CreateTexture(Renderer, SDL.SDL_PIXELFORMAT_RGBA8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_TARGET, (int)Game.Resolution.X, (int)Game.Resolution.Y);

        RenderTarget = new Texture(renderTargetHandle, (int)Game.Resolution.X, (int)Game.Resolution.Y);

        // ======================================================================================
        // Instantiate the game object
        // ======================================================================================

        Game = new Game();
    }
Example #3
0
        private static void InitializeSdlSystems()
        {
            Console.WriteLine("---");
            Console.WriteLine("Initializing SDL2 core...");

            SDL2.SDL_Init(BootConfig.SdlModules.SdlInitFlags);
            SDL_mixer.Mix_Init(BootConfig.MixerModules.SdlMixerFlags);
        }
Example #4
0
        /// <inheritDoc/>
        internal override void InitializeAudioEngine(AudioDevice device)
        {
            // Initialize SDL Audio part
            SDL.SDL_InitSubSystem(SDL.SDL_INIT_AUDIO);

            // Initialize SDL Mixer part
            SDL_mixer.MIX_InitFlags requestedFlags = SDL_mixer.MIX_InitFlags.MIX_INIT_FLAC | SDL_mixer.MIX_InitFlags.MIX_INIT_MP3 |
                                                     SDL_mixer.MIX_InitFlags.MIX_INIT_OGG | SDL_mixer.MIX_InitFlags.MIX_INIT_MOD |
                                                     SDL_mixer.MIX_InitFlags.MIX_INIT_FLUIDSYNTH;
            int flags = SDL_mixer.Mix_Init(requestedFlags);

            // TODO: Check `flags' to see if underlying platform supports all formats
        }
Example #5
0
        public MusicManager()
        {
            int success = SDL_mixer.Mix_Init(SDL_mixer.MIX_InitFlags.MIX_INIT_MP3 | SDL_mixer.MIX_InitFlags.MIX_INIT_MID);

            if (success != -1)
            {
                success = SDL_mixer.Mix_OpenAudio(SDL_mixer.MIX_DEFAULT_FREQUENCY, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 1024);
            }

            if (success == -1)
            {
                throw new Exception("SDL_mixer failed to initialize.");
            }
        }
Example #6
0
 public SDLMixer(MixerFormats flags, int numChannels)
 {
     if (SDL.SDL_Init(SDL.SDL_INIT_AUDIO) != 0)
     {
         throw new SDLException();
     }
     if ((SDL_mixer.MIX_InitFlags)SDL_mixer.Mix_Init((SDL_mixer.MIX_InitFlags)flags) != (SDL_mixer.MIX_InitFlags)flags)
     {
         throw new MixerException();
     }
     if (SDL_mixer.Mix_OpenAudio(44100, SDL.AUDIO_S16, 2, 4096) != 0)
     {
         throw new MixerException();
     }
     if (SDL_mixer.Mix_AllocateChannels(numChannels) != numChannels)
     {
         throw new MixerException();
     }
     formats   = flags;
     nChannels = numChannels;
 }
Example #7
0
        public static Engine Create(EngineOptions options, IController controller, IEventsCycle eventsCycle,
                                    ISceneRenderer sceneRenderer)
        {
            if (SDL_Init(SDL_INIT_VIDEO) < 0)
            {
                throw new InvalidOperationException($"Cant initialize SDL2: {SDL_GetError()}");
            }

            if (SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_JPG | SDL_image.IMG_InitFlags.IMG_INIT_PNG) < 0)
            {
                throw new InvalidOperationException($"Cant initialize SDL_image: {SDL_GetError()}");
            }

            if (SDL_ttf.TTF_Init() < 0)
            {
                throw new InvalidOperationException($"TTF_Init: {SDL_GetError()}");
            }

            if (SDL_ShowCursor(0) < 0)
            {
                throw new InvalidOperationException($"Cant disable cursor: {SDL_GetError()}");
            }

            if (SDL_mixer.Mix_Init(
                    SDL_mixer.MIX_InitFlags.MIX_INIT_MP3
                    | SDL_mixer.MIX_InitFlags.MIX_INIT_MID
                    | SDL_mixer.MIX_InitFlags.MIX_INIT_MOD
                    | SDL_mixer.MIX_InitFlags.MIX_INIT_OGG
                    | SDL_mixer.MIX_InitFlags.MIX_INIT_FLAC
                    | SDL_mixer.MIX_InitFlags.MIX_INIT_OPUS) < 0)
            {
                throw new InvalidOperationException($"MixInit: {SDL_GetError()}");
            }

            if (SDL_mixer.Mix_OpenAudio(22050, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 4096) < 0)
            {
                throw new InvalidOperationException($"OpenAudio: {SDL_GetError()}");
            }

            if (SDL_mixer.Mix_AllocateChannels(16) < 0)
            {
                throw new InvalidOperationException($"Cant allocate channels: {SDL_GetError()}");
            }

            if (SDL_mixer.Mix_Volume(-1, 64) < 0)
            {
                throw new InvalidOperationException($"Min_Volume: {SDL_GetError()}");
            }


            var screen = Ui.Screen.Create(options.WindowTitle, options.ScreenHeight, options.ScreenWidth,
                                          options.FullScreen);
            var miniMapRenderer       = new MiniMapRenderer();
            var statusBarHeight       = screen.Height / 8;
            var statusBarWidth        = screen.Width;
            var statusBarSprite       = Sprite.Load(UiResourcesHelper.StatusBarSpritePath);
            var bowSprite             = Sprite.Load(UiResourcesHelper.BowMiniSpritePath);
            var frameSprite           = Sprite.Load(UiResourcesHelper.FrameSpritePath);
            var crossSprite           = options.CrossSpritePath == null ? null : Sprite.Load(options.CrossSpritePath);
            var arrowSprite           = Sprite.Load(UiResourcesHelper.ArrowSpritePath);
            var swordSprite           = Sprite.Load(UiResourcesHelper.SwordSpritePath);
            var fireBallSprite        = Sprite.Load(UiResourcesHelper.FireBallSpritePath);
            var shockBallSprite       = Sprite.Load(UiResourcesHelper.ShockBallSpritePath);
            var faceSprite            = Sprite.Load(UiResourcesHelper.FaceSprintPath);
            var faceHurtedSprite      = Sprite.Load(UiResourcesHelper.FaceHurtedSpritePath);
            var faceBadSprite         = Sprite.Load(UiResourcesHelper.FaceBadSpritePath);
            var logTextRenderer       = TextRenderer.Load(options.FontPath, screen.Height / 50);
            var notesTextRenderer     = TextRenderer.Load(options.FontPath, screen.Height / 50);
            var statusTextRenderer    = TextRenderer.Load(options.FontPath, screen.Height / 20);
            var notesRenderer         = new NotesRenderer(Sprite.Load(options.NotesSpritePath), notesTextRenderer, statusBarHeight);
            var monologueTextRenderer = TextRenderer.Load(options.FontPath, screen.Height / 50);
            var monologueRenderer     = new MonologueRenderer(monologueTextRenderer, statusBarHeight);
            var statusBarRenderer     = new StatusRenderer(
                statusBarSprite,
                crossSprite,
                statusBarHeight,
                logTextRenderer,
                notesRenderer,
                monologueRenderer,
                bowSprite,
                swordSprite,
                frameSprite,
                statusTextRenderer,
                arrowSprite,
                fireBallSprite,
                shockBallSprite,
                faceSprite,
                faceHurtedSprite,
                faceBadSprite);
            var textRenderer = options.FontPath == null ? null : TextRenderer.Load(options.FontPath, 24);

            return(new Engine(screen, controller, eventsCycle, sceneRenderer, miniMapRenderer, statusBarRenderer,
                              textRenderer));
        }
Example #8
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Please pass in a mp3 file");
            }

            if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Environment.Exit(1);
            }

            if (SDL_mixer.Mix_Init(SDL_mixer.MIX_InitFlags.MIX_INIT_MP3) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Environment.Exit(1);
            }

            IntPtr window = SDL.SDL_CreateWindow("Yay", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, 1024, 600, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN);

            if (window == IntPtr.Zero)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Window");

                Environment.Exit(1);
            }

            IntPtr renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);

            if (renderer == IntPtr.Zero)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Renderer");

                Environment.Exit(1);
            }

            if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Mixer");

                Environment.Exit(1);
            }

            bool mp3Done = false;

            IntPtr joy = SDL_mixer.Mix_LoadMUS(args[0]);

            if (joy == IntPtr.Zero)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Mix_LoadMUS");

                Environment.Exit(1);
            }

            MPGImport.mpg123_init();

            Int32 err = 0;

            GCHandle handleErr = GCHandle.Alloc(err, GCHandleType.Pinned);

            IntPtr ptrErr = GCHandle.ToIntPtr(handleErr);

            IntPtr handle_mpg = MPGImport.mpg123_new(null, ptrErr);
            int    x          = MPGImport.mpg123_open(handle_mpg, args[0]);

            handleErr.Free();

            MPGImport.mpg123_format_none(handle_mpg);
            MPGImport.mpg123_format(handle_mpg, 44100, 2, (int)MPGImport.mpg123_enc_enum.MPG123_ENC_SIGNED_16);

            int FrameSize = MPGImport.mpg123_outblock(handle_mpg);

            byte[] Buffer        = new byte[FrameSize];
            int    lengthSamples = MPGImport.mpg123_length(handle_mpg);

            if (SDL_mixer.Mix_PlayMusic(joy, 0) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Mix_PlayMusic");

                Environment.Exit(1);
            }

            object fun = new object();

            List <Int16> LeftChannels  = new List <Int16>();
            List <Int16> RightChannels = new List <Int16>();

            SDL_mixer.Mix_HookMusic((uData, stream, len) => {
                byte[] mixData       = new byte[len];
                byte[] managedStream = new byte[len];

                Marshal.Copy(stream, managedStream, 0, len);

                IntPtr done = IntPtr.Zero;

                if (MPGImport.mpg123_read(handle_mpg, managedStream, len, out done) is int hello)
                {
                    if (hello == -12)
                    {
                        mp3Done = true;
                    }
                    else if (hello == 0)
                    {
                        // foreach (var b in managedStream.Select((value, idx) => (value, idx)).Where((value, idx) => idx % 4 == 0)) {
                        Int16 leftChannel;
                        Int16 rightChannel;

                        byte[] datum = new byte[2];

                        datum[0]    = managedStream[0];
                        datum[1]    = managedStream[1];
                        leftChannel = BitConverter.ToInt16(datum, 0);

                        datum[0]     = managedStream[2];
                        datum[1]     = managedStream[3];
                        rightChannel = BitConverter.ToInt16(datum, 0);

                        leftChannel  = (Int16)Math.Sqrt(leftChannel);
                        rightChannel = (Int16)Math.Sqrt(rightChannel);

                        lock (fun) {
                            LeftChannels.Add(leftChannel);
                            RightChannels.Add(rightChannel);
                        }
                        // }
                    }
                }

                Marshal.Copy(managedStream, 0, stream, len);
            }, IntPtr.Zero);

            SDL.SDL_Event e;
            bool          quit = false;

            while (!quit)
            {
                while (SDL.SDL_WaitEventTimeout(out e, 10) != 0)
                {
                    switch (e.type)
                    {
                    case SDL.SDL_EventType.SDL_KEYDOWN:
                        switch (e.key.keysym.sym)
                        {
                        case SDL.SDL_Keycode.SDLK_ESCAPE:
                            quit = true;

                            break;
                        }

                        break;

                    case SDL.SDL_EventType.SDL_QUIT:
                        quit = true;

                        break;
                    }
                }

                if (mp3Done)
                {
                    quit = true;
                }
                else
                {
                    SDL.SDL_RenderClear(renderer);
                    SDL.SDL_SetRenderDrawColor(renderer, 218, 112, 214, 255);

                    // 40 is perfect.. ;)
                    lock (fun) {
                        foreach (var channel in LeftChannels.Select((value, idx) => (value, idx)))
                        {
                            if (channel.idx > 40)
                            {
                                continue;
                            }

                            SDL.SDL_Rect rect = new SDL.SDL_Rect()
                            {
                                x = channel.idx * 20,
                                y = 300,
                                w = 15,
                                h = -channel.value
                            };

                            SDL.SDL_RenderDrawRect(renderer, ref rect);
                        }

                        if (LeftChannels.Count > 40)
                        {
                            LeftChannels.RemoveAt(0);
                        }

                        foreach (var channel in RightChannels.Select((value, idx) => (value, idx)))
                        {
                            if (channel.idx > 40)
                            {
                                continue;
                            }

                            SDL.SDL_Rect rect = new SDL.SDL_Rect()
                            {
                                x = channel.idx * 20,
                                y = 315,
                                w = 15,
                                h = channel.value
                            };

                            SDL.SDL_RenderDrawRect(renderer, ref rect);
                        }

                        if (RightChannels.Count > 40)
                        {
                            RightChannels.RemoveAt(0);
                        }
                    }

                    SDL.SDL_Rect r = new SDL.SDL_Rect()
                    {
                        x = 0,
                        y = 300,
                        w = 1024,
                        h = 10
                    };

                    // SDL.SDL_RenderClear(renderer);
                    // SDL.SDL_SetRenderDrawColor(renderer, 218, 112, 214, 255);
                    SDL.SDL_RenderDrawRect(renderer, ref r);

                    SDL.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
                    SDL.SDL_RenderPresent(renderer);
                }
            }

            SDL_mixer.Mix_FreeMusic(joy);

            SDL.SDL_Quit();
        }