Tanner Babcock

Tripprog / main.c

		/* Tripprog - main.c
 * Copyright (c) 2011 Tanner Babcock */
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <psprtc.h>
#include <stdio.h>
#include <stdlib.h>
#include "graphics.h"
#include "main.h"
#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))
PSP_MODULE_INFO("Tripprog", 0, 1, 1);
PSP_HEAP_SIZE_KB(-2048);

int TPwarn() {
	sceCtrlReadBufferPositive(&oldpad, 1);
	while (1) {
		clearScreen(RGB(0, 0, 0));
		sceCtrlReadBufferPositive(&pad, 1);
		printTextScreen(225, 50, "WARNING", RGB(255, 255, 255));
		printTextScreen(100, 80, "This program may be harmful to one's", RGB(255, 255, 255));
		printTextScreen(100, 90, "eyes. If you are prone to seizures,", RGB(255, 255, 255));
		printTextScreen(100, 100, "consult a doctor before using this", RGB(255, 255, 255));
		printTextScreen(100, 110, "program.", RGB(255, 255, 255));
		printTextScreen(90, 150, "Press X to continue, or press O to quit", RGB(255, 255, 255));
		if ((pad.Buttons & PSP_CTRL_CROSS) && !(oldpad.Buttons & PSP_CTRL_CROSS))
			return 0;
		if ((pad.Buttons & PSP_CTRL_CIRCLE) && !(oldpad.Buttons & PSP_CTRL_CIRCLE))
			return 1;
		sceDisplayWaitVblankStart();
		flipScreen();
		oldpad = pad;
	}
}

void TPsetbg() {
	char a0[10], a1[10], d0[10], d1[10], c0[10], c1[10];
	char menu = RED;
	random = FALSE;
	sceCtrlReadBufferPositive(&oldpad, 1);
	while (1) {
		/* r0, g0, b0 = background values
		 * r1, g1, b1 = foreground values */
		clearScreen(RGB(r0, b0, g0));
		sceCtrlReadBufferPositive(&pad, 1);
		sprintf(a0, "-> R: %d", r0);
		sprintf(a1, "   R: %d", r0);
		sprintf(d0, "-> G: %d", b0); /* G and B are mixed up :P */
		sprintf(d1, "   G: %d", b0);
		sprintf(c0, "-> B: %d", g0);
		sprintf(c1, "   B: %d", g0);
		TPdice();
		fillScreenRect(RGB(r0, b0, g0), 0, 0, 480, 272);
		fillScreenRect(RGB(r1, g1, b1), x1, x2, y1, y2);
		if (menu == RED)
			printTextScreen(10, 200, a0, RGB(0, 0, 0));
		else
			printTextScreen(10, 200, a1, RGB(0, 0, 0));
		if (menu == GREEN)
			printTextScreen(10, 210, d0, RGB(0, 0, 0));
		else
			printTextScreen(10, 210, d1, RGB(0, 0, 0));
		if (menu == BLUE)
			printTextScreen(10, 220, c0, RGB(0, 0, 0));
		else
			printTextScreen(10, 220, c1, RGB(0, 0, 0));
		printTextScreen(10, 250, "Use the D-pad to change the values", RGB(0, 0, 0));
		printTextScreen(10, 260, "Press X to confirm", RGB(0, 0, 0));
		if (gray == TRUE) {
			b0 = r0;
			g0 = b0;
		}
		if ((pad.Buttons & PSP_CTRL_DOWN) && !(oldpad.Buttons & PSP_CTRL_DOWN))
			if (menu != BLUE) menu++;
		if ((pad.Buttons & PSP_CTRL_UP) && !(oldpad.Buttons & PSP_CTRL_UP))
			if (menu != RED) menu--;
		if (pad.Buttons & PSP_CTRL_RIGHT) {
			if ((menu == RED) && (r0 != 255)) r0++;
			if ((menu == GREEN) && (b0 != 255)) b0++;
			if ((menu == BLUE) && (g0 != 255)) g0++;
		}
		if (pad.Buttons & PSP_CTRL_LEFT) {
			if ((menu == RED) && (r0 != 0)) r0--;
			if ((menu == GREEN) && (b0 != 0)) b0--;
			if ((menu == BLUE) && (g0 != 0)) g0--;
		}
		if ((pad.Buttons & PSP_CTRL_CROSS) && !(oldpad.Buttons & PSP_CTRL_CROSS))
			break;

		sceDisplayWaitVblankStart();
		flipScreen();
		oldpad = pad;
	}
}

void TPdice() {
	if (gray == FALSE) {
		if (random == TRUE) {
			r0 = rand() % 256;
			g0 = rand() % 256;
			b0 = rand() % 256;
		}
		r1 = rand() % 256;
		g1 = rand() % 256;
		b1 = rand() % 256;
	}
	else {
		if (random == TRUE) {
			r0 = rand() % 256;
			g0 = r0;
			b0 = g0;
		}
		r1 = rand() % 256;
		g1 = r1;
		b1 = g1;
	}
	x1 = rand() % 480;
	x2 = rand() % 272;
	y1 = rand() % 480;
	y2 = rand() % 272;
}

void TPdraw() {
	fillScreenRect(RGB(r0, b0, g0), 0, 0, 480, 272);
	fillScreenRect(RGB(r1, g1, b1), x1, x2, y1, y2);
	if (hud == TRUE) {
		printTextScreen(10, 10, "TRIPPROG by Babkock", RGB(0, 0, 0));
		if (pause == FALSE) printTextScreen(305, 10, "Press START to pause", RGB(0, 0, 0));
		else printTextScreen(305, 10, "Press START to unpause", RGB(0, 0, 0));
		if (gray == FALSE) printTextScreen(276, 20, "Press SELECT for grayscale", RGB(0, 0, 0));
		else printTextScreen(300, 20, "Press SELECT for color", RGB(0, 0, 0));
		printTextScreen(10, 250, "Press X to randomize background", RGB(0, 0, 0));
		printTextScreen(10, 260, "Press [] to set background", RGB(0, 0, 0));
		printTextScreen(305, 250, "Press /\\ to hide HUD", RGB(0, 0, 0));
		printTextScreen(305, 260, "Press O to quit", RGB(0, 0, 0));
	}
}

int main() {
	initGraphics();
	sceCtrlReadBufferPositive(&oldpad, 1);
	if (TPwarn() == 1) goto exit;
	hud = TRUE, random = TRUE, pause = FALSE, gray = FALSE;
	while (1) {
		clearScreen(RGB(0, 0, 0));
		sceCtrlReadBufferPositive(&pad, 1);
		sceRtcGetCurrentClock(&mTime, 0);
		srand(mTime.microseconds);
		if (pause == FALSE) TPdice();
		TPdraw();
		if (gray == TRUE) {
			b0 = r0;
			g0 = b0;
			b1 = r1;
			g1 = b1;
		}
		if ((pad.Buttons & PSP_CTRL_CROSS) && !(oldpad.Buttons & PSP_CTRL_CROSS))
			random = TRUE;
		if ((pad.Buttons & PSP_CTRL_SQUARE) && !(oldpad.Buttons & PSP_CTRL_SQUARE)) {
			TPsetbg();
			random = FALSE;
		}
		if ((pad.Buttons & PSP_CTRL_TRIANGLE) && !(oldpad.Buttons & PSP_CTRL_TRIANGLE)) {
			if (hud == TRUE) hud = FALSE;
			else hud = TRUE;
		}
		if ((pad.Buttons & PSP_CTRL_START) && !(oldpad.Buttons & PSP_CTRL_START)) {
			if (pause == TRUE) pause = FALSE;
			else pause = TRUE;
		}
		if ((pad.Buttons & PSP_CTRL_SELECT) && !(oldpad.Buttons & PSP_CTRL_SELECT)) {
			if (gray == TRUE) gray = FALSE;
			else gray = TRUE;
		}
		if (pad.Buttons & PSP_CTRL_CIRCLE) break;
		flipScreen();
		oldpad = pad;
	}
	exit:
	sceKernelExitGame();
	/* I don't believe in a home menu function
	 * it's a pain in the ass to write code around it */
	return 0;
}