annotate window/system.cc @ 9:b6d6c0ffb423

Fixed some memory leaks
author thib
date Tue, 05 Aug 2008 13:24:20 +0000
parents 223b71206888
children 15a18fbe6f21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2 * Copyright (c) 2004-2006 Kazunori "jagarl" Ueno
223b71206888 Initial import
thib
parents:
diff changeset
3 * All rights reserved.
223b71206888 Initial import
thib
parents:
diff changeset
4 *
223b71206888 Initial import
thib
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
223b71206888 Initial import
thib
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
223b71206888 Initial import
thib
parents:
diff changeset
7 * are met:
223b71206888 Initial import
thib
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
223b71206888 Initial import
thib
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
223b71206888 Initial import
thib
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
223b71206888 Initial import
thib
parents:
diff changeset
13 * 3. The name of the author may not be used to endorse or promote products
223b71206888 Initial import
thib
parents:
diff changeset
14 * derived from this software without specific prior written permission.
223b71206888 Initial import
thib
parents:
diff changeset
15 *
223b71206888 Initial import
thib
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
223b71206888 Initial import
thib
parents:
diff changeset
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223b71206888 Initial import
thib
parents:
diff changeset
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223b71206888 Initial import
thib
parents:
diff changeset
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223b71206888 Initial import
thib
parents:
diff changeset
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223b71206888 Initial import
thib
parents:
diff changeset
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223b71206888 Initial import
thib
parents:
diff changeset
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223b71206888 Initial import
thib
parents:
diff changeset
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223b71206888 Initial import
thib
parents:
diff changeset
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223b71206888 Initial import
thib
parents:
diff changeset
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223b71206888 Initial import
thib
parents:
diff changeset
26 */
223b71206888 Initial import
thib
parents:
diff changeset
27
223b71206888 Initial import
thib
parents:
diff changeset
28 #include<SDL.h>
223b71206888 Initial import
thib
parents:
diff changeset
29 #include"system.h"
223b71206888 Initial import
thib
parents:
diff changeset
30 #include<iostream>
223b71206888 Initial import
thib
parents:
diff changeset
31 #include<stdio.h>
223b71206888 Initial import
thib
parents:
diff changeset
32
223b71206888 Initial import
thib
parents:
diff changeset
33 using namespace std;
223b71206888 Initial import
thib
parents:
diff changeset
34
223b71206888 Initial import
thib
parents:
diff changeset
35 // void SDL_SetEventFilter(SDL_EventFilter filter);
223b71206888 Initial import
thib
parents:
diff changeset
36 // typedef int (*SDL_EventFilter)(const SDL_Event *event);
223b71206888 Initial import
thib
parents:
diff changeset
37 namespace System {
223b71206888 Initial import
thib
parents:
diff changeset
38
223b71206888 Initial import
thib
parents:
diff changeset
39 Main* Main::instance = 0;
223b71206888 Initial import
thib
parents:
diff changeset
40
223b71206888 Initial import
thib
parents:
diff changeset
41 int Main::event_filter(const SDL_Event* event) {
223b71206888 Initial import
thib
parents:
diff changeset
42 return 1; /* throw all event */
223b71206888 Initial import
thib
parents:
diff changeset
43 }
223b71206888 Initial import
thib
parents:
diff changeset
44
223b71206888 Initial import
thib
parents:
diff changeset
45 Main::Main(void) {
223b71206888 Initial import
thib
parents:
diff changeset
46 instance = this;
223b71206888 Initial import
thib
parents:
diff changeset
47 framerate = 20;
223b71206888 Initial import
thib
parents:
diff changeset
48 cursor = 0;
223b71206888 Initial import
thib
parents:
diff changeset
49 }
223b71206888 Initial import
thib
parents:
diff changeset
50 Main::~Main() {
223b71206888 Initial import
thib
parents:
diff changeset
51 if (cursor) delete cursor;
223b71206888 Initial import
thib
parents:
diff changeset
52 }
223b71206888 Initial import
thib
parents:
diff changeset
53 void Main::Quit(void) {
223b71206888 Initial import
thib
parents:
diff changeset
54 is_exit = true;
223b71206888 Initial import
thib
parents:
diff changeset
55 }
223b71206888 Initial import
thib
parents:
diff changeset
56 void Main::EnableVideo(void) {
223b71206888 Initial import
thib
parents:
diff changeset
57 is_video_update = true;
223b71206888 Initial import
thib
parents:
diff changeset
58 }
223b71206888 Initial import
thib
parents:
diff changeset
59 void Main::DisableVideo(void) {
223b71206888 Initial import
thib
parents:
diff changeset
60 is_video_update = false;
223b71206888 Initial import
thib
parents:
diff changeset
61 }
223b71206888 Initial import
thib
parents:
diff changeset
62 bool Main::is_exit = false;
223b71206888 Initial import
thib
parents:
diff changeset
63 bool Main::is_video_update = true;
223b71206888 Initial import
thib
parents:
diff changeset
64
223b71206888 Initial import
thib
parents:
diff changeset
65 void Main::Mainloop(void) {
223b71206888 Initial import
thib
parents:
diff changeset
66 SDL_SetEventFilter(&event_filter);
223b71206888 Initial import
thib
parents:
diff changeset
67 Uint32 old_time = 0;
223b71206888 Initial import
thib
parents:
diff changeset
68 while(!is_exit) {
223b71206888 Initial import
thib
parents:
diff changeset
69 Uint32 start_time = SDL_GetTicks();
223b71206888 Initial import
thib
parents:
diff changeset
70 if (! event.Exec(start_time)) break;
223b71206888 Initial import
thib
parents:
diff changeset
71 if (start_time - old_time > 1000/framerate) {
223b71206888 Initial import
thib
parents:
diff changeset
72 if (is_video_update) root.ExecUpdate();
223b71206888 Initial import
thib
parents:
diff changeset
73 event.Exec(Event::Time::FRAME_UPDATE);
223b71206888 Initial import
thib
parents:
diff changeset
74 cout.flush();
223b71206888 Initial import
thib
parents:
diff changeset
75 old_time = start_time;
223b71206888 Initial import
thib
parents:
diff changeset
76 }
223b71206888 Initial import
thib
parents:
diff changeset
77
223b71206888 Initial import
thib
parents:
diff changeset
78 // 問題:
223b71206888 Initial import
thib
parents:
diff changeset
79 // z 軸と xy 軸の相互干渉;高速化
223b71206888 Initial import
thib
parents:
diff changeset
80 // 移動するウィジット描画の高速化
223b71206888 Initial import
thib
parents:
diff changeset
81 // キャッシュ
223b71206888 Initial import
thib
parents:
diff changeset
82 // 文字列の一部のみ更新の高速化
223b71206888 Initial import
thib
parents:
diff changeset
83 // 「階層 z で x なる領域無効化、y なる領域生成」で良い?>Expose
223b71206888 Initial import
thib
parents:
diff changeset
84 /*
223b71206888 Initial import
thib
parents:
diff changeset
85 Uint32 end_time = SDL_GetTicks();
223b71206888 Initial import
thib
parents:
diff changeset
86 Uint32 delay = (end_time-start_time);
223b71206888 Initial import
thib
parents:
diff changeset
87 if(delay < 1000/framerate) SDL_Delay(1000/framerate - delay);
223b71206888 Initial import
thib
parents:
diff changeset
88 else SDL_Delay(0);
223b71206888 Initial import
thib
parents:
diff changeset
89 */
223b71206888 Initial import
thib
parents:
diff changeset
90 SDL_Delay(0);
223b71206888 Initial import
thib
parents:
diff changeset
91 };
223b71206888 Initial import
thib
parents:
diff changeset
92 }
223b71206888 Initial import
thib
parents:
diff changeset
93
223b71206888 Initial import
thib
parents:
diff changeset
94 void Main::SetCursor(Surface* s, const Rect& r) {
223b71206888 Initial import
thib
parents:
diff changeset
95 if (instance == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
96 if (instance->cursor) delete instance->cursor;
223b71206888 Initial import
thib
parents:
diff changeset
97 if (s == 0) { // カーソル消去
223b71206888 Initial import
thib
parents:
diff changeset
98 instance->cursor = 0;
223b71206888 Initial import
thib
parents:
diff changeset
99 } else if (s == DEFAULT_MOUSECURSOR) {
223b71206888 Initial import
thib
parents:
diff changeset
100 instance->cursor = 0;
223b71206888 Initial import
thib
parents:
diff changeset
101 SDL_ShowCursor(SDL_ENABLE);
223b71206888 Initial import
thib
parents:
diff changeset
102 } else {
223b71206888 Initial import
thib
parents:
diff changeset
103 instance->cursor = new WidMouseCursor(instance->event, instance->root.root, s, r.lx, r.ty, r.width(), r.height());
223b71206888 Initial import
thib
parents:
diff changeset
104 instance->cursor->show();
223b71206888 Initial import
thib
parents:
diff changeset
105 SDL_ShowCursor(SDL_DISABLE);
223b71206888 Initial import
thib
parents:
diff changeset
106 }
223b71206888 Initial import
thib
parents:
diff changeset
107 }
223b71206888 Initial import
thib
parents:
diff changeset
108 }