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 
17 void startup(string _title,int _width,int _height,bool useVsync=true)
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);
42     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, cast(int)useVsync);
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     /** Enable anti-aliasing by default. */
56     //~ glEnable(GL_LINE_SMOOTH);
57     //~ glEnable(GL_POINT_SMOOTH);
58     //~ glEnable(GL_BLEND);
59     //~ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
60     //~ glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
61     //~ glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
62 }
63 
64 alias void delegate(int,int,bool) MouseButton;
65 alias void delegate(int,int) MouseMove;
66 alias void delegate(int,bool) KeyEvent;
67 public bool processEvents(KeyEvent _keyevent,MouseMove _mmove,MouseButton _mbutton)
68 {
69     SDL_Event event;
70     while (SDL_PollEvent(&event))
71     {
72         switch (event.type)
73         {
74             case SDL_KEYUP:
75             case SDL_KEYDOWN:
76                 _keyevent(event.key.keysym.sym,event.type == SDL_KEYDOWN);
77                 break;
78 
79             case SDL_MOUSEMOTION:
80                 _mmove(event.motion.x,event.motion.y);
81                 break;
82 
83             case SDL_MOUSEBUTTONUP:
84             case SDL_MOUSEBUTTONDOWN:
85                 _mbutton(event.button.x,event.button.y,event.type == SDL_MOUSEBUTTONDOWN);
86                 break;
87 
88             case SDL_QUIT:
89                 return false;
90 
91             default:
92                 break;
93         }
94     }
95 
96     return true;
97 }
98 
99 void shutdown()
100 {
101     SDL_Quit();
102 }
103 
104 version (Windows)
105 {
106     import std.c.windows.windows;
107 
108     static long winfrequ;
109 
110     static this()
111     {
112         QueryPerformanceFrequency(&winfrequ);
113     }
114 
115     ulong tickCount(){
116         long ret;
117         QueryPerformanceCounter(&ret);
118         return (cast(ulong)(cast(float)ret / winfrequ * 1000));
119     }
120 }
121 else version (linux)
122 {
123     import std.c.linux.linux;
124 
125     ulong tickCount(){
126 
127         timeval val;
128         gettimeofday(&val,null);
129 
130         //return time(null);
131         return val.tv_usec/1000;
132     }
133 }
134 else version (OSX)
135 {
136     import std.date;
137 
138     ulong tickCount(){
139 
140          return cast(ulong)getUTCtime();
141     }
142 }