ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.62
Committed: Mon Apr 10 19:34:04 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.61: +17 -21 lines
Log Message:
preliminary support for older opengl implementations (NPOT vs. POT)

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::Protocol;
18    
19 root 1.58 use Crossfire::Client;
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 elmex 1.10 our $CFG;
27 root 1.13 our $CONN;
28 root 1.2
29 root 1.30 our $WIDTH;
30     our $HEIGHT;
31     our $FULLSCREEN;
32    
33 root 1.39 our $FONTSIZE;
34 root 1.57
35 root 1.13 our $SDL_TIMER;
36     our $SDL_APP;
37 root 1.58 our $SDL_EV;
38 root 1.13 our %SDL_CB;
39 root 1.18
40 root 1.30 our $ALT_ENTER_MESSAGE;
41 root 1.51 our $STATUS_LINE;
42 root 1.30
43 root 1.45 my $last_refresh;
44 root 1.50 my %ANIMATE;
45     my $refresh_handler;
46 root 1.45
47 root 1.58 our ($tw, $te); # Test widget #d#
48    
49 root 1.13 sub init_screen {
50     $SDL_APP = new SDL::App
51 root 1.5 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
52     -title => "Crossfire+ Client",
53 root 1.30 -width => $WIDTH,
54     -height => $HEIGHT,
55 root 1.5 -opengl => 1,
56 root 1.51 -red_size => 5,
57     -green_size => 5,
58     -blue_size => 5,
59     -alpha_size => 0,
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.58 $SDL_EV = new SDL::Event;
65     $SDL_EV->set_unicode (1);
66    
67 root 1.62 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
68     ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
69     while $SDL_EV->poll;
70    
71     1
72     };
73    
74 root 1.45 $last_refresh = SDL::GetTicks;
75    
76 root 1.62 Crossfire::Client::gl_init;
77 root 1.30
78 root 1.40 $FONTSIZE = int $HEIGHT / 50;
79 root 1.39
80 root 1.52 #############################################################################
81    
82 root 1.62 glClearColor 0.45, 0.45, 0.45, 1;
83 root 1.52
84     glEnable GL_TEXTURE_2D;
85     glEnable GL_COLOR_MATERIAL;
86     glShadeModel GL_FLAT;
87     glDisable GL_DEPTH_TEST;
88     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
89    
90     #############################################################################
91    
92 root 1.51 $STATUS_LINE = new Crossfire::Client::Widget::Label
93     0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
94     "";
95 root 1.58 $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE);
96 root 1.51
97 root 1.40 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
98 root 1.51 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
99 root 1.40 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
100 root 1.58 $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
101 root 1.30
102 elmex 1.34 # Test code #d#
103 elmex 1.43 unless ($tw) { # haha...
104 root 1.58 $te = new Crossfire::Client::Widget::FancyFrame;
105     $te->add (new Crossfire::Client::Widget::Entry);
106     $te->move (300, 0, 2);
107     $Crossfire::Client::Widget::TOPLEVEL->add ($te);
108    
109 root 1.45 $tw = new Crossfire::Client::Widget::Animator;
110 elmex 1.46 my $lbl1 = new Crossfire::Client::Widget::Label
111     0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
112     my $lbl2 = new Crossfire::Client::Widget::Label
113     0, 0, 10, $FONTSIZE, "LBL2";
114    
115     my $vb = new Crossfire::Client::Widget::VBox;
116     my $f = new Crossfire::Client::Widget::FancyFrame;
117     my $f2 = new Crossfire::Client::Widget::FancyFrame;
118     $f->add ($lbl1);
119     $f2->add ($lbl2);
120     $vb->add ($f);
121     $vb->add ($f2, 1);
122    
123     $tw->add ($vb);
124     $tw->w (400);
125     $tw->h (300);
126 elmex 1.44 $tw->move ($WIDTH - 200, 0);
127 root 1.45 $tw->moveto (0, 0);
128 root 1.58 $Crossfire::Client::Widget::TOPLEVEL->add ($tw);
129 elmex 1.46
130     # $f->move ($WIDTH - 200, 0);
131 root 1.58 # $Crossfire::Client::Widget::TOPLEVEL->add ($f);
132 root 1.39 }
133 root 1.2 }
134    
135 root 1.62 sub destroy_screen {
136     remove Glib::Source $SDL_TIMER;
137     undef $SDL_APP;
138     undef $SDL_EV;
139     SDL::Quit;
140     }
141    
142 root 1.13 sub start_game {
143 root 1.30 $WIDTH = $CFG->{width};
144     $HEIGHT = $CFG->{height};
145     $FULLSCREEN = 0;
146    
147 root 1.13 init_screen;
148    
149 root 1.59 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
150 root 1.50
151 root 1.13 $CONN = new conn
152     host => $CFG->{host},
153 root 1.28 port => $CFG->{port},
154     user => $CFG->{user},
155 root 1.37 pass => $CFG->{password},
156 root 1.50 mapw => $mapsize,
157     maph => $mapsize,
158 root 1.37 ;
159 root 1.45
160     Crossfire::Client::lowdelay fileno $CONN->{fh};
161 root 1.13 }
162    
163     sub stop_game {
164 root 1.51 remove Glib::Source $refresh_handler if $refresh_handler;
165 root 1.50 undef $refresh_handler;
166 root 1.13
167 root 1.50 undef $CONN;
168 root 1.62 destroy_screen;
169 root 1.13 }
170    
171 root 1.30 sub force_refresh {
172     glViewport 0, 0, $WIDTH, $HEIGHT;
173 root 1.35
174     glMatrixMode GL_PROJECTION;
175     glLoadIdentity;
176 root 1.59 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
177 root 1.35 glMatrixMode GL_MODELVIEW;
178    
179 root 1.5 glClear GL_COLOR_BUFFER_BIT;
180 root 1.2
181 root 1.58 $Crossfire::Client::Widget::TOPLEVEL->draw;
182 root 1.1
183 root 1.2 SDL::GLSwapBuffers;
184 root 1.1 }
185    
186 root 1.30 sub refresh {
187     $refresh_handler ||= add Glib::Idle sub {
188 root 1.58 if ($SDL_APP) {
189     my $next_refresh = SDL::GetTicks;
190     my $interval = ($next_refresh - $last_refresh) * 0.001;
191     $last_refresh = $next_refresh;
192    
193     force_refresh;
194     $_->animate ($interval) for grep $_, values %ANIMATE;
195    
196     if (%ANIMATE) {
197     1
198     } else {
199     undef $refresh_handler;
200     0
201     }
202 root 1.45 } else {
203     undef $refresh_handler;
204     0
205     }
206 root 1.30 };
207     }
208    
209 root 1.45 sub animation_start {
210     my ($widget) = @_;
211     $ANIMATE{$widget} = $widget;
212     Scalar::Util::weaken $ANIMATE{$widget};
213    
214     refresh;
215     }
216    
217     sub animation_stop {
218     my ($widget) = @_;
219     delete $ANIMATE{$widget};
220     }
221    
222 root 1.13 %SDL_CB = (
223     SDL_QUIT() => sub {
224 root 1.19 main_quit Gtk2;
225 root 1.13 },
226     SDL_VIDEORESIZE() => sub {
227     },
228     SDL_VIDEOEXPOSE() => sub {
229     refresh;
230     },
231     SDL_KEYDOWN() => sub {
232 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
233     # alt-enter
234 root 1.30 $FULLSCREEN = !$FULLSCREEN;
235 root 1.18 init_screen;
236     } else {
237 root 1.22 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
238 root 1.18 }
239 root 1.13 },
240     SDL_KEYUP() => sub {
241 root 1.22 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
242 root 1.13 },
243     SDL_MOUSEMOTION() => sub {
244 root 1.58 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
245 root 1.13 },
246     SDL_MOUSEBUTTONDOWN() => sub {
247 root 1.22 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
248 root 1.13 },
249     SDL_MOUSEBUTTONUP() => sub {
250 root 1.22 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
251 root 1.13 },
252     SDL_ACTIVEEVENT() => sub {
253 root 1.58 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
254 root 1.13 },
255     );
256 root 1.1
257 root 1.2 @conn::ISA = Crossfire::Protocol::;
258 root 1.1
259 root 1.2 sub conn::map_update {
260 root 1.1 my ($self, $dirty) = @_;
261    
262 root 1.2 refresh;
263 root 1.1 }
264    
265 root 1.2 sub conn::map_scroll {
266 root 1.1 my ($self, $dx, $dy) = @_;
267    
268 root 1.45 # refresh;
269 root 1.1 }
270    
271 root 1.2 sub conn::map_clear {
272 root 1.1 my ($self) = @_;
273    
274 root 1.45 # refresh;
275 root 1.1 }
276    
277 root 1.19 sub conn::face_find {
278     my ($self, $face) = @_;
279    
280     $FACECACHE->{"$face->{chksum},$face->{name}"}
281     }
282    
283 root 1.2 sub conn::face_update {
284 root 1.19 my ($self, $face) = @_;
285    
286     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
287 root 1.1
288 root 1.18 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
289 root 1.1 }
290    
291 root 1.33 sub conn::query {
292     my ($self, $flags, $prompt) = @_;
293    
294     warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
295     }
296    
297 elmex 1.23 sub gtk_add_cfg_field {
298     my ($tbl, $cfg, $klbl, $key, $value) = @_;
299     my $i = $cfg->{_i}++;
300     $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
301     $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
302     if ($key eq 'password') {
303     $ent->set_invisible_char ("*");
304     $ent->set (visibility => 0)
305     }
306     $ent->set_text ($value);
307     $ent->signal_connect (changed => sub {
308     my ($ent) = @_;
309     $cfg->{$key} = $ent->get_text;
310 elmex 1.49 # TODO: Mapsize should be a slider in the game gui
311 elmex 1.47 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
312     $cfg->{$key} = 100;
313 elmex 1.48 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
314     $cfg->{$key} = 50;
315 elmex 1.47 }
316 elmex 1.23 });
317     }
318    
319     sub run_config_dialog {
320     my (%events) = @_;
321    
322     my $w = Gtk2::Window->new;
323    
324     my @cfg = (
325     [qw/Host host/],
326     [qw/Port port/],
327 elmex 1.47 [qw/Mapsize% mapsize/],
328 elmex 1.23 [qw/Username user/],
329     [qw/Password password/],
330     );
331    
332     my $cfg = {};
333    
334     my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
335     my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
336    
337     $w->add (my $vb = Gtk2::VBox->new);
338     $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
339     my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
340     $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
341     $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
342     my $i = 0;
343     my $act = 0;
344     for (map { "$_->[0]x$_->[1]" } reverse @modes) {
345     if ($_ eq $selmode) { $act = $i }
346     $cb->append_text ($_);
347     $i++;
348     }
349     $cb->set_active ($act);
350     $cb->signal_connect (changed => sub {
351     my ($cb) = @_;
352     my $txt = $cb->get_active_text;
353     if ($txt =~ m/(\d+)x(\d+)/) {
354     $::CFG->{width} = $1;
355     $::CFG->{height} = $2;
356     }
357     });
358    
359     $cfg->{_i} = 1;
360     for (@cfg) {
361     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
362     }
363    
364     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
365     $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
366     $cb->signal_connect (clicked => sub {
367     for (keys %$cfg) {
368     $::CFG->{$_} = $cfg->{$_}
369     if $_ ne '_i';
370     }
371     Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
372     });
373     $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
374     $cb->signal_connect (clicked => sub {
375     for (keys %$cfg) {
376     $::CFG->{$_} = $cfg->{$_}
377     if $_ ne '_i';
378     }
379     my $cb = $events{login} || sub {};
380     $cb->($::CFG->{user}, $::CFG->{password});
381     });
382     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
383     $cb->signal_connect (clicked => sub {
384 root 1.50 my $cb = $events{logout} || sub {};
385 elmex 1.23 $cb->();
386     });
387     $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
388     $cb->signal_connect (clicked => sub { $w->destroy });
389    
390     $w->show_all;
391    
392     $w->signal_connect (destroy => sub { Gtk2->main_quit });
393     }
394    
395    
396 root 1.1 #############################################################################
397    
398 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
399 elmex 1.20
400     my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
401    
402 root 1.58 $Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
403 elmex 1.16 $mapwidget->focus_in;
404    
405 root 1.22 Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
406 elmex 1.10
407 root 1.13 $CFG ||= {
408     width => 640,
409     height => 480,
410 elmex 1.47 mapsize => 100,
411 root 1.13 fullscreen => 0,
412     host => "crossfire.schmorp.de",
413     port => 13327,
414     };
415 elmex 1.12
416 root 1.42 Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
417 root 1.40
418 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
419    
420 elmex 1.23 run_config_dialog
421 elmex 1.16 login => sub { start_game },
422     logout => sub { stop_game };
423 root 1.1
424 root 1.13 main Gtk2;
425 root 1.19
426     Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";