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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines