/// <summary> /// Create a new SDL font. /// </summary> /// <remarks> /// I'm normally not a huge fan of classes that load themselves, much less a constructor /// that loads itself from a file. I have to make an exception for the moment because /// SDL_image's API only supports loading from a file... once someone (maybe me) adds the /// load from memory calls I can remove this and create a proper byte[] constructor /// instead. (And leave the file loading up to the caller). /// </remarks> public SDLFont(SDLRenderer renderer, string fontName, int fontSize) { // Make sure our inputs are valid! if (renderer == null) { throw new ArgumentNullException("renderer"); } if (String.IsNullOrEmpty(fontName)) { throw new ArgumentNullException("fontName"); } if (fontSize <= 0) { throw new ArgumentOutOfRangeException("fontSize"); } // Make sure the image actually exists on disk before attempting to ask SDL to load it. // (Bonus: SDL doesn't return a null when the image does not exist on disk!) if (!File.Exists(fontName)) { throw new FileNotFoundException(fontName); } // Ask SDL to load our font for us and check if it worked. mFont = SDL_ttf.TTF_OpenFont(fontName, fontSize); if (mFont == null) { throw new SDLException("Failed to load font"); } }
public SDLTexture RenderTextBlended(SDLRenderer renderer, string text, Color color) { // Check inputs. if (renderer == null) { throw new ArgumentNullException("renderer"); } // Convert color to SDL format. SDL.SDL_Color sdlColor = new SDL.SDL_Color(); sdlColor.r = color.RedByte; sdlColor.g = color.GreenByte; sdlColor.b = color.BlueByte; // Render the text message to an SDL_Surface and then convert the surface into an // SDL texture. SDLTexture texture; using (SDLSurface surface = new SDLSurface(SDL_ttf.TTF_RenderText_Blended( mFont, text, sdlColor))) { texture = new SDLTexture(renderer, surface); } return(texture); }
/// <summary> /// Create a new SDL font. /// </summary> /// <remarks> /// I'm normally not a huge fan of classes that load themselves, much less a constructor /// that loads itself from a file. I have to make an exception for the moment because /// SDL_image's API only supports loading from a file... once someone (maybe me) adds the /// load from memory calls I can remove this and create a proper byte[] constructor /// instead. (And leave the file loading up to the caller). /// </remarks> public SDLFont( SDLRenderer renderer, string fontName, int fontSize ) { // Make sure our inputs are valid! if ( renderer == null ) { throw new ArgumentNullException( "renderer" ); } if ( String.IsNullOrEmpty( fontName ) ) { throw new ArgumentNullException( "fontName" ); } if ( fontSize <= 0 ) { throw new ArgumentOutOfRangeException( "fontSize" ); } // Make sure the image actually exists on disk before attempting to ask SDL to load it. // (Bonus: SDL doesn't return a null when the image does not exist on disk!) if ( !File.Exists( fontName ) ) { throw new FileNotFoundException( fontName ); } // Ask SDL to load our font for us and check if it worked. mFont = SDL_ttf.TTF_OpenFont( fontName, fontSize ); if ( mFont == null ) { throw new SDLException( "Failed to load font" ); } }
/// <summary> /// Create a new SDL texture from a file on disk. /// </summary> /// <remarks> /// I'm normally not a huge fan of classes that load themselves, much less a constructor /// that loads itself from a file. I have to make an exception for the moment because /// SDL_image's API only supports loading from a file... once someone (maybe me) adds the /// load from memory calls I can remove this and create a proper byte[] constructor /// instead. (And leave the file loading up to the caller). /// </remarks> /// <param name="surface"></param> public SDLTexture( SDLRenderer renderer, string imagePath ) { // Verify that our references are valid before proceeding. if ( renderer == null ) { throw new ArgumentNullException( "renderer" ); } if ( imagePath == null ) { throw new ArgumentNullException( "imagePath" ); } // Make sure the image actually exists on disk before attempting to ask SDL to load it. // (Bonus: SDL doesn't return a null when the image does not exist on disk!) if ( !File.Exists( imagePath ) ) { throw new FileNotFoundException( imagePath ); } // Attempt to load the bitmap from disk. mTexture = SDL_image.IMG_LoadTexture( renderer.RendererPtr, imagePath ); // Test to make sure the texture was successfully created. if ( mTexture == null ) { throw new SDLException( "Failed to load image from disk into SDL texture" ); } ReadAndStoreTextureProperties(); }
/// <summary> /// Create a new SDL texture from a file on disk. /// </summary> /// <remarks> /// I'm normally not a huge fan of classes that load themselves, much less a constructor /// that loads itself from a file. I have to make an exception for the moment because /// SDL_image's API only supports loading from a file... once someone (maybe me) adds the /// load from memory calls I can remove this and create a proper byte[] constructor /// instead. (And leave the file loading up to the caller). /// </remarks> /// <param name="surface"></param> public SDLTexture(SDLRenderer renderer, string imagePath) { // Verify that our references are valid before proceeding. if (renderer == null) { throw new ArgumentNullException("renderer"); } if (imagePath == null) { throw new ArgumentNullException("imagePath"); } // Make sure the image actually exists on disk before attempting to ask SDL to load it. // (Bonus: SDL doesn't return a null when the image does not exist on disk!) if (!File.Exists(imagePath)) { throw new FileNotFoundException(imagePath); } // Attempt to load the bitmap from disk. mTexture = SDL_image.IMG_LoadTexture(renderer.RendererPtr, imagePath); // Test to make sure the texture was successfully created. if (mTexture == null) { throw new SDLException("Failed to load image from disk into SDL texture"); } ReadAndStoreTextureProperties(); }
/// <summary> /// Create a new SDL texture. /// </summary> /// <param name="surface"></param> public SDLTexture( SDLRenderer renderer, SDLSurface surface ) { if ( renderer == null ) { throw new ArgumentNullException( "renderer" ); } if ( surface == null ) { throw new ArgumentNullException( "surface" ); } // Convert the SDL surface into an SDL texture. mTexture = SDL.SDL_CreateTextureFromSurface( renderer.RendererPtr, surface.SurfacePtr ); if ( mTexture == null ) { throw new SDLException( "Failed to convert SDL surface to texture" ); } ReadAndStoreTextureProperties(); }
/// <summary> /// Create a new SDL texture. /// </summary> /// <param name="surface"></param> public SDLTexture(SDLRenderer renderer, SDLSurface surface) { if (renderer == null) { throw new ArgumentNullException("renderer"); } if (surface == null) { throw new ArgumentNullException("surface"); } // Convert the SDL surface into an SDL texture. mTexture = SDL.SDL_CreateTextureFromSurface(renderer.RendererPtr, surface.SurfacePtr); if (mTexture == null) { throw new SDLException("Failed to convert SDL surface to texture"); } ReadAndStoreTextureProperties(); }
private static void RunGame() { // Create the main render window. SDLWindow window = new SDLWindow( "An SDL2 window", 100, 100, 800, 600 ); SDLRenderer renderer = new SDLRenderer( window ); // Load a test image. SDLTexture texture = new SDLTexture( renderer, "hello.bmp" ); SDLFont font = new SDLFont( renderer, "hello.ttf", 24 ); SDLTexture textTexture = font.RenderTextBlended( renderer, "Hello World!!", Color.BlueColor ); // Event loop. SDL.SDL_Event eventInfo = new SDL.SDL_Event(); bool shouldQuit = false; while ( !shouldQuit ) { CheckForSDLErrors(); // Pump and process pending OS events. while ( SDL.SDL_PollEvent( out eventInfo ) != 0 ) { if ( eventInfo.type == SDL.SDL_EventType.SDL_QUIT ) { shouldQuit = true; } } // Render all of our stuffs. renderer.Clear(); renderer.Draw( texture, 32, 32 ); renderer.Draw( textTexture, 0, 400 ); renderer.Present(); } }
public SDLTexture RenderTextBlended( SDLRenderer renderer, string text, Color color ) { // Check inputs. if ( renderer == null ) { throw new ArgumentNullException( "renderer" ); } // Convert color to SDL format. SDL.SDL_Color sdlColor = new SDL.SDL_Color(); sdlColor.r = color.RedByte; sdlColor.g = color.GreenByte; sdlColor.b = color.BlueByte; // Render the text message to an SDL_Surface and then convert the surface into an // SDL texture. SDLTexture texture; using ( SDLSurface surface = new SDLSurface( SDL_ttf.TTF_RenderText_Blended( mFont, text, sdlColor ) ) ) { texture = new SDLTexture( renderer, surface ); } return texture; }