b9d297e671900ed6c4736c63578601f5c72b72a8
[sdk] / samples / 3D / HiraganaCube / hiraganaCube.ec
1 #ifdef ECERE_STATIC
2 import static "ecere"
3 #else
4 import "ecere"
5 #endif
6
7 class CubeApp : GuiApplication
8 {
9    driver = "OpenGL";
10    timerResolution = 60;
11
12    bool Cycle(bool idle)
13    {
14       test3D.UpdateCube();
15       return true;
16    }
17 };
18
19 Camera camera
20 {
21    fixed,
22    position = { 0, 0, -200 }, 
23    orientation = Euler { 0, 0, 0 },
24    fov = 53;
25 };
26
27 Light light
28 {
29    specular = white;
30    orientation = Euler { pitch = 10, yaw = 30 };
31 };
32
33 Light light2
34 {
35    diffuse = white;
36    orientation = Euler { pitch = 20, yaw = -30 };
37 };
38
39
40 class Test3D : Window
41 {
42    text = "Cube";
43    moveable = true;
44    stayOnTop = true;
45    clientSize = { 400, 400 };
46    background = 0;
47    opacity = 0;
48    alphaBlend = true;
49
50    Bitmap textures[6];
51    Cube cube { };
52    Euler spin { };
53
54    bool UpdateCube()
55    {
56       static Time lastTime = 0;
57       Time time = GetTime(), diffTime = lastTime ? (time - lastTime) : 0;
58       if(spin.yaw || spin.pitch)
59       {
60          int signYaw = 1, signPitch = 1;
61          Radians yaw = spin.yaw, pitch = spin.pitch;
62          Quaternion orientation = cube.transform.orientation;
63          Euler tSpin { yaw * (double)diffTime, pitch * (double)diffTime, 0 };
64          Quaternion thisSpin = tSpin, temp;
65
66          if(yaw < 0) { yaw = -yaw; signYaw = -1; }
67          if(pitch < 0) { pitch = -pitch; signPitch = -1; }
68          yaw   -= (double)diffTime / 3 * yaw;
69          pitch -= (double)diffTime / 3 * pitch;
70          if(yaw < 0.0001) yaw = 0;
71          if(pitch < 0.0001) pitch = 0;
72
73          spin.yaw = yaw * signYaw;
74          spin.pitch = pitch * signPitch;
75
76          temp.Multiply(orientation, thisSpin);
77          orientation.Normalize(temp);
78          
79          cube.transform.orientation = orientation;
80          cube.UpdateTransform();
81          Update(null);
82       }
83       lastTime = time;
84       return true;
85    }
86
87    void OnUnloadGraphics()
88    {
89       int c;
90       PrimitiveGroup group;
91       for(group = cube.mesh.groups.first, c = 0; c<6; c++, group = group.next)
92       {
93          delete textures[c];
94          delete group.material;
95       }
96       
97    }
98    bool OnLoadGraphics()
99    {
100       char * hiragana[6] = { "あ", "い", "う", "え", "お", "ん" };
101       int c;
102       
103       PrimitiveGroup group;
104       Font font;
105       DisplaySystem lfbSystem { };
106
107       cube.Create(displaySystem);
108       lfbSystem.Create("LFB", null, false);
109       //font = lfbSystem.LoadFont("Arial Unicode MS", 180, 0);
110       //font = lfbSystem.LoadFont("MingLiu", 150, 0);
111       font = lfbSystem.LoadFont("FreeSans.ttf", 175, 0);
112       
113       for(group = cube.mesh.groups.first, c = 0; c<6; c++, group = group.next)
114       {
115          Surface surface;
116          int tw, th;
117          textures[c] = Bitmap { };
118          textures[c].Allocate(null, 256, 256, 0, pixelFormat888, false);
119          
120          surface = textures[c].GetSurface(0,0, null);
121          surface.TextFont(font);
122          surface.SetBackground(ColorAlpha { 255/*64*/, beige });
123          surface.Clear(colorBuffer);
124          surface.SetForeground(ColorAlpha { 255/*128*/, ColorHSV { 60 * c, 100, 50 }}); 
125          surface.TextExtent(hiragana[c], strlen(hiragana[c]), &tw, &th);
126          //surface.WriteText((256 - tw) / 2, (256 - th) / 2 /*+ 20*/, hiragana[c], strlen(hiragana[c]));
127          surface.WriteText(12,-45, hiragana[c], strlen(hiragana[c]));
128          surface.SetBackground({ 255 /*64*/, ColorHSV { (90 + 60 * c) % 360, 75, 50 }} );
129          surface.Area(5,5, 250,20);
130          surface.Area(5,5, 20,250);
131          surface.Area(235,5, 250,250);
132          surface.Area(5, 235, 250,250);
133
134          textures[c].MakeDD(displaySystem);
135
136          group.material = { opacity = 1, diffuse = white, ambient = white, flags = { doubleSided = true, translucent = true }, baseMap = textures[c] };
137          delete surface;
138       }
139       lfbSystem.UnloadFont(font);
140       delete lfbSystem;
141
142       cube.transform.scaling = { 100, 100, 100 };
143       cube.transform.position = { 0, 0, 0 };
144       cube.transform.orientation = Euler { 50, 50 };
145       cube.UpdateTransform();
146       return true;
147    }
148  
149    void OnResize(int w, int h)
150    {
151       camera.Setup(w, h, null);
152    }
153  
154    void OnRedraw(Surface surface)
155    {
156       surface.Clear(colorAndDepth);
157       camera.Update();
158       display.SetLight(0, light);
159       display.SetLight(1, light2);
160       display.SetCamera(surface, camera);
161       display.fogDensity = 0;
162       display.DrawObject(cube);
163       display.SetCamera(surface, null);
164    }
165
166    Point startClick;
167    bool moving;
168    Time clickTime;
169
170    bool OnLeftButtonDown(int x, int y, Modifiers mods)
171    {
172       clickTime = GetTime();
173       Capture();
174       startClick = { x, y };
175       moving = true;
176       return true;
177    }
178
179    bool OnLeftButtonUp(int x, int y, Modifiers mods)
180    {
181       if(moving)
182       {
183          ReleaseCapture();
184          moving = false;
185       }
186       return true;
187    }
188
189    bool OnMouseMove(int x, int y, Modifiers mods)
190    {
191       if(moving)
192       {
193          Time time = GetTime(), diffTime = Max(time - clickTime, 0.01);
194          spin.yaw   += Degrees { (x - startClick.x) / (25.0 * (double)diffTime) };
195          spin.pitch += Degrees { (startClick.y - y) / (25.0 * (double)diffTime) };
196          startClick = { x, y };
197          clickTime = time;
198       }
199       return true;
200    }
201
202    bool OnKeyHit(Key key, unichar ch)
203    {
204       if(key == wheelDown || key == wheelUp)
205       {
206          int w = size.w;
207          if(key == wheelDown)
208             w /= 1.1;
209          else if(key == wheelUp)
210             w *= 1.1;
211          Move(position.x + size.w / 2 - w / 2, position.y + size.h / 2 - w / 2, w, w);
212       }
213       Update(null);
214       return true;
215    }
216
217    bool OnKeyDown(Key key, unichar ch)
218    {
219       if(key == escape) Destroy(0);
220       return true;
221    }
222 }
223
224 Test3D test3D {};