comparison trunk/tests/ChipmunkDemos/framework.d @ 16:af2f61a96318

ported chipmunk demos
author Extrawurst
date Sat, 04 Dec 2010 02:02:29 +0100
parents
children a376e5d3eb00
comparison
equal deleted inserted replaced
15:df4ebc8add66 16:af2f61a96318
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)
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
43 // Set the window title
44 SDL_WM_SetCaption(cast(char*)toStringz(_title), null);
45
46 int mode = SDL_OPENGL;
47
48 // Now open a SDL OpenGL window with the given parameters
49 if (SDL_SetVideoMode(_width, _height, 32, mode) is null)
50 {
51 throw new Exception("Failed to open SDL window");
52 }
53 }
54
55 alias void delegate(int,int,bool) MouseButton;
56 alias void delegate(int,int) MouseMove;
57 alias void delegate(int,bool) KeyEvent;
58 public bool processEvents(KeyEvent _keyevent,MouseMove _mmove,MouseButton _mbutton)
59 {
60 SDL_Event event;
61 while (SDL_PollEvent(&event))
62 {
63 switch (event.type)
64 {
65 case SDL_KEYUP:
66 case SDL_KEYDOWN:
67 _keyevent(event.key.keysym.sym,event.type == SDL_KEYDOWN);
68 break;
69
70 case SDL_MOUSEMOTION:
71 _mmove(event.motion.x,event.motion.y);
72 break;
73
74 case SDL_MOUSEBUTTONUP:
75 case SDL_MOUSEBUTTONDOWN:
76 _mbutton(event.button.x,event.button.y,event.type == SDL_MOUSEBUTTONDOWN);
77 break;
78
79 case SDL_QUIT:
80 return false;
81
82 default:
83 break;
84 }
85 }
86
87 return true;
88 }
89
90 void shutdown()
91 {
92 SDL_Quit();
93 }