comparison mde/sdl.d @ 32:316b0230a849

Lots more work on the GUI. Also renamed lots of files. Lots of changes to the GUI. Renderer is now used exclusively for rendering and WidgetDecoration is gone. Renamed lots of files to conform to case policies. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 30 Apr 2008 18:05:56 +0100
parents
children 6886402c1545
comparison
equal deleted inserted replaced
31:baa87e68d7dc 32:316b0230a849
1 /* LICENSE BLOCK
2 Part of mde: a Modular D game-oriented Engine
3 Copyright © 2007-2008 Diggory Hardy
4
5 This program is free software: you can redistribute it and/or modify it under the terms
6 of the GNU General Public License as published by the Free Software Foundation, either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
15
16 /** Just a temporary place to put SDL Init and Video stuff.
17 */
18 module mde.SDL;
19
20 import mde.scheduler.initFunctions;
21 import mde.input.joystick;
22 import mde.Options;
23 import mde.gl.basic;
24 import global = mde.global;
25
26 import tango.util.log.Log : Log, Logger;
27 import tango.stdc.stringz;
28
29 import derelict.sdl.sdl;
30
31 private Logger logger;
32 static this() {
33 logger = Log.getLogger ("mde.SDL");
34
35 init.addFunc (&initSdlAndGl, "initSdlAndGl");
36 }
37
38 private uint flags = 0;
39
40 void initSdlAndGl() { // init func
41 // Initialise SDL
42 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK /+| SDL_INIT_EVENTTHREAD+/)) {
43 logger.fatal ("SDL initialisation failed:");
44 char* msg = SDL_GetError ();
45 logger.fatal (msg ? fromStringz(msg) : "no reason available");
46
47 setInitFailure ();
48 return;
49 }
50
51 debug logger.trace ("SDL initialised");
52
53 // Must be called after SDL has been initialised, so cannot be a separate Init function.
54 openJoysticks (); // after SDL init
55 cleanup.addFunc (&cleanupSDL, "cleanupSDL");
56
57 setupWindow();
58 }
59
60 void setupWindow() { // indirect init func (depends on initSdlAndGl)
61 // Window creation flags and size
62 flags = SDL_OPENGL;
63 if (vidOpts.hardware) flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
64 else flags |= SDL_SWSURFACE;
65 int w, h;
66 if (vidOpts.fullscreen) {
67 flags |= SDL_FULLSCREEN;
68 w = vidOpts.screenW;
69 h = vidOpts.screenH;
70 }
71 else {
72 if (vidOpts.resizable) flags |= SDL_RESIZABLE;
73 if (vidOpts.noFrame) flags |= SDL_NOFRAME;
74 w = vidOpts.windowW;
75 h = vidOpts.windowH;
76 }
77
78 // OpenGL attributes
79 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
80 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
81 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
82 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
83 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
84
85 // Open a window
86 if (SDL_SetVideoMode (w, h, 32, flags) is null) {
87 logger.fatal ("Unable to set video mode:");
88 char* msg = SDL_GetError ();
89 logger.fatal (msg ? fromStringz(msg) : "no reason available");
90
91 setInitFailure ();
92 return;
93 }
94
95 // Now (must be done after GL context is created) try to load later version:
96 /+ No later GL features are currently used.
97 try {
98 DerelictGL.loadVersions(GLVersion.Version21);
99 } catch (DerelictException de) {
100 logger.fatal ("Loading OpenGL version > 1.1 failed:");
101 logger.fatal (de.msg);
102
103 setInitFailure ();
104 return;
105 }
106 +/
107
108 // OpenGL stuff:
109 glSetup();
110 setProjection (w, h);
111
112 // Window-manager settings
113 SDL_WM_SetCaption (toStringz ("mde"), null);
114 // SDL_WM_GrabInput (use later)
115 }
116
117 void resizeWindow (int w, int h) {
118 if (vidOpts.fullscreen) {
119 Options.setInt ("video", "screenW", w);
120 Options.setInt ("video", "screenH", h);
121 } else {
122 Options.setInt ("video", "windowW", w);
123 Options.setInt ("video", "windowH", h);
124 }
125
126 if (SDL_SetVideoMode (w, h, 32, flags) is null) {
127 logger.fatal ("Unable to reset video mode:");
128 char* msg = SDL_GetError ();
129 logger.fatal (msg ? fromStringz(msg) : "no reason available");
130
131 global.run = false;
132 }
133
134 // Reset the projection and viewport
135 setProjection (w, h);
136 }
137
138 void cleanupSDL () { // cleanup func
139 closeJoysticks();
140 SDL_Quit();
141 }
142
143 /+ Load of info-printing stuff (currently doesn't have a use)
144 // Print a load of info:
145 logger.info ("Available video modes:");
146 char[128] tmp;
147 SDL_Rect** modes = SDL_ListModes (null, SDL_FULLSCREEN);
148 if (modes is null) logger.info ("None!");
149 else if (modes is cast(SDL_Rect**) -1) logger.info ("All modes are available");
150 else {
151 for (uint i = 0; modes[i] !is null; ++i) {
152 logger.info (logger.format (tmp, "\t{}x{}", modes[i].w, modes[i].h));
153 }
154 }
155
156 SDL_VideoInfo* vi = SDL_GetVideoInfo ();
157 if (vi !is null) {
158 logger.info ("Video info:");
159 logger.info ("Hardware surface support: "~ (vi.flags & SDL_HWSURFACE ? "yes" : "no"));
160 logger.info (logger.format (tmp, "Video memory: {}", vi.video_mem));
161
162 if (vi.vfmt !is null) {
163 logger.info ("Best video mode:");
164 logger.info (logger.format (tmp, "Bits per pixel: {}", vi.vfmt.BitsPerPixel));
165 }
166 }
167 +/
168
169
170 /** All video options. */
171 OptionsVideo vidOpts;
172 class OptionsVideo : Options {
173 alias store!("fullscreen","hardware","resizable","noFrame") BOOL;
174 alias store!("screenW","screenH","windowW","windowH") INT;
175 //alias store!() CHARA;
176
177 mixin (decBool!(BOOL.a));
178 mixin (decInt!(INT.a));
179 //mixin (decCharA!(CHARA.a));
180
181 this () {
182 mixin (aaBool!(BOOL.a));
183 mixin (aaInt!(INT.a));
184 //mixin (aaCharA!(CHARA.a));
185 }
186
187 static this() {
188 vidOpts = new OptionsVideo;
189 Options.addOptionsClass (vidOpts, "video");
190 }
191 }