1<HTML 2><HEAD 3><TITLE 4>Initializing SDL</TITLE 5><META 6NAME="GENERATOR" 7CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ 8"><LINK 9REL="HOME" 10TITLE="SDL Library Documentation" 11HREF="index.html"><LINK 12REL="UP" 13TITLE="The Basics" 14HREF="guidethebasics.html"><LINK 15REL="PREVIOUS" 16TITLE="The Basics" 17HREF="guidethebasics.html"><LINK 18REL="NEXT" 19TITLE="Graphics and Video" 20HREF="guidevideo.html"></HEAD 21><BODY 22CLASS="SECT1" 23BGCOLOR="#FFF8DC" 24TEXT="#000000" 25LINK="#0000ee" 26VLINK="#551a8b" 27ALINK="#ff0000" 28><DIV 29CLASS="NAVHEADER" 30><TABLE 31SUMMARY="Header navigation table" 32WIDTH="100%" 33BORDER="0" 34CELLPADDING="0" 35CELLSPACING="0" 36><TR 37><TH 38COLSPAN="3" 39ALIGN="center" 40>SDL Library Documentation</TH 41></TR 42><TR 43><TD 44WIDTH="10%" 45ALIGN="left" 46VALIGN="bottom" 47><A 48HREF="guidethebasics.html" 49ACCESSKEY="P" 50>Prev</A 51></TD 52><TD 53WIDTH="80%" 54ALIGN="center" 55VALIGN="bottom" 56>Chapter 1. The Basics</TD 57><TD 58WIDTH="10%" 59ALIGN="right" 60VALIGN="bottom" 61><A 62HREF="guidevideo.html" 63ACCESSKEY="N" 64>Next</A 65></TD 66></TR 67></TABLE 68><HR 69ALIGN="LEFT" 70WIDTH="100%"></DIV 71><DIV 72CLASS="SECT1" 73><H1 74CLASS="SECT1" 75><A 76NAME="GUIDEBASICSINIT" 77></A 78>Initializing SDL</H1 79><P 80>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 81HREF="sdlinit.html" 82><TT 83CLASS="FUNCTION" 84>SDL_Init</TT 85></A 86> (or <A 87HREF="sdlinitsubsystem.html" 88><TT 89CLASS="FUNCTION" 90>SDL_InitSubSystem</TT 91></A 92>). <TT 93CLASS="FUNCTION" 94>SDL_Init</TT 95> 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: 96<PRE 97CLASS="PROGRAMLISTING" 98> SDL_Init ( SDL_INIT_VIDEO );</PRE 99> 100To initialize the default subsystems, the Video subsystem and the Timers subsystem you would call: 101<PRE 102CLASS="PROGRAMLISTING" 103> SDL_Init ( SDL_INIT_VIDEO | SDL_INIT_TIMER );</PRE 104></P 105><P 106><TT 107CLASS="FUNCTION" 108>SDL_Init</TT 109> is complemented by <A 110HREF="sdlquit.html" 111><TT 112CLASS="FUNCTION" 113>SDL_Quit</TT 114></A 115> (and <A 116HREF="sdlquitsubsystem.html" 117><TT 118CLASS="FUNCTION" 119>SDL_QuitSubSystem</TT 120></A 121>). <TT 122CLASS="FUNCTION" 123>SDL_Quit</TT 124> shuts down all subsystems, including the default ones. It should always be called before a SDL application exits.</P 125><P 126>With <TT 127CLASS="FUNCTION" 128>SDL_Init</TT 129> and <TT 130CLASS="FUNCTION" 131>SDL_Quit</TT 132> 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 133CLASS="FUNCTION" 134>SDL_Init</TT 135>, 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 136CLASS="FUNCTION" 137>SDL_GetError</TT 138>. Use this often, you can never know too much about an error.</P 139><DIV 140CLASS="EXAMPLE" 141><A 142NAME="AEN60" 143></A 144><P 145><B 146>Example 1-1. Initializing SDL</B 147></P 148><PRE 149CLASS="PROGRAMLISTING" 150>#include "SDL.h" /* All SDL App's need this */ 151#include <stdio.h> 152 153int main(int argc, char *argv[]) { 154 155 printf("Initializing SDL.\n"); 156 157 /* Initialize defaults, Video and Audio */ 158 if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { 159 printf("Could not initialize SDL: %s.\n", SDL_GetError()); 160 exit(-1); 161 } 162 163 printf("SDL initialized.\n"); 164 165 printf("Quiting SDL.\n"); 166 167 /* Shutdown all subsystems */ 168 SDL_Quit(); 169 170 printf("Quiting....\n"); 171 172 exit(0); 173} </PRE 174></DIV 175></DIV 176><DIV 177CLASS="NAVFOOTER" 178><HR 179ALIGN="LEFT" 180WIDTH="100%"><TABLE 181SUMMARY="Footer navigation table" 182WIDTH="100%" 183BORDER="0" 184CELLPADDING="0" 185CELLSPACING="0" 186><TR 187><TD 188WIDTH="33%" 189ALIGN="left" 190VALIGN="top" 191><A 192HREF="guidethebasics.html" 193ACCESSKEY="P" 194>Prev</A 195></TD 196><TD 197WIDTH="34%" 198ALIGN="center" 199VALIGN="top" 200><A 201HREF="index.html" 202ACCESSKEY="H" 203>Home</A 204></TD 205><TD 206WIDTH="33%" 207ALIGN="right" 208VALIGN="top" 209><A 210HREF="guidevideo.html" 211ACCESSKEY="N" 212>Next</A 213></TD 214></TR 215><TR 216><TD 217WIDTH="33%" 218ALIGN="left" 219VALIGN="top" 220>The Basics</TD 221><TD 222WIDTH="34%" 223ALIGN="center" 224VALIGN="top" 225><A 226HREF="guidethebasics.html" 227ACCESSKEY="U" 228>Up</A 229></TD 230><TD 231WIDTH="33%" 232ALIGN="right" 233VALIGN="top" 234>Graphics and Video</TD 235></TR 236></TABLE 237></DIV 238></BODY 239></HTML 240>