ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.40
Committed: Sun Apr 9 01:19:16 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.39: +7 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #!/opt/bin/perl
2
3 use strict;
4 use utf8;
5
6 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 use Crossfire;
17 use Crossfire::Client;
18 use Crossfire::Protocol;
19
20 use Crossfire::Client::Widget;
21
22 our $FACECACHE;
23
24 our $VERSION = '0.1';
25
26 our %GL_EXT;
27
28 our $CFG;
29 our $CONN;
30
31 our $WIDTH;
32 our $HEIGHT;
33 our $FULLSCREEN;
34
35 our $FONTSIZE;
36
37 our $SDL_TIMER;
38 our $SDL_APP;
39 our $SDL_EV = new SDL::Event;
40 our %SDL_CB;
41
42 our @GL_INIT; # hooks called on every gl init
43
44 our $ALT_ENTER_MESSAGE;
45
46 our $tw; # Test widget #d#
47
48 sub init_screen {
49 $SDL_APP = new SDL::App
50 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
51 -title => "Crossfire+ Client",
52 -width => $WIDTH,
53 -height => $HEIGHT,
54 -opengl => 1,
55 -red_size => 8,
56 -green_size => 8,
57 -blue_size => 8,
58 -double_buffer => 1,
59 -fullscreen => $FULLSCREEN,
60 -resizeable => 0;
61
62 %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 $FONTSIZE = int $HEIGHT / 50;
68
69 $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 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
73 $ALT_ENTER_MESSAGE->activate;
74
75 # Test code #d#
76 {
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
86 glClearColor 0, 0, 0, 0;
87
88 glEnable GL_TEXTURE_2D;
89 glEnable GL_COLOR_MATERIAL;
90 glShadeModel GL_FLAT;
91 glDisable GL_DEPTH_TEST;
92 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
93
94 $_->() for @GL_INIT;
95 }
96
97 sub start_game {
98 $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
102 1
103 };
104
105 $WIDTH = $CFG->{width};
106 $HEIGHT = $CFG->{height};
107 $FULLSCREEN = 0;
108
109 init_screen;
110
111 $CONN = new conn
112 host => $CFG->{host},
113 port => $CFG->{port},
114 user => $CFG->{user},
115 pass => $CFG->{password},
116 mapw => 50,
117 maph => 37,
118 ;
119 }
120
121 sub stop_game {
122 remove Glib::Source $SDL_TIMER;
123
124 undef $SDL_APP;
125 SDL::Quit;
126 }
127
128
129 sub force_refresh {
130 glViewport 0, 0, $WIDTH, $HEIGHT;
131
132 glMatrixMode GL_PROJECTION;
133 glLoadIdentity;
134 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100;
135 glMatrixMode GL_MODELVIEW;
136
137 glClear GL_COLOR_BUFFER_BIT;
138
139 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
140
141 SDL::GLSwapBuffers;
142 }
143
144 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 %SDL_CB = (
155 SDL_QUIT() => sub {
156 main_quit Gtk2;
157 },
158 SDL_VIDEORESIZE() => sub {
159 },
160 SDL_VIDEOEXPOSE() => sub {
161 refresh;
162 },
163 SDL_KEYDOWN() => sub {
164 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
165 # alt-enter
166 $FULLSCREEN = !$FULLSCREEN;
167 init_screen;
168 } else {
169 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
170 }
171 },
172 SDL_KEYUP() => sub {
173 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
174 },
175 SDL_MOUSEMOTION() => sub {
176 warn "sdl motion\n";#d#
177 },
178 SDL_MOUSEBUTTONDOWN() => sub {
179 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
180 },
181 SDL_MOUSEBUTTONUP() => sub {
182 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
183 },
184 SDL_ACTIVEEVENT() => sub {
185 warn "active\n";#d#
186 },
187 );
188
189 @conn::ISA = Crossfire::Protocol::;
190
191 sub conn::map_update {
192 my ($self, $dirty) = @_;
193
194 refresh;
195 }
196
197 sub conn::map_scroll {
198 my ($self, $dx, $dy) = @_;
199
200 refresh;
201 }
202
203 sub conn::map_clear {
204 my ($self) = @_;
205
206 refresh;
207 }
208
209 sub conn::face_find {
210 my ($self, $face) = @_;
211
212 $FACECACHE->{"$face->{chksum},$face->{name}"}
213 }
214
215 sub conn::face_update {
216 my ($self, $face) = @_;
217
218 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
219
220 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
221 }
222
223 sub conn::query {
224 my ($self, $flags, $prompt) = @_;
225
226 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
227 }
228
229 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 #############################################################################
322
323 SDL::Init SDL_INIT_EVERYTHING;
324
325 my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
326
327 $mapwidget->activate;
328 $mapwidget->focus_in;
329
330 Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
331
332 $CFG ||= {
333 width => 640,
334 height => 480,
335 fullscreen => 0,
336 host => "crossfire.schmorp.de",
337 port => 13327,
338 };
339
340 Crossfire::Client::add_font Crossfire::Client::find_rcfile "uifont.ttf";
341 Crossfire::Client::set_font "[Bitstream Vera Sans Mono]";
342
343 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
344
345 run_config_dialog
346 login => sub { start_game },
347 logout => sub { stop_game };
348
349 main Gtk2;
350
351 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";