Date:2010-09-24 04:41:35 (8 years 10 months ago)
Author:Werner Almesberger
Commit:1fa23c574e532cd0a398661828a03912e6a2039c
Message:Introduce a solid data type and use it, mainly in overlap()

- solidify/solid.h: definition of a solid made from two opposing faces
- solidify/overlap.h (overlap), solidify/overlap.c (draw_image,
scroll_event, overlap): operate on solid instead of face
- solidify/overlap.c (sx, sy, draw_image, overlap): helper functions sx()
and sy() to determine canvas size
- solidify/overlap.c (BORDER, sx, sy): added a border around the piece,
to help with positioning
- solidify/solidify.c (clicked, gui_buttons, gui, main): use solid instead
of faces
Files: solidify/overlap.c (6 diffs)
solidify/overlap.h (1 diff)
solidify/solid.h (1 diff)
solidify/solidify.c (5 diffs)

Change Details

solidify/overlap.c
1818#include <gtk/gtk.h>
1919
2020#include "face.h"
21#include "solid.h"
2122#include "overlap.h"
2223
2324
2425#define UNDEF_F HUGE_VAL
26#define BORDER 10 /* pixels around the minimum drawing area */
27
28
29static int sx(const struct solid *s)
30{
31    return (s->a->sx > s->b->sx ? s->a->sx : s->b->sx)+2*BORDER;
32}
33
34
35static int sy(const struct solid *s)
36{
37    return (s->a->sy > s->b->sy ? s->a->sy : s->b->sy)+2*BORDER;
38}
2539
2640
2741static double ramp(int z0, double w0, int z1, double w1)
...... 
5973}
6074
6175
62static void draw_image(GtkWidget *widget, struct face *f)
76static void draw_image(GtkWidget *widget, struct solid *s)
6377{
6478    guchar *rgbbuf, *p;
6579    int x, y;
6680    double z;
81    struct face *f = s->a;
6782
68    rgbbuf = p = calloc(f->sx*f->sy, 3);
83    rgbbuf = p = calloc(sx(s)*sy(s), 3);
6984    if (!rgbbuf) {
7085        perror("calloc");
7186        exit(1);
7287    }
73    for (y = f->sy-1; y >= 0; y--)
74        for (x = 0; x != f->sx ; x++) {
88    for (y = sy(s)-1; y >= 0; y--)
89        for (x = 0; x != sx(s) ; x++) {
7590            int xa = x+f->a->min_x;
7691            int ya = y+f->a->min_y;
7792
...... 
93108        }
94109    gdk_draw_rgb_image(widget->window,
95110        widget->style->fg_gc[GTK_STATE_NORMAL],
96        0, 0, f->sx, f->sy, GDK_RGB_DITHER_MAX, rgbbuf, f->sx*3);
111        0, 0, sx(s), sy(s), GDK_RGB_DITHER_MAX, rgbbuf, sx(s)*3);
97112    free(rgbbuf);
98113}
99114
...... 
124139    gpointer data)
125140{
126141    GtkWidget *da = gtk_bin_get_child(GTK_BIN(widget));
127    struct face *f = data;
142    struct solid *s = data;
143    struct face *f = s->a;
128144    int dx = event->x-f->sx/2;
129145    int dy = event->y-f->sy/2;
130146    double r = hypot(dx, dy);
...... 
134150    switch (event->direction) {
135151    case GDK_SCROLL_UP:
136152        rotate(&f->m, r);
137        draw_image(da, f);
153        draw_image(da, s);
138154        break;
139155    case GDK_SCROLL_DOWN:
140156        rotate(&f->m, -r);
141        draw_image(da, f);
157        draw_image(da, s);
142158        break;
143159    default:
144160        /* ignore */;
...... 
156172
157173
158174
159void overlap(GtkWidget *canvas, struct face *f)
175void overlap(GtkWidget *canvas, struct solid *s)
160176{
161177    GtkWidget *evbox, *da;
162178
163179    evbox = gtk_event_box_new();
164180    da = gtk_drawing_area_new();
165    gtk_widget_set_size_request(da, f->sx, f->sy);
181    gtk_widget_set_size_request(da, sx(s), sy(s));
166182    gtk_container_add(GTK_CONTAINER(canvas), evbox);
167183    gtk_container_add(GTK_CONTAINER(evbox), da);
168184
169    draw_image(da, f);
185    draw_image(da, s);
170186
171187    g_signal_connect(G_OBJECT(evbox), "scroll-event",
172        G_CALLBACK(scroll_event), f);
188        G_CALLBACK(scroll_event), s);
173189    g_signal_connect(G_OBJECT(da), "expose-event",
174        G_CALLBACK(expose_event), f);
175
176    return evbox;
190        G_CALLBACK(expose_event), s);
177191}
solidify/overlap.h
1515
1616#include <gtk/gtk.h>
1717
18#include "solid.h"
1819
19void overlap(GtkWidget *canvas, struct face *face);
20
21void overlap(GtkWidget *canvas, struct solid *solid);
2022
2123#endif /* !OVERLAP_H */
solidify/solid.h
1/*
2 * solid.h - Data structure and handling of a solid made of two opposing faces
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 by 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#ifndef SOLID_H
14#define SOLID_H
15
16struct solid {
17    struct face *a, *b;
18    double dist;
19};
20
21#endif /* SOLID_H */
solidify/solidify.c
1717#include <gtk/gtk.h>
1818
1919#include "face.h"
20#include "solid.h"
2021#include "level.h"
2122#include "overlap.h"
2223
2324
24static struct face *face_a, *face_b;
25static struct solid solid;
2526static const struct face *active;
2627static GtkWidget *canvas;
2728
...... 
3839    if (face)
3940        level(canvas, face);
4041    else
41        overlap(canvas, face_a);
42        overlap(canvas, &solid);
4243    active = face;
4344
4445    gtk_widget_show_all(canvas);
...... 
5455    but = gtk_button_new_with_label("A");
5556    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
5657    g_signal_connect(G_OBJECT(but), "clicked",
57        G_CALLBACK(clicked), face_a);
58        G_CALLBACK(clicked), solid.a);
5859
5960    but = gtk_button_new_with_label("B");
6061    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
6162    g_signal_connect(G_OBJECT(but), "clicked",
62        G_CALLBACK(clicked), face_b);
63        G_CALLBACK(clicked), solid.b);
6364
6465    but = gtk_button_new_with_label("A+B");
6566    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
...... 
9899    buttons = gui_buttons();
99100    gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
100101
101    level(canvas, face_a);
102    active = face_a;
102    level(canvas, solid.a);
103    active = solid.a;
103104
104105    gtk_widget_show_all(root);
105106
...... 
129130        usage(*argv);
130131    }
131132    setlocale(LC_ALL, "C"); /* damage control */
132    face_a = read_face(argv[1]);
133    face_b = face_a;
133    solid.a = read_face(argv[1]);
134    solid.b = solid.a;
134135    gui();
135136
136137    return 0;

Archive Download the corresponding diff file

Branches:
master



interactive