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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines