1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gui.c Mon Oct 14 13:02:19 2013 +0000
1.3 @@ -0,0 +1,366 @@
1.4 +/*
1.5 + * Ben NanoNote graphical user interface utilities.
1.6 + *
1.7 + * Copyright (C) 2013 Paul Boddie
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or modify
1.10 + * it under the terms of the GNU General Public License as published by
1.11 + * the Free Software Foundation; either version 2 of the License, or
1.12 + * (at your option) any later version.
1.13 + */
1.14 +
1.15 +#include <stdio.h>
1.16 +#include "gui.h"
1.17 +
1.18 +SDL_Surface *screen;
1.19 +gui_printer printer = {0, 0, 255, 255, 255, 255};
1.20 +
1.21 +void gui_init()
1.22 +{
1.23 + if (SDL_Init(SDL_INIT_VIDEO) < 0)
1.24 + {
1.25 + fprintf(stderr, "SDL error: %s\n", SDL_GetError());
1.26 + exit(1);
1.27 + }
1.28 +}
1.29 +
1.30 +void gui_display_init()
1.31 +{
1.32 + screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
1.33 + SDL_ShowCursor(SDL_DISABLE);
1.34 +}
1.35 +
1.36 +void gui_shutdown(int signum)
1.37 +{
1.38 + SDL_Quit();
1.39 + text_shutdown(signum);
1.40 +}
1.41 +
1.42 +void gui_shutdown_threaded(int signum)
1.43 +{
1.44 + SDL_Quit();
1.45 + text_shutdown_threaded(signum);
1.46 +}
1.47 +
1.48 +void gui_quit()
1.49 +{
1.50 + SDL_Quit();
1.51 + text_quit();
1.52 +}
1.53 +
1.54 +void gui_clear()
1.55 +{
1.56 + gui_fill(0, 0, 0);
1.57 + printer.x = 0;
1.58 + printer.y = 0;
1.59 +}
1.60 +
1.61 +void gui_fill(uint8_t r, uint8_t g, uint8_t b)
1.62 +{
1.63 + SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
1.64 +}
1.65 +
1.66 +void gui_next_row(gui_printer *printer, uint16_t rows)
1.67 +{
1.68 + printer->x = 0;
1.69 + printer->y = (printer->y + rows * SCREEN_ROW_HEIGHT) % SCREEN_HEIGHT;
1.70 +}
1.71 +
1.72 +void gui_next_column(gui_printer *printer, uint16_t columns)
1.73 +{
1.74 + printer->x += columns * SCREEN_COLUMN_WIDTH;
1.75 + if (printer->x >= SCREEN_WIDTH)
1.76 + {
1.77 + printer->x = 0;
1.78 + printer->y = (printer->y + SCREEN_ROW_HEIGHT) % SCREEN_HEIGHT;
1.79 + }
1.80 +}
1.81 +
1.82 +int gui_printf(const char *format, ...)
1.83 +{
1.84 + va_list args;
1.85 + static char buffer[GUI_BUFSIZE];
1.86 + char *this = buffer, *next;
1.87 + int printed = 0;
1.88 +
1.89 + va_start(args, format);
1.90 + vsnprintf(buffer, GUI_BUFSIZE, format, args);
1.91 + va_end(args);
1.92 +
1.93 + do
1.94 + {
1.95 + next = strchr(this, (int) '\n');
1.96 + if (next != NULL)
1.97 + *next = '\0';
1.98 + stringRGBA(screen, printer.x, printer.y, this, printer.r, printer.g, printer.b, printer.a);
1.99 + if (next != NULL)
1.100 + gui_next_row(&printer, 1);
1.101 + else
1.102 + gui_next_column(&printer, strlen(this));
1.103 + printed += strlen(this);
1.104 + this = next;
1.105 + }
1.106 + while (next != NULL);
1.107 +
1.108 + return printed;
1.109 +}
1.110 +
1.111 +/**
1.112 + * Generate sky points.
1.113 + */
1.114 +void gui_sky(vectorf *viewx, vectorf *viewy, vectorf *viewz)
1.115 +{
1.116 + double direction, elevation;
1.117 + int d, e, estart, efinish, dcount;
1.118 + int16_t lastminx, lastminy, thisminx, thisminy,
1.119 + lastmaxx, lastmaxy, thismaxx, thismaxy;
1.120 + uint32_t colour;
1.121 + vectorf point;
1.122 +
1.123 + direction = vectorf_direction(viewz);
1.124 + elevation = vectorf_elevation(viewz);
1.125 +
1.126 + d = closest(raddeg(direction), SKY_GRID_STEP);
1.127 +
1.128 + /* Plot points between elevations -90 and 90 degrees. */
1.129 +
1.130 + estart = max(-90, closest(raddeg(elevation), SKY_GRID_STEP) - (SKY_GRID_STEP * 3));
1.131 + efinish = min(90, closest(raddeg(elevation), SKY_GRID_STEP) + (SKY_GRID_STEP * 3));
1.132 +
1.133 + for (e = estart; e <= efinish; e += SKY_GRID_STEP)
1.134 + {
1.135 + /* Start from the most central point and work outwards until
1.136 + the points are no longer visible. */
1.137 +
1.138 + for (dcount = 0; dcount <= 180; dcount += SKY_GRID_STEP)
1.139 + {
1.140 + /* Check point visibility. */
1.141 +
1.142 + vectorf_polar(degrad(d - dcount), degrad(e), &point);
1.143 + if (vectorf_dot(viewz, &point) <= 0)
1.144 + break;
1.145 +
1.146 + /* Plot points at the given extremes. */
1.147 +
1.148 + colour = e < 0 ? SKY_LOWER_COLOUR : SKY_UPPER_COLOUR;
1.149 +
1.150 + /* Current minimum extent of the direction. */
1.151 +
1.152 + thisminx = vectorf_dot(viewx, &point) * PROJECTION_FACTOR + SCREEN_WIDTH / 2;
1.153 + thisminy = vectorf_dot(viewy, &point) * -PROJECTION_FACTOR + SCREEN_HEIGHT / 2;
1.154 +
1.155 + /* Draw from the last point at this elevation. */
1.156 +
1.157 + if (dcount != 0)
1.158 + {
1.159 + lineColor(screen,
1.160 + lastminx, lastminy,
1.161 + thisminx, thisminy,
1.162 + colour
1.163 + );
1.164 + }
1.165 +
1.166 + /* Draw to the next elevation. */
1.167 +
1.168 + gui_sky_vertical(viewx, viewy, viewz, d - dcount, e, thisminx, thisminy, colour);
1.169 +
1.170 + /* Current maximum extent of the direction. */
1.171 +
1.172 + vectorf_polar(degrad(d + dcount), degrad(e), &point);
1.173 + thismaxx = vectorf_dot(viewx, &point) * PROJECTION_FACTOR + SCREEN_WIDTH / 2;
1.174 + thismaxy = vectorf_dot(viewy, &point) * -PROJECTION_FACTOR + SCREEN_HEIGHT / 2;
1.175 +
1.176 + /* Draw from the last point at this elevation. */
1.177 +
1.178 + if (dcount != 0)
1.179 + {
1.180 + lineColor(screen,
1.181 + lastmaxx, lastmaxy,
1.182 + thismaxx, thismaxy,
1.183 + colour
1.184 + );
1.185 + }
1.186 +
1.187 + /* Draw to the next elevation. */
1.188 +
1.189 + gui_sky_vertical(viewx, viewy, viewz, d + dcount, e, thismaxx, thismaxy, colour);
1.190 +
1.191 + /* Stop after one point at -90 or 90 degree elevations. */
1.192 +
1.193 + if (abs(e) == 90)
1.194 + {
1.195 + pixelColor(screen,
1.196 + thisminx, thisminy,
1.197 + colour
1.198 + );
1.199 + break;
1.200 + }
1.201 +
1.202 + lastminx = thisminx; lastminy = thisminy;
1.203 + lastmaxx = thismaxx; lastmaxy = thismaxy;
1.204 + }
1.205 + }
1.206 +}
1.207 +
1.208 +void gui_sky_vertical(vectorf *viewx, vectorf *viewy, vectorf *viewz, int direction, int elevation, int16_t x, int16_t y, uint32_t colour)
1.209 +{
1.210 + vectorf point;
1.211 +
1.212 + if ((elevation >= -(90 - SKY_GRID_STEP)) || (elevation <= (90 - SKY_GRID_STEP)))
1.213 + {
1.214 + vectorf_polar(degrad(direction), degrad(elevation + SKY_GRID_STEP), &point);
1.215 + lineColor(screen,
1.216 + x, y,
1.217 + vectorf_dot(viewx, &point) * PROJECTION_FACTOR + SCREEN_WIDTH / 2,
1.218 + vectorf_dot(viewy, &point) * -PROJECTION_FACTOR + SCREEN_HEIGHT / 2,
1.219 + colour
1.220 + );
1.221 +
1.222 + if (elevation == -(90 - SKY_GRID_STEP))
1.223 + {
1.224 + vectorf_polar(degrad(direction), degrad(elevation - SKY_GRID_STEP), &point);
1.225 + lineColor(screen,
1.226 + x, y,
1.227 + vectorf_dot(viewx, &point) * PROJECTION_FACTOR + SCREEN_WIDTH / 2,
1.228 + vectorf_dot(viewy, &point) * -PROJECTION_FACTOR + SCREEN_HEIGHT / 2,
1.229 + colour
1.230 + );
1.231 + }
1.232 + }
1.233 +}
1.234 +
1.235 +/**
1.236 + * Generate motion vector.
1.237 + */
1.238 +void gui_motion(vectorf *viewx, vectorf *viewy, vectorf *viewz, vectorf *accelerationD)
1.239 +{
1.240 + vectorf point;
1.241 + uint32_t colour;
1.242 +
1.243 + if (vectorf_dot(viewz, accelerationD) <= 0)
1.244 + {
1.245 + colour = MOTION_REVERSE_COLOUR;
1.246 + vectorf_negate(accelerationD, &point);
1.247 + }
1.248 + else
1.249 + {
1.250 + colour = MOTION_FORWARD_COLOUR;
1.251 + point = *accelerationD;
1.252 + }
1.253 +
1.254 + lineColor(screen,
1.255 + SCREEN_WIDTH / 2,
1.256 + SCREEN_HEIGHT / 2,
1.257 + vectorf_dot(viewx, &point) * PROJECTION_FACTOR + SCREEN_WIDTH / 2,
1.258 + vectorf_dot(viewy, &point) * -PROJECTION_FACTOR + SCREEN_HEIGHT / 2,
1.259 + colour
1.260 + );
1.261 +}
1.262 +
1.263 +/**
1.264 + * Show the given point in space relative to the origin.
1.265 + */
1.266 +void gui_point(vectorf *viewx, vectorf *viewy, vectorf *viewz, vectorf *point, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
1.267 +{
1.268 + int16_t x, y, radius;
1.269 + double z;
1.270 +
1.271 + if (fabs(vectorf_dot(viewz, point)) < 1)
1.272 + return;
1.273 +
1.274 + z = vectorf_dot(viewz, point);
1.275 + x = vectorf_dot(viewx, point) * PROJECTION_FACTOR / z + SCREEN_WIDTH / 2;
1.276 + y = vectorf_dot(viewy, point) * -PROJECTION_FACTOR / z + SCREEN_HEIGHT / 2;
1.277 + radius = abs(PROJECTION_FACTOR / z);
1.278 +
1.279 + if (vectorf_dot(viewz, point) < 0)
1.280 + circleRGBA(screen,
1.281 + x, y, radius,
1.282 + r, g, b, a
1.283 + );
1.284 + else
1.285 + filledCircleRGBA(screen,
1.286 + x, y, radius,
1.287 + r, g, b, a
1.288 + );
1.289 +}
1.290 +
1.291 +/**
1.292 + * Generate a view from the given point in space of the origin.
1.293 + */
1.294 +void gui_origin(vectorf *viewx, vectorf *viewy, vectorf *viewz, vectorf *point)
1.295 +{
1.296 + vectorf origin;
1.297 +
1.298 + vectorf_negate(point, &origin);
1.299 + gui_point(viewx, viewy, viewz, &origin, 255, 255, 255, 127);
1.300 +}
1.301 +
1.302 +/**
1.303 + * Draw a simple bar representation of the given vector.
1.304 + */
1.305 +void gui_bar(vectorf *value)
1.306 +{
1.307 + lineRGBA(screen,
1.308 + SCREEN_WIDTH / 2,
1.309 + SCREEN_HEIGHT / 2,
1.310 + SCREEN_WIDTH / 2 + value->x * SCREEN_WIDTH / 2,
1.311 + SCREEN_HEIGHT / 2 - value->y * SCREEN_HEIGHT / 2,
1.312 + 255, 255, 255, 255);
1.313 +
1.314 + circleRGBA(screen,
1.315 + SCREEN_WIDTH / 2 + value->x * SCREEN_WIDTH / 2,
1.316 + SCREEN_HEIGHT / 2 - value->y * SCREEN_HEIGHT / 2,
1.317 + fabs(value->z) * SCREEN_WIDTH / 2,
1.318 + 255, 255, 255, 255);
1.319 +}
1.320 +
1.321 +void gui_plot(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
1.322 +{
1.323 + pixelRGBA(screen, x, y, r, g, b, a);
1.324 +}
1.325 +
1.326 +void gui_flush()
1.327 +{
1.328 + SDL_Flip(screen);
1.329 +}
1.330 +
1.331 +imu_ui_op gui_handle_events()
1.332 +{
1.333 + SDL_Event event;
1.334 +
1.335 + while (SDL_PollEvent(&event))
1.336 + {
1.337 + switch (event.type)
1.338 + {
1.339 + case SDL_KEYDOWN:
1.340 + switch (event.key.keysym.sym)
1.341 + {
1.342 + case SDLK_ESCAPE:
1.343 + case SDLK_q:
1.344 + return IMU_UI_OP_QUIT;
1.345 +
1.346 + case SDLK_c:
1.347 + return IMU_UI_OP_CALIBRATE;
1.348 +
1.349 + case SDLK_r:
1.350 + return IMU_UI_OP_RESET;
1.351 +
1.352 + case SDLK_p:
1.353 + return IMU_UI_OP_PAUSE;
1.354 +
1.355 + default:
1.356 + break;
1.357 + }
1.358 + break;
1.359 +
1.360 + case SDL_QUIT:
1.361 + return IMU_UI_OP_QUIT;
1.362 +
1.363 + default:
1.364 + break;
1.365 + }
1.366 + }
1.367 +
1.368 + return IMU_UI_OP_NULL;
1.369 +}