1 |
root |
1.1 |
#!/opt/bin/perl |
2 |
root |
1.25 |
|
3 |
root |
1.2 |
use strict; |
4 |
root |
1.25 |
use utf8; |
5 |
root |
1.2 |
|
6 |
root |
1.13 |
use Glib; |
7 |
|
|
use Gtk2 -init; |
8 |
|
|
|
9 |
|
|
use SDL; |
10 |
|
|
use SDL::App; |
11 |
|
|
use SDL::Event; |
12 |
|
|
use SDL::Surface; |
13 |
|
|
use SDL::OpenGL; |
14 |
|
|
use SDL::OpenGL::Constants; |
15 |
|
|
|
16 |
elmex |
1.11 |
use Crossfire; |
17 |
root |
1.2 |
use Crossfire::Client; |
18 |
|
|
use Crossfire::Protocol; |
19 |
|
|
|
20 |
elmex |
1.20 |
use Crossfire::Client::Widget; |
21 |
elmex |
1.10 |
|
22 |
root |
1.19 |
our $FACECACHE; |
23 |
|
|
|
24 |
root |
1.13 |
our $VERSION = '0.1'; |
25 |
root |
1.2 |
|
26 |
root |
1.30 |
our %GL_EXT; |
27 |
|
|
|
28 |
elmex |
1.10 |
our $CFG; |
29 |
root |
1.13 |
our $CONN; |
30 |
root |
1.2 |
|
31 |
root |
1.30 |
our $WIDTH; |
32 |
|
|
our $HEIGHT; |
33 |
|
|
our $FULLSCREEN; |
34 |
|
|
|
35 |
root |
1.39 |
our $FONTSIZE; |
36 |
root |
1.54 |
our $FOCUS; |
37 |
|
|
our $HOVER; |
38 |
root |
1.24 |
|
39 |
root |
1.13 |
our $SDL_TIMER; |
40 |
|
|
our $SDL_APP; |
41 |
|
|
our $SDL_EV = new SDL::Event; |
42 |
|
|
our %SDL_CB; |
43 |
root |
1.18 |
|
44 |
|
|
our @GL_INIT; # hooks called on every gl init |
45 |
root |
1.2 |
|
46 |
root |
1.30 |
our $ALT_ENTER_MESSAGE; |
47 |
root |
1.51 |
our $STATUS_LINE; |
48 |
root |
1.30 |
|
49 |
elmex |
1.41 |
our $TOPLEVEL; |
50 |
|
|
|
51 |
elmex |
1.34 |
our $tw; # Test widget #d# |
52 |
elmex |
1.32 |
|
53 |
root |
1.45 |
my $last_refresh; |
54 |
root |
1.50 |
my %ANIMATE; |
55 |
|
|
my $refresh_handler; |
56 |
root |
1.45 |
|
57 |
root |
1.13 |
sub init_screen { |
58 |
|
|
$SDL_APP = new SDL::App |
59 |
root |
1.5 |
-flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
60 |
|
|
-title => "Crossfire+ Client", |
61 |
root |
1.30 |
-width => $WIDTH, |
62 |
|
|
-height => $HEIGHT, |
63 |
root |
1.5 |
-opengl => 1, |
64 |
root |
1.51 |
-red_size => 5, |
65 |
|
|
-green_size => 5, |
66 |
|
|
-blue_size => 5, |
67 |
|
|
-alpha_size => 0, |
68 |
root |
1.2 |
-double_buffer => 1, |
69 |
root |
1.30 |
-fullscreen => $FULLSCREEN, |
70 |
root |
1.5 |
-resizeable => 0; |
71 |
root |
1.2 |
|
72 |
root |
1.45 |
$last_refresh = SDL::GetTicks; |
73 |
|
|
|
74 |
root |
1.30 |
%GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
75 |
|
|
|
76 |
|
|
$GL_EXT{GL_ARB_texture_non_power_of_two} |
77 |
|
|
or warn "WARNING: non-power-of-two opengl extension required"; |
78 |
|
|
|
79 |
root |
1.40 |
$FONTSIZE = int $HEIGHT / 50; |
80 |
root |
1.39 |
|
81 |
root |
1.52 |
############################################################################# |
82 |
|
|
|
83 |
|
|
glClearColor 0, 0, 0, 0; |
84 |
|
|
|
85 |
|
|
glEnable GL_TEXTURE_2D; |
86 |
|
|
glEnable GL_COLOR_MATERIAL; |
87 |
|
|
glShadeModel GL_FLAT; |
88 |
|
|
glDisable GL_DEPTH_TEST; |
89 |
|
|
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
90 |
|
|
|
91 |
|
|
$_->() for @GL_INIT; |
92 |
|
|
|
93 |
|
|
############################################################################# |
94 |
|
|
|
95 |
root |
1.51 |
$STATUS_LINE = new Crossfire::Client::Widget::Label |
96 |
|
|
0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, |
97 |
|
|
""; |
98 |
|
|
$TOPLEVEL->add ($STATUS_LINE); |
99 |
|
|
|
100 |
root |
1.40 |
$ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
101 |
root |
1.51 |
0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, |
102 |
root |
1.40 |
"Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
103 |
elmex |
1.41 |
$TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
104 |
root |
1.30 |
|
105 |
elmex |
1.34 |
# Test code #d# |
106 |
elmex |
1.43 |
unless ($tw) { # haha... |
107 |
root |
1.45 |
$tw = new Crossfire::Client::Widget::Animator; |
108 |
elmex |
1.46 |
my $lbl1 = new Crossfire::Client::Widget::Label |
109 |
|
|
0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
110 |
|
|
my $lbl2 = new Crossfire::Client::Widget::Label |
111 |
|
|
0, 0, 10, $FONTSIZE, "LBL2"; |
112 |
|
|
|
113 |
|
|
my $vb = new Crossfire::Client::Widget::VBox; |
114 |
|
|
my $f = new Crossfire::Client::Widget::FancyFrame; |
115 |
|
|
my $f2 = new Crossfire::Client::Widget::FancyFrame; |
116 |
|
|
$f->add ($lbl1); |
117 |
|
|
$f2->add ($lbl2); |
118 |
|
|
$vb->add ($f); |
119 |
|
|
$vb->add ($f2, 1); |
120 |
|
|
|
121 |
|
|
$tw->add ($vb); |
122 |
|
|
$tw->w (400); |
123 |
|
|
$tw->h (300); |
124 |
elmex |
1.44 |
$tw->move ($WIDTH - 200, 0); |
125 |
root |
1.45 |
$tw->moveto (0, 0); |
126 |
elmex |
1.41 |
$TOPLEVEL->add ($tw); |
127 |
elmex |
1.46 |
|
128 |
|
|
# $f->move ($WIDTH - 200, 0); |
129 |
|
|
# $TOPLEVEL->add ($f); |
130 |
root |
1.39 |
} |
131 |
root |
1.2 |
} |
132 |
|
|
|
133 |
root |
1.13 |
sub start_game { |
134 |
root |
1.50 |
$SDL_TIMER = add Glib::Timeout 1000/50, sub { |
135 |
root |
1.30 |
($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
136 |
|
|
while $SDL_EV->poll; |
137 |
root |
1.13 |
|
138 |
|
|
1 |
139 |
|
|
}; |
140 |
|
|
|
141 |
root |
1.30 |
$WIDTH = $CFG->{width}; |
142 |
|
|
$HEIGHT = $CFG->{height}; |
143 |
|
|
$FULLSCREEN = 0; |
144 |
|
|
|
145 |
root |
1.13 |
init_screen; |
146 |
|
|
|
147 |
root |
1.50 |
my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; |
148 |
|
|
|
149 |
root |
1.13 |
$CONN = new conn |
150 |
|
|
host => $CFG->{host}, |
151 |
root |
1.28 |
port => $CFG->{port}, |
152 |
|
|
user => $CFG->{user}, |
153 |
root |
1.37 |
pass => $CFG->{password}, |
154 |
root |
1.50 |
mapw => $mapsize, |
155 |
|
|
maph => $mapsize, |
156 |
root |
1.37 |
; |
157 |
root |
1.45 |
|
158 |
|
|
Crossfire::Client::lowdelay fileno $CONN->{fh}; |
159 |
root |
1.13 |
} |
160 |
|
|
|
161 |
|
|
sub stop_game { |
162 |
|
|
remove Glib::Source $SDL_TIMER; |
163 |
root |
1.51 |
remove Glib::Source $refresh_handler if $refresh_handler; |
164 |
root |
1.50 |
undef $refresh_handler; |
165 |
root |
1.13 |
|
166 |
|
|
undef $SDL_APP; |
167 |
root |
1.50 |
undef $CONN; |
168 |
root |
1.13 |
SDL::Quit; |
169 |
|
|
} |
170 |
|
|
|
171 |
root |
1.30 |
|
172 |
|
|
sub force_refresh { |
173 |
|
|
glViewport 0, 0, $WIDTH, $HEIGHT; |
174 |
root |
1.35 |
|
175 |
|
|
glMatrixMode GL_PROJECTION; |
176 |
|
|
glLoadIdentity; |
177 |
root |
1.45 |
glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; |
178 |
root |
1.35 |
glMatrixMode GL_MODELVIEW; |
179 |
|
|
|
180 |
root |
1.5 |
glClear GL_COLOR_BUFFER_BIT; |
181 |
root |
1.2 |
|
182 |
elmex |
1.41 |
$TOPLEVEL->draw; |
183 |
root |
1.1 |
|
184 |
root |
1.2 |
SDL::GLSwapBuffers; |
185 |
root |
1.1 |
} |
186 |
|
|
|
187 |
root |
1.30 |
sub refresh { |
188 |
|
|
$refresh_handler ||= add Glib::Idle sub { |
189 |
root |
1.50 |
return unless $SDL_APP; |
190 |
|
|
|
191 |
root |
1.45 |
my $next_refresh = SDL::GetTicks; |
192 |
|
|
my $interval = ($next_refresh - $last_refresh) * 0.001; |
193 |
|
|
$last_refresh = $next_refresh; |
194 |
|
|
|
195 |
root |
1.30 |
force_refresh; |
196 |
root |
1.45 |
$_->animate ($interval) for grep $_, values %ANIMATE; |
197 |
|
|
|
198 |
|
|
if (%ANIMATE) { |
199 |
|
|
1 |
200 |
|
|
} else { |
201 |
|
|
undef $refresh_handler; |
202 |
|
|
0 |
203 |
|
|
} |
204 |
root |
1.30 |
}; |
205 |
|
|
} |
206 |
|
|
|
207 |
root |
1.45 |
sub animation_start { |
208 |
|
|
my ($widget) = @_; |
209 |
|
|
$ANIMATE{$widget} = $widget; |
210 |
|
|
Scalar::Util::weaken $ANIMATE{$widget}; |
211 |
|
|
|
212 |
|
|
refresh; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
sub animation_stop { |
216 |
|
|
my ($widget) = @_; |
217 |
|
|
delete $ANIMATE{$widget}; |
218 |
|
|
} |
219 |
|
|
|
220 |
root |
1.13 |
%SDL_CB = ( |
221 |
|
|
SDL_QUIT() => sub { |
222 |
root |
1.19 |
main_quit Gtk2; |
223 |
root |
1.13 |
}, |
224 |
|
|
SDL_VIDEORESIZE() => sub { |
225 |
|
|
}, |
226 |
|
|
SDL_VIDEOEXPOSE() => sub { |
227 |
|
|
refresh; |
228 |
|
|
}, |
229 |
|
|
SDL_KEYDOWN() => sub { |
230 |
root |
1.18 |
if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
231 |
|
|
# alt-enter |
232 |
root |
1.30 |
$FULLSCREEN = !$FULLSCREEN; |
233 |
root |
1.18 |
init_screen; |
234 |
|
|
} else { |
235 |
root |
1.22 |
Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
236 |
root |
1.18 |
} |
237 |
root |
1.13 |
}, |
238 |
|
|
SDL_KEYUP() => sub { |
239 |
root |
1.22 |
Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
240 |
root |
1.13 |
}, |
241 |
|
|
SDL_MOUSEMOTION() => sub { |
242 |
root |
1.53 |
my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y); |
243 |
root |
1.54 |
$HOVER = $TOPLEVEL->find_widget ($x, $y); |
244 |
root |
1.55 |
$self->refresh; |
245 |
root |
1.53 |
|
246 |
root |
1.54 |
warn "mouse $x, $y = $HOVER\n"; |
247 |
root |
1.13 |
}, |
248 |
|
|
SDL_MOUSEBUTTONDOWN() => sub { |
249 |
root |
1.22 |
Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
250 |
root |
1.13 |
}, |
251 |
|
|
SDL_MOUSEBUTTONUP() => sub { |
252 |
root |
1.22 |
Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
253 |
root |
1.13 |
}, |
254 |
|
|
SDL_ACTIVEEVENT() => sub { |
255 |
|
|
warn "active\n";#d# |
256 |
|
|
}, |
257 |
|
|
); |
258 |
root |
1.1 |
|
259 |
root |
1.2 |
@conn::ISA = Crossfire::Protocol::; |
260 |
root |
1.1 |
|
261 |
root |
1.2 |
sub conn::map_update { |
262 |
root |
1.1 |
my ($self, $dirty) = @_; |
263 |
|
|
|
264 |
root |
1.2 |
refresh; |
265 |
root |
1.1 |
} |
266 |
|
|
|
267 |
root |
1.2 |
sub conn::map_scroll { |
268 |
root |
1.1 |
my ($self, $dx, $dy) = @_; |
269 |
|
|
|
270 |
root |
1.45 |
# refresh; |
271 |
root |
1.1 |
} |
272 |
|
|
|
273 |
root |
1.2 |
sub conn::map_clear { |
274 |
root |
1.1 |
my ($self) = @_; |
275 |
|
|
|
276 |
root |
1.45 |
# refresh; |
277 |
root |
1.1 |
} |
278 |
|
|
|
279 |
root |
1.19 |
sub conn::face_find { |
280 |
|
|
my ($self, $face) = @_; |
281 |
|
|
|
282 |
|
|
$FACECACHE->{"$face->{chksum},$face->{name}"} |
283 |
|
|
} |
284 |
|
|
|
285 |
root |
1.2 |
sub conn::face_update { |
286 |
root |
1.19 |
my ($self, $face) = @_; |
287 |
|
|
|
288 |
|
|
$FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
289 |
root |
1.1 |
|
290 |
root |
1.18 |
$face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
291 |
root |
1.1 |
} |
292 |
|
|
|
293 |
root |
1.33 |
sub conn::query { |
294 |
|
|
my ($self, $flags, $prompt) = @_; |
295 |
|
|
|
296 |
|
|
warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
297 |
|
|
} |
298 |
|
|
|
299 |
elmex |
1.23 |
sub gtk_add_cfg_field { |
300 |
|
|
my ($tbl, $cfg, $klbl, $key, $value) = @_; |
301 |
|
|
my $i = $cfg->{_i}++; |
302 |
|
|
$tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1); |
303 |
|
|
$tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1); |
304 |
|
|
if ($key eq 'password') { |
305 |
|
|
$ent->set_invisible_char ("*"); |
306 |
|
|
$ent->set (visibility => 0) |
307 |
|
|
} |
308 |
|
|
$ent->set_text ($value); |
309 |
|
|
$ent->signal_connect (changed => sub { |
310 |
|
|
my ($ent) = @_; |
311 |
|
|
$cfg->{$key} = $ent->get_text; |
312 |
elmex |
1.49 |
# TODO: Mapsize should be a slider in the game gui |
313 |
elmex |
1.47 |
if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
314 |
|
|
$cfg->{$key} = 100; |
315 |
elmex |
1.48 |
} elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
316 |
|
|
$cfg->{$key} = 50; |
317 |
elmex |
1.47 |
} |
318 |
elmex |
1.23 |
}); |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
sub run_config_dialog { |
322 |
|
|
my (%events) = @_; |
323 |
|
|
|
324 |
|
|
my $w = Gtk2::Window->new; |
325 |
|
|
|
326 |
|
|
my @cfg = ( |
327 |
|
|
[qw/Host host/], |
328 |
|
|
[qw/Port port/], |
329 |
elmex |
1.47 |
[qw/Mapsize% mapsize/], |
330 |
elmex |
1.23 |
[qw/Username user/], |
331 |
|
|
[qw/Password password/], |
332 |
|
|
); |
333 |
|
|
|
334 |
|
|
my $cfg = {}; |
335 |
|
|
|
336 |
|
|
my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); |
337 |
|
|
my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; |
338 |
|
|
|
339 |
|
|
$w->add (my $vb = Gtk2::VBox->new); |
340 |
|
|
$vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
341 |
|
|
my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
342 |
|
|
$t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
343 |
|
|
$t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
344 |
|
|
my $i = 0; |
345 |
|
|
my $act = 0; |
346 |
|
|
for (map { "$_->[0]x$_->[1]" } reverse @modes) { |
347 |
|
|
if ($_ eq $selmode) { $act = $i } |
348 |
|
|
$cb->append_text ($_); |
349 |
|
|
$i++; |
350 |
|
|
} |
351 |
|
|
$cb->set_active ($act); |
352 |
|
|
$cb->signal_connect (changed => sub { |
353 |
|
|
my ($cb) = @_; |
354 |
|
|
my $txt = $cb->get_active_text; |
355 |
|
|
if ($txt =~ m/(\d+)x(\d+)/) { |
356 |
|
|
$::CFG->{width} = $1; |
357 |
|
|
$::CFG->{height} = $2; |
358 |
|
|
} |
359 |
|
|
}); |
360 |
|
|
|
361 |
|
|
$cfg->{_i} = 1; |
362 |
|
|
for (@cfg) { |
363 |
|
|
gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]}); |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
$vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); |
367 |
|
|
$hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5); |
368 |
|
|
$cb->signal_connect (clicked => sub { |
369 |
|
|
for (keys %$cfg) { |
370 |
|
|
$::CFG->{$_} = $cfg->{$_} |
371 |
|
|
if $_ ne '_i'; |
372 |
|
|
} |
373 |
|
|
Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; |
374 |
|
|
}); |
375 |
|
|
$hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
376 |
|
|
$cb->signal_connect (clicked => sub { |
377 |
|
|
for (keys %$cfg) { |
378 |
|
|
$::CFG->{$_} = $cfg->{$_} |
379 |
|
|
if $_ ne '_i'; |
380 |
|
|
} |
381 |
|
|
my $cb = $events{login} || sub {}; |
382 |
|
|
$cb->($::CFG->{user}, $::CFG->{password}); |
383 |
|
|
}); |
384 |
|
|
$hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
385 |
|
|
$cb->signal_connect (clicked => sub { |
386 |
root |
1.50 |
my $cb = $events{logout} || sub {}; |
387 |
elmex |
1.23 |
$cb->(); |
388 |
|
|
}); |
389 |
|
|
$hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
390 |
|
|
$cb->signal_connect (clicked => sub { $w->destroy }); |
391 |
|
|
|
392 |
|
|
$w->show_all; |
393 |
|
|
|
394 |
|
|
$w->signal_connect (destroy => sub { Gtk2->main_quit }); |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
|
398 |
root |
1.1 |
############################################################################# |
399 |
|
|
|
400 |
root |
1.24 |
SDL::Init SDL_INIT_EVERYTHING; |
401 |
elmex |
1.20 |
|
402 |
elmex |
1.41 |
$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
403 |
|
|
|
404 |
elmex |
1.20 |
my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
405 |
|
|
|
406 |
elmex |
1.41 |
$TOPLEVEL->add ($mapwidget); |
407 |
elmex |
1.16 |
$mapwidget->focus_in; |
408 |
|
|
|
409 |
root |
1.22 |
Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
410 |
elmex |
1.10 |
|
411 |
root |
1.13 |
$CFG ||= { |
412 |
|
|
width => 640, |
413 |
|
|
height => 480, |
414 |
elmex |
1.47 |
mapsize => 100, |
415 |
root |
1.13 |
fullscreen => 0, |
416 |
|
|
host => "crossfire.schmorp.de", |
417 |
|
|
port => 13327, |
418 |
|
|
}; |
419 |
elmex |
1.12 |
|
420 |
root |
1.42 |
Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
421 |
root |
1.40 |
|
422 |
root |
1.24 |
$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
423 |
|
|
|
424 |
elmex |
1.23 |
run_config_dialog |
425 |
elmex |
1.16 |
login => sub { start_game }, |
426 |
|
|
logout => sub { stop_game }; |
427 |
root |
1.1 |
|
428 |
root |
1.13 |
main Gtk2; |
429 |
root |
1.19 |
|
430 |
|
|
Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |