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.12 by elmex, Fri Apr 7 16:29:19 2006 UTC vs.
Revision 1.52 by root, Sun Apr 9 22:12:12 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines