ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.2 by root, Thu Apr 6 20:00:23 2006 UTC vs.
Revision 1.53 by root, Sun Apr 9 22:17:40 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8;
4 5
5use Crossfire::Client; 6use Glib;
6use Crossfire::Protocol; 7use Gtk2 -init;
7
8package Crossfire::Client; # uh, yeah
9
10use strict;
11 8
12use SDL; 9use SDL;
13use SDL::App; 10use SDL::App;
14use SDL::Event; 11use SDL::Event;
15use SDL::Surface; 12use SDL::Surface;
16use SDL::OpenGL; 13use SDL::OpenGL;
17use SDL::OpenGL::Constants; 14use SDL::OpenGL::Constants;
18 15
19my $conn; 16use Crossfire;
20my $app; 17use Crossfire::Client;
18use Crossfire::Protocol;
21 19
22my $WIDTH = 640; 20use Crossfire::Client::Widget;
23my $HEIGHT = 480;
24 21
25sub glinit { 22our $FACECACHE;
26 # nuke all gl context data 23
27 24our $VERSION = '0.1';
25
26our %GL_EXT;
27
28our $CFG;
29our $CONN;
30
31our $WIDTH;
32our $HEIGHT;
33our $FULLSCREEN;
34
35our $FONTSIZE;
36
37our $SDL_TIMER;
38our $SDL_APP;
39our $SDL_EV = new SDL::Event;
40our %SDL_CB;
41
42our @GL_INIT; # hooks called on every gl init
43
44our $ALT_ENTER_MESSAGE;
45our $STATUS_LINE;
46
47our $TOPLEVEL;
48
49our $tw; # Test widget #d#
50
51my $last_refresh;
52my %ANIMATE;
53my $refresh_handler;
54
55sub init_screen {
28 $app = new SDL::App 56 $SDL_APP = new SDL::App
57 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
29 -title => "Crossfire+ Client", 58 -title => "Crossfire+ Client",
30 -width => $WIDTH, 59 -width => $WIDTH,
31 -height => $HEIGHT, 60 -height => $HEIGHT,
32 -depth => 24,
33 -opengl => 1, 61 -opengl => 1,
62 -red_size => 5,
63 -green_size => 5,
64 -blue_size => 5,
65 -alpha_size => 0,
34 -double_buffer => 1, 66 -double_buffer => 1,
67 -fullscreen => $FULLSCREEN,
35 -resizeable => 0; 68 -resizeable => 0;
69
70 $last_refresh = SDL::GetTicks;
71
72 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions;
73
74 $GL_EXT{GL_ARB_texture_non_power_of_two}
75 or warn "WARNING: non-power-of-two opengl extension required";
76
77 $FONTSIZE = int $HEIGHT / 50;
78
79 #############################################################################
80
81 glClearColor 0, 0, 0, 0;
36 82
37 glEnable GL_TEXTURE_2D; 83 glEnable GL_TEXTURE_2D;
84 glEnable GL_COLOR_MATERIAL;
38 glShadeModel GL_FLAT; 85 glShadeModel GL_FLAT;
39 glDisable GL_DEPTH_TEST; 86 glDisable GL_DEPTH_TEST;
87 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
88
89 $_->() for @GL_INIT;
90
91 #############################################################################
92
93 $STATUS_LINE = new Crossfire::Client::Widget::Label
94 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
95 "";
96 $TOPLEVEL->add ($STATUS_LINE);
97
98 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
99 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
100 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
101 $TOPLEVEL->add ($ALT_ENTER_MESSAGE);
102
103 # Test code #d#
104 unless ($tw) { # haha...
105 $tw = new Crossfire::Client::Widget::Animator;
106 my $lbl1 = new Crossfire::Client::Widget::Label
107 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
108 my $lbl2 = new Crossfire::Client::Widget::Label
109 0, 0, 10, $FONTSIZE, "LBL2";
110
111 my $vb = new Crossfire::Client::Widget::VBox;
112 my $f = new Crossfire::Client::Widget::FancyFrame;
113 my $f2 = new Crossfire::Client::Widget::FancyFrame;
114 $f->add ($lbl1);
115 $f2->add ($lbl2);
116 $vb->add ($f);
117 $vb->add ($f2, 1);
118
119 $tw->add ($vb);
120 $tw->w (400);
121 $tw->h (300);
122 $tw->move ($WIDTH - 200, 0);
123 $tw->moveto (0, 0);
124 $TOPLEVEL->add ($tw);
125
126# $f->move ($WIDTH - 200, 0);
127# $TOPLEVEL->add ($f);
128 }
129}
130
131sub start_game {
132 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
133 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
134 while $SDL_EV->poll;
135
136 1
137 };
138
139 $WIDTH = $CFG->{width};
140 $HEIGHT = $CFG->{height};
141 $FULLSCREEN = 0;
142
143 init_screen;
144
145 my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32;
146
147 $CONN = new conn
148 host => $CFG->{host},
149 port => $CFG->{port},
150 user => $CFG->{user},
151 pass => $CFG->{password},
152 mapw => $mapsize,
153 maph => $mapsize,
154 ;
155
156 Crossfire::Client::lowdelay fileno $CONN->{fh};
157}
158
159sub stop_game {
160 remove Glib::Source $SDL_TIMER;
161 remove Glib::Source $refresh_handler if $refresh_handler;
162 undef $refresh_handler;
163
164 undef $SDL_APP;
165 undef $CONN;
166 SDL::Quit;
167}
168
169
170sub force_refresh {
171 glViewport 0, 0, $WIDTH, $HEIGHT;
172
40 glMatrixMode GL_PROJECTION; 173 glMatrixMode GL_PROJECTION;
174 glLoadIdentity;
175 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000;
176 glMatrixMode GL_MODELVIEW;
41 177
42 #glViewport 0, 0, $WIDTH, $HEIGHT; 178 glClear GL_COLOR_BUFFER_BIT;
43 # re-bind all textures 179
180 $TOPLEVEL->draw;
181
182 SDL::GLSwapBuffers;
44} 183}
45 184
46sub refresh { 185sub refresh {
47 glClearColor 0.5, 0.5, 0.7, 0; 186 $refresh_handler ||= add Glib::Idle sub {
48 glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; 187 return unless $SDL_APP;
49 188
50 glLoadIdentity; 189 my $next_refresh = SDL::GetTicks;
51 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; 190 my $interval = ($next_refresh - $last_refresh) * 0.001;
191 $last_refresh = $next_refresh;
52 192
53 my $map = $conn->{map}; 193 force_refresh;
194 $_->animate ($interval) for grep $_, values %ANIMATE;
54 195
55 for my $x (0 .. $conn->{mapw} - 1) { 196 if (%ANIMATE) {
56 for my $y (0 .. $conn->{maph} - 1) {
57
58 my $cell = $map->[$x][$y]
59 or next;
60
61 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
62 my $tex = $conn->{face}[$num]{texture} ||= do {
63 $conn->send ("askface $num") unless $conn->{face}[$num]{askface}++;
64
65 0
66
67 };
68
69 glBindTexture GL_TEXTURE_2D, $tex;
70
71 glBegin GL_QUADS;
72 glTexCoord 0, 0; glVertex $x, $y;
73 glTexCoord 1, 0; glVertex $x + 1, $y;
74 glTexCoord 1, 1; glVertex $x + 1, $y + 1;
75 glTexCoord 0, 1; glVertex $x, $y + 1;
76 glEnd;
77 } 197 1
198 } else {
199 undef $refresh_handler;
200 0
78 } 201 }
79 } 202 };
80
81 SDL::GLSwapBuffers;
82} 203}
83 204
84my $ev = new SDL::Event; 205sub animation_start {
85my %ev_cb; 206 my ($widget) = @_;
207 $ANIMATE{$widget} = $widget;
208 Scalar::Util::weaken $ANIMATE{$widget};
86 209
87sub event(&$) { 210 refresh;
88 $ev_cb{$_[0]->()} = $_[1];
89} 211}
90 212
91sub does(&) { shift } 213sub animation_stop {
214 my ($widget) = @_;
215 delete $ANIMATE{$widget};
216}
92 217
93event {SDL_QUIT} does { 218%SDL_CB = (
94 exit; 219 SDL_QUIT() => sub {
95}; 220 main_quit Gtk2;
221 },
222 SDL_VIDEORESIZE() => sub {
223 },
224 SDL_VIDEOEXPOSE() => sub {
225 refresh;
226 },
227 SDL_KEYDOWN() => sub {
228 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
229 # alt-enter
230 $FULLSCREEN = !$FULLSCREEN;
231 init_screen;
232 } else {
233 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
234 }
235 },
236 SDL_KEYUP() => sub {
237 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
238 },
239 SDL_MOUSEMOTION() => sub {
240 my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y);
241 my $widget = $TOPLEVEL->find_widget ($x, $y);
96 242
97event {SDL_VIDEORESIZE} does { 243 warn "mouse $x, $y = $widget\n";
98 print "resize\n"; 244 },
99}; 245 SDL_MOUSEBUTTONDOWN() => sub {
100 246 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
101event {SDL_KEYDOWN} does { 247 },
102 print "keypress\n"; 248 SDL_MOUSEBUTTONUP() => sub {
103}; 249 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
104 250 },
105event {SDL_KEYUP} does { 251 SDL_ACTIVEEVENT() => sub {
106 print "keyup\n";#d#
107};
108
109event {SDL_MOUSEMOTION} does {
110 print "motion\n";
111};
112
113event {SDL_MOUSEBUTTONDOWN} does {
114 print "button\n";
115};
116
117event {SDL_MOUSEBUTTONUP} does {
118 print "buttup\n";
119};
120
121event {SDL_ACTIVEEVENT} does {
122 print "active\n"; 252 warn "active\n";#d#
123}; 253 },
124 254);
125package Crossfire::Client;
126 255
127@conn::ISA = Crossfire::Protocol::; 256@conn::ISA = Crossfire::Protocol::;
128 257
129sub conn::map_update { 258sub conn::map_update {
130 my ($self, $dirty) = @_; 259 my ($self, $dirty) = @_;
133} 262}
134 263
135sub conn::map_scroll { 264sub conn::map_scroll {
136 my ($self, $dx, $dy) = @_; 265 my ($self, $dx, $dy) = @_;
137 266
138 refresh; 267# refresh;
139} 268}
140 269
141sub conn::map_clear { 270sub conn::map_clear {
142 my ($self) = @_; 271 my ($self) = @_;
143 272
144 refresh; 273# refresh;
274}
275
276sub conn::face_find {
277 my ($self, $face) = @_;
278
279 $FACECACHE->{"$face->{chksum},$face->{name}"}
145} 280}
146 281
147sub conn::face_update { 282sub conn::face_update {
148 my ($self, $num, $face) = @_; 283 my ($self, $face) = @_;
149 284
150 warn "up face $self,$num,$face\n";#d# 285 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
151 #TODO
152 open my $fh, ">:raw", "/tmp/x~";
153 syswrite $fh, $face->{image};
154 close $fh;
155 286
156 my $surface = new SDL::Surface -name => "/tmp/x~"; 287 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
288}
157 289
158 unlink "/tmp/x~"; 290sub conn::query {
291 my ($self, $flags, $prompt) = @_;
159 292
160 my ($tex) = @{glGenTextures 1}; 293 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
161 glGetError(); 294}
162 295
163 $face->{texture} = $tex; 296sub gtk_add_cfg_field {
297 my ($tbl, $cfg, $klbl, $key, $value) = @_;
298 my $i = $cfg->{_i}++;
299 $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
300 $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
301 if ($key eq 'password') {
302 $ent->set_invisible_char ("*");
303 $ent->set (visibility => 0)
304 }
305 $ent->set_text ($value);
306 $ent->signal_connect (changed => sub {
307 my ($ent) = @_;
308 $cfg->{$key} = $ent->get_text;
309 # TODO: Mapsize should be a slider in the game gui
310 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
311 $cfg->{$key} = 100;
312 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
313 $cfg->{$key} = 50;
314 }
315 });
316}
317
318sub run_config_dialog {
319 my (%events) = @_;
320
321 my $w = Gtk2::Window->new;
322
323 my @cfg = (
324 [qw/Host host/],
325 [qw/Port port/],
326 [qw/Mapsize% mapsize/],
327 [qw/Username user/],
328 [qw/Password password/],
164 329 );
165 glBindTexture GL_TEXTURE_2D, $tex; 330
166 my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; 331 my $cfg = {};
332
333 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
334 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
335
336 $w->add (my $vb = Gtk2::VBox->new);
337 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
338 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
339 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
340 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
341 my $i = 0;
342 my $act = 0;
343 for (map { "$_->[0]x$_->[1]" } reverse @modes) {
344 if ($_ eq $selmode) { $act = $i }
345 $cb->append_text ($_);
346 $i++;
347 }
348 $cb->set_active ($act);
349 $cb->signal_connect (changed => sub {
350 my ($cb) = @_;
351 my $txt = $cb->get_active_text;
352 if ($txt =~ m/(\d+)x(\d+)/) {
353 $::CFG->{width} = $1;
354 $::CFG->{height} = $2;
167 355 }
168# glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 356 });
169# glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
170
171 $surface->rgba;
172 357
173 glTexImage2D GL_TEXTURE_2D, 0, 358 $cfg->{_i} = 1;
174 4, # components 359 for (@cfg) {
175 $surface->width, $surface->height, 360 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
361 }
362
363 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
364 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
365 $cb->signal_connect (clicked => sub {
366 for (keys %$cfg) {
367 $::CFG->{$_} = $cfg->{$_}
368 if $_ ne '_i';
176 0, 369 }
370 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
371 });
372 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
373 $cb->signal_connect (clicked => sub {
374 for (keys %$cfg) {
375 $::CFG->{$_} = $cfg->{$_}
376 if $_ ne '_i';
177 GL_RGBA, 377 }
178 GL_UNSIGNED_BYTE, 378 my $cb = $events{login} || sub {};
179 $surface->pixels; 379 $cb->($::CFG->{user}, $::CFG->{password});
180 my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; 380 });
381 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
382 $cb->signal_connect (clicked => sub {
383 my $cb = $events{logout} || sub {};
384 $cb->();
385 });
386 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
387 $cb->signal_connect (clicked => sub { $w->destroy });
388
389 $w->show_all;
390
391 $w->signal_connect (destroy => sub { Gtk2->main_quit });
181} 392}
393
182 394
183############################################################################# 395#############################################################################
184 396
185use Event; 397SDL::Init SDL_INIT_EVERYTHING;
186 398
187glinit; 399$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
188 400
189$conn = new conn 401my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
402
403$TOPLEVEL->add ($mapwidget);
404$mapwidget->focus_in;
405
406Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
407
408$CFG ||= {
409 width => 640,
410 height => 480,
411 mapsize => 100,
412 fullscreen => 0,
190 host => "cf.schmorp.de", 413 host => "crossfire.schmorp.de",
191 port => 13327; 414 port => 13327,
192
193Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub {
194 while ($ev->poll) {
195 ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
196 }
197}); 415};
198 416
199Event::loop; 417Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
200 418
419$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
201 420
421run_config_dialog
422 login => sub { start_game },
423 logout => sub { stop_game };
424
425main Gtk2;
426
427Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines