-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
306 lines (250 loc) · 9.41 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*******************************************************************************************
*
* raylib [shapes] example - collision area
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2019 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pib.h>
#include <psp2/kernel/processmgr.h>
#include <raylib.h>
#include <psp2/gxm.h>
#include <psp2/ctrl.h>
#include <psp2/system_param.h>
bool flag=true;
extern uint32_t sceLibcHeapSize = 2194304;
#define ALIGN_CENTER(a, b) (((a) - (b)) / 2)
#define ALIGN_RIGHT(x, w) ((x) - (w))
#define ANALOG_CENTER 128
#define ANALOG_THRESHOLD 64
#define ANALOG_SENSITIVITY 16
int enter_button=0;
enum PadButtons {
PAD_UP,
PAD_DOWN,
PAD_LEFT,
PAD_RIGHT,
PAD_LTRIGGER,
PAD_RTRIGGER,
PAD_TRIANGLE,
PAD_CIRCLE,
PAD_CROSS,
PAD_SQUARE,
PAD_START,
PAD_SELECT,
PAD_ENTER,
PAD_CANCEL,
PAD_LEFT_ANALOG_UP,
PAD_LEFT_ANALOG_DOWN,
PAD_LEFT_ANALOG_LEFT,
PAD_LEFT_ANALOG_RIGHT,
PAD_RIGHT_ANALOG_UP,
PAD_RIGHT_ANALOG_DOWN,
PAD_RIGHT_ANALOG_LEFT,
PAD_RIGHT_ANALOG_RIGHT,
PAD_N_BUTTONS
};
typedef uint8_t Pad[PAD_N_BUTTONS];
SceCtrlData pad;
Pad old_pad, current_pad, pressed_pad, released_pad, hold_pad, hold2_pad;
Pad hold_count, hold2_count;
void readPad() {
memset(&pad, 0, sizeof(SceCtrlData));
sceCtrlPeekBufferPositive(0, &pad, 1);
memcpy(&old_pad, current_pad, sizeof(Pad));
memset(¤t_pad, 0, sizeof(Pad));
if (pad.buttons & SCE_CTRL_UP)
current_pad[PAD_UP] = 1;
if (pad.buttons & SCE_CTRL_DOWN)
current_pad[PAD_DOWN] = 1;
if (pad.buttons & SCE_CTRL_LEFT)
current_pad[PAD_LEFT] = 1;
if (pad.buttons & SCE_CTRL_RIGHT)
current_pad[PAD_RIGHT] = 1;
if (pad.buttons & SCE_CTRL_LTRIGGER)
current_pad[PAD_LTRIGGER] = 1;
if (pad.buttons & SCE_CTRL_RTRIGGER)
current_pad[PAD_RTRIGGER] = 1;
if (pad.buttons & SCE_CTRL_TRIANGLE)
current_pad[PAD_TRIANGLE] = 1;
if (pad.buttons & SCE_CTRL_CIRCLE)
current_pad[PAD_CIRCLE] = 1;
if (pad.buttons & SCE_CTRL_CROSS)
current_pad[PAD_CROSS] = 1;
if (pad.buttons & SCE_CTRL_SQUARE)
current_pad[PAD_SQUARE] = 1;
if (pad.buttons & SCE_CTRL_START)
current_pad[PAD_START] = 1;
if (pad.buttons & SCE_CTRL_SELECT)
current_pad[PAD_SELECT] = 1;
if (pad.ly < ANALOG_CENTER - ANALOG_THRESHOLD) {
current_pad[PAD_LEFT_ANALOG_UP] = 1;
} else if (pad.ly > ANALOG_CENTER + ANALOG_THRESHOLD) {
current_pad[PAD_LEFT_ANALOG_DOWN] = 1;
}
if (pad.lx < ANALOG_CENTER - ANALOG_THRESHOLD) {
current_pad[PAD_LEFT_ANALOG_LEFT] = 1;
} else if (pad.lx > ANALOG_CENTER + ANALOG_THRESHOLD) {
current_pad[PAD_LEFT_ANALOG_RIGHT] = 1;
}
if (pad.ry < ANALOG_CENTER - ANALOG_THRESHOLD) {
current_pad[PAD_RIGHT_ANALOG_UP] = 1;
} else if (pad.ry > ANALOG_CENTER + ANALOG_THRESHOLD) {
current_pad[PAD_RIGHT_ANALOG_DOWN] = 1;
}
if (pad.rx < ANALOG_CENTER - ANALOG_THRESHOLD) {
current_pad[PAD_RIGHT_ANALOG_LEFT] = 1;
} else if (pad.rx > ANALOG_CENTER + ANALOG_THRESHOLD) {
current_pad[PAD_RIGHT_ANALOG_RIGHT] = 1;
}
int i;
for (i = 0; i < PAD_N_BUTTONS; i++) {
pressed_pad[i] = current_pad[i] & ~old_pad[i];
released_pad[i] = ~current_pad[i] & old_pad[i];
hold_pad[i] = pressed_pad[i];
hold2_pad[i] = pressed_pad[i];
if (current_pad[i]) {
if (hold_count[i] >= 10) {
hold_pad[i] = 1;
hold_count[i] = 6;
}
if (hold2_count[i] >= 10) {
hold2_pad[i] = 1;
hold2_count[i] = 10;
}
hold_count[i]++;
hold2_count[i]++;
} else {
hold_count[i] = 0;
hold2_count[i] = 0;
}
}
if (enter_button == SCE_SYSTEM_PARAM_ENTER_BUTTON_CIRCLE) {
old_pad[PAD_ENTER] = old_pad[PAD_CIRCLE];
current_pad[PAD_ENTER] = current_pad[PAD_CIRCLE];
pressed_pad[PAD_ENTER] = pressed_pad[PAD_CIRCLE];
released_pad[PAD_ENTER] = released_pad[PAD_CIRCLE];
hold_pad[PAD_ENTER] = hold_pad[PAD_CIRCLE];
hold2_pad[PAD_ENTER] = hold2_pad[PAD_CIRCLE];
old_pad[PAD_CANCEL] = old_pad[PAD_CROSS];
current_pad[PAD_CANCEL] = current_pad[PAD_CROSS];
pressed_pad[PAD_CANCEL] = pressed_pad[PAD_CROSS];
released_pad[PAD_CANCEL] = released_pad[PAD_CROSS];
hold_pad[PAD_CANCEL] = hold_pad[PAD_CROSS];
hold2_pad[PAD_CANCEL] = hold2_pad[PAD_CROSS];
} else {
old_pad[PAD_ENTER] = old_pad[PAD_CROSS];
current_pad[PAD_ENTER] = current_pad[PAD_CROSS];
pressed_pad[PAD_ENTER] = pressed_pad[PAD_CROSS];
released_pad[PAD_ENTER] = released_pad[PAD_CROSS];
hold_pad[PAD_ENTER] = hold_pad[PAD_CROSS];
hold2_pad[PAD_ENTER] = hold2_pad[PAD_CROSS];
old_pad[PAD_CANCEL] = old_pad[PAD_CIRCLE];
current_pad[PAD_CANCEL] = current_pad[PAD_CIRCLE];
pressed_pad[PAD_CANCEL] = pressed_pad[PAD_CIRCLE];
released_pad[PAD_CANCEL] = released_pad[PAD_CIRCLE];
hold_pad[PAD_CANCEL] = hold_pad[PAD_CIRCLE];
hold2_pad[PAD_CANCEL] = hold2_pad[PAD_CIRCLE];
}
}
int holdButtons(SceCtrlData *pad, uint32_t buttons, uint64_t time) {
if ((pad->buttons & buttons) == buttons) {
uint64_t time_start = sceKernelGetProcessTimeWide();
while ((pad->buttons & buttons) == buttons) {
sceKernelDelayThread(10 * 1000);
sceCtrlPeekBufferPositive(0, pad, 1);
if ((sceKernelGetProcessTimeWide() - time_start) >= time) {
return 1;
}
}
}
return 0;
}
const int screenWidth = 960;
const int screenHeight = 544;
int main(void)
{
// Initialization
//---------------------------------------------------------
pibInit(PIB_SHACCCG|PIB_GET_PROC_ADDR_CORE);
debugNetInit("192.168.1.12",18194,3);
sleep(1);
sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG);
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - collision area");
// Box A: Moving box
Rectangle boxA = { 10, GetScreenHeight()/2 - 50, 200, 100 };
int boxASpeedX = 4;
int xflag=0;
// Box B: Mouse moved box
Rectangle boxB = { GetScreenWidth()/2 - 30, GetScreenHeight()/2 - 30, 60, 60 };
Vector2 playerPosition = {GetScreenWidth()/2 - 30, GetScreenHeight()/2 - 30};
Rectangle boxCollision = { 0 }; // Collision rectangle
int screenUpperLimit = 40; // Top menu limits
bool pause = false; // Movement pause
bool collision = false; // Collision detection
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//----------------------------------------------------------
// Main game loop
while (1) // Detect window close button or ESC key
{
readPad();
if (pressed_pad[PAD_RIGHT] || hold_pad[PAD_RIGHT]) playerPosition.x += 10.0f;
else if (pressed_pad[PAD_LEFT] || hold_pad[PAD_LEFT]) playerPosition.x -= 10.0f;
else if (pressed_pad[PAD_DOWN] || hold_pad[PAD_DOWN]) playerPosition.y += 10.0f;
else if (pressed_pad[PAD_UP] || hold_pad[PAD_UP]) playerPosition.y -= 10.0f;
else if (pressed_pad[PAD_CROSS]) xflag=1;
else xflag=0;
// Update
//-----------------------------------------------------
// Move box if not paused
if (!pause) boxA.x += boxASpeedX;
// Bounce box on x screen limits
if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0)) boxASpeedX *= -1;
// Update player-controlled-box (box02)
boxB.x = playerPosition.x - boxB.width/2;
boxB.y = playerPosition.y - boxB.height/2;
// Make sure Box B does not go out of move area limits
if ((boxB.x + boxB.width) >= GetScreenWidth()) boxB.x = GetScreenWidth() - boxB.width;
else if (boxB.x <= 0) boxB.x = 0;
if ((boxB.y + boxB.height) >= GetScreenHeight()) boxB.y = GetScreenHeight() - boxB.height;
else if (boxB.y <= screenUpperLimit) boxB.y = screenUpperLimit;
// Check boxes collision
collision = CheckCollisionRecs(boxA, boxB);
// Get collision rectangle (only on collision)
if (collision) boxCollision = GetCollisionRec(boxA, boxB);
// Pause Box A movement
if (xflag) pause = !pause;
//-----------------------------------------------------
// Draw
//-----------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawRectangle(0, 0, screenWidth, screenUpperLimit, collision? RED : BLACK);
DrawRectangleRec(boxA, GOLD);
DrawRectangleRec(boxB, BLUE);
if (collision)
{
// Draw collision area
DrawRectangleRec(boxCollision, LIME);
// Draw collision message
DrawText("COLLISION!", GetScreenWidth()/2 - MeasureText("COLLISION!", 20)/2, screenUpperLimit/2 - 10, 20, BLACK);
// Draw collision area
DrawText(TextFormat("Collision Area: %i", (int)boxCollision.width*(int)boxCollision.height), GetScreenWidth()/2 - 100, screenUpperLimit + 10, 20, BLACK);
}
DrawFPS(10, 10);
EndDrawing();
//-----------------------------------------------------
}
// De-Initialization
//---------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//----------------------------------------------------------
sceKernelExitProcess(0);
return 0;
}