view demo.c @ 2:496b9ab9238c

Moar.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 21 May 2015 23:30:51 +0300
parents f339a3903bc9
children 37b33851cfbb
line wrap: on
line source

#include "dmengine.h"
#include "dmtext.h"
#include "dmfft.h"
#include "dmvecmat.h"
#include <math.h>

static int demoInit(DMEngineData *engine);
static void demoShutdown(DMEngineData *engine);
static int demoRender(DMEngineData *engine);


int demoPreInit(DMEngineData *engine)
{
    dmInitProg("bussi",
        "Bussi by AnCiat ProDucTionz",
        "0", "ENGINE INITTIALISSSIZING!!!!!", NULL);

    engine->optPackFilename  = "bussi.dat";
    engine->optDataPath      = NULL;
    engine->optResFlags      = DRF_USE_PACK | DRF_PRELOAD_RES | DRF_USE_STDIO;

    engine->optAudioSetup    = DM_ASETUP_JSS;

    engine->optVidSetup      = DM_VSETUP_ASPECT;
    engine->optVidWidth      = 640;
    engine->optVidHeight     = 480;
    engine->optVidDepth      = 32;
    engine->optVFlags        = SDL_SWSURFACE;

    engine->demoInit = demoInit;
    engine->demoRender = demoRender;
    engine->demoShutdown = demoShutdown;

    return DMERR_OK;
}


#define FFT_SIZE 128
DMFFTContext fft;


static int demoInit(DMEngineData *engine)
{
    int i;
    JSSModule *mod = NULL;

    dmInitializeFFT(&fft, FFT_SIZE);

    engineGetResModule(engine, mod, "pas2.xm");

    if ((i = jssConvertModuleForPlaying(mod)) != DMERR_OK)
    {
        dmErrorMsg("Could not convert module for playing, %d: %s\n",
            i, dmErrorStr(i));
        return DMERR_INIT_FAIL;
    }
    
    jvmSetCallback(engine->jssDev, jmpExec, engine->jssPlr);
    jmpSetModule(engine->jssPlr, mod);
    jmpPlayOrder(engine->jssPlr, 0);
    jvmSetGlobalVol(engine->jssDev, 55);

    // Jne
    srand(15);

    return DMERR_OK;
}


static void demoShutdown(DMEngineData *engine)
{
    (void) engine;
    dmEndFFT(&fft);
}


static inline float dmCX(DMEngineData *engine, const float x)
{
    return (x * engine->screen->w);
}


static inline float dmCY(DMEngineData *engine, const float y)
{
    return (y * engine->screen->h);
}


static inline float dmQX(DMEngineData *engine, SDL_Surface *img, const float x)
{
    return engine->optVidNative ? (img->w * x) : (img->w * engine->screen->w * x) / 640.0f;
}


static inline float dmQY(DMEngineData *engine, SDL_Surface *img, const float y)
{
    return engine->optVidNative ? (img->h * y) : (img->h * engine->screen->h * y) / 480.0f;
}


typedef struct
{
    char *filename;
    float xc, yc;
    SDL_Surface *img;
} DMTextItem;


static DMTextItem textItems[] =
{
    { "text01.png", 0.05, 0.10, NULL },
    { "text02.png", 0.10, 0.30, NULL },
    { "text03.png", 0.60, 0.30, NULL },
    { "text04.png", 0.20, 0.60, NULL },
    { "text05.png", 0.30, 0.70, NULL },
};

static const int ntextItems = sizeof(textItems) / sizeof(textItems[0]);


static DMTextItem textItems2[] =
{
    { "text11.png", 0.05, 0.10, NULL },
    { "text02.png", 0.30, 0.25, NULL },
    { "text12.png", 0.10, 0.40, NULL },
    { "text13.png", 0.20, 0.60, NULL },
    { "text14.png", 0.30, 0.70, NULL },
};

static const int ntextItems2 = sizeof(textItems2) / sizeof(textItems2[0]);


static DMTextItem textItems3[] =
{
    { "teki.png"   , 0.25, 0.20, NULL },
    { "mitvit.png" , 0.05, 0.40, NULL },
    { "ja.png"     , 0.05, 0.65, NULL },
    { "kemisti.png", 0.15, 0.65, NULL },
};

static const int ntextItems3 = sizeof(textItems3) / sizeof(textItems3[0]);




#define DM_RADIAL_BLUR(YC, XC) \
        DMVector p1 = { xc, yc, 0, 0 }, p2 = { cx, cy, 0, 0 }, v; \
        dm_vector_sub_r(&v, &p2, &p1); \
        dm_vector_scale(&v, scale); \
        dm_vector_add(&v, &p1); \
        if (v.y YC || v.x XC) continue; \
        DMColor *dp = pix + xc, \
                 *q = ((DMColor *)img->pixels) + ((int)(v.y) * pitch) + (int)v.x; \
        dp->r = (q->r + dp->r) / 2; \
        dp->g = (q->g + dp->g) / 2; \
        dp->b = (q->b + dp->b) / 2;



void dmRadialBlur(SDL_Surface *img, const int cx, const int cy, const DMFloat scale)
{
    const int pitch = img->pitch / sizeof(DMColor);
    int xc, yc;

#pragma omp parallel private(yc, xc) shared(img)
    {
#pragma omp sections nowait
        {
#pragma omp section
            for (yc = cy; yc >= 0; yc--)
            {
                DMColor *pix = ((DMColor *)img->pixels) + yc * pitch;
                for (xc = cx + 1; xc < img->w; xc++)
                {
                    DM_RADIAL_BLUR(< 0, >= img->w)
                }
            }

#pragma omp section
            for (yc = cy; yc >= 0; yc--)
            {
                DMColor *pix = ((DMColor *)img->pixels) + yc * pitch;
                for (xc = cx; xc > 0; xc--)
                {
                    DM_RADIAL_BLUR(< 0, < 0)
                }    
            }

#pragma omp section
            for (yc = cy + 1; yc < img->h; yc++)
            {
                DMColor *pix = ((DMColor *)img->pixels) + yc * pitch;
                for (xc = cx; xc > 0; xc--)
                {
                    DM_RADIAL_BLUR(>= img->h, < 0)
                }    
            }

#pragma omp section
            for (yc = cy + 1; yc < img->h; yc++)
            {
                DMColor *pix = ((DMColor *)img->pixels) + yc * pitch;
                for (xc = cx + 1; xc < img->w; xc++)
                {
                    DM_RADIAL_BLUR(>= img->h, >= img->w)
                }    
            }
        }
    }
}


static int hitlerText(DMEngineData *engine, const int dt, float fftPow, DMTextItem *items, const int nitems, BOOL *nollattu)
{
    int i, vis;
    if (!(*nollattu))
    {
        for (i = 0; i < nitems; i++)
            engineGetResImage(engine, items[i].img, items[i].filename);

        *nollattu = TRUE;
    }
    
    float q = fftPow * 0.05f;
    vis = dt / 1000;
    for (i = 0; i < nitems; i++)
    {
        DMTextItem *item = &items[i];
        if (i < vis)
        {
            dmScaledBlitSurface32to32TransparentGA(
                item->img,
                dmCX(engine, item->xc - q),
                dmCY(engine, item->yc - q),
                
                dmQX(engine, item->img, 1.0f + q * 2.0f),
                dmQY(engine, item->img, 1.0f + q * 2.0f),
                
                engine->screen,
                
                100 + 80 * fftPow);
        }
    }
    
    return DMERR_OK;
}


static int demoRender(DMEngineData *engine)
{
    float t = engineGetTimeDT(engine);
    static DMScaledBlitFunc cblit = NULL;

    // Do FFT
    DMFFTType fftPow = 0;
    BOOL fftOK = FALSE;
    static DMFFTType fftAmp[FFT_SIZE / 2];
    static DMFFTType fftData[FFT_SIZE];

    dmMutexLock(engine->audioStreamMutex);
    if (engine->audioStreamBuf != 0 && engine->audioStreamLen > FFT_SIZE)
    {
        int i;
        Sint16 *buf = (Sint16 *) engine->audioStreamBuf;
        for (i = 0; i < FFT_SIZE; i++)
        {
            fftData[i] = *buf;
            buf += 2;
        }
        fftOK = TRUE;
    }
    dmMutexUnlock(engine->audioStreamMutex);

    if (fftOK)
    {
        dmRealFFT(&fft, fftData);
        dmConvertFFTtoPowerAndSum(&fft, fftData, fftAmp, 1.0, &fftPow, 0.00004f);
    }

    // Demokoodi
    if (t < 5)
    {
        // Alkufeidi sisään
        int dt = engineGetTime(engine, 0);
        static DMLerpContext fadeLerp;
        static SDL_Surface *tausta = NULL;
        static BOOL nollattu = FALSE;
        if (!nollattu)
        {
            engineGetResImage(engine, tausta, "tausta_pv.png");
            cblit = dmGetScaledBlitFunc(tausta->format, engine->screen->format, DMD_NONE);

            dmLerpInit(&fadeLerp, 0, 255, 5000);
            nollattu = TRUE;
        }

        dmClearSurface(engine->screen, dmMapRGB(engine->screen, 0,0,0));

        dmScaledBlitSurface32to32TransparentGA(tausta,
            0,
            0,


    return DMERR_OK;
}