2010-09-22 23:04:43 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2010-09-23 01:20:29 +03:00
|
|
|
#include <math.h>
|
2010-09-22 23:04:43 +03:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include "array.h"
|
|
|
|
#include "face.h"
|
|
|
|
#include "level.h"
|
|
|
|
|
|
|
|
|
2010-09-24 01:55:11 +03:00
|
|
|
#define NEAR 1
|
|
|
|
|
|
|
|
|
2010-09-22 23:04:43 +03:00
|
|
|
static void draw_map(GtkWidget *widget, struct face *f)
|
|
|
|
{
|
|
|
|
int x, y, z;
|
2010-09-23 01:20:29 +03:00
|
|
|
double z0;
|
2010-09-22 23:04:43 +03:00
|
|
|
guchar *rgbbuf, *p;
|
|
|
|
|
|
|
|
rgbbuf = p = calloc(f->sx*f->sy, 3);
|
|
|
|
if (!rgbbuf) {
|
|
|
|
perror("calloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for (y = f->sy-1; y >= 0; y--)
|
|
|
|
for (x = 0; x != f->sx ; x++) {
|
|
|
|
z = get(f->a, x+f->a->min_x, y+f->a->min_y);
|
|
|
|
if (z == UNDEF) {
|
|
|
|
p += 3;
|
|
|
|
continue;
|
|
|
|
}
|
2010-09-23 01:20:29 +03:00
|
|
|
z0 = f->z_ref+f->fx*(x-f->sx/2)+f->fy*(y-f->sy/2);
|
2010-09-24 01:55:11 +03:00
|
|
|
if (fabs(z-z0) < NEAR) {
|
2010-09-23 01:20:29 +03:00
|
|
|
*p++ = 255*fabs(z-z0);
|
|
|
|
*p++ = 255*fabs(z-z0);
|
2010-09-22 23:04:43 +03:00
|
|
|
*p++ = 255;
|
|
|
|
continue;
|
|
|
|
}
|
2010-09-23 01:20:29 +03:00
|
|
|
if (z < z0) {
|
2010-09-24 01:55:11 +03:00
|
|
|
z = z > z0-2*NEAR ? 255*(z-z0)/NEAR :
|
|
|
|
255.0*(z-z0)/(z0-f->a->min_z);
|
2010-09-22 23:04:43 +03:00
|
|
|
*p++ = 255;
|
|
|
|
*p++ = z;
|
|
|
|
*p++ = z;
|
|
|
|
} else {
|
2010-09-24 01:55:11 +03:00
|
|
|
z = z < z0+2*NEAR ? 255*(z0-z)/NEAR :
|
|
|
|
255.0*(z0-z)/(f->a->max_z-z0);
|
2010-09-22 23:04:43 +03:00
|
|
|
*p++ = z;
|
|
|
|
*p++ = 255;
|
|
|
|
*p++ = z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gdk_draw_rgb_image(widget->window,
|
|
|
|
widget->style->fg_gc[GTK_STATE_NORMAL],
|
|
|
|
0, 0, f->sx, f->sy, GDK_RGB_DITHER_MAX, rgbbuf, f->sx*3);
|
|
|
|
free(rgbbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void draw_image(GtkWidget *da, struct face *f)
|
|
|
|
{
|
2010-09-23 01:20:29 +03:00
|
|
|
fprintf(stderr, "%d %g %g\n", f->z_ref, f->fx, f->fy);
|
2010-09-22 23:04:43 +03:00
|
|
|
draw_map(da, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-23 01:20:29 +03:00
|
|
|
static void scroll_z(GtkWidget *da, struct face *f, int up)
|
|
|
|
{
|
|
|
|
if (up) {
|
|
|
|
if (f->z_ref < f->a->max_z)
|
|
|
|
f->z_ref++;
|
|
|
|
} else {
|
|
|
|
if (f->z_ref > f->a->min_z)
|
|
|
|
f->z_ref--;
|
|
|
|
}
|
|
|
|
draw_image(da, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void scroll_xy(GtkWidget *da, struct face *f, int dx, int dy, int up)
|
|
|
|
{
|
|
|
|
double d;
|
|
|
|
|
|
|
|
d = (double) (up ? 1 : -1)/(dx*dx+dy*dy)/2.0;
|
|
|
|
f->fx += d*dx;
|
|
|
|
f->fy += d*dy;
|
|
|
|
draw_image(da, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-22 23:04:43 +03:00
|
|
|
static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *da = gtk_bin_get_child(GTK_BIN(widget));
|
|
|
|
struct face *f = data;
|
2010-09-23 01:20:29 +03:00
|
|
|
int dx = event->x-f->sx/2;
|
|
|
|
int dy = event->y-f->sy/2;
|
|
|
|
int center = hypot(dx, dy)/hypot(f->sx, f->sy) < 0.25;
|
2010-09-22 23:04:43 +03:00
|
|
|
|
|
|
|
switch (event->direction) {
|
|
|
|
case GDK_SCROLL_UP:
|
2010-09-23 01:20:29 +03:00
|
|
|
if (center)
|
|
|
|
scroll_z(da, f, 0);
|
|
|
|
else
|
|
|
|
scroll_xy(da, f, dx, dy, 1);
|
2010-09-22 23:04:43 +03:00
|
|
|
break;
|
|
|
|
case GDK_SCROLL_DOWN:
|
2010-09-23 01:20:29 +03:00
|
|
|
if (center)
|
|
|
|
scroll_z(da, f, 1);
|
|
|
|
else
|
|
|
|
scroll_xy(da, f, dx, dy, 0);
|
2010-09-22 23:04:43 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* ignore */;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
draw_image(widget, user_data);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
if (event->keyval == 'q')
|
|
|
|
gtk_main_quit();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void make_screen(GtkWidget *root, struct face *f)
|
|
|
|
{
|
|
|
|
GtkWidget *evbox, *da;
|
|
|
|
|
|
|
|
da = gtk_drawing_area_new();
|
|
|
|
gtk_widget_set_size_request(da, f->sx, f->sy);
|
|
|
|
evbox = gtk_event_box_new();
|
|
|
|
gtk_container_add(GTK_CONTAINER(evbox), da);
|
|
|
|
gtk_container_add(GTK_CONTAINER(root), evbox);
|
|
|
|
gtk_widget_show_all(root);
|
|
|
|
draw_map(da, f);
|
|
|
|
|
|
|
|
g_signal_connect(G_OBJECT(evbox), "scroll-event",
|
|
|
|
G_CALLBACK(scroll_event), f);
|
|
|
|
g_signal_connect(G_OBJECT(da), "expose-event",
|
|
|
|
G_CALLBACK(expose_event), f);
|
|
|
|
g_signal_connect(G_OBJECT(root), "key-press-event",
|
|
|
|
G_CALLBACK(key_press_event), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void level(struct face *f)
|
|
|
|
{
|
|
|
|
GtkWidget *root;
|
|
|
|
|
|
|
|
root = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* get root->window */
|
|
|
|
gtk_widget_show_all(root);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
g_signal_connect(G_OBJECT(root), "destroy",
|
|
|
|
G_CALLBACK(gtk_main_quit), NULL);
|
|
|
|
make_screen(root, f);
|
|
|
|
gtk_main();
|
|
|
|
}
|