Exemple #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);
        }
        private byte[] musicBytes; // Cannot be local or GC will destory it with great anger

        public SDL2SoundProvider()
        {
            if (SDL_mixer.Mix_OpenAudio(22050, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 1024) < 0)
            {
                log.Error($"SDL_mixer could not initialize! SDL_mixer Error: {SDL.SDL_GetError()}");
            }
        }
Exemple #3
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();
        }
Exemple #4
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();
    }
 public SDL2MusicPlayer()
 {
     if (SDL_mixer.Mix_OpenAudio(22050, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048) < 0)
     {
         log.Error($"SDL_mixer could not initialize! SDL_mixer Error: {SDL.SDL_GetError()}");
         return;
     }
 }
Exemple #6
0
        public CORE()
        {
            SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO);
            win = SDL.SDL_CreateWindow("SDL2 Sharp", 50, 50, 1280, 720, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN);
            ren = SDL.SDL_CreateRenderer(win, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);

            SDL_mixer.Mix_OpenAudio(
                SDL_mixer.MIX_DEFAULT_FREQUENCY, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 128);
        }
Exemple #7
0
 private static void initializeMixer()
 {
     if (!initialized)
     {
         SDL.SDL_InitSubSystem(SDL.SDL_INIT_AUDIO);
         SDL_mixer.Mix_OpenAudio(44100, SDL.AUDIO_S16SYS, 2, 1024);
         initialized = true;
     }
 }
Exemple #8
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.");
            }
        }
Exemple #9
0
        private static void InitSDL2()
        {
            if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0)
            {
                Console.WriteLine("SDL could not initialize! SDL_Error: {0}", SDL.SDL_GetError());
            }
            else
            {
                Console.WriteLine("SDL initialized!");
            }

            // OPTIONAL: init SDL_image
            var imgFlags = SDL_image.IMG_InitFlags.IMG_INIT_JPG | SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_WEBP;

            if ((SDL_image.IMG_Init(imgFlags) > 0 & imgFlags > 0) == false)
            {
                Console.WriteLine("SDL_image could not initialize! SDL_image Error: {0}", SDL.SDL_GetError());
            }
            else
            {
                Console.WriteLine("SDL_image initialized!");
            }

            // OPTIONAL: init SDL_ttf

            if (SDL_ttf.TTF_Init() == -1)
            {
                Console.WriteLine("SDL_ttf could not initialize! SDL_image Error: {0}", SDL.SDL_GetError());
            }
            else
            {
                Console.WriteLine("SDL_ttf initialized!");
            }

            // OPTIONAL: init SDL_mixer
            if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048) < 0)
            {
                Console.WriteLine("SDL_mixer could not initialize! SDL_image Error: {0}", SDL.SDL_GetError());
            }
            else
            {
                Console.WriteLine("SDL_mixer initialized!");
            }

            if (SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "1") == SDL.SDL_bool.SDL_FALSE)
            {
                Console.WriteLine("Warning: Linear texture filtering not enabled!");
            }
        }
Exemple #10
0
 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();
    }
Exemple #12
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;
 }
Exemple #13
0
        public UxContext(
            string windowTitle,
            IUxConfig uxConfig,
            string imagesPath,
            string audiosPath,
            string fontsPath
            )
        {
            Title    = windowTitle;
            UxConfig = uxConfig;
            OnWindowResize(uxConfig.ScreenSize);

            var sdlWindowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE;

            if (uxConfig.Maximized)
            {
                sdlWindowFlags |= SDL.SDL_WindowFlags.SDL_WINDOW_MAXIMIZED;
            }

            SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO);
            SDL_ttf.TTF_Init();

            Window = SDL.SDL_CreateWindow(
                windowTitle,
                SDL.SDL_WINDOWPOS_CENTERED,
                SDL.SDL_WINDOWPOS_CENTERED,
                ScreenSize.Width, ScreenSize.Height,
                sdlWindowFlags
                );

            WRenderer = SDL.SDL_CreateRenderer(Window, -1, 0);
            SDL.SDL_SetRenderDrawColor(WRenderer, 0, 0, 0, 0);
            SDL.SDL_SetRenderDrawBlendMode(WRenderer, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);

            SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048);

            TextureProvider = new TextureProvider(WRenderer, imagesPath);
            AudioProvider   = new AudioProvider(audiosPath);
            FontProvider    = new FontProvider(fontsPath);
        }
Exemple #14
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);
            }
        }
Exemple #15
0
        private static bool Init()
        {
            //Initialization flag
            bool success = true;

            //Initialize SDL
            if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO) < 0)
            {
                Console.WriteLine("SDL could not initialize! SDL_Error: {0}", SDL.SDL_GetError());
                success = false;
            }
            else
            {
                //Set texture filtering to linear
                if (SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "1") == SDL.SDL_bool.SDL_FALSE)
                {
                    Console.WriteLine("Warning: Linear texture filtering not enabled!");
                }

                //Create window
                _Window = SDL.SDL_CreateWindow("SDL Tutorial", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED,
                                               SCREEN_WIDTH, SCREEN_HEIGHT, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN);
                if (_Window == IntPtr.Zero)
                {
                    Console.WriteLine("Window could not be created! SDL_Error: {0}", SDL.SDL_GetError());
                    success = false;
                }
                else
                {
                    //Create vsynced renderer for window
                    var renderFlags = SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC;
                    Renderer = SDL.SDL_CreateRenderer(_Window, -1, renderFlags);
                    if (Renderer == IntPtr.Zero)
                    {
                        Console.WriteLine("Renderer could not be created! SDL Error: {0}", SDL.SDL_GetError());
                        success = false;
                    }
                    else
                    {
                        //Initialize renderer color
                        SDL.SDL_SetRenderDrawColor(Renderer, 0xFF, 0xFF, 0xFF, 0xFF);

                        //Initialize PNG loading
                        var imgFlags = SDL_image.IMG_InitFlags.IMG_INIT_PNG;
                        if ((SDL_image.IMG_Init(imgFlags) > 0 & imgFlags > 0) == false)
                        {
                            Console.WriteLine("SDL_image could not initialize! SDL_image Error: {0}", SDL.SDL_GetError());
                            success = false;
                        }

                        //Initialize SDL_mixer
                        if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048) < 0)
                        {
                            Console.WriteLine("SDL_mixer could not initialize! SDL_mixer Error: {0}", SDL.SDL_GetError());
                            success = false;
                        }
                    }
                }
            }

            return(success);
        }
        public static void SDL_Main()
        {
            // Example code.

            // OPTIONAL: Hide action bar (top bar). Otherwise it just shows the window title.
            // MainActivity.Instance.RunOnUiThread(MainActivity.Instance.ActionBar.Hide);

            // OPTIONAL: Fullscreen (immersive), handled by the activity
            MainActivity.SDL2DCS_Fullscreen = true;

            if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0)
            {
                Console.WriteLine("SDL could not initialize! SDL_Error: {0}", SDL.SDL_GetError());
            }
            else
            {
                Console.WriteLine("SDL initialized!");
            }

            // OPTIONAL: init SDL_image
            var imgFlags = SDL_image.IMG_InitFlags.IMG_INIT_JPG | SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_WEBP;

            if ((SDL_image.IMG_Init(imgFlags) > 0 & imgFlags > 0) == false)
            {
                Console.WriteLine("SDL_image could not initialize! SDL_image Error: {0}", SDL.SDL_GetError());
            }
            else
            {
                Console.WriteLine("SDL_image initialized!");
            }

            // OPTIONAL: init SDL_ttf

            if (SDL_ttf.TTF_Init() == -1)
            {
                Console.WriteLine("SDL_ttf could not initialize! SDL_image Error: {0}", SDL.SDL_GetError());
            }
            else
            {
                Console.WriteLine("SDL_ttf initialized!");
            }

            // OPTIONAL: init SDL_mixer
            if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048) < 0)
            {
                Console.WriteLine("SDL_mixer could not initialize! SDL_image Error: {0}", SDL.SDL_GetError());
            }
            else
            {
                Console.WriteLine("SDL_mixer initialized!");
            }

            // OPTIONAL: Get WM. Required to set the backbuffer size to the screen size
            DisplayMetrics dm = new DisplayMetrics();

            MainActivity.SDL2DCS_Instance.WindowManager.DefaultDisplay.GetMetrics(dm);

            if (SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "1") == SDL.SDL_bool.SDL_FALSE)
            {
                Console.WriteLine("Warning: Linear texture filtering not enabled!");
            }


            IntPtr window = SDL.SDL_CreateWindow(
                "HEY, LISTEN!",
                SDL.SDL_WINDOWPOS_CENTERED,
                SDL.SDL_WINDOWPOS_CENTERED,
                dm.WidthPixels,
                dm.HeightPixels,
                SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL |
                SDL.SDL_WindowFlags.SDL_WINDOW_INPUT_FOCUS |
                SDL.SDL_WindowFlags.SDL_WINDOW_MOUSE_FOCUS
                );

            if (window == IntPtr.Zero)
            {
                Console.WriteLine("Window could not be created! SDL_Error: {0}", SDL.SDL_GetError());
            }
            else
            {
                Console.WriteLine("Window created!");
            }

            //Test code

            TestCode tc = new TestCode(window, dm.WidthPixels, dm.HeightPixels);

            //selector
            const bool gl_test = false;

            if (gl_test)
            {
                //GL Test Code
                tc.GL_Init();
                tc.GL_Loop();
            }
            else
            {
                //Test of SDL_image, SDL_ttf, SDL_mixer
                tc.Init();
                tc.Loop();
            }

            SDL.SDL_Quit();
        }
Exemple #17
0
        static void Main(string[] args)
        {
            if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO) < 0)
            {
                Console.WriteLine("Error initializing SDL");
                SDL.SDL_Quit();
                return;
            }

            // LUNA: Initialize with OpenGL 3.2, so we can debug graphics with RenderDoc
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 2);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);

            var window = SDL.SDL_CreateWindow(
                "Disaster Engine Again",
                SDL.SDL_WINDOWPOS_UNDEFINED,
                SDL.SDL_WINDOWPOS_UNDEFINED,
                ScreenController.windowWidth, ScreenController.windowHeight,
                SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL
                );

            if (window == IntPtr.Zero)
            {
                Console.WriteLine($"There was an issue creating the window. {SDL.SDL_GetError()}");
            }

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

            if (renderer == IntPtr.Zero)
            {
                Console.WriteLine($"There was an issue creating the renderer. {SDL.SDL_GetError()}");
            }

            SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "0");

            Console.WriteLine($"Welcome to disaster engine");

            var frameStart = DateTime.UtcNow.Ticks;

            // software renderer initialisation
            SoftwareCanvas.InitTexture(renderer, 320, 240);
            LoadConfig();
            SoftwareCanvas.LoadFont(Assets.LoadPath("fontsmall.png"));

            screen = new ScreenController(window);
            LoadingMessage("disaster engine 5.0");
            LoadingMessage("(c) jazz mickle ultramegacorp 2021");
            LoadingMessage("initialised screen");
            var js = new JS();

            double ms    = 0;
            int    frame = 0;

            LoadingMessage("building input collection");
            DisasterAPI.Input.keyState = new System.Collections.Generic.Dictionary <SDL.SDL_Keycode, (bool down, bool held, bool up)>();

            LoadingMessage("opening audio");
            SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 1024);

            LoadingMessage("complete");

            System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.LowLatency;

            long ticks;
            long t;

            Debug.enabled = false;

            while (running)
            {
                frame += 1;

                Debug.FrameStart();

                ticks      = DateTime.UtcNow.Ticks;
                t          = ticks - frameStart;
                ms         = t / 10000.0;
                frameStart = ticks;

                while (SDL.SDL_PollEvent(out SDL.SDL_Event e) == 1)
                {
                    switch (e.type)
                    {
                    case SDL.SDL_EventType.SDL_QUIT:
                        running = false;
                        break;

                    case SDL.SDL_EventType.SDL_KEYDOWN:
                        DisasterAPI.Input.keyState[e.key.keysym.sym] = (true, true, DisasterAPI.Input.GetKeyUp(e.key.keysym.sym));
                        break;

                    case SDL.SDL_EventType.SDL_KEYUP:
                        DisasterAPI.Input.keyState[e.key.keysym.sym] = (DisasterAPI.Input.GetKeyDown(e.key.keysym.sym), false, true);
                        break;
                    }
                }

                Debug.Label("sdl events");

                double delta = ms * .001 * timescale;
                js.Update(delta);

                Debug.Label("js update");
                Debug.DrawGraph();
                screen.Update();

                Debug.Label("render update");

                DisasterAPI.Input.Clear();

                SDL.SDL_SetWindowTitle(window, $"DISASTER ENGINE 5 -- MS: {Math.Floor(ms)}");

                if (ms > 20)
                {
                    //Console.WriteLine("HElo");
                    System.Diagnostics.Debugger.Log(0, "hitch", $"took {ms} this frame\n");
                }
                //if (frame % 30 == 0)

                if (GC.GetTotalMemory(false) > 10000000)
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }

                Debug.Label("gc");

                Debug.FrameEnd();
                Debug.GetFrameMSData();
            }

            screen.Done();

            // Clean up the resources that were created.
            Assets.Dispose();

            SDL_mixer.Mix_CloseAudio();
            SDL.SDL_DestroyRenderer(renderer);
            SDL.SDL_DestroyTexture(SoftwareCanvas.drawTexture);
            SDL.SDL_DestroyWindow(window);
            SDL.SDL_Quit();
        }
Exemple #18
0
 public static void OpenAudio(ushort Format, int Channels, int ChunkSize)
 {
     Util.ThrowIfResultIsError(SDL_mixer.Mix_OpenAudio(SDL_mixer.MIX_DEFAULT_FREQUENCY, Format, Channels, ChunkSize));
 }
Exemple #19
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));
        }
Exemple #20
0
 public static void OpenAudio(int Frequerncy, ushort Format, int Channels, int ChunkSize)
 {
     Util.ThrowIfResultIsError(SDL_mixer.Mix_OpenAudio(Frequerncy, Format, Channels, ChunkSize));
 }
Exemple #21
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();
        }
Exemple #22
0
 public static bool TryOpenAudio(ushort Format, int Channels, int ChunkSize)
 {
     return(SDL_mixer.Mix_OpenAudio(SDL_mixer.MIX_DEFAULT_FREQUENCY, Format, Channels, ChunkSize) == 0);
 }
Exemple #23
0
 public static bool TryOpenAudio(int Frequerncy, ushort Format, int Channels, int ChunkSize)
 {
     return(SDL_mixer.Mix_OpenAudio(Frequerncy, Format, Channels, ChunkSize) == 0);
 }