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.52 by root, Sun Apr 9 22:12:12 2006 UTC vs.
Revision 1.69 by root, Tue Apr 11 14:04:27 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::Client;
18use Crossfire::Protocol; 17use Crossfire::Protocol;
19 18
19use CFClient;
20use Crossfire::Client::Widget; 20use CFClient::Widget;
21
22our $VERSION = '0.1';
23
24my $MAX_FPS = 30;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
21 26
22our $FACECACHE; 27our $FACECACHE;
23
24our $VERSION = '0.1';
25
26our %GL_EXT;
27 28
28our $CFG; 29our $CFG;
29our $CONN; 30our $CONN;
30 31
31our $WIDTH; 32our $WIDTH;
32our $HEIGHT; 33our $HEIGHT;
33our $FULLSCREEN; 34our $FULLSCREEN;
34 35
36our $MAPWIDGET;
35our $FONTSIZE; 37our $FONTSIZE;
36 38
37our $SDL_TIMER; 39our $SDL_TIMER;
38our $SDL_APP; 40our $SDL_APP;
39our $SDL_EV = new SDL::Event; 41our $SDL_EV;
40our %SDL_CB; 42our %SDL_CB;
41
42our @GL_INIT; # hooks called on every gl init
43 43
44our $ALT_ENTER_MESSAGE; 44our $ALT_ENTER_MESSAGE;
45our $STATUS_LINE; 45our $STATUS_LINE;
46 46our $DEBUG_STATUS;
47our $TOPLEVEL;
48
49our $tw; # Test widget #d#
50 47
51my $last_refresh; 48my $last_refresh;
52my %ANIMATE; 49my %ANIMATE;
53my $refresh_handler; 50my $refresh_handler;
51
52our ($tw, $te); # Test widget #d#
54 53
55sub init_screen { 54sub init_screen {
56 $SDL_APP = new SDL::App 55 $SDL_APP = new SDL::App
57 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 56 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
58 -title => "Crossfire+ Client", 57 -title => "Crossfire+ Client",
65 -alpha_size => 0, 64 -alpha_size => 0,
66 -double_buffer => 1, 65 -double_buffer => 1,
67 -fullscreen => $FULLSCREEN, 66 -fullscreen => $FULLSCREEN,
68 -resizeable => 0; 67 -resizeable => 0;
69 68
69 $SDL_EV = new SDL::Event;
70 $SDL_EV->set_unicode (1);
71
72 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
73 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
74 while $SDL_EV->poll;
75
76 1
77 };
78
70 $last_refresh = SDL::GetTicks; 79 $last_refresh = SDL::GetTicks;
71 80
72 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 81 CFClient::gl_init;
73
74 $GL_EXT{GL_ARB_texture_non_power_of_two}
75 or warn "WARNING: non-power-of-two opengl extension required";
76 82
77 $FONTSIZE = int $HEIGHT / 50; 83 $FONTSIZE = int $HEIGHT / 50;
78 84
79 ############################################################################# 85 #############################################################################
80 86
81 glClearColor 0, 0, 0, 0; 87 glClearColor 0.45, 0.45, 0.45, 1;
82 88
83 glEnable GL_TEXTURE_2D; 89 glEnable GL_TEXTURE_2D;
84 glEnable GL_COLOR_MATERIAL; 90 glEnable GL_COLOR_MATERIAL;
85 glShadeModel GL_FLAT; 91 glShadeModel GL_FLAT;
86 glDisable GL_DEPTH_TEST; 92 glDisable GL_DEPTH_TEST;
87 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 93 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
88 94
89 $_->() for @GL_INIT;
90
91 ############################################################################# 95 #############################################################################
96
97 $DEBUG_STATUS = new CFClient::Widget::Label;
98 $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS);
92 99
93 $STATUS_LINE = new Crossfire::Client::Widget::Label 100 $STATUS_LINE = new CFClient::Widget::Label
94 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, 101 y => $HEIGHT * 59 / 60 - $FONTSIZE;
95 "";
96 $TOPLEVEL->add ($STATUS_LINE); 102 $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE);
97 103
98 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 104 $ALT_ENTER_MESSAGE = new CFClient::Widget::Label
99 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, 105 y => $HEIGHT * 59 / 60,
106 height => $HEIGHT / 60,
100 "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 107 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
101 $TOPLEVEL->add ($ALT_ENTER_MESSAGE); 108 $CFClient::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
109
110 $MAPWIDGET = new CFClient::Widget::MapWidget;
111 $CFClient::Widget::TOPLEVEL->add ($MAPWIDGET);
112 $MAPWIDGET->focus_in;
102 113
103 # Test code #d# 114 # Test code #d#
104 unless ($tw) { # haha... 115 unless ($tw) { # haha...
105 $tw = new Crossfire::Client::Widget::Animator; 116 $te = new CFClient::Widget::FancyFrame x => 300, z => 1;
106 my $lbl1 = new Crossfire::Client::Widget::Label 117 $te->add (new CFClient::Widget::Entry);
107 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; 118 $CFClient::Widget::TOPLEVEL->add ($te);
119
120 $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 600, h => 300;
121 my $lbl1 = new CFClient::Widget::Label text => "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
108 my $lbl2 = new Crossfire::Client::Widget::Label 122 my $lbl2 = new CFClient::Widget::Label text => "LBL2";
109 0, 0, 10, $FONTSIZE, "LBL2";
110
111 my $vb = new Crossfire::Client::Widget::VBox; 123 my $vb = new CFClient::Widget::VBox;
112 my $f = new Crossfire::Client::Widget::FancyFrame; 124 my $f = new CFClient::Widget::FancyFrame;
113 my $f2 = new Crossfire::Client::Widget::FancyFrame; 125 my $f2 = new CFClient::Widget::FancyFrame;
114 $f->add ($lbl1); 126 $f->add ($lbl1);
115 $f2->add ($lbl2); 127 $f2->add ($lbl2);
116 $vb->add ($f); 128 $vb->add ($f);
117 $vb->add ($f2, 1); 129 $vb->add ($f2, 1);
118 130
119 $tw->add ($vb); 131 $tw->add ($vb);
120 $tw->w (400);
121 $tw->h (300);
122 $tw->move ($WIDTH - 200, 0);
123 $tw->moveto (0, 0); 132 $tw->moveto (0, 0);
124 $TOPLEVEL->add ($tw); 133 $CFClient::Widget::TOPLEVEL->add ($tw);
125 134
126# $f->move ($WIDTH - 200, 0); 135# $f->move ($WIDTH - 200, 0);
127# $TOPLEVEL->add ($f); 136# $CFClient::Widget::TOPLEVEL->add ($f);
128 } 137 }
129} 138}
130 139
140sub destroy_screen {
141 remove Glib::Source $SDL_TIMER;
142 undef $SDL_APP;
143 undef $SDL_EV;
144 SDL::Quit;
145}
146
131sub start_game { 147sub start_game {
132 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
133 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
134 while $SDL_EV->poll;
135
136 1
137 };
138
139 $WIDTH = $CFG->{width}; 148 $WIDTH = $CFG->{width};
140 $HEIGHT = $CFG->{height}; 149 $HEIGHT = $CFG->{height};
141 $FULLSCREEN = 0; 150 $FULLSCREEN = 0;
142 151
143 init_screen; 152 init_screen;
144 153
145 my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; 154 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
146 155
147 $CONN = new conn 156 $CONN = new conn
148 host => $CFG->{host}, 157 host => $CFG->{host},
149 port => $CFG->{port}, 158 port => $CFG->{port},
150 user => $CFG->{user}, 159 user => $CFG->{user},
151 pass => $CFG->{password}, 160 pass => $CFG->{password},
152 mapw => $mapsize, 161 mapw => $mapsize,
153 maph => $mapsize, 162 maph => $mapsize,
154 ; 163 ;
155 164
156 Crossfire::Client::lowdelay fileno $CONN->{fh}; 165 CFClient::lowdelay fileno $CONN->{fh};
157} 166}
158 167
159sub stop_game { 168sub stop_game {
160 remove Glib::Source $SDL_TIMER;
161 remove Glib::Source $refresh_handler if $refresh_handler; 169 remove Glib::Source $refresh_handler if $refresh_handler;
162 undef $refresh_handler; 170 undef $refresh_handler;
163 171
164 undef $SDL_APP;
165 undef $CONN; 172 undef $CONN;
166 SDL::Quit; 173 destroy_screen;
167} 174}
168
169 175
170sub force_refresh { 176sub force_refresh {
171 glViewport 0, 0, $WIDTH, $HEIGHT; 177 glViewport 0, 0, $WIDTH, $HEIGHT;
172 178
173 glMatrixMode GL_PROJECTION; 179 glMatrixMode GL_PROJECTION;
174 glLoadIdentity; 180 glLoadIdentity;
175 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; 181 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
176 glMatrixMode GL_MODELVIEW; 182 glMatrixMode GL_MODELVIEW;
177 183
178 glClear GL_COLOR_BUFFER_BIT; 184 glClear GL_COLOR_BUFFER_BIT;
179 185
180 $TOPLEVEL->draw; 186 $CFClient::Widget::TOPLEVEL->draw;
181 187
182 SDL::GLSwapBuffers; 188 SDL::GLSwapBuffers;
183} 189}
190
191sub debug {
192 $DEBUG_STATUS->set_text ($_[0]);
193 $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request);
194 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0);
195}
196
197my $FPS;
184 198
185sub refresh { 199sub refresh {
186 $refresh_handler ||= add Glib::Idle sub { 200 $refresh_handler ||= add Glib::Idle sub {
187 return unless $SDL_APP; 201 if ($SDL_APP) {
188
189 my $next_refresh = SDL::GetTicks; 202 my $next_refresh = SDL::GetTicks;
203
204 if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) {
205 SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh);
206 $next_refresh = SDL::GetTicks;
207 }
208
190 my $interval = ($next_refresh - $last_refresh) * 0.001; 209 my $interval = ($next_refresh - $last_refresh) * 0.001;
191 $last_refresh = $next_refresh; 210 $last_refresh = $next_refresh;
192 211
212 if ($interval) {
213 $FPS ||= 1 / $interval;
214 $FPS = $FPS * 0.96 + (1 / $interval) * 0.04;
215 debug sprintf "%5.02f", $FPS;
216 }
217
193 force_refresh; 218 force_refresh;
194 $_->animate ($interval) for grep $_, values %ANIMATE; 219 $_->animate ($interval) for grep $_, values %ANIMATE;
195 220
196 if (%ANIMATE) { 221 if (%ANIMATE) {
222 1
223 } else {
224 undef $refresh_handler;
225 0
197 1 226 }
198 } else { 227 } else {
199 undef $refresh_handler; 228 undef $refresh_handler;
200 0 229 0
201 } 230 }
202 }; 231 };
228 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 257 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
229 # alt-enter 258 # alt-enter
230 $FULLSCREEN = !$FULLSCREEN; 259 $FULLSCREEN = !$FULLSCREEN;
231 init_screen; 260 init_screen;
232 } else { 261 } else {
233 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 262 CFClient::Widget::feed_sdl_key_down_event ($SDL_EV);
234 } 263 }
235 }, 264 },
236 SDL_KEYUP() => sub { 265 SDL_KEYUP() => sub {
237 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 266 CFClient::Widget::feed_sdl_key_up_event ($SDL_EV);
238 }, 267 },
239 SDL_MOUSEMOTION() => sub { 268 SDL_MOUSEMOTION() => sub {
240 warn "sdl motion\n";#d# 269 CFClient::Widget::feed_sdl_motion_event ($SDL_EV);
241 }, 270 },
242 SDL_MOUSEBUTTONDOWN() => sub { 271 SDL_MOUSEBUTTONDOWN() => sub {
243 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 272 CFClient::Widget::feed_sdl_button_down_event ($SDL_EV);
244 }, 273 },
245 SDL_MOUSEBUTTONUP() => sub { 274 SDL_MOUSEBUTTONUP() => sub {
246 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 275 CFClient::Widget::feed_sdl_button_up_event ($SDL_EV);
247 }, 276 },
248 SDL_ACTIVEEVENT() => sub { 277 SDL_ACTIVEEVENT() => sub {
249 warn "active\n";#d# 278 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
250 }, 279 },
251); 280);
252 281
253@conn::ISA = Crossfire::Protocol::; 282@conn::ISA = Crossfire::Protocol::;
254 283
255sub conn::map_update { 284sub conn::map_update {
256 my ($self, $dirty) = @_; 285 my ($self, $dirty) = @_;
257 286
258 refresh; 287 $MAPWIDGET->update;
259} 288}
260 289
261sub conn::map_scroll { 290sub conn::map_scroll {
262 my ($self, $dx, $dy) = @_; 291 my ($self, $dx, $dy) = @_;
263 292
279sub conn::face_update { 308sub conn::face_update {
280 my ($self, $face) = @_; 309 my ($self, $face) = @_;
281 310
282 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 311 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
283 312
284 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 313 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
285} 314}
286 315
287sub conn::query { 316sub conn::query {
288 my ($self, $flags, $prompt) = @_; 317 my ($self, $flags, $prompt) = @_;
289 318
362 $cb->signal_connect (clicked => sub { 391 $cb->signal_connect (clicked => sub {
363 for (keys %$cfg) { 392 for (keys %$cfg) {
364 $::CFG->{$_} = $cfg->{$_} 393 $::CFG->{$_} = $cfg->{$_}
365 if $_ ne '_i'; 394 if $_ ne '_i';
366 } 395 }
367 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 396 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
368 }); 397 });
369 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); 398 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
370 $cb->signal_connect (clicked => sub { 399 $cb->signal_connect (clicked => sub {
371 for (keys %$cfg) { 400 for (keys %$cfg) {
372 $::CFG->{$_} = $cfg->{$_} 401 $::CFG->{$_} = $cfg->{$_}
391 420
392############################################################################# 421#############################################################################
393 422
394SDL::Init SDL_INIT_EVERYTHING; 423SDL::Init SDL_INIT_EVERYTHING;
395 424
396$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
397
398my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
399
400$TOPLEVEL->add ($mapwidget);
401$mapwidget->focus_in;
402
403Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 425CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
404 426
405$CFG ||= { 427$CFG ||= {
406 width => 640, 428 width => 640,
407 height => 480, 429 height => 480,
408 mapsize => 100, 430 mapsize => 100,
409 fullscreen => 0, 431 fullscreen => 0,
410 host => "crossfire.schmorp.de", 432 host => "crossfire.schmorp.de",
411 port => 13327, 433 port => 13327,
412}; 434};
413 435
414Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; 436{
437 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
438
439 CFClient::add_font $_ for @fonts;
440 CFClient::set_font $fonts[0];
441}
415 442
416$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 443$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
417 444
418run_config_dialog 445run_config_dialog
419 login => sub { start_game }, 446 login => sub { start_game },

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines