public static RawBmp LoadFromBitMap(string file) { if (!File.Exists(file)) { throw new FileNotFoundException(); } Bitmap bmp = new Bitmap(file, true); BitmapData lockData = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); // Create an array to store image data byte[] imageData = new byte[4 * bmp.Width * bmp.Height]; // Use the Marshal class to copy image data System.Runtime.InteropServices.Marshal.Copy( lockData.Scan0, imageData, 0, imageData.Length); bmp.UnlockBits(lockData); byte[] imageData2 = new byte[4 * bmp.Width * bmp.Height]; RawBmp newBmp = new RawBmp() { ByteArray = imageData, Depth = 4, Height = bmp.Height, Width = bmp.Width, Stride = 4 * bmp.Width }; return(newBmp); }
public static IntPtr CreateSDLTexture(IntPtr rendererPtr, RawBmp rawImg, bool clean = false) { IntPtr texture; int h = rawImg.Height; int w = rawImg.Width; int d = rawImg.Depth * 8; int s = rawImg.Stride; IntPtr pxls; unsafe { fixed(byte *ptr = rawImg.ByteArray) { pxls = new IntPtr(ptr); } } uint rmask = 0xff000000; uint gmask = 0x00ff0000; uint bmask = 0x0000ff00; uint amask = 0x000000ff; SDL.SDL_DestroyTexture(rendererPtr); IntPtr sdlSurface = SDL.SDL_CreateRGBSurfaceFrom(pxls, w, h, d, s, rmask, gmask, bmask, amask); texture = SDL.SDL_CreateTextureFromSurface(rendererPtr, sdlSurface); SDL.SDL_FreeSurface(sdlSurface); int a; uint f; int qw; int qh; int q = SDL.SDL_QueryTexture(texture, out f, out a, out qw, out qh); if (q != 0) { ImGui.Text("QueryResult: " + q); ImGui.Text(SDL.SDL_GetError()); } ImGui.Text("a: " + a + " f: " + f + " w: " + qw + " h: " + qh); return(texture); }
public static RawBmp CreateComponentByteArray(ComponentDesign componentDesign, byte typeID) { //cm resolution var vol = componentDesign.Volume * 100 / 3; int width = (int)Math.Sqrt(vol * componentDesign.AspectRatio); int height = (int)(componentDesign.AspectRatio * width); int v2d = height * width; if (componentDesign.AspectRatio > 1) { width = (int)(width / componentDesign.AspectRatio); height = (int)(height / componentDesign.AspectRatio); } int depth = 4; int size = depth * width * height; int stride = width * depth; byte[] buffer = new byte[size]; for (int ix = 0; ix < width; ix++) { for (int iy = 0; iy < height; iy++) { byte c = typeID; RawBmp.SetPixel(ref buffer, stride, depth, ix, iy, 255, 255, c, 255); } } RawBmp bmp = new RawBmp() { ByteArray = buffer, Stride = stride, Depth = 4, Width = width, Height = height, }; return(bmp); }
private static Bitmap GetString(string str, Color color, Font font) { if (str == "") { return(new Bitmap(1, 1)); } Bitmap bitmap = new Bitmap(1024, 1024); using (Graphics g = Graphics.FromImage(bitmap)) { ConfigureGraphics(g); g.DrawString(str, font, new SolidBrush(color), 64, 64); g.Save(); } RawBmp raw = new RawBmp(bitmap); int left, right, up, down; for (left = 0; left < bitmap.Width; left++) { int i; for (i = 0; i < bitmap.Height; i++) { if (!raw.Trans(left, i)) { break; } } if (i >= bitmap.Height) { continue; } if (!raw.Trans(left, i)) { break; } } for (right = bitmap.Width - 1; right > 0; right--) { int i; for (i = 0; i < bitmap.Height; i++) { if (!raw.Trans(right, i)) { break; } } if (i >= bitmap.Height) { continue; } if (!raw.Trans(right, i)) { break; } } for (up = 0; up < bitmap.Height; up++) { int i; for (i = 0; i < bitmap.Width; i++) { if (!raw.Trans(i, up)) { break; } } if (i >= bitmap.Width) { continue; } if (!raw.Trans(i, up)) { break; } } for (down = bitmap.Height - 1; down > 0; down--) { int i; for (i = 0; i < bitmap.Width; i++) { if (!raw.Trans(i, down)) { break; } } if (i >= bitmap.Width) { continue; } if (!raw.Trans(i, down)) { break; } } int width = right - left + 1, height = down - up + 1; Bitmap result = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(result)) { ConfigureGraphics(g); g.DrawImage( bitmap, new Rectangle(0, 0, width, height), new Rectangle(left, up, width, height), GraphicsUnit.Pixel); g.Save(); } return(result); }
public static List <RawBmp> DealDamage(EntityDamageProfileDB damageProfile, DamageFragment damage) { RawBmp shipDamageProfile = damageProfile.DamageProfile; List <RawBmp> damageFrames = new List <RawBmp>(); var fragmentMass = damage.Mass; var dpos = damage.Position; var dvel = damage.Velocity; var dden = damage.Density; var dlen = damage.Length; var pos = new Vector2(dpos.x, dpos.y); var pixelscale = 0.01; double startMomentum = dvel.Length() * fragmentMass; double momentum = startMomentum; byte[] byteArray = new byte[shipDamageProfile.ByteArray.Length]; Buffer.BlockCopy(shipDamageProfile.ByteArray, 0, byteArray, 0, shipDamageProfile.ByteArray.Length); RawBmp firstFrame = new RawBmp() { ByteArray = byteArray, Height = shipDamageProfile.Height, Width = shipDamageProfile.Width, Depth = shipDamageProfile.Depth, Stride = shipDamageProfile.Stride }; damageFrames.Add(firstFrame); (byte r, byte g, byte b, byte a)savedpx = shipDamageProfile.GetPixel(dpos.x, dpos.y); (int x, int y)savedpxloc = dpos; while (momentum > 0 && dpos.x >= 0 && dpos.x <= shipDamageProfile.Width && dpos.y >= 0 && dpos.y <= shipDamageProfile.Height) { byteArray = new byte[shipDamageProfile.ByteArray.Length]; RawBmp lastFrame = damageFrames.Last(); Buffer.BlockCopy(lastFrame.ByteArray, 0, byteArray, 0, shipDamageProfile.ByteArray.Length); var thisFrame = new RawBmp() { ByteArray = byteArray, Height = shipDamageProfile.Height, Width = shipDamageProfile.Width, Depth = shipDamageProfile.Depth, Stride = shipDamageProfile.Stride }; (byte r, byte g, byte b, byte a)px = thisFrame.GetPixel(dpos.x, dpos.y); if (px.a > 0) { DamageResist damageresist = DamageResistsLookupTable[px.r]; double density = damageresist.Density / (px.a / 255f); //density / health double maxImpactDepth = dlen * dden / density; double depthPercent = pixelscale / maxImpactDepth; dlen -= (float)(damage.Length * depthPercent); var momentumLoss = startMomentum * depthPercent; momentum -= momentumLoss; if (momentum > 0) { px = (px.r, px.g, px.b, 0); damageProfile.ComponentLookupTable[px.g].HTKRemaining -= 1; } } thisFrame.SetPixel(dpos.x, dpos.y, byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)fragmentMass); thisFrame.SetPixel(savedpxloc.x, savedpxloc.y, savedpx.r, savedpx.g, savedpx.b, savedpx.a); damageFrames.Add(thisFrame); savedpxloc = dpos; savedpx = px; double dt = 1 / dvel.Length(); pos.X += dvel.X * dt; pos.Y += dvel.Y * dt; dpos.x = Convert.ToInt32(pos.X); dpos.y = Convert.ToInt32(pos.Y); } Buffer.BlockCopy(damageFrames.Last().ByteArray, 0, byteArray, 0, shipDamageProfile.ByteArray.Length); var finalFrame = new RawBmp() { ByteArray = byteArray, Height = shipDamageProfile.Height, Width = shipDamageProfile.Width, Depth = shipDamageProfile.Depth, Stride = shipDamageProfile.Stride }; finalFrame.SetPixel(savedpxloc.x, savedpxloc.y, savedpx.r, savedpx.g, savedpx.b, savedpx.a); return(damageFrames); }