1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | use utf8; |
4 | use utf8; |
5 | |
5 | |
6 | use Glib; |
6 | use Time::HiRes 'time'; |
7 | use Gtk2 -init; |
7 | use Event; |
8 | |
8 | |
9 | use SDL; |
9 | use SDL; |
10 | use SDL::App; |
10 | use SDL::App; |
11 | use SDL::Event; |
11 | use SDL::Event; |
12 | use SDL::Surface; |
12 | use SDL::Surface; |
13 | use SDL::OpenGL; |
13 | use SDL::OpenGL; |
14 | use SDL::OpenGL::Constants; |
|
|
15 | |
14 | |
16 | use Crossfire; |
15 | use Crossfire; |
17 | use Crossfire::Protocol; |
16 | use Crossfire::Protocol; |
18 | |
17 | |
19 | use CFClient; |
18 | use CFClient; |
20 | use CFClient::Widget; |
19 | use CFClient::UI; |
21 | |
20 | |
22 | our $VERSION = '0.1'; |
21 | our $VERSION = '0.1'; |
23 | |
22 | |
24 | my $MAX_FPS = 30; |
23 | my $MAX_FPS = 60; |
25 | my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame |
24 | my $MIN_FPS = 5; # unused as of yet |
26 | |
25 | |
27 | our $FACECACHE; |
26 | our $FACECACHE; |
|
|
27 | |
|
|
28 | our $LAST_REFRESH; |
|
|
29 | our $NOW; |
28 | |
30 | |
29 | our $CFG; |
31 | our $CFG; |
30 | our $CONN; |
32 | our $CONN; |
|
|
33 | our $FAST; # fast, low-quality mode, possibly useful for software-rendering |
31 | |
34 | |
|
|
35 | our @SDL_MODES; |
32 | our $WIDTH; |
36 | our $WIDTH; |
33 | our $HEIGHT; |
37 | our $HEIGHT; |
34 | our $FULLSCREEN; |
38 | our $FULLSCREEN; |
35 | |
39 | |
36 | our $NOW; |
40 | our $MAP; |
37 | |
|
|
38 | our $MAPWIDGET; |
41 | our $MAPWIDGET; |
39 | our $FONTSIZE; |
42 | our $FONTSIZE; |
40 | |
43 | |
41 | our $SDL_TIMER; |
44 | our $SDL_ACTIVE; |
42 | our $SDL_APP; |
|
|
43 | our $SDL_EV; |
45 | our $SDL_EV; |
44 | our %SDL_CB; |
46 | our %SDL_CB; |
45 | |
47 | |
46 | our $ALT_ENTER_MESSAGE; |
48 | our $ALT_ENTER_MESSAGE; |
47 | our $STATUS_LINE; |
49 | our $STATUS_LINE; |
48 | our $DEBUG_STATUS; |
50 | our $DEBUG_STATUS; |
49 | |
51 | |
50 | my $last_refresh; |
52 | sub status { |
51 | my %ANIMATE; |
53 | $STATUS_LINE->set_text ($_[0]); |
52 | my $refresh_handler; |
54 | my ($w, $h) = $STATUS_LINE->size_request; |
|
|
55 | $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h); |
|
|
56 | } |
53 | |
57 | |
54 | our ($tw, $te); # Test widget #d# |
58 | sub debug { |
|
|
59 | $DEBUG_STATUS->set_text ($_[0]); |
|
|
60 | my ($w, $h) = $DEBUG_STATUS->size_request; |
|
|
61 | $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); |
|
|
62 | } |
55 | |
63 | |
56 | sub init_screen { |
64 | sub start_game { |
57 | $SDL_APP = new SDL::App |
65 | status "logging in..."; |
58 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
|
|
59 | -title => "Crossfire+ Client", |
|
|
60 | -width => $WIDTH, |
|
|
61 | -height => $HEIGHT, |
|
|
62 | -opengl => 1, |
|
|
63 | -red_size => 5, |
|
|
64 | -green_size => 5, |
|
|
65 | -blue_size => 5, |
|
|
66 | -alpha_size => 0, |
|
|
67 | -double_buffer => 1, |
|
|
68 | -fullscreen => $FULLSCREEN, |
|
|
69 | -resizeable => 0; |
|
|
70 | |
66 | |
71 | $SDL_EV = new SDL::Event; |
67 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
72 | $SDL_EV->set_unicode (1); |
|
|
73 | |
68 | |
74 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
69 | $MAP = new CFClient::Map $mapsize, $mapsize; |
75 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
76 | while $SDL_EV->poll; |
|
|
77 | |
|
|
78 | 1 |
|
|
79 | }; |
|
|
80 | |
|
|
81 | $last_refresh = SDL::GetTicks; |
|
|
82 | |
|
|
83 | CFClient::gl_init; |
|
|
84 | |
|
|
85 | $FONTSIZE = int $HEIGHT / 50; |
|
|
86 | |
|
|
87 | ############################################################################# |
|
|
88 | |
|
|
89 | glClearColor 0.45, 0.45, 0.45, 1; |
|
|
90 | |
|
|
91 | glEnable GL_TEXTURE_2D; |
|
|
92 | glEnable GL_COLOR_MATERIAL; |
|
|
93 | glShadeModel GL_FLAT; |
|
|
94 | glDisable GL_DEPTH_TEST; |
|
|
95 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
96 | |
|
|
97 | ############################################################################# |
|
|
98 | |
|
|
99 | $DEBUG_STATUS = new CFClient::Widget::Label; |
|
|
100 | $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS); |
|
|
101 | |
70 | |
102 | $STATUS_LINE = new CFClient::Widget::Label |
|
|
103 | y => $HEIGHT * 59 / 60 - $FONTSIZE; |
|
|
104 | $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE); |
|
|
105 | |
|
|
106 | $ALT_ENTER_MESSAGE = new CFClient::Widget::Label |
|
|
107 | y => $HEIGHT * 59 / 60, |
|
|
108 | height => $HEIGHT / 60, |
|
|
109 | text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
|
|
110 | $CFClient::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
111 | |
|
|
112 | $MAPWIDGET = new CFClient::Widget::MapWidget; |
|
|
113 | $CFClient::Widget::TOPLEVEL->add ($MAPWIDGET); |
|
|
114 | $MAPWIDGET->focus_in; |
|
|
115 | |
|
|
116 | # Test code #d# |
|
|
117 | unless ($tw) { # haha... |
|
|
118 | my $w = new CFClient::Widget::Window |
|
|
119 | w => 300, h => 300, |
|
|
120 | child => (my $frame = new CFClient::Widget::FancyFrame); |
|
|
121 | $CFClient::Widget::TOPLEVEL->add ($w); |
|
|
122 | |
|
|
123 | $frame->add (my $vbox = new CFClient::Widget::VBox); |
|
|
124 | |
|
|
125 | $vbox->add (new CFClient::Widget::Entry text => "hallo"); |
|
|
126 | $vbox->add (new CFClient::Widget::Slider); |
|
|
127 | |
|
|
128 | $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 600, h => 300; |
|
|
129 | my $lbl1 = new CFClient::Widget::Label text => "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
|
|
130 | my $lbl2 = new CFClient::Widget::Label text => "LBL2"; |
|
|
131 | my $vb = new CFClient::Widget::VBox; |
|
|
132 | my $f = new CFClient::Widget::FancyFrame; |
|
|
133 | my $f2 = new CFClient::Widget::FancyFrame; |
|
|
134 | $f->add ($lbl1); |
|
|
135 | $f2->add ($lbl2); |
|
|
136 | $vb->add ($f); |
|
|
137 | $vb->add ($f2, 1); |
|
|
138 | |
|
|
139 | $tw->add ($vb); |
|
|
140 | $tw->moveto (0, 0); |
|
|
141 | $CFClient::Widget::TOPLEVEL->add ($tw); |
|
|
142 | |
|
|
143 | # $f->move ($WIDTH - 200, 0); |
|
|
144 | # $CFClient::Widget::TOPLEVEL->add ($f); |
|
|
145 | } |
|
|
146 | } |
|
|
147 | |
|
|
148 | sub destroy_screen { |
|
|
149 | remove Glib::Source $SDL_TIMER; |
|
|
150 | undef $SDL_APP; |
|
|
151 | undef $SDL_EV; |
|
|
152 | SDL::Quit; |
|
|
153 | } |
|
|
154 | |
|
|
155 | sub start_game { |
|
|
156 | $WIDTH = $CFG->{width}; |
|
|
157 | $HEIGHT = $CFG->{height}; |
|
|
158 | $FULLSCREEN = 0; |
|
|
159 | |
|
|
160 | init_screen; |
|
|
161 | |
|
|
162 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
|
|
163 | |
|
|
164 | $CONN = new conn |
71 | $CONN = new conn |
165 | host => $CFG->{host}, |
72 | host => $CFG->{host}, |
166 | port => $CFG->{port}, |
73 | port => $CFG->{port}, |
167 | user => $CFG->{user}, |
74 | user => $CFG->{user}, |
168 | pass => $CFG->{password}, |
75 | pass => $CFG->{password}, |
169 | mapw => $mapsize, |
76 | mapw => $mapsize, |
170 | maph => $mapsize, |
77 | maph => $mapsize, |
171 | ; |
78 | ; |
172 | |
79 | |
|
|
80 | status "login successful"; |
|
|
81 | |
173 | CFClient::lowdelay fileno $CONN->{fh}; |
82 | CFClient::lowdelay fileno $CONN->{fh}; |
174 | } |
83 | } |
175 | |
84 | |
176 | sub stop_game { |
85 | sub stop_game { |
177 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
178 | undef $refresh_handler; |
|
|
179 | |
|
|
180 | undef $CONN; |
86 | undef $CONN; |
|
|
87 | } |
|
|
88 | |
|
|
89 | sub config_dialog { |
|
|
90 | my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100, |
|
|
91 | child => (my $vbox = new CFClient::UI::VBox); |
|
|
92 | $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup"); |
|
|
93 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
|
|
94 | |
|
|
95 | $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode"); |
|
|
96 | $table->add (1, 0, my $hbox = new CFClient::UI::HBox); |
|
|
97 | |
|
|
98 | $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]); |
|
|
99 | $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8); |
|
|
100 | |
|
|
101 | $mode_slider->connect (changed => sub { |
|
|
102 | my ($self, $value) = @_; |
|
|
103 | |
|
|
104 | $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value; |
|
|
105 | $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]}); |
|
|
106 | }); |
|
|
107 | $mode_slider->emit (changed => $mode_slider->{range}[0]); |
|
|
108 | |
|
|
109 | $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Fullscreen"); |
|
|
110 | $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub { |
|
|
111 | my ($self, $value) = @_; |
|
|
112 | $CFG->{fullscreen} = $value; |
|
|
113 | }); |
|
|
114 | |
|
|
115 | $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Fast & Ugly"); |
|
|
116 | $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub { |
|
|
117 | my ($self, $value) = @_; |
|
|
118 | $CFG->{fast} = $value; |
|
|
119 | }); |
|
|
120 | |
|
|
121 | $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Fog of War"); |
|
|
122 | $table->add (1, 3, new CFClient::UI::CheckBox state => $CFG->{fow_enable}, connect_changed => sub { |
|
|
123 | my ($self, $value) = @_; |
|
|
124 | $CFG->{fow_enable} = $value; |
|
|
125 | }); |
|
|
126 | |
|
|
127 | $table->add (0, 4, new CFClient::UI::Label align => 1, text => "FoW Intensity"); |
|
|
128 | $table->add (1, 4, new CFClient::UI::Slider range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001], connect_changed => sub { |
|
|
129 | my ($self, $value) = @_; |
|
|
130 | $CFG->{fow_intensity} = $value; |
|
|
131 | }); |
|
|
132 | |
|
|
133 | $table->add (0, 5, new CFClient::UI::Label align => 1, text => "FoW Smooth"); |
|
|
134 | $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub { |
|
|
135 | my ($self, $value) = @_; |
|
|
136 | $CFG->{fow_smooth} = $value; |
|
|
137 | }); |
|
|
138 | |
|
|
139 | $table->add (1, 6, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { |
181 | destroy_screen; |
140 | destroy_screen (); |
|
|
141 | init_screen (); |
|
|
142 | }); |
|
|
143 | |
|
|
144 | $vbox->add (new CFClient::UI::Label align => 0, text => "Server Setup"); |
|
|
145 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
|
|
146 | $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host"); |
|
|
147 | $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host}); |
|
|
148 | |
|
|
149 | $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port"); |
|
|
150 | $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port}); |
|
|
151 | |
|
|
152 | $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username"); |
|
|
153 | $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user}); |
|
|
154 | |
|
|
155 | $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password"); |
|
|
156 | $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1); |
|
|
157 | |
|
|
158 | $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size"); |
|
|
159 | $table->add (1, 6, new CFClient::UI::Slider |
|
|
160 | req_w => 100, |
|
|
161 | range => [$CFG->{mapsize}, 10, 100 + 1, 1], |
|
|
162 | connect_changed => sub { |
|
|
163 | my ($self, $value) = @_; |
|
|
164 | |
|
|
165 | $CFG->{mapsize} = $self->{range}[0] = $value = int $value; |
|
|
166 | }, |
|
|
167 | ); |
|
|
168 | |
|
|
169 | $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub { |
|
|
170 | start_game; |
|
|
171 | }); |
|
|
172 | |
|
|
173 | $vbox->add (my $hbox = new CFClient::UI::HBox); |
|
|
174 | |
|
|
175 | $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub { |
|
|
176 | CFClient::write_cfg "$Crossfire::VARDIR/pclientrc"; |
|
|
177 | status "Configuration Saved"; |
|
|
178 | }); |
|
|
179 | $CFClient::UI::TOPLEVEL->add ($dialog); |
182 | } |
180 | } |
|
|
181 | |
|
|
182 | sub sdl_init { |
|
|
183 | SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE |
|
|
184 | and die "SDL::Init failed!\n"; |
|
|
185 | } |
|
|
186 | |
|
|
187 | sub init_screen { |
|
|
188 | sdl_init; |
|
|
189 | |
|
|
190 | ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; |
|
|
191 | $FULLSCREEN = $CFG->{fullscreen}; |
|
|
192 | $FAST = $CFG->{fast}; |
|
|
193 | |
|
|
194 | SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; |
|
|
195 | SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5; |
|
|
196 | SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5; |
|
|
197 | SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0; |
|
|
198 | |
|
|
199 | SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0; |
|
|
200 | SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0; |
|
|
201 | SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0; |
|
|
202 | SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0; |
|
|
203 | |
|
|
204 | SDL::GLSetAttribute SDL_GL_DOUBLEBUFFER, 1; |
|
|
205 | SDL::GLSetAttribute SDL_GL_BUFFER_SIZE, 15; |
|
|
206 | SDL::GLSetAttribute SDL_GL_DEPTH_SIZE, 0; |
|
|
207 | |
|
|
208 | SDL::SetVideoMode $WIDTH, $HEIGHT, 0, |
|
|
209 | SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL | SDL_DOUBLEBUF |
|
|
210 | | ($FULLSCREEN ? SDL_FULLSCREEN : 0) |
|
|
211 | or die "SDL::SetVideoMode failed!\n"; |
|
|
212 | |
|
|
213 | SDL::WMSetCaption "Crossfire+ Client", "Crossfire+"; |
|
|
214 | |
|
|
215 | $SDL_EV = new SDL::Event; |
|
|
216 | $SDL_EV->set_unicode (1); |
|
|
217 | |
|
|
218 | $SDL_ACTIVE = 1; |
|
|
219 | |
|
|
220 | $LAST_REFRESH = time - 0.01; |
|
|
221 | |
|
|
222 | CFClient::gl_init; |
|
|
223 | |
|
|
224 | $FONTSIZE = int $HEIGHT / 40; |
|
|
225 | |
|
|
226 | ############################################################################# |
|
|
227 | |
|
|
228 | $DEBUG_STATUS = new CFClient::UI::Label padding => 0; |
|
|
229 | $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS); |
|
|
230 | |
|
|
231 | $STATUS_LINE = new CFClient::UI::Label |
|
|
232 | padding => 0, |
|
|
233 | y => $HEIGHT * 49 / 50 - $FONTSIZE; |
|
|
234 | $CFClient::UI::TOPLEVEL->add ($STATUS_LINE); |
|
|
235 | |
|
|
236 | $ALT_ENTER_MESSAGE = new CFClient::UI::Label |
|
|
237 | padding => 0, |
|
|
238 | y => $HEIGHT * 49 / 50, |
|
|
239 | height => $HEIGHT / 50, |
|
|
240 | text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
|
|
241 | $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
242 | |
|
|
243 | $MAPWIDGET = new CFClient::UI::MapWidget; |
|
|
244 | $CFClient::UI::TOPLEVEL->add ($MAPWIDGET); |
|
|
245 | $MAPWIDGET->focus_in; |
|
|
246 | |
|
|
247 | config_dialog; |
|
|
248 | } |
|
|
249 | |
|
|
250 | sub destroy_screen { |
|
|
251 | $CFClient::UI::TOPLEVEL->{children} = []; |
|
|
252 | undef $SDL_ACTIVE; |
|
|
253 | undef $SDL_EV; |
|
|
254 | SDL::Quit; |
|
|
255 | } |
|
|
256 | |
|
|
257 | my %animate_object; |
|
|
258 | my $animate_timer; |
|
|
259 | |
|
|
260 | my $want_refresh; |
|
|
261 | my $can_refresh; |
|
|
262 | |
|
|
263 | my $fps = 9; |
183 | |
264 | |
184 | sub force_refresh { |
265 | sub force_refresh { |
|
|
266 | $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05; |
|
|
267 | debug sprintf "%3.2f", $fps; |
|
|
268 | |
|
|
269 | $want_refresh = 0; |
|
|
270 | $can_refresh = 0; |
|
|
271 | |
185 | glViewport 0, 0, $WIDTH, $HEIGHT; |
272 | glViewport 0, 0, $WIDTH, $HEIGHT; |
186 | |
273 | |
187 | glMatrixMode GL_PROJECTION; |
274 | glMatrixMode GL_PROJECTION; |
188 | glLoadIdentity; |
275 | glLoadIdentity; |
189 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
276 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
190 | glMatrixMode GL_MODELVIEW; |
277 | glMatrixMode GL_MODELVIEW; |
|
|
278 | glLoadIdentity; |
191 | |
279 | |
|
|
280 | glClearColor +($CFG->{fow_intensity}) x 3, 1; |
192 | glClear GL_COLOR_BUFFER_BIT; |
281 | glClear GL_COLOR_BUFFER_BIT; |
193 | |
282 | |
194 | $CFClient::Widget::TOPLEVEL->draw; |
283 | $CFClient::UI::TOPLEVEL->draw; |
195 | |
284 | |
196 | SDL::GLSwapBuffers; |
285 | SDL::GLSwapBuffers; |
197 | } |
|
|
198 | |
286 | |
199 | sub debug { |
287 | $LAST_REFRESH = $NOW; |
200 | $DEBUG_STATUS->set_text ($_[0]); |
|
|
201 | $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request); |
|
|
202 | $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0); |
|
|
203 | } |
288 | } |
204 | |
289 | |
205 | my $FPS; |
290 | my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub { |
|
|
291 | $NOW = time; |
|
|
292 | |
|
|
293 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
294 | while $SDL_EV->poll; |
|
|
295 | |
|
|
296 | if (%animate_object) { |
|
|
297 | $_->animate ($LAST_REFRESH - $NOW) for values %animate_object; |
|
|
298 | $want_refresh++; |
|
|
299 | } |
|
|
300 | |
|
|
301 | if ($want_refresh) { |
|
|
302 | force_refresh; |
|
|
303 | } else { |
|
|
304 | $can_refresh = 1; |
|
|
305 | } |
|
|
306 | }); |
206 | |
307 | |
207 | sub refresh { |
308 | sub refresh { |
208 | $refresh_handler ||= add Glib::Idle sub { |
309 | $want_refresh++; |
209 | if ($SDL_APP) { |
|
|
210 | $NOW = SDL::GetTicks; |
|
|
211 | |
|
|
212 | if ($NOW - $last_refresh < $TICKS_PER_FRAME) { |
|
|
213 | SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh); |
|
|
214 | $NOW = SDL::GetTicks; |
|
|
215 | } |
|
|
216 | |
|
|
217 | my $interval = ($NOW - $last_refresh) * 0.001; |
|
|
218 | $last_refresh = $NOW; |
|
|
219 | |
|
|
220 | if ($interval) { |
|
|
221 | $FPS ||= 1 / $interval; |
|
|
222 | $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; |
|
|
223 | debug sprintf "%5.02f", $FPS; |
|
|
224 | } |
|
|
225 | |
|
|
226 | force_refresh; |
|
|
227 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
228 | |
|
|
229 | if (%ANIMATE) { |
|
|
230 | 1 |
|
|
231 | } else { |
|
|
232 | undef $refresh_handler; |
|
|
233 | 0 |
|
|
234 | } |
|
|
235 | } else { |
|
|
236 | undef $refresh_handler; |
|
|
237 | 0 |
|
|
238 | } |
|
|
239 | }; |
|
|
240 | } |
310 | } |
241 | |
311 | |
242 | sub animation_start { |
312 | sub animation_start { |
243 | my ($widget) = @_; |
313 | my ($widget) = @_; |
244 | $ANIMATE{$widget} = $widget; |
314 | $animate_object{$widget} = $widget; |
245 | Scalar::Util::weaken $ANIMATE{$widget}; |
|
|
246 | |
|
|
247 | refresh; |
|
|
248 | } |
315 | } |
249 | |
316 | |
250 | sub animation_stop { |
317 | sub animation_stop { |
251 | my ($widget) = @_; |
318 | my ($widget) = @_; |
252 | delete $ANIMATE{$widget}; |
319 | delete $animate_object{$widget}; |
|
|
320 | } |
|
|
321 | |
|
|
322 | @conn::ISA = Crossfire::Protocol::; |
|
|
323 | |
|
|
324 | sub conn::user_send { |
|
|
325 | my ($self, $command) = @_; |
|
|
326 | |
|
|
327 | $self->send ($command); |
|
|
328 | status $command; |
|
|
329 | } |
|
|
330 | |
|
|
331 | sub conn::feed_map1a { |
|
|
332 | my ($self, $data) = @_; |
|
|
333 | |
|
|
334 | # $self->Crossfire::Protocol::feed_map1a ($data); |
|
|
335 | |
|
|
336 | $MAP->scroll (delete $self->{delayed_scroll_x}, delete $self->{delayed_scroll_y}); |
|
|
337 | $MAP->map1a_update ($data); |
|
|
338 | $MAPWIDGET->update; |
|
|
339 | } |
|
|
340 | |
|
|
341 | #sub conn::map_update { |
|
|
342 | # my ($self, $dirty) = @_; |
|
|
343 | # |
|
|
344 | # $MAPWIDGET->update; |
|
|
345 | #} |
|
|
346 | |
|
|
347 | sub conn::map_clear { |
|
|
348 | my ($self) = @_; |
|
|
349 | |
|
|
350 | $MAP->clear; |
|
|
351 | |
|
|
352 | # refresh; |
|
|
353 | } |
|
|
354 | |
|
|
355 | sub conn::face_find { |
|
|
356 | my ($self, $face) = @_; |
|
|
357 | |
|
|
358 | $FACECACHE->{"$face->{chksum},$face->{name}"} |
|
|
359 | } |
|
|
360 | |
|
|
361 | sub conn::face_update { |
|
|
362 | my ($self, $facenum, $face) = @_; |
|
|
363 | |
|
|
364 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
|
|
365 | |
|
|
366 | my $tex = $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; |
|
|
367 | |
|
|
368 | $MAP->set_texture ($facenum, @$tex{qw(name w h s t)}); |
|
|
369 | $MAPWIDGET->update; |
|
|
370 | } |
|
|
371 | |
|
|
372 | sub conn::query { |
|
|
373 | my ($self, $flags, $prompt) = @_; |
|
|
374 | |
|
|
375 | warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
253 | } |
376 | } |
254 | |
377 | |
255 | %SDL_CB = ( |
378 | %SDL_CB = ( |
256 | SDL_QUIT() => sub { |
379 | SDL_QUIT() => sub { |
257 | main_quit Gtk2; |
380 | Event::unloop -1; |
258 | }, |
381 | }, |
259 | SDL_VIDEORESIZE() => sub { |
382 | SDL_VIDEORESIZE() => sub { |
260 | }, |
383 | }, |
261 | SDL_VIDEOEXPOSE() => sub { |
384 | SDL_VIDEOEXPOSE() => sub { |
262 | refresh; |
385 | refresh; |
263 | }, |
386 | }, |
264 | SDL_KEYDOWN() => sub { |
387 | SDL_KEYDOWN() => sub { |
265 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
388 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
266 | # alt-enter |
389 | # alt-enter |
267 | $FULLSCREEN = !$FULLSCREEN; |
390 | $FULLSCREEN = !$FULLSCREEN; |
|
|
391 | destroy_screen; |
268 | init_screen; |
392 | init_screen; |
269 | } else { |
393 | } else { |
270 | CFClient::Widget::feed_sdl_key_down_event ($SDL_EV); |
394 | CFClient::UI::feed_sdl_key_down_event ($SDL_EV); |
271 | } |
395 | } |
272 | }, |
396 | }, |
273 | SDL_KEYUP() => sub { |
397 | SDL_KEYUP() => sub { |
274 | CFClient::Widget::feed_sdl_key_up_event ($SDL_EV); |
398 | CFClient::UI::feed_sdl_key_up_event ($SDL_EV); |
275 | }, |
399 | }, |
276 | SDL_MOUSEMOTION() => sub { |
400 | SDL_MOUSEMOTION() => sub { |
277 | CFClient::Widget::feed_sdl_motion_event ($SDL_EV); |
401 | CFClient::UI::feed_sdl_motion_event ($SDL_EV); |
278 | }, |
402 | }, |
279 | SDL_MOUSEBUTTONDOWN() => sub { |
403 | SDL_MOUSEBUTTONDOWN() => sub { |
280 | CFClient::Widget::feed_sdl_button_down_event ($SDL_EV); |
404 | CFClient::UI::feed_sdl_button_down_event ($SDL_EV); |
281 | }, |
405 | }, |
282 | SDL_MOUSEBUTTONUP() => sub { |
406 | SDL_MOUSEBUTTONUP() => sub { |
283 | CFClient::Widget::feed_sdl_button_up_event ($SDL_EV); |
407 | CFClient::UI::feed_sdl_button_up_event ($SDL_EV); |
284 | }, |
408 | }, |
285 | SDL_ACTIVEEVENT() => sub { |
409 | SDL_ACTIVEEVENT() => sub { |
286 | # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
410 | # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
287 | }, |
411 | }, |
288 | ); |
412 | ); |
289 | |
413 | |
290 | @conn::ISA = Crossfire::Protocol::; |
|
|
291 | |
|
|
292 | sub conn::map_update { |
|
|
293 | my ($self, $dirty) = @_; |
|
|
294 | |
|
|
295 | $MAPWIDGET->update; |
|
|
296 | } |
|
|
297 | |
|
|
298 | sub conn::map_scroll { |
|
|
299 | my ($self, $dx, $dy) = @_; |
|
|
300 | |
|
|
301 | # refresh; |
|
|
302 | } |
|
|
303 | |
|
|
304 | sub conn::map_clear { |
|
|
305 | my ($self) = @_; |
|
|
306 | |
|
|
307 | # refresh; |
|
|
308 | } |
|
|
309 | |
|
|
310 | sub conn::face_find { |
|
|
311 | my ($self, $face) = @_; |
|
|
312 | |
|
|
313 | $FACECACHE->{"$face->{chksum},$face->{name}"} |
|
|
314 | } |
|
|
315 | |
|
|
316 | sub conn::face_update { |
|
|
317 | my ($self, $face) = @_; |
|
|
318 | |
|
|
319 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
|
|
320 | |
|
|
321 | $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; |
|
|
322 | } |
|
|
323 | |
|
|
324 | sub conn::query { |
|
|
325 | my ($self, $flags, $prompt) = @_; |
|
|
326 | |
|
|
327 | warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
|
|
328 | } |
|
|
329 | |
|
|
330 | sub gtk_add_cfg_field { |
|
|
331 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
|
|
332 | my $i = $cfg->{_i}++; |
|
|
333 | $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1); |
|
|
334 | $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1); |
|
|
335 | if ($key eq 'password') { |
|
|
336 | $ent->set_invisible_char ("*"); |
|
|
337 | $ent->set (visibility => 0) |
|
|
338 | } |
|
|
339 | $ent->set_text ($value); |
|
|
340 | $ent->signal_connect (changed => sub { |
|
|
341 | my ($ent) = @_; |
|
|
342 | $cfg->{$key} = $ent->get_text; |
|
|
343 | # TODO: Mapsize should be a slider in the game gui |
|
|
344 | if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
|
|
345 | $cfg->{$key} = 100; |
|
|
346 | } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
|
|
347 | $cfg->{$key} = 50; |
|
|
348 | } |
|
|
349 | }); |
|
|
350 | } |
|
|
351 | |
|
|
352 | sub run_config_dialog { |
|
|
353 | my (%events) = @_; |
|
|
354 | |
|
|
355 | my $w = Gtk2::Window->new; |
|
|
356 | |
|
|
357 | my @cfg = ( |
|
|
358 | [qw/Host host/], |
|
|
359 | [qw/Port port/], |
|
|
360 | [qw/Mapsize% mapsize/], |
|
|
361 | [qw/Username user/], |
|
|
362 | [qw/Password password/], |
|
|
363 | ); |
|
|
364 | |
|
|
365 | my $cfg = {}; |
|
|
366 | |
|
|
367 | my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); |
|
|
368 | my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; |
|
|
369 | |
|
|
370 | $w->add (my $vb = Gtk2::VBox->new); |
|
|
371 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
|
|
372 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
|
|
373 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
|
|
374 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
|
|
375 | my $i = 0; |
|
|
376 | my $act = 0; |
|
|
377 | for (map { "$_->[0]x$_->[1]" } reverse @modes) { |
|
|
378 | if ($_ eq $selmode) { $act = $i } |
|
|
379 | $cb->append_text ($_); |
|
|
380 | $i++; |
|
|
381 | } |
|
|
382 | $cb->set_active ($act); |
|
|
383 | $cb->signal_connect (changed => sub { |
|
|
384 | my ($cb) = @_; |
|
|
385 | my $txt = $cb->get_active_text; |
|
|
386 | if ($txt =~ m/(\d+)x(\d+)/) { |
|
|
387 | $::CFG->{width} = $1; |
|
|
388 | $::CFG->{height} = $2; |
|
|
389 | } |
|
|
390 | }); |
|
|
391 | |
|
|
392 | $cfg->{_i} = 1; |
|
|
393 | for (@cfg) { |
|
|
394 | gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]}); |
|
|
395 | } |
|
|
396 | |
|
|
397 | $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); |
|
|
398 | $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5); |
|
|
399 | $cb->signal_connect (clicked => sub { |
|
|
400 | for (keys %$cfg) { |
|
|
401 | $::CFG->{$_} = $cfg->{$_} |
|
|
402 | if $_ ne '_i'; |
|
|
403 | } |
|
|
404 | CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc"; |
|
|
405 | }); |
|
|
406 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
|
|
407 | $cb->signal_connect (clicked => sub { |
|
|
408 | for (keys %$cfg) { |
|
|
409 | $::CFG->{$_} = $cfg->{$_} |
|
|
410 | if $_ ne '_i'; |
|
|
411 | } |
|
|
412 | my $cb = $events{login} || sub {}; |
|
|
413 | $cb->($::CFG->{user}, $::CFG->{password}); |
|
|
414 | }); |
|
|
415 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
|
|
416 | $cb->signal_connect (clicked => sub { |
|
|
417 | my $cb = $events{logout} || sub {}; |
|
|
418 | $cb->(); |
|
|
419 | }); |
|
|
420 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
|
|
421 | $cb->signal_connect (clicked => sub { $w->destroy }); |
|
|
422 | |
|
|
423 | $w->show_all; |
|
|
424 | |
|
|
425 | $w->signal_connect (destroy => sub { Gtk2->main_quit }); |
|
|
426 | } |
|
|
427 | |
|
|
428 | |
|
|
429 | ############################################################################# |
414 | ############################################################################# |
430 | |
415 | |
431 | SDL::Init SDL_INIT_EVERYTHING; |
|
|
432 | |
|
|
433 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
416 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
434 | |
417 | |
435 | $CFG ||= { |
418 | my %DEF_CFG = ( |
436 | width => 640, |
419 | width => 640, |
437 | height => 480, |
420 | height => 480, |
|
|
421 | fast => 0, |
|
|
422 | fow_enable => 1, |
|
|
423 | fow_intensity => 0.45, |
|
|
424 | fow_smooth => 0, |
|
|
425 | fullscreen => 0, |
|
|
426 | sdl_mode => 0, |
438 | mapsize => 100, |
427 | mapsize => 100, |
439 | fullscreen => 0, |
|
|
440 | host => "crossfire.schmorp.de", |
428 | host => "crossfire.schmorp.de", |
441 | port => 13327, |
429 | port => 13327, |
442 | }; |
430 | ); |
|
|
431 | |
|
|
432 | while (my ($k, $v) = each %DEF_CFG) { |
|
|
433 | $CFG->{$k} = $v unless exists $CFG->{$k}; |
|
|
434 | } |
|
|
435 | |
|
|
436 | sdl_init; |
|
|
437 | |
|
|
438 | @SDL_MODES = reverse |
|
|
439 | grep $_->[0] >= 640 && $_->[1] >= 480, |
|
|
440 | map [SDL::RectW ($_), SDL::RectH ($_)], |
|
|
441 | @{ SDL::ListModes 0, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_OPENGL }; |
|
|
442 | |
|
|
443 | @SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)"; |
|
|
444 | |
|
|
445 | $CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES; |
|
|
446 | |
|
|
447 | init_screen; |
443 | |
448 | |
444 | { |
449 | { |
445 | my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
450 | my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
446 | |
451 | |
447 | CFClient::add_font $_ for @fonts; |
452 | CFClient::add_font $_ for @fonts; |
448 | CFClient::set_font $fonts[0]; |
453 | CFClient::set_font $fonts[0]; |
449 | } |
454 | } |
450 | |
455 | |
451 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
456 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
452 | |
457 | |
453 | run_config_dialog |
458 | Event::loop; |
454 | login => sub { start_game }, |
|
|
455 | logout => sub { stop_game }; |
|
|
456 | |
|
|
457 | main Gtk2; |
|
|
458 | |
459 | |
459 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |
460 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |
|
|
461 | |