Subversion Repositories pspware

Rev

Rev 346 | Go to most recent revision | 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 350 2006-04-04 11:00:27Z Oobles $
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>
21
#include <time.h>
128 shine 22
#include <stdlib.h>
23
#include <string.h>
136 nevyn 24
#include <unistd.h>
128 shine 25
 
26
#include "graphics.h"
136 nevyn 27
#include "sound.h"
128 shine 28
#include "luaplayer.h"
29
 
210 shine 30
/* the boot.lua */
279 shine 31
#include "boot.cpp"
210 shine 32
 
128 shine 33
/* Define the module info section */
350 Oobles 34
PSP_MODULE_INFO(LUAPLAYER, 0, 1, 1);
287 shine 35
PSP_MAIN_THREAD_ATTR(0);
345 Oobles 36
PSP_HEAP_SIZE_KB(10024); /* 10MB */
128 shine 37
 
287 shine 38
// startup path
39
char path[256];
128 shine 40
 
41
/* Exit callback */
279 shine 42
int exit_callback(int arg1, int arg2, void *common)
128 shine 43
{
208 nevyn 44
 
286 shine 45
        // Unload modules
208 nevyn 46
        unloadMikmod();
47
 
128 shine 48
        sceKernelExitGame();
49
        return 0;
50
}
51
 
52
/* Callback thread */
53
int CallbackThread(SceSize args, void *argp)
54
{
55
        int cbid;
56
 
279 shine 57
        cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
128 shine 58
        sceKernelRegisterExitCallback(cbid);
59
 
60
        sceKernelSleepThreadCB();
61
 
62
        return 0;
63
}
64
 
65
/* Sets up the callback thread and returns its thread id */
66
int SetupCallbacks(void)
67
{
68
        int thid = 0;
69
 
70
        thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
71
        if(thid >= 0)
72
        {
73
                sceKernelStartThread(thid, 0, 0);
74
        }
75
 
76
        return thid;
77
}
78
 
346 Oobles 79
static int debugInitialized = 0;
80
 
345 Oobles 81
void debugResetScreen()
128 shine 82
{
346 Oobles 83
        debugInitialized = 0;
128 shine 84
}
85
 
345 Oobles 86
int debugOutput(const char *format, ...)
128 shine 87
{
345 Oobles 88
        va_list opt;
89
        char buffer[2048];
90
        int bufsz;
91
 
128 shine 92
        if (!debugInitialized) {
346 Oobles 93
                disableGraphics();
94
                pspDebugScreenInit();
128 shine 95
                debugInitialized = 1;
96
        }
345 Oobles 97
        va_start(opt, format);
98
        bufsz = vsnprintf( buffer, (size_t) sizeof(buffer), format, opt);
99
        return pspDebugScreenPrintData(buffer, bufsz);
128 shine 100
}
101
 
345 Oobles 102
int loadModule( char * moduleLocation )
128 shine 103
{
345 Oobles 104
        char path[256];
105
        getcwd(path, 256);
106
        strcat(path, moduleLocation );
107
        int retVal = sceKernelLoadModule( path, 0, NULL );
108
        if (retVal < 0)
109
        {
110
                return retVal;
111
        }
112
 
113
        int fd;
114
        retVal = sceKernelStartModule( retVal, 0, NULL, &fd, NULL );
115
        if ( retVal < 0 )
116
        {
117
                return retVal;
118
        }
119
 
120
        return 0;
121
}
122
 
123
 
124
int user_main( SceSize argc, void *argp )
125
{
128 shine 126
        SetupCallbacks();
246 shine 127
        tzset();
128 shine 128
 
136 nevyn 129
        // init modules
128 shine 130
        initGraphics();
136 nevyn 131
        initMikmod();
128 shine 132
 
208 nevyn 133
        // execute Lua script (according to boot sequence)
136 nevyn 134
        getcwd(path, 256);
212 shine 135
        char* bootStringWith0 = (char*) malloc(size_bootString + 1);
136
        memcpy(bootStringWith0, bootString, size_bootString);
137
        bootString[size_bootString] = 0;
345 Oobles 138
 
208 nevyn 139
        while(1) { // reload on error
287 shine 140
                chdir(path); // set base path luaplater/
155 shine 141
                clearScreen(0);
142
                flipScreen();
143
                clearScreen(0);
210 shine 144
 
345 Oobles 145
                const char * errMsg = runScript(bootStringWith0, true);
146
                if ( errMsg != NULL);
279 shine 147
                {
345 Oobles 148
                        debugOutput("Error: %s\n", errMsg );
279 shine 149
                }
345 Oobles 150
                debugOutput("\nPress start to restart\n");
290 shine 151
 
136 nevyn 152
                SceCtrlData pad; int i;
153
                sceCtrlReadBufferPositive(&pad, 1);
189 nevyn 154
                for(i = 0; i < 40; i++) sceDisplayWaitVblankStart();
136 nevyn 155
                while(!(pad.Buttons&PSP_CTRL_START)) sceCtrlReadBufferPositive(&pad, 1);
208 nevyn 156
 
345 Oobles 157
                debugResetScreen();
136 nevyn 158
                initGraphics();
159
        }
345 Oobles 160
 
212 shine 161
        free(bootStringWith0);
128 shine 162
 
163
        // wait until user ends the program
164
        sceKernelSleepThread();
165
 
166
        return 0;
167
}
287 shine 168
 
345 Oobles 169
int main(SceSize argc, char **argv)
287 shine 170
{
171
        // create user thread, tweek stack size here if necessary
172
        SceUID thid = sceKernelCreateThread("User Mode Thread", user_main,
173
            0x11, // default priority
174
            256 * 1024, // stack size (256KB is regular default)
175
            PSP_THREAD_ATTR_USER, NULL);
176
 
177
        // start user thread, then wait for it to do everything else
178
        sceKernelStartThread(thid, 0, NULL);
179
        sceKernelWaitThreadEnd(thid, NULL);
345 Oobles 180
 
287 shine 181
        sceKernelExitGame();
182
        return 0;
183
}
184