Exemple #1
0
static mrb_value
mrb_sdl2_video_surface_fill_rects(mrb_state *mrb, mrb_value self)
{
    uint32_t color;
    mrb_value rects;
    mrb_get_args(mrb, "io", &color, &rects);
    if (!mrb_array_p(rects)) {
        mrb_raise(mrb, E_TYPE_ERROR, "given 2nd argument is unexpected type (expected Array).");
    }
    mrb_int const n = mrb_ary_len(mrb, rects);
    SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self);
    SDL_Rect r[n];
    mrb_int i;
    for (i = 0; i < n; ++i) {
        SDL_Rect const * const ptr = mrb_sdl2_rect_get_ptr(mrb, mrb_ary_ref(mrb, rects, i));
        if (NULL != ptr) {
            r[i] = *ptr;
        } else {
            r[i] = (SDL_Rect) {
                0, 0, 0, 0
            };
        }
    }
    if (0 != SDL_FillRects(s, r, n, color)) {
        mruby_sdl2_raise_error(mrb);
    }
    return self;
}
Exemple #2
0
static SDL_Surface*
createShadowSet()
{
	int bpp;
	Uint32 rm, gm, bm, am;

	SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_ABGR8888, &bpp, &rm, &gm, &bm, &am);
	SDL_Surface *surf = SDL_CreateRGBSurface(0, 1*32, 16*32, bpp, rm, gm, bm, am);

	std::vector<SDL_Rect> rects;
	SDL_Rect rect = { 0, 0, 16, 16 };

	for (int val = 0; val < 16; ++val)
	{
		int origY = val*32;

		/* Top left */
		if (val & (1 << 0))
		{
			rect.x = 0;
			rect.y = origY;
			rects.push_back(rect);
		}

		/* Top Right */
		if (val & (1 << 1))
		{
			rect.x = 16;
			rect.y = origY;
			rects.push_back(rect);
		}

		/* Bottom left */
		if (val & (1 << 2))
		{
			rect.x = 0;
			rect.y = origY+16;
			rects.push_back(rect);
		}

		/* Bottom right */
		if (val & (1 << 3))
		{
			rect.x = 16;
			rect.y = origY+16;
			rects.push_back(rect);
		}
	}

	/* Fill rects with half opacity black */
	uint32_t color = (0x80808080 & am);
	SDL_FillRects(surf, dataPtr(rects), rects.size(), color);

	return surf;
}
Exemple #3
0
static int
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
{
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
    SDL_Rect *final_rects;
    int i, status;

    if (!surface) {
        return -1;
    }

    final_rects = SDL_stack_alloc(SDL_Rect, count);
    if (!final_rects) {
        return SDL_OutOfMemory();
    }
    if (renderer->viewport.x || renderer->viewport.y) {
        int x = renderer->viewport.x;
        int y = renderer->viewport.y;

        for (i = 0; i < count; ++i) {
            final_rects[i].x = (int)(x + rects[i].x);
            final_rects[i].y = (int)(y + rects[i].y);
            final_rects[i].w = SDL_max((int)rects[i].w, 1);
            final_rects[i].h = SDL_max((int)rects[i].h, 1);
        }
    } else {
        for (i = 0; i < count; ++i) {
            final_rects[i].x = (int)rects[i].x;
            final_rects[i].y = (int)rects[i].y;
            final_rects[i].w = SDL_max((int)rects[i].w, 1);
            final_rects[i].h = SDL_max((int)rects[i].h, 1);
        }
    }

    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
        Uint32 color = SDL_MapRGBA(surface->format,
                                   renderer->r, renderer->g, renderer->b,
                                   renderer->a);
        status = SDL_FillRects(surface, final_rects, count, color);
    } else {
        status = SDL_BlendFillRects(surface, final_rects, count,
                                    renderer->blendMode,
                                    renderer->r, renderer->g, renderer->b,
                                    renderer->a);
    }
    SDL_stack_free(final_rects);

    return status;
}
Exemple #4
0
void ex2() {
    SDL_SetWindowTitle( SDL_GL_GetCurrentWindow(), "ex2" );
    SDL_Color c;
    c.r = 40;
    c.g = 225;
    c.b = 40;
    c.a = 255;

    SDL_Rect rects[10];

    for ( int i = 0; i < 10; i++ ) {
        SDL_Rect r;
        r.x = rand()%WIN_W;
        r.y = rand()%WIN_H;
        r.w = 50;
        r.h = 100;

        bool colliding = false;
        for ( int j = i-1; j >= 0; j-- ) {
            SDL_Rect r2 = rects[j];
            int cx1 = r.x + (r.w/2);
            int cx2 = r2.x + (r2.w/2);
            int cy1 = r.y + (r.y/2);
            int cy2 = r2.y + (r2.y/2);

            int xDist = abs( cx1 - cx2 );
            int yDist = abs( cy1 - cy2 );
            int touchDistX = (r.w/2) + (r2.w/2);
            int touchDistY = (r.h/2) + (r2.h/2);

            if ( xDist <= touchDistX || yDist <= touchDistY ) {
                colliding = true;
                break;
            }
        }

        if ( colliding ) {
            i--;
        } else {
            rects[i] = r;
        }
    }

    SDL_FillRects( backBuffer, rects, 10, SDL_MapRGB( backBuffer->format, c.r, c.g, c.b ) );
}
Exemple #5
0
static int
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects, int count)
{
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
    SDL_Rect *temp = NULL;
    int status;

    if (!surface) {
        return -1;
    }

    if (renderer->viewport.x || renderer->viewport.y) {
        int i;
        int x = renderer->viewport.x;
        int y = renderer->viewport.y;

        temp = SDL_stack_alloc(SDL_Rect, count);
        for (i = 0; i < count; ++i) {
            temp[i].x = x + rects[i].x;
            temp[i].y = y + rects[i].y;
            temp[i].w = rects[i].w;
            temp[i].h = rects[i].h;
        }
        rects = temp;
    }

    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
        Uint32 color = SDL_MapRGBA(surface->format,
                                   renderer->r, renderer->g, renderer->b,
                                   renderer->a);
        status = SDL_FillRects(surface, rects, count, color);
    } else {
        status = SDL_BlendFillRects(surface, rects, count,
                                    renderer->blendMode,
                                    renderer->r, renderer->g, renderer->b,
                                    renderer->a);
    }

    if (temp) {
        SDL_stack_free(temp);
    }
    return status;
}
static int
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
                   int count)
{
    SDL_Surface *surface = SW_ActivateRenderer(renderer);

    if (!surface) {
        return -1;
    }

    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
        Uint32 color = SDL_MapRGBA(surface->format,
                                   renderer->r, renderer->g, renderer->b,
                                   renderer->a);
        return SDL_FillRects(surface, rects, count, color);
    } else {
        return SDL_BlendFillRects(surface, rects, count,
                                     renderer->blendMode,
                                     renderer->r, renderer->g, renderer->b,
                                     renderer->a);
    }
}
static int
SDL_DUMMY_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
                          int count)
{
    SDL_DUMMY_RenderData *data =
        (SDL_DUMMY_RenderData *) renderer->driverdata;
    SDL_Surface *target = data->screens[data->current_screen];

    if (renderer->blendMode == SDL_BLENDMODE_NONE ||
        renderer->blendMode == SDL_BLENDMODE_MASK) {
        Uint32 color = SDL_MapRGBA(target->format,
                                   renderer->r, renderer->g, renderer->b,
                                   renderer->a);

        return SDL_FillRects(target, rects, count, color);
    } else {
        return SDL_BlendFillRects(target, rects, count,
                                  renderer->blendMode,
                                  renderer->r, renderer->g, renderer->b,
                                  renderer->a);
    }
}
Exemple #8
0
static int
SW_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
{
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
    const SDL_Rect *viewport = NULL;
    const SDL_Rect *cliprect = NULL;

    if (!surface) {
        return -1;
    }

    while (cmd) {
        switch (cmd->command) {
            case SDL_RENDERCMD_SETDRAWCOLOR: {
                break;  /* Not used in this backend. */
            }

            case SDL_RENDERCMD_SETVIEWPORT: {
                viewport = &cmd->data.viewport.rect;
                SDL_SetClipRect(data->surface, viewport);
                break;
            }

            case SDL_RENDERCMD_SETCLIPRECT: {
                SDL_assert(viewport != NULL);
                cliprect = cmd->data.cliprect.enabled ? &cmd->data.cliprect.rect : NULL;
                if (cliprect) {
                    SDL_Rect clip_rect;
                    clip_rect.x = cliprect->x + viewport->x;
                    clip_rect.y = cliprect->y + viewport->y;
                    clip_rect.w = cliprect->w;
                    clip_rect.h = cliprect->h;
                    SDL_IntersectRect(viewport, &clip_rect, &clip_rect);
                    SDL_SetClipRect(surface, &clip_rect);
                } else {
                    SDL_SetClipRect(surface, viewport);
                }
                break;
            }

            case SDL_RENDERCMD_CLEAR: {
                const Uint8 r = cmd->data.color.r;
                const Uint8 g = cmd->data.color.g;
                const Uint8 b = cmd->data.color.b;
                const Uint8 a = cmd->data.color.a;
                const SDL_Rect clip_rect = surface->clip_rect;
                /* By definition the clear ignores the clip rect */
                SDL_SetClipRect(surface, NULL);
                SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, r, g, b, a));
                SDL_SetClipRect(surface, &clip_rect);
                break;
            }

            case SDL_RENDERCMD_DRAW_POINTS: {
                const Uint8 r = cmd->data.draw.r;
                const Uint8 g = cmd->data.draw.g;
                const Uint8 b = cmd->data.draw.b;
                const Uint8 a = cmd->data.draw.a;
                const int count = (int) cmd->data.draw.count;
                const SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
                const SDL_BlendMode blend = cmd->data.draw.blend;
                if (blend == SDL_BLENDMODE_NONE) {
                    SDL_DrawPoints(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
                } else {
                    SDL_BlendPoints(surface, verts, count, blend, r, g, b, a);
                }
                break;
            }

            case SDL_RENDERCMD_DRAW_LINES: {
                const Uint8 r = cmd->data.draw.r;
                const Uint8 g = cmd->data.draw.g;
                const Uint8 b = cmd->data.draw.b;
                const Uint8 a = cmd->data.draw.a;
                const int count = (int) cmd->data.draw.count;
                const SDL_Point *verts = (SDL_Point *) (((Uint8 *) vertices) + cmd->data.draw.first);
                const SDL_BlendMode blend = cmd->data.draw.blend;
                if (blend == SDL_BLENDMODE_NONE) {
                    SDL_DrawLines(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
                } else {
                    SDL_BlendLines(surface, verts, count, blend, r, g, b, a);
                }
                break;
            }

            case SDL_RENDERCMD_FILL_RECTS: {
                const Uint8 r = cmd->data.draw.r;
                const Uint8 g = cmd->data.draw.g;
                const Uint8 b = cmd->data.draw.b;
                const Uint8 a = cmd->data.draw.a;
                const int count = (int) cmd->data.draw.count;
                const SDL_Rect *verts = (SDL_Rect *) (((Uint8 *) vertices) + cmd->data.draw.first);
                const SDL_BlendMode blend = cmd->data.draw.blend;
                if (blend == SDL_BLENDMODE_NONE) {
                    SDL_FillRects(surface, verts, count, SDL_MapRGBA(surface->format, r, g, b, a));
                } else {
                    SDL_BlendFillRects(surface, verts, count, blend, r, g, b, a);
                }
                break;
            }

            case SDL_RENDERCMD_COPY: {
                SDL_Rect *verts = (SDL_Rect *) (((Uint8 *) vertices) + cmd->data.draw.first);
                const SDL_Rect *srcrect = verts;
                SDL_Rect *dstrect = verts + 1;
                SDL_Texture *texture = cmd->data.draw.texture;
                SDL_Surface *src = (SDL_Surface *) texture->driverdata;

                PrepTextureForCopy(cmd);

                if ( srcrect->w == dstrect->w && srcrect->h == dstrect->h ) {
                    SDL_BlitSurface(src, srcrect, surface, dstrect);
                } else {
                    /* If scaling is ever done, permanently disable RLE (which doesn't support scaling)
                     * to avoid potentially frequent RLE encoding/decoding.
                     */
                    SDL_SetSurfaceRLE(surface, 0);
                    SDL_BlitScaled(src, srcrect, surface, dstrect);
                }
                break;
            }

            case SDL_RENDERCMD_COPY_EX: {
                const CopyExData *copydata = (CopyExData *) (((Uint8 *) vertices) + cmd->data.draw.first);
                PrepTextureForCopy(cmd);
                SW_RenderCopyEx(renderer, surface, cmd->data.draw.texture, &copydata->srcrect,
                                &copydata->dstrect, copydata->angle, &copydata->center, copydata->flip);
                break;
            }

            case SDL_RENDERCMD_NO_OP:
                break;
        }

        cmd = cmd->next;
    }

    return 0;
}