public static void WriteToStream(Image img, Stream stream) { IntPtr point = img.gdImageStructPtr; DLLImports.gdImageStruct gdImageStruct = Marshal.PtrToStructure<DLLImports.gdImageStruct>(point); var wrapper = new gdStreamWrapper(stream); DLLImports.gdImageJpegCtx(ref gdImageStruct, ref wrapper.IOCallbacks); }
public static void Write(Image bmp, Stream stream) { DLLImports.gdImageSaveAlpha(bmp.gdImageStructPtr, 1); //MARSHALLING? DLLImports.gdImageStruct gdImageStruct = Marshal.PtrToStructure<DLLImports.gdImageStruct>(bmp.gdImageStructPtr); var wrapper = new gdStreamWrapper(stream); DLLImports.gdImagePngCtx(ref gdImageStruct, ref wrapper.IOCallbacks); }
//add png specific method later public static void WriteToFile(Image img, string filePath) { DLLImports.gdImageSaveAlpha(img.gdImageStructPtr, 1); if (!DLLImports.gdSupportsFileType(filePath, true)) { throw new InvalidOperationException("File type not supported or not found."); } else { if (!DLLImports.gdImageFile(img.gdImageStructPtr, filePath)) { throw new FileLoadException("Failed to write to file."); } } }
//add jpg specific method later public static void Write(Image img, string filePath) { DLLImports.gdImageSaveAlpha(img.gdImageStructPtr, 1); if (!DLLImports.gdSupportsFileType(filePath, true)) { throw new InvalidOperationException(SR.Format(SR.FileTypeNotSupported, filePath)); } else { if (!DLLImports.gdImageFile(img.gdImageStructPtr, filePath)) { throw new FileLoadException(SR.Format(SR.WriteToFileFailed, filePath)); } } }
//add jpg specific method later public static Image Load(string filePath) { if (!File.Exists(filePath)) { throw new FileNotFoundException(SR.Format(SR.MalformedFilePath, filePath)); } else if (DLLImports.gdSupportsFileType(filePath, false)) { Image img = new Image(DLLImports.gdImageCreateFromFile(filePath)); if (!img.TrueColor) { DLLImports.gdImagePaletteToTrueColor(img.gdImageStructPtr); } return img; } else { throw new FileLoadException(SR.Format(SR.FileTypeNotSupported, filePath)); } }
//add png specific method later public static Image Load(string filePath) { if (!File.Exists(filePath)) { throw new FileNotFoundException("Malformed file path given."); } else if (DLLImports.gdSupportsFileType(filePath, false)) { Image img = new Image(DLLImports.gdImageCreateFromFile(filePath)); DLLImports.gdImageStruct gdImageStruct = Marshal.PtrToStructure<DLLImports.gdImageStruct>(img.gdImageStructPtr); if (!img.TrueColor) { DLLImports.gdImagePaletteToTrueColor(img.gdImageStructPtr); gdImageStruct = Marshal.PtrToStructure<DLLImports.gdImageStruct>(img.gdImageStructPtr); } return img; } else { throw new FileLoadException("File type not supported."); } }
//Stamping an Image onto another public static void Draw(this Image destinationImage, Image sourceImage, int xOffset, int yOffset) { //turn alpha blending on for drawing DLLImports.gdImageAlphaBlending(destinationImage.gdImageStructPtr, 1); unsafe { DLLImports.gdImageStruct* pStructSource = (DLLImports.gdImageStruct*)sourceImage.gdImageStructPtr; DLLImports.gdImageStruct* pStructDest = (DLLImports.gdImageStruct*)destinationImage.gdImageStructPtr; //loop through the source image for (int y = 0; y < sourceImage.HeightInPixels; y++) { for (int x = 0; x < sourceImage.WidthInPixels; x++) { //ignores what falls outside the bounds of dsetination image if ((y + yOffset) >= destinationImage.HeightInPixels || (x + xOffset) >= destinationImage.WidthInPixels) { continue; } int sourceColor = pStructSource->tpixels[y][x]; int alpha = (sourceColor >> 24) & 0xff; //should not have 127 as magic if (alpha == 127) { continue; } int destColor = pStructDest->tpixels[y + yOffset][x + xOffset]; int blendedColor = DLLImports.gdAlphaBlend(destColor, sourceColor); pStructDest->tpixels[y + yOffset][x + xOffset] = blendedColor; } } } }
/// <summary> /// Loads this scene node from a given binary reader. /// </summary> /// <param name="reader">Binary reader to load this scene node from.</param> public override void Load(BinaryReader reader) { // Load all the basic entity details. base.Load(reader); // Load all the camera specific details. _viewport.X = reader.ReadInt16(); _viewport.Y = reader.ReadInt16(); _viewport.Width = reader.ReadInt16(); _viewport.Height = reader.ReadInt16(); _boundingRectangle.Width = _viewport.Width; _boundingRectangle.Height = _viewport.Height; _clearColor = reader.ReadInt32(); _zoom = reader.ReadSingle(); if (reader.ReadBoolean() == true) { string imageUrl = reader.ReadString(); int cellWidth = reader.ReadInt32(); int cellHeight = reader.ReadInt32(); int hSpacing = reader.ReadInt16(); int vSpacing = reader.ReadInt16(); if (ResourceManager.ResourceExists(imageUrl) == true) _backgroundImage = GraphicsManager.LoadImage(imageUrl, cellWidth, cellHeight, hSpacing, vSpacing, 0); } }
/// <summary> /// Responsable for removing references of this object and deallocated /// resources that have been allocated. /// </summary> public override void Dispose() { // Call the abstracted base method to clean up general things. base.Dispose(); // Deinitialize this entitys collision. DeinitializeCollision(); // Nullify media. _image = null; _bitmapFont = null; // Remove event nodes. _eventNodes.Clear(); }
/// <summary> /// Reloads the currently selected font. /// </summary> private void ReloadFont() { // Check we have selected a node. if (fileTreeView.SelectedNode == null || _loadFontThread != null) return; // Work out the path to the node. _loadFontUrl = Engine.GlobalInstance.FontPath + "\\" + fileTreeView.SelectedNode.FullPath; // Check its not a dictionary. if (ResourceManager.ResourceExists(_loadFontUrl) == false) return; // Start up the loading thread. _font = null; _fontImage = null; _loadFontThread = new Thread(new ThreadStart(LoadFont)); _loadFontThread.IsBackground = true; _loadFontThread.Start(); }
/// <summary> /// Entry point for the font loading thread. /// </summary> private void LoadFont() { lock (_fontLock) { _font = GraphicsManager.LoadFont(_loadFontUrl); _fontImage = GraphicsManager.LoadImage(_font.NormalImage.URL, 0); } _loadFontThread = null; }
/// <summary> /// Called when the visibility of this window is changed. /// </summary> /// <param name="sender">Recent file menu that caused this event.</param> /// <param name="e">Arguments explaining why this event was invoked.</param> private void ImageSelectorWindow_VisibleChanged(object sender, EventArgs e) { if (Visible == true) { fileTreeView.Nodes.Clear(); _ignoreSelections = true; PopulateFileTree(Engine.GlobalInstance.FontPath, fileTreeView.Nodes); _ignoreSelections = false; if (_font != null && _fontImage == null) _fontImage = GraphicsManager.LoadImage(_font.NormalImage.URL, 0); } else if (_canvas != null) { fileTreeView.Nodes.Clear(); } }
/// <summary> /// Loads this scene node from a given binary reader. /// </summary> /// <param name="reader">Binary reader to load this scene node from.</param> public override void Load(BinaryReader reader) { base.Load(reader); _event = reader.ReadString(); int flagMask = reader.ReadInt32(); _visible = (flagMask & 1) != 0; _enabled = (flagMask & 2) != 0; _solid = (flagMask & 4) != 0; if ((flagMask & 262144) != 0) { string meshUrl = reader.ReadString(); if (ResourceManager.ResourceExists(meshUrl) == true) _mesh = GraphicsManager.LoadMesh(meshUrl, 0); } if ((flagMask & 8) != 0) { string imageUrl = reader.ReadString(); int cellWidth = reader.ReadInt32(); int cellHeight = reader.ReadInt32(); int hSpacing = reader.ReadInt16(); int vSpacing = reader.ReadInt16(); if (ResourceManager.ResourceExists(imageUrl) == true) _image = GraphicsManager.LoadImage(imageUrl,cellWidth,cellHeight,hSpacing,vSpacing, 0); } if ((flagMask & 16) != 0) { string fontUrl = reader.ReadString(); if (ResourceManager.ResourceExists(fontUrl) == true) _bitmapFont = GraphicsManager.LoadFont(fontUrl); } if ((flagMask & 32) != 0) _text = reader.ReadString(); if ((flagMask & 64) != 0) { _boundingRectangle.X = reader.ReadInt32(); _boundingRectangle.Y = reader.ReadInt32(); } if ((flagMask & 128) != 0) { _boundingRectangle.Width = reader.ReadInt32(); _boundingRectangle.Height = reader.ReadInt32(); } if ((flagMask & 256) != 0) _frame = reader.ReadInt32(); if ((flagMask & 512) != 0) _renderMode = (EntityRenderMode)reader.ReadByte(); else _renderMode = EntityRenderMode.Image; if ((flagMask & 1024) != 0) _blendMode = (BlendMode)reader.ReadByte(); else _blendMode = BlendMode.Alpha; if ((flagMask & 2048) != 0) _color = reader.ReadInt32(); else _color = unchecked((int)0xFFFFFFFF); if ((flagMask & 4096) != 0) { if (_collisionPolygon == null) InitializeCollision(); int count = reader.ReadInt32(); _collisionPolygon.Layers = new int[count]; for (int i = 0; i < count; i++) _collisionPolygon.Layers[i] = reader.ReadInt32(); } if ((flagMask & 8192) != 0) { _collisionRectangle.X = reader.ReadInt32(); _collisionRectangle.Y = reader.ReadInt32(); } if ((flagMask & 16384) != 0) { _collisionRectangle.Width = reader.ReadInt32(); _collisionRectangle.Height = reader.ReadInt32(); } if ((flagMask & 32768) != 0) _depthLayer = reader.ReadInt32(); if ((flagMask & 65536) != 0) _depthMode = (EntityDepthMode)reader.ReadByte(); if ((flagMask & 131072) != 0) _shader = GraphicsManager.LoadShader(reader.ReadString()); }
/// <summary> /// Reloads the currently selected image. /// </summary> private void ReloadImage() { // Check we have selected a node. if (fileTreeView.SelectedNode == null || _loadImageThread != null) return; // Work out the path to the node. _loadImageUrl = Engine.GlobalInstance.GraphicPath + "\\" + fileTreeView.SelectedNode.FullPath; // Check its not a dictionary. if (ResourceManager.ResourceExists(_loadImageUrl) == false) return; // Start up the loading thread. int originalColorKey = GraphicsManager.ColorKey; GraphicsManager.ColorKey = maskColorPanel.BackColor.ToArgb(); _image = GraphicsManager.LoadImage(_loadImageUrl, 0, false); GraphicsManager.ColorKey = originalColorKey; }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void clearBackgroundToolStripMenuItem_Click(object sender, EventArgs e) { _backgroundImage = null; Editor.GlobalInstance.CameraNode.BackgroundImage = new Graphics.Image(ReflectionMethods.GetEmbeddedResourceStream("grid.png"), 0); }
/// <summary> /// Resets this entity to the state it was in when it was created. /// </summary> public override void Reset() { base.Reset(); if (_collisionPolygon != null) DeinitializeCollision(); _forceVisibility = false; _forceBoundingBoxVisibility = false; _forceCollisionBoxVisibility = false; _visible = true; _enabled = true; _solid = false; _event = ""; _renderEventLines = false; _eventNodes.Clear(); _eventLineColor = unchecked((int)0xFFFF0000); _image = null; _mesh = null; _frame = 0; _renderMode = EntityRenderMode.Rectangle; _boundingRectangle = new Rectangle(0, 0, 16, 16); _collisionRectangle = new Rectangle(0, 0, 16, 16); _color = unchecked((int)0xFFFFFFFF); _blendMode = BlendMode.Alpha; _text = ""; _bitmapFont = null; _renderBoundingBox = false; _renderCollisionBox = false; _renderSizingPoints = false; _boundingBoxColor = unchecked((int)0xFF0000FF); _collisionBoxColor = unchecked((int)0xFF666666); _sizingPointsColor = unchecked((int)0xFFFFFFFF); _sizingPointsSize = 5; _collisionPolygon = null; _depthLayer = 0; _depthMode = EntityDepthMode.SubtractCollisionBoxBottom; _triggered = false; _shader = null; }
/// <summary> /// Loads this modifier from a given binary reader. /// </summary> /// <param name="reader">Binary reader to load this modifier from.</param> public override void Load(BinaryReader reader) { // Load ze base! base.Load(reader); // Load properties. _renderMode = (EntityRenderMode)reader.ReadInt32(); _blendMode = (BlendMode)reader.ReadInt32(); _text = reader.ReadString(); _isVisible = reader.ReadBoolean(); _isEnabled = reader.ReadBoolean(); if (reader.ReadBoolean() == true) { string imageUrl = reader.ReadString(); int cellWidth = reader.ReadInt32(); int cellHeight = reader.ReadInt32(); int hSpacing = reader.ReadInt16(); int vSpacing = reader.ReadInt16(); if (ResourceManager.ResourceExists(imageUrl) == true) { _image = GraphicsManager.LoadImage(imageUrl, cellWidth, cellHeight, hSpacing, vSpacing, 0); _image.Origin = new Point(cellWidth / 2, cellHeight / 2); } } if (reader.ReadBoolean() == true) { string fontUrl = reader.ReadString(); if (ResourceManager.ResourceExists(fontUrl) == true) _font = GraphicsManager.LoadFont(fontUrl); } if (reader.ReadBoolean() == true) { string shaderUrl = reader.ReadString(); if (ResourceManager.ResourceExists(shaderUrl) == true) { _shader = GraphicsManager.LoadShader(shaderUrl); _shaderFileEditor.FileUrl = shaderUrl; } } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void setBackgroundToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Image Files|*.png;*.tga;*.bmp"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { _backgroundImage = GraphicsManager.LoadImage(dialog.FileName, 0); Editor.GlobalInstance.CameraNode.BackgroundImage = _backgroundImage; } }
/// <summary> /// Initializes a new instance of this class, and sets up the form. /// </summary> public EditorWindow() { // Initialize the windows form controls. InitializeComponent(); // Refresh the recent file list. RefreshRecentFiles(); // Create the map canvas to render to. _mapCanvas = GraphicsManager.CreateCanvas(mapPanel, 0, new CanvasRenderHandler(Render)); // Grab the path marker image. _pathMarkerImage = new Graphics.Image(ReflectionMethods.GetEmbeddedResourceStream("path_marker.png"), 0); // Syncronize the window to the current data. SyncronizeWindow(); // Create a new event listener to listen out for input events. _listener = new EventListener(new ProcessorDelegate(EventCaptured)); EventManager.AttachListener(_listener); }