view dmgfx.c @ 49:033c660c25f5

Restructure module playing, removing 8bit sample mixing (output can still be 8bit, but samples are internally upconverted to 16bit after module loading.) Also prepare for floating point mixing support.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 01 Oct 2012 02:51:41 +0300
parents 32250b436bca
children 1ab3fd8b9afc
line wrap: on
line source

#include "dmlib.h"


void dmFillRect(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint32 col)
{
    SDL_Rect rc;
    rc.x = x0;
    rc.y = y0;
    rc.w = x1 - x0 + 1;
    rc.h = y1 - y0 + 1;
    SDL_FillRect(screen, &rc, col);
}

#define DM_SWAP(T, A, B) { if ((B) < (A)) { T swtmp = (B); B = (A); A = swtmp; } }

void dmDrawHLine(SDL_Surface *screen, int x0, int x1, int yc, const Uint32 col)
{
    int x;
    const int bpp = screen->format->BytesPerPixel,
              cx0 = screen->clip_rect.x, cy0 = screen->clip_rect.y,
              cx1 = screen->clip_rect.x + screen->clip_rect.w - 1,
              cy1 = screen->clip_rect.y + screen->clip_rect.h - 1;
    
    DM_SWAP(int, x0, x1);
    if (yc < cy0|| yc > cy1 || x1 < cx0 || x0 > cx1) return;
    if (x0 < cx0) x0 = cx0;
    if (x1 > cx1) x1 = cx1;
    
    Uint8 *pix = screen->pixels + yc * screen->pitch + (x0 * bpp);
    switch (screen->format->BitsPerPixel)
    {
        case 8:
            for (x = x0; x <= x1; x++)
                *pix++ = col;
            break;
        
        case 15:
        case 16:
            {
            Uint16 *p = (Uint16 *) pix;
            for (x = x0; x <= x1; x++)
                *p++ = col;
            }
            break;

        case 24:
            for (x = x0; x <= x1; x++)
            {
                *pix++ = col;
                *pix++ = col;
                *pix++ = col;
            }
            break;
        
        case 32:
            {
            Uint32 *p = (Uint32 *) pix;
            for (x = x0; x <= x1; x++)
                *p++ = col;
            }
            break;
    }
}


void dmDrawVLine(SDL_Surface *screen, int y0, int y1, int xc, const Uint32 col)
{
    int y;
    const int bpp = screen->format->BytesPerPixel,
              pitch = screen->pitch / bpp,
              cx0 = screen->clip_rect.x, cy0 = screen->clip_rect.y,
              cx1 = screen->clip_rect.x + screen->clip_rect.w - 1,
              cy1 = screen->clip_rect.y + screen->clip_rect.h - 1;

    DM_SWAP(int, y0, y1);
    if (xc < cx0 || xc > cx1 || y1 < cy0 || y0 > cy1) return;
    if (y0 < cy0) y0 = cy0;
    if (y1 > cy1) y1 = cy1;

    Uint8 *pix = screen->pixels + y0 * screen->pitch + (xc * bpp);
    switch (screen->format->BitsPerPixel)
    {
        case 8:
            for (y = y0; y <= y1; y++, pix += pitch)
                *pix = col;
            break;
        
        case 16:
        case 15:
            {
            Uint16 *p = (Uint16 *) pix;
            for (y = y0; y <= y1; y++, p += pitch)
                *p = col;
            }
            break;

        case 24:
            for (y = y0; y <= y1; y++, pix += pitch)
                pix[0] = pix[1] = pix[2] = col;
            break;
        
        case 32:
            {
            Uint32 *p = (Uint32 *) pix;
            for (y = y0; y <= y1; y++, p += pitch)
                *p = col;
            }
            break;
    }
}


void dmDrawBox3D(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint32 bgcol, Uint32 ucol, Uint32 dcol)
{
    SDL_Rect rc;

    rc.x = x0 + 1;
    rc.y = y0 + 1;
    rc.w = x1 - x0 - 1;
    rc.h = y1 - y0 - 1;
    SDL_FillRect(screen, &rc, bgcol);

    dmDrawHLine(screen, x0    , x1 - 1, y0, ucol);
    dmDrawHLine(screen, x0 + 1, x1    , y1, dcol);

    dmDrawVLine(screen, y0    , y1 - 1, x0, ucol);
    dmDrawVLine(screen, y0 + 1, y1    , x1, dcol);
}


void dmClearSurface(SDL_Surface *screen, const Uint32 col)
{
    SDL_FillRect(screen, NULL, col);
}


Uint32 dmMapRGB(SDL_Surface *screen, int r, int g, int b)
{
    return SDL_MapRGB(screen->format, r, g, b);
}


Uint32 dmMapRGBA(SDL_Surface *screen, int r, int g, int b, int a)
{
    return SDL_MapRGBA(screen->format, r, g, b, a);
}


int dmDirectBlitSurface(SDL_Surface *bmp, SDL_Surface *screen)
{
    return SDL_BlitSurface(bmp, NULL, screen, NULL);
}