ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.15
Committed: Fri Apr 7 17:18:22 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.14: +7 -3 lines
Log Message:
added widget class

File Contents

# Content
1 #!/opt/bin/perl
2
3 use strict;
4
5 use Glib;
6 use Gtk2 -init;
7
8 use SDL;
9 use SDL::App;
10 use SDL::Event;
11 use SDL::Surface;
12 use SDL::OpenGL;
13 use SDL::OpenGL::Constants;
14
15 use Crossfire;
16 use Crossfire::Client;
17 use Crossfire::Protocol;
18
19 use Client::Util;
20 use Client::Widget;
21
22 our $VERSION = '0.1';
23
24 our $CFG;
25 our $CONN;
26
27 our $SDL_TIMER;
28 our $SDL_APP;
29 our $SDL_EV = new SDL::Event;
30 our %SDL_CB;
31
32 sub init_screen {
33 # nuke all gl context data
34
35 $SDL_APP = new SDL::App
36 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
37 -title => "Crossfire+ Client",
38 -width => $CFG->{width},
39 -height => $CFG->{height},
40 -opengl => 1,
41 -red_size => 8,
42 -green_size => 8,
43 -blue_size => 8,
44 -double_buffer => 1,
45 -fullscreen => $CFG->{fullscreen},
46 -resizeable => 0;
47
48 glEnable GL_TEXTURE_2D;
49 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
50 glShadeModel GL_FLAT;
51 glDisable GL_DEPTH_TEST;
52 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
53 glEnable GL_BLEND;
54
55 glMatrixMode GL_PROJECTION;
56 glLoadIdentity;
57 glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -1 , 1;
58
59 # re-bind all textures
60 }
61
62 sub start_game {
63 $SDL_TIMER = add Glib::Timeout 1000/20, sub {
64 while ($SDL_EV->poll) {
65 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->();
66 }
67
68 1
69 };
70
71 init_screen;
72
73 $CONN = new conn
74 host => $CFG->{host},
75 port => $CFG->{port};
76 }
77
78 sub stop_game {
79 remove Glib::Source $SDL_TIMER;
80
81 undef $SDL_APP;
82 SDL::Quit;
83 }
84
85 sub refresh {
86 glClearColor 0, 0, 0, 0;
87 glClear GL_COLOR_BUFFER_BIT;
88
89 my $map = $CONN->{map};
90
91 for my $x (0 .. $CONN->{mapw} - 1) {
92 for my $y (0 .. $CONN->{maph} - 1) {
93
94 my $cell = $map->[$x][$y]
95 or next;
96
97 my $darkness = $cell->[3] * (1 / 255);
98 glColor $darkness, $darkness, $darkness;
99
100 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
101 my $tex = $CONN->{face}[$num]{texture} || 0;
102
103 glBindTexture GL_TEXTURE_2D, $tex;
104
105 glBegin GL_QUADS;
106 glTexCoord 0, 0; glVertex $x, $y;
107 glTexCoord 0, 1; glVertex $x, $y + 1;
108 glTexCoord 1, 1; glVertex $x + 1, $y + 1;
109 glTexCoord 1, 0; glVertex $x + 1, $y;
110 glEnd;
111 }
112 }
113 }
114
115 SDL::GLSwapBuffers;
116 }
117
118 %SDL_CB = (
119 SDL_QUIT() => sub {
120 warn "sdl quit\n";#d#
121 exit;
122 },
123 SDL_VIDEORESIZE() => sub {
124 },
125 SDL_VIDEOEXPOSE() => sub {
126 refresh;
127 },
128 SDL_KEYDOWN() => sub {
129 # Client::Widget::feed_sdl_key_down_event ($SDL_EV);
130 },
131 SDL_KEYUP() => sub {
132 # Client::Widget::feed_sdl_key_up_event ($SDL_EV);
133 },
134 SDL_MOUSEMOTION() => sub {
135 warn "sdl motion\n";#d#
136 },
137 SDL_MOUSEBUTTONDOWN() => sub {
138 # Client::Widget::feed_sdl_button_down_event ($SDL_EV);
139 },
140 SDL_MOUSEBUTTONUP() => sub {
141 # Client::Widget::feed_sdl_button_up_event ($SDL_EV);
142 },
143 SDL_ACTIVEEVENT() => sub {
144 warn "active\n";#d#
145 },
146 );
147
148 @conn::ISA = Crossfire::Protocol::;
149
150 sub conn::map_update {
151 my ($self, $dirty) = @_;
152
153 refresh;
154 }
155
156 sub conn::map_scroll {
157 my ($self, $dx, $dy) = @_;
158
159 refresh;
160 }
161
162 sub conn::map_clear {
163 my ($self) = @_;
164
165 refresh;
166 }
167
168 sub conn::face_update {
169 my ($self, $num, $face) = @_;
170
171 my $pb = new Gtk2::Gdk::PixbufLoader;
172 $pb->write ($face->{image});
173 $pb->close;
174
175 $pb = $pb->get_pixbuf;
176 $pb = $pb->add_alpha (0, 0, 0, 0);
177
178 glGetError();
179 my ($tex) = @{glGenTextures 1};
180
181 $face->{texture} = $tex;
182
183 glBindTexture GL_TEXTURE_2D, $tex;
184 my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr;
185
186 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
187 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
188 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
189 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
190
191 glTexImage2D GL_TEXTURE_2D, 0,
192 GL_RGBA8,
193 $pb->get_width, $pb->get_height,
194 0,
195 GL_RGBA,
196 GL_UNSIGNED_BYTE,
197 $pb->get_pixels;
198 my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr;
199 }
200
201 #############################################################################
202
203 Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc";
204
205 $CFG ||= {
206 width => 640,
207 height => 480,
208 fullscreen => 0,
209 host => "crossfire.schmorp.de",
210 port => 13327,
211 };
212
213 Client::Util::run_config_dialog
214 login => sub { start_game },
215 logout => sub { stop_game },
216
217 main Gtk2;