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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "starfield_plugin.h"

#include "gate/mathematics.h"
#include "gate/randomgen.h"

#include "gate/graphics/drawing.h"
#include "gate/graphics/fonts.h"
#include "gate/times.h"

static gate_rasterimage_t starfield_backbuffer;
#if defined(GATE_SYS_DOS)
typedef float num_t;

#define NUM_FROM_INT(x) ((float)x)
#define NUM_INIT_FLOAT(x, micro) ((float)x + (float)micro / 1000000.0f)


#define NUM_INIT_INT(x) (x)

#define NUM_NADD(x, y) (x + y)
#define NUM_NSUB(x, y) (x - y)
#define NUM_NMUL(x, y) (x * y)
#define NUM_NDIV(x, y) (x / y)

#define NUM_IADD(x, y) (x + y)
#define NUM_ISUB(x, y) (x - y)
#define NUM_IMUL(x, y) (x * y)
#define NUM_IDIV(x, y) (x / y)

#define NUM_SQRT(x) ((float)gate_math_sqrt((gate_real64_t)x))
#define NUM_2_INT(x) (int)x

#define NUM_EQ(x, y) (x == y)
#define NUM_LESS(x, y) (x < y)
#define NUM_LESSEQ(x, y) (x <= y)
#define NUM_GT(x, y) (x > y)
#define NUM_GTEQ(x, y) (x >= y)
#define NUM_RND(r) ((float)((r / 0xffffffff) & 0xffffffff) / (float)0xffffffff)

static num_t const num_zero = GATE_DECIMAL_INIT(0, 0);

#else

typedef gate_decimal_t num_t;

static num_t num_init(int value, int micro)
{
    num_t ret = GATE_DECIMAL_INIT(value, micro);
    return ret;
}
#define NUM_INIT_INT(x) num_init((int)(x), 0)
#define NUM_INIT_FLOAT(x, micro) num_init((int)(x), micro * GATE_DECIMAL_FACTOR / 1000000)

static num_t num_nadd(num_t x, num_t y) { gate_decimal_add_dec(&x, &y); return x; }
static num_t num_nsub(num_t x, num_t y) { gate_decimal_sub_dec(&x, &y); return x; }
static num_t num_nmul(num_t x, num_t y) { gate_decimal_mul_dec(&x, &y); return x; }
static num_t num_ndiv(num_t x, num_t y) { gate_decimal_div_dec(&x, &y); return x; }
#define NUM_NADD(x, y) num_nadd(x, y)
#define NUM_NSUB(x, y) num_nsub(x, y)
#define NUM_NMUL(x, y) num_nmul(x, y)
#define NUM_NDIV(x, y) num_ndiv(x, y)

static num_t num_iadd(num_t x, int y) { gate_decimal_add_int(&x, y); return x; }
static num_t num_isub(num_t x, int y) { gate_decimal_sub_int(&x, y); return x; }
static num_t num_imul(num_t x, int y) { gate_decimal_mul_int(&x, y); return x; }
static num_t num_idiv(num_t x, int y) { gate_decimal_div_int(&x, y); return x; }
#define NUM_IADD(x, y) num_iadd(x, y)
#define NUM_ISUB(x, y) num_isub(x, y)
#define NUM_IMUL(x, y) num_imul(x, y)
#define NUM_IDIV(x, y) num_idiv(x, y)

static num_t num_sqrt(num_t x) { gate_decimal_sqrt(&x); return x; }
#define NUM_SQRT(x) num_sqrt(x)
#define NUM_2_INT(x) gate_decimal_get_int(&(x))

static int num_comp(num_t x, num_t y)
{
    return gate_decimal_compare(&x, &y);
}

#define NUM_EQ(x, y) (num_comp(x, y) == 0)
#define NUM_LESS(x, y) (num_comp(x, y) < 0)
#define NUM_LESSEQ(x, y) (num_comp(x, y) <= 0)
#define NUM_GT(x, y) (num_comp(x, y) > 0)
#define NUM_GTEQ(x, y) (num_comp(x, y) >= 0)
static num_t num_rnd(gate_uint64_t r)
{
    num_t n = GATE_DECIMAL_INIT(0, (gate_uint32_t)(r % GATE_DECIMAL_FACTOR));
    return n;
}
#define NUM_RND(r) num_rnd(r)

static num_t const num_zero = GATE_DECIMAL_INIT(0, 0);


#endif


typedef struct star_class
{
    num_t x;
    num_t y;
    num_t warp;
} star_t;


typedef struct starfield_class
{
    gate_size_t				stars_count;
    num_t					warp_factor;
    unsigned				fps;
    gate_int64_t			frame_timeout_us;
    gate_randomsession_t	session;
    star_t					stars[512];
    gate_timecounter_t		last_update;
    gate_timecounter_t		last_paint;
} starfield_t;

static starfield_t sf;

static gate_color_t black = GATE_COLOR_BLACK;
static gate_color_t white = GATE_COLOR_WHITE;

static void starfield_paint(starfield_t* starfield, gate_rasterimage_t* image)
{
    gate_size_t ndx;
    num_t width, height;
    star_t* ptr_star;
    gate_uint64_t r;
    num_t mx, my;
    num_t vx, vy, vx2, vy2, vz;
    num_t l;
    num_t sx, sy;
    num_t tmp;
    gate_timecounter_t now;
    gate_int64_t timediff;
#if !defined(GATE_SYS_DOS)
    gate_uint16_t current_fps = 0;
    char buffer[256];
    gate_strbuilder_t builder = GATE_INIT_EMPTY;
    gate_string_t title = GATE_STRING_INIT_EMPTY;
    static gate_color_t const text_color = GATE_COLOR_WHITE;
#endif

    width = NUM_INIT_INT(gate_rasterimage_width(image));
    height = NUM_INIT_INT(gate_rasterimage_height(image));

    mx = NUM_IDIV(width, 2);
    my = NUM_IDIV(height, 2);

    gate_rasterimage_clear(image, &black);

    for (ndx = 0; ndx != starfield->stars_count; ++ndx)
    {
        ptr_star = &starfield->stars[ndx];
        if (NUM_LESSEQ(ptr_star->x, num_zero) || NUM_LESSEQ(ptr_star->y, num_zero) || NUM_GT(ptr_star->x, width) || NUM_GT(ptr_star->y, height))
        {
            gate_randomgenerator_get_num(&starfield->session, &r);
            tmp = NUM_RND(r);
            ptr_star->x = NUM_NMUL(width, tmp);
            gate_randomgenerator_get_num(&starfield->session, &r);
            tmp = NUM_RND(r);
            ptr_star->y = NUM_NMUL(height, tmp);
            tmp = NUM_NSUB(ptr_star->x, mx);
            if (NUM_EQ(tmp, num_zero))
            {
                ptr_star->x = NUM_IADD(ptr_star->x, 1);
            }
            ptr_star->warp = NUM_INIT_INT(1);
        }
        ptr_star->warp = NUM_NMUL(ptr_star->warp, starfield->warp_factor);
        vx = NUM_NSUB(ptr_star->x, mx);
        vy = NUM_NSUB(ptr_star->y, my);
        vx2 = NUM_NMUL(vx, vx);
        vy2 = NUM_NMUL(vy, vy);
        vz = NUM_NADD(vx2, vy2);
        l = NUM_SQRT(vz);
        vx = NUM_NDIV(vx, l);
        vy = NUM_NDIV(vy, l);
        vx2 = NUM_NMUL(vx, ptr_star->warp);
        vy2 = NUM_NMUL(vy, ptr_star->warp);
        sx = NUM_NADD(ptr_star->x, vx2);
        sy = NUM_NADD(ptr_star->y, vy2);
        gate_draw_line(image, NUM_2_INT(ptr_star->x), NUM_2_INT(ptr_star->y), NUM_2_INT(sx), NUM_2_INT(sy), white);

        ptr_star->x = sx;
        ptr_star->y = sy;
    }

    gate_timecounter_now(&now);
    timediff = gate_timecounter_diff(now, starfield->last_paint);
    starfield->last_paint = now;
#if !defined(GATE_SYS_DOS)
    if (timediff > 0)
    {
        current_fps = (gate_uint16_t)((gate_int64_t)1000000 / timediff);
    }
    gate_strbuilder_create_static(&builder, buffer, sizeof(buffer), 0);
    gate_strbuilder_append(&builder,
        GATE_PRINT_CSTR, "Starfield: ",
        GATE_PRINT_UI16, current_fps,
        GATE_PRINT_CSTR, " fps (",
        GATE_PRINT_UI16, sf.fps,
        GATE_PRINT_CSTR, ")",
        GATE_PRINT_END);
    gate_strbuilder_to_string(&builder, &title);
    gate_font8_print_text(image, &title, 0, 0, text_color);
#endif
}

static void set_fps(unsigned fps)
{
    if (fps < 5)
    {
        fps = 5;
    }
    else if (fps > 200)
    {
        fps = 200;
    }
    sf.fps = fps;
    sf.frame_timeout_us = (1000000L / fps);
}

static gate_result_t gateframes_starfield_create(gate_framebuffer_t* fb)
{
    gate_result_t ret;
    gate_framebuffer_info_t fb_info;
    gate_uint16_t format_id;

    do
    {
        ret = gate_framebuffer_get_info(fb, &fb_info);
        GATE_BREAK_IF_FAILED(ret);

        switch (fb_info.bits_per_pixel)
        {
        case 8:		format_id = GATE_IMAGE_PIXELFORMAT_PAL8; break;
        case 15:	format_id = GATE_IMAGE_PIXELFORMAT_RGB555; break;
        case 16:	format_id = GATE_IMAGE_PIXELFORMAT_RGB555; break;
        case 24:	format_id = GATE_IMAGE_PIXELFORMAT_RGB24; break;
        case 32:	format_id = GATE_IMAGE_PIXELFORMAT_RGBA; break;
        default:	format_id = GATE_IMAGE_PIXELFORMAT_DEFAULT; break;
        }

        if (NULL == gate_rasterimage_create(&starfield_backbuffer, format_id, fb_info.width, fb_info.height, NULL))
        {
            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }

        ret = gate_randomgenerator_create(&sf.session);
        if (GATE_FAILED(ret))
        {
            gate_rasterimage_release(&starfield_backbuffer);
            break;
        }

        sf.stars_count = 64;
        sf.warp_factor = NUM_INIT_FLOAT(1, 150000);
        sf.fps = 25;

        set_fps(25);

        ret = GATE_RESULT_OK;
    } while (0);

    return ret;
}

static gate_result_t gateframes_starfield_destroy(gate_framebuffer_t* fb)
{
    GATE_UNUSED_ARG(fb);
    gate_randomgenerator_destroy(&sf.session);
    gate_rasterimage_release(&starfield_backbuffer);
    return GATE_RESULT_OK;
}

static gate_result_t gateframes_starfield_render(gate_framebuffer_t* fb, gate_bool_t redraw_all)
{
    GATE_UNUSED_ARG(redraw_all);
    starfield_paint(&sf, &starfield_backbuffer);
    return gate_framebuffer_set_image(fb, &starfield_backbuffer, 0, 0);
}

static gate_result_t gateframes_starfield_handle_event(gate_framebuffer_t* fb, gate_framebuffer_event_t* evt,
    gate_bool_t* render_frame, gate_uint32_t* out_next_event_timeout)
{
    gate_result_t ret = GATE_RESULT_OK;
    gate_timecounter_t now;
    gate_int64_t timediff_us;

    GATE_UNUSED_ARG(fb);

    *render_frame = false;

    switch (evt->event_type)
    {
    case GATE_FRAMEBUFFER_EVENT_KEY_DOWN:
    {
        *render_frame = true;
        if (evt->event_data.key.char_code == '+')
        {
            set_fps(sf.fps + 5);
        }
        else if (evt->event_data.key.char_code == '-')
        {
            set_fps(sf.fps - 5);
        }
        break;
    }
    case GATE_FRAMEBUFFER_EVENT_KEY_UP:
    {
        break;
    }
    case GATE_FRAMEBUFFER_EVENT_KEY_PRESS:
    {
        break;
    }
    case GATE_FRAMEBUFFER_EVENT_POINTER_BUTTON_DOWN:
    {
        break;
    }
    case GATE_FRAMEBUFFER_EVENT_POINTER_BUTTON_UP:
    {
        break;
    }
    case GATE_FRAMEBUFFER_EVENT_POINTER_MOVE:
    {
        break;
    }
    case GATE_FRAMEBUFFER_EVENT_CANCEL:
    {
        break;
    }
    }

    gate_timecounter_now(&now);
    timediff_us = gate_timecounter_diff(now, sf.last_update);
    if ((timediff_us < 0) || (timediff_us >= sf.frame_timeout_us))
    {
        sf.last_update = now;
        *out_next_event_timeout = (gate_uint32_t)(sf.frame_timeout_us / 1000);
        *render_frame = true;
    }
    else
    {
        *out_next_event_timeout = (gate_uint32_t)((sf.frame_timeout_us - timediff_us) / 1000);
    }

    return ret;
}


static gateframes_plugin_t starfield_plugin =
{
    &gateframes_starfield_create,
    &gateframes_starfield_destroy,
    &gateframes_starfield_render,
    &gateframes_starfield_handle_event
};


gateframes_plugin_t* get_starfield_plugin()
{
    return &starfield_plugin;
}