summaryrefslogtreecommitdiffstats
path: root/apps/plugins/xrick/system/sysvid_rockbox.c
blob: 697bf2fd060489ec599738b5070a7fda53c809f2 (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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
 /***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Port of xrick, a Rick Dangerous clone, to Rockbox.
 * See http://www.bigorno.net/xrick/
 *
 * Copyright (C) 2008-2014 Pierluigi Vicinanza
 *
 * 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 "xrick/system/system.h"

#include "xrick/config.h"
#include "xrick/draw.h"
#include "xrick/game.h"
#include "xrick/data/img.h"
#include "xrick/debug.h"

#include "plugin.h"
#include "lib/helper.h"

/*
 * Global variables
 */
U8 *sysvid_fb = NULL; /* xRick generic 320x200 8bpp frame buffer */

/*
 * Local variables
 */
static fb_data palette[256] IBSS_ATTR;
static bool isVideoInitialised = false;
#ifndef HAVE_LCD_COLOR
#  include "lib/grey.h"
GREY_INFO_STRUCT_IRAM
static unsigned char greybuffer[LCD_HEIGHT * LCD_WIDTH] IBSS_ATTR; /* off screen buffer */
static unsigned char *gbuf;
#  if LCD_PIXELFORMAT == HORIZONTAL_PACKING
enum { GREYBUFSIZE = (((LCD_WIDTH+7)/8)*LCD_HEIGHT*16+200) };
#  else
enum { GREYBUFSIZE = (LCD_WIDTH*((LCD_HEIGHT+7)/8)*16+200) };
#  endif
#endif /* ndef HAVE_LCD_COLOR */

#ifdef HAVE_LCD_COLOR
static fb_data *lcd_fb = NULL;
#endif

#if (LCD_HEIGHT < SYSVID_HEIGHT)
enum { ROW_RESIZE_STEP = (LCD_HEIGHT << 16) / SYSVID_HEIGHT };

static bool rowsToSkip[SYSVID_HEIGHT];

/*
 *
 */
static void calculateRowsToSkip(void)
{
    U32 currentRow, prevResizedRow;

    prevResizedRow = 0;
    rowsToSkip[0] = false;

    for (currentRow = 1; currentRow < SYSVID_HEIGHT; ++currentRow)
    {
        U32 resizedRow = (currentRow * ROW_RESIZE_STEP) >> 16;
        if (resizedRow == prevResizedRow)
        {
            rowsToSkip[currentRow] = true;
        }
        prevResizedRow = resizedRow;
    }
}
#endif /* (LCD_HEIGHT < SYSVID_HEIGHT) */

#if (LCD_WIDTH < SYSVID_WIDTH)
enum { COLUMN_RESIZE_STEP = (LCD_WIDTH << 16) / (SYSVID_WIDTH + (DRAW_XYMAP_SCRLEFT*2)) };

static bool columnsToSkip[SYSVID_WIDTH + (DRAW_XYMAP_SCRLEFT*2)];

/*
 *
 */
static void calculateColumnsToSkip(void)
{
    U32 currentColumn, prevResizedColumn;

    prevResizedColumn = 0;
    columnsToSkip[0] = false;

    for (currentColumn = 1; currentColumn < (SYSVID_WIDTH + (DRAW_XYMAP_SCRLEFT*2)); ++currentColumn)
    {
        U32 resizedColumn = (currentColumn * COLUMN_RESIZE_STEP) >> 16;
        if (resizedColumn == prevResizedColumn)
        {
            columnsToSkip[currentColumn] = true;
        }
        prevResizedColumn = resizedColumn;
    }
}
#endif /* (LCD_WIDTH < SYSVID_WIDTH) */

/*
 *
 */
void sysvid_setPalette(img_color_t *pal, U16 n)
{
    U16 i;

    for (i = 0; i < n; i++)
    {
#ifdef HAVE_LCD_COLOR
#if LCD_PIXELFORMAT == XRGB8888
        int x =  LCD_RGBPACK(pal[i].r, pal[i].g, pal[i].b);
        palette[i] = FB_SCALARPACK(x);
#else
        palette[i] = LCD_RGBPACK(pal[i].r, pal[i].g, pal[i].b);
#endif
#else
        palette[i] = ((3 * pal[i].r) + (6 * pal[i].g) + pal[i].b) / 10;
#endif
    }
}

/*
 *
 */
void sysvid_setGamePalette()
{
    sysvid_setPalette(game_colors, game_color_count);
}

/*
 * Update screen
 */
void sysvid_update(const rect_t *rects)
{
    unsigned sourceRow, sourceLastRow;
    unsigned sourceColumn, sourceLastColumn;
    unsigned resizedRow, resizedColumn;
    unsigned resizedWidth, resizedHeight;
    unsigned x, y;
    U8 *sourceBuf, *sourceTemp;
    fb_data *destBuf, *destTemp;

    if (!rects)
    {
        return;
    }

    while (rects)
    {
        sourceRow = rects->y;
        sourceLastRow = sourceRow + rects->height;
        sourceColumn = rects->x;
        sourceLastColumn = sourceColumn + rects->width;

#if (LCD_WIDTH < SYSVID_WIDTH)
        /* skip black borders */
        if (sourceColumn < -DRAW_XYMAP_SCRLEFT)
        {
            sourceColumn = -DRAW_XYMAP_SCRLEFT;
        }
        if (sourceLastColumn > (SYSVID_WIDTH + DRAW_XYMAP_SCRLEFT))
        {
            sourceLastColumn = SYSVID_WIDTH + DRAW_XYMAP_SCRLEFT;
        }

        /* skip unwanted columns */
        while (columnsToSkip[sourceColumn + DRAW_XYMAP_SCRLEFT] /* && sourceColumn < (SYSVID_WIDTH + DRAW_XYMAP_SCRLEFT) */)
        {
            ++sourceColumn;
        }

        resizedColumn = ((sourceColumn + DRAW_XYMAP_SCRLEFT) * COLUMN_RESIZE_STEP) >> 16;
        resizedWidth = 0;
#else
        resizedColumn = sourceColumn;
        resizedWidth = rects->width;
#endif /* (LCD_WIDTH < SYSVID_WIDTH) */

#if (LCD_HEIGHT < SYSVID_HEIGHT)
        /* skip unwanted rows */
        while (rowsToSkip[sourceRow] /* && sourceRow < SYSVID_HEIGHT */)
        {
            ++sourceRow;
        }

        resizedRow = (sourceRow * ROW_RESIZE_STEP) >> 16;
        resizedHeight = 0;
#else
        resizedRow = sourceRow;
        resizedHeight = rects->height;
#endif /* (LCD_HEIGHT < SYSVID_HEIGHT) */

        sourceBuf = sysvid_fb;
        sourceBuf += sourceColumn + sourceRow * SYSVID_WIDTH;

#ifdef HAVE_LCD_COLOR
        if(!lcd_fb)
        {
            struct viewport *vp_main = rb->lcd_set_viewport(NULL);
            lcd_fb = vp_main->buffer->fb_ptr;
        }
        destBuf = lcd_fb;
#else
        destBuf = (fb_data*) greybuffer;
#endif /* HAVE_LCD_COLOR */
        destBuf += resizedColumn + resizedRow * LCD_WIDTH;

#if (LCD_WIDTH < SYSVID_WIDTH)
        sourceColumn += DRAW_XYMAP_SCRLEFT;
        sourceLastColumn += DRAW_XYMAP_SCRLEFT;
#endif /* (LCD_WIDTH < SYSVID_WIDTH) */

        for (y = sourceRow; y < sourceLastRow; ++y)
        {
#if (LCD_HEIGHT < SYSVID_HEIGHT)
            if (rowsToSkip[y])
            {
                sourceBuf += SYSVID_WIDTH;
                continue;
            }

            ++resizedHeight;
#endif /* (LCD_HEIGHT < SYSVID_HEIGHT) */

            sourceTemp = sourceBuf;
            destTemp = destBuf;
            for (x = sourceColumn; x < sourceLastColumn; ++x)
            {
#if (LCD_WIDTH < SYSVID_WIDTH)
                if (columnsToSkip[x])
                {
                    ++sourceTemp;
                    continue;
                }

                if (y == sourceRow)
                {
                    ++resizedWidth;
                }
#endif /* (LCD_WIDTH < SYSVID_WIDTH) */

                *destTemp = palette[*sourceTemp];

                ++sourceTemp;
                ++destTemp;
            }

            sourceBuf += SYSVID_WIDTH;
            destBuf += LCD_WIDTH;
        }

#ifdef HAVE_LCD_COLOR
        IFDEBUG_VIDEO2(
            for (y = resizedRow; y < resizedRow + resizedHeight; ++y)
            {
                destBuf = lcd_fb + resizedColumn + y * LCD_WIDTH;
                *destBuf = palette[0x01];
                *(destBuf + resizedWidth - 1) = palette[0x01];
            }

            for (x = resizedColumn; x < resizedColumn + resizedWidth; ++x)
            {
                destBuf = rb->lcd_fb + x + resizedRow * LCD_WIDTH;
                *destBuf = palette[0x01];
                *(destBuf + (resizedHeight - 1) * LCD_WIDTH) = palette[0x01];
            }
        );

        rb->lcd_update_rect(resizedColumn, resizedRow, resizedWidth, resizedHeight);
#else
        grey_ub_gray_bitmap_part(greybuffer, resizedColumn, resizedRow, LCD_WIDTH, resizedColumn, resizedRow, resizedWidth, resizedHeight);
#endif /* HAVE_LCD_COLOR */

       rects = rects->next;
    }
}

/*
 * Clear screen
 * (077C)
 */
void sysvid_clear(void)
{
    rb->memset(sysvid_fb, 0, sizeof(U8) * SYSVID_WIDTH * SYSVID_HEIGHT);
}

/*
 * Initialise video
 */
bool sysvid_init()
{
    bool success;

    if (isVideoInitialised)
    {
        return true;
    }

    IFDEBUG_VIDEO(sys_printf("xrick/video: start\n"););

    success = false;
    do
    {
        /* allocate xRick generic frame buffer into memory */
        sysvid_fb = sysmem_push(sizeof(U8) * SYSVID_WIDTH * SYSVID_HEIGHT);
        if (!sysvid_fb)
        {
            sys_error("(video) unable to allocate frame buffer");
            break;
        }

#ifndef HAVE_LCD_COLOR
        gbuf = sysmem_push(GREYBUFSIZE);
        if (!gbuf)
        {
            sys_error("(video) unable to allocate buffer for greyscale functions");
            break;
        }

        if (!grey_init(gbuf, GREYBUFSIZE, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL))
        {
            sys_error("(video) not enough memory to initialise greyscale functions");
            break;
        }
#endif /* ndef HAVE_LCD_COLOR */

        success = true;
    } while (false);

    if (!success)
    {
#ifndef HAVE_LCD_COLOR
        sysmem_pop(gbuf);
#endif
        sysmem_pop(sysvid_fb);
        return false;
    }

#if (LCD_HEIGHT < SYSVID_HEIGHT)
    calculateRowsToSkip();
#endif
#if (LCD_WIDTH < SYSVID_WIDTH)
    calculateColumnsToSkip();
#endif

#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif
    /* Turn off backlight timeout */
    backlight_ignore_timeout();

    rb->lcd_set_foreground(LCD_WHITE);
    rb->lcd_set_background(LCD_BLACK);
    rb->lcd_clear_display();

#ifdef HAVE_LCD_COLOR
    rb->lcd_update();
#else
    /* switch on greyscale overlay */
    grey_show(true);
#endif /* HAVE_LCD_COLOR */

    isVideoInitialised = true;
    IFDEBUG_VIDEO(sys_printf("xrick/video: ready\n"););
    return true;
}

/*
 * Shutdown video
 */
void sysvid_shutdown(void)
{
    if (!isVideoInitialised)
    {
        return;
    }

#ifndef HAVE_LCD_COLOR
    grey_show(false);
    grey_release();

    sysmem_pop(gbuf);
#endif /* ndef HAVE_LCD_COLOR */
    sysmem_pop(sysvid_fb);

    /* Turn on backlight timeout (revert to settings) */
    backlight_use_settings();

    isVideoInitialised = false;
    IFDEBUG_VIDEO(sys_printf("xrick/video: stop\n"););
}

/* eof */