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