Skip to content

Commit b64e793

Browse files
authored
Merge pull request #58 from pspdev/improve-basic-examples
Improve basic programs page with better code
2 parents 2e426c0 + 9df223b commit b64e793

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

_includes/samples/sdl2/main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,22 @@ int main(int argc, char *argv[])
2020
int running = 1;
2121
SDL_Event event;
2222
while (running) {
23+
// Process input
2324
if (SDL_PollEvent(&event)) {
2425
switch (event.type) {
2526
case SDL_QUIT:
27+
// End the loop if the programs is being closed
2628
running = 0;
2729
break;
2830
case SDL_CONTROLLERDEVICEADDED:
31+
// Connect a controller when it is connected
2932
SDL_GameControllerOpen(event.cdevice.which);
3033
break;
3134
case SDL_CONTROLLERBUTTONDOWN:
32-
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START)
35+
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
36+
// Close the program if start is pressed
3337
running = 0;
38+
}
3439
break;
3540
}
3641
}
@@ -46,6 +51,9 @@ int main(int argc, char *argv[])
4651
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
4752
SDL_RenderPresent(renderer);
4853
}
54+
SDL_DestroyRenderer(renderer);
55+
SDL_DestroyWindow(window);
56+
SDL_Quit();
4957

5058
return 0;
5159
}

_includes/samples/shape/main.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU | THREAD_ATTR_USER);
1212

1313
char list[0x20000] __attribute__((aligned(64)));
1414

15+
int exit_callback(int arg1, int arg2, void *common) {
16+
sceKernelExitGame();
17+
return 0;
18+
}
19+
20+
int callback_thread(SceSize args, void *argp) {
21+
int cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
22+
sceKernelRegisterExitCallback(cbid);
23+
sceKernelSleepThreadCB();
24+
return 0;
25+
}
26+
27+
int setup_callbacks(void) {
28+
int thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
29+
if(thid >= 0)
30+
sceKernelStartThread(thid, 0, 0);
31+
return thid;
32+
}
33+
1534
void initGu(){
1635
sceGuInit();
1736

@@ -67,21 +86,26 @@ void drawRect(float x, float y, float w, float h) {
6786
vertices[0].x = x;
6887
vertices[0].y = y;
6988

70-
vertices[1].x = y + w;
71-
vertices[1].y = x + h;
89+
vertices[1].x = x + w;
90+
vertices[1].y = y + h;
7291

7392
sceGuColor(0xFF0000FF); // Red, colors are ABGR
7493
sceGuDrawArray(GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices);
7594
}
7695

7796

7897
int main() {
98+
// Make exiting with the home button possible
99+
setup_callbacks();
100+
101+
// Setup the library used for rendering
79102
initGu();
103+
80104
int running = 1;
81105
while(running){
82106
startFrame();
83107

84-
drawRect(32, 32, 64, 64);
108+
drawRect(216, 96, 34, 64);
85109

86110
endFrame();
87111
}

basic_programs.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ nav_order: 3
1212

1313
![](images/hello.png)
1414

15-
> This is a simple Hello World program for the PSP.
15+
This is a simple Hello World program for the PSP.
1616

1717
Click on view source below to see the code and how to build it.
1818

@@ -50,7 +50,7 @@ This will result in an EBOOT.PBP file in the build directory. Put it in a direct
5050

5151
![](images/shape.png)
5252

53-
> This is a simple square drawn on the PSP. It uses the native libgu library.
53+
This is a simple square drawn on the PSP. It uses the native libgu library.
5454

5555
Click on view source below to see the code and how to build it.
5656

@@ -89,7 +89,7 @@ More libgu examples can be found <a href="https://github.com/pspdev/pspsdk/tree/
8989

9090
![](images/controls.png)
9191

92-
> This is a simple program to use the PSP's input functions.
92+
This is a simple program to use the PSP's input functions.
9393

9494
Click on view source below to see the code and how to build it.
9595

@@ -126,7 +126,7 @@ make
126126

127127
![](images/audio.png)
128128

129-
> This is a simple program to use the audio of the PSP with minimal effort. It uses native audio library.
129+
This is a simple program to use the audio of the PSP with minimal effort. It uses native audio library.
130130

131131
Click on view source below to see the code and how to build it.
132132

@@ -163,9 +163,11 @@ More audiolib examples can be found <a href="https://github.com/pspdev/pspsdk/tr
163163
## Using SDL2
164164
{: .fs-6 .fw-700 }
165165

166-
![](images/sdl2.png)
166+
![](images/shape.png)
167+
168+
SDL2 is a library which handles system specific things like input, audio and window management for you. It can also be used to render shapes and images, just like the native libgu. This can be slower, but will result in code that is easier to read and can run on multiple platforms.
167169

168-
> SDL2 is a library which handles system specific things like input, audio and window management for you. It can also be used to render shapes and images, just like the native libgu. This will be slower, but will result in code that can be run more easily on multiple platforms.
170+
Despite this example adding an option to close by pressing the start button, the code is much shorter. It can even be build for Linux without any further modifications.
169171

170172
Click on view source below for the to see the code and how to build it.
171173

@@ -212,7 +214,7 @@ More documentation on SDL can be found <a href="http://wiki.libsdl.org/FrontPage
212214

213215
![](images/sdl2_mixer.png)
214216

215-
> This is a simple program to use the SDL2_mixer library. It handle audio playback in multimedia applications and games. It supports various audio formats(MP3/OGG).
217+
This is a simple program to use the SDL2_mixer library. It handle audio playback in multimedia applications and games. It supports various audio formats(MP3/OGG).
216218

217219
Click on view source below to see the code and how to build it.
218220

@@ -249,7 +251,7 @@ This will result in an EBOOT.PBP file in the build directory. Put it in a direct
249251

250252
![](images/sdl2_ttf.jpg)
251253

252-
> This is a simple program to use the SDL2_ttf library. It provides functionality for rendering TrueType fonts for your PSP.
254+
This is a simple program to use the SDL2_ttf library. It provides functionality for rendering TrueType fonts for your PSP.
253255

254256
Click on view source below to see the code and how to build it.
255257

images/sdl2.png

-371 Bytes
Binary file not shown.

images/shape.png

33 Bytes
Loading

0 commit comments

Comments
 (0)