Root/tools/atrf-rssi/zgrid.c

1/*
2 * atrf-rssi/zgrid.c - Display a surface in faux 3D with SDL/SDL_gfx
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15#include <stdlib.h>
16#include <stdio.h>
17
18#include "SDL.h"
19#include "SDL_gfxPrimitives.h"
20
21#include "zgrid.h"
22
23
24#define SWAP(x, y) \
25    do { typeof(x) swap_tmp = (x); (x) = (y); (y) = swap_tmp; } while (0)
26
27
28static int *alloc_row(int nx)
29{
30    int *p;
31
32    p = malloc(nx*sizeof(*p));
33    if (p)
34        return p;
35    perror("malloc");
36    exit(1);
37}
38
39
40static void draw_band(SDL_Surface *s, const int *ax, const int *ay,
41    const int *bx, const int *by, int n, uint32_t fg, uint32_t bg)
42{
43    Sint16 px[4], py[4];
44    int i;
45
46    for (i = n-2; i >= 0; i--) {
47        px[0] = ax[i];
48        px[1] = ax[i+1];
49        px[2] = bx[i+1];
50        px[3] = bx[i];
51        py[0] = ay[i];
52        py[1] = ay[i+1];
53        py[2] = by[i+1];
54        py[3] = by[i];
55        filledPolygonColor(s, px, py, 4, bg);
56        aalineColor(s, ax[i+1], ay[i+1], bx[i+1], by[i+1], fg);
57        aalineColor(s, bx[i], by[i], bx[i+1], by[i+1], fg);
58    }
59    aalineColor(s, ax[0], ay[0], bx[0], by[0], fg);
60}
61
62
63static void draw_polyline(SDL_Surface *s, const int *px, const int *py,
64    int n, uint32_t rgba)
65{
66    int i;
67
68    for (i = 0; i != n-1; i++)
69        aalineColor(s, px[i], py[i], px[i+1], py[i+1], rgba);
70}
71
72
73void zgrid_draw(SDL_Surface *s, const int *z, int nx, int ny,
74    int sx, int sy, int sxy, int x0, int y0, uint32_t fg, uint32_t bg)
75{
76    int *lx = alloc_row(nx);
77    int *ly = alloc_row(nx);
78    int *px = alloc_row(nx);
79    int *py = alloc_row(nx);
80    int x, y, yz0;
81    const int *zp;
82    uint8_t a;
83
84    for (y = ny-1; y >= 0; y--) {
85        a = (ny-1-y)*0xe0/(ny-1)+0x1f;
86        yz0 = s->h-y0-y*sy-1;
87        zp = z+y*nx;
88        for (x = 0; x != nx; x++) {
89            px[x] = x0+x*sx+y*sxy;
90            py[x] = yz0-zp[x];
91        }
92        if (y != ny-1) {
93            draw_band(s, px, py, lx, ly, nx, fg | a, bg);
94        }
95        SWAP(px, lx);
96        SWAP(py, ly);
97    }
98    draw_polyline(s, lx, ly, nx, fg | 0xff);
99}
100

Archive Download this file



interactive