Subversion Repositories pspware

Rev

Rev 350 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
128 shine 1
/*
2
 * Lua Player for PSP
3
 * ------------------------------------------------------------------------
4
 * Licensed under the BSD license, see LICENSE for details.
5
 *
6
 * Copyright (c) 2005 Frank Buss <fb@frank-buss.de> (aka Shine)
7
 *
8
 * Credits:
9
 *   many thanks to the authors of the PSPSDK from http://forums.ps2dev.org
10
 *   and to the hints and discussions from #pspdev on freenode.net
11
 *
12
 * $Id: main.cpp 357 2006-06-03 17:54:53Z shine $
13
 */
14
 
15
#include <pspkernel.h>
16
#include <pspdebug.h>
17
#include <pspdisplay.h>
136 nevyn 18
#include <pspctrl.h>
141 shine 19
#include <pspsdk.h>
234 shine 20
#include <psputility.h>
357 shine 21
#include <psprtc.h>
234 shine 22
#include <time.h>
128 shine 23
#include <stdlib.h>
24
#include <string.h>
136 nevyn 25
#include <unistd.h>
128 shine 26
 
27
#include "graphics.h"
136 nevyn 28
#include "sound.h"
128 shine 29
#include "luaplayer.h"
30
 
210 shine 31
/* the boot.lua */
279 shine 32
#include "boot.cpp"
210 shine 33
 
128 shine 34
/* Define the module info section */
350 Oobles 35
PSP_MODULE_INFO(LUAPLAYER, 0, 1, 1);
287 shine 36
PSP_MAIN_THREAD_ATTR(0);
345 Oobles 37
PSP_HEAP_SIZE_KB(10024); /* 10MB */
128 shine 38
 
287 shine 39
// startup path
40
char path[256];
128 shine 41
 
357 shine 42
// timezone
43
static char tz[20];
44
 
128 shine 45
/* Exit callback */
279 shine 46
int exit_callback(int arg1, int arg2, void *common)
128 shine 47
{
208 nevyn 48
 
286 shine 49
        // Unload modules
208 nevyn 50
        unloadMikmod();
51
 
128 shine 52
        sceKernelExitGame();
53
        return 0;
54
}
55
 
56
/* Callback thread */
57
int CallbackThread(SceSize args, void *argp)
58
{
59
        int cbid;
60
 
279 shine 61
        cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
128 shine 62
        sceKernelRegisterExitCallback(cbid);
63
 
64
        sceKernelSleepThreadCB();
65
 
66
        return 0;
67
}
68
 
69
/* Sets up the callback thread and returns its thread id */
70
int SetupCallbacks(void)
71
{
72
        int thid = 0;
73
 
74
        thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
75
        if(thid >= 0)
76
        {
77
                sceKernelStartThread(thid, 0, 0);
78
        }
79
 
80
        return thid;
81
}
82
 
346 Oobles 83
static int debugInitialized = 0;
84
 
345 Oobles 85
void debugResetScreen()
128 shine 86
{
346 Oobles 87
        debugInitialized = 0;
128 shine 88
}
89
 
345 Oobles 90
int debugOutput(const char *format, ...)
128 shine 91
{
345 Oobles 92
        va_list opt;
93
        char buffer[2048];
94
        int bufsz;
95
 
128 shine 96
        if (!debugInitialized) {
346 Oobles 97
                disableGraphics();
98
                pspDebugScreenInit();
128 shine 99
                debugInitialized = 1;
100
        }
345 Oobles 101
        va_start(opt, format);
102
        bufsz = vsnprintf( buffer, (size_t) sizeof(buffer), format, opt);
103
        return pspDebugScreenPrintData(buffer, bufsz);
128 shine 104
}
105
 
345 Oobles 106
int loadModule( char * moduleLocation )
128 shine 107
{
345 Oobles 108
        char path[256];
109
        getcwd(path, 256);
110
        strcat(path, moduleLocation );
111
        int retVal = sceKernelLoadModule( path, 0, NULL );
112
        if (retVal < 0)
113
        {
114
                return retVal;
115
        }
116
 
117
        int fd;
118
        retVal = sceKernelStartModule( retVal, 0, NULL, &fd, NULL );
119
        if ( retVal < 0 )
120
        {
121
                return retVal;
122
        }
123
 
124
        return 0;
125
}
126
 
357 shine 127
void initTimezone()
128
{
129
        // calculate the difference between UTC and local time
130
        u64 tick, localTick;
131
        sceRtcGetCurrentTick(&tick);
132
        sceRtcConvertUtcToLocalTime(&tick, &localTick);
133
        int minutesDelta;
134
        if (tick < localTick) {
135
                u64 delta = localTick - tick;
136
                delta /= sceRtcGetTickResolution();
137
                minutesDelta = delta;
138
                minutesDelta = -minutesDelta;
139
        } else {
140
                u64 delta = tick - localTick;
141
                delta /= sceRtcGetTickResolution();
142
                minutesDelta = delta;
143
        }
144
        minutesDelta = minutesDelta / 60;
345 Oobles 145
 
357 shine 146
        // calculate the timezone offset
147
        int tzOffsetAbs = minutesDelta < 0 ? -minutesDelta : minutesDelta;
148
        int hours = tzOffsetAbs / 60;
149
        int minutes = tzOffsetAbs - hours * 60;
150
        sprintf(tz, "GMT%s%02i:%02i", minutesDelta < 0 ? "-" : "+", hours, minutes);
151
        setenv("TZ", tz, 1);
152
        tzset();
153
}
154
 
345 Oobles 155
int user_main( SceSize argc, void *argp )
156
{
128 shine 157
        SetupCallbacks();
357 shine 158
        initTimezone();
128 shine 159
 
136 nevyn 160
        // init modules
128 shine 161
        initGraphics();
136 nevyn 162
        initMikmod();
128 shine 163
 
208 nevyn 164
        // execute Lua script (according to boot sequence)
136 nevyn 165
        getcwd(path, 256);
212 shine 166
        char* bootStringWith0 = (char*) malloc(size_bootString + 1);
167
        memcpy(bootStringWith0, bootString, size_bootString);
168
        bootString[size_bootString] = 0;
345 Oobles 169
 
208 nevyn 170
        while(1) { // reload on error
287 shine 171
                chdir(path); // set base path luaplater/
155 shine 172
                clearScreen(0);
173
                flipScreen();
174
                clearScreen(0);
210 shine 175
 
345 Oobles 176
                const char * errMsg = runScript(bootStringWith0, true);
177
                if ( errMsg != NULL);
279 shine 178
                {
345 Oobles 179
                        debugOutput("Error: %s\n", errMsg );
279 shine 180
                }
345 Oobles 181
                debugOutput("\nPress start to restart\n");
290 shine 182
 
136 nevyn 183
                SceCtrlData pad; int i;
184
                sceCtrlReadBufferPositive(&pad, 1);
189 nevyn 185
                for(i = 0; i < 40; i++) sceDisplayWaitVblankStart();
136 nevyn 186
                while(!(pad.Buttons&PSP_CTRL_START)) sceCtrlReadBufferPositive(&pad, 1);
208 nevyn 187
 
345 Oobles 188
                debugResetScreen();
136 nevyn 189
                initGraphics();
190
        }
345 Oobles 191
 
212 shine 192
        free(bootStringWith0);
128 shine 193
 
194
        // wait until user ends the program
195
        sceKernelSleepThread();
196
 
197
        return 0;
198
}
287 shine 199
 
345 Oobles 200
int main(SceSize argc, char **argv)
287 shine 201
{
202
        // create user thread, tweek stack size here if necessary
203
        SceUID thid = sceKernelCreateThread("User Mode Thread", user_main,
204
            0x11, // default priority
205
            256 * 1024, // stack size (256KB is regular default)
206
            PSP_THREAD_ATTR_USER, NULL);
207
 
208
        // start user thread, then wait for it to do everything else
209
        sceKernelStartThread(thid, 0, NULL);
210
        sceKernelWaitThreadEnd(thid, NULL);
345 Oobles 211
 
287 shine 212
        sceKernelExitGame();
213
        return 0;
214
}
215