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.31 by elmex, Sat Apr 8 16:34:47 2006 UTC vs.
Revision 1.62 by root, Mon Apr 10 19:34:04 2006 UTC

12use SDL::Surface; 12use SDL::Surface;
13use SDL::OpenGL; 13use SDL::OpenGL;
14use SDL::OpenGL::Constants; 14use SDL::OpenGL::Constants;
15 15
16use Crossfire; 16use Crossfire;
17use Crossfire::Protocol;
18
17use Crossfire::Client; 19use Crossfire::Client;
18use Crossfire::Protocol;
19
20use Crossfire::Client::Widget; 20use Crossfire::Client::Widget;
21 21
22our $FACECACHE; 22our $FACECACHE;
23 23
24our $VERSION = '0.1'; 24our $VERSION = '0.1';
25
26our %GL_EXT;
27 25
28our $CFG; 26our $CFG;
29our $CONN; 27our $CONN;
30 28
31our $WIDTH; 29our $WIDTH;
32our $HEIGHT; 30our $HEIGHT;
33our $FULLSCREEN; 31our $FULLSCREEN;
34 32
35our $UIFONT; 33our $FONTSIZE;
36 34
37our $SDL_TIMER; 35our $SDL_TIMER;
38our $SDL_APP; 36our $SDL_APP;
39our $SDL_EV = new SDL::Event; 37our $SDL_EV;
40our %SDL_CB; 38our %SDL_CB;
41 39
42our @GL_INIT; # hooks called on every gl init
43
44our $ALT_ENTER_MESSAGE; 40our $ALT_ENTER_MESSAGE;
41our $STATUS_LINE;
42
43my $last_refresh;
44my %ANIMATE;
45my $refresh_handler;
46
47our ($tw, $te); # Test widget #d#
45 48
46sub init_screen { 49sub init_screen {
47 $SDL_APP = new SDL::App 50 $SDL_APP = new SDL::App
48 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 51 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
49 -title => "Crossfire+ Client", 52 -title => "Crossfire+ Client",
50 -width => $WIDTH, 53 -width => $WIDTH,
51 -height => $HEIGHT, 54 -height => $HEIGHT,
52 -opengl => 1, 55 -opengl => 1,
53 -red_size => 8, 56 -red_size => 5,
54 -green_size => 8, 57 -green_size => 5,
55 -blue_size => 8, 58 -blue_size => 5,
59 -alpha_size => 0,
56 -double_buffer => 1, 60 -double_buffer => 1,
57 -fullscreen => $FULLSCREEN, 61 -fullscreen => $FULLSCREEN,
58 -resizeable => 0; 62 -resizeable => 0;
59 63
60 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 64 $SDL_EV = new SDL::Event;
65 $SDL_EV->set_unicode (1);
61 66
62 $GL_EXT{GL_ARB_texture_non_power_of_two} 67 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
63 or warn "WARNING: non-power-of-two opengl extension required"; 68 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
69 while $SDL_EV->poll;
64 70
65 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 71 1
66 or die "TTFOpenFont: $!"; 72 };
67 73
68 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; 74 $last_refresh = SDL::GetTicks;
69 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
70 $ALT_ENTER_MESSAGE->activate;
71 75
76 Crossfire::Client::gl_init;
77
78 $FONTSIZE = int $HEIGHT / 50;
79
80 #############################################################################
81
72 glClearColor 0, 0, 0, 0; 82 glClearColor 0.45, 0.45, 0.45, 1;
73 83
74 glEnable GL_TEXTURE_2D; 84 glEnable GL_TEXTURE_2D;
85 glEnable GL_COLOR_MATERIAL;
75 glShadeModel GL_FLAT; 86 glShadeModel GL_FLAT;
76 glDisable GL_DEPTH_TEST; 87 glDisable GL_DEPTH_TEST;
77 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 88 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
78 89
79 glMatrixMode GL_PROJECTION; 90 #############################################################################
80 glLoadIdentity; 91
81 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; 92 $STATUS_LINE = new Crossfire::Client::Widget::Label
93 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
94 "";
95 $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE);
82 96
83 glMatrixMode GL_MODELVIEW; 97 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
98 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
99 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
100 $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
84 101
85 $_->() for @GL_INIT; 102 # Test code #d#
103 unless ($tw) { # haha...
104 $te = new Crossfire::Client::Widget::FancyFrame;
105 $te->add (new Crossfire::Client::Widget::Entry);
106 $te->move (300, 0, 2);
107 $Crossfire::Client::Widget::TOPLEVEL->add ($te);
108
109 $tw = new Crossfire::Client::Widget::Animator;
110 my $lbl1 = new Crossfire::Client::Widget::Label
111 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
112 my $lbl2 = new Crossfire::Client::Widget::Label
113 0, 0, 10, $FONTSIZE, "LBL2";
114
115 my $vb = new Crossfire::Client::Widget::VBox;
116 my $f = new Crossfire::Client::Widget::FancyFrame;
117 my $f2 = new Crossfire::Client::Widget::FancyFrame;
118 $f->add ($lbl1);
119 $f2->add ($lbl2);
120 $vb->add ($f);
121 $vb->add ($f2, 1);
122
123 $tw->add ($vb);
124 $tw->w (400);
125 $tw->h (300);
126 $tw->move ($WIDTH - 200, 0);
127 $tw->moveto (0, 0);
128 $Crossfire::Client::Widget::TOPLEVEL->add ($tw);
129
130# $f->move ($WIDTH - 200, 0);
131# $Crossfire::Client::Widget::TOPLEVEL->add ($f);
132 }
133}
134
135sub destroy_screen {
136 remove Glib::Source $SDL_TIMER;
137 undef $SDL_APP;
138 undef $SDL_EV;
139 SDL::Quit;
86} 140}
87 141
88sub start_game { 142sub start_game {
89 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
90 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
91 while $SDL_EV->poll;
92
93 1
94 };
95
96 $WIDTH = $CFG->{width}; 143 $WIDTH = $CFG->{width};
97 $HEIGHT = $CFG->{height}; 144 $HEIGHT = $CFG->{height};
98 $FULLSCREEN = 0; 145 $FULLSCREEN = 0;
99 146
100 init_screen; 147 init_screen;
148
149 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
101 150
102 $CONN = new conn 151 $CONN = new conn
103 host => $CFG->{host}, 152 host => $CFG->{host},
104 port => $CFG->{port}, 153 port => $CFG->{port},
105 user => $CFG->{user}, 154 user => $CFG->{user},
106 pass => $CFG->{password}; 155 pass => $CFG->{password},
156 mapw => $mapsize,
157 maph => $mapsize,
158 ;
159
160 Crossfire::Client::lowdelay fileno $CONN->{fh};
107} 161}
108 162
109sub stop_game { 163sub stop_game {
110 remove Glib::Source $SDL_TIMER; 164 remove Glib::Source $refresh_handler if $refresh_handler;
165 undef $refresh_handler;
111 166
112 undef $SDL_APP; 167 undef $CONN;
113 SDL::Quit; 168 destroy_screen;
114} 169}
115
116 170
117sub force_refresh { 171sub force_refresh {
118 glViewport 0, 0, $WIDTH, $HEIGHT; 172 glViewport 0, 0, $WIDTH, $HEIGHT;
173
174 glMatrixMode GL_PROJECTION;
175 glLoadIdentity;
176 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
177 glMatrixMode GL_MODELVIEW;
178
119 glClear GL_COLOR_BUFFER_BIT; 179 glClear GL_COLOR_BUFFER_BIT;
120 180
121 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; 181 $Crossfire::Client::Widget::TOPLEVEL->draw;
122 182
123 SDL::GLSwapBuffers; 183 SDL::GLSwapBuffers;
124} 184}
125
126my $refresh_handler;
127 185
128sub refresh { 186sub refresh {
129 $refresh_handler ||= add Glib::Idle sub { 187 $refresh_handler ||= add Glib::Idle sub {
188 if ($SDL_APP) {
189 my $next_refresh = SDL::GetTicks;
190 my $interval = ($next_refresh - $last_refresh) * 0.001;
191 $last_refresh = $next_refresh;
192
130 force_refresh; 193 force_refresh;
194 $_->animate ($interval) for grep $_, values %ANIMATE;
195
196 if (%ANIMATE) {
197 1
198 } else {
199 undef $refresh_handler;
200 0
201 }
202 } else {
131 undef $refresh_handler; 203 undef $refresh_handler;
204 0
132 0 205 }
133 }; 206 };
207}
208
209sub animation_start {
210 my ($widget) = @_;
211 $ANIMATE{$widget} = $widget;
212 Scalar::Util::weaken $ANIMATE{$widget};
213
214 refresh;
215}
216
217sub animation_stop {
218 my ($widget) = @_;
219 delete $ANIMATE{$widget};
134} 220}
135 221
136%SDL_CB = ( 222%SDL_CB = (
137 SDL_QUIT() => sub { 223 SDL_QUIT() => sub {
138 main_quit Gtk2; 224 main_quit Gtk2;
153 }, 239 },
154 SDL_KEYUP() => sub { 240 SDL_KEYUP() => sub {
155 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 241 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
156 }, 242 },
157 SDL_MOUSEMOTION() => sub { 243 SDL_MOUSEMOTION() => sub {
158 warn "sdl motion\n";#d# 244 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
159 }, 245 },
160 SDL_MOUSEBUTTONDOWN() => sub { 246 SDL_MOUSEBUTTONDOWN() => sub {
161 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 247 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
162 }, 248 },
163 SDL_MOUSEBUTTONUP() => sub { 249 SDL_MOUSEBUTTONUP() => sub {
164 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 250 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
165 }, 251 },
166 SDL_ACTIVEEVENT() => sub { 252 SDL_ACTIVEEVENT() => sub {
167 warn "active\n";#d# 253 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
168 }, 254 },
169); 255);
170 256
171@conn::ISA = Crossfire::Protocol::; 257@conn::ISA = Crossfire::Protocol::;
172 258
177} 263}
178 264
179sub conn::map_scroll { 265sub conn::map_scroll {
180 my ($self, $dx, $dy) = @_; 266 my ($self, $dx, $dy) = @_;
181 267
182 refresh; 268# refresh;
183} 269}
184 270
185sub conn::map_clear { 271sub conn::map_clear {
186 my ($self) = @_; 272 my ($self) = @_;
187 273
188 refresh; 274# refresh;
189} 275}
190 276
191sub conn::face_find { 277sub conn::face_find {
192 my ($self, $face) = @_; 278 my ($self, $face) = @_;
193 279
198 my ($self, $face) = @_; 284 my ($self, $face) = @_;
199 285
200 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 286 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
201 287
202 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 288 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
289}
290
291sub conn::query {
292 my ($self, $flags, $prompt) = @_;
293
294 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
203} 295}
204 296
205sub gtk_add_cfg_field { 297sub gtk_add_cfg_field {
206 my ($tbl, $cfg, $klbl, $key, $value) = @_; 298 my ($tbl, $cfg, $klbl, $key, $value) = @_;
207 my $i = $cfg->{_i}++; 299 my $i = $cfg->{_i}++;
213 } 305 }
214 $ent->set_text ($value); 306 $ent->set_text ($value);
215 $ent->signal_connect (changed => sub { 307 $ent->signal_connect (changed => sub {
216 my ($ent) = @_; 308 my ($ent) = @_;
217 $cfg->{$key} = $ent->get_text; 309 $cfg->{$key} = $ent->get_text;
310 # TODO: Mapsize should be a slider in the game gui
311 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
312 $cfg->{$key} = 100;
313 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
314 $cfg->{$key} = 50;
315 }
218 }); 316 });
219} 317}
220 318
221sub run_config_dialog { 319sub run_config_dialog {
222 my (%events) = @_; 320 my (%events) = @_;
224 my $w = Gtk2::Window->new; 322 my $w = Gtk2::Window->new;
225 323
226 my @cfg = ( 324 my @cfg = (
227 [qw/Host host/], 325 [qw/Host host/],
228 [qw/Port port/], 326 [qw/Port port/],
327 [qw/Mapsize% mapsize/],
229 [qw/Username user/], 328 [qw/Username user/],
230 [qw/Password password/], 329 [qw/Password password/],
231 ); 330 );
232 331
233 my $cfg = {}; 332 my $cfg = {};
280 my $cb = $events{login} || sub {}; 379 my $cb = $events{login} || sub {};
281 $cb->($::CFG->{user}, $::CFG->{password}); 380 $cb->($::CFG->{user}, $::CFG->{password});
282 }); 381 });
283 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); 382 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
284 $cb->signal_connect (clicked => sub { 383 $cb->signal_connect (clicked => sub {
285 my $cb = $events{login} || sub {}; 384 my $cb = $events{logout} || sub {};
286 $cb->(); 385 $cb->();
287 }); 386 });
288 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); 387 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
289 $cb->signal_connect (clicked => sub { $w->destroy }); 388 $cb->signal_connect (clicked => sub { $w->destroy });
290 389
295 394
296 395
297############################################################################# 396#############################################################################
298 397
299SDL::Init SDL_INIT_EVERYTHING; 398SDL::Init SDL_INIT_EVERYTHING;
300SDL::TTFInit;
301 399
302my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 400my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
303 401
304$mapwidget->activate; 402$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
305$mapwidget->focus_in; 403$mapwidget->focus_in;
306 404
307Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 405Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
308 406
309$CFG ||= { 407$CFG ||= {
310 width => 640, 408 width => 640,
311 height => 480, 409 height => 480,
410 mapsize => 100,
312 fullscreen => 0, 411 fullscreen => 0,
313 host => "crossfire.schmorp.de", 412 host => "crossfire.schmorp.de",
314 port => 13327, 413 port => 13327,
315}; 414};
316 415
416Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
417
317$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 418$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
318 419
319run_config_dialog 420run_config_dialog
320 login => sub { start_game }, 421 login => sub { start_game },
321 logout => sub { stop_game }; 422 logout => sub { stop_game };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines