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.50 by root, Sun Apr 9 21:05:50 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 43
42our @GL_INIT; # hooks called on every gl init
43
44our $ALT_ENTER_MESSAGE; 44our $ALT_ENTER_MESSAGE;
45 45our $STATUS_LINE;
46our $TOPLEVEL; 46our $DEBUG_STATUS;
47
48our $tw; # Test widget #d#
49 47
50my $last_refresh; 48my $last_refresh;
51my %ANIMATE; 49my %ANIMATE;
52my $refresh_handler; 50my $refresh_handler;
51
52our ($tw, $te); # Test widget #d#
53 53
54sub init_screen { 54sub init_screen {
55 $SDL_APP = new SDL::App 55 $SDL_APP = new SDL::App
56 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 56 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
57 -title => "Crossfire+ Client", 57 -title => "Crossfire+ Client",
58 -width => $WIDTH, 58 -width => $WIDTH,
59 -height => $HEIGHT, 59 -height => $HEIGHT,
60 -opengl => 1, 60 -opengl => 1,
61 -red_size => 8, 61 -red_size => 5,
62 -green_size => 8, 62 -green_size => 5,
63 -blue_size => 8, 63 -blue_size => 5,
64 -alpha_size => 0,
64 -double_buffer => 1, 65 -double_buffer => 1,
65 -fullscreen => $FULLSCREEN, 66 -fullscreen => $FULLSCREEN,
66 -resizeable => 0; 67 -resizeable => 0;
67 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
68 $last_refresh = SDL::GetTicks; 79 $last_refresh = SDL::GetTicks;
69 80
70 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 81 CFClient::gl_init;
71
72 $GL_EXT{GL_ARB_texture_non_power_of_two}
73 or warn "WARNING: non-power-of-two opengl extension required";
74 82
75 $FONTSIZE = int $HEIGHT / 50; 83 $FONTSIZE = int $HEIGHT / 50;
76 84
77 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 85 #############################################################################
78 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE,
79 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
80 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
81 $TOPLEVEL->add ($ALT_ENTER_MESSAGE);
82 86
83 # Test code #d#
84 unless ($tw) { # haha...
85 $tw = new Crossfire::Client::Widget::Animator;
86 my $lbl1 = new Crossfire::Client::Widget::Label
87 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
88 my $lbl2 = new Crossfire::Client::Widget::Label
89 0, 0, 10, $FONTSIZE, "LBL2";
90
91 my $vb = new Crossfire::Client::Widget::VBox;
92 my $f = new Crossfire::Client::Widget::FancyFrame;
93 my $f2 = new Crossfire::Client::Widget::FancyFrame;
94 $f->add ($lbl1);
95 $f2->add ($lbl2);
96 $vb->add ($f);
97 $vb->add ($f2, 1);
98
99 $tw->add ($vb);
100 $tw->w (400);
101 $tw->h (300);
102 $tw->move ($WIDTH - 200, 0);
103 $tw->moveto (0, 0);
104 $TOPLEVEL->add ($tw);
105
106# $f->move ($WIDTH - 200, 0);
107# $TOPLEVEL->add ($f);
108 }
109
110 glClearColor 0, 0, 0, 0; 87 glClearColor 0.45, 0.45, 0.45, 1;
111 88
112 glEnable GL_TEXTURE_2D; 89 glEnable GL_TEXTURE_2D;
113 glEnable GL_COLOR_MATERIAL; 90 glEnable GL_COLOR_MATERIAL;
114 glShadeModel GL_FLAT; 91 glShadeModel GL_FLAT;
115 glDisable GL_DEPTH_TEST; 92 glDisable GL_DEPTH_TEST;
116 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 93 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
117 94
118 $_->() for @GL_INIT; 95 #############################################################################
96
97 $DEBUG_STATUS = new CFClient::Widget::Label;
98 $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS);
99
100 $STATUS_LINE = new CFClient::Widget::Label
101 y => $HEIGHT * 59 / 60 - $FONTSIZE;
102 $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE);
103
104 $ALT_ENTER_MESSAGE = new CFClient::Widget::Label
105 y => $HEIGHT * 59 / 60,
106 height => $HEIGHT / 60,
107 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
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;
113
114 # Test code #d#
115 unless ($tw) { # haha...
116 $te = new CFClient::Widget::FancyFrame x => 300, z => 1;
117 $te->add (new CFClient::Widget::Entry);
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!";
122 my $lbl2 = new CFClient::Widget::Label text => "LBL2";
123 my $vb = new CFClient::Widget::VBox;
124 my $f = new CFClient::Widget::FancyFrame;
125 my $f2 = new CFClient::Widget::FancyFrame;
126 $f->add ($lbl1);
127 $f2->add ($lbl2);
128 $vb->add ($f);
129 $vb->add ($f2, 1);
130
131 $tw->add ($vb);
132 $tw->moveto (0, 0);
133 $CFClient::Widget::TOPLEVEL->add ($tw);
134
135# $f->move ($WIDTH - 200, 0);
136# $CFClient::Widget::TOPLEVEL->add ($f);
137 }
138}
139
140sub destroy_screen {
141 remove Glib::Source $SDL_TIMER;
142 undef $SDL_APP;
143 undef $SDL_EV;
144 SDL::Quit;
119} 145}
120 146
121sub start_game { 147sub start_game {
122 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
123 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
124 while $SDL_EV->poll;
125
126 1
127 };
128
129 $WIDTH = $CFG->{width}; 148 $WIDTH = $CFG->{width};
130 $HEIGHT = $CFG->{height}; 149 $HEIGHT = $CFG->{height};
131 $FULLSCREEN = 0; 150 $FULLSCREEN = 0;
132 151
133 init_screen; 152 init_screen;
134 153
135 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;
136 155
137 $CONN = new conn 156 $CONN = new conn
138 host => $CFG->{host}, 157 host => $CFG->{host},
139 port => $CFG->{port}, 158 port => $CFG->{port},
140 user => $CFG->{user}, 159 user => $CFG->{user},
141 pass => $CFG->{password}, 160 pass => $CFG->{password},
142 mapw => $mapsize, 161 mapw => $mapsize,
143 maph => $mapsize, 162 maph => $mapsize,
144 ; 163 ;
145 164
146 Crossfire::Client::lowdelay fileno $CONN->{fh}; 165 CFClient::lowdelay fileno $CONN->{fh};
147} 166}
148 167
149sub stop_game { 168sub stop_game {
150 remove Glib::Source $SDL_TIMER;
151 remove Glib::Source $refresh_handler; 169 remove Glib::Source $refresh_handler if $refresh_handler;
152 undef $refresh_handler; 170 undef $refresh_handler;
153 171
154 undef $SDL_APP;
155 undef $CONN; 172 undef $CONN;
156 SDL::Quit; 173 destroy_screen;
157} 174}
158
159 175
160sub force_refresh { 176sub force_refresh {
161 glViewport 0, 0, $WIDTH, $HEIGHT; 177 glViewport 0, 0, $WIDTH, $HEIGHT;
162 178
163 glMatrixMode GL_PROJECTION; 179 glMatrixMode GL_PROJECTION;
164 glLoadIdentity; 180 glLoadIdentity;
165 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; 181 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
166 glMatrixMode GL_MODELVIEW; 182 glMatrixMode GL_MODELVIEW;
167 183
168 glClear GL_COLOR_BUFFER_BIT; 184 glClear GL_COLOR_BUFFER_BIT;
169 185
170 $TOPLEVEL->draw; 186 $CFClient::Widget::TOPLEVEL->draw;
171 187
172 SDL::GLSwapBuffers; 188 SDL::GLSwapBuffers;
173} 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;
174 198
175sub refresh { 199sub refresh {
176 $refresh_handler ||= add Glib::Idle sub { 200 $refresh_handler ||= add Glib::Idle sub {
177 return unless $SDL_APP; 201 if ($SDL_APP) {
178
179 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
180 my $interval = ($next_refresh - $last_refresh) * 0.001; 209 my $interval = ($next_refresh - $last_refresh) * 0.001;
181 $last_refresh = $next_refresh; 210 $last_refresh = $next_refresh;
182 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
183 force_refresh; 218 force_refresh;
184 $_->animate ($interval) for grep $_, values %ANIMATE; 219 $_->animate ($interval) for grep $_, values %ANIMATE;
185 220
186 if (%ANIMATE) { 221 if (%ANIMATE) {
222 1
223 } else {
224 undef $refresh_handler;
225 0
187 1 226 }
188 } else { 227 } else {
189 undef $refresh_handler; 228 undef $refresh_handler;
190 0 229 0
191 } 230 }
192 }; 231 };
218 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) {
219 # alt-enter 258 # alt-enter
220 $FULLSCREEN = !$FULLSCREEN; 259 $FULLSCREEN = !$FULLSCREEN;
221 init_screen; 260 init_screen;
222 } else { 261 } else {
223 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 262 CFClient::Widget::feed_sdl_key_down_event ($SDL_EV);
224 } 263 }
225 }, 264 },
226 SDL_KEYUP() => sub { 265 SDL_KEYUP() => sub {
227 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 266 CFClient::Widget::feed_sdl_key_up_event ($SDL_EV);
228 }, 267 },
229 SDL_MOUSEMOTION() => sub { 268 SDL_MOUSEMOTION() => sub {
230 warn "sdl motion\n";#d# 269 CFClient::Widget::feed_sdl_motion_event ($SDL_EV);
231 }, 270 },
232 SDL_MOUSEBUTTONDOWN() => sub { 271 SDL_MOUSEBUTTONDOWN() => sub {
233 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 272 CFClient::Widget::feed_sdl_button_down_event ($SDL_EV);
234 }, 273 },
235 SDL_MOUSEBUTTONUP() => sub { 274 SDL_MOUSEBUTTONUP() => sub {
236 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 275 CFClient::Widget::feed_sdl_button_up_event ($SDL_EV);
237 }, 276 },
238 SDL_ACTIVEEVENT() => sub { 277 SDL_ACTIVEEVENT() => sub {
239 warn "active\n";#d# 278 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
240 }, 279 },
241); 280);
242 281
243@conn::ISA = Crossfire::Protocol::; 282@conn::ISA = Crossfire::Protocol::;
244 283
245sub conn::map_update { 284sub conn::map_update {
246 my ($self, $dirty) = @_; 285 my ($self, $dirty) = @_;
247 286
248 refresh; 287 $MAPWIDGET->update;
249} 288}
250 289
251sub conn::map_scroll { 290sub conn::map_scroll {
252 my ($self, $dx, $dy) = @_; 291 my ($self, $dx, $dy) = @_;
253 292
269sub conn::face_update { 308sub conn::face_update {
270 my ($self, $face) = @_; 309 my ($self, $face) = @_;
271 310
272 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 311 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
273 312
274 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 313 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
275} 314}
276 315
277sub conn::query { 316sub conn::query {
278 my ($self, $flags, $prompt) = @_; 317 my ($self, $flags, $prompt) = @_;
279 318
352 $cb->signal_connect (clicked => sub { 391 $cb->signal_connect (clicked => sub {
353 for (keys %$cfg) { 392 for (keys %$cfg) {
354 $::CFG->{$_} = $cfg->{$_} 393 $::CFG->{$_} = $cfg->{$_}
355 if $_ ne '_i'; 394 if $_ ne '_i';
356 } 395 }
357 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 396 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
358 }); 397 });
359 $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);
360 $cb->signal_connect (clicked => sub { 399 $cb->signal_connect (clicked => sub {
361 for (keys %$cfg) { 400 for (keys %$cfg) {
362 $::CFG->{$_} = $cfg->{$_} 401 $::CFG->{$_} = $cfg->{$_}
381 420
382############################################################################# 421#############################################################################
383 422
384SDL::Init SDL_INIT_EVERYTHING; 423SDL::Init SDL_INIT_EVERYTHING;
385 424
386$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
387
388my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
389
390$TOPLEVEL->add ($mapwidget);
391$mapwidget->focus_in;
392
393Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 425CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
394 426
395$CFG ||= { 427$CFG ||= {
396 width => 640, 428 width => 640,
397 height => 480, 429 height => 480,
398 mapsize => 100, 430 mapsize => 100,
399 fullscreen => 0, 431 fullscreen => 0,
400 host => "crossfire.schmorp.de", 432 host => "crossfire.schmorp.de",
401 port => 13327, 433 port => 13327,
402}; 434};
403 435
404Crossfire::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}
405 442
406$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 443$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
407 444
408run_config_dialog 445run_config_dialog
409 login => sub { start_game }, 446 login => sub { start_game },

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines