sdl_introduction_copy&pastate
来源:互联网 发布:C语言间接访问 编辑:程序博客网 时间:2024/05/17 23:19
Your first SDL2 application
This part will teach you the basic parts of
SDL2
namely SDL_Renderer
andSDL_Window
, what they do and how we use them in order to create a simple window.SDL2 structures
The SDL2
library has a substantially improved rendering structure from the oldSDL
. The new structure is very simple and easy to understand. This is a short tutorial / reference guide that will teach you everything you need to know in order to start programmingSDL2
applications.
The two major objects you need to know :
SDL_Window
This is the physical window you see on your screen. One SDL_Window
represents one physical window on your screen. You can have as manySDL_Window
s as you like. The structs holds info about the window like position, size, window state and window style.
SDL_Renderer
The
SDL_Renderer
is basically what you use to render to the screen. The renderer is usually tied to a window. One renderer can only render within one window. TheSDL_Renderer
also contains info about the rending itself like hardware acceleration and v-sync prevention.This tutorial will focus on creating and setting up these so we can start drawing to our screen.
Setting up SDL2
Before you can use
SDL2
, you must set it up by initializing it and creating aSDL_Window
and a SDL_Renderer
This section will help you to just that.Initializing SDL2
The first thing you need to do is to initialize SDL. This is done using the following function :
Parameters :
- flags – specifies what you want to initialize. Set it to
SDL_INIT_EVERYTHING
to initialize everything.
Return value
0
on success-1
oterwise
Since the function can fail ( though it probably won’t ) you should add code that handles this. I also recommend you print an error message withSLD_GetError()
because it will tell you what went wrong. This tutorial will do this for the most important setup functions.
This makes our code so far look something like this :
Setting up the SDL_Window
Now that we have initialized
SDL2
, we can can start creating our window, this is done by using the following function :Parameters :
title
-the window caption/title that appears on the top of the windowposX
– x position of the window.posY
– y position of the window.sizeW
– width of the windowsizeH
– height of the windowflags
– specifies window state or properties. More info here
The flags
parameters has a few different possible values I will not go through all of them, but you can read about themhere
Here are the flags that are most revelant to us now.
SDL_WINDOW_FULLSCREEN
-the window will start out in fullscreen modeSDL_WINDOW_FULLSCREEN_DESKTOP
-the window will start out in fullscreen mode with the same resoltion as your desktop currently hasSDL_WINDOW_OPENGL
-for use withOpenGL
, see my tutorial onOpenGL
SDL_WINDOW_SHOWN
-the window will start out being visibleSDL_WINDOW_HIDDEN
-the window will start out being invisible
The window will always start out visible, regardless of whether SDL_WINDOW_SHOWN
is set. The only way to make a window start out as hidden is to passSDL_WINDOW_HIDDEN
. Because of this, the SDL_WINDOW_SHOWN
flag is ignore bySDL_CreateWindow
, and we can just pass 0
here.
Return value
A valid pointer on success.
nullptr
/NULL
on oterwise
We’ll set the flag parameter to 0
for now. This simply means we’ll end up with a standard window that’s not maximized, minimized or fullscreen.
Remember to handle it if the function returns NULL
. So we end up with something like this :
Creating the SDL_Renderer
Now for the last object we need to start rendering. This time it’s a little bit more work involved, but nothing too scary. We begin by creating the renderer. This is done using the following function :
Parameters :
window
– theSDL_Window
this renderer will be attached to. In our case, this is theSDL_Window
we just created.index -
specifies which rendering driver to use. It sounds complicated, but we can just specify-1
. This means we’ll use the first driver we can find.flags -
species how the rendering should be done. For now, we’ll just useSDL_RENDERER_ACCELERATED
which lets us use the graphics card to render quickly. You can read morehere.
Return value :
Window a valid pointer on success. Otherwise
nullptr
/NULL
.
As always ; remember to handle any return of 0
.
Setting up the renderer
Now that we have created the
SDL_Renderer
we are technically ready to start rendering. But there are a few more things we should do first…First of all we should set the resolution of the renderer :
Parameters :
renderer
– theSDL_Renderer
we want to set the resolution on.width -
the desired width in pixelsheight -
the desired height in pixels
Return value
0
on success-1
oterwise
This is pretty straight forward, the first argument is the renderer on which to set the resolution on. The second and third is the width and height. Normally these are the same as the width and height of theSDL_Window
.
And now the time has come to set the color. Here is the function for doing that :
Parameters :
renderer
– theSDL_Renderer
on which to set render colorspecifies amount of red ( 0 – 255 )
red
-specifies amount of green ( 0 – 255 )
-
green
specifies amount of blue ( 0 – 255 )
-
blue
specifies amount of alpha ( 0 – 255 ) 0 = completely transparent
-
alpha
Return value
0
on success-1
oterwise
That’s it! We’re done with the setup. And now we can start using the renderer for fun stuff!
Rendering something
Now it’s time to get started on the renderng.
The first thing we need to do before drawing something, is to clear our window. This means we fill our window with the color we set usingSDL_SetRenderDrawColor()
This is done by calling :
Parameters :
renderer
– theSDL_Renderer
that we’ll clear
Return value
0
on success-1
oterwise
This function should be called at the beginning on every frame to clear the screen and make it ready for more stuff.
But…
SDL_RenderClear
( and any other SDL_Render
functions ) works behind the scenes. They don’t actually draw anything on the screen. So, in order for he drawing ( includingSDL_RenderClear
) to take effect, we need to call another function.Parameters :
renderer
– theSDL_Renderer
on which to render
Return value
0
on success-1
oterwise
This function takes whatever you have drawn to the screen using SDL_Render*
and actually puts it on the screen. So that after calling this, the screen will be all nice and red. And that’s it for now. In the next part I’ll go into rectangles and actually rendering something interesting ( yes, I assume red screens doesn’t catch your interest. )
Conclusion
That’s if for part 2. We now have a nice red window. Next time we’ll be rendering rects and other neat stuff.
Here is all the code so far. You should be able to just copy paste and compile.
Feel free to comment if you have anything to say or ask questions if anything is unclear. I always appreciate getting comments.
You can also email me : olevegard@headerphile.com- sdl_introduction_copy&pastate
- OC语言——传入时间与现在时间 差距 60 秒以内,输出@"刚刚" 以此类推
- Sublime Text3工具的安装、破解、VIM功能vintage插件教程(已经实践、绝对可用)
- Qt学习之Qt控件的介绍
- ajax的return返回值为undefined
- 【设计模式】——建造者模式 单例模式
- sdl_introduction_copy&pastate
- Matlab基础用法总结
- ZOJ - 3490 String Successor 递归模拟
- Oracle 序列
- 第八周 项目三 顺序串算法(3)
- 黑马程序员——Java之基本数据类型包装类及泛型
- 时间复杂度
- 【leetcode】318. Maximum Product of Word Lengths
- lua 编译