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.11 by elmex, Fri Apr 7 16:08:01 2006 UTC vs.
Revision 1.19 by root, Fri Apr 7 19:33:42 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines