public static Texture2D GetTexture(byte[] file, GraphicsDevice device) { Texture2D tmpTex; DDSLib.DDSFromStream(new MemoryStream(file), device, 20, false, out tmpTex); return(tmpTex); }
public bool Load() { if (Loaded) { return(true); } var extension = Path.GetExtension(Name); switch (extension) { case ".png": case ".jpg": case ".gif": { var stream = FileSystem.OpenCopy(Name); try { Texture = Texture2D.FromStream(Renderer.Device, stream); } catch (Exception ex) { stream.Close(); Log.Write(LogLevel.Error, ex.ToString()); return(false); } stream.Close(); return(true); } case ".dds": { var stream = FileSystem.OpenCopy(Name); try { Texture2D texture; DDSLib.DDSFromStream(stream, Renderer.Device, 0, true, out texture); Texture = texture; } catch (Exception ex) { stream.Close(); Log.Write(LogLevel.Error, ex.ToString()); return(false); } stream.Close(); return(true); } } Log.Write(LogLevel.Error, "Image {0} - not of a supported type", Name); return(false); }
// --------------------------------------------------------- public void SaveMipLevels(String Directory, String Prefix) { for (int i = 0; i < MAX_DOWNSAMPLE; ++i) { Texture2D tex; tex = _DownSampleTargets[i]; DDSLib.DDSToFile(Directory + Prefix + i + ".dds", true, tex, false); // tex.Save( Directory + Prefix + i + ".dds", ImageFileFormat.Dds ); } // for }
protected override Texture2D GetTexture(Game game) { Texture2D texture; GraphicsDevice gd = game.RenderProcess.GraphicsDevice; string defaultScreen = "load.ace"; string loadingScreen = Simulator.Instance.TRK.Route.LoadingScreen; if (IsWideScreen(game)) { string loadingScreenWide = Simulator.Instance.TRK.Route.LoadingScreenWide; loadingScreen = loadingScreenWide ?? loadingScreen; } loadingScreen = loadingScreen ?? defaultScreen; string path = Path.Combine(Simulator.Instance.RoutePath, loadingScreen); if (Path.GetExtension(path) == ".dds" && File.Exists(path)) { DDSLib.DDSFromFile(path, gd, true, out texture); } else if (Path.GetExtension(path) == ".ace") { string alternativeTexture = Path.ChangeExtension(path, ".dds"); if (File.Exists(alternativeTexture) && game.Settings.PreferDDSTexture) { DDSLib.DDSFromFile(alternativeTexture, gd, true, out texture); } else if (File.Exists(path)) { texture = AceFile.Texture2DFromFile(gd, path); } else { path = Path.Combine(Simulator.Instance.RoutePath, defaultScreen); if (File.Exists(path)) { texture = AceFile.Texture2DFromFile(gd, path); } else { texture = null; } } } else { texture = null; } return(texture); }
private void mnuExportDDS_Click(object sender, RoutedEventArgs e) { try { if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section)) { RW4Section section = this.DataContext as RW4Section; SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture; SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "DirectDraw Surface .DDS|*.dds"; if (dlg.ShowDialog().GetValueOrDefault(false)) { using (Stream stream = File.Create(dlg.FileName)) { DXT5Image img = new DXT5Image(); img.Height = textureSection.height; img.Width = textureSection.width; img.TextureType = textureSection.textureType; img.MipMaps = (int)textureSection.mipmapInfo; img.SetData(textureSection.texData.blob); img.Write(stream); GraphicsDeviceService.AddRef(new WindowInteropHelper(Application.Current.MainWindow).Handle); Texture2D texture; try { DDSLib.DDSFromStream(stream, GraphicsDeviceService.Instance.GraphicsDevice, 0, true, out texture); texturePreview.Source = DDSLib.Texture2Image(texture); texturePreview.Visibility = System.Windows.Visibility.Visible; textBlockError.Visibility = System.Windows.Visibility.Hidden; } catch (Exception ex) { texturePreview.Visibility = System.Windows.Visibility.Hidden; textBlockError.Visibility = System.Windows.Visibility.Visible; textBlockError.Text = ex.Message; } } } } } catch (Exception ex) { MessageBox.Show(string.Format("An exception occurred: {0}", ex.Message)); } }
public WriteableBitmap ToImage(bool opaqueAlpha) { if (this.textureType != 21) { using (var stream = new MemoryStream()) { stream.WriteU32(0x20534444); // 'DDS ' stream.WriteU32(0x7C); // header size stream.WriteU32(0xA1007); // flags: stream.WriteU32(height); stream.WriteU32(width); stream.WriteU32((uint)height * (uint)width); // size of top mipmap level... at least in DXT5 for >4x4 stream.WriteU32(0); stream.WriteU32(mipmapInfo / 0x100); for (int i = 0; i < 11; i++) { stream.WriteU32(0); } // pixel format stream.WriteU32(32); stream.WriteU32(4); // DDPF_FOURCC? stream.WriteU32(textureType); stream.WriteU32(32); stream.WriteU32(0xff0000); stream.WriteU32(0x00ff00); stream.WriteU32(0x0000ff); stream.WriteU32(0xff000000); stream.WriteU32(0); // 0x41008 for (int i = 0; i < 4; i++) { stream.WriteU32(0); } stream.Write(texData.blob, 0, texData.blob.Length); GraphicsDeviceService.AddRef(new WindowInteropHelper(Application.Current.MainWindow).Handle); Texture2D texture; try { DDSLib.DDSFromStream(stream, GraphicsDeviceService.Instance.GraphicsDevice, 0, true, out texture); return(new WriteableBitmap(DDSLib.Texture2Image(texture))); } catch { return(null); } } } else //In this case it's a regular bitmap saved as a texture { using (MemoryStream byteStream = new MemoryStream(this.texData.blob, 0, this.texData.blob.Length)) { for (int i = 0; i < this.mipmapInfo; i++) { // uint blockSize = byteStream.ReadU32().Swap(); WriteableBitmap bitmap = new WriteableBitmap((int)this.width, (int)this.height, 300, 300, PixelFormats.Pbgra32, BitmapPalettes.Halftone64); if (this.textureType == 21) { for (int j = 0; j < (byteStream.Length / 4); j++) { byte b = byteStream.ReadU8(); byte g = byteStream.ReadU8(); byte r = byteStream.ReadU8(); byte a = byteStream.ReadU8(); if (opaqueAlpha) { a = 255; } try { if ((j / this.width) < this.height) { bitmap.SetPixel((int)(j % this.width), (int)(j / this.width), a, r, g, b); } } catch { } } return(bitmap); } } } } return(null); }
public GameModel(string meshPath, string texturePath = "null") { using (BinaryReader reader = new BinaryReader((Stream)File.OpenRead(meshPath))) { this.Header = Encoding.ASCII.GetString(reader.ReadBytes(16)); C3PartType type = C3PartType.START; while (type != C3PartType.EXIT && reader.BaseStream.Position + 4L <= reader.BaseStream.Length) { type = (C3PartType)reader.ReadInt32(); Console.WriteLine("Loading: " + (object)type); switch (type) { case C3PartType.PHY4: case C3PartType.PHY: case C3PartType.PHY3: this.PhysicalObjects.Add(Physics.ReadPhysics(reader, type)); break; case C3PartType.MOTI: this.Motions.Add(Motion.ReadMotion(reader)); break; case C3PartType.SHAP: this.Shapes.Add(Shape.ReadShape(reader)); break; default: reader.BaseStream.Position -= 4L; int num = (int)MessageBox.Show("Warning: This file was not completely loaded. Please avoid exporting this model or it may become corrupted."); type = C3PartType.EXIT; break; } } reader.Close(); } meshPath = meshPath.Replace('\\', '/'); int num1 = meshPath.LastIndexOf('/'); string s = meshPath.Substring(num1 + 1, meshPath.Length - num1 - 4); int.TryParse(s, out this.ID); if (File.Exists(meshPath.Substring(0, meshPath.Length - 3) + ".dds")) { DDSLib.DDSFromFile(meshPath.Substring(0, meshPath.Length - 3) + ".dds", Settings.Graphics, false, out this.Texture); } else if (File.Exists(Settings.Conquer_Path + "c3/texture/" + s + ".dds")) { DDSLib.DDSFromFile(Settings.Conquer_Path + "c3/texture/" + s + ".dds", Settings.Graphics, false, out this.Texture); } else if (File.Exists(Settings.Conquer_Path + "c3/texture/" + (object)(this.ID / 10 * 10) + ".dds")) { DDSLib.DDSFromFile(Settings.Conquer_Path + "c3/texture/" + (object)(this.ID / 10 * 10) + ".dds", Settings.Graphics, false, out this.Texture); } this.Name = this.ID.ToString(); foreach (Physics physicalObject in this.PhysicalObjects) { Settings.GUI.AddPhysicalObject(this.Name, physicalObject); } foreach (Shape shape in this.Shapes) { Settings.GUI.AddShape(this.Name, shape); } }
private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section)) { RW4Section section = this.DataContext as RW4Section; SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture; if (textureSection.textureType != 21) { if (textureSection != null) { using (var stream = new MemoryStream()) { DXT5Image img = new DXT5Image(); img.Height = textureSection.height; img.Width = textureSection.width; img.TextureType = textureSection.textureType; img.MipMaps = (int)textureSection.mipmapInfo; img.SetData(textureSection.texData.blob); img.Write(stream); if (textureSection.textureType == 0x21) { mnuExportBMP.IsEnabled = true; mnuExportDDS.IsEnabled = false; mnuImportBMP.IsEnabled = true; mnuImportDDS.IsEnabled = false; } else { mnuExportBMP.IsEnabled = false; mnuExportDDS.IsEnabled = true; mnuImportBMP.IsEnabled = false; mnuImportDDS.IsEnabled = true; } GraphicsDeviceService.AddRef(new WindowInteropHelper(Application.Current.MainWindow).Handle); Texture2D texture; try { DDSLib.DDSFromStream(stream, GraphicsDeviceService.Instance.GraphicsDevice, 0, true, out texture); texturePreview.Source = DDSLib.Texture2Image(texture); texturePreview.Visibility = System.Windows.Visibility.Visible; textBlockError.Visibility = System.Windows.Visibility.Hidden; } catch (Exception ex) { texturePreview.Visibility = System.Windows.Visibility.Hidden; textBlockError.Visibility = System.Windows.Visibility.Visible; textBlockError.Text = ex.Message; } } } } else { using (MemoryStream byteStream = new MemoryStream(textureSection.texData.blob, 0, textureSection.texData.blob.Length)) { for (int i = 0; i < textureSection.mipmapInfo; i++) { // uint blockSize = byteStream.ReadU32().Swap(); WriteableBitmap bitmap = new WriteableBitmap((int)textureSection.width, (int)textureSection.height, 300, 300, PixelFormats.Pbgra32, BitmapPalettes.Halftone64); if (textureSection.textureType == 21) { for (int j = 0; j < (byteStream.Length / 4); j++) { byte r = byteStream.ReadU8(); byte g = byteStream.ReadU8(); byte b = byteStream.ReadU8(); byte a = byteStream.ReadU8(); try { if ((j / textureSection.width) < textureSection.height) { bitmap.SetPixel((int)(j % textureSection.width), (int)(j / textureSection.width), a, r, g, b); } } catch { } } texturePreview.Source = bitmap; break; } } } } if (textureSection != null) { txtHeight.Text = textureSection.height.ToString(); txtWidth.Text = textureSection.width.ToString(); txtTextureType.Text = textureSection.textureType.ToHex(); txtMipMapInfo.Text = textureSection.mipmapInfo.ToHex(); txtSection.Text = textureSection.texData.section.Number.ToString(); txtUnknown.Text = textureSection.unk1.ToHex(); } // .AddObject(texture.texData, TextureBlob.type_code); } }
public Texture2D Load(string path, GraphicsDevice device) { if (LoadedTextures.ContainsKey(path)) { LoadedTexture texture = LoadedTextures[path]; texture.UsedCount++; return(texture.Texture); } //check long path //if(path.StartsWith("Assess")) //Look in map file first if (ProjectProperties.CurrentProjectPropperties.InputMap != null && ProjectProperties.CurrentProjectPropperties.InputMap.Exists) { if (ProjectProperties.CurrentProjectPropperties.InputMap is FileInfo) { using ( MpqEditor.MpqReader reader = new MpqEditor.MpqReader(ProjectProperties.CurrentProjectPropperties.InputMap.FullName)) { if (reader.HasFile(path)) { byte[] rawFile = reader.ExtractFile(path); using (MemoryStream stream = new MemoryStream(rawFile)) { //Texture tex = Texture.FromFile(device, stream); //Texture2D texture; //if (tex is Texture2D) //{ // texture = (Texture2D)tex; //} //else //{ // /*MessageBox.Show( // "Unable to load texture:\n" + path + // "\nBecause it is not a two dimentional texture.", "Error");*/ // return null; //} Texture2D texture = null; DDSLib.DDSFromStream(stream, device, 0, true, out texture); if (texture == null) { return(null); } LoadedTextures.Add(path, new LoadedTexture() { Texture = texture, UsedCount = 1 }); return(texture); } } } } } //Look in standard library after if (Options.General.SC2Exe == null || !Options.General.SC2Exe.Exists) { RegistryKey key = Registry.LocalMachine; key = key.OpenSubKey("SOFTWARE\\Wow6432Node\\Blizzard Entertainment\\Starcraft II Retail"); if (key != null) { Options.General.SC2Exe = new FileInfo((string)key.GetValue("GamePath")); } else { MessageBox.Show(Form1.Form, "I lost track of where Starcraft II is located. Can you help me?", "Missing data"); OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); dialog.InitialDirectory = dialog.InitialDirectory.Substring(0, dialog.InitialDirectory.IndexOf("\\")); dialog.Filter = "StarCraft II (Starcraft II.exe)|Starcraft II.exe"; if (dialog.ShowDialog(Form1.Form) == DialogResult.Cancel) { return(null); } Options.General.SC2Exe = new FileInfo(dialog.FileName); } } //can only get the file from the casc file system. DirectoryInfo DataDir = new DirectoryInfo(Options.General.SC2Exe.Directory + @"\SC2Data"); String strDataDir = DataDir.FullName; using (CascStorageContext casc = new CascStorageContext(strDataDir)) { try { using (var file = casc.OpenFile(path)) {//currently, only * operation is supported. byte[] rawFile = ReadAllBytes(file); using (MemoryStream stream = new MemoryStream(rawFile)) { Texture2D texture = null; DDSLib.DDSFromStream(stream, device, 0, true, out texture); if (texture == null) { return(null); } LoadedTextures.Add(path, new LoadedTexture() { Texture = texture, UsedCount = 1 }); return(texture); } } }catch (Exception ex) { } } //load textures from mod directories? Stack <DirectoryInfo> modDirs = new Stack <DirectoryInfo>(); modDirs.Push(new DirectoryInfo(Options.General.SC2Exe.Directory + @"\Mods")); List <FileInfo> modfiles = new List <FileInfo>(); //parse base mods and store SC2Mod files while (modDirs.Count != 0) { DirectoryInfo curVdir = modDirs.Pop(); if (!curVdir.Exists) { continue; } foreach (var subDir in curVdir.GetDirectories()) { List <FileInfo> filesToSearch = new List <FileInfo>(); filesToSearch.AddRange(subDir.GetFiles("*.SC2Mod")); if (filesToSearch.Count > 0) { modfiles.AddRange(filesToSearch); } filesToSearch.Clear(); } } //Read Assets\Textures\files foreach (var archive in modfiles) { MpqEditor.MpqReader fileReader = new MpqEditor.MpqReader(archive.FullName); { if (!fileReader.Valid) { continue; } //fileReader.FindFiles("*.SC2Data"); foreach (var supportedfile in SupportedFiles) { string[] files = fileReader.FindFiles("Assets\\Textures\\" + supportedfile); foreach (var file in files) { int i = file.LastIndexOf("Assets\\Textures"); if (i == -1) { continue; } string fileName = file.Substring(i); if (fileName == path) { byte[] rawFile = fileReader.ExtractFile(file); using (MemoryStream stream = new MemoryStream(rawFile)) { Texture2D texture = null; try { DDSLib.DDSFromStream(stream, device, 0, true, out texture); } catch (Exception ex) { } if (texture == null) { return(null); } LoadedTextures.Add(path, new LoadedTexture() { Texture = texture, UsedCount = 1 }); return(texture); } } } } } } //StarCraft II\Versions\<newest>\patch.SC2Archive\Mods\Core.SC2Mod\Base.SC2Assets\Assets\Textures\.. //StarCraft II\Versions\<newest>\patch.SC2Archive\Mods\Liberty.SC2Mod\Base.SC2Assets\Assets\Textures\.. //StarCraft II\Versions\<newest>\patch.SC2Archive\Campaigns\Liberty.SC2Campaign\Base.SC2Assets\Assets\Textures\.. //StarCraft II\Mods\Core.SC2Mod\base.SC2Assets\Assets\Textures\.. //StarCraft II\Mods\Liberty.SC2Mod\base.SC2Assets\Assets\Textures\.. //StarCraft II\Mods\Liberty.SC2Mod\enGB.SC2Assets\Assets\Textures\.. //StarCraft II\*.SC2Assets|Assets\Textures\.. return(null); }
protected override void Update(GameTime gameTime) { if (!this.IsActive || Settings.GUI.Visible) { this.mouseAim = false; } else { Vector3 zero = Vector3.Zero; MouseState state1 = Mouse.GetState(); if (!Settings.GUI.Visible && state1.LeftButton == ButtonState.Pressed && this.mouseState.LeftButton == ButtonState.Released) { this.mouseState = Mouse.GetState(); if (this.showColorWheel && new Rectangle((int)this.colorWheelLocation.X, (int)this.colorWheelLocation.Y, this.colorWheel.Width, this.colorWheel.Height).Contains(new Point(this.mouseState.X, this.mouseState.Y))) { Color[] data = new Color[this.colorWheel.Width * this.colorWheel.Height]; this.colorWheel.GetData <Color>(data); int index = this.mouseState.X + this.mouseState.Y * this.colorWheel.Width; if (index < data.Length) { this.selectedColor = data[index]; } } else { this.mouseAim = !this.mouseAim; } } if (this.mouseAim) { this.camera.Yaw -= (float)(state1.X - this.mouseState.X) * (1f / 500f); this.camera.Pitch -= (float)(state1.Y - this.mouseState.Y) * (1f / 500f); this.camera.Zoom += (float)(this.mouseState.ScrollWheelValue - state1.ScrollWheelValue) * 0.05f; Viewport viewport = this.GraphicsDevice.Viewport; int x = viewport.Width / 2; viewport = this.GraphicsDevice.Viewport; int y = viewport.Height / 2; Mouse.SetPosition(x, y); } this.mouseState = Mouse.GetState(); KeyboardState state2 = Keyboard.GetState(); foreach (Keys pressedKey in state2.GetPressedKeys()) { switch (pressedKey) { case Keys.S: case Keys.End: case Keys.Down: this.camera.Pitch -= 0.025f; break; case Keys.W: case Keys.Home: case Keys.Up: this.camera.Pitch += 0.025f; break; case Keys.LeftShift: --this.camera.Zoom; break; case Keys.Space: ++this.camera.Zoom; break; case Keys.PageDown: case Keys.Right: case Keys.D: this.camera.Yaw -= 0.025f; break; case Keys.Left: case Keys.Delete: case Keys.A: this.camera.Yaw += 0.025f; break; } } if (state2.IsKeyDown(Keys.Tab) && !this.keyboardState.IsKeyDown(Keys.Tab)) { this.CurrentRasterizeState = this.CurrentRasterizeState == this.WireframeRasterizeState ? this.FillRasterizeState : this.WireframeRasterizeState; } if (state2.IsKeyDown(Keys.F10) && !this.keyboardState.IsKeyDown(Keys.F10)) { int backBufferWidth = this.GraphicsDevice.PresentationParameters.BackBufferWidth; int backBufferHeight = this.GraphicsDevice.PresentationParameters.BackBufferHeight; this.Draw(new GameTime()); int[] data = new int[backBufferWidth * backBufferHeight]; this.GraphicsDevice.GetBackBufferData <int>(data); Texture2D texture2D = new Texture2D(this.GraphicsDevice, backBufferWidth, backBufferHeight, false, this.GraphicsDevice.PresentationParameters.BackBufferFormat); texture2D.SetData <int>(data); Stream stream = (Stream)File.OpenWrite("Screenshots/" + (object)Environment.TickCount + ".jpg"); texture2D.SaveAsJpeg(stream, backBufferWidth, backBufferHeight); stream.Dispose(); texture2D.Dispose(); } if (state2.IsKeyDown(Keys.F1) && !this.keyboardState.IsKeyDown(Keys.F1)) { Settings.GUI.Show(); } if (state2.IsKeyDown(Keys.F5) && !this.keyboardState.IsKeyDown(Keys.F5)) { System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); string str = (Settings.Conquer_Path + "c3/weapon/").Replace('/', '\\'); openFileDialog.InitialDirectory = str; if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Settings.Scene.LoadModel(openFileDialog.FileName); this.centerCamera(); } } if (state2.IsKeyDown(Keys.F2) && !this.keyboardState.IsKeyDown(Keys.F2)) { System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Settings.Scene.LoadModel(openFileDialog.FileName); this.centerCamera(); } } if (state2.IsKeyDown(Keys.F3) && !this.keyboardState.IsKeyDown(Keys.F3)) { System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (Settings.GUI.SelectedModel == null) { Settings.GUI.SelectedModel = Settings.Scene.Models.FirstOrDefault <GameModel>(); } if (Settings.GUI.SelectedModel != null) { DDSLib.DDSFromFile(openFileDialog.FileName, this.GraphicsDevice, false, out Settings.GUI.SelectedModel.Texture); } } } if (state2.IsKeyDown(Keys.F12) && !this.keyboardState.IsKeyDown(Keys.F12)) { Settings.Scene = new GameScene(); Settings.GUI = new Manager(); } foreach (GameModel model in Settings.Scene.Models) { foreach (Physics physicalObject in model.PhysicalObjects) { if (physicalObject.Motion != null && DateTime.Now > this.nextFrame) { this.nextFrame = DateTime.Now.AddMilliseconds(30.0); physicalObject.FrameIndex = (physicalObject.FrameIndex + 1) % physicalObject.Motion.KeyFrames.Count; } if (!(physicalObject.UV_Step == Vector2.Zero)) { foreach (Vertex normalVertex in physicalObject.NormalVertexes) { normalVertex.UV += physicalObject.UV_Step; } foreach (Vertex alphaVertex in physicalObject.AlphaVertexes) { alphaVertex.UV += physicalObject.UV_Step; } } } } this.keyboardState = state2; base.Update(gameTime); } }