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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines