private void btnReplaceSelected_Click(object sender, EventArgs e) { // TODO: Ideally this will be done by loading the BMD to a ModelBase object, modifying the // selected textures and then writing the ModelBase object to BMD again. if (lbxTextures.SelectedIndex != -1) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Select an image"; DialogResult result = ofd.ShowDialog(); if (result == DialogResult.Cancel) { return; } int texIndex = lbxTextures.SelectedIndex = (int)m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_TextureID; int palIndex = lbxPalettes.SelectedIndex = (int)m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PaletteID; try { Bitmap bmp = new Bitmap(ofd.FileName); NitroTexture tex = NitroTexture.FromBitmapAndType( (uint)texIndex, m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_TextureName, (uint)palIndex, m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PaletteName, bmp, BestTexTypeForBitmap(bmp, chkCompressReplacedTextures.Checked)); // Update texture entry uint curoffset = m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_EntryOffset; m_Model.m_File.Write32(curoffset + 0x08, (uint)tex.m_TextureDataLength); m_Model.m_File.Write16(curoffset + 0x0C, (ushort)(8 << (int)((tex.m_DSTexParam >> 20) & 0x7))); m_Model.m_File.Write16(curoffset + 0x0E, (ushort)(8 << (int)((tex.m_DSTexParam >> 23) & 0x7))); m_Model.m_File.Write32(curoffset + 0x10, tex.m_DSTexParam); // Update palette entry if (tex.m_RawPaletteData != null) { curoffset = m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PalEntryOffset; m_Model.m_File.Write32(curoffset + 0x08, (uint)tex.m_RawPaletteData.Length); m_Model.m_File.Write32(curoffset + 0x0C, 0xFFFFFFFF); } // Write new texture and texture palette data // Check whether we need to make room for additional data uint oldTexDataSize = (uint)m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_RawTextureData.Length; uint newTexDataSize = (uint)((tex.m_RawTextureData.Length + 3) & ~3); uint oldPalDataSize = (uint)m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PaletteDataLength; uint newPalDataSize = (uint)((tex.m_PaletteDataLength + 3) & ~3); uint texDataOffset = m_Model.m_File.Read32(m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_EntryOffset + 0x04); // If necessary, make room for additional texture data if (newTexDataSize > oldTexDataSize) { m_Model.AddSpace(texDataOffset + oldTexDataSize, newTexDataSize - oldTexDataSize); } m_Model.m_File.WriteBlock(texDataOffset, tex.m_RawTextureData); uint palDataOffset = m_Model.m_File.Read32(m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PalEntryOffset + 0x04); // If necessary, make room for additional palette data if (newPalDataSize > oldPalDataSize) { m_Model.AddSpace(palDataOffset + oldPalDataSize, newPalDataSize - oldPalDataSize); } // Reload palette data offset palDataOffset = m_Model.m_File.Read32(m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PalEntryOffset + 0x04); if (tex.m_RawPaletteData != null) { m_Model.m_File.WriteBlock(palDataOffset, tex.m_RawPaletteData); } m_Model.m_File.SaveChanges(); LoadTextures(); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.Source + ex.TargetSite + ex.StackTrace); } } else { MessageBox.Show("Please select a texture first."); } }
private void btnReplaceSelected_Click(object sender, EventArgs e) { if (lbxTextures.SelectedIndex != -1) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Select an image"; DialogResult result = ofd.ShowDialog(); if (result == DialogResult.Cancel) { return; } //int index = lbxTextures.SelectedIndex; int texIndex = lbxTextures.SelectedIndex = (int)m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_TexID; int palIndex = lbxPalettes.SelectedIndex = (int)m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PalID; try { BMDWriter.ConvertedTexture tex = BMDWriter.ConvertTexture(ofd.FileName, m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_TexName, m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PalName, new Bitmap(ofd.FileName)); tex.m_TextureID = (uint)texIndex; tex.m_PaletteID = (uint)palIndex; // Update texture entry uint curoffset = m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_EntryOffset; m_Model.m_File.Write32(curoffset + 0x08, (uint)tex.m_TextureDataLength); m_Model.m_File.Write16(curoffset + 0x0C, (ushort)(8 << (int)((tex.m_DSTexParam >> 20) & 0x7))); m_Model.m_File.Write16(curoffset + 0x0E, (ushort)(8 << (int)((tex.m_DSTexParam >> 23) & 0x7))); m_Model.m_File.Write32(curoffset + 0x10, tex.m_DSTexParam); // Update palette entry if (tex.m_PaletteData != null) { curoffset = m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PalEntryOffset; m_Model.m_File.Write32(curoffset + 0x08, (uint)tex.m_PaletteData.Length); m_Model.m_File.Write32(curoffset + 0x0C, 0xFFFFFFFF); } // Write new texture and texture palette data // Check if we need to make room for additional data // For compressed (type 5) textures, the size of the texture data doesn't count the palette index data. // The texture data is then directly followed by (size/2) of palette index data. uint oldTexDataSize = (uint)m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_TexDataSize; if (m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_TexType == 5) { oldTexDataSize += (oldTexDataSize / 2); } uint newTexDataSize = (uint)((tex.m_TextureData.Length + 3) & ~3); uint oldPalDataSize = (uint)m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PalSize; uint newPalDataSize = (uint)((tex.m_PaletteData.Length + 3) & ~3); uint texDataOffset = m_Model.m_File.Read32(m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_EntryOffset + 0x04); // If necessary, make room for additional texture data if (newTexDataSize > oldTexDataSize) { m_Model.AddSpace(texDataOffset + oldTexDataSize, newTexDataSize - oldTexDataSize); } m_Model.m_File.WriteBlock(texDataOffset, tex.m_TextureData); uint palDataOffset = m_Model.m_File.Read32(m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PalEntryOffset + 0x04); // If necessary, make room for additional palette data if (newPalDataSize > oldPalDataSize) { m_Model.AddSpace(palDataOffset + oldPalDataSize, newPalDataSize - oldPalDataSize); } // Reload palette data offset palDataOffset = m_Model.m_File.Read32(m_Model.m_Textures[lbxTextures.SelectedItem.ToString()].m_PalEntryOffset + 0x04); if (tex.m_PaletteData != null) { m_Model.m_File.WriteBlock(palDataOffset, tex.m_PaletteData); } m_Model.m_File.SaveChanges(); LoadTextures(); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.Source + ex.TargetSite + ex.StackTrace); } } else { MessageBox.Show("Please select a texture first."); } }