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