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.23 by elmex, Fri Apr 7 20:15:01 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines