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.35 by root, Sat Apr 8 18:48:35 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; # 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 $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, 0, 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, 0);
80 $tw->activate;
81 # Test code end #d#
82
83 glClearColor 0, 0, 0, 0;
84
40 glEnable GL_TEXTURE_2D; 85 glEnable GL_TEXTURE_2D;
41# glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
42 glShadeModel GL_FLAT; 86 glShadeModel GL_FLAT;
43 glDisable GL_DEPTH_TEST; 87 glDisable GL_DEPTH_TEST;
88 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
89
90 $_->() for @GL_INIT;
91}
92
93sub start_game {
94 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
95 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
96 while $SDL_EV->poll;
97
98 1
99 };
100
101 $WIDTH = $CFG->{width};
102 $HEIGHT = $CFG->{height};
103 $FULLSCREEN = 0;
104
105 init_screen;
106
107 $CONN = new conn
108 host => $CFG->{host},
109 port => $CFG->{port},
110 user => $CFG->{user},
111 pass => $CFG->{password};
112}
113
114sub stop_game {
115 remove Glib::Source $SDL_TIMER;
116
117 undef $SDL_APP;
118 SDL::Quit;
119}
120
121
122sub force_refresh {
123 glViewport 0, 0, $WIDTH, $HEIGHT;
124
44 glMatrixMode GL_PROJECTION; 125 glMatrixMode GL_PROJECTION;
45 glBlendFunc GL_SRC_ALPHA, GL_ZERO;
46 glEnable GL_BLEND;
47
48 glLoadIdentity; 126 glLoadIdentity;
49 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; 127 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100;
128 glMatrixMode GL_MODELVIEW;
50 129
51 # re-bind all textures 130 glClear GL_COLOR_BUFFER_BIT;
131
132 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
133
134 SDL::GLSwapBuffers;
52} 135}
136
137my $refresh_handler;
53 138
54sub refresh { 139sub refresh {
55 glClearColor 0, 0, 0, 0; 140 $refresh_handler ||= add Glib::Idle sub {
56 glClear GL_COLOR_BUFFER_BIT; 141 force_refresh;
142 undef $refresh_handler;
143 0
144 };
145}
57 146
58 my $map = $conn->{map}; 147%SDL_CB = (
59 148 SDL_QUIT() => sub {
60 for my $x (0 .. $conn->{mapw} - 1) { 149 main_quit Gtk2;
61 for my $y (0 .. $conn->{maph} - 1) { 150 },
62 151 SDL_VIDEORESIZE() => sub {
63 my $cell = $map->[$x][$y] 152 },
64 or next; 153 SDL_VIDEOEXPOSE() => sub {
65 154 refresh;
66# my $darkness = $cell->[3] * (1 / 255); 155 },
67# glColor $darkness, $darkness, $darkness; 156 SDL_KEYDOWN() => sub {
68 157 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
69 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 158 # alt-enter
70 my $tex = $conn->{face}[$num]{texture} || 0; 159 $FULLSCREEN = !$FULLSCREEN;
71 160 init_screen;
72 glBindTexture GL_TEXTURE_2D, $tex; 161 } else {
73 162 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 } 163 }
82 } 164 },
83 165 SDL_KEYUP() => sub {
84 SDL::GLSwapBuffers; 166 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
85} 167 },
86 168 SDL_MOUSEMOTION() => sub {
87my $ev = new SDL::Event; 169 warn "sdl motion\n";#d#
88my %ev_cb; 170 },
89 171 SDL_MOUSEBUTTONDOWN() => sub {
90sub event(&$) { 172 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
91 $ev_cb{$_[0]->()} = $_[1]; 173 },
92} 174 SDL_MOUSEBUTTONUP() => sub {
93 175 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
94sub does(&) { shift } 176 },
95 177 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"; 178 warn "active\n";#d#
130}; 179 },
131 180);
132package Crossfire::Client;
133 181
134@conn::ISA = Crossfire::Protocol::; 182@conn::ISA = Crossfire::Protocol::;
135 183
136sub conn::map_update { 184sub conn::map_update {
137 my ($self, $dirty) = @_; 185 my ($self, $dirty) = @_;
149 my ($self) = @_; 197 my ($self) = @_;
150 198
151 refresh; 199 refresh;
152} 200}
153 201
202sub conn::face_find {
203 my ($self, $face) = @_;
204
205 $FACECACHE->{"$face->{chksum},$face->{name}"}
206}
207
154sub conn::face_update { 208sub conn::face_update {
155 my ($self, $num, $face) = @_; 209 my ($self, $face) = @_;
156 210
157 warn "up face $self,$num,$face\n";#d# 211 $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 212
163 my $surface = new SDL::Surface -name => "/tmp/x~"; 213 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
214}
164 215
165 unlink "/tmp/x~"; 216sub conn::query {
217 my ($self, $flags, $prompt) = @_;
166 218
167 my ($tex) = @{glGenTextures 1}; 219 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
168 glGetError(); 220}
169 221
170 $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/],
171 248 );
172 glBindTexture GL_TEXTURE_2D, $tex; 249
173 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;
174 274 }
175 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 275 });
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 276
182 glTexImage2D GL_TEXTURE_2D, 0, 277 $cfg->{_i} = 1;
183 4, # components 278 for (@cfg) {
184 $surface->width, $surface->height, 279 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
280 }
281
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';
185 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';
186 GL_RGBA, 296 }
187 GL_UNSIGNED_BYTE, 297 my $cb = $events{login} || sub {};
188 $surface->pixels; 298 $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; 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 });
190} 311}
312
191 313
192############################################################################# 314#############################################################################
193 315
194use Event; 316SDL::Init SDL_INIT_EVERYTHING;
317SDL::TTFInit;
195 318
196glinit; 319my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
197 320
198$conn = new conn 321$mapwidget->activate;
322$mapwidget->focus_in;
323
324Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
325
326$CFG ||= {
327 width => 640,
328 height => 480,
329 fullscreen => 0,
199 host => "cf.schmorp.de", 330 host => "crossfire.schmorp.de",
200 port => 13327; 331 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}); 332};
207 333
208Event::loop; 334$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
209 335
336run_config_dialog
337 login => sub { start_game },
338 logout => sub { stop_game };
210 339
340main Gtk2;
341
342Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines