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.7 by elmex, Thu Apr 6 21:43:17 2006 UTC vs.
Revision 1.32 by elmex, Sat Apr 8 17:21:01 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8;
4 5
5use Crossfire::Client; 6use Glib;
6use Crossfire::Protocol; 7use Gtk2 -init;
7
8package Crossfire::Client; # uh, yeah
9
10use strict;
11 8
12use SDL; 9use SDL;
13use SDL::App; 10use SDL::App;
14use SDL::Event; 11use SDL::Event;
15use SDL::Surface; 12use SDL::Surface;
16use SDL::OpenGL; 13use SDL::OpenGL;
17use SDL::OpenGL::Constants; 14use SDL::OpenGL::Constants;
18 15
19my $conn; 16use Crossfire;
20my $app; 17use Crossfire::Client;
18use Crossfire::Protocol;
21 19
22my $WIDTH = 640; 20use Crossfire::Client::Widget;
23my $HEIGHT = 480;
24 21
25sub glinit { 22our $FACECACHE;
26 # nuke all gl context data 23
27 24our $VERSION = '0.1';
25
26our %GL_EXT;
27
28our $CFG;
29our $CONN;
30
31our $WIDTH;
32our $HEIGHT;
33our $FULLSCREEN;
34
35our $UIFONT;
36
37our $SDL_TIMER;
38our $SDL_APP;
39our $SDL_EV = new SDL::Event;
40our %SDL_CB;
41
42our @GL_INIT; # hooks called on every gl init
43
44our $ALT_ENTER_MESSAGE;
45
46our $tw;
47
48sub init_screen {
28 $app = new SDL::App 49 $SDL_APP = new SDL::App
29 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 50 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
30 -title => "Crossfire+ Client", 51 -title => "Crossfire+ Client",
31 -width => $WIDTH, 52 -width => $WIDTH,
32 -height => $HEIGHT, 53 -height => $HEIGHT,
33 -opengl => 1, 54 -opengl => 1,
34 -red_size => 8, 55 -red_size => 8,
35 -green_size => 8, 56 -green_size => 8,
36 -blue_size => 8, 57 -blue_size => 8,
37 -double_buffer => 1, 58 -double_buffer => 1,
59 -fullscreen => $FULLSCREEN,
38 -resizeable => 0; 60 -resizeable => 0;
39 61
62 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions;
63
64 $GL_EXT{GL_ARB_texture_non_power_of_two}
65 or warn "WARNING: non-power-of-two opengl extension required";
66
67 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40
68 or die "TTFOpenFont: $!";
69
70 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode";
71 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
72 $ALT_ENTER_MESSAGE->activate;
73
74 $tw = new Crossfire::Client::Widget::Window;
75 $tw->add (my $lbl = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!");
76# $tw = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!";
77
78 $tw->move (0, $HEIGHT - 50);
79 $tw->activate;
80
81 glClearColor 0, 0, 0, 0;
82
40 glEnable GL_TEXTURE_2D; 83 glEnable GL_TEXTURE_2D;
41 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL;
42 glShadeModel GL_FLAT; 84 glShadeModel GL_FLAT;
43 glDisable GL_DEPTH_TEST; 85 glDisable GL_DEPTH_TEST;
86 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
87
44 glMatrixMode GL_PROJECTION; 88 glMatrixMode GL_PROJECTION;
45
46 glLoadIdentity; 89 glLoadIdentity;
47 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; 90 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100;
48 91
49 # re-bind all textures 92 glMatrixMode GL_MODELVIEW;
93
94 $_->() for @GL_INIT;
50} 95}
96
97sub start_game {
98 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
99 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
100 while $SDL_EV->poll;
101
102 1
103 };
104
105 $WIDTH = $CFG->{width};
106 $HEIGHT = $CFG->{height};
107 $FULLSCREEN = 0;
108
109 init_screen;
110
111 $CONN = new conn
112 host => $CFG->{host},
113 port => $CFG->{port},
114 user => $CFG->{user},
115 pass => $CFG->{password};
116}
117
118sub stop_game {
119 remove Glib::Source $SDL_TIMER;
120
121 undef $SDL_APP;
122 SDL::Quit;
123}
124
125
126sub force_refresh {
127 glViewport 0, 0, $WIDTH, $HEIGHT;
128 glClear GL_COLOR_BUFFER_BIT;
129
130 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
131
132 SDL::GLSwapBuffers;
133}
134
135my $refresh_handler;
51 136
52sub refresh { 137sub refresh {
53 glClearColor 0.5, 0.5, 0.7, 0; 138 $refresh_handler ||= add Glib::Idle sub {
54 glClear GL_COLOR_BUFFER_BIT; 139 force_refresh;
140 undef $refresh_handler;
141 0
142 };
143}
55 144
56 my $map = $conn->{map}; 145%SDL_CB = (
57 146 SDL_QUIT() => sub {
58 for my $x (0 .. $conn->{mapw} - 1) { 147 main_quit Gtk2;
59 for my $y (0 .. $conn->{maph} - 1) { 148 },
60 149 SDL_VIDEORESIZE() => sub {
61 my $cell = $map->[$x][$y] 150 },
62 or next; 151 SDL_VIDEOEXPOSE() => sub {
63 152 refresh;
64 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 153 },
65 my $tex = $conn->{face}[$num]{texture} || 0; 154 SDL_KEYDOWN() => sub {
66 155 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
67 glBindTexture GL_TEXTURE_2D, $tex; 156 # alt-enter
68 157 $FULLSCREEN = !$FULLSCREEN;
69 glColor 1,0,1; 158 init_screen;
70 glBegin GL_QUADS; 159 } else {
71 glTexCoord 0, 0; glVertex $x, $y; 160 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
72 glTexCoord 0, 1; glVertex $x, $y + 0.9;
73 glTexCoord 1, 1; glVertex $x + 0.9, $y + 0.9;
74 glTexCoord 1, 0; glVertex $x + 0.9, $y;
75 glEnd;
76 }
77 } 161 }
78 } 162 },
79 163 SDL_KEYUP() => sub {
80 SDL::GLSwapBuffers; 164 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
81} 165 },
82 166 SDL_MOUSEMOTION() => sub {
83my $ev = new SDL::Event; 167 warn "sdl motion\n";#d#
84my %ev_cb; 168 },
85 169 SDL_MOUSEBUTTONDOWN() => sub {
86sub event(&$) { 170 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
87 $ev_cb{$_[0]->()} = $_[1]; 171 },
88} 172 SDL_MOUSEBUTTONUP() => sub {
89 173 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
90sub does(&) { shift } 174 },
91 175 SDL_ACTIVEEVENT() => sub {
92event {SDL_QUIT} does {
93 exit;
94};
95
96event {SDL_VIDEORESIZE} does {
97 print "resize\n";
98};
99
100event {SDL_VIDEOEXPOSE} does {
101 refresh;
102};
103
104event {SDL_KEYDOWN} does {
105 print "keypress\n";
106};
107
108event {SDL_KEYUP} does {
109 print "keyup\n";#d#
110};
111
112event {SDL_MOUSEMOTION} does {
113 print "motion\n";
114};
115
116event {SDL_MOUSEBUTTONDOWN} does {
117 print "button\n";
118};
119
120event {SDL_MOUSEBUTTONUP} does {
121 print "buttup\n";
122};
123
124event {SDL_ACTIVEEVENT} does {
125 print "active\n"; 176 warn "active\n";#d#
126}; 177 },
127 178);
128package Crossfire::Client;
129 179
130@conn::ISA = Crossfire::Protocol::; 180@conn::ISA = Crossfire::Protocol::;
131 181
132sub conn::map_update { 182sub conn::map_update {
133 my ($self, $dirty) = @_; 183 my ($self, $dirty) = @_;
145 my ($self) = @_; 195 my ($self) = @_;
146 196
147 refresh; 197 refresh;
148} 198}
149 199
200sub conn::face_find {
201 my ($self, $face) = @_;
202
203 $FACECACHE->{"$face->{chksum},$face->{name}"}
204}
205
150sub conn::face_update { 206sub conn::face_update {
151 my ($self, $num, $face) = @_; 207 my ($self, $face) = @_;
152 208
153 warn "up face $self,$num,$face\n";#d# 209 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
154 #TODO
155 open my $fh, ">:raw", "/tmp/x~";
156 syswrite $fh, $face->{image};
157 close $fh;
158 210
159 my $surface = new SDL::Surface -name => "/tmp/x~"; 211 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
212}
160 213
161 unlink "/tmp/x~"; 214sub gtk_add_cfg_field {
215 my ($tbl, $cfg, $klbl, $key, $value) = @_;
216 my $i = $cfg->{_i}++;
217 $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
218 $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
219 if ($key eq 'password') {
220 $ent->set_invisible_char ("*");
221 $ent->set (visibility => 0)
222 }
223 $ent->set_text ($value);
224 $ent->signal_connect (changed => sub {
225 my ($ent) = @_;
226 $cfg->{$key} = $ent->get_text;
227 });
228}
162 229
163 my ($tex) = @{glGenTextures 1}; 230sub run_config_dialog {
164 glGetError(); 231 my (%events) = @_;
165 232
166 $face->{texture} = $tex; 233 my $w = Gtk2::Window->new;
234
235 my @cfg = (
236 [qw/Host host/],
237 [qw/Port port/],
238 [qw/Username user/],
239 [qw/Password password/],
167 240 );
168 glBindTexture GL_TEXTURE_2D, $tex; 241
169 my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; 242 my $cfg = {};
243
244 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
245 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
246
247 $w->add (my $vb = Gtk2::VBox->new);
248 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
249 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
250 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
251 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
252 my $i = 0;
253 my $act = 0;
254 for (map { "$_->[0]x$_->[1]" } reverse @modes) {
255 if ($_ eq $selmode) { $act = $i }
256 $cb->append_text ($_);
257 $i++;
258 }
259 $cb->set_active ($act);
260 $cb->signal_connect (changed => sub {
261 my ($cb) = @_;
262 my $txt = $cb->get_active_text;
263 if ($txt =~ m/(\d+)x(\d+)/) {
264 $::CFG->{width} = $1;
265 $::CFG->{height} = $2;
170 266 }
171 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 267 });
172 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
173 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
174 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
175
176 $surface->rgba;
177 268
178 glTexImage2D GL_TEXTURE_2D, 0, 269 $cfg->{_i} = 1;
179 4, # components 270 for (@cfg) {
180 $surface->width, $surface->height, 271 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
272 }
273
274 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
275 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
276 $cb->signal_connect (clicked => sub {
277 for (keys %$cfg) {
278 $::CFG->{$_} = $cfg->{$_}
279 if $_ ne '_i';
181 0, 280 }
281 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
282 });
283 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
284 $cb->signal_connect (clicked => sub {
285 for (keys %$cfg) {
286 $::CFG->{$_} = $cfg->{$_}
287 if $_ ne '_i';
182 GL_RGBA, 288 }
183 GL_UNSIGNED_BYTE, 289 my $cb = $events{login} || sub {};
184 $surface->pixels; 290 $cb->($::CFG->{user}, $::CFG->{password});
185 my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; 291 });
292 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
293 $cb->signal_connect (clicked => sub {
294 my $cb = $events{login} || sub {};
295 $cb->();
296 });
297 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
298 $cb->signal_connect (clicked => sub { $w->destroy });
299
300 $w->show_all;
301
302 $w->signal_connect (destroy => sub { Gtk2->main_quit });
186} 303}
304
187 305
188############################################################################# 306#############################################################################
189 307
190use Event; 308SDL::Init SDL_INIT_EVERYTHING;
309SDL::TTFInit;
191 310
192glinit; 311my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
193 312
194$conn = new conn 313$mapwidget->activate;
314$mapwidget->focus_in;
315
316Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
317
318$CFG ||= {
319 width => 640,
320 height => 480,
321 fullscreen => 0,
195 host => "cf.schmorp.de", 322 host => "crossfire.schmorp.de",
196 port => 13327; 323 port => 13327,
197
198Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub {
199 while ($ev->poll) {
200 ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
201 }
202}); 324};
203 325
204Event::loop; 326$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
205 327
328run_config_dialog
329 login => sub { start_game },
330 logout => sub { stop_game };
206 331
332main Gtk2;
333
334Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines