-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.c
161 lines (133 loc) · 3.34 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) 2015 Sergi Granell (xerpi)
*/
#include <stdlib.h>
#include <psp2/ctrl.h>
#include <psp2/touch.h>
#include <psp2/display.h>
#include <psp2/gxm.h>
#include <psp2/kernel/processmgr.h>
#include "utils.h"
#include "draw.h"
int main()
{
init_video();
/* Enable analog stick */
sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG);
/* Enable front touchscreen */
sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, 1);
/* FPS counting */
SceUInt64 cur_micros = 0, delta_micros = 0, last_micros = 0;
uint32_t frames = 0;
float fps = 0.0f;
/* Square variables */
int w = 60;
int h = 60;
int x = SCREEN_W/2 - w/2;
int y = SCREEN_H/2 - h/2;
int speed = 2;
uint32_t color = RGBA8(255, 0, 0, 255);
/* Input variables */
SceCtrlData pad;
SceTouchData touch;
signed char lx, ly, rx, ry;
while (1) {
clear_screen();
/* Read controls and touchscreen */
sceCtrlPeekBufferPositive(0, &pad, 1);
sceTouchPeek(0, &touch, 1);
if (pad.buttons & SCE_CTRL_START) {
break;
}
font_draw_string(10, 10, RGBA8(0, 0, 255, 255), "PSVita sample by xerpi!");
font_draw_stringf(SCREEN_W - 160, 10, RGBA8(0, 255, 0, 255), "FPS: %.2f", fps);
font_draw_stringf(10, 30, RGBA8(255, 0, 0, 255),
"(%3d, %3d) size: (%d, %d) speed: %d\n", x, y, w, h, speed);
/* Move the rectangle */
if (pad.buttons & SCE_CTRL_UP) {
y -= speed;
} else if (pad.buttons & SCE_CTRL_DOWN) {
y += speed;
}
if (pad.buttons & SCE_CTRL_LEFT) {
x -= speed;
} else if (pad.buttons & SCE_CTRL_RIGHT) {
x += speed;
}
if (pad.buttons & SCE_CTRL_LTRIGGER) {
speed--;
if (speed < 0) speed = 0;
} else if (pad.buttons & SCE_CTRL_RTRIGGER) {
speed++;
if (speed > 100) speed = 100;
}
if (pad.buttons & SCE_CTRL_CROSS) {
color = RGBA8(rand()%255, rand()%255, rand()%255, 255);
}
/* Update joystick values */
lx = (signed char)pad.lx - 128;
ly = (signed char)pad.ly - 128;
rx = (signed char)pad.rx - 128;
ry = (signed char)pad.ry - 128;
/* Move using the left yoystick */
if (abs(lx) > 50) {
x += speed * lx/50.0f;
}
if (abs(ly) > 50) {
y += speed * ly/50.0f;
}
/* Resize using the right yoystick */
if (abs(rx) > 50) {
w += rx/15.0f;
if (w < 5) {
w = 5;
} else if (w > SCREEN_W) {
w = SCREEN_W;
}
}
if (abs(ry) > 50) {
h += ry/15.0f;
if (h < 5) {
h = 5;
} if (h > SCREEN_H) {
h = SCREEN_H;
}
}
/* Move using the touchscreen! */
if (touch.reportNum > 0) {
/* Front touchscreen: 1920x1088 */
x = lerp(touch.report[0].x, 1920, SCREEN_W) - w/2;
y = lerp(touch.report[0].y, 1088, SCREEN_H) - h/2;
}
/* Check left and right collisions */
if (x < 0) {
x = 0;
} else if ((x + w) > SCREEN_W) {
x = SCREEN_W - w;
}
/* Check top and bottom collisions */
if (y < 0) {
y = 0;
} else if ((y + h) > SCREEN_H) {
y = SCREEN_H - h;
}
/* Draw the rectangle */
draw_rectangle(x, y, w, h, color);
/* Draw a circle */
draw_circle(SCREEN_W / 2, SCREEN_H / 2, 50, RGBA8(0,0,255,255));
/* Calculate FPS */
cur_micros = sceKernelGetProcessTimeWide();
if (cur_micros >= (last_micros + 1000000)) {
delta_micros = cur_micros - last_micros;
last_micros = cur_micros;
fps = (frames/(double)delta_micros)*1000000.0f;
frames = 0;
}
swap_buffers();
sceDisplayWaitVblankStart();
frames++;
}
end_video();
sceKernelExitProcess(0);
return 0;
}