summaryrefslogtreecommitdiffstats
path: root/apps/plugins/fractals/fractal.c
blob: 8c5d93e0b752f113751f7d748081bf44548f1cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2004 Matthias Wientapper
 * Heavily extended 2005 Jens Arnold
 * Extended 2009 Tomer Shalev
 *
 *
 * 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.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"

#include "fractal.h"
#include "fractal_rect.h"
#include "fractal_sets.h"
#include "mandelbrot_set.h"
#include "lib/pluginlib_exit.h"

#ifdef USEGSLIB
GREY_INFO_STRUCT
static unsigned char *gbuf;
static size_t gbuf_size = 0;
#endif

#define REDRAW_NONE         0
#define REDRAW_PARTIAL      1
#define REDRAW_FULL         2
#define REDRAW_FULL_OVERLAY 3



/* returns 1 if a button has been pressed, 0 otherwise */
static int button_yield(void *ctx)
{
    long *button = (long *)ctx;

    *button = rb->button_get(false);
    switch (*button)
    {
        case FRACTAL_QUIT:
        case FRACTAL_UP:
        case FRACTAL_DOWN:
        case FRACTAL_LEFT:
        case FRACTAL_RIGHT:
        case FRACTAL_ZOOM_IN:
        case FRACTAL_ZOOM_OUT:
        case FRACTAL_PRECISION_INC:
        case FRACTAL_PRECISION_DEC:
        case FRACTAL_RESET:
#ifdef FRACTAL_ZOOM_IN2
        case FRACTAL_ZOOM_IN2:
#endif
#ifdef FRACTAL_ZOOM_IN_PRE
        case FRACTAL_ZOOM_IN_PRE:
#endif
#if defined(FRACTAL_ZOOM_OUT_PRE) && \
            (FRACTAL_ZOOM_OUT_PRE != FRACTAL_ZOOM_IN_PRE)
        case FRACTAL_ZOOM_OUT_PRE:
#endif
#ifdef FRACTAL_PRECISION_INC_PRE
        case FRACTAL_PRECISION_INC_PRE:
#endif
#if defined(FRACTAL_PRECISION_DEC_PRE) && \
            (FRACTAL_PRECISION_DEC_PRE != FRACTAL_PRECISION_INC_PRE)
        case FRACTAL_PRECISION_DEC_PRE:
#endif
            return 1;
        default:
            *button = BUTTON_NONE;
            return 0;
    }
}

static void cleanup(void)
{
#ifdef USEGSLIB
    grey_release();
#endif
}

enum plugin_status plugin_start(const void* parameter)
{
#if defined(FRACTAL_ZOOM_OUT_PRE) || \
    defined(FRACTAL_ZOOM_IN_PRE) || \
    defined(FRACTAL_PRECISION_DEC_PRE) || \
    defined(FRACTAL_PRECISION_INC_PRE)
    long lastbutton = BUTTON_NONE;
#endif
    int redraw = REDRAW_FULL;
    struct fractal_ops *ops = &mandelbrot_ops;

    (void)parameter;

#ifdef USEGSLIB
    /* get the remainder of the plugin buffer */
    gbuf = (unsigned char *)rb->plugin_get_buffer(&gbuf_size);

    /* initialize the greyscale buffer.*/
    if (!grey_init(gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL))
    {
        rb->splash(HZ, "Couldn't init greyscale display");
        return PLUGIN_ERROR;
    }
    grey_show(true); /* switch on greyscale overlay */
#endif

    /* release greylib on exit */
    atexit(cleanup);
#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif

    ops->init();

    /* main loop */
    while (true)
    {
        long button = BUTTON_NONE;

        if (redraw != REDRAW_NONE)
        {
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
            rb->cpu_boost(true);
#endif
            switch (redraw)
            {
                case REDRAW_FULL:
                    mylcd_ub_clear_display();
                    mylcd_ub_update();
                    /* fall-through */
                case REDRAW_FULL_OVERLAY:
                    rects_queue_init();
                    break;
                default:
                    break;
            }

            /* paint all rects */
            rects_calc_all(ops->calc, button_yield, (void *)&button);
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
            rb->cpu_boost(false);
#endif
            /* not interrupted by button press - screen is fully drawn */
            redraw = (button == BUTTON_NONE) ? REDRAW_NONE : REDRAW_PARTIAL;
        }

        if (button == BUTTON_NONE)
            button = rb->button_get(true);

        switch (button)
        {
#ifdef FRACTAL_RC_QUIT
        case FRACTAL_RC_QUIT:
#endif
        case FRACTAL_QUIT:
            return PLUGIN_OK;

        case FRACTAL_ZOOM_OUT:
#ifdef FRACTAL_ZOOM_OUT_PRE
            if (lastbutton != FRACTAL_ZOOM_OUT_PRE)
                break;
#endif
            if (!ops->zoom(-1))
                redraw = REDRAW_FULL;
            break;


        case FRACTAL_ZOOM_IN:
#ifdef FRACTAL_ZOOM_IN_PRE
            if (lastbutton != FRACTAL_ZOOM_IN_PRE)
                break;
#endif
#ifdef FRACTAL_ZOOM_IN2
        case FRACTAL_ZOOM_IN2:
#endif
            if (!ops->zoom(1))
                redraw = REDRAW_FULL;
            break;

        case FRACTAL_UP:
            ops->move(0, +1);
            mylcd_ub_scroll_down(LCD_SHIFT_Y);
            mylcd_ub_update();
            if (redraw != REDRAW_FULL)
                redraw = rects_move_down() ? REDRAW_FULL : REDRAW_PARTIAL;
            break;

        case FRACTAL_DOWN:
            ops->move(0, -1);
            mylcd_ub_scroll_up(LCD_SHIFT_Y);
            mylcd_ub_update();
            if (redraw != REDRAW_FULL)
                redraw = rects_move_up() ? REDRAW_FULL : REDRAW_PARTIAL;
            break;

        case FRACTAL_LEFT:
            ops->move(-1, 0);
            mylcd_ub_scroll_right(LCD_SHIFT_X);
            mylcd_ub_update();
            if (redraw != REDRAW_FULL)
                redraw = rects_move_right() ? REDRAW_FULL : REDRAW_PARTIAL;
            break;

        case FRACTAL_RIGHT:
            ops->move(+1, 0);
            mylcd_ub_scroll_left(LCD_SHIFT_X);
            mylcd_ub_update();
            if (redraw != REDRAW_FULL)
                redraw = rects_move_left() ? REDRAW_FULL : REDRAW_PARTIAL;
            break;

        case FRACTAL_PRECISION_DEC:
#ifdef FRACTAL_PRECISION_DEC_PRE
            if (lastbutton != FRACTAL_PRECISION_DEC_PRE)
                break;
#endif
            if (ops->precision(-1))
                redraw = REDRAW_FULL_OVERLAY;

            break;

        case FRACTAL_PRECISION_INC:
#ifdef FRACTAL_PRECISION_INC_PRE
            if (lastbutton != FRACTAL_PRECISION_INC_PRE)
                break;
#endif
            if (ops->precision(+1))
                redraw = REDRAW_FULL_OVERLAY;

            break;

        case FRACTAL_RESET:
            ops->init();
            redraw = REDRAW_FULL;
            break;

        default:
            exit_on_usb(button);
            break;
        }
#if defined(FRACTAL_ZOOM_OUT_PRE) || \
    defined(FRACTAL_ZOOM_IN_PRE) || \
    defined(FRACTAL_PRECISION_DEC_PRE) || \
    defined(FRACTAL_PRECISION_INC_PRE)
        if (button != BUTTON_NONE)
            lastbutton = button;
#endif
    }
    return PLUGIN_OK;
}