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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines