public EpisodeSelectSection() { JsonParser jsonParser = new JsonParser(); IImageCodec imageCodec = ImageCodec.GetRead(ImageCodec.FormatPng); foreach (string episode in DirectoryOp.GetDirectories(PathOp.Combine(DualityApp.DataDirectory, "Episodes"))) { string pathAbsolute = PathOp.Combine(episode, ".res"); if (FileOp.Exists(pathAbsolute)) { Episode json; using (Stream s = DualityApp.SystemBackend.FileSystem.OpenFile(pathAbsolute, FileAccessMode.Read)) { json = jsonParser.Parse <Episode>(s); } json.Token = PathOp.GetFileName(episode); if (!DirectoryOp.Exists(PathOp.Combine(episode, json.FirstLevel))) { continue; } EpisodeEntry entry; entry.Episode = json; if (json.PreviousEpisode != null) { int time = Preferences.Get <int>("EpisodeEnd_Time_" + json.PreviousEpisode); entry.IsAvailable = (time > 0); } else { entry.IsAvailable = true; } entry.CanContinue = Preferences.Get <byte[]>("EpisodeContinue_Misc_" + entry.Episode.Token) != null; string logoPath = PathOp.Combine(episode, ".png"); if (FileOp.Exists(logoPath)) { PixelData pixelData; using (Stream s = FileOp.Open(logoPath, FileAccessMode.Read)) { pixelData = imageCodec.Read(s); } Texture texture = new Texture(new Pixmap(pixelData), TextureSizeMode.NonPowerOfTwo); entry.Logo = new Material(DrawTechnique.Alpha, texture); } else { entry.Logo = null; } episodes.Add(entry); } } episodes.Sort((x, y) => x.Episode.Position.CompareTo(y.Episode.Position)); }
// ToDo: Move parameters to .config file, rework .config file format public BitmapFont(Canvas canvas, string path, int width, int height, int cols, int first, int last, int defaultSpacing) { this.canvas = canvas; #if UNCOMPRESSED_CONTENT string png = PathOp.Combine(DualityApp.DataDirectory, "Animations", path + ".png"); #else string png = PathOp.Combine(DualityApp.DataDirectory, ".dz", "Animations", path + ".png"); #endif string config = png + ".config"; IImageCodec imageCodec = ImageCodec.GetRead(ImageCodec.FormatPng); using (Stream s = FileOp.Open(png, FileAccessMode.Read)) { PixelData pixelData = imageCodec.Read(s); ColorRgba[] palette = ContentResolver.Current.Palette.Res.BasePixmap.Res.PixelData[0].Data; ColorRgba[] data = pixelData.Data; Parallel.ForEach(Partitioner.Create(0, data.Length), range => { for (int i = range.Item1; i < range.Item2; i++) { int colorIdx = data[i].R; data[i] = palette[colorIdx].WithAlpha(palette[colorIdx].A * data[i].A / (255f * 255f)); } }); Texture texture = new Texture(new Pixmap(pixelData), TextureSizeMode.NonPowerOfTwo, TextureMagFilter.Linear, TextureMinFilter.Linear); materialPlain = new Material(DrawTechnique.Alpha, texture); materialColor = new Material(ContentResolver.Current.RequestShader("Colorize"), texture); } byte[] widthFromFileTable = new byte[256]; using (Stream s = FileOp.Open(config, FileAccessMode.Read)) { s.Read(widthFromFileTable, 0, widthFromFileTable.Length); } this.height = height; spacing = defaultSpacing; uint charCode = 0; for (int i = first; i < last; i++, charCode++) { chars[i] = new Rect( (float)((i - first) % cols) / cols, (float)((i - first) / cols) / cols, widthFromFileTable[charCode], height); if (charCode > last || i >= 255) { break; } } }
internal static void InitDefaultContent() { IImageCodec codec = ImageCodec.GetRead(ImageCodec.FormatPng); if (codec == null) { Log.Core.WriteError( "Unable to retrieve image codec for format '{0}'. Can't initialize default {1} Resources.", ImageCodec.FormatPng, typeof(Pixmap).Name); return; } InitDefaultContent <Pixmap>(".png", stream => new Pixmap(codec.Read(stream))); }
public TileMap(ILevelHandler levelHandler, string tilesetPath, bool hasPit) { this.levelHandler = levelHandler; this.hasPit = hasPit; IImageCodec codec = ImageCodec.GetRead(ImageCodec.FormatPng); tileset = new TileSet(tilesetPath); if (!tileset.IsValid) { throw new InvalidDataException("Tileset is corrupted"); } triggerState = new BitArray(TriggerCount); }
internal static void InitDefaultContent() { IImageCodec codec = ImageCodec.GetRead(ImageCodec.FormatPng); if (codec == null) { Logs.Core.WriteError( "Unable to retrieve image codec for format '{0}'. Can't initialize default {1} Resources.", ImageCodec.FormatPng, typeof(Pixmap).Name); // Initialize default content with generic error instances, so // everything else can still work as expected. We logged the error, // and there's nothing anyone can do about this at runtime, so just // fail gracefully without causing more trouble. DefaultContent.InitType <Pixmap>(name => new Pixmap(new PixelData(1, 1, new ColorRgba(255, 0, 255)))); return; } DefaultContent.InitType <Pixmap>(".png", stream => new Pixmap(codec.Read(stream))); }
private ContentResolver() { jsonParser = new JsonParser(); imageCodec = ImageCodec.GetRead(ImageCodec.FormatPng); #if !UNCOMPRESSED_CONTENT string dz = PathOp.Combine(DualityApp.DataDirectory, ".dz"); PathOp.Mount(dz, new CompressedContent(dz)); #endif defaultNormalMap = new Texture(new Pixmap(new PixelData(2, 2, new ColorRgba(0.5f, 0.5f, 1f))), TextureSizeMode.Default, TextureMagFilter.Nearest, TextureMinFilter.Nearest); cachedMetadata = new Dictionary <string, Metadata>(); cachedGraphics = new Dictionary <string, GenericGraphicResource>(); cachedShaders = new Dictionary <string, ContentRef <DrawTechnique> >(); //cachedSounds = new Dictionary<string, ContentRef<Sound>>(); basicNormal = RequestShader("BasicNormal"); paletteNormal = RequestShader("PaletteNormal"); AllowAsyncLoading(); }
private void PrerenderTexturedBackground() { try { IImageCodec codec = ImageCodec.GetRead(ImageCodec.FormatPng); // Try to use "The Secret Files" background string levelPath = PathOp.Combine(DualityApp.DataDirectory, "Episodes", "secretf", "01_easter1"); if (!DirectoryOp.Exists(levelPath)) { // Try to use "Base Game" background levelPath = PathOp.Combine(DualityApp.DataDirectory, "Episodes", "prince", "03_carrot1"); if (!DirectoryOp.Exists(levelPath)) { // Try to use "Holiday Hare '98" background levelPath = PathOp.Combine(DualityApp.DataDirectory, "Episodes", "xmas98", "03_xmas3"); if (!DirectoryOp.Exists(levelPath)) { // Try to use "Christmas Chronicles" background levelPath = PathOp.Combine(DualityApp.DataDirectory, "Episodes", "xmas99", "03_xmas3"); if (!DirectoryOp.Exists(levelPath)) { // Try to use "Shareware Demo" background; levelPath = PathOp.Combine(DualityApp.DataDirectory, "Episodes", "share", "02_share2"); if (!DirectoryOp.Exists(levelPath)) { // No usable background found throw new FileNotFoundException(); } } } } } // Load metadata JsonParser json = new JsonParser(); LevelHandler.LevelConfigJson config; using (Stream s = FileOp.Open(PathOp.Combine(levelPath, ".res"), FileAccessMode.Read)) { config = json.Parse <LevelHandler.LevelConfigJson>(s); } LevelHandler.LevelConfigJson.LayerSection layer; if (config.Layers.TryGetValue("Sky", out layer)) { if (layer.BackgroundColor != null && layer.BackgroundColor.Count >= 3) { horizonColor = new Vector4(layer.BackgroundColor[0] / 255f, layer.BackgroundColor[1] / 255f, layer.BackgroundColor[2] / 255f, 1f); } switch ((BackgroundStyle)layer.BackgroundStyle) { case BackgroundStyle.Sky: default: texturedBackgroundShader = ContentResolver.Current.RequestShader("TexturedBackground"); break; case BackgroundStyle.Circle: texturedBackgroundShader = ContentResolver.Current.RequestShader("TexturedBackgroundCircle"); break; } } // Render background layer to texture string tilesetPath = PathOp.Combine(DualityApp.DataDirectory, "Tilesets", config.Description.DefaultTileset); ColorRgba[] tileMapPalette = TileSet.LoadPalette(PathOp.Combine(tilesetPath, ".palette")); ContentResolver.Current.ApplyBasePalette(tileMapPalette); TileSet levelTileset = new TileSet(config.Description.DefaultTileset); if (!levelTileset.IsValid) { throw new InvalidDataException(); } using (Stream s = FileOp.Open(PathOp.Combine(levelPath, "Sky.layer"), FileAccessMode.Read)) { using (DeflateStream deflate = new DeflateStream(s, CompressionMode.Decompress)) using (BinaryReader r = new BinaryReader(deflate)) { int width = r.ReadInt32(); int height = r.ReadInt32(); TileMapLayer newLayer = new TileMapLayer(); newLayer.Layout = new LayerTile[width * height]; for (int i = 0; i < newLayer.Layout.Length; i++) { ushort tileType = r.ReadUInt16(); byte flags = r.ReadByte(); if (flags == 0) { newLayer.Layout[i] = levelTileset.GetDefaultTile(tileType); continue; } bool isFlippedX = (flags & 0x01) > 0; bool isFlippedY = (flags & 0x02) > 0; bool isAnimated = (flags & 0x04) > 0; bool legacyTranslucent = (flags & 0x80) > 0; // Invalid tile numbers (higher than tileset tile amount) are silently changed to empty tiles if (tileType >= levelTileset.TileCount && !isAnimated) { tileType = 0; } LayerTile tile; // Copy the default tile and do stuff with it tile = levelTileset.GetDefaultTile(tileType); tile.IsFlippedX = isFlippedX; tile.IsFlippedY = isFlippedY; tile.IsAnimated = isAnimated; if (legacyTranslucent) { tile.MaterialAlpha = /*127*/ 140; } newLayer.Layout[i] = tile; } newLayer.LayoutWidth = width; RecreateTexturedBackground(levelTileset, ref newLayer); } } } catch (Exception ex) { Console.WriteLine("Cannot prerender textured background: " + ex); cachedTexturedBackground = new Texture(new Pixmap(new PixelData(2, 2, ColorRgba.Black))); } }
private void InitializeInput() { if (virtualButtons != null) { // It's already initialized... return; } DualityApp.Keyboard.Source = new KeyboardInputSource(this); //DualityApp.Gamepads.AddSource(new GamepadInputSource(this)); const float dpadLeft = 0.02f; const float dpadTop = 0.58f; const float dpadWidth = 0.2f; const float dpadHeight = 0.37f; const float dpadThresholdX = 0.05f; const float dpadThresholdY = 0.09f; IImageCodec imageCodec = ImageCodec.GetRead(ImageCodec.FormatPng); AssetManager assets = Context.Assets; Material matDpad, matFire, matJump, matRun, matSwitchWeapon; using (Stream s = assets.Open("dpad.png")) { matDpad = new Material(DrawTechnique.Alpha, new Texture(new Pixmap(imageCodec.Read(s)), TextureSizeMode.NonPowerOfTwo)); } using (Stream s = assets.Open("fire.png")) { matFire = new Material(DrawTechnique.Alpha, new Texture(new Pixmap(imageCodec.Read(s)), TextureSizeMode.NonPowerOfTwo)); } using (Stream s = assets.Open("jump.png")) { matJump = new Material(DrawTechnique.Alpha, new Texture(new Pixmap(imageCodec.Read(s)), TextureSizeMode.NonPowerOfTwo)); } using (Stream s = assets.Open("run.png")) { matRun = new Material(DrawTechnique.Alpha, new Texture(new Pixmap(imageCodec.Read(s)), TextureSizeMode.NonPowerOfTwo)); } using (Stream s = assets.Open("switch.png")) { matSwitchWeapon = new Material(DrawTechnique.Alpha, new Texture(new Pixmap(imageCodec.Read(s)), TextureSizeMode.NonPowerOfTwo)); } virtualButtons = new[] { new VirtualButton { Left = dpadLeft, Top = dpadTop, Width = dpadWidth, Height = dpadHeight, Material = matDpad, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.Left, Left = dpadLeft - dpadThresholdX, Top = dpadTop, Width = (dpadWidth / 3) + dpadThresholdX, Height = dpadHeight, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.Right, Left = (dpadLeft + (dpadWidth * 2 / 3)), Top = dpadTop, Width = (dpadWidth / 3) + dpadThresholdX, Height = dpadHeight, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.Up, Left = dpadLeft, Top = dpadTop - dpadThresholdY, Width = dpadWidth, Height = (dpadHeight / 3) + dpadThresholdY, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.Down, Left = dpadLeft, Top = (dpadTop + (dpadHeight * 2 / 3)), Width = dpadWidth, Height = (dpadHeight / 3) + dpadThresholdY, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.Space, Left = 0.68f, Top = 0.79f, Width = 0.094f, Height = 0.168f, Material = matFire, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.V, Left = 0.785f, Top = 0.71f, Width = 0.094f, Height = 0.168f, Material = matJump, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.C, Left = 0.89f, Top = 0.64f, Width = 0.094f, Height = 0.168f, Material = matRun, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.X, Left = 0.83f, Top = 0.57f, Width = 0.055f, Height = 0.096f, Material = matSwitchWeapon, CurrentPointerId = -1 }, #if DEBUG new VirtualButton { KeyCode = Key.D, Left = 0.8f, Top = 0.1f, Width = 0.06f, Height = 0.1f, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.N, Left = 0.9f, Top = 0.1f, Width = 0.08f, Height = 0.16f, CurrentPointerId = -1 }, #endif new VirtualButton { KeyCode = Key.Enter, Left = 0.68f, Top = 0.79f, Width = 0.094f, Height = 0.17f, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.Enter, Left = 0.785f, Top = 0.71f, Width = 0.094f, Height = 0.17f, CurrentPointerId = -1 }, new VirtualButton { KeyCode = Key.Enter, Left = 0.89f, Top = 0.64f, Width = 0.094f, Height = 0.17f, CurrentPointerId = -1 }, }; showVirtualButtons = true; allowVibrations = Preferences.Get("Vibrations", true); }