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.24 |
|
37 |
root |
1.13 |
our $SDL_TIMER; |
38 |
|
|
our $SDL_APP; |
39 |
|
|
our $SDL_EV = new SDL::Event; |
40 |
|
|
our %SDL_CB; |
41 |
root |
1.18 |
|
42 |
|
|
our @GL_INIT; # hooks called on every gl init |
43 |
root |
1.2 |
|
44 |
root |
1.30 |
our $ALT_ENTER_MESSAGE; |
45 |
|
|
|
46 |
elmex |
1.34 |
our $tw; # Test widget #d# |
47 |
elmex |
1.32 |
|
48 |
root |
1.13 |
sub init_screen { |
49 |
|
|
$SDL_APP = new SDL::App |
50 |
root |
1.5 |
-flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
51 |
|
|
-title => "Crossfire+ Client", |
52 |
root |
1.30 |
-width => $WIDTH, |
53 |
|
|
-height => $HEIGHT, |
54 |
root |
1.5 |
-opengl => 1, |
55 |
|
|
-red_size => 8, |
56 |
|
|
-green_size => 8, |
57 |
|
|
-blue_size => 8, |
58 |
root |
1.2 |
-double_buffer => 1, |
59 |
root |
1.30 |
-fullscreen => $FULLSCREEN, |
60 |
root |
1.5 |
-resizeable => 0; |
61 |
root |
1.2 |
|
62 |
root |
1.30 |
%GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
63 |
|
|
|
64 |
|
|
$GL_EXT{GL_ARB_texture_non_power_of_two} |
65 |
|
|
or warn "WARNING: non-power-of-two opengl extension required"; |
66 |
|
|
|
67 |
root |
1.40 |
$FONTSIZE = int $HEIGHT / 50; |
68 |
root |
1.39 |
|
69 |
root |
1.40 |
$ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
70 |
|
|
0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE, |
71 |
|
|
"Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
72 |
root |
1.30 |
$ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
73 |
|
|
$ALT_ENTER_MESSAGE->activate; |
74 |
|
|
|
75 |
elmex |
1.34 |
# Test code #d# |
76 |
root |
1.39 |
{ |
77 |
|
|
my $frm = new Crossfire::Client::Widget::Frame; |
78 |
|
|
$tw = new Crossfire::Client::Widget::Window; |
79 |
|
|
$frm->add (new Crossfire::Client::Widget::Label 0, 0, 10, $FONTSIZE, "Foo in the garden!"); |
80 |
|
|
$tw->add ($frm); |
81 |
|
|
|
82 |
|
|
$tw->move (0, $HEIGHT - 120); |
83 |
|
|
$tw->activate; |
84 |
|
|
} |
85 |
elmex |
1.32 |
|
86 |
root |
1.19 |
glClearColor 0, 0, 0, 0; |
87 |
|
|
|
88 |
root |
1.2 |
glEnable GL_TEXTURE_2D; |
89 |
elmex |
1.36 |
glEnable GL_COLOR_MATERIAL; |
90 |
root |
1.2 |
glShadeModel GL_FLAT; |
91 |
|
|
glDisable GL_DEPTH_TEST; |
92 |
root |
1.9 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
93 |
root |
1.2 |
|
94 |
root |
1.17 |
$_->() for @GL_INIT; |
95 |
root |
1.2 |
} |
96 |
|
|
|
97 |
root |
1.13 |
sub start_game { |
98 |
root |
1.30 |
$SDL_TIMER = add Glib::Timeout 1000/100, sub { |
99 |
|
|
($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
100 |
|
|
while $SDL_EV->poll; |
101 |
root |
1.13 |
|
102 |
|
|
1 |
103 |
|
|
}; |
104 |
|
|
|
105 |
root |
1.30 |
$WIDTH = $CFG->{width}; |
106 |
|
|
$HEIGHT = $CFG->{height}; |
107 |
|
|
$FULLSCREEN = 0; |
108 |
|
|
|
109 |
root |
1.13 |
init_screen; |
110 |
|
|
|
111 |
|
|
$CONN = new conn |
112 |
|
|
host => $CFG->{host}, |
113 |
root |
1.28 |
port => $CFG->{port}, |
114 |
|
|
user => $CFG->{user}, |
115 |
root |
1.37 |
pass => $CFG->{password}, |
116 |
|
|
mapw => 50, |
117 |
|
|
maph => 37, |
118 |
|
|
; |
119 |
root |
1.13 |
} |
120 |
|
|
|
121 |
|
|
sub stop_game { |
122 |
|
|
remove Glib::Source $SDL_TIMER; |
123 |
|
|
|
124 |
|
|
undef $SDL_APP; |
125 |
|
|
SDL::Quit; |
126 |
|
|
} |
127 |
|
|
|
128 |
root |
1.30 |
|
129 |
|
|
sub force_refresh { |
130 |
|
|
glViewport 0, 0, $WIDTH, $HEIGHT; |
131 |
root |
1.35 |
|
132 |
|
|
glMatrixMode GL_PROJECTION; |
133 |
|
|
glLoadIdentity; |
134 |
|
|
glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
135 |
|
|
glMatrixMode GL_MODELVIEW; |
136 |
|
|
|
137 |
root |
1.5 |
glClear GL_COLOR_BUFFER_BIT; |
138 |
root |
1.2 |
|
139 |
elmex |
1.26 |
$_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; |
140 |
root |
1.1 |
|
141 |
root |
1.2 |
SDL::GLSwapBuffers; |
142 |
root |
1.1 |
} |
143 |
|
|
|
144 |
root |
1.30 |
my $refresh_handler; |
145 |
|
|
|
146 |
|
|
sub refresh { |
147 |
|
|
$refresh_handler ||= add Glib::Idle sub { |
148 |
|
|
force_refresh; |
149 |
|
|
undef $refresh_handler; |
150 |
|
|
0 |
151 |
|
|
}; |
152 |
|
|
} |
153 |
|
|
|
154 |
root |
1.13 |
%SDL_CB = ( |
155 |
|
|
SDL_QUIT() => sub { |
156 |
root |
1.19 |
main_quit Gtk2; |
157 |
root |
1.13 |
}, |
158 |
|
|
SDL_VIDEORESIZE() => sub { |
159 |
|
|
}, |
160 |
|
|
SDL_VIDEOEXPOSE() => sub { |
161 |
|
|
refresh; |
162 |
|
|
}, |
163 |
|
|
SDL_KEYDOWN() => sub { |
164 |
root |
1.18 |
if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
165 |
|
|
# alt-enter |
166 |
root |
1.30 |
$FULLSCREEN = !$FULLSCREEN; |
167 |
root |
1.18 |
init_screen; |
168 |
|
|
} else { |
169 |
root |
1.22 |
Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
170 |
root |
1.18 |
} |
171 |
root |
1.13 |
}, |
172 |
|
|
SDL_KEYUP() => sub { |
173 |
root |
1.22 |
Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
174 |
root |
1.13 |
}, |
175 |
|
|
SDL_MOUSEMOTION() => sub { |
176 |
|
|
warn "sdl motion\n";#d# |
177 |
|
|
}, |
178 |
|
|
SDL_MOUSEBUTTONDOWN() => sub { |
179 |
root |
1.22 |
Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
180 |
root |
1.13 |
}, |
181 |
|
|
SDL_MOUSEBUTTONUP() => sub { |
182 |
root |
1.22 |
Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
183 |
root |
1.13 |
}, |
184 |
|
|
SDL_ACTIVEEVENT() => sub { |
185 |
|
|
warn "active\n";#d# |
186 |
|
|
}, |
187 |
|
|
); |
188 |
root |
1.1 |
|
189 |
root |
1.2 |
@conn::ISA = Crossfire::Protocol::; |
190 |
root |
1.1 |
|
191 |
root |
1.2 |
sub conn::map_update { |
192 |
root |
1.1 |
my ($self, $dirty) = @_; |
193 |
|
|
|
194 |
root |
1.2 |
refresh; |
195 |
root |
1.1 |
} |
196 |
|
|
|
197 |
root |
1.2 |
sub conn::map_scroll { |
198 |
root |
1.1 |
my ($self, $dx, $dy) = @_; |
199 |
|
|
|
200 |
root |
1.2 |
refresh; |
201 |
root |
1.1 |
} |
202 |
|
|
|
203 |
root |
1.2 |
sub conn::map_clear { |
204 |
root |
1.1 |
my ($self) = @_; |
205 |
|
|
|
206 |
root |
1.2 |
refresh; |
207 |
root |
1.1 |
} |
208 |
|
|
|
209 |
root |
1.19 |
sub conn::face_find { |
210 |
|
|
my ($self, $face) = @_; |
211 |
|
|
|
212 |
|
|
$FACECACHE->{"$face->{chksum},$face->{name}"} |
213 |
|
|
} |
214 |
|
|
|
215 |
root |
1.2 |
sub conn::face_update { |
216 |
root |
1.19 |
my ($self, $face) = @_; |
217 |
|
|
|
218 |
|
|
$FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
219 |
root |
1.1 |
|
220 |
root |
1.18 |
$face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
221 |
root |
1.1 |
} |
222 |
|
|
|
223 |
root |
1.33 |
sub conn::query { |
224 |
|
|
my ($self, $flags, $prompt) = @_; |
225 |
|
|
|
226 |
|
|
warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
227 |
|
|
} |
228 |
|
|
|
229 |
elmex |
1.23 |
sub gtk_add_cfg_field { |
230 |
|
|
my ($tbl, $cfg, $klbl, $key, $value) = @_; |
231 |
|
|
my $i = $cfg->{_i}++; |
232 |
|
|
$tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1); |
233 |
|
|
$tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1); |
234 |
|
|
if ($key eq 'password') { |
235 |
|
|
$ent->set_invisible_char ("*"); |
236 |
|
|
$ent->set (visibility => 0) |
237 |
|
|
} |
238 |
|
|
$ent->set_text ($value); |
239 |
|
|
$ent->signal_connect (changed => sub { |
240 |
|
|
my ($ent) = @_; |
241 |
|
|
$cfg->{$key} = $ent->get_text; |
242 |
|
|
}); |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
sub run_config_dialog { |
246 |
|
|
my (%events) = @_; |
247 |
|
|
|
248 |
|
|
my $w = Gtk2::Window->new; |
249 |
|
|
|
250 |
|
|
my @cfg = ( |
251 |
|
|
[qw/Host host/], |
252 |
|
|
[qw/Port port/], |
253 |
|
|
[qw/Username user/], |
254 |
|
|
[qw/Password password/], |
255 |
|
|
); |
256 |
|
|
|
257 |
|
|
my $cfg = {}; |
258 |
|
|
|
259 |
|
|
my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); |
260 |
|
|
my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; |
261 |
|
|
|
262 |
|
|
$w->add (my $vb = Gtk2::VBox->new); |
263 |
|
|
$vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
264 |
|
|
my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
265 |
|
|
$t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
266 |
|
|
$t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
267 |
|
|
my $i = 0; |
268 |
|
|
my $act = 0; |
269 |
|
|
for (map { "$_->[0]x$_->[1]" } reverse @modes) { |
270 |
|
|
if ($_ eq $selmode) { $act = $i } |
271 |
|
|
$cb->append_text ($_); |
272 |
|
|
$i++; |
273 |
|
|
} |
274 |
|
|
$cb->set_active ($act); |
275 |
|
|
$cb->signal_connect (changed => sub { |
276 |
|
|
my ($cb) = @_; |
277 |
|
|
my $txt = $cb->get_active_text; |
278 |
|
|
if ($txt =~ m/(\d+)x(\d+)/) { |
279 |
|
|
$::CFG->{width} = $1; |
280 |
|
|
$::CFG->{height} = $2; |
281 |
|
|
} |
282 |
|
|
}); |
283 |
|
|
|
284 |
|
|
$cfg->{_i} = 1; |
285 |
|
|
for (@cfg) { |
286 |
|
|
gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]}); |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
$vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); |
290 |
|
|
$hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5); |
291 |
|
|
$cb->signal_connect (clicked => sub { |
292 |
|
|
for (keys %$cfg) { |
293 |
|
|
$::CFG->{$_} = $cfg->{$_} |
294 |
|
|
if $_ ne '_i'; |
295 |
|
|
} |
296 |
|
|
Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; |
297 |
|
|
}); |
298 |
|
|
$hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
299 |
|
|
$cb->signal_connect (clicked => sub { |
300 |
|
|
for (keys %$cfg) { |
301 |
|
|
$::CFG->{$_} = $cfg->{$_} |
302 |
|
|
if $_ ne '_i'; |
303 |
|
|
} |
304 |
|
|
my $cb = $events{login} || sub {}; |
305 |
|
|
$cb->($::CFG->{user}, $::CFG->{password}); |
306 |
|
|
}); |
307 |
|
|
$hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
308 |
|
|
$cb->signal_connect (clicked => sub { |
309 |
|
|
my $cb = $events{login} || sub {}; |
310 |
|
|
$cb->(); |
311 |
|
|
}); |
312 |
|
|
$hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
313 |
|
|
$cb->signal_connect (clicked => sub { $w->destroy }); |
314 |
|
|
|
315 |
|
|
$w->show_all; |
316 |
|
|
|
317 |
|
|
$w->signal_connect (destroy => sub { Gtk2->main_quit }); |
318 |
|
|
} |
319 |
|
|
|
320 |
|
|
|
321 |
root |
1.1 |
############################################################################# |
322 |
|
|
|
323 |
root |
1.24 |
SDL::Init SDL_INIT_EVERYTHING; |
324 |
elmex |
1.20 |
|
325 |
|
|
my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
326 |
|
|
|
327 |
elmex |
1.16 |
$mapwidget->activate; |
328 |
|
|
$mapwidget->focus_in; |
329 |
|
|
|
330 |
root |
1.22 |
Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
331 |
elmex |
1.10 |
|
332 |
root |
1.13 |
$CFG ||= { |
333 |
|
|
width => 640, |
334 |
|
|
height => 480, |
335 |
|
|
fullscreen => 0, |
336 |
|
|
host => "crossfire.schmorp.de", |
337 |
|
|
port => 13327, |
338 |
|
|
}; |
339 |
elmex |
1.12 |
|
340 |
root |
1.40 |
Crossfire::Client::add_font Crossfire::Client::find_rcfile "uifont.ttf"; |
341 |
|
|
Crossfire::Client::set_font "[Bitstream Vera Sans Mono]"; |
342 |
|
|
|
343 |
root |
1.24 |
$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
344 |
|
|
|
345 |
elmex |
1.23 |
run_config_dialog |
346 |
elmex |
1.16 |
login => sub { start_game }, |
347 |
|
|
logout => sub { stop_game }; |
348 |
root |
1.1 |
|
349 |
root |
1.13 |
main Gtk2; |
350 |
root |
1.19 |
|
351 |
|
|
Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |