ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.38
Committed: Sun Apr 9 00:06:10 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.37: +0 -4 lines
Log Message:
use pango for rendering, instead of broken sdl_ttf

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