Esempio n. 1
0
cairo_surface_t * surface_new_from_file (const gchar * name)
{
    GError * error = NULL;
    GdkPixbuf * p = gdk_pixbuf_new_from_file (name, & error);
    if (error)
        fprintf (stderr, "Error loading %s: %s.\n", name, error->message);
    if (! p)
        return NULL;

    cairo_surface_t * surface = surface_new (gdk_pixbuf_get_width (p),
     gdk_pixbuf_get_height (p));
    cairo_t * cr = cairo_create (surface);

    gdk_cairo_set_source_pixbuf (cr, p, 0, 0);
    cairo_paint (cr);

    cairo_destroy (cr);
    g_object_unref (p);
    return surface;
}
Esempio n. 2
0
INT WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine, int shown)
{
    LARGE_INTEGER freq;
    long long start;
    long long dt;

    /* GUI */
    struct gui_input in;
    struct gui_font font;
    struct demo_gui gui;

    /* Window */
    QueryPerformanceFrequency(&freq);
    xw.wc.style = CS_HREDRAW|CS_VREDRAW;
    xw.wc.lpfnWndProc = wnd_proc;
    xw.wc.hInstance = hInstance;
    xw.wc.lpszClassName = "GUI";
    RegisterClass(&xw.wc);
    xw.hWnd = CreateWindowEx(
        0, xw.wc.lpszClassName, "Demo",
        WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT,
        WINDOW_WIDTH, WINDOW_HEIGHT,
        0, 0, hInstance, 0);

    xw.hdc = GetDC(xw.hWnd);
    GetClientRect(xw.hWnd, &xw.rect);
    xw.backbuffer = surface_new(xw.hdc, xw.rect.right, xw.rect.bottom);
    xw.font = font_new(xw.hdc, "Times New Roman", 14);
    xw.width = xw.rect.right;
    xw.height = xw.rect.bottom;

    /* GUI */
    memset(&in, 0, sizeof in);
    memset(&gui, 0, sizeof gui);
    font.userdata.ptr = &xw;
    font.height = (gui_float)xw.font->height;
    font.width = font_get_text_width;
    gui.running = gui_true;
    gui.memory = malloc(MAX_MEMORY);
    init_demo(&gui, &font);

    while (gui.running && !quit) {
        /* Input */
        MSG msg;
        start = timestamp(freq);
        gui_input_begin(&in);
        while (PeekMessage(&msg, xw.hWnd, 0, 0, PM_REMOVE)) {
            if (msg.message == WM_KEYDOWN) key(&in, &msg, gui_true);
            else if (msg.message == WM_KEYUP) key(&in, &msg, gui_false);
            else if (msg.message == WM_LBUTTONDOWN) btn(&in, &msg, gui_true);
            else if (msg.message == WM_LBUTTONUP) btn(&in, &msg, gui_false);
            else if (msg.message == WM_MOUSEMOVE) motion(&in, &msg);
            else if (msg.message == WM_CHAR) text(&in, &msg);
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        gui_input_end(&in);

        /* GUI */
        run_demo(&gui, &in);

        /* Draw */
        surface_begin(xw.backbuffer);
        surface_clear(xw.backbuffer, 100, 100, 100);
        draw(xw.backbuffer, &gui.stack);
        surface_end(xw.backbuffer, xw.hdc);

        /* Timing */
        dt = timestamp(freq) - start;
        if (dt < DTIME) Sleep(DTIME - dt);
    }

    free(gui.memory);
    font_del(xw.font);
    surface_del(xw.backbuffer);
    ReleaseDC(xw.hWnd, xw.hdc);
    return 0;
}
Esempio n. 3
0
/* was geo_fxyd */
static int
geo_fit_xy(
        geomap_fit_t* const fit,
        surface_t* const sf1,
        surface_t* const sf2,
        const size_t ncoord,
        const int xfit,
        const coord_t* const input,
        const coord_t* const ref,
        /* Output */
        int* has_secondary,
        double* const weights,
        double* const residual,
        stimage_error_t* error) {

    bbox_t              bbox;
    double*             zfit      = NULL;
    const double* const z = (double*)input + (xfit ? 0 : 1);
    surface_t           savefit;
    surface_fit_error_e fit_error = surface_fit_error_ok;
    size_t              i         = 0;
    int                 status    = 1;

    assert(fit);
    assert(sf1);
    assert(sf2);
    assert(ref);
    assert(weights);
    assert(residual);
    assert(has_secondary);
    assert(error);

    surface_new(&savefit);

    surface_free(sf1);
    surface_free(sf2);

    *has_secondary = 1;

    zfit = malloc_with_error(ncoord * sizeof(double), error);
    if (zfit == NULL) goto exit;

    bbox_copy(&fit->bbox, &bbox);
    bbox_make_nonsingular(&bbox);

    if (xfit) {
        switch(fit->fit_geometry) {
        case geomap_fit_shift:
            if (surface_init(
                        &savefit, fit->function, 2, 2, xterms_none, &bbox,
                        error)) goto exit;
            surface_free(sf1);
            if (surface_init(
                        sf1, fit->function, 1, 1, xterms_none, &bbox,
                        error)) goto exit;
            for (i = 0; i < ncoord; ++i) {
                zfit[i] = z[i<<1] - ref[i].x;
            }

            if (surface_fit(
                        sf1, ncoord, ref, zfit, weights,
                        surface_fit_weight_user, &fit_error, error)) goto exit;

            if (fit->function == surface_type_polynomial) {
                savefit.coeff[0] = sf1->coeff[0];
                savefit.coeff[1] = 1.0;
                savefit.coeff[2] = 0.0;
            } else {
                savefit.coeff[0] = sf1->coeff[0] + (bbox.max.x + bbox.min.x) / 2.0;
                savefit.coeff[1] = (bbox.max.x - bbox.min.x) / 2.0;
                savefit.coeff[2] = 0.0;
            }
            surface_free(sf1);
            if (surface_copy(&savefit, sf1, error)) goto exit;
            *has_secondary = 0;
            break;

        case geomap_fit_xyscale:
            if (surface_init(
                        sf1, fit->function, 2, 1, xterms_none, &bbox,
                        error)) goto exit;
            if (surface_fit(
                        sf1, ncoord, ref, z, weights,
                        surface_fit_weight_user, &fit_error, error)) goto exit;
            *has_secondary = 0;
            break;

        default:
            if (surface_init(
                        sf1, fit->function, 2, 2, xterms_none, &bbox,
                        error)) goto exit;
            if (surface_fit(
                        sf1, ncoord, ref, z, weights,
                        surface_fit_weight_user, &fit_error, error)) goto exit;

            if (fit->xxorder > 2 || fit->xyorder > 2 ||
                fit->xxterms == xterms_full) {
                if (surface_init(
                            sf2, fit->function, fit->xxorder, fit->xyorder,
                            fit->xxterms, &fit->bbox, error)) {
                    surface_free(sf1);
                    goto exit;
                }
            } else {
                *has_secondary = 0;
            }
            break;
        }
    } else {
        switch(fit->fit_geometry) {
        case geomap_fit_shift:
            if (surface_init(
                        &savefit, fit->function, 2, 2, xterms_none, &bbox,
                        error)) goto exit;
            surface_free(sf1);
            if (surface_init(
                        sf1, fit->function, 1, 1, xterms_none, &bbox,
                        error)) goto exit;
            for (i = 0; i < ncoord; ++i) {
                zfit[i] = z[i<<1] - ref[i].y;
            }
            if (surface_fit(
                        sf1, ncoord, ref, zfit, weights,
                        surface_fit_weight_user, &fit_error, error)) goto exit;
            if (fit->function == surface_type_polynomial) {
                savefit.coeff[0] = sf1->coeff[0];
                savefit.coeff[1] = 0.0;
                savefit.coeff[2] = 1.0;
            } else {
                savefit.coeff[0] = sf1->coeff[0] + (bbox.min.y + bbox.max.y) / 2.0;
                savefit.coeff[1] = 0.0;
                savefit.coeff[2] = (bbox.max.y - bbox.min.y) / 2.0;
            }
            surface_free(sf1);
            if (surface_copy(&savefit, sf1, error)) goto exit;

            *has_secondary = 0;
            break;

        case geomap_fit_xyscale:
            if (surface_init(
                        sf1, fit->function, 1, 2, xterms_none, &bbox,
                        error)) goto exit;
            if (surface_fit(
                        sf1, ncoord, ref, z, weights,
                        surface_fit_weight_user, &fit_error, error)) goto exit;
            *has_secondary = 0;
            break;

        default:
            if (surface_init(
                        sf1, fit->function, 2, 2, xterms_none, &bbox,
                        error)) goto exit;
            if (surface_fit(
                        sf1, ncoord, ref, z, weights,
                        surface_fit_weight_user, &fit_error, error)) goto exit;
            if (fit->yxorder > 2 || fit->yyorder > 2 ||
                fit->yxterms == xterms_full) {
                if (surface_init(
                            sf2, fit->function, fit->yxorder, fit->yyorder,
                            fit->yxterms, &bbox, error)) goto exit;
            } else {
                *has_secondary = 0;
            }

            break;
        }
    }

    if (_geo_fit_xy_validate_fit_error(
                fit_error, xfit, fit->projection, error)) goto exit;

    if (surface_vector(sf1, ncoord, ref, residual, error)) goto exit;
    for (i = 0; i < ncoord; ++i) {
        residual[i] = z[i<<1] - residual[i];
    }

    /* Calculate the higher-order fit */
    if (*has_secondary) {
        if (surface_fit(
                    sf2, ncoord, ref, residual, weights,
                    surface_fit_weight_user, &fit_error, error)) goto exit;
        if (_geo_fit_xy_validate_fit_error(
                    fit_error, xfit, fit->projection, error)) goto exit;

        if (surface_vector(sf2, ncoord, ref, zfit, error)) goto exit;
        for (i = 0; i < ncoord; ++i) {
            residual[i] = zfit[i] - residual[i];
        }
    }

    /* Compute the number of zero weighted points */
    fit->n_zero_weighted = count_zero_weighted(ncoord, weights);

    /* Calculate the RMS of the fit */
    if (xfit) {
        fit->xrms = 0.0;
        for (i = 0; i < ncoord; ++i) {
            fit->xrms += weights[i] * residual[i] * residual[i];
        }
    } else {
        fit->yrms = 0.0;
        for (i = 0; i < ncoord; ++i) {
            fit->yrms += weights[i] * residual[i] * residual[i];
        }
    }

    fit->ncoord = ncoord;

    status = 0;

 exit:

    surface_free(&savefit);
    free(zfit);

    return status;
}
Esempio n. 4
0
entity_t* pl_new(key_state_t* ks, uint32_t c)
{
   anim_pl_stand_left.surface = surface_new("/usr/share/nogg/pl_stand_left.png");
   anim_pl_stand_left.t = 0;
   anim_pl_stand_left.p = 240;
   anim_pl_stand_left.w = 48;
   anim_pl_stand_left.h = 48;

   anim_pl_stand_right.surface = surface_new("/usr/share/nogg/pl_stand_right.png");
   anim_pl_stand_right.t = 0;
   anim_pl_stand_right.p = 240;
   anim_pl_stand_right.w = 48;
   anim_pl_stand_right.h = 48;

   anim_pl_run_left.surface = surface_new("/usr/share/nogg/pl_run_left.png");
   anim_pl_run_left.t = 0;
   anim_pl_run_left.p = 3;
   anim_pl_run_left.w = 48;
   anim_pl_run_left.h = 48;

   anim_pl_run_right.surface = surface_new("/usr/share/nogg/pl_run_right.png");
   anim_pl_run_right.t = 0;
   anim_pl_run_right.p = 3;
   anim_pl_run_right.w = 48;
   anim_pl_run_right.h = 48;

   anim_pl_walk_left.surface = surface_new("/usr/share/nogg/pl_walk_left.png");
   anim_pl_walk_left.t = 0;
   anim_pl_walk_left.p = 9;
   anim_pl_walk_left.w = 48;
   anim_pl_walk_left.h = 48;

   anim_pl_walk_right.surface = surface_new("/usr/share/nogg/pl_walk_right.png");
   anim_pl_walk_right.t = 0;
   anim_pl_walk_right.p = 9;
   anim_pl_walk_right.w = 48;
   anim_pl_walk_right.h = 48;

   anim_pl_jump_left.surface = surface_new("/usr/share/nogg/pl_jump_left.png");
   anim_pl_jump_left.t = 0;
   anim_pl_jump_left.p = 1;
   anim_pl_jump_left.w = 48;
   anim_pl_jump_left.h = 48;

   anim_pl_jump_right.surface = surface_new("/usr/share/nogg/pl_jump_right.png");
   anim_pl_jump_right.t = 0;
   anim_pl_jump_right.p = 1;
   anim_pl_jump_right.w = 48;
   anim_pl_jump_right.h = 48;

   anim_pl_fall_left.surface = surface_new("/usr/share/nogg/pl_fall_left.png");
   anim_pl_fall_left.t = 0;
   anim_pl_fall_left.p = 1;
   anim_pl_fall_left.w = 48;
   anim_pl_fall_left.h = 48;

   anim_pl_fall_right.surface = surface_new("/usr/share/nogg/pl_fall_right.png");
   anim_pl_fall_right.t = 0;
   anim_pl_fall_right.p = 1;
   anim_pl_fall_right.w = 48;
   anim_pl_fall_right.h = 48;

   anim_pl_duck_left.surface = surface_new("/usr/share/nogg/pl_duck_left.png");
   anim_pl_duck_left.t = 0;
   anim_pl_duck_left.p = 1;
   anim_pl_duck_left.w = 48;
   anim_pl_duck_left.h = 48;

   anim_pl_duck_right.surface = surface_new("/usr/share/nogg/pl_duck_right.png");
   anim_pl_duck_right.t = 0;
   anim_pl_duck_right.p = 1;
   anim_pl_duck_right.w = 48;
   anim_pl_duck_right.h = 48;

   anim_pl_slip_left.surface = surface_new("/usr/share/nogg/pl_slip_left.png");
   anim_pl_slip_left.t = 0;
   anim_pl_slip_left.p = 1;
   anim_pl_slip_left.w = 48;
   anim_pl_slip_left.h = 48;

   anim_pl_slip_right.surface = surface_new("/usr/share/nogg/pl_slip_right.png");
   anim_pl_slip_right.t = 0;
   anim_pl_slip_right.p = 1;
   anim_pl_slip_right.w = 48;
   anim_pl_slip_right.h = 48;

   entity_t *self = NULL;
   self = (entity_t*)realloc(self, sizeof(entity_t));

   self->type = 0;

   self->w = 16;
   self->h = 32;
   self->x = SCREEN_WIDTH/2  - self->w/2;
   self->y = SCREEN_HEIGHT/2 - self->h/2;
   self->vx = 0;
   self->vy = 0;
   self->d = true;
   self->anim = &anim_pl_stand_left;
   self->update = &update;
   self->draw = &draw;
   self->on_collide = &on_collide;

   self->ks = ks;
   self->c = c;

   num_entities++;
   entities = (entity_t**)realloc(entities, num_entities * sizeof(entity_t));
   entities[num_entities-1] = self;

   return self;
}
Esempio n. 5
0
int
geomap(
        const size_t ninput, const coord_t* const input,
        const size_t nref, const coord_t* const ref,
        const bbox_t* const bbox,
        const geomap_fit_e fit_geometry,
        const surface_type_e function,
        const size_t xxorder,
        const size_t xyorder,
        const size_t yxorder,
        const size_t yyorder,
        const xterms_e xxterms,
        const xterms_e yxterms,
        const size_t maxiter,
        const double reject,
        /* Input/Output */
        size_t* const noutput,
        /* Output */
        geomap_output_t* const output, /* [MAX(ninput, nref)] */
        geomap_result_t* const result,
        stimage_error_t* const error) {

    geomap_fit_t     fit;
    bbox_t           tbbox;
    size_t           ninput_in_bbox = ninput;
    size_t           nref_in_bbox   = nref;
    coord_t*         input_in_bbox  = NULL;
    coord_t*         ref_in_bbox    = NULL;
    double*          xfit           = NULL;
    double*          yfit           = NULL;
    double*          weights        = NULL;
    double*          tweights       = NULL;
    geomap_output_t* outi           = NULL;
    surface_t        sx1, sy1, sx2, sy2;
    int              has_sx2        = 0;
    int              has_sy2        = 0;
    size_t           i              = 0;
    double           my_nan         = fmod(1.0, 0.0);
    int              status         = 1;

    assert(input);
    assert(ref);
    assert(error);

    if (ninput != nref) {
        stimage_error_set_message(
            error, "Must have the same number of input and reference coordinates.");
        goto exit;
    }

    surface_new(&sx1);
    surface_new(&sy1);
    surface_new(&sx2);
    surface_new(&sy2);

    geomap_fit_init(
            &fit, geomap_proj_none, fit_geometry, function,
            xxorder, xyorder, xxterms, yxorder, yyorder, yxterms,
            maxiter, reject);

    /* If bbox is NULL, provide a dummy one full of NaNs */
    if (bbox == NULL) {
        bbox_init(&tbbox);
    } else {
        bbox_copy(bbox, &tbbox);
    }

    /* If the bbox is all NaNs, we don't need to reduce the data, saving an
       alloc and copy */
    if (bbox == NULL ||
        (!isfinite64(tbbox.min.x) && !isfinite64(tbbox.min.y) &&
         !isfinite64(tbbox.max.x) && !isfinite64(tbbox.max.y))) {
        /* If we have no bbox, we don't need to allocate and copy */
        input_in_bbox = (coord_t*)input;
        ref_in_bbox = (coord_t*)ref;
        ninput_in_bbox = ninput;
        nref_in_bbox = nref;
    } else {
        input_in_bbox = malloc_with_error(
                ninput * sizeof(coord_t), error);
        if (input_in_bbox == NULL) goto exit;

        ref_in_bbox = malloc_with_error(
                nref * sizeof(coord_t), error);
        if (ref_in_bbox == NULL) goto exit;

        /* Reduce data to only those in the bbox */
        ninput_in_bbox = nref_in_bbox = limit_to_bbox(
                ninput, input, ref, &tbbox, input_in_bbox, ref_in_bbox);
    }

    /* Compute the mean of the reference and input coordinates */
    compute_mean_coord(nref_in_bbox, ref_in_bbox, &fit.oref);
    compute_mean_coord(ninput_in_bbox, input_in_bbox, &fit.oin);

    /* Set the reference point for the projections to undefined */
    fit.refpt.x = my_nan;
    fit.refpt.y = my_nan;

    /* Allocate some memory */
    xfit = malloc_with_error(ninput_in_bbox * sizeof(double), error);
    if (xfit == NULL) goto exit;

    yfit = malloc_with_error(ninput_in_bbox * sizeof(double), error);
    if (yfit == NULL) goto exit;

    /* Compute the weights */
    weights = malloc_with_error(ninput_in_bbox * sizeof(double), error);
    if (weights == NULL) goto exit;

    for (i = 0; i < ninput_in_bbox; ++i) {
        weights[i] = 1.0;
    }

    /* Determine the actual max and min of the coordinates */
    determine_bbox(nref_in_bbox, ref_in_bbox, &tbbox);
    bbox_copy(&tbbox, &fit.bbox);

    if (geofit(
                &fit, &sx1, &sy1, &sx2, &sy2, &has_sx2, &has_sy2,
                ninput_in_bbox, input_in_bbox, ref_in_bbox, weights,
                error)) goto exit;

    /* Compute the fitted x and y values */
    if (geoeval(
                &sx1, &sy1, &sx2, &sy2, has_sx2, has_sy2, ninput_in_bbox,
                ref_in_bbox, xfit, yfit, error)) goto exit;

    if (geo_get_results(
                &fit, &sx1, &sy1, &sx2, &sy2, has_sx2, has_sy2, result,
                error)) goto exit;

    /* DIFF: This section is from geo_plistd */

    /* Copy the results to the output buffer */
    tweights = malloc_with_error(ninput_in_bbox * sizeof(double), error);
    if (tweights == NULL) goto exit;

    for (i = 0; i < ninput_in_bbox; ++i) {
        tweights[i] = weights[i];
    }

    for (i = 0; i < fit.nreject; ++i) {
        assert(fit.rej);
        assert(fit.rej[i] < ninput_in_bbox);
        if (weights[fit.rej[i]] > 0.0) {
            tweights[fit.rej[i]] = 0.0;
        }
    }

    outi = output;
    for (i = 0; i < ninput_in_bbox; ++i, ++outi) {
        outi->ref.x = ref_in_bbox[i].x;
        outi->ref.y = ref_in_bbox[i].y;
        outi->input.x = input_in_bbox[i].x;
        outi->input.y = input_in_bbox[i].y;
        if (tweights[i] > 0.0) {
            outi->fit.x = xfit[i];
            outi->fit.y = yfit[i];
            outi->residual.x = input_in_bbox[i].x - xfit[i];
            outi->residual.y = input_in_bbox[i].y - yfit[i];
        } else {
            outi->fit.x = my_nan;
            outi->fit.y = my_nan;
            outi->residual.x = my_nan;
            outi->residual.y = my_nan;
        }
    }
    *noutput = ninput_in_bbox;

    status = 0;

 exit:

    if (input_in_bbox != input) {
        free(input_in_bbox);
    }
    if (ref_in_bbox != ref) {
        free(ref_in_bbox);
    }
    free(weights);
    free(xfit);
    free(yfit);
    free(tweights);
    surface_free(&sx1);
    surface_free(&sy1);
    surface_free(&sx2);
    surface_free(&sy2);

    return status;
}