SDL_Init

Use this function to initialize the SDL library. This must be called before using any other SDL function.
使用这个函数可以初始化SDL库,在使用SDL其他的库函数之前必须被调用。

Syntax 语法

  1. int SDL_Init(Uint32 flags)

Function Parameters 函数相关参数

参数名 解释
flags subsystem initialization flags; see Remarks for details 子系统初始化标志,看 Remarks 注意 了解跟多消息。

Return Value 返回值

Returns 0 on success or a negative error code on failure; call SDL_GetError() for more information.
返回0成功,负数则失败,可以通过调用函数 SDL_GetError 获取错误详情。

Code Examples 代码示例

C

  1. #include "SDL.h"
  2. #include <stdio.h> /* for fprintf() */
  3. #include <stdlib.h> /* for atexit() */
  4. int main(int argc, char** argv) {
  5. if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
  6. fprintf(stderr,
  7. "\nUnable to initialize SDL: %s\n",
  8. SDL_GetError()
  9. );
  10. return 1;
  11. }
  12. atexit(SDL_Quit);
  13. /* ... */
  14. return 0;
  15. }

C++

  1. 切换行号显示
  2. #include <exception>
  3. #include <string>
  4. #include "SDL.h"
  5. class InitError: public std::exception {
  6. public:
  7. InitError();
  8. InitError(const std::string&);
  9. virtual ~InitError() throw();
  10. virtual const char* what() const throw();
  11. private:
  12. std::string msg;
  13. };
  14. InitError::InitError():
  15. exception(), msg(SDL_GetError()) {}
  16. InitError::InitError(const std::string& m):
  17. exception(), msg(m) {}
  18. InitError::~InitError() throw() {}
  19. const char* InitError::what() const throw() {
  20. return msg.c_str();
  21. }
  22. class SDL {
  23. public:
  24. SDL(Uint32 flags = 0) throw(InitError);
  25. virtual ~SDL();
  26. };
  27. SDL::SDL(Uint32 flags) throw(InitError) {
  28. if (SDL_Init(flags) != 0)
  29. throw InitError();
  30. }
  31. SDL::~SDL() {
  32. SDL_Quit();
  33. }
  34. /* ... */
  35. #include <iostream>
  36. int main(int argc, char **argv) {
  37. try {
  38. SDL sdl(SDL_INIT_VIDEO|SDL_INIT_TIMER);
  39. /* ... */
  40. return 0;
  41. }
  42. catch (const InitError& err) {
  43. std::cerr
  44. << "Error while initializing SDL: "
  45. << err.what() << std::endl;
  46. }
  47. return 1;
  48. }

Remarks 注意

SDL_Init() simply forwards to calling SDL_InitSubSystem().

Therefore, the two may be used interchangeably.

Though for readability of your code SDL_InitSubSystem() might be preferred.

The File I/O and Threading subsystems are initialized by default.

You must specifically initialize other subsystems if you use them in your application.

flags may be any of the following OR’d together:

初始标志 解释
SDL_INIT_TIMER timer subsystem
SDL_INIT_AUDIO audio subsystem
SDL_INIT_VIDEO video subsystem. Automatically initializes the SDL_INIT_EVENTS subsystem
SDL_INIT_JOYSTICK joystick subsystem
SDL_INIT_HAPTIC haptic (force feedback) subsystem
SDL_INIT_GAMECONTROLLER controller subsystem. Automatically initializes the SDL_INIT_JOYSTICK subsystem
SDL_INIT_EVENTS events subsystem
SDL_INIT_EVERYTHING all of the above subsystems
SDL_INIT_NOPARACHUTE compatibility; this flag is ignored

If you want to initialize subsystems separately you would call SDL_Init(0) followed by SDL_InitSubSystem() with the desired subsystem flag.

Related Functions 相关函数

  1. SDL_InitSubSystem
  2. SDL_Quit
  3. SDL_SetMainReady
  4. SDL_WasInit