SDL Quick Start

Source: Internet
Author: User
Tags transparent color

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

Discuss newsgroups and documents

Summary

Learn How to Use SDL to create a window and draw a graph.

Preface

Today, I want to make a simple demo, because some of them need to use Objective C, so we still need to use cross-platform. I found that none of the things I know is really suitable for doing this, cocos2d for iPhone can only run under iPhone, HGE can only run under Windows, orx can run across platforms, but obviously, it is too troublesome to do a simple demo, this is because I only need a simple drawbmp function. orx is not suitable for using a set of functions once it is used. What I can think of is OpenGL, however, it is quite troublesome to use OpenGL for cross-platform applications on your own. You should find a framework.
In fact, I have three options: glut/free glut, SDL, and glfw. Although glut has been used when learning OpenGL, I don't want to invest more time in it because the project is dead, it is enough to understand the code level (in fact, it is very easy to use. SDL was recommended by many people. When I was looking for a job, a Chinese company mentioned it directly, which means that it was used in China. I have also read the SDL API since then, and it is still simple. Like HGE, image display uses a abstraction similar to DirectDraw. I am interested in glfw because iarwain, the author of orx, mentioned it recently and gave a high comment, he was very impressed with glfw's lightweight model. In the latest orx version, glfw is the default plug-in of orx, and iarwain testing is about 5% faster than SDL (although not too much ), the most important thing is that the glfw encapsulation is very simple. It focuses on using OpenGL directly. By taking this opportunity, I will also review OpenGL. Recently, I have been using libraries, I almost forgot how to use it.
Compared with each other, I found that I don't know whether to Use SDL or glfw. According to my habits, the two are used together first, and then come to a conclusion. This article takes a look at SDL.

Actual use

In fact, my requirements are very simple. I create a window and draw a graph at the place where I made it. Simply put, it is similar to the createwindows and drawbmp functions.

SDL is actually quite famous. Some people have mentioned that even if you are not really ready to Use SDL, you can think of a library that can be transplanted to so many platforms, abstract encapsulation methods and source code are at least worth studying. Because of this, I will also take a look at it, although it is really not intended to be a long-term SDL. The SDL protocol is lgpl (or commercial) and is acceptable.
The environment is easy to build. Windows SDL requires d3d SDK support.
For a simple reference, the process of displaying BMP images is simple:
1

2
# Include


3
# Include


4
# Include


5
# Include
"SDL. H"

6

7
Int
_ Tmain (int
Argc, _ tchar * argv [])
8
{
9
If
(Sdl_init (sdl_init_audio | sdl_init_video) <0
)
10
{
11
Printf ("unable to initialize SDL:
% S
N
"
, Sdl_geterror ());
12

13
Exit (1
);
14
}
15
Atexit (sdl_quit );
16

17
// Load Image

18
Sdl_surface * Picture = sdl_loadbmp ("dragon.bmp"
);
19

20
Sdl_surface * screen = sdl_setvideomode (640
, 480
, 16
, Sdl_doublebuf );
21
If
(Screen = NULL
)
22
{
23
Printf ("unable to set video mode:
% S
N
"
, Sdl_geterror ());
24

25
Exit (1
);
26
}
27

28
// Apply image to screen

29
Sdl_blitsurface (picture, null
, Screen, null
);
30

31
// Update Screen

32
Sdl_flip (screen );
33

34
// Pause

35
Sdl_delay (2000
);
36

37
// Free the loaded image

38
Sdl_freesurface (picture );
39

40
Return
1
;
41
}

Here, we can see that SDL does not manage the main loop, and I have not used my own main loop. It involves the SDL event system. Therefore, in this demonstration, sdl_delay is used, to view the image. The size is not specified and the Alpha value is not specified. Therefore, the original size of the image is displayed. Sdl_blitsurface
Functions are similar to Windows API functions. There is nothing to say about others. You can see what you are doing by reading comments and function names.

Then, set the desired transparent color by using the following methods (in BMP without alpha channel, you can only use such a poor Color Key method)
1

2
Uint32 colorkey = sdl_maprgb (picture-> Format, 0xff
, 0xff
, 0xff
);
3
Sdl_setcolorkey (picture, sdl_srccolorkey, colorkey );


0xff, 0xff, and 0xff are respectively the R, G, and B colors that you want to set. Here they are both 0xff, Which is white.
Therefore, the source image:

In the black background, the surrounding white is transparent and the following effects are displayed:

To be honest, it is relatively simple to use SDL APIs. It is a bit time-space traversal, a little back to learning to use Win32 APIs to do similar things. The concept of interfaces is almost the same. Perhaps the advantage of SDL is that the Win32 API in the past only lies in speed and cross-platform.
To display the two graphs, you only need to perform blit once again:
// Apply image to screen

Sdl_blitsurface (picture, null
, Screen, null
);

Sdl_rect DEST;
DeST. x = picture-> W;
DeST. Y = 0
;

// Apply image to screen again and move it to right

Sdl_blitsurface (picture, null
, Screen, & DEST );


Then, add SDL to control the main loop.

// Main Loop

Bool
Running = true
;
While
(Running ){
// Update Screen

Sdl_flip (screen );

// Delay, 50 for simple

Sdl_delay (50
);

// While there's an event to handle

Sdl_event event;
While
(Sdl_pollevent (& event )){
If
(Event. type = sdl_quit ){
Running = false
;
}
}
}


Sdl_pollevent is used to poll SDL events.
Now, the windows created by SDL can be dragged or closed.
What I want to say is that SDL is really simple. When I look at the tutorial, I basically only need to look at the source code to understand most of the meaning.

As shown in the following key section, although SDL only supports BMP, someone has created a library named sdl_image that supports other formats. (Test whether an open-source database is good or not, and whether there is good 3rd-party support is very important, SDL is obviously good) So we don't need to use libpng directly, I personally do not like libpng interfaces very much .....
I don't know if I have adhered to the excellent tradition of simple SDL, and the use of sdl_image is also very simple. After compilation, I will compile the dynamic library in it (because we need to support PNG, so we can copy the dynamic libraries with libpng and zlib to the running directory and then include "sdl_image.h. The distance from BMP to PNG is only a few lines of code ..... It takes less than 10 minutes to load and display a PNG image from sdl_image ....
All source code:

# Include


# Include


# Include


# Include
"SDL. H"

# Include
"Sdl_image.h"

Int
_ Tmain (int
Argc, _ tchar * argv [])
{
If
(Sdl_init (sdl_init_audio | sdl_init_video) <0
)
{
Printf ("unable to initialize SDL:
% S
N
"
, Sdl_geterror ());

Exit (1
);
}
If
(Img_init (img_init_png) = 0
){
Printf ("unable to initialize sdl_image"
);

Exit (1
);
}
Atexit (sdl_quit );

// Load Image

// Sdl_surface * Picture = sdl_loadbmp ("dragon.bmp ");

Sdl_surface * Picture = img_load ("dragon.png"
);

Sdl_surface * screen = sdl_setvideomode (640
, 480
, 16
, Sdl_doublebuf );
If
(Screen = NULL
)
{
Printf ("unable to set video mode:
% S
N
"
, Sdl_geterror ());

Exit (1
);
}

// Because we use PNG with Alpha now

// Uint32 colorkey = sdl_maprgb (picture-> Format, 0xff, 0xff, 0xff );

// Sdl_setcolorkey (picture, sdl_srccolorkey, colorkey );

// Apply image to screen

Sdl_blitsurface (picture, null
, Screen, null
);

Sdl_rect DEST;
DeST. x = picture-> W;
DeST. Y = 0
;

// Apply image to screen again and move it to right

Sdl_blitsurface (picture, null
, Screen, & DEST );

// Main Loop

Bool
Running = true
;
While
(Running ){
// Update Screen

Sdl_flip (screen );

// Delay, 50 for simple

Sdl_delay (50
);

// While there's an event to handle

Sdl_event event;
While
(Sdl_pollevent (& event )){
If
(Event. type = sdl_quit ){
Running = false
;
}
}
}

// Free the loaded image

Sdl_freesurface (picture );

Return
1
;
}


There are only a few lines of modifications, which are clear in the code.

 

Summary

Simple and simple. This is my biggest experience in learning SDL. The API design is simple and related concepts are simple. SDL is worthy of the Simple DirectMedia Layer. To really start learning from OpenGL, and then call libpng to load PNG images and display them, you have to see the tenth chapter of the redbooks until the texture maps are learned. However, it is too easy to do these tasks in SDL. Even compared with the Win32 API of the year, the windows-specific message loop principle and other things are much less learned. Compared with the APIs used to create windows, the MS group is useless, to create an API in the designed window, you need to use dozens of lines of code to create a window. You also need to register the window class ........... In fact, you only need a line of code ......

Generally, a popular open-source library is good because it is good and popular. It is better because it is popular. The most important thing is that it should be easy to use, the complicated and difficult-to-use libraries, no matter how elaborate the design, are difficult to attract users, making it difficult to enter this virtuous circle. I have to say that SDL is in this regard, it is very good.

 

 

The author of the original article retains the copyright reprinted. Please indicate the original author and give a link

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.