1 |
#!/opt/bin/perl |
2 |
use strict; |
3 |
|
4 |
use Glib; |
5 |
use Gtk2 -init; |
6 |
|
7 |
use SDL; |
8 |
use SDL::App; |
9 |
use SDL::Event; |
10 |
use SDL::Surface; |
11 |
use SDL::OpenGL; |
12 |
use SDL::OpenGL::Constants; |
13 |
|
14 |
use Crossfire; |
15 |
use Crossfire::Client; |
16 |
use Crossfire::Protocol; |
17 |
|
18 |
use Client::Util; |
19 |
use Client::Widget; |
20 |
|
21 |
our $FACECACHE; |
22 |
|
23 |
our $VERSION = '0.1'; |
24 |
|
25 |
our $CFG; |
26 |
our $CONN; |
27 |
|
28 |
our $SDL_TIMER; |
29 |
our $SDL_APP; |
30 |
our $SDL_EV = new SDL::Event; |
31 |
our %SDL_CB; |
32 |
|
33 |
our @GL_INIT; # hooks called on every gl init |
34 |
|
35 |
sub init_screen { |
36 |
$SDL_APP = new SDL::App |
37 |
-flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
38 |
-title => "Crossfire+ Client", |
39 |
-width => $CFG->{width}, |
40 |
-height => $CFG->{height}, |
41 |
-opengl => 1, |
42 |
-red_size => 8, |
43 |
-green_size => 8, |
44 |
-blue_size => 8, |
45 |
-double_buffer => 1, |
46 |
-fullscreen => $CFG->{fullscreen}, |
47 |
-resizeable => 0; |
48 |
|
49 |
glClearColor 0, 0, 0, 0; |
50 |
|
51 |
glEnable GL_TEXTURE_2D; |
52 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
53 |
glShadeModel GL_FLAT; |
54 |
glDisable GL_DEPTH_TEST; |
55 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
56 |
glEnable GL_BLEND; |
57 |
|
58 |
glMatrixMode GL_PROJECTION; |
59 |
glLoadIdentity; |
60 |
glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -100 , 100; |
61 |
|
62 |
glMatrixMode GL_MODELVIEW; |
63 |
|
64 |
$_->() for @GL_INIT; |
65 |
} |
66 |
|
67 |
sub 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 |
|
83 |
sub stop_game { |
84 |
remove Glib::Source $SDL_TIMER; |
85 |
|
86 |
undef $SDL_APP; |
87 |
SDL::Quit; |
88 |
} |
89 |
|
90 |
sub refresh { |
91 |
glClear GL_COLOR_BUFFER_BIT; |
92 |
|
93 |
for (values %Client::Widget::ACTIVE_WIDGETS) { |
94 |
$_->draw |
95 |
} |
96 |
|
97 |
SDL::GLSwapBuffers; |
98 |
} |
99 |
|
100 |
%SDL_CB = ( |
101 |
SDL_QUIT() => sub { |
102 |
warn "sdl quit\n";#d# |
103 |
main_quit Gtk2; |
104 |
}, |
105 |
SDL_VIDEORESIZE() => sub { |
106 |
}, |
107 |
SDL_VIDEOEXPOSE() => sub { |
108 |
refresh; |
109 |
}, |
110 |
SDL_KEYDOWN() => sub { |
111 |
if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
112 |
# alt-enter |
113 |
$CFG->{fullscreen} = !$CFG->{fullscreen}; |
114 |
init_screen; |
115 |
} else { |
116 |
Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
117 |
} |
118 |
}, |
119 |
SDL_KEYUP() => sub { |
120 |
Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
121 |
}, |
122 |
SDL_MOUSEMOTION() => sub { |
123 |
warn "sdl motion\n";#d# |
124 |
}, |
125 |
SDL_MOUSEBUTTONDOWN() => sub { |
126 |
Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
127 |
}, |
128 |
SDL_MOUSEBUTTONUP() => sub { |
129 |
Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
130 |
}, |
131 |
SDL_ACTIVEEVENT() => sub { |
132 |
warn "active\n";#d# |
133 |
}, |
134 |
); |
135 |
|
136 |
@conn::ISA = Crossfire::Protocol::; |
137 |
|
138 |
sub conn::map_update { |
139 |
my ($self, $dirty) = @_; |
140 |
|
141 |
refresh; |
142 |
} |
143 |
|
144 |
sub conn::map_scroll { |
145 |
my ($self, $dx, $dy) = @_; |
146 |
|
147 |
refresh; |
148 |
} |
149 |
|
150 |
sub conn::map_clear { |
151 |
my ($self) = @_; |
152 |
|
153 |
refresh; |
154 |
} |
155 |
|
156 |
sub conn::face_find { |
157 |
my ($self, $face) = @_; |
158 |
|
159 |
$FACECACHE->{"$face->{chksum},$face->{name}"} |
160 |
} |
161 |
|
162 |
sub conn::face_update { |
163 |
my ($self, $face) = @_; |
164 |
|
165 |
$FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
166 |
|
167 |
$face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
168 |
} |
169 |
|
170 |
############################################################################# |
171 |
|
172 |
my $mapwidget = Client::MapWidget->new; |
173 |
|
174 |
$mapwidget->activate; |
175 |
$mapwidget->focus_in; |
176 |
|
177 |
Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; |
178 |
|
179 |
$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
180 |
|
181 |
$CFG ||= { |
182 |
width => 640, |
183 |
height => 480, |
184 |
fullscreen => 0, |
185 |
host => "crossfire.schmorp.de", |
186 |
port => 13327, |
187 |
}; |
188 |
|
189 |
Client::Util::run_config_dialog |
190 |
login => sub { start_game }, |
191 |
logout => sub { stop_game }; |
192 |
|
193 |
main Gtk2; |
194 |
|
195 |
Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |