//Constructors public Score(string key, IntPtr render) : base() { color = new SDL_Color { r = 255, g = 255, b = 255, a = 0 }; SpriteEntity scoreSprite; TextureStruct textTexture = TextGenerator.GenerateTexture(render, TextGenerator.lazyFont, "void", color); if (!SpriteEntity.TextureExists(key)) { scoreSprite = new SpriteEntity("scorePlayer1", textTexture); } else { // If it already exists, simply use existing one scoreSprite = AllSprites["scorePlayer1"]; } name = key; AddSprite(key, scoreSprite); ResetPoint(render); }
public TextureStruct TextToTexture(IntPtr font, string text, SDL_Color color) { TextureStruct finalTexture = new TextureStruct(IntPtr.Zero, 0, 0); IntPtr textSurface = TTF_RenderText_Solid(font, text, color); IntPtr myTexture = IntPtr.Zero; if (textSurface == IntPtr.Zero) { Console.Write("Unable to render text surface! SDL_ttf Error: %s\n", SDL_GetError()); } else { //Create texture from surface pixels myTexture = SDL_CreateTextureFromSurface(renderer, textSurface); if (myTexture == IntPtr.Zero) { Console.Write("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError()); } //Create texture struct finalTexture.h = ((SDL_Surface)Marshal.PtrToStructure(textSurface, typeof(SDL_Surface))).h; finalTexture.w = ((SDL_Surface)Marshal.PtrToStructure(textSurface, typeof(SDL_Surface))).w; finalTexture.texture = myTexture; //Get rid of old surface SDL_FreeSurface(textSurface); } return(finalTexture); }
/* #################################################################### */ /* # METHODS # */ /* #################################################################### */ public void Create(string text, string font, int size, SDL_Color colour) { var surface = SDL_ttf.TTF_RenderUTF8_Solid(SDLTtf.Instance.GetFont(font, size), text, colour); _texture.CreateFromSurface(surface); SDL.SDL_FreeSurface(surface); Position = new Vector2 <int>(); }
public RenderedText(string text, string fontName) : base() { this.fontName = fontName; if (!Fonts.ContainsKey(fontName)) { throw new Exception("Font name " + fontName + " does not exist in the collection! Make sure to call RenderedText.AddFont() before creating a RenderedText object!"); } color = new SDL_Color(255, 255, 255, 255); this.Text = text; }
void IGraphicsDriver.SetPalette(GraphicsColor[] colors) { SDL_Color[] palette = new SDL_Color[colors.Length]; for (int i = 0; i < colors.Length; i++) { palette[i].r = colors[i].R; palette[i].g = colors[i].G; palette[i].b = colors[i].B; palette[i].unused = 0; } SDL_SetPalette(this.surfacePtr, SDL_LOGPAL | SDL_PHYSPAL, palette, 0, palette.Length); }
public Sprite(string key, IntPtr font, string text, SDL_Color color) { TextureStruct texture; if (textureList.ContainsKey(key)) { texture = textureList[key]; } else { texture = TextToTexture(font, text, color); textureList.Add(key, texture); } SetupSpriteEntity(key); }
private void GetTextureAndRect(int x, int y, string text, IntPtr ttfFont, ref IntPtr sdlTexture, ref SDL_Rect rect) { var textColor = new SDL_Color { r = 255, g = 255, b = 255, a = 0 }; var surface = Sdl(TTF_RenderText_Solid(ttfFont, text, textColor)); sdlTexture = Sdl(SDL_CreateTextureFromSurface(_renderer, surface)); var surface2 = (SDL_Surface)Marshal.PtrToStructure(surface, typeof(SDL_Surface)); var textWidth = surface2.w; var textHeight = surface2.h; SDL_FreeSurface(surface); rect.x = x; rect.y = y; rect.w = textWidth; rect.h = textHeight; }
internal void Text(string text, int x, int y) { if (text == "") { return; } if (lastText != text) { SDL_Color color = new SDL_Color(); color.a = 255; color.r = 0; color.g = 0; color.b = 0; SDL_FreeSurface(textSurface); SDL_DestroyTexture(textTexture); textSurface = TTF_RenderText_Solid(font, text, color); if (textSurface == IntPtr.Zero) { throw new Exception("Could not render text to surface: " + SDL_GetError()); } textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); if (textTexture == IntPtr.Zero) { throw new Exception("Could not create a texture from the font surface: " + SDL_GetError()); } lastText = text; } SDL_Rect targetRect = new SDL_Rect(); targetRect.x = x; targetRect.y = y; SDL_QueryTexture(textTexture, out _, out _, out targetRect.w, out targetRect.h); SDL_RenderCopy(renderer, textTexture, IntPtr.Zero, ref targetRect); }
//Constructors public MenuTextModel(string key, string menuText) : base() { name = key; status = "Inactive"; posX = 0; posY = 0; SDL_Color color = new SDL_Color { r = 255, g = 215, b = 0, a = 0 }; Sprite menuTextSprite; if (!Sprite.TextureExists(key + "Active")) { menuTextSprite = new Sprite(key + "Active", TextGenerator.pixel_millenium_big, menuText, color); menuTextSprite.Pos.x = posX; menuTextSprite.Pos.y = posY; AddSprite("Active", menuTextSprite); } color = new SDL_Color { r = 255, g = 255, b = 255, a = 0 }; if (!Sprite.TextureExists(key + "Inactive")) { menuTextSprite = new Sprite(key + "Inactive", TextGenerator.pixel_millenium_big, menuText, color); menuTextSprite.Pos.x = posX; menuTextSprite.Pos.y = posY; AddSprite("Inactive", menuTextSprite); } }
//TODO: change it to draw into a new texture instead of on screen! (SDL_SetRenderTarget) static public void DrawFilledCircle(Coordinate center, double radius, SDL_Color color) { SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_Rect line; double xUp; double yUp; for (double x = 0; x < 2 * radius; x++) { xUp = x - radius; yUp = Math.Sqrt(radius * radius - xUp * xUp); line.x = (int)Math.Round(xUp + center.x); line.y = (int)Math.Round(-yUp + center.y); line.w = 1; line.h = (int)Math.Round(2 * yUp); //draw Rectangle SDL_RenderFillRect(renderer, ref line); } }
//Constructors public ClockModel(int offSet) { this.offSet = offSet; color = new SDL_Color { r = 0, g = 0, b = 0, a = 0 }; Sprite clockSprite; if (!Sprite.TextureExists("Clock")) { clockSprite = new Sprite("Clock", TextGenerator.pixel_millenium_medium, "NaN:NaN", color); posX = offSet; posY = offSet; AddSprite("Clock", clockSprite); } }
void IGraphicsDriver.SetPalette(GraphicsColor[] colors) { //for (int i = 0; i < colors.Length; i++) //{ // this.palette[i] = (uint)(((byte)0xff << 24) | (colors[i].R << 16) | (colors[i].G << 8) | colors[i].B); //} SDL_Color[] palette = new SDL_Color[colors.Length]; for (int i = 0; i < colors.Length; i++) { palette[i].r = colors[i].R; palette[i].g = colors[i].G; palette[i].b = colors[i].B; //palette[i].unused = 0; } SDL_Surface surface = (SDL_Surface)Marshal.PtrToStructure(this.surfacePtr, typeof(SDL_Surface)); SDL_PixelFormat format = (SDL_PixelFormat)Marshal.PtrToStructure(surface.format, typeof(SDL_PixelFormat)); SDL_SetPaletteColors(format.palette, palette, 0, palette.Length); this.texturePtr = SDL_CreateTextureFromSurface(this.rendererPtr, this.surfacePtr); }
public void TimeIsUp(int ennemyHit, int timesHit) { this.timeUp = !timeUp; SDL_Color color = new SDL_Color() { r = 0, g = 0, b = 0, a = 0 }; title = new Sprite("TimeUp", TextGenerator.pixel_millenium_big, "Time is UP!", color); score1 = new Sprite("Score1", TextGenerator.pixel_millenium_medium, "You put " + ennemyHit + " holes in your ennemy!", color); score2 = new Sprite("Score2", TextGenerator.pixel_millenium_medium, "Your enemy got you " + timesHit + " times.", color); title.Pos.x = windowX / 2 - title.Pos.w / 2; title.Pos.y = windowY / 2 - title.Pos.h / 2; score1.Pos.x = windowX / 2 - score1.Pos.w / 2; score1.Pos.y = title.Pos.y + title.Pos.h + 100; score2.Pos.x = windowX / 2 - score2.Pos.w / 2; score2.Pos.y = score1.Pos.y + score1.Pos.h + 50; }
public static extern IntPtr TTF_RenderText_Solid(/* TTF_Font * */IntPtr font, /* char * */string text, SDL_Color fg);
/* For compatibility with previous versions, here are the old functions */ /* SDL_Surface* */ public static IntPtr TTF_RenderText(/* TTF_Font* */IntPtr font, /* char* */string text, SDL_Color fg, SDL_Color bg) { return TTF_RenderText_Shaded(font, text, fg, bg); }
public static extern IntPtr TTF_RenderGlyph_Solid(/* TTF_Font * */IntPtr font, Uint16 ch, SDL_Color fg);
public static extern IntPtr TTF_RenderGlyph_Shaded(/* TTF_Font * */IntPtr font, /* Uint16 */char ch, SDL_Color fg, SDL_Color bg);
public static extern int SDL_SetPalette(/* SDL_Surface * */IntPtr surface, int flags, /* SDL_Color * */ref SDL_Color colors, int firstcolor, int ncolors);
public static extern int SDL_SetColors(/* SDL_Surface * */IntPtr surface, /* SDL_Color * */ref SDL_Color colors, int firstcolor, int ncolors);
public static extern IntPtr RenderUTF8_Shaded(IntPtr font, string text, SDL_Color fg, SDL_Color bg);
public static extern IntPtr TTF_RenderUTF8_Solid(/* TTF_Font * */IntPtr font, /* char * */byte[] text, SDL_Color fg);
/* SDL_Surface* */ public static IntPtr TTF_RenderUTF8(/* TTF_Font* */IntPtr font, /* char* */byte[] text, SDL_Color fg, SDL_Color bg) { return TTF_RenderUTF8_Shaded(font, text, fg, bg); }
public void ReplaceText(string newText, IntPtr font, SDL_Color color) { TextureStruct newTexture = TextToTexture(font, newText, color); this.ReplaceTexture(newTexture); }
/* #################################################################### */ /* # METHODS # */ /* #################################################################### */ public void Create(string text, string font, int size, SDL_Color colour) { Position = new Vector2 <int>(); _text = new SDLText(); _text.Create(text, font, size, colour); }
public static extern IntPtr RenderUTF8_Blended_Wrapped(IntPtr font, string text, SDL_Color fg, int wrapLength);
//TODO: change it to draw into a new texture instead of on screen! (SDL_SetRenderTarget) static public void DrawFilledRectangle(Coordinate A, Coordinate B, Coordinate C, Coordinate D, SDL_Color color) { SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); Coordinate low = new Coordinate(); Coordinate left = new Coordinate(); Coordinate high = new Coordinate(); Coordinate right = new Coordinate(); //Case A is the lowest point in X-Axis if ((A.x <= D.x) && (A.x <= B.x)) { low = A; high = C; left = B; right = D; } //Case B is the lowest point in X-Axis else if ((B.x <= C.x) && (B.x <= A.x)) { low = B; high = D; left = C; right = A; } //Case C is the lowest point in X-Axis else if ((C.x <= D.x) && (C.x <= B.x)) { low = C; high = A; left = D; right = B; } //Case D is the lowest point in X-Axis else if ((D.x <= C.x) && (D.x <= A.x)) { low = D; high = B; left = A; right = C; } SDL_Rect line; //Handle cases of rectangles that are NOT angled if (low.x - left.x == 0) {//it is highly possible that one needs to use right instead of left here because of X-axe inversion! line.x = (int)Math.Round(low.x); line.y = (int)Math.Round(low.y); line.h = (int)Math.Round(left.y - low.y); line.w = (int)Math.Round(right.x - low.x); //draw Rectangle SDL_RenderFillRect(renderer, ref line); return; //Leave because everything is draw! } //Get line equations for all 4 lines; y= m*x + p double mLowLeft = (low.y - left.y) / (double)(low.x - left.x); double pLowLeft = low.y - mLowLeft * low.x; double mLowRight = (low.y - right.y) / (double)(low.x - right.x); double pLowRight = low.y - mLowRight * low.x; double mLeftHigh = (high.y - left.y) / (double)(high.x - left.x); double pLeftHigh = high.y - mLeftHigh * high.x; double mRightHigh = (high.y - right.y) / (double)(high.x - right.x); double pRightHigh = high.y - mRightHigh * high.x; double mSmall = mLowLeft; double pSmall = pLowLeft; double mBig = mLowRight; double pBig = pLowRight; //Draw rectangle low1, low2, high1, high2 starting from left to right for (double x = low.x; x < high.x; x++) { if (x >= left.x) { mSmall = mLeftHigh; pSmall = pLeftHigh; } if (x >= right.x) { mBig = mRightHigh; pBig = pRightHigh; } line.x = (int)Math.Round(x); line.y = (int)Math.Round(x * mBig + pBig); line.h = (int)Math.Round(-x * mBig - pBig + x * mSmall + pSmall); line.w = 1; //draw Rectangle SDL_RenderFillRect(renderer, ref line); } }
/* SDL_Surface* */ public static IntPtr TTF_RenderUNICODE(/* TTF_Font* */IntPtr font, /* Uint16* */string text, SDL_Color fg, SDL_Color bg) { return TTF_RenderUNICODE_Shaded(font, text, fg, bg); }
public static extern IntPtr TTF_RenderUNICODE_Solid(/* TTF_Font * */IntPtr font, /* Uint16 * */string text, SDL_Color fg);
public static extern int SDL_SetPaletteColors( IntPtr palette, SDL_Color[] colors, int firstcolor, int ncolors );
public static extern IntPtr TTF_RenderUTF8_Blended(/* TTF_Font * */IntPtr font, /* char * */IntPtr text, SDL_Color fg);
public static Color FromSDLColor(this SDL_Color color) => Color.FromArgb(color.a, color.r, color.g, color.b);
private static void SetColor(SDL_Color color) { SDL_SetRenderDrawColor(renderer, color.r, color.b, color.g, color.a); }
public FC_Font(IntPtr ttf, IntPtr gRenderer, SDL_Color color) { height = TTF_FontHeight(ttf); heightPadd = height + 1; ascent = TTF_FontAscent(ttf); descent = -TTF_FontDescent(ttf); baseline = height - descent; default_color = color; int w = heightPadd * 12; int h = heightPadd * 12; // FontTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, (int)SDL_TextureAccess.SDL_TEXTUREACCESS_TARGET, w * 16, h * 16); // SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING IntPtr fontSurface = SDL_CreateRGBSurface(0, h, w, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF); SDL_Rect cursorRect = new SDL_Rect(); for (int i = 0; i < 144; i++) { byte[] charByte = new byte[1] { (byte)(i + 33) }; char letter = Encoding.ASCII.GetString(charByte, 0, 1)[0]; IntPtr glyphSurface = TTF_RenderUTF8_Blended(ttf, letter.ToString(), color); if (glyphSurface != IntPtr.Zero) { SDL_Surface glyphSurfaceT = Marshal.PtrToStructure <SDL_Surface>(glyphSurface); // IntPtr glyphTexture = SDL_CreateTextureFromSurface(gRenderer, glyphSurface); // SDL_Rect destRect = new SDL_Rect() { x = (i % 12) * height, y = (i / 12) * height, h = height, w = height }; SDL_Rect srcRect = new SDL_Rect { x = 0, y = 0, w = glyphSurfaceT.w, h = glyphSurfaceT.h }; cursorRect.w = srcRect.w; cursorRect.h = srcRect.h; //drop to next line if (cursorRect.x + cursorRect.w > w) { cursorRect.x = 0; cursorRect.y += heightPadd; } // SDL_Rect destRect = new SDL_Rect() { x = (i % 12) * height, y = (i / 12) * height, h = height, w = height }; // SDL_SetTextureBlendMode(glyphTexture, SDL_BlendMode.SDL_BLENDMODE_BLEND); //Render Glyph SDL_SetSurfaceBlendMode(glyphSurface, SDL_BlendMode.SDL_BLENDMODE_NONE); // SDL_BlitSurface(glyphSurface, ref srcRect, fontSurface, ref destRect); SDL_BlitSurface(glyphSurface, ref srcRect, fontSurface, ref cursorRect); if (cursorRect.w % 2 != 0) { cursorRect.w++; } if (cursorRect.h % 2 != 0) { cursorRect.h++; } if (!Glyphs.ContainsKey(letter)) { Glyphs.Add(letter, new Glyph() { code = (ushort)(i + 33), width = (byte)cursorRect.w, height = (byte)cursorRect.h, textureX = cursorRect.x, textureY = cursorRect.y, }); } cursorRect.x += cursorRect.w; // SDL_SetTextureBlendMode(glyphTexture, SDL_BlendMode.SDL_BLENDMODE_BLEND); // SDL_RenderCopy(gRenderer, glyphTexture, IntPtr.Zero, ref destRect); // SDL_DestroyTexture(glyphTexture); SDL_FreeSurface(glyphSurface); } } FontTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, (int)SDL_TextureAccess.SDL_TEXTUREACCESS_TARGET, w, h); SDL_SetTextureBlendMode(FontTexture, SDL_BlendMode.SDL_BLENDMODE_BLEND); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); SDL_SetRenderTarget(gRenderer, FontTexture); SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0); SDL_RenderClear(gRenderer); IntPtr glyphTexture = SDL_CreateTextureFromSurface(gRenderer, fontSurface); SDL_RenderCopy(gRenderer, glyphTexture, IntPtr.Zero, IntPtr.Zero); SDL_DestroyTexture(glyphTexture); SDL_FreeSurface(fontSurface); SDL_SetRenderTarget(gRenderer, IntPtr.Zero); }