Hello SDL: Your First Graphics Window C#

first we have to download SDL form GitHub and add dll in reference. add some required namespaces SDL as shown blow.

needed now IntPtr to store address of window and Surface.

public IntPtr _Window; // pointer for SDL window
public IntPtr _Surface; // ponter for surface

A SDL surface is just a 2D image. A 2D image can be loaded from a file or it can be the image inside of a window. In this case it will be the image we see inside of the window on the screen. After declaring our window and screen surface, we initialize SDL. You can’t call any SDL functions without initializing SDL first. Since all we care about is using SDL’s video subsystem, we will only be passing it the SDL_INIT_VIDEO flag.

SDL_Init(SDL_INIT_VIDEO) is use to init the sdl and it return -1 if it fail so use use like this.

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
throw (new InvalidOperationException(“Error occurred while initializing SDL”));

now it’s time to create a window.If SDL initialized successfully, we’ll want to create a window using SDL_CreateWindow. The first argument sets the window’s caption or this part of the window: The next two arguments define the x and y position the window is created in. Since we don’t care where it is created, we just put SDL_WINDOWPOS_UNDEFINED for the x and y position.

The next two arguments define the window’s width and height. The last argument are the creation flags. SDL_WINDOW_SHOWN makes sure the window is shown when it is created.

If there is an error, SDL_CreateWindow returns NULL. If there’s no window, we want to throw exception.

// if succeeded create our window
_Window = SDL_CreateWindow("title of window", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,600,800,SDL_WindowFlags.SDL_WINDOW_SHOWN);
// if the window creation succeeded create our renderer
if (_Window == null || _Window == IntPtr.Zero)
throw (new InvalidOperationException(“Error occurred while initializing Window”));

If the window was created successfully, we want to get the window’s surface so we can draw to it. SDL_GetWindowSurface does just this.To keep this tutorial simple, all we’re going to do here is fill the window’s surface white using SDL_FillRect. Don’t worry too much about this function here. This tutorial is only concerned about getting a window to pop up. An important thing to know about rendering is that just because you’ve drawn something to the screen surface doesn’t mean you’ll see it. After you’ve done all your drawing you need to update the window so it shows everything you drew. A call to SDL_UpdateWindowSurface will do this. If all we do is create the window, fill it, and update it, all we’re going to see is a window flash for a second and then close. To keep it from disappearing, we’ll call SDL_Delay. SDL_Delay will wait for a given amount of milliseconds. A millisecond is 1/1000 of a second. This means the code above will make the window wait for 2000 1/1000 of a second or 2 seconds. An important thing to note is that when SDL is delaying, it cannot accept input from the keyboard or mouse. Don’t panic when you run this program and it doesn’t respond. We haven’t given it the code to handle the mouse and keyboard.

screenSurface = SDL_GetWindowSurface( window );
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
SDL_UpdateWindowSurface( window );

After the window has delayed there for 2 seconds, we’ll destroy the window to free its memory. This will also take care of the SDL_Surface we got from it. After everything is deallocated, we quit SDL and return 0 to terminate the program.

SDL_DestroyWindow( window );

reference:

http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php

Latest From Our Blog

Shopify Web Development:

Card image cap

2022-03-01 12:59:57

To help our clients reach their business objectives, we provide complete Shopify web development services. Our Shopify developers at MS Solu...

Read more

Bigcommerace Web Development:

Card image cap

2022-03-01 08:01:34

Web development for Bigcommerace:BigCommerce is a feature-rich, hosted platform with a hosted SaaS pricing model. The ease of use and scalab...

Read more

WooCommerce Web Development:

Card image cap

2022-02-09 07:29:36

“Collaborate, organize and build work with MS solutions from virtually anywhere. It's a remote-friendly team workspace where knowledge and c...

Read more
UK Office

78 Plough Road London SW11 2AR78

Tel: +44-74-414-292-94


US Office

5415 Sugarloaf Parkway, Ste 1108 #2889 Lawrenceville, Georgia 30043

Tel: +1-25-13-580-370


UAE Office

Dubai Internet City, 1st Floor, Building Number 12, Premises ED 29, Dubai, UAE

Tel: +44 7441429294


Pakistan Office (Lahore)

MS Solution(Pvt)Ltd., office no 105,2nd floor, Chaudhary center, 168 Multan Road Lahore, Punjab, 54000

Tel: +92-42-37-481-119


Pakistan Office (Multan)

House # 434 ,Street# 8, Block-K, New Multan, Shah Rukn-e-Alam Colony,Multan

Tel: +92-61-67-714-81


Pakistan Office (Islamabad)

Spring north Commercial Bahria town, Phase 7 Islamabad, Islamabad Capital Territory 44000

Tel: +92-32-14-048-436


© 2015 - 2022, MS solution Software Inc., All Rights Reserved.

Privacy Policy and Terms of Use
Cookie Policy

Follow us on