Exemplo n.º 1
0
 public GraphicsProvider(
     ISdl2 sdl,
     IOpenGl gl,
     IntPtr windowPointer)
 {
     _sdl           = sdl;
     Graphics       = gl;
     _windowPointer = windowPointer;
 }
Exemplo n.º 2
0
        public GameLoopManager(
            ILogger <GameLoopManager> logger,
            ISdl2 sdl,
            IGameLoop gameLoop)
        {
            _logger   = logger;
            _sdl      = sdl;
            _gameLoop = gameLoop;

            _thread = new Thread(RunGameLoopInBackground);
            _thread.Start();
        }
Exemplo n.º 3
0
    internal Window(
        ISdl2 sdl,
        IOpenGl openGl,
        IWindowEventHandler windowEventHandler,
        IntPtr windowPointer,
        IntPtr contextPointer,
        uint windowId)
    {
        Sdl = sdl;
        WindowEventHandler = windowEventHandler;
        WindowPointer      = windowPointer;
        ContextPointer     = contextPointer;
        WindowId           = windowId;

        _graphicsProvider = new GraphicsProvider(sdl, openGl, windowPointer);
    }
Exemplo n.º 4
0
        private static IntPtr CreateWindowPtr(
            ISdl2 sdl,
            ILogger logger,
            string title,
            int width,
            int height,
            bool fullscreen)
        {
            var flags = SdlWindow.OpenGl;
            var pos   = SdlWindowPos.Centered;

            var result = sdl.GetDesktopDisplayMode(0, out var mode);

            if (result < 0)
            {
                throw new SdlException("Unable to get desktop display mode: " + sdl.GetError());
            }

            logger.LogDebug($"Detected display of {mode.w}x{mode.h}.");

            if (fullscreen)
            {
                pos    = 0;
                width  = mode.w;
                height = mode.h;
            }
            else
            {
                flags |= SdlWindow.Resizable | SdlWindow.Shown;
            }

            var windowPtr = sdl.CreateWindow(
                title,
                pos,
                pos,
                width,
                height,
                flags);

            if (windowPtr.IsInvalid())
            {
                throw new SdlException($"Unable to create window ({windowPtr}): {sdl.GetError()}");
            }

            return(windowPtr);
        }
Exemplo n.º 5
0
        static void CreateSheetsFromFolder(
            ILogger logger,
            ISdl2 sdl,
            IStb stb,
            Point32 sheetSize,
            string inputFolder,
            string outputFolder)
        {
            var surfaces      = new List <IntPtr>();
            var nameBySurface = new Dictionary <IntPtr, string>();
            var folders       = new Stack <string>();

            folders.Push(inputFolder);

            while (folders.TryPop(out var folder))
            {
                logger.LogDebug("Processing folder {0}", folder);

                foreach (var innerFolder in Directory.EnumerateDirectories(folder))
                {
                    folders.Push(innerFolder);
                }

                foreach (var pngFile in Directory.EnumerateFiles(folder, "*.png"))
                {
                    logger.LogDebug("Loading {0}", pngFile);
                    var pngBytes   = File.ReadAllBytes(pngFile);
                    var pixelBytes = stb.StbiLoadFromMemory(
                        pngBytes[0],
                        pngBytes.Length,
                        out var width,
                        out var height,
                        out var comp,
                        4);

                    if (pixelBytes.IsInvalid())
                    {
                        throw new Exception("Unable to load PNG.");
                    }

                    var surface = sdl.CreateRGBSurfaceFrom(
                        pixelBytes,
                        width,
                        height,
                        32,
                        4 * width,
                        Platform.Rmask,
                        Platform.Gmask,
                        Platform.Bmask,
                        Platform.Amask);

                    if (surface.IsInvalid())
                    {
                        stb.StbiImageFree(pixelBytes);
                        throw new SdlException("Unable to create surface: " + sdl.GetError());
                    }

                    surfaces.Add(surface);
                    nameBySurface.Add(surface, Path.GetFileNameWithoutExtension(pngFile));
                }
            }

            surfaces.Sort(
                (a, b) =>
            {
                var viewA = new SurfaceView(a);
                var viewB = new SurfaceView(b);
                var areaA = viewA.Width * viewA.Height;
                var areaB = viewB.Width * viewB.Height;
                return(-areaA.CompareTo(areaB));    // Sort descending!
            });

            var infoPath = Path.Combine(outputFolder, "info.json");
            var options  = new JsonWriterOptions
            {
                Indented       = true,
                SkipValidation = true
            };

            using (var builder = new SheetImageBuilder(stb, sdl, sheetSize))
                using (var fileStream = File.Create(infoPath))
                    using (var writer = new Utf8JsonWriter(fileStream, options))
                    {
                        writer.WriteStartObject();
                        writer.WriteNumber("width", sheetSize.X);
                        writer.WriteNumber("height", sheetSize.Y);
                        writer.WritePropertyName("sprites");
                        writer.WriteStartArray();
                        foreach (var surface in surfaces)
                        {
                            var position = builder.Add(surface);
                            writer.WriteStartObject();
                            writer.WriteString("name", nameBySurface[surface]);
                            writer.WriteNumber("sheetIndex", position.SheetIndex);
                            writer.WriteNumber("x", position.Rectangle.Position.X);
                            writer.WriteNumber("y", position.Rectangle.Position.Y);
                            writer.WriteNumber("width", position.Rectangle.Size.X);
                            writer.WriteNumber("height", position.Rectangle.Size.Y);
                            writer.WriteEndObject();
                            writer.Flush();
                        }
                        writer.WriteEndArray();
                        writer.WriteEndObject();
                        builder.SaveImages(outputFolder);
                    }

            foreach (var surface in surfaces)
            {
                var view = new SurfaceView(surface);
                stb.StbiImageFree(view.Pixels);
                sdl.FreeSurface(surface);
            }
        }