summaryrefslogtreecommitdiff
path: root/apps/plugins/xrick/system
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/xrick/system')
-rw-r--r--apps/plugins/xrick/system/basic_funcs.c33
-rw-r--r--apps/plugins/xrick/system/basic_funcs.h141
-rw-r--r--apps/plugins/xrick/system/basic_types.h48
-rw-r--r--apps/plugins/xrick/system/main_rockbox.c43
-rw-r--r--apps/plugins/xrick/system/miniz_config.h38
-rw-r--r--apps/plugins/xrick/system/rockboxcodes.h110
-rw-r--r--apps/plugins/xrick/system/sysarg_rockbox.c49
-rw-r--r--apps/plugins/xrick/system/sysevt_rockbox.c156
-rw-r--r--apps/plugins/xrick/system/sysfile_rockbox.c122
-rw-r--r--apps/plugins/xrick/system/sysmem_rockbox.c156
-rw-r--r--apps/plugins/xrick/system/sysmenu_rockbox.c200
-rw-r--r--apps/plugins/xrick/system/sysmenu_rockbox.h32
-rw-r--r--apps/plugins/xrick/system/syssnd_rockbox.c483
-rw-r--r--apps/plugins/xrick/system/syssnd_rockbox.h48
-rw-r--r--apps/plugins/xrick/system/system.h178
-rw-r--r--apps/plugins/xrick/system/system_rockbox.c262
-rw-r--r--apps/plugins/xrick/system/sysvid_rockbox.c402
17 files changed, 2501 insertions, 0 deletions
diff --git a/apps/plugins/xrick/system/basic_funcs.c b/apps/plugins/xrick/system/basic_funcs.c
new file mode 100644
index 0000000000..fbc025a6ef
--- /dev/null
+++ b/apps/plugins/xrick/system/basic_funcs.c
@@ -0,0 +1,33 @@
1/*
2 * xrick/system/basic_funcs.c
3 *
4 * Copyright (C) 2008-2014 Pierluigi Vicinanza. All rights reserved.
5 *
6 * The use and distribution terms for this software are contained in the file
7 * named README, which can be found in the root of this distribution. By
8 * using this software in any fashion, you are agreeing to be bound by the
9 * terms of this license.
10 *
11 * You must not remove this notice, or any other, from this software.
12 */
13
14#include "xrick/system/basic_funcs.h"
15
16#ifdef USE_DEFAULT_ENDIANNESS_FUNCTIONS
17
18extern inline uint16_t swap16(uint16_t x);
19extern inline uint32_t swap32(uint32_t x);
20
21extern inline uint16_t htobe16(uint16_t host);
22extern inline uint16_t htole16(uint16_t host);
23extern inline uint16_t betoh16(uint16_t big_endian);
24extern inline uint16_t letoh16(uint16_t little_endian);
25
26extern inline uint32_t htobe32(uint32_t host);
27extern inline uint32_t htole32(uint32_t host);
28extern inline uint32_t betoh32(uint32_t big_endian);
29extern inline uint32_t letoh32(uint32_t little_endian);
30
31#endif /* USE_DEFAULT_ENDIANNESS_FUNCTIONS */
32
33/* eof */
diff --git a/apps/plugins/xrick/system/basic_funcs.h b/apps/plugins/xrick/system/basic_funcs.h
new file mode 100644
index 0000000000..1ac5c58d32
--- /dev/null
+++ b/apps/plugins/xrick/system/basic_funcs.h
@@ -0,0 +1,141 @@
1/*
2 * xrick/system/basic_funcs.h
3 *
4 * Copyright (C) 2008-2014 Pierluigi Vicinanza. All rights reserved.
5 *
6 * The use and distribution terms for this software are contained in the file
7 * named README, which can be found in the root of this distribution. By
8 * using this software in any fashion, you are agreeing to be bound by the
9 * terms of this license.
10 *
11 * You must not remove this notice, or any other, from this software.
12 */
13
14#ifndef _BASIC_FUNCS_H
15#define _BASIC_FUNCS_H
16
17#include "xrick/system/basic_types.h"
18#include "xrick/system/system.h"
19
20#ifdef __WIN32__
21/* Windows is little endian only */
22# define __ORDER_LITTLE_ENDIAN__ 1234
23# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
24# define USE_DEFAULT_ENDIANNESS_FUNCTIONS
25# include <stdlib.h> /* _byteswap_XXX */
26
27#elif defined(ROCKBOX)
28/* Rockbox*/
29# include "plugin.h"
30# define __ORDER_LITTLE_ENDIAN__ 1234
31# define __ORDER_BIG_ENDIAN__ 4321
32# ifdef ROCKBOX_BIG_ENDIAN
33# define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
34# else
35# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
36# endif
37
38#elif (defined(__FreeBSD__) && __FreeBSD_version >= 470000) || defined(__OpenBSD__) || defined(__NetBSD__)
39/* *BSD */
40# include <sys/endian.h>
41# define __ORDER_BIG_ENDIAN__ BIG_ENDIAN
42# define __ORDER_LITTLE_ENDIAN__ LITTLE_ENDIAN
43# define __BYTE_ORDER__ BYTE_ORDER
44
45#elif (defined(BSD) && (BSD >= 199103)) || defined(__MacOSX__)
46/* more BSD */
47# include <machine/endian.h>
48# define __ORDER_BIG_ENDIAN__ BIG_ENDIAN
49# define __ORDER_LITTLE_ENDIAN__ LITTLE_ENDIAN
50# define __BYTE_ORDER__ BYTE_ORDER
51
52#elif defined(__linux__) /*|| defined (__BEOS__)*/
53/* Linux, BeOS */
54# include <endian.h>
55# define betoh16(x) be16toh(x)
56# define letoh16(x) le16toh(x)
57# define betoh32(x) be32toh(x)
58# define letoh32(x) le32toh(x)
59
60#else
61/* shall we just '#include <endian.h>'? */
62# define USE_DEFAULT_ENDIANNESS_FUNCTIONS
63
64#endif /* __WIN32__ */
65
66/* define default endianness */
67#ifndef __ORDER_LITTLE_ENDIAN__
68# define __ORDER_LITTLE_ENDIAN__ 1234
69#endif
70
71#ifndef __ORDER_BIG_ENDIAN__
72# define __ORDER_BIG_ENDIAN__ 4321
73#endif
74
75#ifndef __BYTE_ORDER__
76# warning "Byte order not defined on your system, assuming little endian!"
77# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
78#endif
79
80/* provide default endianness functions */
81#ifdef USE_DEFAULT_ENDIANNESS_FUNCTIONS
82
83# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
84
85inline uint32_t swap32(uint32_t x)
86{
87# ifdef _MSC_VER
88 return _byteswap_ulong(x);
89# elif (GCC_VERSION > 40300) || defined(__clang__)
90 return __builtin_bswap32(x);
91# else
92 return (x >> 24) |
93 ((x >> 8) & 0x0000FF00) |
94 ((x << 8) & 0x00FF0000) |
95 (x << 24);
96# endif /* _MSC_VER */
97}
98
99inline uint16_t swap16(uint16_t x)
100{
101# ifdef _MSC_VER
102 return _byteswap_ushort(x);
103# elif (GCC_VERSION > 40800) || defined(__clang__)
104 return __builtin_bswap16(x);
105# else
106 return (x << 8)|(x >> 8);
107# endif /* _MSC_VER */
108}
109
110# if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__)
111inline uint16_t htobe16(uint16_t host) { return swap16(host); }
112inline uint16_t htole16(uint16_t host) { return host; }
113inline uint16_t betoh16(uint16_t big_endian) { return swap16(big_endian); }
114inline uint16_t letoh16(uint16_t little_endian) { return little_endian; }
115
116inline uint32_t htobe32(uint32_t host) { return swap32(host); }
117inline uint32_t htole32(uint32_t host) { return host; }
118inline uint32_t betoh32(uint32_t big_endian) { return swap32(big_endian); }
119inline uint32_t letoh32(uint32_t little_endian) { return little_endian; }
120
121# elif (__BYTE_ORDER__==__ORDER_BIG_ENDIAN__)
122inline uint16_t htobe16(uint16_t host) { return host; }
123inline uint16_t htole16(uint16_t host) { return swap16(host); }
124inline uint16_t betoh16(uint16_t big_endian) { return big_endian; }
125inline uint16_t letoh16(uint16_t little_endian) { return swap16(little_endian); }
126
127inline uint32_t htobe32(uint32_t host) { return host; }
128inline uint32_t htole32(uint32_t host) { return swap32(host); }
129inline uint32_t betoh32(uint32_t big_endian) { return big_endian; }
130inline uint32_t letoh32(uint32_t little_endian) { return swap32(little_endian); }
131
132# else
133# error "Unknown/unsupported byte order!"
134
135# endif
136
137#endif /* USE_DEFAULT_ENDIANNESS_FUNCTIONS */
138
139#endif /* ndef _BASIC_FUNCS_H */
140
141/* eof */
diff --git a/apps/plugins/xrick/system/basic_types.h b/apps/plugins/xrick/system/basic_types.h
new file mode 100644
index 0000000000..e05fd477f3
--- /dev/null
+++ b/apps/plugins/xrick/system/basic_types.h
@@ -0,0 +1,48 @@
1/*
2 * xrick/system/basic_types.h
3 *
4 * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net).
5 * Copyright (C) 2008-2014 Pierluigi Vicinanza.
6 * All rights reserved.
7 *
8 * The use and distribution terms for this software are contained in the file
9 * named README, which can be found in the root of this distribution. By
10 * using this software in any fashion, you are agreeing to be bound by the
11 * terms of this license.
12 *
13 * You must not remove this notice, or any other, from this software.
14 */
15
16#ifndef _BASIC_TYPES_H
17#define _BASIC_TYPES_H
18
19#ifdef _MSC_VER
20
21typedef enum { false, true } bool;
22
23#define inline __inline
24
25typedef unsigned __int8 uint8_t;
26typedef unsigned __int16 uint16_t;
27typedef unsigned __int32 uint32_t;
28typedef __int8 int8_t;
29typedef __int16 int16_t;
30typedef __int32 int32_t;
31
32#else /* ndef _MSC_VER */
33
34#include <stdbool.h>
35#include <stdint.h>
36
37#endif /* _MSC_VER */
38
39typedef uint8_t U8; /* 8 bits unsigned */
40typedef uint16_t U16; /* 16 bits unsigned */
41typedef uint32_t U32; /* 32 bits unsigned */
42typedef int8_t S8; /* 8 bits signed */
43typedef int16_t S16; /* 16 bits signed */
44typedef int32_t S32; /* 32 bits signed */
45
46#endif /* ndef _BASIC_TYPES_H */
47
48/* eof */
diff --git a/apps/plugins/xrick/system/main_rockbox.c b/apps/plugins/xrick/system/main_rockbox.c
new file mode 100644
index 0000000000..e273e1dc8d
--- /dev/null
+++ b/apps/plugins/xrick/system/main_rockbox.c
@@ -0,0 +1,43 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/system/system.h"
26#include "xrick/game.h"
27
28#include "plugin.h"
29
30/* Plug-in entry point */
31enum plugin_status plugin_start(const void* parameter)
32{
33 char *filename = (char*)parameter;
34 bool success = sys_init(1, &filename);
35 if (success)
36 {
37 game_run();
38 }
39 sys_shutdown();
40 return (success? PLUGIN_OK : PLUGIN_ERROR);
41}
42
43/* eof */
diff --git a/apps/plugins/xrick/system/miniz_config.h b/apps/plugins/xrick/system/miniz_config.h
new file mode 100644
index 0000000000..65899b0c23
--- /dev/null
+++ b/apps/plugins/xrick/system/miniz_config.h
@@ -0,0 +1,38 @@
1/*
2 * xrick/system/miniz_config.h
3 *
4 * Copyright (C) 2008-2014 Pierluigi Vicinanza. All rights reserved.
5 *
6 * The use and distribution terms for this software are contained in the file
7 * named README, which can be found in the root of this distribution. By
8 * using this software in any fashion, you are agreeing to be bound by the
9 * terms of this license.
10 *
11 * You must not remove this notice, or any other, from this software.
12 */
13
14#ifndef _MINIZ_CONFIG_H
15#define _MINIZ_CONFIG_H
16
17/*
18 * miniz used only for crc32 calculation
19 */
20#define MINIZ_NO_STDIO
21#define MINIZ_NO_TIME
22#define MINIZ_NO_ARCHIVE_APIS
23#define MINIZ_NO_ARCHIVE_WRITING_APIS
24#define MINIZ_NO_ZLIB_APIS
25#define MINIZ_NO_MALLOC
26#ifdef ROCKBOX
27# define MINIZ_NO_ASSERT
28# ifndef SIMULATOR
29# define MINIZ_X86_OR_X64_CPU 0
30# define MINIZ_HAS_64BIT_REGISTERS 0
31# define TINFL_USE_64BIT_BITBUF 0
32# define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
33# endif /* ndef SIMULATOR */
34#endif
35
36#endif /* ndef _MINIZ_CONFIG_H */
37
38/* eof */
diff --git a/apps/plugins/xrick/system/rockboxcodes.h b/apps/plugins/xrick/system/rockboxcodes.h
new file mode 100644
index 0000000000..ca56c338b6
--- /dev/null
+++ b/apps/plugins/xrick/system/rockboxcodes.h
@@ -0,0 +1,110 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#ifndef _ROCKBOXCODES_H
26#define _ROCKBOXCODES_H
27
28/* keypad mappings */
29#include "plugin.h"
30
31#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
32 (CONFIG_KEYPAD == IRIVER_H300_PAD)
33#define XRICK_BTN_UP BUTTON_UP | BUTTON_REC
34#define XRICK_BTN_DOWN BUTTON_DOWN | BUTTON_MODE
35#define XRICK_BTN_LEFT BUTTON_LEFT
36#define XRICK_BTN_RIGHT BUTTON_RIGHT
37#define XRICK_BTN_FIRE BUTTON_ON
38#define XRICK_BTN_PAUSE BUTTON_SELECT
39#define XRICK_BTN_MENU BUTTON_OFF
40
41#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
42#define XRICK_BTN_MENU BUTTON_POWER
43#define XRICK_BTN_FIRE BUTTON_PLAY
44#define XRICK_BTN_PAUSE BUTTON_REW
45#define XRICK_BTN_UP BUTTON_SCROLL_UP
46#define XRICK_BTN_DOWN BUTTON_SCROLL_DOWN
47#define XRICK_BTN_LEFT BUTTON_LEFT
48#define XRICK_BTN_RIGHT BUTTON_RIGHT
49
50#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
51 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
52 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
53#define XRICK_BTN_UP BUTTON_MENU
54#define XRICK_BTN_DOWN BUTTON_PLAY
55#define XRICK_BTN_LEFT BUTTON_LEFT
56#define XRICK_BTN_RIGHT BUTTON_RIGHT
57#define XRICK_BTN_FIRE BUTTON_SELECT
58#define XRICK_BTN_PAUSE BUTTON_SCROLL_BACK
59#define XRICK_BTN_MENU BUTTON_SCROLL_FWD
60
61#elif (CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD)
62#define XRICK_BTN_UP BUTTON_UP
63#define XRICK_BTN_UPLEFT BUTTON_BACK
64#define XRICK_BTN_UPRIGHT BUTTON_PLAYPAUSE
65#define XRICK_BTN_DOWN BUTTON_DOWN
66#define XRICK_BTN_DOWNLEFT BUTTON_BOTTOMLEFT
67#define XRICK_BTN_DOWNRIGHT BUTTON_BOTTOMRIGHT
68#define XRICK_BTN_LEFT BUTTON_LEFT
69#define XRICK_BTN_RIGHT BUTTON_RIGHT
70#define XRICK_BTN_FIRE BUTTON_VOL_DOWN
71#define XRICK_BTN_PAUSE BUTTON_VOL_UP
72#define XRICK_BTN_MENU BUTTON_POWER
73
74#elif (CONFIG_KEYPAD == SAMSUNG_YH92X_PAD)
75#define XRICK_BTN_UP BUTTON_UP
76#define XRICK_BTN_DOWN BUTTON_DOWN
77#define XRICK_BTN_LEFT BUTTON_LEFT
78#define XRICK_BTN_RIGHT BUTTON_RIGHT
79#define XRICK_BTN_FIRE BUTTON_PLAY
80#define XRICK_BTN_PAUSE BUTTON_FFWD
81#define XRICK_BTN_MENU BUTTON_REW
82
83#elif (CONFIG_KEYPAD == SAMSUNG_YH820_PAD)
84#define XRICK_BTN_UP BUTTON_UP
85#define XRICK_BTN_DOWN BUTTON_DOWN
86#define XRICK_BTN_LEFT BUTTON_LEFT
87#define XRICK_BTN_RIGHT BUTTON_RIGHT
88#define XRICK_BTN_FIRE BUTTON_PLAY
89#define XRICK_BTN_PAUSE BUTTON_FFWD
90#define XRICK_BTN_MENU BUTTON_REW
91
92/* place other keypad mappings here
93#elif CONFIG_KEYPAD ==...
94#define XRICK_BTN...
95*/
96
97#else
98# include "lib/pluginlib_actions.h"
99#define XRICK_BTN_UP PLA_UP
100#define XRICK_BTN_DOWN PLA_DOWN
101#define XRICK_BTN_LEFT PLA_LEFT
102#define XRICK_BTN_RIGHT PLA_RIGHT
103#define XRICK_BTN_FIRE PLA_SELECT
104#define XRICK_BTN_PAUSE PLA_CANCEL
105#define XRICK_BTN_MENU PLA_EXIT
106#endif
107
108#endif /* ndef _ROCKBOXCODES_H */
109
110/* eof */
diff --git a/apps/plugins/xrick/system/sysarg_rockbox.c b/apps/plugins/xrick/system/sysarg_rockbox.c
new file mode 100644
index 0000000000..fa502ff4b0
--- /dev/null
+++ b/apps/plugins/xrick/system/sysarg_rockbox.c
@@ -0,0 +1,49 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/system/system.h"
26
27/*
28 * globals
29 */
30int sysarg_args_period = 0; /* time between each frame, in milliseconds. The default is 40. */
31int sysarg_args_map = 0;
32int sysarg_args_submap = 0;
33bool sysarg_args_nosound = false;
34const char *sysarg_args_data = NULL;
35
36/*
37 * Read and process arguments
38 */
39bool sysarg_init(int argc/*unused*/, char **argv)
40{
41 (void)argc;
42
43 /* note: "*argv" is truly a "const *" */
44 sysarg_args_data = *argv;
45
46 return true;
47}
48
49/* eof */
diff --git a/apps/plugins/xrick/system/sysevt_rockbox.c b/apps/plugins/xrick/system/sysevt_rockbox.c
new file mode 100644
index 0000000000..f5314712e8
--- /dev/null
+++ b/apps/plugins/xrick/system/sysevt_rockbox.c
@@ -0,0 +1,156 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/system/system.h"
26
27#include "xrick/config.h"
28#include "xrick/control.h"
29#include "xrick/game.h"
30#include "xrick/system/sysmenu_rockbox.h"
31#include "xrick/system/rockboxcodes.h"
32
33/*
34 * Helper function to set/clear controls according to key press
35 */
36static inline void checkKey(int key, unsigned button, control_t control)
37{
38 if (key & button)
39 {
40 control_set(control);
41 }
42 else
43 {
44 control_clear(control);
45 }
46}
47
48/*
49 * Process events, if any, then return
50 */
51void sysevt_poll(void)
52{
53 static int previousKey, currentKey;
54
55 /* this is because "Restart Game" is handled via menu */
56 if (control_test(Control_END))
57 {
58 control_clear(Control_END);
59 }
60
61 for (;;)
62 {
63 /* check for USB connection */
64 if ((rb->default_event_handler(rb->button_get(false)) == SYS_USB_CONNECTED)
65#if defined(HAS_BUTTON_HOLD)
66 || rb->button_hold()
67#endif
68 )
69 {
70 sysmenu_exec();
71 }
72
73 currentKey = rb->button_status();
74 if (currentKey != previousKey)
75 {
76 break;
77 }
78 else if (game_waitevt)
79 {
80 rb->yield();
81 }
82 else /* (currentKey == previousKey) && !game_waitevt */
83 {
84 return;
85 }
86 }
87
88#ifdef XRICK_BTN_MENU
89 if (currentKey & XRICK_BTN_MENU)
90 {
91 sysmenu_exec();
92 }
93#endif
94
95#ifdef XRICK_BTN_PAUSE
96 checkKey(currentKey, XRICK_BTN_PAUSE, Control_PAUSE);
97#endif
98
99 checkKey(currentKey, XRICK_BTN_UP, Control_UP);
100
101 checkKey(currentKey, XRICK_BTN_DOWN, Control_DOWN);
102
103 checkKey(currentKey, XRICK_BTN_LEFT, Control_LEFT);
104
105 checkKey(currentKey, XRICK_BTN_RIGHT, Control_RIGHT);
106
107 checkKey(currentKey, XRICK_BTN_FIRE, Control_FIRE);
108
109#ifdef XRICK_BTN_UPLEFT
110 if (!control_test(Control_UP | Control_LEFT))
111 {
112 checkKey(currentKey, XRICK_BTN_UPLEFT, Control_UP | Control_LEFT);
113 }
114#endif /* XRICK_BTN_UPLEFT */
115
116#ifdef XRICK_BTN_UPRIGHT
117 if (!control_test(Control_UP | Control_RIGHT))
118 {
119 checkKey(currentKey, XRICK_BTN_UPRIGHT, Control_UP | Control_RIGHT);
120 }
121#endif /* XRICK_BTN_UPRIGHT */
122
123#ifdef XRICK_BTN_DOWNLEFT
124 if (!control_test(Control_DOWN | Control_LEFT))
125 {
126 checkKey(currentKey, XRICK_BTN_DOWNLEFT, Control_DOWN | Control_LEFT);
127 }
128#endif /* XRICK_BTN_DOWNLEFT */
129
130#ifdef XRICK_BTN_DOWNRIGHT
131 if (!control_test(Control_DOWN | Control_RIGHT))
132 {
133 checkKey(currentKey, XRICK_BTN_DOWNRIGHT, Control_DOWN | Control_RIGHT);
134 }
135#endif /* XRICK_BTN_DOWNRIGHT */
136
137 previousKey = currentKey;
138}
139
140/*
141 * Wait for an event, then process it and return
142 */
143void sysevt_wait(void)
144{
145#ifdef HAVE_ADJUSTABLE_CPU_FREQ
146 rb->cpu_boost(false);
147#endif
148
149 sysevt_poll(); /* sysevt_poll deals with blocking case as well */
150
151#ifdef HAVE_ADJUSTABLE_CPU_FREQ
152 rb->cpu_boost(true);
153#endif
154}
155
156/* eof */
diff --git a/apps/plugins/xrick/system/sysfile_rockbox.c b/apps/plugins/xrick/system/sysfile_rockbox.c
new file mode 100644
index 0000000000..72227d5301
--- /dev/null
+++ b/apps/plugins/xrick/system/sysfile_rockbox.c
@@ -0,0 +1,122 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/system/system.h"
26
27#include "xrick/config.h"
28#include "xrick/util.h"
29
30#include "plugin.h"
31
32#define XRICK_GAME_DIR ROCKBOX_DIR "/xrick/"
33
34/*
35 * Global variables
36 */
37const char *sysfile_defaultPath = XRICK_GAME_DIR;
38
39/*
40 * Local variables
41 */
42static char *rootPath = NULL;
43
44/*
45 *
46 */
47bool sysfile_setRootPath(const char *name)
48{
49 rootPath = u_strdup(name);
50 return (rootPath != NULL);
51}
52
53/*
54 *
55 */
56void sysfile_clearRootPath()
57{
58 sysmem_pop(rootPath);
59 rootPath = NULL;
60}
61
62/*
63 * Open a data file.
64 */
65file_t sysfile_open(const char *name)
66{
67 int fd;
68
69 size_t fullPathLength = rb->strlen(rootPath) + rb->strlen(name) + 2;
70 char *fullPath = sysmem_push(fullPathLength);
71 if (!fullPath)
72 {
73 return NULL;
74 }
75 rb->snprintf(fullPath, fullPathLength, "%s/%s", rootPath, name);
76 fd = rb->open(fullPath, O_RDONLY);
77 sysmem_pop(fullPath);
78
79 /*
80 * note: I've never seen zero/NULL being used as a file descriptor under Rockbox.
81 * Putting a check here in case this will ever happen (will need a fix).
82 */
83 if (fd == 0)
84 {
85 sys_error("(file) unsupported file descriptor (zero/NULL) being used");
86 }
87 if (fd < 0)
88 {
89 return NULL;
90 }
91
92 return (file_t)fd;
93}
94
95/*
96 * Read a file within a data archive.
97 */
98int sysfile_read(file_t file, void *buf, size_t size, size_t count)
99{
100 int fd = (int)file;
101 return (rb->read(fd, buf, size * count) / size);
102}
103
104/*
105 * Seek.
106 */
107int sysfile_seek(file_t file, long offset, int origin)
108{
109 int fd = (int)file;
110 return rb->lseek(fd, offset, origin);
111}
112
113/*
114 * Close a file within a data archive.
115 */
116void sysfile_close(file_t file)
117{
118 int fd = (int)file;
119 rb->close(fd);
120}
121
122/* eof */
diff --git a/apps/plugins/xrick/system/sysmem_rockbox.c b/apps/plugins/xrick/system/sysmem_rockbox.c
new file mode 100644
index 0000000000..06a683a463
--- /dev/null
+++ b/apps/plugins/xrick/system/sysmem_rockbox.c
@@ -0,0 +1,156 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/system/system.h"
26
27#include "xrick/debug.h"
28
29#include "plugin.h"
30
31/*
32 * Local variables
33 */
34enum
35{
36 ALIGNMENT = sizeof(void*) /* this is more of an educated guess; might want to adjust for your specific architecture */
37};
38static U8 * stackBuffer;
39static U8 * stackTop;
40static size_t stackSize;
41static size_t stackMaxSize;
42static bool isMemoryInitialised = false;
43IFDEBUG_MEMORY( static size_t maxUsedMemory = 0; );
44
45/*
46 * Initialise memory stack
47 */
48bool sysmem_init(void)
49{
50 if (isMemoryInitialised)
51 {
52 return true;
53 }
54
55 if (rb->audio_status())
56 {
57 /* Playback must be stopped the entire time the sound buffer is used.*/
58 rb->audio_stop();
59 }
60
61 stackBuffer = rb->plugin_get_audio_buffer(&stackMaxSize);
62 stackTop = stackBuffer;
63 stackSize = 0;
64 isMemoryInitialised = true;
65 return true;
66}
67
68/*
69 * Cleanup memory stack
70 */
71void sysmem_shutdown(void)
72{
73 if (!isMemoryInitialised)
74 {
75 return;
76 }
77
78 if (stackTop != stackBuffer || stackSize != 0)
79 {
80 sys_error("(memory) improper deallocation detected");
81 }
82
83 IFDEBUG_MEMORY(
84 sys_printf("xrick/memory: max memory usage was %u bytes\n", maxUsedMemory);
85 );
86
87 isMemoryInitialised = false;
88}
89
90/*
91 * Allocate a memory-aligned block on top of the memory stack
92 */
93void *sysmem_push(size_t size)
94{
95 uintptr_t alignedPtr;
96 size_t * allocatedSizePtr;
97
98 size_t neededSize = sizeof(size_t) + size + (ALIGNMENT - 1);
99 if (stackSize + neededSize > stackMaxSize)
100 {
101 sys_error("(memory) tried to allocate a block when memory full");
102 return NULL;
103 }
104
105 alignedPtr = (((uintptr_t)stackTop) + sizeof(size_t) + ALIGNMENT) & ~((uintptr_t)(ALIGNMENT - 1));
106
107 allocatedSizePtr = (size_t *)(alignedPtr);
108 allocatedSizePtr[-1] = neededSize;
109
110 stackTop += neededSize;
111 stackSize += neededSize;
112
113 IFDEBUG_MEMORY(
114 sys_printf("xrick/memory: allocated %u bytes\n", neededSize);
115 if (stackSize > maxUsedMemory) maxUsedMemory = stackSize;
116 );
117
118 return (void *)alignedPtr;
119}
120
121/*
122 * Release block from the top of the memory stack
123 */
124void sysmem_pop(void * alignedPtr)
125{
126 size_t allocatedSize;
127
128 if (!alignedPtr)
129 {
130 return;
131 }
132
133 if (stackSize == 0)
134 {
135 sys_error("(memory) tried to release a block when memory empty");
136 return;
137 }
138
139 allocatedSize = ((size_t *)(alignedPtr))[-1];
140 stackTop -= allocatedSize;
141 stackSize -= allocatedSize;
142
143 IFDEBUG_MEMORY(
144 if ((uintptr_t)alignedPtr != ((((uintptr_t)stackTop) + sizeof(size_t) + ALIGNMENT) & ~((uintptr_t)(ALIGNMENT - 1))))
145 {
146 sys_error("(memory) tried to release a wrong block");
147 return;
148 }
149 );
150
151 IFDEBUG_MEMORY(
152 sys_printf("xrick/memory: released %u bytes\n", allocatedSize);
153 );
154}
155
156/* eof */
diff --git a/apps/plugins/xrick/system/sysmenu_rockbox.c b/apps/plugins/xrick/system/sysmenu_rockbox.c
new file mode 100644
index 0000000000..fb80881749
--- /dev/null
+++ b/apps/plugins/xrick/system/sysmenu_rockbox.c
@@ -0,0 +1,200 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/system/sysmenu_rockbox.h"
26
27#include "xrick/config.h"
28#include "xrick/control.h"
29#include "xrick/draw.h"
30#include "xrick/game.h"
31#include "xrick/system/system.h"
32#include "xrick/system/syssnd_rockbox.h"
33
34#include "plugin.h"
35#ifndef HAVE_LCD_COLOR
36#include "lib/grey.h"
37#endif
38
39#ifdef HAVE_LCD_COLOR
40static fb_data *lcd_fb=NULL;
41#endif
42
43#ifdef ENABLE_CHEATS
44/*
45 * Cheat settings menu
46 */
47static char * sysmenu_cheatItemText(int selected_item, void *data, char *buffer, size_t buffer_len)
48{
49 (void)selected_item;
50 cheat_t cheat = (cheat_t)data;
51 (void)buffer;
52 (void)buffer_len;
53 char * messages[] =
54 {
55 "Disable Unlimited Lives/Ammo Mode",
56 "Enable Unlimited Lives/Ammo Mode",
57 "Disable Never Die Mode",
58 "Enable Never Die Mode",
59 "Disable Expose Mode",
60 "Enable Expose Mode"
61 };
62
63 switch (cheat)
64 {
65 case Cheat_UNLIMITED_ALL:
66 {
67 return game_cheat1? messages[0] : messages[1];
68 }
69 case Cheat_NEVER_DIE:
70 {
71 return game_cheat2? messages[2] : messages[3];
72 }
73 case Cheat_EXPOSE:
74 {
75 return game_cheat3? messages[4] : messages[5];
76 }
77 default: break;
78 }
79 return "";
80}
81
82/*
83 * Callback invoked by cheat menu item
84 */
85static int sysmenu_doToggleCheat(void *param)
86{
87 cheat_t cheat = (cheat_t)param;
88 game_toggleCheat(cheat);
89 return 0;
90}
91
92MENUITEM_FUNCTION_DYNTEXT(sysmenu_unlimitedAllItem, MENU_FUNC_USEPARAM, sysmenu_doToggleCheat,
93 sysmenu_cheatItemText, NULL, (void *)Cheat_UNLIMITED_ALL,
94 NULL, Icon_NOICON);
95
96MENUITEM_FUNCTION_DYNTEXT(sysmenu_neverDieItem, MENU_FUNC_USEPARAM, sysmenu_doToggleCheat,
97 sysmenu_cheatItemText, NULL, (void *)Cheat_NEVER_DIE,
98 NULL, Icon_NOICON);
99
100MENUITEM_FUNCTION_DYNTEXT(sysmenu_exposeItem, MENU_FUNC_USEPARAM, sysmenu_doToggleCheat,
101 sysmenu_cheatItemText, NULL, (void *)Cheat_EXPOSE,
102 NULL, Icon_NOICON);
103
104MAKE_MENU(sysmenu_cheatItems, "Cheat Settings", NULL, Icon_NOICON,
105 &sysmenu_unlimitedAllItem, &sysmenu_neverDieItem, &sysmenu_exposeItem);
106
107#endif /* ENABLE_CHEATS */
108
109/*
110 * Display main menu
111 */
112void sysmenu_exec(void)
113{
114 int result;
115 bool done;
116
117 enum
118 {
119 Menu_RESUME,
120 Menu_RESTART,
121#ifdef ENABLE_CHEATS
122 Menu_CHEAT_SETTINGS,
123#endif
124 Menu_QUIT
125 };
126
127 MENUITEM_STRINGLIST(sysmenu_mainItems, "xrick Menu", NULL,
128 "Resume Game",
129 "Restart Game",
130#ifdef ENABLE_CHEATS
131 "Cheat Settings",
132#endif
133 ID2P(LANG_MENU_QUIT));
134
135#ifdef ENABLE_SOUND
136 syssnd_pauseAll(true);
137#endif
138
139#ifndef HAVE_LCD_COLOR
140 grey_show(false);
141#endif
142
143 done = false;
144 do
145 {
146 rb->button_clear_queue();
147
148 result = rb->do_menu(&sysmenu_mainItems, NULL, NULL, false);
149 switch(result)
150 {
151 case Menu_RESUME:
152 {
153 done = true;
154 break;
155 }
156 case Menu_RESTART:
157 {
158 control_set(Control_END);
159 done = true;
160 break;
161 }
162#ifdef ENABLE_CHEATS
163 case Menu_CHEAT_SETTINGS:
164 {
165 rb->do_menu(&sysmenu_cheatItems, NULL, NULL, false);
166 break;
167 }
168#endif
169 case Menu_QUIT:
170 {
171 control_set(Control_EXIT);
172 done = true;
173 break;
174 }
175 default: break;
176 }
177 } while (!done);
178
179#ifdef HAVE_LCD_COLOR
180 if (!(control_test(Control_EXIT)))
181 {
182 if(!lcd_fb)
183 {
184 struct viewport *vp_main = rb->lcd_set_viewport(NULL);
185 lcd_fb = vp_main->buffer->fb_ptr;
186 }
187 rb->memset(lcd_fb, 0, sizeof(fb_data) * LCD_WIDTH * LCD_HEIGHT);
188 sysvid_update(&draw_SCREENRECT);
189 rb->lcd_update();
190 }
191#else
192 grey_show(true);
193#endif
194
195#ifdef ENABLE_SOUND
196 syssnd_pauseAll(false);
197#endif
198}
199
200/* eof */
diff --git a/apps/plugins/xrick/system/sysmenu_rockbox.h b/apps/plugins/xrick/system/sysmenu_rockbox.h
new file mode 100644
index 0000000000..fcd13606fa
--- /dev/null
+++ b/apps/plugins/xrick/system/sysmenu_rockbox.h
@@ -0,0 +1,32 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#ifndef _SYSMENU_ROCKBOX_H
26#define _SYSMENU_ROCKBOX_H
27
28extern void sysmenu_exec(void);
29
30#endif /* ndef _SYSMENU_ROCKBOX_H */
31
32/* eof */
diff --git a/apps/plugins/xrick/system/syssnd_rockbox.c b/apps/plugins/xrick/system/syssnd_rockbox.c
new file mode 100644
index 0000000000..97ed5474f1
--- /dev/null
+++ b/apps/plugins/xrick/system/syssnd_rockbox.c
@@ -0,0 +1,483 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/config.h"
26
27#ifdef ENABLE_SOUND
28
29#include "xrick/system/system.h"
30
31#include "xrick/game.h"
32#include "xrick/debug.h"
33#include "xrick/system/syssnd_rockbox.h"
34
35#include "plugin.h"
36
37/*
38 * Global variables
39 */
40const U8 syssnd_period = 20;
41
42/*
43 * Local variables
44 */
45enum
46{
47 SYSSND_MIX_CHANNELS = 5,
48 SYSSND_MIX_SAMPLES = 1024, /* try changing this value if sound mixing is too slow or choppy */
49 SYSSND_SOURCE_SAMPLES = SYSSND_MIX_SAMPLES / 2
50};
51
52/* channels to be mixed */
53static channel_t channels[SYSSND_MIX_CHANNELS];
54/* buffer used to mix sounds sent to pcm playback, stores 16b stereo 44Khz audio samples */
55enum { AUDIO_BUFFER_COUNT = 4 };
56typedef struct
57{
58 U32 data[SYSSND_MIX_SAMPLES];
59 size_t length; /* in 8 bit mono samples */
60} mix_buffer_t;
61static mix_buffer_t mixBuffers[AUDIO_BUFFER_COUNT];
62static size_t writeIndex;
63static size_t readIndex;
64static size_t fillCount;
65static bool isAudioPlaying;
66static bool isAudioInitialised = false;
67
68/*
69 * Prototypes
70 */
71static void endChannel(size_t c);
72static void get_more(const void **start, size_t *size);
73
74/*
75 * Deactivate channel
76 */
77static void endChannel(size_t c)
78{
79 channels[c].loop = 0;
80 channels[c].sound = NULL;
81}
82
83/*
84 * Audio callback
85 */
86static void get_more(const void **start, size_t *size)
87{
88 if (fillCount > 0)
89 {
90 /* Store output data address and size. */
91 *start = mixBuffers[readIndex].data;
92 *size = mixBuffers[readIndex].length * 8;
93
94 /* Free this part of output buffer. */
95 mixBuffers[readIndex].length = 0;
96
97 /* Advance to the next part of output buffer. */
98 readIndex = (readIndex + 1) & (AUDIO_BUFFER_COUNT - 1);
99 fillCount--;
100 }
101 else
102 {
103 /* Nothing to play. */
104 isAudioPlaying = false;
105 }
106}
107
108/*
109 * Mix audio samples and fill playback buffer
110 */
111void syssnd_update(void)
112{
113 if (!isAudioInitialised)
114 {
115 return;
116 }
117
118 for (;;)
119 {
120 size_t c;
121 size_t sampleOffset;
122 size_t maxSampleCount;
123 bool isFirstSound;
124 U8 *sourceBuf, *sourceBufEnd;
125 U32 *destBuf;
126
127 /* Cancel if whole buffer filled. */
128 if (fillCount >= (AUDIO_BUFFER_COUNT - 1))
129 {
130 return;
131 }
132
133 maxSampleCount = 0;
134
135 sampleOffset = mixBuffers[writeIndex].length;
136 destBuf = mixBuffers[writeIndex].data + sampleOffset * 2;
137
138 isFirstSound = true;
139 for (c = 0; c < SYSSND_MIX_CHANNELS ; ++c)
140 {
141 U32 * mixBuffer;
142 size_t sampleCount;
143 channel_t * channel = &channels[c];
144
145 if (!channel->sound /* no sound to play on this channel */
146 || (channel->loop == 0)) /* channel is inactive */
147 {
148 continue;
149 }
150
151 if (isFirstSound)
152 {
153 /* clear mixing buffer */
154 rb->memset(destBuf, 0, (SYSSND_MIX_SAMPLES - (sampleOffset * 2)) * sizeof(U32));
155 isFirstSound = false;
156 }
157
158 sampleCount = MIN(SYSSND_SOURCE_SAMPLES - sampleOffset, channel->len);
159 if (maxSampleCount < sampleCount)
160 {
161 maxSampleCount = sampleCount;
162 }
163
164 /* mix sound samples */
165 mixBuffer = destBuf;
166 sourceBuf = channel->buf;
167 sourceBufEnd = channel->buf + sampleCount;
168 while (sourceBuf < sourceBufEnd)
169 {
170 /* Convert from unsigned 8 bit mono 22khz to signed 16 bit stereo 44khz */
171 const int sourceSample = *sourceBuf++;
172 int monoSample = (sourceSample - 0x80) << 8;
173 U32 stereoSample = *mixBuffer;
174 monoSample += (S32)(stereoSample) >> 16;
175 if (monoSample >= 0x8000)
176 {
177 monoSample = 0x7FFF;
178 }
179 else if (monoSample < -0x8000)
180 {
181 monoSample = -0x8000;
182 }
183 stereoSample = (U16)monoSample | ((U16)monoSample << 16);
184 *mixBuffer++ = stereoSample;
185 *mixBuffer++ = stereoSample;
186 }
187 channel->buf = sourceBufEnd;
188
189 channel->len -= sampleCount;
190 if (channel->len == 0) /* ending ? */
191 {
192 if (channel->loop > 0)
193 {
194 channel->loop--;
195 }
196 if (channel->loop)
197 {
198 /* just loop */
199 IFDEBUG_AUDIO2(sys_printf("xrick/audio: channel %d - loop\n", c););
200 channel->buf = channel->sound->buf;
201 channel->len = channel->sound->len;
202 }
203 else
204 {
205 /* end for real */
206 IFDEBUG_AUDIO2(sys_printf("xrick/audio: channel %d - end\n", c););
207 endChannel(c);
208 }
209 }
210 }
211
212 if (maxSampleCount == 0)
213 {
214 return;
215 }
216
217 mixBuffers[writeIndex].length += maxSampleCount;
218
219 /* Advance one part of audio buffer. */
220 writeIndex = (writeIndex + 1) & (AUDIO_BUFFER_COUNT - 1);
221 fillCount++;
222
223 if (!isAudioPlaying && fillCount > 0)
224 {
225 rb->pcm_play_data(&get_more, NULL, NULL, 0);
226 isAudioPlaying = true;
227 }
228 }
229}
230
231/*
232 * Initialise audio
233 */
234bool syssnd_init(void)
235{
236 if (isAudioInitialised)
237 {
238 return true;
239 }
240
241 IFDEBUG_AUDIO(sys_printf("xrick/audio: start\n"););
242
243 rb->talk_disable(true);
244
245 /* Stop playback to reconfigure audio settings and acquire audio buffer */
246 rb->mixer_channel_stop(PCM_MIXER_CHAN_PLAYBACK);
247
248#if INPUT_SRC_CAPS != 0
249 /* Select playback */
250 rb->audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
251 rb->audio_set_output_source(AUDIO_SRC_PLAYBACK);
252#endif
253
254 rb->pcm_set_frequency(HW_FREQ_44);
255 rb->pcm_apply_settings();
256
257 rb->memset(channels, 0, sizeof(channels));
258 rb->memset(mixBuffers, 0, sizeof(mixBuffers));
259
260 writeIndex = 0;
261 readIndex = 0;
262 fillCount = 0;
263 isAudioPlaying = false;
264
265 isAudioInitialised = true;
266 IFDEBUG_AUDIO(sys_printf("xrick/audio: ready\n"););
267 return true;
268}
269
270/*
271 * Shutdown
272 */
273void syssnd_shutdown(void)
274{
275 if (!isAudioInitialised)
276 {
277 return;
278 }
279
280 /* Stop playback. */
281 rb->pcm_play_stop();
282
283 /* Reset playing status. */
284 isAudioPlaying = false;
285
286 /* Restore default sampling rate. */
287 rb->pcm_set_frequency(HW_SAMPR_DEFAULT);
288 rb->pcm_apply_settings();
289
290 rb->talk_disable(false);
291
292 isAudioInitialised = false;
293 IFDEBUG_AUDIO(sys_printf("xrick/audio: stop\n"););
294}
295
296/*
297 * Play a sound
298 *
299 * loop: number of times the sound should be played, -1 to loop forever
300 *
301 * NOTE if sound is already playing, simply reset it (i.e. can not have
302 * twice the same sound playing -- tends to become noisy when too many
303 * bad guys die at the same time).
304 */
305void syssnd_play(sound_t *sound, S8 loop)
306{
307 size_t c;
308
309 if (!isAudioInitialised || !sound)
310 {
311 return;
312 }
313
314 c = 0;
315 while (channels[c].sound != sound &&
316 channels[c].loop != 0 &&
317 c < SYSSND_MIX_CHANNELS)
318 {
319 c++;
320 }
321 if (c >= SYSSND_MIX_CHANNELS)
322 {
323 return;
324 }
325
326 if (!sound->buf)
327 {
328 syssnd_load(sound);
329 if (!sound->buf)
330 {
331 sys_error("(audio) can not load %s", sound->name);
332 return;
333 }
334 }
335
336 IFDEBUG_AUDIO(
337 if (channels[c].sound == sound)
338 {
339 sys_printf("xrick/audio: already playing %s on channel %d - resetting\n",
340 sound->name, c);
341 }
342 else
343 {
344 sys_printf("xrick/audio: playing %s on channel %d\n", sound->name, c);
345 }
346 );
347
348 channels[c].loop = loop;
349 channels[c].sound = sound;
350 channels[c].buf = sound->buf;
351 channels[c].len = sound->len;
352}
353
354/*
355 * Pause all sounds
356 */
357void syssnd_pauseAll(bool pause)
358{
359 if (!isAudioInitialised)
360 {
361 return;
362 }
363
364 rb->pcm_play_lock();
365 rb->mixer_channel_play_pause(PCM_MIXER_CHAN_PLAYBACK, !pause);
366 rb->pcm_play_unlock();
367}
368
369/*
370 * Stop a sound
371 */
372void syssnd_stop(sound_t *sound)
373{
374 size_t c;
375
376 if (!isAudioInitialised || !sound)
377 {
378 return;
379 }
380
381 for (c = 0; c < SYSSND_MIX_CHANNELS; c++)
382 {
383 if (channels[c].sound == sound)
384 {
385 endChannel(c);
386 }
387 }
388}
389
390/*
391 * Stops all channels.
392 */
393void syssnd_stopAll(void)
394{
395 size_t c;
396
397 if (!isAudioInitialised)
398 {
399 return;
400 }
401
402 for (c = 0; c < SYSSND_MIX_CHANNELS; c++)
403 {
404 if (channels[c].sound)
405 {
406 endChannel(c);
407 }
408 }
409}
410
411/*
412 * Load a sound.
413 */
414void syssnd_load(sound_t *sound)
415{
416 int bytesRead;
417 file_t fp;
418 bool success;
419
420 if (!isAudioInitialised || !sound)
421 {
422 return;
423 }
424
425 success = false;
426 do
427 {
428 sound->buf = sysmem_push(sound->len);
429 if (!sound->buf)
430 {
431 sys_error("(audio) not enough memory for \"%s\", %d bytes needed", sound->name, sound->len);
432 break;
433 }
434
435 fp = sysfile_open(sound->name);
436 if (!fp)
437 {
438 sys_error("(audio) unable to open \"%s\"", sound->name);
439 break;
440 }
441
442 sysfile_seek(fp, sizeof(wave_header_t), SEEK_SET); /* skip WAVE header */
443
444 bytesRead = sysfile_read(fp, sound->buf, sound->len, 1);
445 sysfile_close(fp);
446 if (bytesRead != 1)
447 {
448 sys_error("(audio) unable to read from \"%s\"", sound->name);
449 break;
450 }
451
452 success = true;
453 } while (false);
454
455 if (!success)
456 {
457 sysmem_pop(sound->buf);
458 sound->buf = NULL;
459 sound->len = 0;
460 return;
461 }
462
463 IFDEBUG_AUDIO(sys_printf("xrick/audio: successfully loaded \"%s\"\n", sound->name););
464}
465
466/*
467 * Unload a sound.
468 */
469void syssnd_unload(sound_t *sound)
470{
471 if (!isAudioInitialised || !sound || !sound->buf)
472 {
473 return;
474 }
475
476 sysmem_pop(sound->buf);
477 sound->buf = NULL;
478 sound->len = 0;
479}
480
481#endif /* ENABLE_SOUND */
482
483/* eof */
diff --git a/apps/plugins/xrick/system/syssnd_rockbox.h b/apps/plugins/xrick/system/syssnd_rockbox.h
new file mode 100644
index 0000000000..41bd7d8454
--- /dev/null
+++ b/apps/plugins/xrick/system/syssnd_rockbox.h
@@ -0,0 +1,48 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#ifndef _SYSSND_ROCKBOX_H
26#define _SYSSND_ROCKBOX_H
27
28#include "xrick/config.h"
29
30#ifdef ENABLE_SOUND
31
32#include "xrick/system/system.h"
33
34typedef struct {
35 sound_t *sound;
36 U8 *buf;
37 U32 len;
38 S8 loop;
39} channel_t;
40
41extern void syssnd_load(sound_t *);
42extern void syssnd_unload(sound_t *);
43
44#endif /* ENABLE_SOUND */
45
46#endif /* ndef _SYSSND_ROCKBOX_H */
47
48/* eof */
diff --git a/apps/plugins/xrick/system/system.h b/apps/plugins/xrick/system/system.h
new file mode 100644
index 0000000000..d4dda3d5d4
--- /dev/null
+++ b/apps/plugins/xrick/system/system.h
@@ -0,0 +1,178 @@
1/*
2 * xrick/system/system.h
3 *
4 * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net).
5 * Copyright (C) 2008-2014 Pierluigi Vicinanza.
6 * All rights reserved.
7 *
8 * The use and distribution terms for this software are contained in the file
9 * named README, which can be found in the root of this distribution. By
10 * using this software in any fashion, you are agreeing to be bound by the
11 * terms of this license.
12 *
13 * You must not remove this notice, or any other, from this software.
14 */
15
16#ifndef _SYSTEM_H
17#define _SYSTEM_H
18
19/*
20 * Detect GCC
21 */
22#ifdef __GNUC__
23/*
24 * make POSIX functions available
25 */
26# ifndef _POSIX_SOURCE
27# define _POSIX_SOURCE
28# endif
29#endif
30
31/*
32 * Detect Microsoft Visual C
33 */
34#ifdef _MSC_VER
35/*
36 * FIXME disable "integral size mismatch in argument; conversion supplied" warning
37 * as long as the code has not been cleared -- there are so many of them...
38 */
39#pragma warning( disable : 4761 )
40#endif
41
42/*
43 * Detect Microsoft Windows
44 */
45#if !defined( __WIN32__ ) && ( defined( WIN32 ) || defined( _WIN32 ) )
46#define __WIN32__
47#endif
48
49#include "xrick/config.h"
50#include "xrick/rects.h"
51#include "xrick/data/img.h"
52#ifdef ENABLE_SOUND
53#include "xrick/data/sounds.h"
54#endif
55
56#include <stddef.h> /* size_t */
57#include <sys/types.h> /* off_t */
58
59/*
60 * main section
61 */
62extern bool sys_init(int, char **);
63extern void sys_shutdown(void);
64extern void sys_error(const char *, ...);
65extern void sys_printf(const char *, ...);
66extern void sys_snprintf(char *, size_t, const char *, ...);
67extern size_t sys_strlen(const char *);
68extern U32 sys_gettime(void);
69extern void sys_yield(void);
70extern bool sys_cacheData(void);
71extern void sys_uncacheData(void);
72
73/*
74 * memory section
75 */
76extern bool sysmem_init(void);
77extern void sysmem_shutdown(void);
78extern void *sysmem_push(size_t);
79extern void sysmem_pop(void *);
80
81/*
82 * video section
83 */
84#define SYSVID_ZOOM 2
85#define SYSVID_MAXZOOM 4
86#define SYSVID_WIDTH 320
87#define SYSVID_HEIGHT 200
88
89extern U8 *sysvid_fb; /* frame buffer */
90
91extern bool sysvid_init(void);
92extern void sysvid_shutdown(void);
93extern void sysvid_update(const rect_t *);
94extern void sysvid_clear(void);
95extern void sysvid_zoom(S8);
96extern void sysvid_toggleFullscreen(void);
97extern void sysvid_setGamePalette(void);
98extern void sysvid_setPalette(img_color_t *, U16);
99
100/*
101 * file management section
102 */
103typedef void *file_t;
104
105extern const char *sysfile_defaultPath;
106
107extern bool sysfile_setRootPath(const char *);
108extern void sysfile_clearRootPath(void);
109
110extern file_t sysfile_open(const char *);
111extern int sysfile_seek(file_t file, long offset, int origin);
112extern int sysfile_tell(file_t);
113extern off_t sysfile_size(file_t);
114extern int sysfile_read(file_t, void *, size_t, size_t);
115extern void sysfile_close(file_t);
116
117/*
118 * events section
119 */
120extern void sysevt_poll(void);
121extern void sysevt_wait(void);
122
123/*
124 * keyboard section
125 */
126extern U8 syskbd_up;
127extern U8 syskbd_down;
128extern U8 syskbd_left;
129extern U8 syskbd_right;
130extern U8 syskbd_pause;
131extern U8 syskbd_end;
132extern U8 syskbd_xtra;
133extern U8 syskbd_fire;
134
135/*
136 * sound section
137 */
138#ifdef ENABLE_SOUND
139extern const U8 syssnd_period; /* time between each sound update, in millisecond */
140
141extern bool syssnd_init(void);
142extern void syssnd_shutdown(void);
143extern void syssnd_update(void);
144extern void syssnd_vol(S8);
145extern void syssnd_toggleMute(void);
146extern void syssnd_play(sound_t *, S8);
147extern void syssnd_pauseAll(bool);
148extern void syssnd_stop(sound_t *);
149extern void syssnd_stopAll(void);
150#endif /* ENABLE_ SOUND */
151
152/*
153 * args section
154 */
155extern int sysarg_args_period;
156extern int sysarg_args_map;
157extern int sysarg_args_submap;
158extern int sysarg_args_fullscreen;
159extern int sysarg_args_zoom;
160#ifdef ENABLE_SOUND
161extern bool sysarg_args_nosound;
162extern int sysarg_args_vol;
163#endif /* ENABLE_ SOUND */
164extern const char *sysarg_args_data;
165
166extern bool sysarg_init(int, char **);
167
168/*
169 * joystick section
170 */
171#ifdef ENABLE_JOYSTICK
172extern bool sysjoy_init(void);
173extern void sysjoy_shutdown(void);
174#endif
175
176#endif /* ndef _SYSTEM_H */
177
178/* eof */
diff --git a/apps/plugins/xrick/system/system_rockbox.c b/apps/plugins/xrick/system/system_rockbox.c
new file mode 100644
index 0000000000..3b5f96a4ed
--- /dev/null
+++ b/apps/plugins/xrick/system/system_rockbox.c
@@ -0,0 +1,262 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/system/system.h"
26
27#include "xrick/config.h"
28#ifdef ENABLE_SOUND
29#include "xrick/system/syssnd_rockbox.h"
30#endif
31
32#include "plugin.h"
33
34enum { LINE_LENGTH = 80 };
35
36/*
37* Error
38*/
39void sys_error(const char *err, ...)
40{
41 va_list argptr;
42 char s[LINE_LENGTH];
43
44 /* prepare message */
45 va_start(argptr, err);
46 rb->vsnprintf(s, sizeof(s), err, argptr);
47 va_end(argptr);
48
49 /* print error message */
50 rb->splashf(HZ*3, ID2P(LANG_ERROR_FORMATSTR), s);
51 DEBUGF("Error: %s\n", s);
52}
53
54/*
55* Print a message to standard output
56*/
57void sys_printf(const char *msg, ...)
58{
59 va_list argptr;
60 char s[LINE_LENGTH];
61
62 /* prepare message */
63 va_start(argptr, msg);
64 rb->vsnprintf(s, sizeof(s), msg, argptr);
65 va_end(argptr);
66
67 /* print message */
68 DEBUGF("%s",s);
69
70#ifdef ENABLE_SYSPRINTF_TO_SCREEN
71 {
72 static int currentYPos = 0;
73 size_t i;
74
75 /* Device LCDs display newlines funny. */
76 for(i = 0; s[i] != '\0'; ++i)
77 {
78 if(s[i] == '\n')
79 {
80 s[i] = ' ';
81 }
82 }
83
84 rb->lcd_putsxy(1, currentYPos, (unsigned char *)s);
85 rb->lcd_update();
86
87 currentYPos += 12;
88 if(currentYPos > LCD_HEIGHT-12)
89 {
90 currentYPos = 0;
91 rb->lcd_clear_display();
92 }
93 }
94#endif /* ENABLE_SYSPRINTF_TO_SCREEN */
95}
96
97/*
98* Print a message to string buffer
99*/
100void sys_snprintf(char *buf, size_t size, const char *msg, ...)
101{
102 va_list argptr;
103
104 va_start(argptr, msg);
105 rb->vsnprintf(buf, size, msg, argptr);
106 va_end(argptr);
107}
108
109/*
110* Returns string length
111*/
112size_t sys_strlen(const char * str)
113{
114 return rb->strlen(str);
115}
116
117/*
118* Return number of milliseconds elapsed since first call
119*/
120U32 sys_gettime(void)
121{
122 long ticks = *(rb->current_tick);
123 return (U32)((ticks * 1000) / HZ);
124}
125
126/*
127* Yield execution to another thread
128*/
129void sys_yield(void)
130{
131 rb->yield();
132}
133
134/*
135* Initialize system
136*/
137bool sys_init(int argc, char **argv)
138{
139#ifdef HAVE_ADJUSTABLE_CPU_FREQ
140 rb->cpu_boost(true);
141#endif
142
143 if (!sysarg_init(argc, argv))
144 {
145 return false;
146 }
147 if (!sysmem_init())
148 {
149 return false;
150 }
151 if (!sysvid_init())
152 {
153 return false;
154 }
155#ifdef ENABLE_SOUND
156 if (!sysarg_args_nosound && !syssnd_init())
157 {
158 return false;
159 }
160#endif
161 if (!sysfile_setRootPath(sysarg_args_data? sysarg_args_data : sysfile_defaultPath))
162 {
163 return false;
164 }
165 return true;
166}
167
168/*
169* Shutdown system
170*/
171void sys_shutdown(void)
172{
173 sysfile_clearRootPath();
174#ifdef ENABLE_SOUND
175 syssnd_shutdown();
176#endif
177 sysvid_shutdown();
178 sysmem_shutdown();
179
180#ifdef HAVE_ADJUSTABLE_CPU_FREQ
181 rb->cpu_boost(false);
182#endif
183}
184
185/*
186* Preload data before entering main loop
187*/
188bool sys_cacheData(void)
189{
190#ifdef ENABLE_SOUND
191 syssnd_load(soundGameover);
192 syssnd_load(soundSbonus2);
193 syssnd_load(soundBullet);
194 syssnd_load(soundBombshht);
195 syssnd_load(soundExplode);
196 syssnd_load(soundStick);
197 syssnd_load(soundWalk);
198 syssnd_load(soundCrawl);
199 syssnd_load(soundJump);
200 syssnd_load(soundPad);
201 syssnd_load(soundBox);
202 syssnd_load(soundBonus);
203 syssnd_load(soundSbonus1);
204 syssnd_load(soundDie);
205 syssnd_load(soundEntity[0]);
206 syssnd_load(soundEntity[1]);
207 syssnd_load(soundEntity[2]);
208 syssnd_load(soundEntity[3]);
209 syssnd_load(soundEntity[4]);
210 syssnd_load(soundEntity[5]);
211 syssnd_load(soundEntity[6]);
212 syssnd_load(soundEntity[7]);
213 syssnd_load(soundEntity[8]);
214 syssnd_load(soundTune0);
215 syssnd_load(soundTune1);
216 syssnd_load(soundTune2);
217 syssnd_load(soundTune3);
218 syssnd_load(soundTune4);
219 syssnd_load(soundTune5);
220#endif /* ENABLE_SOUND */
221 return true;
222}
223
224/*
225* Clear preloaded data before shutdown
226*/
227void sys_uncacheData(void)
228{
229#ifdef ENABLE_SOUND
230 syssnd_unload(soundTune5);
231 syssnd_unload(soundTune4);
232 syssnd_unload(soundTune3);
233 syssnd_unload(soundTune2);
234 syssnd_unload(soundTune1);
235 syssnd_unload(soundTune0);
236 syssnd_unload(soundEntity[8]);
237 syssnd_unload(soundEntity[7]);
238 syssnd_unload(soundEntity[6]);
239 syssnd_unload(soundEntity[5]);
240 syssnd_unload(soundEntity[4]);
241 syssnd_unload(soundEntity[3]);
242 syssnd_unload(soundEntity[2]);
243 syssnd_unload(soundEntity[1]);
244 syssnd_unload(soundEntity[0]);
245 syssnd_unload(soundDie);
246 syssnd_unload(soundSbonus1);
247 syssnd_unload(soundBonus);
248 syssnd_unload(soundBox);
249 syssnd_unload(soundPad);
250 syssnd_unload(soundJump);
251 syssnd_unload(soundCrawl);
252 syssnd_unload(soundWalk);
253 syssnd_unload(soundStick);
254 syssnd_unload(soundExplode);
255 syssnd_unload(soundBombshht);
256 syssnd_unload(soundBullet);
257 syssnd_unload(soundSbonus2);
258 syssnd_unload(soundGameover);
259#endif /* ENABLE_SOUND */
260}
261
262/* eof */
diff --git a/apps/plugins/xrick/system/sysvid_rockbox.c b/apps/plugins/xrick/system/sysvid_rockbox.c
new file mode 100644
index 0000000000..236bc87616
--- /dev/null
+++ b/apps/plugins/xrick/system/sysvid_rockbox.c
@@ -0,0 +1,402 @@
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Port of xrick, a Rick Dangerous clone, to Rockbox.
11 * See http://www.bigorno.net/xrick/
12 *
13 * Copyright (C) 2008-2014 Pierluigi Vicinanza
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include "xrick/system/system.h"
26
27#include "xrick/config.h"
28#include "xrick/draw.h"
29#include "xrick/game.h"
30#include "xrick/data/img.h"
31#include "xrick/debug.h"
32
33#include "plugin.h"
34#include "lib/helper.h"
35
36/*
37 * Global variables
38 */
39U8 *sysvid_fb = NULL; /* xRick generic 320x200 8bpp frame buffer */
40
41/*
42 * Local variables
43 */
44static fb_data palette[256] IBSS_ATTR;
45static bool isVideoInitialised = false;
46#ifndef HAVE_LCD_COLOR
47# include "lib/grey.h"
48GREY_INFO_STRUCT_IRAM
49static unsigned char greybuffer[LCD_HEIGHT * LCD_WIDTH] IBSS_ATTR; /* off screen buffer */
50static unsigned char *gbuf;
51# if LCD_PIXELFORMAT == HORIZONTAL_PACKING
52enum { GREYBUFSIZE = (((LCD_WIDTH+7)/8)*LCD_HEIGHT*16+200) };
53# else
54enum { GREYBUFSIZE = (LCD_WIDTH*((LCD_HEIGHT+7)/8)*16+200) };
55# endif
56#endif /* ndef HAVE_LCD_COLOR */
57
58static fb_data *lcd_fb = NULL;
59
60#if (LCD_HEIGHT < SYSVID_HEIGHT)
61enum { ROW_RESIZE_STEP = (LCD_HEIGHT << 16) / SYSVID_HEIGHT };
62
63static bool rowsToSkip[SYSVID_HEIGHT];
64
65/*
66 *
67 */
68static void calculateRowsToSkip(void)
69{
70 U32 currentRow, prevResizedRow;
71
72 prevResizedRow = 0;
73 rowsToSkip[0] = false;
74
75 for (currentRow = 1; currentRow < SYSVID_HEIGHT; ++currentRow)
76 {
77 U32 resizedRow = (currentRow * ROW_RESIZE_STEP) >> 16;
78 if (resizedRow == prevResizedRow)
79 {
80 rowsToSkip[currentRow] = true;
81 }
82 prevResizedRow = resizedRow;
83 }
84}
85#endif /* (LCD_HEIGHT < SYSVID_HEIGHT) */
86
87#if (LCD_WIDTH < SYSVID_WIDTH)
88enum { COLUMN_RESIZE_STEP = (LCD_WIDTH << 16) / (SYSVID_WIDTH + (DRAW_XYMAP_SCRLEFT*2)) };
89
90static bool columnsToSkip[SYSVID_WIDTH + (DRAW_XYMAP_SCRLEFT*2)];
91
92/*
93 *
94 */
95static void calculateColumnsToSkip(void)
96{
97 U32 currentColumn, prevResizedColumn;
98
99 prevResizedColumn = 0;
100 columnsToSkip[0] = false;
101
102 for (currentColumn = 1; currentColumn < (SYSVID_WIDTH + (DRAW_XYMAP_SCRLEFT*2)); ++currentColumn)
103 {
104 U32 resizedColumn = (currentColumn * COLUMN_RESIZE_STEP) >> 16;
105 if (resizedColumn == prevResizedColumn)
106 {
107 columnsToSkip[currentColumn] = true;
108 }
109 prevResizedColumn = resizedColumn;
110 }
111}
112#endif /* (LCD_WIDTH < SYSVID_WIDTH) */
113
114/*
115 *
116 */
117void sysvid_setPalette(img_color_t *pal, U16 n)
118{
119 U16 i;
120
121 for (i = 0; i < n; i++)
122 {
123#ifdef HAVE_LCD_COLOR
124 palette[i] = LCD_RGBPACK(pal[i].r, pal[i].g, pal[i].b);
125#else
126 palette[i] = ((3 * pal[i].r) + (6 * pal[i].g) + pal[i].b) / 10;
127#endif
128 }
129}
130
131/*
132 *
133 */
134void sysvid_setGamePalette()
135{
136 sysvid_setPalette(game_colors, game_color_count);
137}
138
139/*
140 * Update screen
141 */
142void sysvid_update(const rect_t *rects)
143{
144 unsigned sourceRow, sourceLastRow;
145 unsigned sourceColumn, sourceLastColumn;
146 unsigned resizedRow, resizedColumn;
147 unsigned resizedWidth, resizedHeight;
148 unsigned x, y;
149 U8 *sourceBuf, *sourceTemp;
150 fb_data *destBuf, *destTemp;
151
152 if (!rects)
153 {
154 return;
155 }
156
157 while (rects)
158 {
159 sourceRow = rects->y;
160 sourceLastRow = sourceRow + rects->height;
161 sourceColumn = rects->x;
162 sourceLastColumn = sourceColumn + rects->width;
163
164#if (LCD_WIDTH < SYSVID_WIDTH)
165 /* skip black borders */
166 if (sourceColumn < -DRAW_XYMAP_SCRLEFT)
167 {
168 sourceColumn = -DRAW_XYMAP_SCRLEFT;
169 }
170 if (sourceLastColumn > (SYSVID_WIDTH + DRAW_XYMAP_SCRLEFT))
171 {
172 sourceLastColumn = SYSVID_WIDTH + DRAW_XYMAP_SCRLEFT;
173 }
174
175 /* skip unwanted columns */
176 while (columnsToSkip[sourceColumn + DRAW_XYMAP_SCRLEFT] /* && sourceColumn < (SYSVID_WIDTH + DRAW_XYMAP_SCRLEFT) */)
177 {
178 ++sourceColumn;
179 }
180
181 resizedColumn = ((sourceColumn + DRAW_XYMAP_SCRLEFT) * COLUMN_RESIZE_STEP) >> 16;
182 resizedWidth = 0;
183#else
184 resizedColumn = sourceColumn;
185 resizedWidth = rects->width;
186#endif /* (LCD_WIDTH < SYSVID_WIDTH) */
187
188#if (LCD_HEIGHT < SYSVID_HEIGHT)
189 /* skip unwanted rows */
190 while (rowsToSkip[sourceRow] /* && sourceRow < SYSVID_HEIGHT */)
191 {
192 ++sourceRow;
193 }
194
195 resizedRow = (sourceRow * ROW_RESIZE_STEP) >> 16;
196 resizedHeight = 0;
197#else
198 resizedRow = sourceRow;
199 resizedHeight = rects->height;
200#endif /* (LCD_HEIGHT < SYSVID_HEIGHT) */
201
202 sourceBuf = sysvid_fb;
203 sourceBuf += sourceColumn + sourceRow * SYSVID_WIDTH;
204
205#ifdef HAVE_LCD_COLOR
206 if(!lcd_fb)
207 {
208 struct viewport *vp_main = rb->lcd_set_viewport(NULL);
209 lcd_fb = vp_main->buffer->fb_ptr;
210 }
211 destBuf = lcd_fb;
212#else
213 destBuf = greybuffer;
214#endif /* HAVE_LCD_COLOR */
215 destBuf += resizedColumn + resizedRow * LCD_WIDTH;
216
217#if (LCD_WIDTH < SYSVID_WIDTH)
218 sourceColumn += DRAW_XYMAP_SCRLEFT;
219 sourceLastColumn += DRAW_XYMAP_SCRLEFT;
220#endif /* (LCD_WIDTH < SYSVID_WIDTH) */
221
222 for (y = sourceRow; y < sourceLastRow; ++y)
223 {
224#if (LCD_HEIGHT < SYSVID_HEIGHT)
225 if (rowsToSkip[y])
226 {
227 sourceBuf += SYSVID_WIDTH;
228 continue;
229 }
230
231 ++resizedHeight;
232#endif /* (LCD_HEIGHT < SYSVID_HEIGHT) */
233
234 sourceTemp = sourceBuf;
235 destTemp = destBuf;
236 for (x = sourceColumn; x < sourceLastColumn; ++x)
237 {
238#if (LCD_WIDTH < SYSVID_WIDTH)
239 if (columnsToSkip[x])
240 {
241 ++sourceTemp;
242 continue;
243 }
244
245 if (y == sourceRow)
246 {
247 ++resizedWidth;
248 }
249#endif /* (LCD_WIDTH < SYSVID_WIDTH) */
250
251 *destTemp = palette[*sourceTemp];
252
253 ++sourceTemp;
254 ++destTemp;
255 }
256
257 sourceBuf += SYSVID_WIDTH;
258 destBuf += LCD_WIDTH;
259 }
260
261#ifdef HAVE_LCD_COLOR
262 IFDEBUG_VIDEO2(
263 for (y = resizedRow; y < resizedRow + resizedHeight; ++y)
264 {
265 destBuf = lcd_fb + resizedColumn + y * LCD_WIDTH;
266 *destBuf = palette[0x01];
267 *(destBuf + resizedWidth - 1) = palette[0x01];
268 }
269
270 for (x = resizedColumn; x < resizedColumn + resizedWidth; ++x)
271 {
272 destBuf = rb->lcd_fb + x + resizedRow * LCD_WIDTH;
273 *destBuf = palette[0x01];
274 *(destBuf + (resizedHeight - 1) * LCD_WIDTH) = palette[0x01];
275 }
276 );
277
278 rb->lcd_update_rect(resizedColumn, resizedRow, resizedWidth, resizedHeight);
279#else
280 grey_ub_gray_bitmap_part(greybuffer, resizedColumn, resizedRow, LCD_WIDTH, resizedColumn, resizedRow, resizedWidth, resizedHeight);
281#endif /* HAVE_LCD_COLOR */
282
283 rects = rects->next;
284 }
285}
286
287/*
288 * Clear screen
289 * (077C)
290 */
291void sysvid_clear(void)
292{
293 rb->memset(sysvid_fb, 0, sizeof(U8) * SYSVID_WIDTH * SYSVID_HEIGHT);
294}
295
296/*
297 * Initialise video
298 */
299bool sysvid_init()
300{
301 bool success;
302
303 if (isVideoInitialised)
304 {
305 return true;
306 }
307
308 IFDEBUG_VIDEO(sys_printf("xrick/video: start\n"););
309
310 success = false;
311 do
312 {
313 /* allocate xRick generic frame buffer into memory */
314 sysvid_fb = sysmem_push(sizeof(U8) * SYSVID_WIDTH * SYSVID_HEIGHT);
315 if (!sysvid_fb)
316 {
317 sys_error("(video) unable to allocate frame buffer");
318 break;
319 }
320
321#ifndef HAVE_LCD_COLOR
322 gbuf = sysmem_push(GREYBUFSIZE);
323 if (!gbuf)
324 {
325 sys_error("(video) unable to allocate buffer for greyscale functions");
326 break;
327 }
328
329 if (!grey_init(gbuf, GREYBUFSIZE, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL))
330 {
331 sys_error("(video) not enough memory to initialise greyscale functions");
332 break;
333 }
334#endif /* ndef HAVE_LCD_COLOR */
335
336 success = true;
337 } while (false);
338
339 if (!success)
340 {
341#ifndef HAVE_LCD_COLOR
342 sysmem_pop(gbuf);
343#endif
344 sysmem_pop(sysvid_fb);
345 return false;
346 }
347
348#if (LCD_HEIGHT < SYSVID_HEIGHT)
349 calculateRowsToSkip();
350#endif
351#if (LCD_WIDTH < SYSVID_WIDTH)
352 calculateColumnsToSkip();
353#endif
354
355#if LCD_DEPTH > 1
356 rb->lcd_set_backdrop(NULL);
357#endif
358 /* Turn off backlight timeout */
359 backlight_ignore_timeout();
360
361 rb->lcd_set_foreground(LCD_WHITE);
362 rb->lcd_set_background(LCD_BLACK);
363 rb->lcd_clear_display();
364
365#ifdef HAVE_LCD_COLOR
366 rb->lcd_update();
367#else
368 /* switch on greyscale overlay */
369 grey_show(true);
370#endif /* HAVE_LCD_COLOR */
371
372 isVideoInitialised = true;
373 IFDEBUG_VIDEO(sys_printf("xrick/video: ready\n"););
374 return true;
375}
376
377/*
378 * Shutdown video
379 */
380void sysvid_shutdown(void)
381{
382 if (!isVideoInitialised)
383 {
384 return;
385 }
386
387#ifndef HAVE_LCD_COLOR
388 grey_show(false);
389 grey_release();
390
391 sysmem_pop(gbuf);
392#endif /* ndef HAVE_LCD_COLOR */
393 sysmem_pop(sysvid_fb);
394
395 /* Turn on backlight timeout (revert to settings) */
396 backlight_use_settings();
397
398 isVideoInitialised = false;
399 IFDEBUG_VIDEO(sys_printf("xrick/video: stop\n"););
400}
401
402/* eof */