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.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
29 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 50 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
30 -title => "Crossfire+ Client", 51 -title => "Crossfire+ Client",
31 -width => $WIDTH, 52 -width => $WIDTH,
32 -height => $HEIGHT, 53 -height => $HEIGHT,
33 -opengl => 1, 54 -opengl => 1,
34 -red_size => 8, 55 -red_size => 8,
35 -green_size => 8, 56 -green_size => 8,
36 -blue_size => 8, 57 -blue_size => 8,
37 -double_buffer => 1, 58 -double_buffer => 1,
59 -fullscreen => $FULLSCREEN,
38 -resizeable => 0; 60 -resizeable => 0;
39 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;
85
40 glEnable GL_TEXTURE_2D; 86 glEnable GL_TEXTURE_2D;
41# glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 87 glEnable GL_COLOR_MATERIAL;
42 glShadeModel GL_FLAT; 88 glShadeModel GL_FLAT;
43 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
44 glMatrixMode GL_PROJECTION; 130 glMatrixMode GL_PROJECTION;
45 glBlendFunc GL_SRC_ALPHA, GL_ZERO;
46 glEnable GL_BLEND;
47
48 glLoadIdentity; 131 glLoadIdentity;
49 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; 132 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100;
133 glMatrixMode GL_MODELVIEW;
50 134
51 # re-bind all textures 135 glClear GL_COLOR_BUFFER_BIT;
136
137 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
138
139 SDL::GLSwapBuffers;
52} 140}
141
142my $refresh_handler;
53 143
54sub refresh { 144sub refresh {
55 glClearColor 0, 0, 0, 0; 145 $refresh_handler ||= add Glib::Idle sub {
56 glClear GL_COLOR_BUFFER_BIT; 146 force_refresh;
147 undef $refresh_handler;
148 0
149 };
150}
57 151
58 my $map = $conn->{map}; 152%SDL_CB = (
59 153 SDL_QUIT() => sub {
60 for my $x (0 .. $conn->{mapw} - 1) { 154 main_quit Gtk2;
61 for my $y (0 .. $conn->{maph} - 1) { 155 },
62 156 SDL_VIDEORESIZE() => sub {
63 my $cell = $map->[$x][$y] 157 },
64 or next; 158 SDL_VIDEOEXPOSE() => sub {
65 159 refresh;
66# my $darkness = $cell->[3] * (1 / 255); 160 },
67# glColor $darkness, $darkness, $darkness; 161 SDL_KEYDOWN() => sub {
68 162 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
69 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 163 # alt-enter
70 my $tex = $conn->{face}[$num]{texture} || 0; 164 $FULLSCREEN = !$FULLSCREEN;
71 165 init_screen;
72 glBindTexture GL_TEXTURE_2D, $tex; 166 } else {
73 167 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
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 }
81 } 168 }
82 } 169 },
83 170 SDL_KEYUP() => sub {
84 SDL::GLSwapBuffers; 171 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
85} 172 },
86 173 SDL_MOUSEMOTION() => sub {
87my $ev = new SDL::Event; 174 warn "sdl motion\n";#d#
88my %ev_cb; 175 },
89 176 SDL_MOUSEBUTTONDOWN() => sub {
90sub event(&$) { 177 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
91 $ev_cb{$_[0]->()} = $_[1]; 178 },
92} 179 SDL_MOUSEBUTTONUP() => sub {
93 180 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
94sub does(&) { shift } 181 },
95 182 SDL_ACTIVEEVENT() => sub {
96event {SDL_QUIT} does {
97 exit;
98};
99
100event {SDL_VIDEORESIZE} does {
101 print "resize\n";
102};
103
104event {SDL_VIDEOEXPOSE} does {
105 refresh;
106};
107
108event {SDL_KEYDOWN} does {
109 print "keypress\n";
110};
111
112event {SDL_KEYUP} does {
113 print "keyup\n";#d#
114};
115
116event {SDL_MOUSEMOTION} does {
117 print "motion\n";
118};
119
120event {SDL_MOUSEBUTTONDOWN} does {
121 print "button\n";
122};
123
124event {SDL_MOUSEBUTTONUP} does {
125 print "buttup\n";
126};
127
128event {SDL_ACTIVEEVENT} does {
129 print "active\n"; 183 warn "active\n";#d#
130}; 184 },
131 185);
132package Crossfire::Client;
133 186
134@conn::ISA = Crossfire::Protocol::; 187@conn::ISA = Crossfire::Protocol::;
135 188
136sub conn::map_update { 189sub conn::map_update {
137 my ($self, $dirty) = @_; 190 my ($self, $dirty) = @_;
149 my ($self) = @_; 202 my ($self) = @_;
150 203
151 refresh; 204 refresh;
152} 205}
153 206
207sub conn::face_find {
208 my ($self, $face) = @_;
209
210 $FACECACHE->{"$face->{chksum},$face->{name}"}
211}
212
154sub conn::face_update { 213sub conn::face_update {
155 my ($self, $num, $face) = @_; 214 my ($self, $face) = @_;
156 215
157 warn "up face $self,$num,$face\n";#d# 216 $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 217
163 my $surface = new SDL::Surface -name => "/tmp/x~"; 218 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
219}
164 220
165 unlink "/tmp/x~"; 221sub conn::query {
222 my ($self, $flags, $prompt) = @_;
166 223
167 my ($tex) = @{glGenTextures 1}; 224 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
168 glGetError(); 225}
169 226
170 $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/],
171 253 );
172 glBindTexture GL_TEXTURE_2D, $tex; 254
173 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;
174 279 }
175 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 280 });
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 281
182 glTexImage2D GL_TEXTURE_2D, 0, 282 $cfg->{_i} = 1;
183 4, # components 283 for (@cfg) {
184 $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';
185 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';
186 GL_RGBA, 301 }
187 GL_UNSIGNED_BYTE, 302 my $cb = $events{login} || sub {};
188 $surface->pixels; 303 $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; 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 });
190} 316}
317
191 318
192############################################################################# 319#############################################################################
193 320
194use Event; 321SDL::Init SDL_INIT_EVERYTHING;
195 322
196glinit; 323my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
197 324
198$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,
199 host => "cf.schmorp.de", 334 host => "crossfire.schmorp.de",
200 port => 13327; 335 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}); 336};
207 337
208Event::loop; 338$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
209 339
340run_config_dialog
341 login => sub { start_game },
342 logout => sub { stop_game };
210 343
344main Gtk2;
345
346Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines