comparison mde/SDL.d @ 25:2c28ee04a4ed

Some minor and some futile efforts. Played around with init functions, had problems, gave up and put them back. Removed idea for multiple init stages; it's not good for performance or simplicity. Adjusted exception messages. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 03 Apr 2008 17:26:52 +0100
parents 32eff0e01c05
children 611f7b9063c6
comparison
equal deleted inserted replaced
24:32eff0e01c05 25:2c28ee04a4ed
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.scheduler.InitStage; 20 import mde.scheduler.InitFunctions;
21 import mde.input.joystick; 21 import mde.input.joystick;
22 import mde.options; 22 import mde.options;
23 import mde.gl; 23 import mde.gl;
24 import global = mde.global; 24 import global = mde.global;
25 25
32 32
33 Logger logger; 33 Logger logger;
34 static this() { 34 static this() {
35 logger = Log.getLogger ("mde.SDL"); 35 logger = Log.getLogger ("mde.SDL");
36 36
37 init2.addFunc (&initSdlAndGl); 37 init.addFunc (&initSdlAndGl);
38 init4.addFunc (&setupWindow);
39 } 38 }
40 39
41 private uint flags = 0; 40 private uint flags = 0;
42 41
43 void initSdlAndGl() { // init2 func 42 void initSdlAndGl() { // init func
44 debug logger.trace ("init2: initSdlAndGl() started");
45
46 // Load SDL and GL dynamic libs 43 // Load SDL and GL dynamic libs
47 try { 44 try {
48 DerelictSDL.load(); 45 DerelictSDL.load();
49 DerelictGL.load(); 46 DerelictGL.load();
50 } catch (DerelictException de) { 47 } catch (DerelictException de) {
64 61
65 setInitFailure (); 62 setInitFailure ();
66 return; 63 return;
67 } 64 }
68 65
69 cleanup2.addFunc (&cleanupSDL);
70 debug logger.trace ("SDL initialised"); 66 debug logger.trace ("SDL initialised");
71 67
72 // Must be called after SDL has been initialised, so cannot be a separate Init function. 68 // Must be called after SDL has been initialised, so cannot be a separate Init function.
73 openJoysticks (); // after SDL init 69 openJoysticks (); // after SDL init
74 cleanup2.addFunc (&closeJoysticks); 70 cleanup.addFunc (&cleanupSDL);
75 71
76 debug logger.trace ("init2: initSdlAndGl() finished"); 72 setupWindow();
77 } 73 }
78 74
79 version = MDE_OPENGL; 75 void setupWindow() { // indirect init func (depends on initSdlAndGl)
80
81 void setupWindow() { // init4 func
82 debug logger.trace ("init4: setupWindow() started");
83
84 // Window creation flags and size 76 // Window creation flags and size
85 /* NOTE: I'm getting an API mismatch error from the nvidia driver when using OpenGL, 77 flags = SDL_OPENGL;
86 * thus I've temporarily disabled it. */
87 version (MDE_OPENGL) flags = SDL_OPENGL;
88 if (vidOpts.hardware) flags |= SDL_HWSURFACE | SDL_DOUBLEBUF; 78 if (vidOpts.hardware) flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
89 else flags |= SDL_SWSURFACE; 79 else flags |= SDL_SWSURFACE;
90 int w, h; 80 int w, h;
91 if (vidOpts.fullscreen) { 81 if (vidOpts.fullscreen) {
92 flags |= SDL_FULLSCREEN; 82 flags |= SDL_FULLSCREEN;
98 if (vidOpts.noFrame) flags |= SDL_NOFRAME; 88 if (vidOpts.noFrame) flags |= SDL_NOFRAME;
99 w = vidOpts.windowW; 89 w = vidOpts.windowW;
100 h = vidOpts.windowH; 90 h = vidOpts.windowH;
101 } 91 }
102 92
103 version (MDE_OPENGL) {
104 // OpenGL attributes 93 // OpenGL attributes
105 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); 94 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
106 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); 95 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
107 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); 96 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
108 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); 97 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
109 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); 98 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
110 }
111 99
112 // Open a window 100 // Open a window
113 if (SDL_SetVideoMode (w, h, 32, flags) is null) { 101 if (SDL_SetVideoMode (w, h, 32, flags) is null) {
114 logger.fatal ("Unable to set video mode:"); 102 logger.fatal ("Unable to set video mode:");
115 char* msg = SDL_GetError (); 103 char* msg = SDL_GetError ();
117 105
118 setInitFailure (); 106 setInitFailure ();
119 return; 107 return;
120 } 108 }
121 109
110 // Now (must be done after GL context is created) try to load later version:
111 /+ No later GL features are currently used.
112 try {
113 DerelictGL.loadVersions(GLVersion.Version21);
114 } catch (DerelictException de) {
115 logger.fatal ("Loading OpenGL version > 1.1 failed:");
116 logger.fatal (de.msg);
117
118 setInitFailure ();
119 return;
120 }
121 +/
122
122 // OpenGL stuff: 123 // OpenGL stuff:
123 glSetup(); 124 glSetup();
124 125
125 // Projection (mde.gl) 126 // Projection (mde.gl)
126 setProjection (w, h); 127 setProjection (w, h);
127 128
128 // Window-manager settings 129 // Window-manager settings
129 SDL_WM_SetCaption (toStringz ("mde"), null); 130 SDL_WM_SetCaption (toStringz ("mde"), null);
130 // SDL_WM_GrabInput (use later) 131 // SDL_WM_GrabInput (use later)
131
132 debug logger.trace ("init4: setupWindow() finished");
133 } 132 }
134 133
135 void resizeWindow (int w, int h) { 134 void resizeWindow (int w, int h) {
136 if (vidOpts.fullscreen) { 135 if (vidOpts.fullscreen) {
137 Options.setInt ("video", "screenW", w); 136 Options.setInt ("video", "screenW", w);
151 150
152 // Reset the projection and viewport 151 // Reset the projection and viewport
153 setProjection (w, h); 152 setProjection (w, h);
154 } 153 }
155 154
156 void cleanupSDL () { // cleanup2 func 155 void cleanupSDL () { // cleanup func
157 debug logger.trace ("cleanup2: cleanupSDL() started"); 156 closeJoysticks();
158 SDL_Quit(); 157 SDL_Quit();
159 debug logger.trace ("cleanup2: cleanupSDL() finished");
160 } 158 }
161 159
162 /+ Load of info-printing stuff (currently doesn't have a use) 160 /+ Load of info-printing stuff (currently doesn't have a use)
163 // Print a load of info: 161 // Print a load of info:
164 logger.info ("Available video modes:"); 162 logger.info ("Available video modes:");