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, 2 months ago) by elmex
Branch: MAIN
Changes since 1.14: +7 -3 lines
Log Message:
added widget class

File Contents

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