2013-01-30 19:38:54 +02:00
|
|
|
/*
|
|
|
|
* gui.c - Graphical output for ubb-la
|
|
|
|
*
|
|
|
|
* Written 2013 by Werner Almesberger
|
|
|
|
* Copyright 2013 Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_gfxPrimitives.h"
|
|
|
|
|
|
|
|
#include "gui.h"
|
|
|
|
|
|
|
|
|
2013-01-31 02:25:47 +02:00
|
|
|
#if 0
|
|
|
|
#define DEBUG(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DEBUG(fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-01-30 19:38:54 +02:00
|
|
|
#define XRES 320 /* canvas width */
|
|
|
|
#define YRES 240 /* canvas height */
|
|
|
|
|
|
|
|
#define LEVEL_RGBA 0xffff00ff
|
|
|
|
#define BOUNCE_RGBA 0xff8080ff
|
|
|
|
|
|
|
|
#define CH_XOFF 30
|
|
|
|
#define CH_YOFF 30
|
|
|
|
#define CH_SKIP 16
|
|
|
|
#define CH_HEIGHT 8
|
|
|
|
|
2013-01-31 01:35:30 +02:00
|
|
|
#define MAX_ZOOM 3
|
2013-01-30 19:38:54 +02:00
|
|
|
|
2013-01-31 01:55:58 +02:00
|
|
|
#define REPEAT_DELAY_MS 300
|
|
|
|
#define REPEAT_INTERVAL_MS 30
|
2013-01-30 19:38:54 +02:00
|
|
|
|
|
|
|
static SDL_Surface *surf;
|
|
|
|
|
|
|
|
|
|
|
|
void gui_init(void)
|
|
|
|
{
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
|
|
fprintf(stderr, "SDL_init: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
|
|
|
|
surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE);
|
|
|
|
if (!surf) {
|
|
|
|
fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-01-31 01:55:58 +02:00
|
|
|
|
|
|
|
SDL_EnableKeyRepeat(REPEAT_DELAY_MS, REPEAT_INTERVAL_MS);
|
2013-01-30 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void clear(void)
|
|
|
|
{
|
|
|
|
SDL_LockSurface(surf);
|
|
|
|
SDL_FillRect(surf, NULL, SDL_MapRGB(surf->format, 0, 0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int ch_y(int ch, int v)
|
|
|
|
{
|
|
|
|
return CH_YOFF+CH_SKIP*ch+(v ? 0 : CH_HEIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void bounce(int x, int ch)
|
|
|
|
{
|
|
|
|
vlineColor(surf, x, ch_y(ch, 0), ch_y(ch, 1), BOUNCE_RGBA);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void change(int x, int ch)
|
|
|
|
{
|
|
|
|
vlineColor(surf, x, ch_y(ch, 0), ch_y(ch, 1), LEVEL_RGBA);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-31 01:35:30 +02:00
|
|
|
static void level(int x0, int x1, int ch, int v)
|
2013-01-30 19:38:54 +02:00
|
|
|
{
|
2013-01-31 01:35:30 +02:00
|
|
|
if (x0 == x1)
|
|
|
|
pixelColor(surf, x0, ch_y(ch, v), LEVEL_RGBA);
|
|
|
|
else
|
|
|
|
hlineColor(surf, x0, x1, ch_y(ch, v), LEVEL_RGBA);
|
2013-01-30 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-31 01:35:30 +02:00
|
|
|
static void show_buffer_zoom_in(const uint8_t *buf, int skip, int nibbles,
|
|
|
|
int x0, int x1)
|
|
|
|
{
|
|
|
|
int f = (x1-x0)/(nibbles-skip);
|
|
|
|
int x, i, j;
|
|
|
|
int last[4] = { -1, -1, -1, -1};
|
|
|
|
uint8_t v, bit;
|
|
|
|
|
2013-01-31 02:25:47 +02:00
|
|
|
DEBUG("in: %d-%d (%d) Sa %d-%d (%d) pix %d f (%d)\n",
|
|
|
|
skip, nibbles, nibbles-skip, x0, x1, x1-x0, f, f*(nibbles-skip));
|
2013-01-31 01:35:30 +02:00
|
|
|
x = x0;
|
|
|
|
for (i = skip; i != nibbles; i++) {
|
|
|
|
v = (buf[i >> 1] >> 4*(~i & 1)) & 0xf;
|
|
|
|
for (j = 0; j != 4; j++) {
|
|
|
|
bit = (v >> j) & 1;
|
|
|
|
if (bit == last[j] || last[j] == -1) {
|
|
|
|
level(x, x+f-1, j, bit);
|
|
|
|
} else {
|
|
|
|
change(x, j);
|
|
|
|
level(x+1, x+f-1, j, bit);
|
|
|
|
}
|
|
|
|
last[j] = bit;
|
|
|
|
}
|
|
|
|
x += f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void show_buffer_zoom_out(const uint8_t *buf, int skip, int nibbles,
|
2013-01-30 19:38:54 +02:00
|
|
|
int x0, int x1)
|
|
|
|
{
|
2013-01-31 01:35:30 +02:00
|
|
|
int f = (nibbles-skip)/(x1-x0);
|
2013-01-30 19:38:54 +02:00
|
|
|
int x;
|
2013-01-31 01:35:30 +02:00
|
|
|
int i = skip, n, j;
|
2013-01-30 19:38:54 +02:00
|
|
|
int changes[4], last[4] = { -1, -1, -1, -1}, next[4];
|
|
|
|
uint8_t v, bit;
|
|
|
|
|
2013-01-31 02:25:47 +02:00
|
|
|
DEBUG("out: %d-%d (%d) Sa %d-%d (%d) pix %d f (%d)\n",
|
|
|
|
skip, nibbles, nibbles-skip, x0, x1, x1-x0, f, f*(x1-x0));
|
2013-01-31 01:35:30 +02:00
|
|
|
for (x = x0; x != x1; x++) {
|
|
|
|
n = i+f;
|
2013-01-30 19:38:54 +02:00
|
|
|
for (j = 0; j != 4; j++) {
|
|
|
|
next[j] = last[j];
|
|
|
|
changes[j] = 0;
|
|
|
|
}
|
|
|
|
while (i != n) {
|
|
|
|
v = (buf[i >> 1] >> 4*(~i & 1)) & 0xf;
|
|
|
|
for (j = 0; j != 4; j++) {
|
|
|
|
bit = (v >> j) & 1;
|
|
|
|
if (bit != next[j]) {
|
|
|
|
changes[j]++;
|
|
|
|
next[j] = bit;
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 01:35:30 +02:00
|
|
|
i++;
|
2013-01-30 19:38:54 +02:00
|
|
|
}
|
|
|
|
for (j = 0; j != 4; j++) {
|
|
|
|
if (changes[j] > 1)
|
|
|
|
bounce(x, j);
|
|
|
|
else if (!changes[j] || last[j] == -1)
|
2013-01-31 01:35:30 +02:00
|
|
|
level(x, x, j, next[j]);
|
2013-01-30 19:38:54 +02:00
|
|
|
else
|
|
|
|
change(x, j);
|
|
|
|
last[j] = next[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-31 01:35:30 +02:00
|
|
|
static void show_buffer(const uint8_t *buf, int skip, int nibbles,
|
|
|
|
int x0, int x1, int zoom, int pos)
|
|
|
|
{
|
|
|
|
int xm, w, p0, p1;
|
2013-01-31 01:49:39 +02:00
|
|
|
int d, dp;
|
2013-01-31 01:35:30 +02:00
|
|
|
|
|
|
|
xm = (x0+x1) >> 1;
|
2013-01-31 01:49:39 +02:00
|
|
|
dp = pos-((nibbles+skip) >> 1);
|
2013-01-31 01:35:30 +02:00
|
|
|
if (zoom < 0) {
|
|
|
|
w = (nibbles-skip) >> -zoom;
|
2013-01-31 01:49:39 +02:00
|
|
|
p0 = xm-(w >> 1)-(dp >> -zoom);
|
|
|
|
p1 = xm+((w+1) >> 1)-(dp >> -zoom);
|
2013-01-31 01:35:30 +02:00
|
|
|
if (p0 < x0) {
|
|
|
|
skip += (x0-p0) << -zoom;
|
|
|
|
p0 = x0;
|
|
|
|
}
|
|
|
|
if (p1 > x1) {
|
|
|
|
nibbles -= (p1-x1) << -zoom;
|
|
|
|
p1 = x1;
|
|
|
|
}
|
|
|
|
show_buffer_zoom_out(buf, skip, nibbles, p0, p1);
|
|
|
|
} else {
|
|
|
|
w = (nibbles-skip) << zoom;
|
2013-01-31 01:49:39 +02:00
|
|
|
p0 = xm-(w >> 1)-(dp << zoom);
|
|
|
|
p1 = xm+((w+1) >> 1)-(dp << zoom);
|
2013-01-31 01:35:30 +02:00
|
|
|
if (p0 < x0) {
|
|
|
|
d = ((x0-p0)+(1 << zoom)-1) >> zoom;
|
|
|
|
skip += d;
|
|
|
|
p0 += d << zoom;
|
|
|
|
}
|
|
|
|
if (p1 > x1) {
|
|
|
|
d = ((p1-x1)+(1 << zoom)-1) >> zoom;
|
|
|
|
nibbles -= d;
|
|
|
|
p1 -= d << zoom;
|
|
|
|
}
|
|
|
|
show_buffer_zoom_in(buf, skip, nibbles, p0, p1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-30 19:38:54 +02:00
|
|
|
static void update(void)
|
|
|
|
{
|
|
|
|
SDL_UnlockSurface(surf);
|
|
|
|
SDL_UpdateRect(surf, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-31 02:08:25 +02:00
|
|
|
static int pos_step(int zoom)
|
|
|
|
{
|
|
|
|
return 1 << (MAX_ZOOM-zoom+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-30 19:38:54 +02:00
|
|
|
void gui(const uint8_t *buf, int skip, int nibbles)
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
2013-01-31 01:35:30 +02:00
|
|
|
int pos = (skip+nibbles) >> 1;
|
|
|
|
int zoom; /* < 0: zoom out; 0: 1 pixel = 1 sample; > 1: zoom in */
|
|
|
|
int min_zoom = 0;
|
2013-01-30 19:38:54 +02:00
|
|
|
|
2013-01-31 01:35:30 +02:00
|
|
|
while (XRES-CH_XOFF < (nibbles-skip) >> -min_zoom)
|
|
|
|
min_zoom--;
|
|
|
|
zoom = min_zoom;
|
2013-01-30 19:38:54 +02:00
|
|
|
while (1) {
|
|
|
|
clear();
|
2013-01-31 01:35:30 +02:00
|
|
|
show_buffer(buf, skip, nibbles, CH_XOFF, XRES, zoom, pos);
|
2013-01-30 19:38:54 +02:00
|
|
|
update();
|
|
|
|
|
2013-01-31 01:37:42 +02:00
|
|
|
while (1) {
|
2013-01-30 19:38:54 +02:00
|
|
|
SDL_WaitEvent(&event);
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_UP:
|
|
|
|
if (zoom < MAX_ZOOM)
|
|
|
|
zoom++;
|
|
|
|
break;
|
|
|
|
case SDLK_DOWN:
|
2013-01-31 01:35:30 +02:00
|
|
|
if (zoom > min_zoom)
|
2013-01-30 19:38:54 +02:00
|
|
|
zoom--;
|
|
|
|
break;
|
2013-01-31 01:49:39 +02:00
|
|
|
case SDLK_LEFT:
|
2013-01-31 02:08:25 +02:00
|
|
|
pos -= pos_step(zoom);
|
|
|
|
if (pos < 0)
|
|
|
|
pos = 0;
|
2013-01-31 01:49:39 +02:00
|
|
|
break;
|
|
|
|
case SDLK_RIGHT:
|
2013-01-31 02:08:25 +02:00
|
|
|
pos += pos_step(zoom);
|
|
|
|
if (pos > nibbles-1)
|
|
|
|
pos = nibbles-1;
|
2013-01-31 01:49:39 +02:00
|
|
|
break;
|
2013-01-30 19:38:54 +02:00
|
|
|
case SDLK_RETURN:
|
|
|
|
case SDLK_q:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
printf("%x\n", event.key.keysym.sym);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_QUIT:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-31 01:37:42 +02:00
|
|
|
break;
|
2013-01-30 19:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|