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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines