summaryrefslogtreecommitdiff
path: root/apps/plugins/xrick/system/system_rockbox.c
blob: 74de2ff4302b82bd8580d8b67a15af533f31af30 (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
 /***************************************************************************
 *             __________               __   ___.
 *   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"
#ifdef ENABLE_SOUND
#include "xrick/system/syssnd_rockbox.h"
#endif

#include "plugin.h"

enum { LINE_LENGTH = 80 };

/*
* Error
*/
void sys_error(const char *err, ...)
{
    va_list argptr;
    char s[LINE_LENGTH];

    /* prepare message */
    va_start(argptr, err);
    rb->vsnprintf(s, sizeof(s), err, argptr);
    va_end(argptr);

    /* print error message */
    rb->splashf(HZ*3, "%s\nError!\n", s);
    DEBUGF("%s\nError!\n", s);
}

/*
* Print a message to standard output
*/
void sys_printf(const char *msg, ...)
{
    va_list argptr;
    char s[LINE_LENGTH];

    /* prepare message */
    va_start(argptr, msg);
    rb->vsnprintf(s, sizeof(s), msg, argptr);
    va_end(argptr);

    /* print message */
    DEBUGF("%s",s);

#ifdef ENABLE_SYSPRINTF_TO_SCREEN
    {
        static int currentYPos = 0;
        size_t i;

        /* Device LCDs display newlines funny. */
        for(i = 0; s[i] != '\0'; ++i)
        {
            if(s[i] == '\n')
            {
                s[i] = ' ';
            }
        }

        rb->lcd_putsxy(1, currentYPos, (unsigned char *)s);
        rb->lcd_update();

        currentYPos += 12;
        if(currentYPos > LCD_HEIGHT-12)
        {
            currentYPos = 0;
            rb->lcd_clear_display();
        }
    }
#endif /* ENABLE_SYSPRINTF_TO_SCREEN */
}

/*
* Print a message to string buffer
*/
void sys_snprintf(char *buf, size_t size, const char *msg, ...)
{
    va_list argptr;

    va_start(argptr, msg);
    rb->vsnprintf(buf, size, msg, argptr);
    va_end(argptr);
}

/*
* Returns string length
*/
size_t sys_strlen(const char * str)
{
    return rb->strlen(str);
}

/*
* Return number of milliseconds elapsed since first call
*/
U32 sys_gettime(void)
{
    long ticks = *(rb->current_tick);
    return (U32)((ticks * 1000) / HZ);
}

/*
* Yield execution to another thread
*/
void sys_yield(void)
{
    rb->yield();
}

/*
* Initialize system
*/
bool sys_init(int argc, char **argv)
{
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(true);
#endif

    if (!sysarg_init(argc, argv))
    {
        return false;
    }
    if (!sysmem_init())
    {
        return false;
    }
    if (!sysvid_init())
    {
        return false;
    }
#ifdef ENABLE_SOUND
    if (!sysarg_args_nosound && !syssnd_init())
    {
        return false;
    }
#endif
    if (!sysfile_setRootPath(sysarg_args_data? sysarg_args_data : sysfile_defaultPath))
    {
        return false;
    }
    return true;
}

/*
* Shutdown system
*/
void sys_shutdown(void)
{
    sysfile_clearRootPath();
#ifdef ENABLE_SOUND
    syssnd_shutdown();
#endif
    sysvid_shutdown();
    sysmem_shutdown();

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(false);
#endif
}

/*
* Preload data before entering main loop
*/
bool sys_cacheData(void)
{
#ifdef ENABLE_SOUND
    syssnd_load(soundGameover);
    syssnd_load(soundSbonus2);
    syssnd_load(soundBullet);
    syssnd_load(soundBombshht);
    syssnd_load(soundExplode);
    syssnd_load(soundStick);
    syssnd_load(soundWalk);
    syssnd_load(soundCrawl);
    syssnd_load(soundJump);
    syssnd_load(soundPad);
    syssnd_load(soundBox);
    syssnd_load(soundBonus);
    syssnd_load(soundSbonus1);
    syssnd_load(soundDie);
    syssnd_load(soundEntity[0]);
    syssnd_load(soundEntity[1]);
    syssnd_load(soundEntity[2]);
    syssnd_load(soundEntity[3]);
    syssnd_load(soundEntity[4]);
    syssnd_load(soundEntity[5]);
    syssnd_load(soundEntity[6]);
    syssnd_load(soundEntity[7]);
    syssnd_load(soundEntity[8]);
    syssnd_load(soundTune0);
    syssnd_load(soundTune1);
    syssnd_load(soundTune2);
    syssnd_load(soundTune3);
    syssnd_load(soundTune4);
    syssnd_load(soundTune5);
#endif /* ENABLE_SOUND */
    return true;
}

/*
* Clear preloaded data before shutdown
*/
void sys_uncacheData(void)
{
#ifdef ENABLE_SOUND
    syssnd_unload(soundTune5);
    syssnd_unload(soundTune4);
    syssnd_unload(soundTune3);
    syssnd_unload(soundTune2);
    syssnd_unload(soundTune1);
    syssnd_unload(soundTune0);
    syssnd_unload(soundEntity[8]);
    syssnd_unload(soundEntity[7]);
    syssnd_unload(soundEntity[6]);
    syssnd_unload(soundEntity[5]);
    syssnd_unload(soundEntity[4]);
    syssnd_unload(soundEntity[3]);
    syssnd_unload(soundEntity[2]);
    syssnd_unload(soundEntity[1]);
    syssnd_unload(soundEntity[0]);
    syssnd_unload(soundDie);
    syssnd_unload(soundSbonus1);
    syssnd_unload(soundBonus);
    syssnd_unload(soundBox);
    syssnd_unload(soundPad);
    syssnd_unload(soundJump);
    syssnd_unload(soundCrawl);
    syssnd_unload(soundWalk);
    syssnd_unload(soundStick);
    syssnd_unload(soundExplode);
    syssnd_unload(soundBombshht);
    syssnd_unload(soundBullet);
    syssnd_unload(soundSbonus2);
    syssnd_unload(soundGameover);
#endif /* ENABLE_SOUND */
}

/* eof */