Example #1
0
        public static F3DZEXTextureWrapper GetTextureWrapper(Texture texture, F3DZEX_G_SetTile command, F3DZEX_G_Texture textureCommand)
        {
            if (_textureWrappers.ContainsKey(texture))
                return _textureWrappers[texture];

            F3DZEXTextureWrapper wrapper = new F3DZEXTextureWrapper(texture, command, textureCommand);
            _textureWrappers.Add(texture, wrapper);

            return wrapper;
        }
Example #2
0
        private static void ResetReaderVariables()
        {
            _foundCommands.Clear();
            _foundPalettes.Clear();
            _foundTextures.Clear();
            _foundVertices.Clear();

            _currentTexture = null;
            
            _vertexBuffer.Clear();
            for (int i = 0; i < 32; i++)
                _vertexBuffer.Add(null);
            
            TMEM = new TMem();

            DefaultTextureTile = 0;
            
            _lastSetTile = null;
            _lastSetTImg = null;
            _lastSetTileSize = null;
            _lastLoadBlock = null;
            _lastLoadTLut = null;
        }
 public F3DZEXTextureWrapper(Texture texture, F3DZEX_G_SetTile command, F3DZEX_G_Texture textureCommand)
 {
     F3DTexture = texture;
     SetTileCommand = command;
     TextureCommand = textureCommand;
 }
 public TextureWrapper(int i, Texture t)
 {
     TextureCount = i;
     Texture = t;
 }
        private void LoadHyruleTextures(List<byte> romData)
        {
            _palettes.Clear();
            _textures.Clear();
            cbTextures.Items.Clear();

            Palette palette = null;
            Texture texture = null;
            byte[] paletteData = new byte[0x20];
            byte[] textureData6464 = new byte[64 * 64 / 2]; //All textures are CI4, so we can calc the size like this
            byte[] textureData6432 = new byte[64 * 32 / 2];
            byte[] textureData3232 = new byte[32 * 32 / 2];
            byte[] textureData1616 = new byte[16 * 16 / 2];

            for (int i = 0; i < HyrulePaletteLocations.Length; i++)
            {
                romData.CopyTo(HyrulePaletteLocations[i], paletteData, 0, 0x20);//All are 16 colors
                palette = new Palette(HyrulePaletteLocations[i], paletteData);
                switch (i)
                {
                    case 0:
                    case 3:
                    case 6:
                        //64x64
                        romData.CopyTo(HyruleTextureLocations[i], textureData6464, 0, textureData6464.Length);
                        texture = new Texture(HyruleTextureLocations[i], textureData6464, Texture.ImageFormat.CI, Texture.PixelInfo.Size_4b,
                            64, 64, palette, 0);
                        break;
                    case 1:
                    case 4:
                        //64x32
                        romData.CopyTo(HyruleTextureLocations[i], textureData6432, 0, textureData6432.Length);
                        texture = new Texture(HyruleTextureLocations[i], textureData6432, Texture.ImageFormat.CI, Texture.PixelInfo.Size_4b,
                            64, 32, palette, 0);
                        break;
                    case 2:
                    case 5:
                    case 7:
                    case 9:
                    case 10:
                    case 11:
                        //32x32
                        romData.CopyTo(HyruleTextureLocations[i], textureData3232, 0, textureData3232.Length);
                        texture = new Texture(HyruleTextureLocations[i], textureData3232, Texture.ImageFormat.CI, Texture.PixelInfo.Size_4b,
                            32, 32, palette, 0);
                        break;
                    case 8:
                        //16x16
                        romData.CopyTo(HyruleTextureLocations[i], textureData1616, 0, textureData1616.Length);
                        texture = new Texture(HyruleTextureLocations[i], textureData1616, Texture.ImageFormat.CI, Texture.PixelInfo.Size_4b,
                            16, 16, palette, 0);
                        break;
                    case 12:
                        //64x16
                        romData.CopyTo(HyruleTextureLocations[i], textureData3232, 0, textureData3232.Length);
                        texture = new Texture(HyruleTextureLocations[i], textureData3232, Texture.ImageFormat.CI, Texture.PixelInfo.Size_4b,
                            64, 16, palette, 0);
                        break;
                }

                _palettes.Add(palette);
                _textures.Add(texture);

            }

            foreach (Texture text in _textures)
            {
                if (text.IsValidFormat)
                {
                    cbTextures.Items.Add(new TextureWrapper(cbTextures.Items.Count + 1, text));
                }
            }

            if(cbTextures.Items.Count > 0)
                cbTextures.SelectedIndex = 0;
            /*Combos: (Palette/Texture, all in 16 color-CI4 format)
                        8, 30 (64x64)
                        838, 860 (64x32)
                        C68, C90 (32x32)
                        E98, EC0 (64x64)
                        16C8, 16F0 (64x32)
                        1AF8, 1B20 (32x32)
                        1D28, 1D50 (64x64)
                        2558, 2580 (32x32)
                        2788, 27B0 (16x16)
                        2838, 2860 (32x32)
                        2A68, 2A90 (32x32)
                        2C98, 2CC0 (32x32)
                        2EC8, 2EF0 (64x16)
             */
        }
Example #6
0
        private static Texture ReadTextureFromTMem(TileDescriptor tile, Palette palette = null)
        {
            Texture newTexture;

            if (!TMEM.LoadedData.ContainsKey(tile.TMem))
                return null;

            //if (!tile.On)   //Not sure if used??
            //    return null;

            int offset = TMEM.LoadedData[tile.TMem].FileOffset;
            int sizeOfData = TMEM.LoadedData[tile.TMem].Size;

            int widthInBytes = 8 * tile.Line;

            double texelSize = 1;
            switch (tile.PixelSize)
            {
                case Texture.PixelInfo.Size_4b:
                    texelSize = 0.5;
                    break;
                case Texture.PixelInfo.Size_16b:
                    texelSize = 2;
                    break;
                case Texture.PixelInfo.Size_32b:
                    texelSize = 4;
                    break;
            }

            int widthInTexels = (int)Math.Round(widthInBytes / texelSize);
            int heightInTexels = sizeOfData / widthInTexels;

            byte[] data = new byte[sizeOfData];
            Array.Copy(TMEM.Data, tile.TMemInBytes, data, 0, sizeOfData);
            
            if(tile.ImageFormat == Texture.ImageFormat.CI)
                newTexture = new Texture(offset, data, tile.ImageFormat, tile.PixelSize,
                    widthInTexels, heightInTexels, palette, tile.Palette);
            else
                newTexture = new Texture(offset, data, tile.ImageFormat, tile.PixelSize,
                    widthInTexels, heightInTexels, null);

            tile.SettingsChanged = false;

            return newTexture;
        }
Example #7
0
        private static bool TryLoadExistingTexture(TileDescriptor tile, out Texture texture)
        {
            texture = null;

            if (TMEM.LoadedElements.ContainsKey(TMEM.TileDescriptors[DefaultTextureTile].TMem))
            {
                texture = (TMEM.LoadedElements[TMEM.TileDescriptors[DefaultTextureTile].TMem] as Texture);
                Palette newPalette;
                if (TryLoadExistingPalette(out newPalette))
                    texture.ImagePalette = newPalette;
                return true;
            }

            if (!TMEM.LoadedData.ContainsKey(TMEM.TileDescriptors[DefaultTextureTile].TMem))
                return false;

            LoadedTMemData tmemInfo = TMEM.LoadedData[TMEM.TileDescriptors[DefaultTextureTile].TMem];

            if(tmemInfo.SourceFile == null || !_foundTextures.ContainsKey(tmemInfo.SourceFile))
                return false;

            for (int i = 0; i < _foundTextures[tmemInfo.SourceFile].Count; i++)
            {
                if (_foundTextures[tmemInfo.SourceFile][i].FileOffset == tmemInfo.FileOffset)
                {
                    texture = _foundTextures[tmemInfo.SourceFile][i];
                    return true;
                }
            }

            return false;
        }
Example #8
0
        private static void ParseCommand(F3DZEXCommand command)
        {
            TileDescriptor tile;
            //Here handle extra command things (like if it's referencing textures and such)
            switch (command.CommandID)
            {
                case F3DZEXCommandID.F3DZEX_G_TEXTURE:
                    F3DZEX_G_Texture textureCmd = (F3DZEX_G_Texture)command;
                    tile = TMEM.TileDescriptors[textureCmd.Tile];
                    tile.On = (textureCmd.TurnOn & 1) == 0;
                    tile.ScaleS = textureCmd.ScaleS;
                    tile.ScaleT = textureCmd.ScaleT;
                    tile.Level = textureCmd.Level;

                    break;
                case F3DZEXCommandID.F3DZEX_G_SETTILE:
                    _lastSetTile = (F3DZEX_G_SetTile)command;

                    tile = TMEM.TileDescriptors[_lastSetTile.Tile];
                    tile.CMSMirror = _lastSetTile.CMSMirror;
                    tile.CMTMirror = _lastSetTile.CMTMirror;
                    tile.CMSWrap = _lastSetTile.CMSWrap;
                    tile.CMTWrap = _lastSetTile.CMTWrap;
                    tile.ImageFormat = _lastSetTile.Format;
                    tile.Line = _lastSetTile.Line;
                    tile.MaskS = _lastSetTile.MaskS;
                    tile.MaskT = _lastSetTile.MaskT;
                    tile.Palette = _lastSetTile.Palette;
                    tile.PixelSize = _lastSetTile.PixelSize;
                    tile.ShiftS = _lastSetTile.ShiftS;
                    tile.ShiftT = _lastSetTile.ShiftT;
                    tile.TMem = _lastSetTile.TMem;

                    break;
                case F3DZEXCommandID.F3DZEX_G_SETTILESIZE:
                    _lastSetTileSize = (F3DZEX_G_SetTileSize)command;

                    tile = TMEM.TileDescriptors[_lastSetTileSize.Tile];
                    tile.LRS = _lastSetTileSize.LRS;
                    tile.LRT = _lastSetTileSize.LRT;
                    tile.ULS = _lastSetTileSize.ULS;
                    tile.ULT = _lastSetTileSize.ULT;

                    break;
                case F3DZEXCommandID.F3DZEX_G_SETTIMG:
                    _lastSetTImg = (F3DZEX_G_SetTImg)command;
                    TMEM.LastTImgCommand = _lastSetTImg;
                    //TMEM.RAMTexturePixelSize = _lastSetTImg.PixelSize;
                    //TMEM.RAMTextureAddress = _lastSetTImg.ImageAddress;
                    //TMEM.RAMTextureImageFormat = _lastSetTImg.Format;
                    //TMEM.RAMTextureWidth = _lastSetTImg.Width;

                    break;
                case F3DZEXCommandID.F3DZEX_G_LOADBLOCK:
                    _lastLoadBlock = (F3DZEX_G_LoadBlock)command;

                    TMEM.LoadBlockIntoTMem(_lastLoadBlock);
                    break;
                case F3DZEXCommandID.F3DZEX_G_LOADTLUT:
                    _lastLoadTLut = (F3DZEX_G_LoadTLut)command;

                    TMEM.LoadTLutIntoTMem(_lastLoadTLut);
                    break;
                case F3DZEXCommandID.F3DZEX_G_VTX:
                    F3DZEX_G_Vtx vtx = (F3DZEX_G_Vtx)command;

                    RomFile file;
                    int offset;
                    if(RomProject.Instance.FindRamOffset(vtx.VertexSourceAddress, out file, out offset))
                    {
                        if (file.GetElementAt(offset) is VertexCollection)
                        {
                            //Just update the reference buffer
                            VertexCollection collection = (VertexCollection)file.GetElementAt(offset);

                            if(vtx.VertexCount > 0)
                            {
                                int startIndex = 0;
                                bool foundVertices = false;
                                for (int i = 0; i < collection.Vertices.Count; i++)
                                {
                                    if (collection.Vertices[i].FileOffset == offset)
                                    {
                                        startIndex = i;
                                        foundVertices = true;
                                    }

                                    if (foundVertices)
                                    {
                                        _vertexBuffer[vtx.TargetBufferIndex + (i - startIndex)] = collection.Vertices[i];
                                        if (i - startIndex + 1 >= vtx.VertexCount)
                                            break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            //Actually load in the bytes as new Vertices
                            byte[] bytes = file.GetAsBytes();

                            byte[] vtxBytes = new byte[0x10];

                            List<Vertex> vertices = new List<Vertex>();

                            for (int i = 0; i < vtx.VertexCount; i++)
                            {
                                Array.Copy(bytes, offset, vtxBytes, 0, 0x10);
                                Vertex vertex = new Vertex(offset, vtxBytes);
                                //Add to collection somewhere
                                vertices.Add(vertex);

                                offset += 0x10;

                                _vertexBuffer[i + vtx.TargetBufferIndex] = vertex;
                            }

                            if (!_foundVertices.ContainsKey(file))
                                _foundVertices.Add(file, new List<Vertex>());

                            _foundVertices[file].AddRange(vertices);
                        }
                    }
                    break;
                case F3DZEXCommandID.F3DZEX_G_TRI1:
                case F3DZEXCommandID.F3DZEX_G_TRI2:
                    if (TMEM.TileDescriptors[DefaultTextureTile].SettingsChanged)
                    {
                        //Try seeing if the texture has been loaded before
                        if (!TryLoadExistingTexture(TMEM.TileDescriptors[DefaultTextureTile], out _currentTexture) &&
                            TMEM.LoadedData.ContainsKey(TMEM.TileDescriptors[DefaultTextureTile].TMem))
                        {
                            LoadedTMemData tmemInfo = TMEM.LoadedData[TMEM.TileDescriptors[DefaultTextureTile].TMem];

                            //First try loading up the palette
                            Palette palette = null;
                            if (!TryLoadExistingPalette(out palette) &&
                                TMEM.LoadedData.ContainsKey(PALETTE_TMEM_OFFSET))
                            {
                                palette = ReadPaletteFromTMem();

                                if (palette != null)
                                {
                                    tmemInfo = TMEM.LoadedData[PALETTE_TMEM_OFFSET];

                                    if (!_foundPalettes.ContainsKey(tmemInfo.SourceFile))
                                        _foundPalettes.Add(tmemInfo.SourceFile, new List<Palette>());

                                    _foundPalettes[tmemInfo.SourceFile].Add(palette);
                                }
                            }

                            //Load up new Texture
                            _currentTexture = ReadTextureFromTMem(TMEM.TileDescriptors[DefaultTextureTile], palette);

                            if (_currentTexture != null)
                            {
                                //Store in the found variables
                                if (!_foundTextures.ContainsKey(tmemInfo.SourceFile))
                                    _foundTextures.Add(tmemInfo.SourceFile, new List<Texture>());

                                _foundTextures[tmemInfo.SourceFile].Add(_currentTexture);

                            }
                        }
                        TMEM.TileDescriptors[0].SettingsChanged = false;
                    }
                    if (command.CommandID == F3DZEXCommandID.F3DZEX_G_TRI1)
                    {
                        F3DZEX_G_Tri1 tri = (F3DZEX_G_Tri1)command;
                        tri.TextureReference = _currentTexture;

                        tri.Vertex1Reference = _vertexBuffer[tri.Vertex1];
                        tri.Vertex2Reference = _vertexBuffer[tri.Vertex2];
                        tri.Vertex3Reference = _vertexBuffer[tri.Vertex3];
                    }
                    else if (command.CommandID == F3DZEXCommandID.F3DZEX_G_TRI2)
                    {
                        F3DZEX_G_Tri2 tri = (F3DZEX_G_Tri2)command;
                        tri.TextureReference = _currentTexture;


                        tri.Vertex1Reference = _vertexBuffer[tri.Vertex1];
                        tri.Vertex2Reference = _vertexBuffer[tri.Vertex2];
                        tri.Vertex3Reference = _vertexBuffer[tri.Vertex3];
                        tri.Vertex4Reference = _vertexBuffer[tri.Vertex4];
                        tri.Vertex5Reference = _vertexBuffer[tri.Vertex5];
                        tri.Vertex6Reference = _vertexBuffer[tri.Vertex6];

                    }

                    break;
            }
        }