Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve basic programs page with better code #58

Merged
merged 3 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion _includes/samples/sdl2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ int main(int argc, char *argv[])
int running = 1;
SDL_Event event;
while (running) {
// Process input
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
// End the loop if the programs is being closed
running = 0;
break;
case SDL_CONTROLLERDEVICEADDED:
// Connect a controller when it is connected
SDL_GameControllerOpen(event.cdevice.which);
break;
case SDL_CONTROLLERBUTTONDOWN:
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START)
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
// Close the program if start is pressed
running = 0;
}
break;
}
}
Expand All @@ -46,6 +51,9 @@ int main(int argc, char *argv[])
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();

return 0;
}
30 changes: 27 additions & 3 deletions _includes/samples/shape/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU | THREAD_ATTR_USER);

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

int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}

int callback_thread(SceSize args, void *argp) {
int cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}

int setup_callbacks(void) {
int thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
if(thid >= 0)
sceKernelStartThread(thid, 0, 0);
return thid;
}

void initGu(){
sceGuInit();

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

vertices[1].x = y + w;
vertices[1].y = x + h;
vertices[1].x = x + w;
vertices[1].y = y + h;

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


int main() {
// Make exiting with the home button possible
setup_callbacks();

// Setup the library used for rendering
initGu();

int running = 1;
while(running){
startFrame();

drawRect(32, 32, 64, 64);
drawRect(216, 96, 34, 64);

endFrame();
}
Expand Down
18 changes: 10 additions & 8 deletions basic_programs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ nav_order: 3

![](images/hello.png)

> This is a simple Hello World program for the PSP.
This is a simple Hello World program for the PSP.

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

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

![](images/shape.png)

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

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

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

![](images/controls.png)

> This is a simple program to use the PSP's input functions.
This is a simple program to use the PSP's input functions.

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

Expand Down Expand Up @@ -126,7 +126,7 @@ make

![](images/audio.png)

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

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

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

![](images/sdl2.png)
![](images/shape.png)

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.

> 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.
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.

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

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

![](images/sdl2_mixer.png)

> 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).
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).

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

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

![](images/sdl2_ttf.jpg)

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

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

Expand Down
Binary file removed images/sdl2.png
Binary file not shown.
Binary file modified images/shape.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.