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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines