ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.23
Committed: Fri Apr 7 20:15:01 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.22: +94 -2 lines
Log Message:
removed Util

File Contents

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