Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "glib2d.h"
- #include <pspkernel.h>
- #include <pspgu.h>
- #include <pspctrl.h>
- #include <math.h>
- #define SM_SIZE 0.25f // A _low_ resolution must be enough.
- PSP_MODULE_INFO("App",0,1,1);
- PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
- /* Callbacks */
- int exit_callback(int arg1, int arg2, void *common) {
- sceKernelExitGame();
- return 0; }
- int CallbackThread(SceSize args, void *argp) {
- int cbid;
- cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
- sceKernelRegisterExitCallback(cbid);
- sceKernelSleepThreadCB();
- return 0; }
- int SetupCallbacks() {
- int thid = 0;
- thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
- if(thid >= 0) sceKernelStartThread(thid, 0, 0);
- return thid; }
- int main()
- {
- g2dImage *t1, *t2, *sm;
- int x[4] = {50, 200, -130, 80};
- int y[4] = {70, 20, -50, 100};
- int s[4] = {6, 8, 10, 15};
- int a[4] = {60, 100, 150, 200};
- g2dColor c[4] = {CYAN, WHITE, RED, GREEN};
- int i, r=0, mode=0;
- SceCtrlData pad, oldPad;
- SetupCallbacks();
- g2dInit();
- t1 = g2dTexLoad("t1.png", G2D_SWIZZLE); // Background
- t2 = g2dTexLoad("t2.png", G2D_SWIZZLE); // Point light
- sm = g2dTexCreate(G2D_SCR_W*SM_SIZE, G2D_SCR_H*SM_SIZE, true); // Shadowmap
- while (1)
- {
- // Update controls
- oldPad = pad;
- sceCtrlPeekBufferPositive(&pad, 1);
- if ( (pad.Buttons & PSP_CTRL_CROSS) &&
- !(oldPad.Buttons & PSP_CTRL_CROSS))
- {
- mode = (mode+1) % 3;
- }
- // Rotation step
- r = (r+1) % 360;
- /* First pass : construct the shadow map into the framebuffer */
- if (mode > 0)
- {
- g2dClear(BLACK);
- // Limit the draw zone
- g2dSetScissor(0, 0, G2D_SCR_W*SM_SIZE, G2D_SCR_H*SM_SIZE);
- // Scale
- g2dSetGlobalScale(SM_SIZE);
- // Color add
- sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_FIX, 0, WHITE);
- // Some coloured point lights
- g2dBeginRects(t2);
- for (i=0; i<4; i++)
- {
- g2dSetCoordXYRelative(x[i], y[i]);
- g2dSetRotationRelative(i%3 ? r : -r); // Random rotations
- g2dSetAlpha(a[i]);
- g2dSetColor(c[i]);
- g2dSetScale(s[i], s[i]);
- g2dAdd();
- }
- g2dEnd();
- // Copy the framebuffer back to the shadowmap texture
- sceGuCopyImage(GU_PSM_8888, 0, 0,
- g2d_draw_buffer.w*SM_SIZE+1,
- g2d_draw_buffer.h*SM_SIZE+1,
- g2d_draw_buffer.tw, g2d_draw_buffer.data,
- 0, 0, sm->tw, sm->data);
- // Restore
- g2dResetScissor();
- g2dResetGlobalScale();
- sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
- }
- if (mode != 1)
- {
- g2dClear(BLACK);
- /* Draw dafuq you want here */
- g2dBeginRects(t1);
- g2dAdd();
- g2dEnd();
- }
- /* Second pass : draw the shadowmap */
- if (mode == 2)
- {
- // Color multiply
- sceGuBlendFunc(GU_ADD, GU_DST_COLOR, GU_FIX, 0, 0);
- // Ignore alpha
- sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
- // Draw the shadowmap texture
- g2dBeginRects(sm);
- g2dSetScaleWH(G2D_SCR_W+1, G2D_SCR_H+1);
- g2dAdd();
- g2dEnd();
- // Restore
- sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
- sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
- }
- /* Framebuffer flip */
- g2dFlip(G2D_VSYNC);
- }
- g2dTerm();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement