private static void FillIndexedProperties(TexHeader header, GLTexture palettes) { using (GLService.AcquireContext()) { int frameBuffer = GL.Ext.GenFramebuffer(); GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, frameBuffer); GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, palettes.Id, 0); byte[] pixels = new byte[4]; GL.ReadPixels(0, 0, 1, 1, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, pixels); GL.Ext.DeleteFramebuffer(frameBuffer); if (pixels[3] == 0) { header.ColorKeyFlag = 1; } } header.BytesPerPixel = 1; header.BitDepth = 8; header.BitsPerIndex = 8; header.BitsPerPixel = 8; header.PaletteFlag = 1; header.NumberOfPalettes = palettes.Height; header.NumberOfColorsPerPalette1 = palettes.Width; header.NumberOfColorsPerPalette2 = palettes.Width; header.PaletteSize = palettes.Width * palettes.Height; header.Unknown7 = 288; header.Unknown8 = 224; }
private void DrawPreview() { using (GLService.AcquireContext(GLControlElement.WindowInfo)) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GLLocationRenderer.Draw(); } }
private void ConfigViewport() { using (GLService.AcquireContext()) { GL.ClearColor(Color4.DimGray); int w = GLControlElement.Width; int h = GLControlElement.Height; GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(0, w, h, 0, -1, 1); GL.Viewport(0, 0, w, h); GLLocationRenderer.DrawEvent.NullSafeSet(); } }
public async override Task <GLTexture> ReadTextureAsync(CancellationToken cancelationToken) { if (cancelationToken.IsCancellationRequested) { return(RaiseTextureReaded(null)); } using (SafeHGlobalHandle pixels = new SafeHGlobalHandle(_map.Width * _map.Height * 3)) { using (Stream stream = pixels.OpenStream(FileAccess.ReadWrite)) { if (cancelationToken.IsCancellationRequested) { return(RaiseTextureReaded(null)); } CreateBackground(stream); foreach (MimTile tile in _map.LayredTiles[0]) { if (cancelationToken.IsCancellationRequested) { return(RaiseTextureReaded(null)); } //if (tile.Layered.Z < _map.LayredTiles[0][0].Layered.Z) // break; int position = tile.Layered.GetPositionInImage(_map, _map.Width * 3); stream.Seek(position, SeekOrigin.Begin); Color[] colors = ReadTileColors(tile); for (int i = 0; i < 16; i++) { for (int k = 0; k < 16; k++) { WriteColor(colors[i * 16 + k], tile.Layered.BlendType, stream); } if (i < 15) { stream.Seek((_map.Width - 16) * 3, SeekOrigin.Current); } } } } if (cancelationToken.IsCancellationRequested) { return(RaiseTextureReaded(null)); } PixelFormatDescriptor pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb; int textureId; using (GLService.AcquireContext()) { GL.GenTextures(1, out textureId); GL.BindTexture(TextureTarget.Texture2D, textureId); GL.TexImage2D(TextureTarget.Texture2D, 0, pixelFormat, _map.Width, _map.Height, 0, pixelFormat, pixelFormat, pixels.DangerousGetHandle()); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); } if (cancelationToken.IsCancellationRequested) { return(RaiseTextureReaded(null)); } return(new GLTexture(textureId, _map.Width, _map.Height, pixelFormat)); } }
private void OnWindowDeactivated(object sender, EventArgs eventArgs) { GLService.UnregisterDrawMethod(DrawPreview); GLService.UnsubscribeControl(GLControlHost); }
private void OnWindowActivated(object sender, EventArgs eventArgs) { GLService.SubscribeControl(GLControlHost); GLLocationRenderer.DrawEvent = GLService.RegisterDrawMethod(DrawPreview); ConfigViewport(); }
private void DrawPreview() { Dictionary <char, int> charsToBytes; lock (_charsToIndices) charsToBytes = ExpandCollectionArray(_charsToIndices); string text = Dispatcher.Invoke(() => PreviewTextBox.Text); GLRectangle rectangle = new GLRectangle { Height = 23 }; using (GLService.AcquireContext(GLControlElement.WindowInfo)) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); using (GLShaders.PalettedTexture.Use(_texture, _palette, _currentPalette)) _texture.Draw(0, 0, 0); for (int i = 0; i < _charactersWidths.Length; i++) { rectangle.X = (i % 21) * 24; rectangle.Y = (i / 21) * 24; rectangle.Width = _charactersWidths[i] * 2; if (i == _currentIndex) { rectangle.DrawSolid(SelectedColor); } else { rectangle.DrawBorder(GridColor); } } float x = 0; float y = _texture.Height + 24; using (GLShaders.PalettedTexture.Use(_texture, _palette, FF8TextTagColor.White - FF8TextTagColor.Disabled)) { foreach (char ch in text) { if (ch == '\r') { continue; } if (ch == '\n') { x = 0; y += 24; continue; } int index; if (charsToBytes.TryGetValue(ch, out index)) { index -= 32; const float h = 23; float w = _charactersWidths[index] * 2; _texture.Draw(x, y, 0, (index % 21) * 24f, (index / 21) * 24, w, h); x += w; } } } GL.Disable(EnableCap.Blend); } }
private void OnWindowActivated(object sender, EventArgs e) { GLService.SubscribeControl(GLControlHost); _drawEvent = GLService.RegisterDrawMethod(DrawPreview); ConfigPreview(); }