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.6 by root, Thu Apr 6 21:15:11 2006 UTC vs.
Revision 1.15 by elmex, Fri Apr 7 17:18:22 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines