0
+ − 1 <HTML
+ − 2 ><HEAD
+ − 3 ><TITLE
+ − 4 >Initializing SDL</TITLE
+ − 5 ><META
+ − 6 NAME="GENERATOR"
+ − 7 CONTENT="Modular DocBook HTML Stylesheet Version 1.61
+ − 8 "><LINK
+ − 9 REL="HOME"
+ − 10 TITLE="SDL Library Documentation"
+ − 11 HREF="index.html"><LINK
+ − 12 REL="UP"
+ − 13 TITLE="The Basics"
+ − 14 HREF="guidethebasics.html"><LINK
+ − 15 REL="PREVIOUS"
+ − 16 TITLE="The Basics"
+ − 17 HREF="guidethebasics.html"><LINK
+ − 18 REL="NEXT"
+ − 19 TITLE="Graphics and Video"
+ − 20 HREF="guidevideo.html"></HEAD
+ − 21 ><BODY
+ − 22 CLASS="SECT1"
+ − 23 BGCOLOR="#FFF8DC"
+ − 24 TEXT="#000000"
+ − 25 LINK="#0000ee"
+ − 26 VLINK="#551a8b"
+ − 27 ALINK="#ff0000"
+ − 28 ><DIV
+ − 29 CLASS="NAVHEADER"
+ − 30 ><TABLE
+ − 31 WIDTH="100%"
+ − 32 BORDER="0"
+ − 33 CELLPADDING="0"
+ − 34 CELLSPACING="0"
+ − 35 ><TR
+ − 36 ><TH
+ − 37 COLSPAN="3"
+ − 38 ALIGN="center"
+ − 39 >SDL Library Documentation</TH
+ − 40 ></TR
+ − 41 ><TR
+ − 42 ><TD
+ − 43 WIDTH="10%"
+ − 44 ALIGN="left"
+ − 45 VALIGN="bottom"
+ − 46 ><A
+ − 47 HREF="guidethebasics.html"
+ − 48 >Prev</A
+ − 49 ></TD
+ − 50 ><TD
+ − 51 WIDTH="80%"
+ − 52 ALIGN="center"
+ − 53 VALIGN="bottom"
+ − 54 >Chapter 1. The Basics</TD
+ − 55 ><TD
+ − 56 WIDTH="10%"
+ − 57 ALIGN="right"
+ − 58 VALIGN="bottom"
+ − 59 ><A
+ − 60 HREF="guidevideo.html"
+ − 61 >Next</A
+ − 62 ></TD
+ − 63 ></TR
+ − 64 ></TABLE
+ − 65 ><HR
+ − 66 ALIGN="LEFT"
+ − 67 WIDTH="100%"></DIV
+ − 68 ><DIV
+ − 69 CLASS="SECT1"
+ − 70 ><H1
+ − 71 CLASS="SECT1"
+ − 72 ><A
+ − 73 NAME="GUIDEBASICSINIT"
+ − 74 >Initializing SDL</A
+ − 75 ></H1
+ − 76 ><P
+ − 77 >SDL is composed of eight subsystems - Audio, CDROM, Event Handling, File I/O, Joystick Handling, Threading, Timers and Video. Before you can use any of these subsystems they must be initialized by calling <A
+ − 78 HREF="sdlinit.html"
+ − 79 ><TT
+ − 80 CLASS="FUNCTION"
+ − 81 >SDL_Init</TT
+ − 82 ></A
+ − 83 > (or <A
+ − 84 HREF="sdlinitsubsystem.html"
+ − 85 ><TT
+ − 86 CLASS="FUNCTION"
+ − 87 >SDL_InitSubSystem</TT
+ − 88 ></A
+ − 89 >. <TT
+ − 90 CLASS="FUNCTION"
+ − 91 >SDL_Init</TT
+ − 92 > must be called before any other SDL function. It automatically initializes the Event Handling, File I/O and Threading subsystems and it takes a parameter specifying which other subsystems to initialize. So, to initialize the default subsystems and the Video subsystems you would call:
+ − 93 <PRE
+ − 94 CLASS="PROGRAMLISTING"
+ − 95 > SDL_Init ( SDL_INIT_VIDEO );</PRE
+ − 96 >
+ − 97 To initialize the default subsystems, the Video subsystem and the Timers subsystem you would call:
+ − 98 <PRE
+ − 99 CLASS="PROGRAMLISTING"
+ − 100 > SDL_Init ( SDL_INIT_VIDEO | SDL_INIT_TIMER );</PRE
+ − 101 ></P
+ − 102 ><P
+ − 103 ><TT
+ − 104 CLASS="FUNCTION"
+ − 105 >SDL_Init</TT
+ − 106 > is complemented by <A
+ − 107 HREF="sdlquit.html"
+ − 108 ><TT
+ − 109 CLASS="FUNCTION"
+ − 110 >SDL_Quit</TT
+ − 111 ></A
+ − 112 > (and <A
+ − 113 HREF="sdlquitsubsystem.html"
+ − 114 ><TT
+ − 115 CLASS="FUNCTION"
+ − 116 >SDL_QuitSubSystem</TT
+ − 117 ></A
+ − 118 >). <TT
+ − 119 CLASS="FUNCTION"
+ − 120 >SDL_Quit</TT
+ − 121 > shuts down all subsystems, including the default ones. It should always be called before a SDL application exits.</P
+ − 122 ><P
+ − 123 >With <TT
+ − 124 CLASS="FUNCTION"
+ − 125 >SDL_Init</TT
+ − 126 > and <TT
+ − 127 CLASS="FUNCTION"
+ − 128 >SDL_Quit</TT
+ − 129 > firmly embedded in your programmers toolkit you can write your first and most basic SDL application. However, we must be prepare to handle errors. Many SDL functions return a value and indicates whether the function has succeeded or failed, <TT
+ − 130 CLASS="FUNCTION"
+ − 131 >SDL_Init</TT
+ − 132 >, for instance, returns -1 if it could not initialize a subsystem. SDL provides a useful facility that allows you to determine exactly what the problem was, every time an error occurs within SDL an error message is stored which can be retrieved using <TT
+ − 133 CLASS="FUNCTION"
+ − 134 >SDL_GetError</TT
+ − 135 >. Use this often, you can never know too much about an error.</P
+ − 136 ><P
+ − 137 ><PRE
+ − 138 CLASS="PROGRAMLISTING"
+ − 139 >#include "SDL.h" /* All SDL App's need this */
+ − 140 #include <stdio.h>
+ − 141
+ − 142 int main() {
+ − 143
+ − 144 printf("Initializing SDL.\n");
+ − 145
+ − 146 /* Initialize defaults, Video and Audio */
+ − 147 if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
+ − 148 printf("Could not initialize SDL: %s.\n", SDL_GetError());
+ − 149 exit(-1);
+ − 150 }
+ − 151
+ − 152 printf("SDL initialized.\n");
+ − 153
+ − 154 printf("Quiting SDL.\n");
+ − 155
+ − 156 /* Shutdown all subsystems */
+ − 157 SDL_Quit();
+ − 158
+ − 159 printf("Quiting....\n");
+ − 160
+ − 161 exit(0);
+ − 162 } </PRE
+ − 163 ></P
+ − 164 ></DIV
+ − 165 ><DIV
+ − 166 CLASS="NAVFOOTER"
+ − 167 ><HR
+ − 168 ALIGN="LEFT"
+ − 169 WIDTH="100%"><TABLE
+ − 170 WIDTH="100%"
+ − 171 BORDER="0"
+ − 172 CELLPADDING="0"
+ − 173 CELLSPACING="0"
+ − 174 ><TR
+ − 175 ><TD
+ − 176 WIDTH="33%"
+ − 177 ALIGN="left"
+ − 178 VALIGN="top"
+ − 179 ><A
+ − 180 HREF="guidethebasics.html"
+ − 181 >Prev</A
+ − 182 ></TD
+ − 183 ><TD
+ − 184 WIDTH="34%"
+ − 185 ALIGN="center"
+ − 186 VALIGN="top"
+ − 187 ><A
+ − 188 HREF="index.html"
+ − 189 >Home</A
+ − 190 ></TD
+ − 191 ><TD
+ − 192 WIDTH="33%"
+ − 193 ALIGN="right"
+ − 194 VALIGN="top"
+ − 195 ><A
+ − 196 HREF="guidevideo.html"
+ − 197 >Next</A
+ − 198 ></TD
+ − 199 ></TR
+ − 200 ><TR
+ − 201 ><TD
+ − 202 WIDTH="33%"
+ − 203 ALIGN="left"
+ − 204 VALIGN="top"
+ − 205 >The Basics</TD
+ − 206 ><TD
+ − 207 WIDTH="34%"
+ − 208 ALIGN="center"
+ − 209 VALIGN="top"
+ − 210 ><A
+ − 211 HREF="guidethebasics.html"
+ − 212 >Up</A
+ − 213 ></TD
+ − 214 ><TD
+ − 215 WIDTH="33%"
+ − 216 ALIGN="right"
+ − 217 VALIGN="top"
+ − 218 >Graphics and Video</TD
+ − 219 ></TR
+ − 220 ></TABLE
+ − 221 ></DIV
+ − 222 ></BODY
+ − 223 ></HTML
+ − 224 >