ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
(Generate patch)

Comparing deliantra/Deliantra-Client/Client.xs (file contents):
Revision 1.28 by root, Thu Apr 13 00:25:28 2006 UTC vs.
Revision 1.30 by root, Thu Apr 13 23:07:56 2006 UTC

6#include <stdio.h> 6#include <stdio.h>
7 7
8#include <SDL.h> 8#include <SDL.h>
9#include <SDL_image.h> 9#include <SDL_image.h>
10#include <SDL_opengl.h> 10#include <SDL_opengl.h>
11
12#include <glib/gmacros.h>
11 13
12#include <pango/pango.h> 14#include <pango/pango.h>
13#include <pango/pangofc-fontmap.h> 15#include <pango/pangofc-fontmap.h>
14#include <pango/pangoft2.h> 16#include <pango/pangoft2.h>
15 17
17#include <sys/socket.h> 19#include <sys/socket.h>
18#include <netinet/in.h> 20#include <netinet/in.h>
19#include <netinet/tcp.h> 21#include <netinet/tcp.h>
20 22
21#include <inttypes.h> 23#include <inttypes.h>
24
25#define FOW_DARKNESS 32
26
27#define MAP_EXTEND_X 32
28#define MAP_EXTEND_Y 512
22 29
23static PangoContext *context; 30static PangoContext *context;
24static PangoFontMap *fontmap; 31static PangoFontMap *fontmap;
25 32
26typedef struct cf_layout { 33typedef struct cf_layout {
56 if (!*w) *w = 1; 63 if (!*w) *w = 1;
57 if (!*h) *h = 1; 64 if (!*h) *h = 1;
58} 65}
59 66
60typedef struct { 67typedef struct {
68 GLint name;
69 int w, h;
70 float s, t;
71} mapface;
72
73typedef struct {
74 int16_t darkness;
61 uint16_t face[3]; 75 uint16_t face[3];
62 uint8_t darkness;
63 uint8_t padding;
64} mapcell; 76} mapcell;
65 77
66typedef struct { 78typedef struct {
67 uint32_t cols; 79 int32_t c0, c1;
68 mapcell *col; 80 mapcell *col;
69} maprow; 81} maprow;
70 82
71typedef struct map { 83typedef struct map {
72 int x, y, w, h; 84 int x, y, w, h;
85 int vx, vy;
73 int faces; 86 int faces;
74 GLint *face; 87 mapface *face;
75 88
76 uint32_t rows; 89 uint32_t rows;
77 maprow *row; 90 maprow *row;
78} *CFClient__Map; 91} *CFClient__Map;
92
93static char *
94prepend (char *ptr, int sze, int inc)
95{
96 char *p;
97
98 New (0, p, sze + inc, char);
99 Zero (p, inc, char);
100 Move (ptr, p + inc, sze, char);
101 Safefree (ptr);
102
103 return p;
104}
105
106static char *
107append (char *ptr, int sze, int inc)
108{
109 Renew (ptr, sze + inc, char);
110 Zero (ptr + sze, inc, char);
111
112 return ptr;
113}
114
115#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
116#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
117
118static void
119map_clear (CFClient__Map self)
120{
121 int r;
122
123 for (r = 0; r < self->rows; r++)
124 Safefree (self->row[r].col);
125
126 Safefree (self->row);
127
128 self->x = 0;
129 self->y = 0;
130 self->vx = 0;
131 self->vy = 0;
132 self->row = 0;
133 self->rows = 0;
134}
135
136static void
137map_blank (CFClient__Map self, int x0, int y0, int w, int h)
138{
139 int x, y;
140
141 for (y = y0; y < y0 + h; y++)
142 if (y >= 0)
143 {
144 if (y >= self->rows)
145 break;
146
147 maprow *row = self->row + y;
148
149 for (x = x0; x < x0 + w; x++)
150 if (x >= row->c0)
151 {
152 if (x >= row->c1)
153 break;
154
155 mapcell *cell = row->col + (x - row->c0);
156 cell->darkness = -1;
157 cell->face [2] = 0;
158 }
159 }
160}
79 161
80MODULE = CFClient PACKAGE = CFClient 162MODULE = CFClient PACKAGE = CFClient
81 163
82PROTOTYPES: ENABLE 164PROTOTYPES: ENABLE
83 165
259 PPCODE: 341 PPCODE:
260{ 342{
261 PangoRectangle strong_pos; 343 PangoRectangle strong_pos;
262 layout_update (self); 344 layout_update (self);
263 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); 345 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
346
264 EXTEND (SP, 3); 347 EXTEND (SP, 3);
265 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); 348 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
266 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 349 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
267 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 350 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
268} 351}
334 New (0, RETVAL, 1, struct map); 417 New (0, RETVAL, 1, struct map);
335 RETVAL->x = 0; 418 RETVAL->x = 0;
336 RETVAL->y = 0; 419 RETVAL->y = 0;
337 RETVAL->w = map_width; 420 RETVAL->w = map_width;
338 RETVAL->h = map_height; 421 RETVAL->h = map_height;
422 RETVAL->vx = 0;
423 RETVAL->vy = 0;
339 RETVAL->faces = 0; 424 RETVAL->faces = 8192;
340 RETVAL->face = 0; 425 Newz (0, RETVAL->face, RETVAL->faces, mapface);
341
342 RETVAL->rows = 0; 426 RETVAL->rows = 0;
343 RETVAL->row = 0; 427 RETVAL->row = 0;
344 OUTPUT: 428 OUTPUT:
345 RETVAL 429 RETVAL
346 430
347void 431void
348DESTROY (CFClient::Map self) 432DESTROY (CFClient::Map self)
349 CODE: 433 CODE:
350{ 434{
351 int r, c; 435 map_clear (self);
352
353 Safefree (self->face); 436 Safefree (self->face);
354 for (r = 0; r < self->rows; r++) 437 Safefree (self);
438}
439
440void
441clear (CFClient::Map self)
442 CODE:
443 map_clear (self);
444
445void
446set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t)
447 CODE:
448{
449 while (self->faces < face)
355 { 450 {
356 maprow *row = self->row + r; 451 Append (mapface, self->face, self->faces, self->faces);
357 if (!row) 452 self->faces *= 2;
358 continue;
359
360 Safefree (row->col);
361 } 453 }
362 454
363 Safefree (self->row); 455 self->face [face].name = name;
364 Safefree (self); 456 self->face [face].w = w;
457 self->face [face].h = h;
458 self->face [face].s = s;
459 self->face [face].t = t;
365} 460}
366 461
462void
463scroll (CFClient::Map self, int dx, int dy)
464 CODE:
465{
466 if (dx > 0)
467 map_blank (self, self->x, self->y, dx - 1, self->h);
468 else if (dx < 0)
469 map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h);
470
471 if (dy > 0)
472 map_blank (self, self->x, self->y, self->w, dy - 1);
473 else if (dy < 0)
474 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
475
476 self->x += dx;
477 self->y += dy;
478
479 while (self->y < 0)
480 {
481 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
482
483 self->rows += MAP_EXTEND_Y;
484 self->y += MAP_EXTEND_Y;
485 }
486}
487
488void
489map1a_update (CFClient::Map self, SV *data_)
490 CODE:
491{
492 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
493 uint8_t *data_end = (uint8_t *)SvEND (data_);
494
495 while (data < data_end)
496 {
497 int flags = (data [0] << 8) + data [1]; data += 2;
498
499 int x = ((flags >> 10) & 63) + self->x;
500 int y = ((flags >> 4) & 63) + self->y;
501
502 while (y >= self->rows)
503 {
504 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
505
506 self->rows += MAP_EXTEND_Y;
507 }
508
509 maprow *row = self->row + y;
510
511 if (!row->col)
512 {
513 Newz (0, row->col, MAP_EXTEND_X, mapcell);
514 row->c0 = self->x - MAP_EXTEND_X / 4;
515 row->c1 = row->c0 + MAP_EXTEND_X;
516 }
517
518 if (row->c0 > x)
519 {
520 int extend = row->c0 - x + MAP_EXTEND_X;
521 Prepend (mapcell, row->col, row->c1 - row->c0, extend);
522 row->c0 -= extend;
523 }
524 else if (x >= row->c1)
525 {
526 int extend = x - row->c1 + MAP_EXTEND_X;
527 Append (mapcell, row->col, row->c1 - row->c0, extend);
528 row->c1 += extend;
529 }
530
531 assert (y >= 0);
532 assert (y < self->rows);
533 assert (x >= row->c0);
534 assert (x < row->c1);
535 mapcell *cell = row->col + (x - row->c0);
536
537 if (flags & 15)
538 {
539 if (cell->darkness < 0 && x < self->w && y < self->h)
540 {
541 cell->darkness = 0;
542 cell->face [0] = 0;
543 cell->face [1] = 0;
544 cell->face [2] = 0;
545 }
546
547 cell->darkness = flags & 8 ? *data++ : 255;
548
549 if (flags & 4)
550 {
551 cell->face [0] = (data [0] << 8) + data [1]; data += 2;
552 }
553
554 if (flags & 2)
555 {
556 cell->face [1] = (data [0] << 8) + data [1]; data += 2;
557 }
558
559 if (flags & 1)
560 {
561 cell->face [2] = (data [0] << 8) + data [1]; data += 2;
562 }
563 }
564 else
565 {
566 cell->darkness = -1;
567 cell->face [2] = 0;
568 }
569 }
570}
571
572void
573draw (CFClient::Map self, int x0, int y0, int sw, int sh)
574 CODE:
575{
576 int vx = self->vx = self->w >= sw
577 ? self->x + (self->w - sw) / 2
578 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
579
580 int vy = self->vy = self->h >= sh
581 ? self->y + (self->h - sh) / 2
582 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
583
584 glColor4ub (255, 255, 255, 255);
585
586 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
587 glEnable (GL_BLEND);
588 glEnable (GL_TEXTURE_2D);
589 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
590
591 int x, y, z;
592
593 int last_name = 0;
594
595 glBegin (GL_QUADS);
596
597 for (z = 0; z < 3; z++)
598 for (y = 0; y < sh; y++)
599 if (0 <= y + vy && y + vy < self->rows)
600 {
601 maprow *row = self->row + (y + vy);
602
603 for (x = 0; x < sw; x++)
604 if (row->c0 <= x + vx && x + vx < row->c1)
605 {
606 mapcell *cell = row->col + (x + vx - row->c0);
607 uint16_t face = cell->face [z];
608
609 if (face)
610 {
611 mapface tex = self->face [face];
612
613 int px = (x + 1) * 32 - tex.w;
614 int py = (y + 1) * 32 - tex.h;
615
616 if (last_name != tex.name)
617 {
618 glEnd ();
619 last_name = tex.name;
620 glBindTexture (GL_TEXTURE_2D, last_name);
621 glBegin (GL_QUADS);
622 }
623
624 glTexCoord2f (0 , 0 ); glVertex2f (px , py );
625 glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h);
626 glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h);
627 glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py );
628 }
629 }
630 }
631
632 glEnd ();
633}
634

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines