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.55 by root, Sun Apr 9 22:27:52 2006 UTC vs.
Revision 1.63 by root, Mon Apr 10 22:53:50 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;
36our $FOCUS;
37our $HOVER;
38 37
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
49our $TOPLEVEL;
50
51our $tw; # Test widget #d#
52 45
53my $last_refresh; 46my $last_refresh;
54my %ANIMATE; 47my %ANIMATE;
55my $refresh_handler; 48my $refresh_handler;
49
50our ($tw, $te); # Test widget #d#
56 51
57sub init_screen { 52sub init_screen {
58 $SDL_APP = new SDL::App 53 $SDL_APP = new SDL::App
59 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 54 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
60 -title => "Crossfire+ Client", 55 -title => "Crossfire+ Client",
67 -alpha_size => 0, 62 -alpha_size => 0,
68 -double_buffer => 1, 63 -double_buffer => 1,
69 -fullscreen => $FULLSCREEN, 64 -fullscreen => $FULLSCREEN,
70 -resizeable => 0; 65 -resizeable => 0;
71 66
67 $SDL_EV = new SDL::Event;
68 $SDL_EV->set_unicode (1);
69
70 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
71 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
72 while $SDL_EV->poll;
73
74 1
75 };
76
72 $last_refresh = SDL::GetTicks; 77 $last_refresh = SDL::GetTicks;
73 78
74 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 79 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 80
79 $FONTSIZE = int $HEIGHT / 50; 81 $FONTSIZE = int $HEIGHT / 50;
80 82
81 ############################################################################# 83 #############################################################################
82 84
83 glClearColor 0, 0, 0, 0; 85 glClearColor 0.45, 0.45, 0.45, 1;
84 86
85 glEnable GL_TEXTURE_2D; 87 glEnable GL_TEXTURE_2D;
86 glEnable GL_COLOR_MATERIAL; 88 glEnable GL_COLOR_MATERIAL;
87 glShadeModel GL_FLAT; 89 glShadeModel GL_FLAT;
88 glDisable GL_DEPTH_TEST; 90 glDisable GL_DEPTH_TEST;
89 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 91 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
90 92
91 $_->() for @GL_INIT;
92
93 ############################################################################# 93 #############################################################################
94 94
95 $STATUS_LINE = new Crossfire::Client::Widget::Label 95 $STATUS_LINE = new Crossfire::Client::Widget::Label
96 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, 96 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
97 ""; 97 "";
98 $TOPLEVEL->add ($STATUS_LINE); 98 $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE);
99 99
100 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 100 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
101 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, 101 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
102 "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 102 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
103 $TOPLEVEL->add ($ALT_ENTER_MESSAGE); 103 $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
104 104
105 # Test code #d# 105 # Test code #d#
106 unless ($tw) { # haha... 106 unless ($tw) { # haha...
107 $te = new Crossfire::Client::Widget::FancyFrame;
108 $te->add (new Crossfire::Client::Widget::Entry);
109 $te->move (300, 0, 2);
110 $Crossfire::Client::Widget::TOPLEVEL->add ($te);
111
107 $tw = new Crossfire::Client::Widget::Animator; 112 $tw = new Crossfire::Client::Widget::Animator;
108 my $lbl1 = new Crossfire::Client::Widget::Label 113 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!"; 114 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 115 my $lbl2 = new Crossfire::Client::Widget::Label
111 0, 0, 10, $FONTSIZE, "LBL2"; 116 0, 0, 10, $FONTSIZE, "LBL2";
121 $tw->add ($vb); 126 $tw->add ($vb);
122 $tw->w (400); 127 $tw->w (400);
123 $tw->h (300); 128 $tw->h (300);
124 $tw->move ($WIDTH - 200, 0); 129 $tw->move ($WIDTH - 200, 0);
125 $tw->moveto (0, 0); 130 $tw->moveto (0, 0);
126 $TOPLEVEL->add ($tw); 131 $Crossfire::Client::Widget::TOPLEVEL->add ($tw);
127 132
128# $f->move ($WIDTH - 200, 0); 133# $f->move ($WIDTH - 200, 0);
129# $TOPLEVEL->add ($f); 134# $Crossfire::Client::Widget::TOPLEVEL->add ($f);
130 } 135 }
131} 136}
132 137
138sub destroy_screen {
139 remove Glib::Source $SDL_TIMER;
140 undef $SDL_APP;
141 undef $SDL_EV;
142 SDL::Quit;
143}
144
133sub start_game { 145sub 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}; 146 $WIDTH = $CFG->{width};
142 $HEIGHT = $CFG->{height}; 147 $HEIGHT = $CFG->{height};
143 $FULLSCREEN = 0; 148 $FULLSCREEN = 0;
144 149
145 init_screen; 150 init_screen;
146 151
147 my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; 152 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
148 153
149 $CONN = new conn 154 $CONN = new conn
150 host => $CFG->{host}, 155 host => $CFG->{host},
151 port => $CFG->{port}, 156 port => $CFG->{port},
152 user => $CFG->{user}, 157 user => $CFG->{user},
157 162
158 Crossfire::Client::lowdelay fileno $CONN->{fh}; 163 Crossfire::Client::lowdelay fileno $CONN->{fh};
159} 164}
160 165
161sub stop_game { 166sub stop_game {
162 remove Glib::Source $SDL_TIMER;
163 remove Glib::Source $refresh_handler if $refresh_handler; 167 remove Glib::Source $refresh_handler if $refresh_handler;
164 undef $refresh_handler; 168 undef $refresh_handler;
165 169
166 undef $SDL_APP;
167 undef $CONN; 170 undef $CONN;
168 SDL::Quit; 171 destroy_screen;
169} 172}
170
171 173
172sub force_refresh { 174sub force_refresh {
173 glViewport 0, 0, $WIDTH, $HEIGHT; 175 glViewport 0, 0, $WIDTH, $HEIGHT;
174 176
175 glMatrixMode GL_PROJECTION; 177 glMatrixMode GL_PROJECTION;
176 glLoadIdentity; 178 glLoadIdentity;
177 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; 179 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
178 glMatrixMode GL_MODELVIEW; 180 glMatrixMode GL_MODELVIEW;
179 181
180 glClear GL_COLOR_BUFFER_BIT; 182 glClear GL_COLOR_BUFFER_BIT;
181 183
182 $TOPLEVEL->draw; 184 $Crossfire::Client::Widget::TOPLEVEL->draw;
183 185
184 SDL::GLSwapBuffers; 186 SDL::GLSwapBuffers;
185} 187}
186 188
187sub refresh { 189sub refresh {
188 $refresh_handler ||= add Glib::Idle sub { 190 $refresh_handler ||= add Glib::Idle sub {
189 return unless $SDL_APP; 191 if ($SDL_APP) {
190
191 my $next_refresh = SDL::GetTicks; 192 my $next_refresh = SDL::GetTicks;
193
194 if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) {
195 SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh);
196 $next_refresh = SDL::GetTicks;
197 }
198
192 my $interval = ($next_refresh - $last_refresh) * 0.001; 199 my $interval = ($next_refresh - $last_refresh) * 0.001;
193 $last_refresh = $next_refresh; 200 $last_refresh = $next_refresh;
194 201
195 force_refresh; 202 force_refresh;
196 $_->animate ($interval) for grep $_, values %ANIMATE; 203 $_->animate ($interval) for grep $_, values %ANIMATE;
197 204
198 if (%ANIMATE) { 205 if (%ANIMATE) {
206 1
207 } else {
208 undef $refresh_handler;
209 0
199 1 210 }
200 } else { 211 } else {
201 undef $refresh_handler; 212 undef $refresh_handler;
202 0 213 0
203 } 214 }
204 }; 215 };
237 }, 248 },
238 SDL_KEYUP() => sub { 249 SDL_KEYUP() => sub {
239 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 250 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
240 }, 251 },
241 SDL_MOUSEMOTION() => sub { 252 SDL_MOUSEMOTION() => sub {
242 my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y); 253 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
243 $HOVER = $TOPLEVEL->find_widget ($x, $y);
244 $self->refresh;
245
246 warn "mouse $x, $y = $HOVER\n";
247 }, 254 },
248 SDL_MOUSEBUTTONDOWN() => sub { 255 SDL_MOUSEBUTTONDOWN() => sub {
249 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 256 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
250 }, 257 },
251 SDL_MOUSEBUTTONUP() => sub { 258 SDL_MOUSEBUTTONUP() => sub {
252 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 259 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
253 }, 260 },
254 SDL_ACTIVEEVENT() => sub { 261 SDL_ACTIVEEVENT() => sub {
255 warn "active\n";#d# 262 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
256 }, 263 },
257); 264);
258 265
259@conn::ISA = Crossfire::Protocol::; 266@conn::ISA = Crossfire::Protocol::;
260 267
397 404
398############################################################################# 405#############################################################################
399 406
400SDL::Init SDL_INIT_EVERYTHING; 407SDL::Init SDL_INIT_EVERYTHING;
401 408
402$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
403
404my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 409my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
405 410
406$TOPLEVEL->add ($mapwidget); 411$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
407$mapwidget->focus_in; 412$mapwidget->focus_in;
408 413
409Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 414Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
410 415
411$CFG ||= { 416$CFG ||= {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines