ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.27
Committed: Fri Apr 7 21:07:29 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.26: +4 -5 lines
Log Message:
*** empty log message ***

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::Util;
21     use Crossfire::Client::Widget;
22 elmex 1.10
23 root 1.19 our $FACECACHE;
24    
25 root 1.13 our $VERSION = '0.1';
26 root 1.2
27 elmex 1.10 our $CFG;
28 root 1.13 our $CONN;
29 root 1.2
30 root 1.25 our $UIFONT;
31 root 1.24
32 root 1.13 our $SDL_TIMER;
33     our $SDL_APP;
34     our $SDL_EV = new SDL::Event;
35     our %SDL_CB;
36 root 1.18
37     our @GL_INIT; # hooks called on every gl init
38 root 1.2
39 root 1.13 sub init_screen {
40     $SDL_APP = new SDL::App
41 root 1.5 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
42     -title => "Crossfire+ Client",
43 root 1.13 -width => $CFG->{width},
44     -height => $CFG->{height},
45 root 1.5 -opengl => 1,
46     -red_size => 8,
47     -green_size => 8,
48     -blue_size => 8,
49 root 1.2 -double_buffer => 1,
50 root 1.13 -fullscreen => $CFG->{fullscreen},
51 root 1.5 -resizeable => 0;
52 root 1.2
53 root 1.27 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $CFG->{height} / 40
54     or die "TTFOpenFont: $!";
55    
56 root 1.19 glClearColor 0, 0, 0, 0;
57    
58 root 1.2 glEnable GL_TEXTURE_2D;
59     glShadeModel GL_FLAT;
60     glDisable GL_DEPTH_TEST;
61 root 1.9 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
62 root 1.2
63 root 1.9 glMatrixMode GL_PROJECTION;
64 root 1.4 glLoadIdentity;
65 root 1.24 glOrtho 0, $CFG->{width}, $CFG->{height}, 0, -100 , 100;
66 root 1.19
67     glMatrixMode GL_MODELVIEW;
68 root 1.4
69 root 1.17 $_->() for @GL_INIT;
70 root 1.2 }
71    
72 root 1.24 my $label;#d#
73    
74 root 1.13 sub start_game {
75     $SDL_TIMER = add Glib::Timeout 1000/20, sub {
76     while ($SDL_EV->poll) {
77     ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->();
78     }
79    
80     1
81     };
82    
83     init_screen;
84    
85 root 1.25 $label = new Crossfire::Client::Widget::Label 500, 10, 1, $UIFONT, "Testü[]";
86     $label->activate;
87 root 1.24
88 root 1.13 $CONN = new conn
89     host => $CFG->{host},
90     port => $CFG->{port};
91     }
92    
93     sub stop_game {
94     remove Glib::Source $SDL_TIMER;
95    
96     undef $SDL_APP;
97     SDL::Quit;
98     }
99    
100 root 1.2 sub refresh {
101 root 1.5 glClear GL_COLOR_BUFFER_BIT;
102 root 1.2
103 elmex 1.26 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
104 root 1.1
105 root 1.2 SDL::GLSwapBuffers;
106 root 1.1 }
107    
108 root 1.13 %SDL_CB = (
109     SDL_QUIT() => sub {
110 root 1.19 main_quit Gtk2;
111 root 1.13 },
112     SDL_VIDEORESIZE() => sub {
113     },
114     SDL_VIDEOEXPOSE() => sub {
115     refresh;
116     },
117     SDL_KEYDOWN() => sub {
118 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
119     # alt-enter
120     $CFG->{fullscreen} = !$CFG->{fullscreen};
121     init_screen;
122     } else {
123 root 1.22 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
124 root 1.18 }
125 root 1.13 },
126     SDL_KEYUP() => sub {
127 root 1.22 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
128 root 1.13 },
129     SDL_MOUSEMOTION() => sub {
130     warn "sdl motion\n";#d#
131     },
132     SDL_MOUSEBUTTONDOWN() => sub {
133 root 1.22 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
134 root 1.13 },
135     SDL_MOUSEBUTTONUP() => sub {
136 root 1.22 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
137 root 1.13 },
138     SDL_ACTIVEEVENT() => sub {
139     warn "active\n";#d#
140     },
141     );
142 root 1.1
143 root 1.2 @conn::ISA = Crossfire::Protocol::;
144 root 1.1
145 root 1.2 sub conn::map_update {
146 root 1.1 my ($self, $dirty) = @_;
147    
148 root 1.2 refresh;
149 root 1.1 }
150    
151 root 1.2 sub conn::map_scroll {
152 root 1.1 my ($self, $dx, $dy) = @_;
153    
154 root 1.2 refresh;
155 root 1.1 }
156    
157 root 1.2 sub conn::map_clear {
158 root 1.1 my ($self) = @_;
159    
160 root 1.2 refresh;
161 root 1.1 }
162    
163 root 1.19 sub conn::face_find {
164     my ($self, $face) = @_;
165    
166     $FACECACHE->{"$face->{chksum},$face->{name}"}
167     }
168    
169 root 1.2 sub conn::face_update {
170 root 1.19 my ($self, $face) = @_;
171    
172     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
173 root 1.1
174 root 1.18 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
175 root 1.1 }
176    
177 elmex 1.23 sub gtk_add_cfg_field {
178     my ($tbl, $cfg, $klbl, $key, $value) = @_;
179     my $i = $cfg->{_i}++;
180     $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
181     $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
182     if ($key eq 'password') {
183     $ent->set_invisible_char ("*");
184     $ent->set (visibility => 0)
185     }
186     $ent->set_text ($value);
187     $ent->signal_connect (changed => sub {
188     my ($ent) = @_;
189     $cfg->{$key} = $ent->get_text;
190     });
191     }
192    
193     sub run_config_dialog {
194     my (%events) = @_;
195    
196     my $w = Gtk2::Window->new;
197    
198     my @cfg = (
199     [qw/Host host/],
200     [qw/Port port/],
201     [qw/Username user/],
202     [qw/Password password/],
203     );
204    
205     my $cfg = {};
206    
207     my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
208     my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
209    
210     $w->add (my $vb = Gtk2::VBox->new);
211     $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
212     my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
213     $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
214     $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
215     my $i = 0;
216     my $act = 0;
217     for (map { "$_->[0]x$_->[1]" } reverse @modes) {
218     if ($_ eq $selmode) { $act = $i }
219     $cb->append_text ($_);
220     $i++;
221     }
222     $cb->set_active ($act);
223     $cb->signal_connect (changed => sub {
224     my ($cb) = @_;
225     my $txt = $cb->get_active_text;
226     if ($txt =~ m/(\d+)x(\d+)/) {
227     $::CFG->{width} = $1;
228     $::CFG->{height} = $2;
229     }
230     });
231    
232     $cfg->{_i} = 1;
233     for (@cfg) {
234     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
235     }
236    
237     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
238     $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
239     $cb->signal_connect (clicked => sub {
240     for (keys %$cfg) {
241     $::CFG->{$_} = $cfg->{$_}
242     if $_ ne '_i';
243     }
244     Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
245     });
246     $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
247     $cb->signal_connect (clicked => sub {
248     for (keys %$cfg) {
249     $::CFG->{$_} = $cfg->{$_}
250     if $_ ne '_i';
251     }
252     my $cb = $events{login} || sub {};
253     $cb->($::CFG->{user}, $::CFG->{password});
254     });
255     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
256     $cb->signal_connect (clicked => sub {
257     my $cb = $events{login} || sub {};
258     $cb->();
259     });
260     $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
261     $cb->signal_connect (clicked => sub { $w->destroy });
262    
263     $w->show_all;
264    
265     $w->signal_connect (destroy => sub { Gtk2->main_quit });
266     }
267    
268    
269 root 1.1 #############################################################################
270    
271 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
272 root 1.27 SDL::TTFInit;
273 elmex 1.20
274     my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
275    
276 elmex 1.16 $mapwidget->activate;
277     $mapwidget->focus_in;
278    
279 root 1.22 Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
280 elmex 1.10
281 root 1.13 $CFG ||= {
282     width => 640,
283     height => 480,
284     fullscreen => 0,
285     host => "crossfire.schmorp.de",
286     port => 13327,
287     };
288 elmex 1.12
289 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
290    
291 elmex 1.23 run_config_dialog
292 elmex 1.16 login => sub { start_game },
293     logout => sub { stop_game };
294 root 1.1
295 root 1.13 main Gtk2;
296 root 1.19
297     Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";