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))); }
void ISerializeExplicit.ReadData(IDataReader reader) { int version; try { reader.ReadValue("version", out version); } catch (Exception) { version = Serialize_Version_Unknown; } string formatId; if (version == Serialize_Version_FormatId) { reader.ReadValue("formatId", out formatId); } else if (version == Serialize_Version_LayerPng) { formatId = ImageCodec.FormatPng; } else { throw new NotSupportedException(string.Format( "Unknown PixelData serialization version '{0}'. Can't load image data.", version)); } IImageCodec codec = ImageCodec.GetRead(formatId); if (codec == null) { throw new NotSupportedException(string.Format( "Unable to retrieve image codec for format '{0}'. Can't load image data.", formatId)); } byte[] dataBlock; reader.ReadValue("pixelData", out dataBlock); using (MemoryStream stream = new MemoryStream(dataBlock)) { PixelData pixelData = codec.Read(stream); this.data = pixelData.data; this.width = pixelData.width; this.height = pixelData.height; pixelData = null; } }
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))); }
public GenericGraphicResource RequestGraphicResource(string path, bool async = false) { GenericGraphicResource resource; if (!cachedGraphics.TryGetValue(path, out resource)) { #if UNCOMPRESSED_CONTENT string pathAbsolute = PathOp.Combine(DualityApp.DataDirectory, "Animations", path); #else string pathAbsolute = PathOp.Combine(DualityApp.DataDirectory, ".dz", "Animations", path); #endif SpriteJson json; using (Stream s = FileOp.Open(pathAbsolute + ".res", FileAccessMode.Read)) { lock (jsonParser) { json = jsonParser.Parse <SpriteJson>(s); } } resource = new GenericGraphicResource { FrameDimensions = new Point2(json.FrameSize[0], json.FrameSize[1]), FrameConfiguration = new Point2(json.FrameConfiguration[0], json.FrameConfiguration[1]), FrameDuration = (1f / json.FrameRate) * 5, FrameCount = json.FrameCount }; if (json.Hotspot != null) { resource.Hotspot = new Point2(json.Hotspot[0], json.Hotspot[1]); } if (json.Coldspot != null) { resource.Coldspot = new Point2(json.Coldspot[0], json.Coldspot[1]); resource.HasColdspot = true; } if (json.Gunspot != null) { resource.Gunspot = new Point2(json.Gunspot[0], json.Gunspot[1]); resource.HasGunspot = true; } PixelData pixelData; using (Stream s = FileOp.Open(pathAbsolute, FileAccessMode.Read)) { pixelData = imageCodec.Read(s); } // Use external palette if ((json.Flags & 0x01) != 0x00) { ColorRgba[] palette = paletteTexture.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)); // ToDo: Pinball sprites have strange palette (1-3 indices down), CandionV looks bad, other levels look different } }); } bool linearSampling = (json.Flags & 0x02) != 0x00; Pixmap map = new Pixmap(pixelData); map.GenerateAnimAtlas(resource.FrameConfiguration.X, resource.FrameConfiguration.Y, 0); if (async) { GenericGraphicResourceAsyncFinalize asyncFinalize = new GenericGraphicResourceAsyncFinalize(); asyncFinalize.TextureMap = map; string filenameNormal = pathAbsolute.Replace(".png", ".n.png"); if (FileOp.Exists(filenameNormal)) { asyncFinalize.TextureNormalMap = new Pixmap(imageCodec.Read(FileOp.Open(filenameNormal, FileAccessMode.Read))); } else { resource.TextureNormal = defaultNormalMap; } asyncFinalize.TextureWrap = json.TextureWrap; asyncFinalize.LinearSampling = linearSampling; resource.AsyncFinalize = asyncFinalize; } else { TextureMagFilter magFilter; TextureMinFilter minFilter; if (linearSampling) { magFilter = TextureMagFilter.Linear; minFilter = TextureMinFilter.LinearMipmapLinear; } else { magFilter = TextureMagFilter.Nearest; minFilter = TextureMinFilter.Nearest; } resource.Texture = new Texture(map, TextureSizeMode.NonPowerOfTwo, magFilter, minFilter, json.TextureWrap, json.TextureWrap); string filenameNormal = pathAbsolute.Replace(".png", ".n.png"); if (FileOp.Exists(filenameNormal)) { pixelData = imageCodec.Read(FileOp.Open(filenameNormal, FileAccessMode.Read)); resource.TextureNormal = new Texture(new Pixmap(pixelData), TextureSizeMode.NonPowerOfTwo, magFilter, minFilter, json.TextureWrap, json.TextureWrap); resource.TextureNormal.Res.DetachPixmap(); } else { resource.TextureNormal = defaultNormalMap; } } cachedGraphics[path] = resource; } resource.Referenced = true; return(resource); }
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); }