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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines