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.12 by elmex, Fri Apr 7 16:29:19 2006 UTC vs.
Revision 1.17 by root, Fri Apr 7 18:20:13 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 $VERSION = '0.1';
32 # nuke all gl context data 22
33 23our $CFG;
24our $CONN;
25
26our $SDL_TIMER;
27our $SDL_APP;
28our $SDL_EV = new SDL::Event;
29our %SDL_CB;
30our @GL_INIT;
31
32sub init_screen {
34 $app = new SDL::App 33 $SDL_APP = new SDL::App
35 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 34 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
36 -title => "Crossfire+ Client", 35 -title => "Crossfire+ Client",
37 -width => $WIDTH, 36 -width => $CFG->{width},
38 -height => $HEIGHT, 37 -height => $CFG->{height},
39 -opengl => 1, 38 -opengl => 1,
40 -red_size => 8, 39 -red_size => 8,
41 -green_size => 8, 40 -green_size => 8,
42 -blue_size => 8, 41 -blue_size => 8,
43 -double_buffer => 1, 42 -double_buffer => 1,
43 -fullscreen => $CFG->{fullscreen},
44 -resizeable => 0; 44 -resizeable => 0;
45 45
46 glEnable GL_TEXTURE_2D; 46 glEnable GL_TEXTURE_2D;
47 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 47 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
48 glShadeModel GL_FLAT; 48 glShadeModel GL_FLAT;
50 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 50 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
51 glEnable GL_BLEND; 51 glEnable GL_BLEND;
52 52
53 glMatrixMode GL_PROJECTION; 53 glMatrixMode GL_PROJECTION;
54 glLoadIdentity; 54 glLoadIdentity;
55 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; 55 glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -1 , 1;
56 56
57 # re-bind all textures 57 $_->() for @GL_INIT;
58}
59
60sub start_game {
61 $SDL_TIMER = add Glib::Timeout 1000/20, sub {
62 while ($SDL_EV->poll) {
63 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->();
64 }
65
66 1
67 };
68
69 init_screen;
70
71 $CONN = new conn
72 host => $CFG->{host},
73 port => $CFG->{port};
74}
75
76sub stop_game {
77 remove Glib::Source $SDL_TIMER;
78
79 undef $SDL_APP;
80 SDL::Quit;
58} 81}
59 82
60sub refresh { 83sub refresh {
61 glClearColor 0, 0, 0, 0; 84 glClearColor 0, 0, 0, 0;
62 glClear GL_COLOR_BUFFER_BIT; 85 glClear GL_COLOR_BUFFER_BIT;
63 86
64 my $map = $conn->{map}; 87 for (values %Client::Widget::ACTIVE_WIDGETS) {
65 88 $_->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 } 89 }
90 90
91 SDL::GLSwapBuffers; 91 SDL::GLSwapBuffers;
92} 92}
93 93
94my $ev = new SDL::Event; 94%SDL_CB = (
95my %ev_cb; 95 SDL_QUIT() => sub {
96 96 warn "sdl quit\n";#d#
97sub event(&$) {
98 $ev_cb{$_[0]->()} = $_[1];
99}
100
101sub does(&) { shift }
102
103event {SDL_QUIT} does {
104 exit; 97 exit;
105}; 98 },
106 99 SDL_VIDEORESIZE() => sub {
107event {SDL_VIDEORESIZE} does { 100 },
108 print "resize\n"; 101 SDL_VIDEOEXPOSE() => sub {
109};
110
111event {SDL_VIDEOEXPOSE} does {
112 refresh; 102 refresh;
113}; 103 },
114 104 SDL_KEYDOWN() => sub {
115event {SDL_KEYDOWN} does { 105 Client::Widget::feed_sdl_key_down_event ($SDL_EV);
116 print "keypress\n"; 106 },
117}; 107 SDL_KEYUP() => sub {
118 108 Client::Widget::feed_sdl_key_up_event ($SDL_EV);
119event {SDL_KEYUP} does { 109 },
120 print "keyup\n";#d# 110 SDL_MOUSEMOTION() => sub {
121}; 111 warn "sdl motion\n";#d#
122 112 },
123event {SDL_MOUSEMOTION} does { 113 SDL_MOUSEBUTTONDOWN() => sub {
124 print "motion\n"; 114 Client::Widget::feed_sdl_button_down_event ($SDL_EV);
125}; 115 },
126 116 SDL_MOUSEBUTTONUP() => sub {
127event {SDL_MOUSEBUTTONDOWN} does { 117 Client::Widget::feed_sdl_button_up_event ($SDL_EV);
128 print "button\n"; 118 },
129}; 119 SDL_ACTIVEEVENT() => sub {
130
131event {SDL_MOUSEBUTTONUP} does {
132 print "buttup\n";
133};
134
135event {SDL_ACTIVEEVENT} does {
136 print "active\n"; 120 warn "active\n";#d#
137}; 121 },
138 122);
139package Crossfire::Client;
140 123
141@conn::ISA = Crossfire::Protocol::; 124@conn::ISA = Crossfire::Protocol::;
142 125
143sub conn::map_update { 126sub conn::map_update {
144 my ($self, $dirty) = @_; 127 my ($self, $dirty) = @_;
159} 142}
160 143
161sub conn::face_update { 144sub conn::face_update {
162 my ($self, $num, $face) = @_; 145 my ($self, $num, $face) = @_;
163 146
164 warn "up face $self,$num,$face\n";#d#
165 use Gtk2;
166
167 my $pb = new Gtk2::Gdk::PixbufLoader;
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} 147}
196 148
197############################################################################# 149#############################################################################
198 150
199use Event; 151my $mapwidget = Client::MapWidget->new;
152
153$mapwidget->activate;
154$mapwidget->focus_in;
200 155
201Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; 156Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc";
202 157
158$CFG ||= {
159 width => 640,
160 height => 480,
161 fullscreen => 0,
162 host => "crossfire.schmorp.de",
163 port => 13327,
164};
203 165
204if ($ARGV[0] eq 'config') {
205 Client::Util::run_config_dialog; 166Client::Util::run_config_dialog
206} 167 login => sub { start_game },
168 logout => sub { stop_game };
207 169
208glinit; 170main Gtk2;
209
210$conn = new conn
211 host => "cf.schmorp.de",
212 port => 13327;
213
214Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub {
215 while ($ev->poll) {
216 ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
217 }
218});
219
220Event::loop;
221

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines