ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.26
Committed: Fri Apr 7 20:57:29 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.25: +1 -1 lines
Log Message:
added z ordering

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