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.14 by elmex, Fri Apr 7 16:36:44 2006 UTC vs.
Revision 1.37 by root, Sat Apr 8 22:25:37 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8;
4 5
5use Glib; 6use Glib;
6use Gtk2 -init; 7use Gtk2 -init;
7 8
8use SDL; 9use SDL;
14 15
15use Crossfire; 16use Crossfire;
16use Crossfire::Client; 17use Crossfire::Client;
17use Crossfire::Protocol; 18use Crossfire::Protocol;
18 19
19use Client::Util; 20use Crossfire::Client::Widget;
21
22our $FACECACHE;
20 23
21our $VERSION = '0.1'; 24our $VERSION = '0.1';
25
26our %GL_EXT;
22 27
23our $CFG; 28our $CFG;
24our $CONN; 29our $CONN;
30
31our $WIDTH;
32our $HEIGHT;
33our $FULLSCREEN;
34
35our $UIFONT;
25 36
26our $SDL_TIMER; 37our $SDL_TIMER;
27our $SDL_APP; 38our $SDL_APP;
28our $SDL_EV = new SDL::Event; 39our $SDL_EV = new SDL::Event;
29our %SDL_CB; 40our %SDL_CB;
30 41
42our @GL_INIT; # hooks called on every gl init
43
44our $ALT_ENTER_MESSAGE;
45
46our $tw; # Test widget #d#
47
31sub init_screen { 48sub init_screen {
32 # nuke all gl context data
33
34 $SDL_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 => $CFG->{width}, 52 -width => $WIDTH,
38 -height => $CFG->{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,
44 -fullscreen => $CFG->{fullscreen}, 59 -fullscreen => $FULLSCREEN,
45 -resizeable => 0; 60 -resizeable => 0;
46 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 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, $UIFONT, "Foo in the garden!");
78 $tw->add ($frm);
79
80 $tw->move (0, $HEIGHT - 120);
81 $tw->activate;
82 # Test code end #d#
83
84 glClearColor 0, 0, 0, 0;
85
47 glEnable GL_TEXTURE_2D; 86 glEnable GL_TEXTURE_2D;
48 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 87 glEnable GL_COLOR_MATERIAL;
49 glShadeModel GL_FLAT; 88 glShadeModel GL_FLAT;
50 glDisable GL_DEPTH_TEST; 89 glDisable GL_DEPTH_TEST;
51 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 90 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
52 glEnable GL_BLEND; 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;
53 129
54 glMatrixMode GL_PROJECTION; 130 glMatrixMode GL_PROJECTION;
55 glLoadIdentity; 131 glLoadIdentity;
56 glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -1 , 1; 132 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100;
133 glMatrixMode GL_MODELVIEW;
57 134
58 # re-bind all textures 135 glClear GL_COLOR_BUFFER_BIT;
59}
60 136
61sub start_game { 137 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
62 $SDL_TIMER = add Glib::Timeout 1000/20, sub { 138
63 while ($SDL_EV->poll) { 139 SDL::GLSwapBuffers;
64 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); 140}
141
142my $refresh_handler;
143
144sub refresh {
145 $refresh_handler ||= add Glib::Idle sub {
146 force_refresh;
147 undef $refresh_handler;
65 } 148 0
66
67 1
68 }; 149 };
69
70 init_screen;
71
72 $CONN = new conn
73 host => $CFG->{host},
74 port => $CFG->{port};
75}
76
77sub stop_game {
78 remove Glib::Source $SDL_TIMER;
79
80 undef $SDL_APP;
81 SDL::Quit;
82}
83
84sub refresh {
85 glClearColor 0, 0, 0, 0;
86 glClear GL_COLOR_BUFFER_BIT;
87
88 my $map = $CONN->{map};
89
90 for my $x (0 .. $CONN->{mapw} - 1) {
91 for my $y (0 .. $CONN->{maph} - 1) {
92
93 my $cell = $map->[$x][$y]
94 or next;
95
96 my $darkness = $cell->[3] * (1 / 255);
97 glColor $darkness, $darkness, $darkness;
98
99 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
100 my $tex = $CONN->{face}[$num]{texture} || 0;
101
102 glBindTexture GL_TEXTURE_2D, $tex;
103
104 glBegin GL_QUADS;
105 glTexCoord 0, 0; glVertex $x, $y;
106 glTexCoord 0, 1; glVertex $x, $y + 1;
107 glTexCoord 1, 1; glVertex $x + 1, $y + 1;
108 glTexCoord 1, 0; glVertex $x + 1, $y;
109 glEnd;
110 }
111 }
112 }
113
114 SDL::GLSwapBuffers;
115} 150}
116 151
117%SDL_CB = ( 152%SDL_CB = (
118 SDL_QUIT() => sub { 153 SDL_QUIT() => sub {
119 warn "sdl quit\n";#d# 154 main_quit Gtk2;
120 exit;
121 }, 155 },
122 SDL_VIDEORESIZE() => sub { 156 SDL_VIDEORESIZE() => sub {
123 }, 157 },
124 SDL_VIDEOEXPOSE() => sub { 158 SDL_VIDEOEXPOSE() => sub {
125 refresh; 159 refresh;
126 }, 160 },
127 SDL_KEYDOWN() => sub { 161 SDL_KEYDOWN() => sub {
162 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
163 # alt-enter
164 $FULLSCREEN = !$FULLSCREEN;
165 init_screen;
166 } else {
167 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
168 }
128 }, 169 },
129 SDL_KEYUP() => sub { 170 SDL_KEYUP() => sub {
171 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
130 }, 172 },
131 SDL_MOUSEMOTION() => sub { 173 SDL_MOUSEMOTION() => sub {
132 warn "sdl motion\n";#d# 174 warn "sdl motion\n";#d#
133 }, 175 },
134 SDL_MOUSEBUTTONDOWN() => sub { 176 SDL_MOUSEBUTTONDOWN() => sub {
177 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
135 }, 178 },
136 SDL_MOUSEBUTTONUP() => sub { 179 SDL_MOUSEBUTTONUP() => sub {
180 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
137 }, 181 },
138 SDL_ACTIVEEVENT() => sub { 182 SDL_ACTIVEEVENT() => sub {
139 warn "active\n";#d# 183 warn "active\n";#d#
140 }, 184 },
141); 185);
158 my ($self) = @_; 202 my ($self) = @_;
159 203
160 refresh; 204 refresh;
161} 205}
162 206
207sub conn::face_find {
208 my ($self, $face) = @_;
209
210 $FACECACHE->{"$face->{chksum},$face->{name}"}
211}
212
163sub conn::face_update { 213sub conn::face_update {
164 my ($self, $num, $face) = @_; 214 my ($self, $face) = @_;
165 215
166 my $pb = new Gtk2::Gdk::PixbufLoader; 216 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
167 $pb->write ($face->{image});
168 $pb->close;
169 217
170 $pb = $pb->get_pixbuf; 218 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
171 $pb = $pb->add_alpha (0, 0, 0, 0); 219}
172 220
173 glGetError(); 221sub conn::query {
174 my ($tex) = @{glGenTextures 1}; 222 my ($self, $flags, $prompt) = @_;
175 223
176 $face->{texture} = $tex; 224 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
225}
226
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/],
177 253 );
178 glBindTexture GL_TEXTURE_2D, $tex; 254
179 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;
180 279 }
181 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 280 });
182 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 281
183 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 282 $cfg->{_i} = 1;
184 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 283 for (@cfg) {
185 284 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
186 glTexImage2D GL_TEXTURE_2D, 0, 285 }
187 GL_RGBA8, 286
188 $pb->get_width, $pb->get_height, 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';
189 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';
190 GL_RGBA, 301 }
191 GL_UNSIGNED_BYTE, 302 my $cb = $events{login} || sub {};
192 $pb->get_pixels; 303 $cb->($::CFG->{user}, $::CFG->{password});
193 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 });
194} 316}
317
195 318
196############################################################################# 319#############################################################################
197 320
321SDL::Init SDL_INIT_EVERYTHING;
322SDL::TTFInit;
323
324my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
325
326$mapwidget->activate;
327$mapwidget->focus_in;
328
198Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; 329Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
199 330
200$CFG ||= { 331$CFG ||= {
201 width => 640, 332 width => 640,
202 height => 480, 333 height => 480,
203 fullscreen => 0, 334 fullscreen => 0,
204 host => "crossfire.schmorp.de", 335 host => "crossfire.schmorp.de",
205 port => 13327, 336 port => 13327,
206}; 337};
207 338
208Client::Util::run_config_dialog 339$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
209 login => sub { print "Login $_[0] with $_[1]\n" }, 340
210 logout => sub { print "Logout" }; 341run_config_dialog
342 login => sub { start_game },
343 logout => sub { stop_game };
211 344
212main Gtk2; 345main Gtk2;
213 346
347Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines