示例#1
0
        internal static int initialiser_son()
        {
            if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, SDL_mixer.MIX_DEFAULT_CHANNELS, 1024) < 0)
            {
                Program.MessageErr("Mix_OpenAudio: ERREUR ({0})\n", SDL.SDL_GetError());
                SDL.SDL_Quit();
                return(1);
            }

            if (prechargerSons() > 0)
            {
                Program.MessageErr("prechargerSons: ERREUR ({0})\n", SDL.SDL_GetError());
                SDL_mixer.Mix_CloseAudio();
                SDL.SDL_Quit();
                return(2);
            }

            if (prechargerMusiques() > 0)
            {
                Program.MessageErr("prechargerMusiques: ERREUR ({0})\n", SDL.SDL_GetError());
                SDL_mixer.Mix_CloseAudio();
                SDL.SDL_Quit();
                return(3);
            }

            SDL_mixer.Mix_AllocateChannels(sons.Count);
            ArreterJouerSons();
            ReglerVolumeSons(50);
            ReglerVolumeMusique(50);

            return(0);
        }
示例#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();
    }
示例#3
0
文件: SDLSounds.cs 项目: Jebeli/Tiles
 public SDLSounds()
 {
     playback = new Dictionary <int, Playback>();
     channels = new Dictionary <string, int>();
     if (SDL_mixer.Mix_OpenAudio(22050, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 1024) != 0)
     {
         Logger.Error("Sound", $"Error during Mix_OpenAudio: {SDL.SDL_GetError()}");
     }
     else
     {
         Logger.Info("Sound", $"Using SDLSoundManager (SDL2, {SDL.SDL_GetCurrentAudioDriver()})");
         SDL_mixer.Mix_AllocateChannels(128);
         channelFinished = new SDL_mixer.ChannelFinishedDelegate(OnChannelFinished);
         SetSoundVolume(soundVolume);
     }
 }
    public static void Main()
    {
        int    audio_rate     = 44100;
        ushort audio_format   = SDL.AUDIO_S16SYS;
        int    audio_channels = 2;
        int    audio_buffers  = 4096;
        int    audio_volume   = SDL_mixer.MIX_MAX_VOLUME;
        int    timeleft       = 50;

        SDL.SDL_Init(SDL.SDL_INIT_AUDIO);
        if (SDL_mixer.Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0)
        {
            return;
        }
        else
        {
            SDL_mixer.Mix_QuerySpec(out audio_rate, out audio_format, out audio_channels);
        }
        SDL_mixer.Mix_AllocateChannels(32);
        SDL_mixer.Mix_VolumeMusic(audio_volume / 3);

        System.IntPtr music = SDL_mixer.Mix_LoadMUS("Tetris_theme.ogg");
        System.IntPtr wave  = SDL_mixer.Mix_LoadWAV("Meow.ogg");
        SDL_mixer.Mix_FadeInMusic(music, 1, 2000);
        while (SDL_mixer.Mix_PlayingMusic() != 0)
        {
            SDL.SDL_Delay(100);
            timeleft--;
            if (timeleft == 0)
            {
                break;
            }
            if (timeleft == 25)
            {
                SDL_mixer.Mix_PlayChannel(-1, wave, 0);
            }
        }
        SDL_mixer.Mix_FadeOutMusic(2000);
        SDL_mixer.Mix_FreeMusic(music);
        SDL_mixer.Mix_FreeChunk(wave);
        SDL.SDL_Delay(500);
        SDL_mixer.Mix_CloseAudio();
        SDL.SDL_Quit();
    }
示例#5
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;
 }
示例#6
0
        /**
         * Used to initialize audio using SDL and SDL_mixer. This handles loading the sound effect WAV
         * files from disk and initializing the audio channels.
         */
        public void InitializeAudio(Dictionary <SoundEffect, String> soundEffectsFiles = null)
        {
            var audioRate     = 22050;            // 22.05KHz
            var audioFormat   = SDL.AUDIO_S16SYS; // Unsigned 16-bit samples in the system's byte order
            var audioChannels = 1;                // Mono
            var audioBuffers  = 4096;             // 4KB

            var openAudioResult = SDL_mixer.Mix_OpenAudio(audioRate, audioFormat, audioChannels, audioBuffers);

            if (openAudioResult != 0)
            {
                throw new Exception(String.Format("Failure while opening SDL audio mixer. SDL Error: {0}", SDL.SDL_GetError()));
            }

            // We'll use 3 channels; see AUDIO_CHANNEL constants.
            var allocateChannelsResult = SDL_mixer.Mix_AllocateChannels(3);

            if (allocateChannelsResult == 0)
            {
                throw new Exception(String.Format("Failure while allocating SDL audio mixer channels. SDL Error: {0}", SDL.SDL_GetError()));
            }

            soundEffects = new Dictionary <SoundEffect, IntPtr>();

            foreach (var entry in soundEffectsFiles)
            {
                var sfx      = entry.Key;
                var filePath = entry.Value;

                var pointer = SDL_mixer.Mix_LoadWAV(filePath);

                if (pointer == null)
                {
                    throw new Exception(String.Format("Error loading sound {0}. SDL Error: {1}", sfx, SDL.SDL_GetError()));
                }

                soundEffects.Add(sfx, pointer);
            }
        }
示例#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));
        }