static public void Main() { System.IntPtr window; System.Random rand = new System.Random(); // SDL.SDL_LogSetPriority(SDL.SDL_LOG_CATEGORY_APPLICATION, SDL.SDL_LOG_PRIORITY_INFO); // SDL.SDL_SetHint(SDL.SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, "1"); SDL.SDL_WindowFlags flags = 0; // flags |= SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN; if (SDL.SDL_CreateWindowAndRenderer(WindowWidth, WindowHeight, flags, out window, out Renderer) < 0) { Quit(2); } SDL.SDL_SetWindowTitle(window, "TestTtf"); SDL.SDL_ShowCursor(0); SDL_ttf.TTF_Init(); Font = SDL_ttf.TTF_OpenFont("misaki_gothic.ttf", FontSize); if (LoadSprite("icon.bmp", Renderer) < 0) { Quit(2); } for (int i = 0; i < NumSprites; ++i) { Positions[i].x = rand.Next(WindowWidth - Sprite_w); Positions[i].y = rand.Next(WindowHeight - Sprite_h); Positions[i].w = Sprite_w; Positions[i].h = Sprite_h; Velocities[i].x = 0; Velocities[i].y = 0; while (Velocities[i].x == 0 && Velocities[i].y == 0) { Velocities[i].x = rand.Next(MaxSpeed * 2 + 1) - MaxSpeed; Velocities[i].y = rand.Next(MaxSpeed * 2 + 1) - MaxSpeed; } } Done = 0; sw.Start(); while (Done == 0) { Loop(); } SDL.SDL_DestroyRenderer(Renderer); SDL.SDL_DestroyWindow(window); SDL.SDL_Quit(); SDL_ttf.TTF_CloseFont(Font); SDL_ttf.TTF_Quit(); Quit(0); }
void ShowLoadingScreen(int?duration = 1000) { if (!EnableLoadingScreen || (SDL_ttf.TTF_WasInit() == 0 && SDL_ttf.TTF_Init() < 0)) { Debug.WriteLine($"Could not create LoadingScreen: {((EnableLoadingScreen) ? SDL.SDL_GetError() : "disabled")}"); return; } SDL.SDL_RenderClear(_renderer); // updating image UpdateImage( $"{AppDomain.CurrentDomain.BaseDirectory}/Images/OpenXboxLogo.png", new SDL.SDL_Rect { x = _rectOrigin.w / 2 - 200 / 2, y = 200, w = 200, h = 200 } ); // updating string(font) UpdateFontString( "Initializing...", new SDL.SDL_Rect { y = 420 }, new SdlTextureOrientation { Horizontal = TextureOrientation.Center } ); SDL.SDL_RenderPresent(_renderer); Thread.Sleep(duration ?? 0); }
public void Init(string title, int xPos, int yPos, int width, int height) { SDL_WindowFlags flags = 0; Width = width; Height = height; endScene = false; if (SDL_Init(SDL_INIT_EVERYTHING) == 0) { SDL_ttf.TTF_Init(); Window = SDL_CreateWindow(title, xPos, yPos, width, height, flags); Renderer = SDL_CreateRenderer(Window, -1, 0); SDL_SetRenderDrawColor(Renderer, 200, 200, 50, 90); IsRunning = true; } x = 250; y = 500; w = 170; h = 100; mrect.x = x; mrect.y = y; mrect.w = w; mrect.h = h; endScreen = Texture.LoadTexture("Textures/EndScreen.png"); scrRect.x = 0; scrRect.y = 0; scrRect.w = Width; scrRect.h = Height; Scene.SetUpSceneScreen("Scenes/main menu.txt", this); }
public Font(string path, int pointSize, FontStyle style = FontStyle.Normal, bool unicode = true) { cacheFonts = SettingsManager.instance.engine.cacheFonts; this.pointSize = pointSize; this.style = style; this.unicode = unicode; if (SDL_ttf.TTF_WasInit() == 0) { if (SDL_ttf.TTF_Init() != 0) { throw new ApplicationException("Failed to initialize SDL_ttf: " + SDL.SDL_GetError()); } } handle = SDL_ttf.TTF_OpenFont(path, pointSize); if (handle == IntPtr.Zero) { throw new ApplicationException("Failed to load font: " + SDL.SDL_GetError()); } SDL_ttf.TTF_SetFontStyle(handle, (int)style); familyName = SDL_ttf.TTF_FontFaceFamilyName(handle); ushort characterCount = unicode ? ushort.MaxValue : (ushort)256; bool generateTextures = true; if (cacheFonts) { if (LoadGlyphDataCached() && LoadTextureDataCached()) { Log.Info("Font: Using cached font data"); generateTextures = false; } else { glyphs = null; foreach (Texture2D texture in textures) { texture.Dispose(); } textures.Clear(); } } if (generateTextures) { GenerateGlyphData(characterCount); GenerateTextures(characterCount); if (cacheFonts) { CacheGlyphData(); } } loadedFonts++; }
public void UpdateFontString(string strOutput, SDL.SDL_Rect dstRect, SdlTextureOrientation orientation = null, int fontSize = 28, SDL.SDL_Color?color = null) { // checking initialization if (SDL_ttf.TTF_WasInit() == 0 && SDL_ttf.TTF_Init() < 0) { Debug.WriteLine($"Could not create LoadingScreen: {SDL.SDL_GetError() }"); return; } var textTexture = SDL.SDL_CreateTextureFromSurface(_renderer, SDL_ttf.TTF_RenderText_Blended( SDL_ttf.TTF_OpenFont(_fontSourceRegular, fontSize), "Initializing...", color ?? new SDL.SDL_Color { a = 0xff, r = 0xff, g = 0xff, b = 0xff } )); orientation = orientation ?? new SdlTextureOrientation(); SDL.SDL_QueryTexture(textTexture, out uint fmt, out int acs, out dstRect.w, out dstRect.h); // TODO: testwise implement horizontal centering (ignoring all other cases for now) switch (orientation.Horizontal) { case TextureOrientation.Center: dstRect.x = _rectOrigin.w / 2 - dstRect.w / 2; break; } if (textTexture == IntPtr.Zero) { Debug.WriteLine($"Could not create texture for ttf: {SDL.SDL_GetError()}"); return; } SDL.SDL_RenderCopy(_renderer, textTexture, ref _rectOrigin, ref dstRect); }
public void GEALOSinit() { if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) < 0) { Console.WriteLine($"There was an issue initilizing SDL. {SDL.SDL_GetError()}"); Environment.Exit(-1); } if (SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_TIF | SDL_image.IMG_InitFlags.IMG_INIT_WEBP) < 0) { Console.WriteLine($"There was an issue initilizing SDL_image. {SDL.SDL_GetError()}"); Environment.Exit(-1); } if (SDL_ttf.TTF_Init() < 0) { Console.WriteLine($"There was an issue initilizing SDL_ttf. {SDL.SDL_GetError()}"); Environment.Exit(-1); } if (SDL_mixer.Mix_OpenAudio(22050, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 4096) < 0 || SDL_mixer.Mix_Init(SDL_mixer.MIX_InitFlags.MIX_INIT_FLAC | SDL_mixer.MIX_InitFlags.MIX_INIT_MID | SDL_mixer.MIX_InitFlags.MIX_INIT_MOD | SDL_mixer.MIX_InitFlags.MIX_INIT_MP3 | SDL_mixer.MIX_InitFlags.MIX_INIT_OGG | SDL_mixer.MIX_InitFlags.MIX_INIT_OPUS) < 0) { Console.WriteLine($"There was an issue initilizing SDL_mixer. {SDL.SDL_GetError()}"); Environment.Exit(-1); } running = true; GEALOSprog(); }
private static void Start() { // ====================================================================================== // Initialize SDL // ====================================================================================== if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) != 0) { throw new Exception("Failed to initialize SDL."); } if (SDL_ttf.TTF_Init() != 0) { throw new Exception("Failed to initialize SDL_ttf."); } SDL_mixer.MIX_InitFlags mixInitFlags = SDL_mixer.MIX_InitFlags.MIX_INIT_MP3 | SDL_mixer.MIX_InitFlags.MIX_INIT_OGG | SDL_mixer.MIX_InitFlags.MIX_INIT_FLAC; if (((SDL_mixer.MIX_InitFlags)SDL_mixer.Mix_Init(mixInitFlags) & mixInitFlags) != mixInitFlags) { throw new Exception("Failed to initialize SDL_mixer."); } if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 1024) != 0) { throw new Exception("Failed to initialize SDL_mixer."); } SDL_mixer.Mix_AllocateChannels(AudioChannelCount); Window = SDL.SDL_CreateWindow( Game.Title, SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(0), SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(0), (int)Game.Resolution.X, (int)Game.Resolution.Y, 0); if (Window == IntPtr.Zero) { throw new Exception("Failed to create window."); } Renderer = SDL.SDL_CreateRenderer(Window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC | SDL.SDL_RendererFlags.SDL_RENDERER_TARGETTEXTURE); if (Renderer == IntPtr.Zero) { throw new Exception("Failed to create renderer."); } IntPtr renderTargetHandle = SDL.SDL_CreateTexture(Renderer, SDL.SDL_PIXELFORMAT_RGBA8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_TARGET, (int)Game.Resolution.X, (int)Game.Resolution.Y); RenderTarget = new Texture(renderTargetHandle, (int)Game.Resolution.X, (int)Game.Resolution.Y); // ====================================================================================== // Instantiate the game object // ====================================================================================== Game = new Game(); }
public FontManager() { //Initialize SDL_ttf if (SDL_ttf.TTF_Init() == -1) { Console.WriteLine("SDL_ttf could not initialize! SDL_ttf Error: {0}", SDL.SDL_GetError()); } }
public SDL2GeneralDisplay(string title, int width, int height, int internalWidth, int internalHeight, int x = 0, int y = 0, string fontFile = null, int?fontSize = null) { SDL_ttf.TTF_Init(); _font = SDL_ttf.TTF_OpenFont(fontFile ?? "./default.ttf", fontSize ?? 20); InternalWidth = internalWidth; InternalHeight = internalHeight; ActualWidth = width; ActualHeight = height; SDL.SDL_Init(SDL.SDL_INIT_VIDEO); try { _window = SDL.SDL_CreateWindow(title, SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, width, height, SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE | SDL.SDL_WindowFlags.SDL_WINDOW_VULKAN ); } catch { } finally { if (_window == IntPtr.Zero) { _window = SDL.SDL_CreateWindow(title, SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, width, height, SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE); } } if (x > 0 && y > 0) { SDL.SDL_SetWindowPosition(_window, x, y); } Pixels = new uint[internalWidth * internalHeight * 4]; PixelsText = new uint[internalWidth * internalHeight * 4]; _renderer = SDL.SDL_CreateRenderer(_window, -1, 0); _texture = SDL.SDL_CreateTexture(_renderer, SDL.SDL_PIXELFORMAT_ARGB8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_TARGET, internalWidth, internalHeight); SDL.SDL_GL_SetSwapInterval(0); unsafe { fixed(uint *t = &Pixels[0]) _bufferPtr = new IntPtr(t); } IsOpen = true; }
public static void Start() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { SetProcessDpiAwareness(2); } SDL.SDL_Init(SDL.SDL_INIT_VIDEO); SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "linear"); SDL_ttf.TTF_Init(); SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_JPG); asyncCallbacksAdded = SDL.SDL_RegisterEvents(1); SynchronizationContext.SetSynchronizationContext(new UiSyncronizationContext()); mainThreadId = Thread.CurrentThread.ManagedThreadId; }
private static void InitSDL2() { if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0) { Console.WriteLine("SDL could not initialize! SDL_Error: {0}", SDL.SDL_GetError()); } else { Console.WriteLine("SDL initialized!"); } // OPTIONAL: init SDL_image var imgFlags = SDL_image.IMG_InitFlags.IMG_INIT_JPG | SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_WEBP; if ((SDL_image.IMG_Init(imgFlags) > 0 & imgFlags > 0) == false) { Console.WriteLine("SDL_image could not initialize! SDL_image Error: {0}", SDL.SDL_GetError()); } else { Console.WriteLine("SDL_image initialized!"); } // OPTIONAL: init SDL_ttf if (SDL_ttf.TTF_Init() == -1) { Console.WriteLine("SDL_ttf could not initialize! SDL_image Error: {0}", SDL.SDL_GetError()); } else { Console.WriteLine("SDL_ttf initialized!"); } // OPTIONAL: init SDL_mixer if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048) < 0) { Console.WriteLine("SDL_mixer could not initialize! SDL_image Error: {0}", SDL.SDL_GetError()); } else { Console.WriteLine("SDL_mixer initialized!"); } if (SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "1") == SDL.SDL_bool.SDL_FALSE) { Console.WriteLine("Warning: Linear texture filtering not enabled!"); } }
private static void Init() { if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) != 0) { throw new SdlException(nameof(SDL.SDL_Init)); } if (SDL_ttf.TTF_Init() != 0) { throw new SdlException(nameof(SDL_ttf.TTF_Init)); } _window = SDL.SDL_CreateWindow("Langton`s Ant", 0, 0, 640, 480, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN); if (_window == IntPtr.Zero) { throw new SdlException(nameof(SDL.SDL_CreateWindow)); } _renderer = SDL.SDL_CreateRenderer( _window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC); if (_renderer == IntPtr.Zero) { throw new SdlException(nameof(SDL.SDL_CreateRenderer)); } _font = SDL_ttf.TTF_OpenFont(_fontPath, 14); KeydownHandler = new KeyHandler(); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_ESCAPE, () => _quit = true); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_F5, LoadWorld); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_F2, () => _speed -= 10); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_F3, () => _speed += 10); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_F4, () => _speed = 0); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_z, () => GetWorldRenderer().AddZoom(-10)); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_x, () => GetWorldRenderer().AddZoom(10)); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_c, () => GetWorldRenderer().ResetZoom()); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_RIGHT, () => GetWorldRenderer().MoveZoom(10, 0)); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_UP, () => GetWorldRenderer().MoveZoom(0, -10)); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_LEFT, () => GetWorldRenderer().MoveZoom(-10, 0)); KeydownHandler.Add(SDL.SDL_Keycode.SDLK_DOWN, () => GetWorldRenderer().MoveZoom(0, 10)); }
public override void Initialize() { TypeO.RequireTypeO(new Core.Engine.Version(0, 1, 1)); TypeO.RequireModule <DesktopModule>(new Core.Engine.Version(0, 1, 1)); TypeO.AddService <ISDLService, SDLService>(); ((ISDLService)TypeO.Context.Services[typeof(ISDLService)]).Option = Option; //Initial SDL foreach (var hint in Option.Hints) { SDL2.SDL.SDL_SetHint(hint.Key, hint.Value); } foreach (var eventState in Option.EventStates) { SDL2.SDL.SDL_EventState(eventState.Key, eventState.Value); } if (SDL2.SDL.SDL_Init(Option.SDLInitFlags) != 0) { var message = $"SDL_Init Error: {SDL2.SDL.SDL_GetError()}"; Logger.Log(LogLevel.Fatal, message); TypeO.Context.Exit(); throw new ApplicationException(message); } if (SDL_image.IMG_Init(Option.IMGInitFlags) == 0) { var message = $"IMG_Init Error: {SDL2.SDL.SDL_GetError()}"; Logger.Log(LogLevel.Fatal, message); TypeO.Context.Exit(); throw new ApplicationException(message); } if (SDL_ttf.TTF_Init() != 0) { var message = $"TTF_Init Error: {SDL2.SDL.SDL_GetError()}"; Logger.Log(LogLevel.Fatal, message); TypeO.Context.Exit(); throw new ApplicationException(message); } }
/// <summary> /// Initializes the specified game. /// </summary> /// <param name="game">The game.</param> /// <param name="title">The title.</param> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="flags">The flags.</param> public static void Initialize(SchwiftyGame game, string title = "", int x = 0, int y = 0, int width = 1280, int height = 720, SDL.SDL_WindowFlags flags = SDL_WindowFlags.SDL_WINDOW_SHOWN) { Resource.StartupCheck(); SDL_Init(SDL_INIT_VIDEO); if (title == "") { title = Assembly.GetExecutingAssembly().GetName().FullName; } window = new Window(title, x, y, width, height, flags); window.Create(); IsFullScreen = false; //initialize audio engine. AudioEngine.Init(); SDL_ttf.TTF_Init(); _game = game; }
public bool Init() { if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) < 0) { return(false); } if (SDL_ttf.TTF_Init() < 0) { return(false); } if (SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "1") == SDL.SDL_bool.SDL_FALSE) { Console.WriteLine("SDL_HINT_RENDER_SCALE_QUALITY failed"); } if ((_window = SDL.SDL_CreateWindow("SDL2", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED , WindowWidth, WindowHeight, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN)) == IntPtr.Zero) { Console.WriteLine("unable to create window"); Cleanup(); return(false); } _primarySurface = SDL.SDL_GetWindowSurface(_window); if ((_renderer = SDL.SDL_CreateRenderer(_window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED)) == IntPtr.Zero) { Console.WriteLine("unable to create renderer"); Cleanup(); return(false); } GetFont(12); SDL.SDL_SetRenderDrawColor(_renderer, 0xae, 0xFF, 0x00, 0xFF); return(true); }
private static IntPtr MakeTextSurface(string message) { SDL_ttf.TTF_Init(); var font = SDL_ttf.TTF_OpenFont(ArialFontFilename(), 128); if (font == IntPtr.Zero) { throw new Exception($"Could not initialize font: {SDL_GetError()}"); } var color = new SDL_pixels.SDL_Color { r = 255, g = 255, b = 255, a = 255 }; var surface = SDL_ttf.TTF_RenderText_Solid(font, message, color); SDL_ttf.TTF_CloseFont(font); SDL_ttf.TTF_Quit(); return(surface); }
public static bool InitEngine() { // Initialise the log log = new Log("harambae_runtime.log"); log.Info("Engine starting"); // Initialise required SDL2 components int result; result = SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING); if (result < 0) { log.Error("Could not init SDL2"); return(false); } if (CreateContext(800, 600, false) == false) { log.Error("Could not create a context"); return(false); } // SDL2_TTF result = SDL_ttf.TTF_Init(); if (result < 0) { log.Error("Could not init SDL2_TTF"); DestroyContext(); SDL.SDL_Quit(); return(false); } largefont = SDL_ttf.TTF_OpenFont("assets/papyrus.ttf", 32); smallfont = SDL_ttf.TTF_OpenFont("assets/papyrus.ttf", 16); // SDL2_image result = SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG); return(true); }
public UxContext( string windowTitle, IUxConfig uxConfig, string imagesPath, string audiosPath, string fontsPath ) { Title = windowTitle; UxConfig = uxConfig; OnWindowResize(uxConfig.ScreenSize); var sdlWindowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE; if (uxConfig.Maximized) { sdlWindowFlags |= SDL.SDL_WindowFlags.SDL_WINDOW_MAXIMIZED; } SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO); SDL_ttf.TTF_Init(); Window = SDL.SDL_CreateWindow( windowTitle, SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, ScreenSize.Width, ScreenSize.Height, sdlWindowFlags ); WRenderer = SDL.SDL_CreateRenderer(Window, -1, 0); SDL.SDL_SetRenderDrawColor(WRenderer, 0, 0, 0, 0); SDL.SDL_SetRenderDrawBlendMode(WRenderer, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND); SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048); TextureProvider = new TextureProvider(WRenderer, imagesPath); AudioProvider = new AudioProvider(audiosPath); FontProvider = new FontProvider(fontsPath); }
bool INTERNAL_Init_SDLSystems(uint subsysFlags) { #if DEBUG // Need to add the no parachute flag for debuggers so SDL will interact with them nicely // According to the wiki (http://wiki.libsdl.org/), this does nothing in SDL2 however, // we'll do it anyway "just in case". subsysFlags |= SDL.SDL_INIT_NOPARACHUTE; if (System.Diagnostics.Debugger.IsAttached) { // ¡Windows es muy estúpido! SDL.SDL_SetHint( SDL.SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, "1" ); } #endif // SDL_Init returns 0 on success if (SDL.SDL_Init(subsysFlags) != 0) { return(false); } // IMG_Init returns same init flags on success var imgInit = SDL_image.IMG_InitFlags.IMG_INIT_PNG; if (imgInit != (SDL_image.IMG_InitFlags)SDL_image.IMG_Init(imgInit)) { return(false); } // TTF_Init returns 0 on success if (SDL_ttf.TTF_Init() != 0) { return(false); } // We made it through the gambit! return(true); }
/// <summary> /// Initialize SDL, SDL_image and SDL_ttf /// </summary> public static void InitGraphics() { // Initialize SDL if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) != 0) { throw new SDLException("SDL_Init"); } // Initialize SDL PNG image loading if ((SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG) & (int)SDL_image.IMG_InitFlags.IMG_INIT_PNG) != (int)SDL_image.IMG_InitFlags.IMG_INIT_PNG) { throw new SDLException("IMG_Init"); } // Initialize SDL TTF rendering if (SDL_ttf.TTF_Init() != 0) { throw new SDLException("TTF_Init"); } }
public static void Start() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { try { SetProcessDpiAwareness(2); } catch (Exception) { Console.WriteLine("DPI awareness setup failed"); // On older versions on Windows } } SDL.SDL_Init(SDL.SDL_INIT_VIDEO); SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "linear"); SDL.SDL_EnableScreenSaver(); SDL_ttf.TTF_Init(); SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_JPG); asyncCallbacksAdded = SDL.SDL_RegisterEvents(1); SynchronizationContext.SetSynchronizationContext(new UiSyncronizationContext()); mainThreadId = Thread.CurrentThread.ManagedThreadId; }
public unsafe void init() { SDL.SDL_Init(SDL.SDL_INIT_VIDEO); SDL_ttf.TTF_Init(); window = SDL.SDL_CreateWindow("Card Games", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED, 600, 480, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN); windowSurface = SDL.SDL_GetWindowSurface(window); arrowSprite = SDL.SDL_LoadBMP("arrow.bmp"); titleSprite = SDL.SDL_LoadBMP("title.bmp"); SDL.SDL_Surface *windowSDLSurface = (SDL.SDL_Surface *)windowSurface; clearColor = SDL.SDL_MapRGB(windowSDLSurface->format, 0, 100, 0); textColor.r = 255; textColor.b = 255; textColor.g = 255; textColor.a = 255; favoritesColor.r = 255; favoritesColor.b = 0; favoritesColor.g = 255; favoritesColor.a = 255; font = SDL_ttf.TTF_OpenFont("times.ttf", 20); string instructionText; instructionText = "ENTER to select game, F to favorite, SPACE to toggle showing favorites"; instructionTextSurface = SDL_ttf.TTF_RenderText_Solid(font, instructionText, textColor); screenRect.x = 0; screenRect.y = 0; screenRect.w = 600; screenRect.h = 480; //initialize the card games gameList.Add(new CardGame("Three Card Poker", 1)); gameList[0].generateNameSurface(font, textColor); gameList.Add(new CardGame("Omaha High Poker", 2)); gameList[1].generateNameSurface(font, textColor); gameList.Add(new CardGame("Three Card Draw", 3)); gameList[2].generateNameSurface(font, textColor); gameList.Add(new CardGame("Pai Gow Poker", 4)); gameList[3].generateNameSurface(font, textColor); }
/// <summary>Initializes the game by calling initialize on the SDL2 instance with the passed flags /// or "EVERYTHING" if 0. Additionally, this method will initialize SDL_ttf and SDL_image to load fonts and images. /// </summary> /// <param name="types">Bit flags indicating the way in which SDL should be initialized</param> private void InitializeBase(GameEngineInitializeType types) { if (SDL.SDL_Init((uint)types) != 0) { throw new InvalidOperationException($"SDL_Init: {SDL.SDL_GetError()}"); } if (SDL_ttf.TTF_Init() != 0) { throw new InvalidOperationException($"TTF_Init: {SDL.SDL_GetError()}"); } SDL_image.IMG_InitFlags initImageFlags = SDL_image.IMG_InitFlags.IMG_INIT_JPG | SDL_image.IMG_InitFlags.IMG_INIT_PNG | SDL_image.IMG_InitFlags.IMG_INIT_TIF | SDL_image.IMG_InitFlags.IMG_INIT_WEBP; int initImageResult = SDL_image.IMG_Init(initImageFlags); if ((initImageResult & (int)initImageFlags) != (int)initImageFlags) { throw new InvalidOperationException($"IMG_Init: {SDL.SDL_GetError()}"); } }
/// <summary>Initializes the game by calling initialize on the SDL2 instance with the passed flags /// or "EVERYTHING" if 0. Additionally, this method will initialize SDL_ttf and SDL_image to load fonts and images. /// </summary> /// <param name="flags">Bit flags indicating the way in which SDL should be initialized</param> protected virtual void Initialize(uint flags) { if (flags == EMPTY_UINT) { flags = SDL.SDL_INIT_EVERYTHING; } if (SDL.SDL_Init(flags) != 0) { throw new InvalidOperationException(String.Format("SDL_Init: {0}", SDL.SDL_GetError())); } if (SDL_ttf.TTF_Init() != 0) { throw new InvalidOperationException(String.Format("TTF_Init: {0}", SDL.SDL_GetError())); } int initImageResult = SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG); if ((initImageResult & (int)SDL_image.IMG_InitFlags.IMG_INIT_PNG) != (int)SDL_image.IMG_InitFlags.IMG_INIT_PNG) { throw new InvalidOperationException(String.Format("IMG_Init: {0}", SDL.SDL_GetError())); } }
//Inits SDL,SDL_image and SDL_ttf or exits the aplication if it fails static Hardware() { if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0) { SDL.SDL_ShowSimpleMessageBox (SDL.SDL_MessageBoxFlags.SDL_MESSAGEBOX_ERROR, "Error", "Could not init SDL", screen); Environment.Exit(1); } if (SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG) < 0) { SDL.SDL_ShowSimpleMessageBox (SDL.SDL_MessageBoxFlags.SDL_MESSAGEBOX_ERROR, "Error", "Could not init SDL_Image", screen); Environment.Exit(1); } if (SDL_ttf.TTF_Init() < 0) { SDL.SDL_ShowSimpleMessageBox (SDL.SDL_MessageBoxFlags.SDL_MESSAGEBOX_ERROR, "Error", "Could not init SDL_ttf", screen); Environment.Exit(1); } }
public void Execute() { if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) != 0) { SdlLogger.Fatal(nameof(SDL.SDL_Init)); } SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_PNG); if (SDL_ttf.TTF_Init() != 0) { SdlLogger.Fatal(nameof(SDL_ttf.TTF_Init)); } var window = SDL.SDL_CreateWindow(TITLE, 0, 0, WIDTH, HEIGHT, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN); if (window == IntPtr.Zero) { SdlLogger.Fatal(nameof(SDL.SDL_CreateWindow), () => ReleaseAndQuit(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)); } var renderer = SDL.SDL_CreateRenderer( window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC); if (renderer == IntPtr.Zero) { SdlLogger.Fatal( nameof(SDL.SDL_CreateRenderer), () => ReleaseAndQuit(window, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)); } var font = SDL_ttf.TTF_OpenFont(Resources.GetFilePath("lesson6/long_pixel-7.ttf"), 16); if (font == IntPtr.Zero) { SdlLogger.Fatal( nameof(Resources.LoadTextureFromImage), () => ReleaseAndQuit(window, renderer, IntPtr.Zero, IntPtr.Zero)); } var colorWhite = new SDL.SDL_Color(); colorWhite.r = 255; colorWhite.g = 255; colorWhite.b = 255; var image = SdlDrawing.CreateTextureFromText(@"SDL2_TTF - крутота!", font, colorWhite, renderer); var quit = false; int x, y, iW = 100, iH = 100; x = WIDTH / 2 - iW / 2; y = HEIGHT / 2 - iH / 2; var e = new SDL.SDL_Event(); while (!quit) { // event handling while (SDL.SDL_PollEvent(out e) > 0) { if (e.type == SDL.SDL_EventType.SDL_QUIT) { quit = true; } if (e.type == SDL.SDL_EventType.SDL_KEYDOWN) { switch (e.key.keysym.sym) { case SDL.SDL_Keycode.SDLK_ESCAPE: quit = true; break; } } } // rendering SDL.SDL_RenderClear(renderer); SdlDrawing.RenderTexture(image, renderer, x, y); SDL.SDL_RenderPresent(renderer); } ReleaseAndQuit(window, renderer, font, image); }
private static bool Init() { //Initialization flag bool success = true; //Initialize SDL if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) < 0) { Console.WriteLine("SDL could not initialize! SDL_Error: {0}", SDL.SDL_GetError()); success = false; } else { //Set texture filtering to linear if (SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "1") == SDL.SDL_bool.SDL_FALSE) { Console.WriteLine("Warning: Linear texture filtering not enabled!"); } //Create window _Window = SDL.SDL_CreateWindow("SDL Tutorial", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN); if (_Window == IntPtr.Zero) { Console.WriteLine("Window could not be created! SDL_Error: {0}", SDL.SDL_GetError()); success = false; } else { //Create vsynced renderer for window var renderFlags = SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC; Renderer = SDL.SDL_CreateRenderer(_Window, -1, renderFlags); if (Renderer == IntPtr.Zero) { Console.WriteLine("Renderer could not be created! SDL Error: {0}", SDL.SDL_GetError()); success = false; } else { //Initialize renderer color SDL.SDL_SetRenderDrawColor(Renderer, 0xFF, 0xFF, 0xFF, 0xFF); //Initialize PNG loading var imgFlags = SDL_image.IMG_InitFlags.IMG_INIT_PNG; if ((SDL_image.IMG_Init(imgFlags) > 0 & imgFlags > 0) == false) { Console.WriteLine("SDL_image could not initialize! SDL_image Error: {0}", SDL.SDL_GetError()); success = false; } //Initialize SDL_ttf if (SDL_ttf.TTF_Init() == -1) { Console.WriteLine("SDL_ttf could not initialize! SDL_ttf Error: {0}", SDL.SDL_GetError()); success = false; } } } } return(success); }
public SDLFontEngine() { SDL_ttf.TTF_Init(); }
public GameView() { MenuStrip menu = new MenuStrip(); menu.Text = "File Menu"; menu.Name = "MainMenu"; menu.Dock = DockStyle.Top; ToolStripMenuItem item = new ToolStripMenuItem("File"); item.DropDownItems.Add("Load", null, (s, e) => LoadROM()); menu.Items.Add(item); this.MainMenuStrip = menu; Controls.Add(menu); windowSize = new Size(screenWidth + textWidth, screenHeight + menu.Height); gamePanel = new Panel(); gamePanel.Size = windowSize; gamePanel.Location = new Point(0, menu.Height); ClientSize = windowSize; FormClosing += new FormClosingEventHandler(WindowClosing); Controls.Add(gamePanel); SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING); SDL_ttf.TTF_Init(); gameWindow = SDL.SDL_CreateWindow("GBEmu", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, windowSize.Width, windowSize.Height, SDL.SDL_WindowFlags.SDL_WINDOW_BORDERLESS | SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL); glRenderer = SDL.SDL_CreateRenderer(gameWindow, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED); glFont = SDL_ttf.TTF_OpenFont("assets/arial.ttf", 12); glTexture = SDL.SDL_CreateTexture(glRenderer, SDL.SDL_PIXELFORMAT_RGB24, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING, gbWidth, gbHeight); // Get the Win32 HWND from the SDL2 window SDL.SDL_SysWMinfo info = new SDL.SDL_SysWMinfo(); SDL.SDL_GetWindowWMInfo(gameWindow, ref info); IntPtr winHandle = info.info.win.window; SetWindowPos( winHandle, Handle, 0, 0, 0, 0, 0x0401 // NOSIZE | SHOWWINDOW ); // Attach the SDL2 window to the panel SetParent(winHandle, gamePanel.Handle); ShowWindow(winHandle, 1); // SHOWNORMAL renderCancellationToken = new CancellationTokenSource(); renderTask = Task.Factory.StartNew(async() => { DateTime now = DateTime.Now; DateTime lastExecution = now; TimeSpan elapsedTime = TimeSpan.FromSeconds(0); SDL.SDL_Event e; try { while (true) { now = DateTime.Now; elapsedTime = now - lastExecution; lastExecution = now; PrintCPU(); while (SDL.SDL_PollEvent(out e) != 0) { switch (e.type) { case SDL.SDL_EventType.SDL_KEYDOWN: switch (e.key.keysym.sym) { case SDL.SDL_Keycode.SDLK_SPACE: if (play) { break; } do { bus.GetCPU().Clock(); } while (bus.GetCPU().Complete); break; case SDL.SDL_Keycode.SDLK_p: play = !play; break; } break; } } if (play) { do { bus.GetCPU().Clock(); } while (bus.GetCPU().Complete); } Render(elapsedTime.TotalMilliseconds); await Task.Delay(30); } } catch (Exception ex) { Log.Error(ex, "Rendering error"); MethodInvoker methodInvokerDelegate = delegate() { this.Close(); }; this.Invoke(methodInvokerDelegate); } }, renderCancellationToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); }
public static Engine Create(EngineOptions options, IController controller, IEventsCycle eventsCycle, ISceneRenderer sceneRenderer) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { throw new InvalidOperationException($"Cant initialize SDL2: {SDL_GetError()}"); } if (SDL_image.IMG_Init(SDL_image.IMG_InitFlags.IMG_INIT_JPG | SDL_image.IMG_InitFlags.IMG_INIT_PNG) < 0) { throw new InvalidOperationException($"Cant initialize SDL_image: {SDL_GetError()}"); } if (SDL_ttf.TTF_Init() < 0) { throw new InvalidOperationException($"TTF_Init: {SDL_GetError()}"); } if (SDL_ShowCursor(0) < 0) { throw new InvalidOperationException($"Cant disable cursor: {SDL_GetError()}"); } if (SDL_mixer.Mix_Init( SDL_mixer.MIX_InitFlags.MIX_INIT_MP3 | SDL_mixer.MIX_InitFlags.MIX_INIT_MID | SDL_mixer.MIX_InitFlags.MIX_INIT_MOD | SDL_mixer.MIX_InitFlags.MIX_INIT_OGG | SDL_mixer.MIX_InitFlags.MIX_INIT_FLAC | SDL_mixer.MIX_InitFlags.MIX_INIT_OPUS) < 0) { throw new InvalidOperationException($"MixInit: {SDL_GetError()}"); } if (SDL_mixer.Mix_OpenAudio(22050, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 4096) < 0) { throw new InvalidOperationException($"OpenAudio: {SDL_GetError()}"); } if (SDL_mixer.Mix_AllocateChannels(16) < 0) { throw new InvalidOperationException($"Cant allocate channels: {SDL_GetError()}"); } if (SDL_mixer.Mix_Volume(-1, 64) < 0) { throw new InvalidOperationException($"Min_Volume: {SDL_GetError()}"); } var screen = Ui.Screen.Create(options.WindowTitle, options.ScreenHeight, options.ScreenWidth, options.FullScreen); var miniMapRenderer = new MiniMapRenderer(); var statusBarHeight = screen.Height / 8; var statusBarWidth = screen.Width; var statusBarSprite = Sprite.Load(UiResourcesHelper.StatusBarSpritePath); var bowSprite = Sprite.Load(UiResourcesHelper.BowMiniSpritePath); var frameSprite = Sprite.Load(UiResourcesHelper.FrameSpritePath); var crossSprite = options.CrossSpritePath == null ? null : Sprite.Load(options.CrossSpritePath); var arrowSprite = Sprite.Load(UiResourcesHelper.ArrowSpritePath); var swordSprite = Sprite.Load(UiResourcesHelper.SwordSpritePath); var fireBallSprite = Sprite.Load(UiResourcesHelper.FireBallSpritePath); var shockBallSprite = Sprite.Load(UiResourcesHelper.ShockBallSpritePath); var faceSprite = Sprite.Load(UiResourcesHelper.FaceSprintPath); var faceHurtedSprite = Sprite.Load(UiResourcesHelper.FaceHurtedSpritePath); var faceBadSprite = Sprite.Load(UiResourcesHelper.FaceBadSpritePath); var logTextRenderer = TextRenderer.Load(options.FontPath, screen.Height / 50); var notesTextRenderer = TextRenderer.Load(options.FontPath, screen.Height / 50); var statusTextRenderer = TextRenderer.Load(options.FontPath, screen.Height / 20); var notesRenderer = new NotesRenderer(Sprite.Load(options.NotesSpritePath), notesTextRenderer, statusBarHeight); var monologueTextRenderer = TextRenderer.Load(options.FontPath, screen.Height / 50); var monologueRenderer = new MonologueRenderer(monologueTextRenderer, statusBarHeight); var statusBarRenderer = new StatusRenderer( statusBarSprite, crossSprite, statusBarHeight, logTextRenderer, notesRenderer, monologueRenderer, bowSprite, swordSprite, frameSprite, statusTextRenderer, arrowSprite, fireBallSprite, shockBallSprite, faceSprite, faceHurtedSprite, faceBadSprite); var textRenderer = options.FontPath == null ? null : TextRenderer.Load(options.FontPath, 24); return(new Engine(screen, controller, eventsCycle, sceneRenderer, miniMapRenderer, statusBarRenderer, textRenderer)); }