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