static PyObject* _gfx_texturedpolygon (PyObject *self, PyObject* args) { PyObject *surface, *texture, *points, *item, *pt; Sint16 *vx, *vy; int tmp1, tmp2, tdx, tdy; Py_ssize_t count, i; int ret; ASSERT_VIDEO_INIT (NULL); if (!PyArg_ParseTuple (args, "OOOO:textured_polygon", &surface, &points, &texture, &pt)) { PyErr_Clear (); if (!PyArg_ParseTuple (args, "OOOii:textured_polygon", &surface, &points, &texture, &tdx, &tdy)) return NULL; } else { if (!PointFromObj (pt, &tdx, &tdy)) return NULL; } if (!PySDLSurface_Check (surface)) { PyErr_SetString (PyExc_TypeError, "surface must be a Surface"); return NULL; } if (!PySDLSurface_Check (texture)) { PyErr_SetString (PyExc_TypeError, "texture must be a Surface"); return NULL; } if (!PySequence_Check (points)) { PyErr_SetString (PyExc_TypeError, "points must be a sequence"); return NULL; } count = PySequence_Size (points); if (count < 3) { PyErr_SetString (PyExc_ValueError, "points must contain more than 2 points"); return NULL; } vx = PyMem_New (Sint16, (size_t) count); vy = PyMem_New (Sint16, (size_t) count); if (!vx || !vy) { if (vx) PyMem_Free (vx); if (vy) PyMem_Free (vy); return NULL; } for (i = 0; i < count; i++) { item = PySequence_ITEM (points, i); if (!PointFromObj (item, &tmp1, &tmp2)) { PyMem_Free (vx); PyMem_Free (vy); Py_XDECREF (item); return NULL; } Py_DECREF (item); vx[i] = (Sint16)tmp1; vy[i] = (Sint16)tmp2; } Py_BEGIN_ALLOW_THREADS; ret = texturedPolygon (((PySDLSurface*)surface)->surface, vx, vy, (int)count, ((PySDLSurface*)texture)->surface, (Sint16)tdx, (Sint16)tdy); Py_END_ALLOW_THREADS; PyMem_Free (vx); PyMem_Free (vy); if (ret == -1) { PyErr_SetString (PyExc_PyGameError, SDL_GetError ()); return NULL; } Py_RETURN_NONE; }
/** * Draws a textured polygon on the surface. * @param x Array of x coordinates. * @param y Array of y coordinates. * @param n Number of points. * @param texture Texture for polygon. * @param dx X offset of texture relative to the screen. * @param dy Y offset of texture relative to the screen. */ void Surface::drawTexturedPolygon(Sint16 *x, Sint16 *y, int n, Surface *texture, int dx, int dy) { texturedPolygon(_surface, x, y, n, texture->getSurface(), dx, dy); }
void Draw(SDL_Surface *screen) { int i,rate,x,y,dx,dy; int psize = NUM_POINTS; double sin_start = 0; double sin_amp = 100; Sint16 polygon_x[NUM_POINTS], polygon_y[NUM_POINTS]; Sint16 polygon_alpha_x[4], polygon_alpha_y[4]; SDL_Surface *texture; SDL_Surface *texture_alpha; FPSmanager fpsm; int width_half = screen->w/2; int height_half = screen->h/2; /* Load texture surfaces */ texture = SDL_LoadBMP("texture.bmp"); texture_alpha = SDL_LoadBMP("texture_alpha.bmp"); SDL_SetAlpha(texture_alpha, SDL_SRCALPHA, 128); /* Initialize variables */ srand((int)time(NULL)); i=0; x=width_half; y=height_half; dx=7; dy=5; /* Initialize Framerate manager */ SDL_initFramerate(&fpsm); /* Polygon for blended texture */ polygon_alpha_x[0]= 0; polygon_alpha_y[0]= 0; polygon_alpha_x[1]= width_half; polygon_alpha_y[1]= 0; polygon_alpha_x[2]= screen->w*2 / 3; polygon_alpha_y[2]= screen->h; polygon_alpha_x[3]= 0; polygon_alpha_y[3]= screen->h; /* Set/switch framerate */ rate=25; SDL_setFramerate(&fpsm,rate); /* Drawing loop */ while (1) { /* Generate wave polygon */ sin_start++; polygon_x[0]= 0; polygon_y[0]= screen->h; polygon_x[1]= 0; polygon_y[1]= height_half; for (i=2; i < psize -2 ; i++){ polygon_x[i]= (screen->w * (i-2)) / (psize -5) ; polygon_y[i]= (Sint16)(sin(sin_start/100) * 200) + height_half - (Sint16)(sin((i + sin_start) / 20) * sin_amp); } polygon_x[psize-2]= screen->w; polygon_y[psize-2]= height_half; polygon_x[psize-1]= screen->w; polygon_y[psize-1]= screen->h; /* Event handler */ HandleEvent(); /* Black screen */ ClearScreen(screen); /* Move */ x += dx; y += dy; /* Reflect */ if ((x<0) || (x>screen->w)) { dx=-dx; } if ((y<0) || (y>screen->h)) { dy=-dy; } /* Draw */ texturedPolygon(screen,polygon_x,polygon_y,psize,texture, -(screen->w * (Sint16)(sin_start-2)) / (psize - 5), -(Sint16)(sin(sin_start/100) * 200)); texturedPolygon(screen,polygon_alpha_x,polygon_alpha_y,4,texture_alpha,(Sint16)sin_start,-(Sint16)sin_start); /* Display by flipping screens */ SDL_Flip(screen); /* Delay to fix rate */ SDL_framerateDelay(&fpsm); } }