File: textured_polygon.h

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (256 lines) | stat: -rw-r--r-- 7,066 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A ARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU GeneralPublic License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Textured polygon (from SDL_Gfx)
 *****************************************************************************/

#ifndef TEXTURED_POLYGON_H
#define TEXTURED_POLYGON_H

#include <SDL_gfxPrimitives.h>

static int gfxPrimitivesPolyAllocated = 0;
static int *gfxPrimitivesPolyInts = NULL;
inline int gfxPrimitivesCompareInt(const void *a, const void *b)
{
  return (*(const int *) a) - (*(const int *) b);
}

int _texturedHLine(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y,SDL_Surface *texture,int texture_dx,int texture_dy)
{
  Sint16 left, right, top, bottom;
  Sint16 w;
  Sint16 xtmp;
  int result = 0;
  int texture_x_walker;
  int texture_y_start;
  SDL_Rect source_rect,dst_rect;
  int pixels_written,write_width;

  /*
   * Get clipping boundary
   */
  left = dst->clip_rect.x;
  right = dst->clip_rect.x + dst->clip_rect.w - 1;
  top = dst->clip_rect.y;
  bottom = dst->clip_rect.y + dst->clip_rect.h - 1;

  /*
   * Check visibility of hline
   */
  if ((x1<left) && (x2<left)) {
    return(0);
  }
  if ((x1>right) && (x2>right)) {
    return(0);
  }
  if ((y<top) || (y>bottom)) {
    return (0);
  }

  /*
   * Swap x1, x2 if required
   */
  if (x1 > x2) {
    xtmp = x1;
    x1 = x2;
    x2 = xtmp;
  }

  /*
   * Clip x
   */
  if (x1 < left) {
    x1 = left;
  }
  if (x2 > right) {
    x2 = right;
  }

  /*
   * Calculate width
   */
  w = x2 - x1;

  /*
   * Sanity check on width
   */
  if (w < 0) {
    return (0);
  }

  /*
   * Determint where in the texture we start drawing
   **/
  texture_x_walker =   (x1 - texture_dx)  % texture->w;
  if (texture_x_walker < 0){
    texture_x_walker = texture->w +texture_x_walker ;
  }

  texture_y_start = (y + texture_dy) % texture->h;
  if (texture_y_start < 0){
    texture_y_start = texture->h + texture_y_start;
  }

  //setup the source rectangle  we are only drawing one horizontal line
  source_rect.y = texture_y_start;
  source_rect.x =texture_x_walker;
  source_rect.h =1;
  //we will draw to the current y
  dst_rect.y = y;

  //if there are enough pixels left in the current row of the texture
  //draw it all at once
  if (w <= texture->w -texture_x_walker){
    source_rect.w = w;
    source_rect.x = texture_x_walker;
    dst_rect.x= x1;
    result |= !SDL_BlitSurface  (texture,&source_rect , dst, &dst_rect) ;
  } else {//we need to draw multiple times
    //draw the first segment
    pixels_written = texture->w  -texture_x_walker;
    source_rect.w = pixels_written;
    source_rect.x = texture_x_walker;
    dst_rect.x= x1;
    result |= !SDL_BlitSurface  (texture,&source_rect , dst, &dst_rect);
    write_width = texture->w;

    //now draw the rest
    //set the source x to 0
    source_rect.x = 0;
    while(pixels_written < w){
      if (write_width >= w - pixels_written){
        write_width=  w- pixels_written;
      }
      source_rect.w = write_width;
      dst_rect.x = x1 + pixels_written;
      result |= !SDL_BlitSurface  (texture,&source_rect , dst, &dst_rect) ;
      pixels_written += write_width;
    }
  }
  return result;
}

/**
 * Draws a polygon filled with the given texture. this operation use SDL_BlitSurface. It supports
 * alpha drawing.
 * to get the best performance of this operation you need to make sure the texture and the dst surface have the same format
 * see  http://docs.mandragor.org/files/Common_libs_documentation/SDL/SDL_Documentation_project_en/sdlblitsurface.html
 *
 * dest the destination surface,
 * vx array of x vector components
 * vy array of x vector components
 * n the amount of vectors in the vx and vy array
 * texture the sdl surface to use to fill the polygon
 * texture_dx the offset of the texture relative to the screeen. if you move the polygon 10 pixels
 * to the left and want the texture to apear the same you need to increase the texture_dx value
 * texture_dy see texture_dx
 **/
int texturedPolygon(SDL_Surface * dst, const Sint16 * vx, const Sint16 * vy, int n, SDL_Surface * texture,int texture_dx,int texture_dy)
{
  int result;
  int i;
  int y, xa, xb;
  int minx,maxx,miny, maxy;
  int x1, y1;
  int x2, y2;
  int ind1, ind2;
  int ints;
  /*
   * Sanity check
   */
  if (n < 3) {
    return -1;
  }
  /*
   * Allocate temp array, only grow array
   */
  if (!gfxPrimitivesPolyAllocated) {
    gfxPrimitivesPolyInts = (int *) malloc(sizeof(int) * n);
    gfxPrimitivesPolyAllocated = n;
  } else {
    if (gfxPrimitivesPolyAllocated < n) {
      gfxPrimitivesPolyInts = (int *) realloc(gfxPrimitivesPolyInts, sizeof(int) * n);
      gfxPrimitivesPolyAllocated = n;
    }
  }
  miny = vy[0];
  maxy = vy[0];
  minx = vx[0];
  maxx = vx[0];
  for (i = 1; (i < n); i++) {
    if (vy[i] < miny) {
      miny = vy[i];
    } else if (vy[i] > maxy) {
      maxy = vy[i];
    }
    if (vx[i] < minx) {
      minx = vx[i];
    } else if (vx[i] > maxx) {
      maxx = vx[i];
    }
  }
  if (maxx <0 || minx > dst->w){
    return 0;
  }
  if (maxy <0 || miny > dst->h){
    return 0;
  }
  /*
   * Draw, scanning y
   */
  result = 0;
  for (y = miny; (y <= maxy); y++) {
    ints = 0;
    for (i = 0; (i < n); i++) {
      if (!i) {
        ind1 = n - 1;
        ind2 = 0;
      } else {
        ind1 = i - 1;
        ind2 = i;
      }
      y1 = vy[ind1];
      y2 = vy[ind2];
      if (y1 < y2) {
        x1 = vx[ind1];
        x2 = vx[ind2];
      } else if (y1 > y2) {
        y2 = vy[ind1];
        y1 = vy[ind2];
        x2 = vx[ind1];
        x1 = vx[ind2];
      } else {
        continue;
      }
      if ( ((y >= y1) && (y < y2)) || ((y == maxy) && (y > y1) && (y <= y2)) ) {
        gfxPrimitivesPolyInts[ints++] = ((65536 * (y - y1)) / (y2 - y1)) * (x2 - x1) + (65536 * x1);
      }
    }
    qsort(gfxPrimitivesPolyInts, ints, sizeof(int), gfxPrimitivesCompareInt);
    for (i = 0; (i < ints); i += 2) {
      xa = gfxPrimitivesPolyInts[i] + 1;
      xa = (xa >> 16) + ((xa & 32768) >> 15);
      xb = gfxPrimitivesPolyInts[i+1] - 1;
      xb = (xb >> 16) + ((xb & 32768) >> 15);
      result |= _texturedHLine(dst, xa, xb, y, texture,texture_dx,texture_dy);
    }
  }
  return (result);
}
#endif /* TEXTURED_POLYGON_H */