ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.43
Committed: Sun Apr 9 01:40:32 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.42: +3 -3 lines
Log Message:
some fiddling with testing code

File Contents

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