Exemple #1
0
        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);
        }
Exemple #2
0
 public Sprite(string key, TextureStruct texture)
 {
     if (textureList.ContainsKey(key))
     {
         texture = textureList[key];
     }
     SetupSpriteEntity(key);
 }
Exemple #3
0
        private TextureStruct BMPToTexture(string imagePath, byte r, byte g, byte b)
        {
            TextureStruct finalTexture = new TextureStruct(IntPtr.Zero, 0, 0);
            IntPtr        sprite       = IntPtr.Zero;

            //loads all (supported) images, not only BMP
            sprite = SDL_image.IMG_Load(imagePath);
            //sprite = SDL_LoadBMP(imagePath);
            if (sprite == IntPtr.Zero)
            {
                Console.Write("Unable to load image: " + imagePath + " SDL Error:" + SDL_GetError() + " \n");
                SDL_FreeSurface(sprite);
                return(finalTexture);
            }
            //Set Colorkey
            var format = ((SDL_Surface)Marshal.PtrToStructure(sprite, typeof(SDL_Surface))).format;

            SDL_SetColorKey(sprite, 1, SDL_MapRGB(format, r, g, b));

            IntPtr myTexture = SDL_CreateTextureFromSurface(renderer, sprite);

            if (myTexture == IntPtr.Zero)
            {
                Console.Write("Unable to create texture! SDL Error:" + SDL_GetError() + " \n");
                Console.Write(imagePath + " \n");
                SDL_FreeSurface(sprite);
                return(finalTexture);
            }

            finalTexture.h       = ((SDL_Surface)Marshal.PtrToStructure(sprite, typeof(SDL_Surface))).h;
            finalTexture.w       = ((SDL_Surface)Marshal.PtrToStructure(sprite, typeof(SDL_Surface))).w;
            finalTexture.texture = myTexture;

            SDL_FreeSurface(sprite);
            return(finalTexture);
        }
Exemple #4
0
        public void ReplaceText(string newText, IntPtr font, SDL_Color color)
        {
            TextureStruct newTexture = TextToTexture(font, newText, color);

            this.ReplaceTexture(newTexture);
        }
Exemple #5
0
 public void ReplaceTexture(TextureStruct newtexture)
 {
     SDL_DestroyTexture(textureList[name].texture);
     textureList[name] = newtexture;
 }