16
|
1
|
|
2 // written in the D programming language
|
|
3
|
|
4 /++
|
|
5 + Authors: Stephan Dilly, www.extrawurst.org
|
|
6 +/
|
|
7
|
|
8 module framework;
|
|
9
|
|
10 import derelict.sdl.sdl;
|
|
11 import derelict.opengl.gl;
|
|
12 import derelict.opengl.glu;
|
|
13
|
|
14 import std.string;
|
|
15 import std.stdio;
|
|
16
|
18
|
17 void startup(string _title,int _width,int _height,bool _disableVsync=true)
|
16
|
18 {
|
|
19 DerelictGL.load();
|
|
20 DerelictGLU.load();
|
|
21 DerelictSDL.load();
|
|
22
|
|
23 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
|
|
24 {
|
|
25 throw new Exception("Failed to initialize SDL");
|
|
26 }
|
|
27
|
|
28 // Enable key repeating
|
|
29 if ((SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL)))
|
|
30 {
|
|
31 throw new Exception("Failed to set key repeat");
|
|
32 }
|
|
33
|
|
34 //enable to get ascii/unicode info of key event
|
|
35 SDL_EnableUNICODE(1);
|
|
36
|
|
37 // Set the OpenGL attributes
|
|
38 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
|
|
39 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
|
|
40 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
|
|
41 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
18
|
42 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,_disableVsync?0:1);
|
16
|
43
|
|
44 // Set the window title
|
|
45 SDL_WM_SetCaption(cast(char*)toStringz(_title), null);
|
|
46
|
|
47 int mode = SDL_OPENGL;
|
|
48
|
|
49 // Now open a SDL OpenGL window with the given parameters
|
|
50 if (SDL_SetVideoMode(_width, _height, 32, mode) is null)
|
|
51 {
|
|
52 throw new Exception("Failed to open SDL window");
|
|
53 }
|
|
54 }
|
|
55
|
|
56 alias void delegate(int,int,bool) MouseButton;
|
|
57 alias void delegate(int,int) MouseMove;
|
|
58 alias void delegate(int,bool) KeyEvent;
|
|
59 public bool processEvents(KeyEvent _keyevent,MouseMove _mmove,MouseButton _mbutton)
|
|
60 {
|
|
61 SDL_Event event;
|
|
62 while (SDL_PollEvent(&event))
|
|
63 {
|
|
64 switch (event.type)
|
|
65 {
|
|
66 case SDL_KEYUP:
|
|
67 case SDL_KEYDOWN:
|
|
68 _keyevent(event.key.keysym.sym,event.type == SDL_KEYDOWN);
|
|
69 break;
|
|
70
|
|
71 case SDL_MOUSEMOTION:
|
|
72 _mmove(event.motion.x,event.motion.y);
|
|
73 break;
|
|
74
|
|
75 case SDL_MOUSEBUTTONUP:
|
|
76 case SDL_MOUSEBUTTONDOWN:
|
|
77 _mbutton(event.button.x,event.button.y,event.type == SDL_MOUSEBUTTONDOWN);
|
|
78 break;
|
|
79
|
|
80 case SDL_QUIT:
|
|
81 return false;
|
|
82
|
|
83 default:
|
|
84 break;
|
|
85 }
|
|
86 }
|
|
87
|
|
88 return true;
|
|
89 }
|
|
90
|
|
91 void shutdown()
|
|
92 {
|
|
93 SDL_Quit();
|
|
94 } |