Example #1
0
        public static F3DEXTextureWrapper GetTextureWrapper(Texture texture, F3DEX_G_SetTile command, F3DEX_G_Texture textureCommand)
        {
            if (_textureWrappers.ContainsKey(texture))
                return _textureWrappers[texture];

            F3DEXTextureWrapper wrapper = new F3DEXTextureWrapper(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;
        }
Example #3
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 / widthInBytes;

            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 #4
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 #5
0
        private static void ParseCommand(F3DEXCommand command)
        {
            TileDescriptor tile;
            //Here handle extra command things (like if it's referencing textures and such)
            switch (command.CommandID)
            {
                case F3DEXCommandID.F3DEX_G_TEXTURE:
                    F3DEX_G_Texture textureCmd = (F3DEX_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 F3DEXCommandID.F3DEX_G_SETTILE:
                    _lastSetTile = (F3DEX_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 F3DEXCommandID.F3DEX_G_SETTILESIZE:
                    _lastSetTileSize = (F3DEX_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 F3DEXCommandID.F3DEX_G_SETTIMG:
                    _lastSetTImg = (F3DEX_G_SetTImg)command;
                    TMEM.LastTImgCommand = _lastSetTImg;
                    //TMEM.RAMTexturePixelSize = _lastSetTImg.PixelSize;
                    //TMEM.RAMTextureAddress = _lastSetTImg.ImageAddress;
                    //TMEM.RAMTextureImageFormat = _lastSetTImg.Format;
                    //TMEM.RAMTextureWidth = _lastSetTImg.Width;

                    break;
                case F3DEXCommandID.F3DEX_G_LOADBLOCK:
                    _lastLoadBlock = (F3DEX_G_LoadBlock)command;

                    TMEM.LoadBlockIntoTMem(_lastLoadBlock);
                    break;
                case F3DEXCommandID.F3DEX_G_LOADTLUT:
                    _lastLoadTLut = (F3DEX_G_LoadTLut)command;

                    TMEM.LoadTLutIntoTMem(_lastLoadTLut);
                    break;
                case F3DEXCommandID.F3DEX_G_VTX:
                    F3DEX_G_Vtx vtx = (F3DEX_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 F3DEXCommandID.F3DEX_G_TRI1:
                case F3DEXCommandID.F3DEX_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 == F3DEXCommandID.F3DEX_G_TRI1)
                    {
                        F3DEX_G_Tri1 tri = (F3DEX_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 == F3DEXCommandID.F3DEX_G_TRI2)
                    {
                        F3DEX_G_Tri2 tri = (F3DEX_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;
            }
        }
 public F3DEXTextureWrapper(Texture texture, F3DEX_G_SetTile command, F3DEX_G_Texture textureCommand)
 {
     F3DTexture = texture;
     SetTileCommand = command;
     TextureCommand = textureCommand;
 }