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