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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines