Mercurial > projects > mde
comparison mde/SDL.d @ 20:838577503598
Reworked much of Init.
Moved mde.Init to mde.scheduler.Init and largely cleaned up the code.
Implemented mde.scheduler.InitStage to reduce dependancies of modules running Init functions.
committer: Diggory Hardy <diggory.hardy@gmail.com>
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Sat, 22 Mar 2008 16:22:59 +0000 |
parents | 5f90774ea1ef |
children | a60cbb7359dd |
comparison
equal
deleted
inserted
replaced
19:db0b48f02b69 | 20:838577503598 |
---|---|
15 | 15 |
16 /** Just a temporary place to put SDL Init and Video stuff. | 16 /** Just a temporary place to put SDL Init and Video stuff. |
17 */ | 17 */ |
18 module mde.SDL; | 18 module mde.SDL; |
19 | 19 |
20 import mde.Init; | 20 import mde.scheduler.InitStage; |
21 import mde.input.joystick; | 21 import mde.input.joystick; |
22 | 22 |
23 import tango.util.log.Log : Log, Logger; | 23 import tango.util.log.Log : Log, Logger; |
24 import tango.stdc.stringz; | |
24 | 25 |
25 import derelict.sdl.sdl; | 26 import derelict.sdl.sdl; |
27 import derelict.opengl.gl; | |
26 import derelict.util.exception; | 28 import derelict.util.exception; |
27 | 29 |
28 Logger logger; | 30 Logger logger; |
29 static this() { | 31 static this() { |
30 logger = Log.getLogger ("mde.init.SDL"); | 32 logger = Log.getLogger ("mde.SDL"); |
31 | 33 |
32 Init.addFunc (&initSDL); | 34 init2.addFunc (&initSdlAndGl); |
35 init4.addFunc (&setupWindow); | |
33 } | 36 } |
34 | 37 |
35 void initSDL() { | 38 void initSdlAndGl() { // init2 func |
36 | 39 // Load SDL and GL dynamic libs |
37 // Inits SDL and related stuff (joystick). | |
38 try { | 40 try { |
39 DerelictSDL.load(); | 41 DerelictSDL.load(); |
42 DerelictGL.load(); | |
40 } catch (DerelictException de) { | 43 } catch (DerelictException de) { |
41 logger.fatal ("Loading dynamic library failed:"); | 44 logger.fatal ("Loading dynamic library failed:"); |
42 logger.fatal (de.msg); | 45 logger.fatal (de.msg); |
43 | 46 |
44 init.setFailure (); // abort | 47 init2.setFailure (); |
45 return; | 48 return; |
46 } | 49 } |
47 logger.trace ("Derelict: loaded SDL"); | 50 logger.trace ("Derelict: loaded SDL and OpenGL"); |
48 | 51 |
52 // Initialise SDL | |
49 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK /+| SDL_INIT_EVENTTHREAD+/)) { | 53 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK /+| SDL_INIT_EVENTTHREAD+/)) { |
50 logger.fatal ("SDL initialisation failed:"); | 54 logger.fatal ("SDL initialisation failed:"); |
51 char* msg = SDL_GetError (); | 55 char* msg = SDL_GetError (); |
52 logger.fatal (msg ? fromStringz(msg) : "no reason available"); | 56 logger.fatal (msg ? fromStringz(msg) : "no reason available"); |
53 | 57 |
54 init.setFailure (); // abort | 58 init2.setFailure (); |
55 return; | 59 return; |
56 } | 60 } |
57 | 61 |
58 init.addCleanupFct (&cleanupSDL); | 62 cleanup2.addFunc (&cleanupSDL); |
59 logger.trace ("SDL initialised"); | 63 logger.trace ("SDL initialised"); |
60 | 64 |
61 /+ Load of info-printing stuff | 65 // Must be called after SDL has been initialised, so cannot be a separate Init function. |
66 openJoysticks (); // after SDL init | |
67 cleanup2.addFunc (&closeJoysticks); | |
68 } | |
69 | |
70 void setupWindow() { // init4 func | |
71 // Open a window | |
72 // FIXME: provide a way to re-set video mode and change settings | |
73 if (SDL_SetVideoMode (800, 600, 0, 0) is null) { // Can't open in windows!! | |
74 logger.fatal ("Unable to set video mode:"); | |
75 char* msg = SDL_GetError (); | |
76 logger.fatal (msg ? fromStringz(msg) : "no reason available"); | |
77 | |
78 init4.setFailure (); | |
79 return; | |
80 } | |
81 } | |
82 | |
83 void cleanupSDL () { | |
84 SDL_Quit(); | |
85 } | |
86 | |
87 /+ Load of info-printing stuff (currently doesn't have a use) | |
62 // Print a load of info: | 88 // Print a load of info: |
63 logger.info ("Available video modes:"); | 89 logger.info ("Available video modes:"); |
64 char[128] tmp; | 90 char[128] tmp; |
65 SDL_Rect** modes = SDL_ListModes (null, SDL_FULLSCREEN); | 91 SDL_Rect** modes = SDL_ListModes (null, SDL_FULLSCREEN); |
66 if (modes is null) logger.info ("None!"); | 92 if (modes is null) logger.info ("None!"); |
68 else { | 94 else { |
69 for (uint i = 0; modes[i] !is null; ++i) { | 95 for (uint i = 0; modes[i] !is null; ++i) { |
70 logger.info (logger.format (tmp, "\t{}x{}", modes[i].w, modes[i].h)); | 96 logger.info (logger.format (tmp, "\t{}x{}", modes[i].w, modes[i].h)); |
71 } | 97 } |
72 } | 98 } |
73 | 99 |
74 SDL_VideoInfo* vi = SDL_GetVideoInfo (); | 100 SDL_VideoInfo* vi = SDL_GetVideoInfo (); |
75 if (vi !is null) { | 101 if (vi !is null) { |
76 logger.info ("Video info:"); | 102 logger.info ("Video info:"); |
77 logger.info ("Hardware surface support: "~ (vi.flags & SDL_HWSURFACE ? "yes" : "no")); | 103 logger.info ("Hardware surface support: "~ (vi.flags & SDL_HWSURFACE ? "yes" : "no")); |
78 logger.info (logger.format (tmp, "Video memory: {}", vi.video_mem)); | 104 logger.info (logger.format (tmp, "Video memory: {}", vi.video_mem)); |
79 | 105 |
80 if (vi.vfmt !is null) { | 106 if (vi.vfmt !is null) { |
81 logger.info ("Best video mode:"); | 107 logger.info ("Best video mode:"); |
82 logger.info (logger.format (tmp, "Bits per pixel: {}", vi.vfmt.BitsPerPixel)); | 108 logger.info (logger.format (tmp, "Bits per pixel: {}", vi.vfmt.BitsPerPixel)); |
83 } | 109 } |
84 } | 110 } |
85 +/ | 111 +/ |
86 | |
87 // FIXME: make this non-fatal and provide a way to re-set video mode | |
88 if (SDL_SetVideoMode (800, 600, 0, 0) is null) { // Can't open in windows!! | |
89 logger.fatal ("Unable to set video mode:"); | |
90 char* msg = SDL_GetError (); | |
91 logger.fatal (msg ? fromStringz(msg) : "no reason available"); | |
92 | |
93 init.setFailure (); | |
94 return; | |
95 } | |
96 | |
97 openJoysticks (); // after SDL init | |
98 init.addCleanupFct (&closeJoysticks); | |
99 } | |
100 | |
101 void cleanupSDL () { | |
102 SDL_Quit(); | |
103 } |