ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.34
Committed: Sat Apr 8 18:18:09 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.33: +3 -1 lines
Log Message:
debugging Widget::Window

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