Skip to content

Commit 983e5d6

Browse files
authored
Merge pull request #61 from pspdev/add-texture-example
Add sprite and sdl2_image example
2 parents 4adac09 + d134ffe commit 983e5d6

File tree

7 files changed

+364
-3
lines changed

7 files changed

+364
-3
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(sdl2-image)
4+
5+
add_executable(${PROJECT_NAME} main.c)
6+
7+
include(FindPkgConfig)
8+
pkg_search_module(SDL2 REQUIRED sdl2)
9+
pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image)
10+
11+
target_include_directories(${PROJECT_NAME} PRIVATE
12+
${SDL2_INCLUDE_DIRS}
13+
${SDL2_IMAGE_INCLUDE_DIRS}
14+
)
15+
16+
target_link_libraries(${PROJECT_NAME} PRIVATE
17+
${SDL2_LIBRARIES}
18+
${SDL2_IMAGE_LIBRARIES}
19+
)
20+
21+
if(PSP)
22+
# Create an EBOOT.PBP file
23+
create_pbp_file(
24+
TARGET ${PROJECT_NAME}
25+
ICON_PATH NULL
26+
BACKGROUND_PATH NULL
27+
PREVIEW_PATH NULL
28+
TITLE ${PROJECT_NAME}
29+
VERSION 01.00
30+
)
31+
endif()

_includes/samples/sdl2_image/main.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <SDL.h>
2+
#include <SDL_image.h>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
7+
8+
// Enable png support for SDL2_image
9+
IMG_Init(IMG_INIT_PNG);
10+
11+
SDL_Window * window = SDL_CreateWindow(
12+
"window",
13+
SDL_WINDOWPOS_UNDEFINED,
14+
SDL_WINDOWPOS_UNDEFINED,
15+
480,
16+
272,
17+
0
18+
);
19+
20+
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
21+
22+
// Load the texture
23+
SDL_Surface * pixels = IMG_Load("grass.png");
24+
SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels);
25+
SDL_FreeSurface(pixels);
26+
27+
// Store the dimensions of the texture
28+
SDL_Rect sprite_rect;
29+
SDL_QueryTexture(sprite, NULL, NULL, &sprite_rect.w, &sprite_rect.h);
30+
31+
// Set the position to draw to in the middle of the screen
32+
sprite_rect.x = 480/2 - sprite_rect.w/2;
33+
sprite_rect.y = 272/2 - sprite_rect.h/2;
34+
35+
int running = 1;
36+
SDL_Event event;
37+
while (running) {
38+
// Process input
39+
if (SDL_PollEvent(&event)) {
40+
switch (event.type) {
41+
case SDL_QUIT:
42+
// End the loop if the programs is being closed
43+
running = 0;
44+
break;
45+
case SDL_CONTROLLERDEVICEADDED:
46+
// Connect a controller when it is connected
47+
SDL_GameControllerOpen(event.cdevice.which);
48+
break;
49+
case SDL_CONTROLLERBUTTONDOWN:
50+
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
51+
// Close the program if start is pressed
52+
running = 0;
53+
}
54+
break;
55+
}
56+
}
57+
58+
// Clear the screen
59+
SDL_RenderClear(renderer);
60+
61+
// Draw a red square
62+
SDL_RenderCopy(renderer, sprite, NULL, &sprite_rect);
63+
64+
// Draw everything on a white background
65+
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
66+
SDL_RenderPresent(renderer);
67+
}
68+
SDL_DestroyRenderer(renderer);
69+
SDL_DestroyWindow(window);
70+
SDL_Quit();
71+
72+
return 0;
73+
}

_includes/samples/shape/main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <pspgu.h>
33
#include <pspdisplay.h>
44

5-
PSP_MODULE_INFO("gutest", 0, 1, 0);
5+
PSP_MODULE_INFO("shape", 0, 1, 0);
66
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU | THREAD_ATTR_USER);
77

88
#define BUFFER_WIDTH 512
@@ -11,9 +11,10 @@ PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU | THREAD_ATTR_USER);
1111
#define SCREEN_HEIGHT BUFFER_HEIGHT
1212

1313
char list[0x20000] __attribute__((aligned(64)));
14+
int running;
1415

1516
int exit_callback(int arg1, int arg2, void *common) {
16-
sceKernelExitGame();
17+
running = 0;
1718
return 0;
1819
}
1920

@@ -101,7 +102,7 @@ int main() {
101102
// Setup the library used for rendering
102103
initGu();
103104

104-
int running = 1;
105+
running = 1;
105106
while(running){
106107
startFrame();
107108

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(texture)
4+
5+
add_executable(${PROJECT_NAME} main.c)
6+
7+
target_link_libraries(${PROJECT_NAME} PRIVATE
8+
pspgu
9+
pspge
10+
pspdisplay
11+
)
12+
13+
# Create an EBOOT.PBP file
14+
create_pbp_file(
15+
TARGET ${PROJECT_NAME}
16+
ICON_PATH NULL
17+
BACKGROUND_PATH NULL
18+
PREVIEW_PATH NULL
19+
TITLE ${PROJECT_NAME}
20+
VERSION 01.00
21+
)

_includes/samples/sprite/main.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <pspkernel.h>
2+
#include <pspctrl.h>
3+
#include <pspdisplay.h>
4+
#include <pspgu.h>
5+
6+
#define STB_IMAGE_IMPLEMENTATION
7+
#include <stb_image.h>
8+
9+
PSP_MODULE_INFO("texture", 0, 1, 0);
10+
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU | THREAD_ATTR_USER);
11+
12+
#define BUFFER_WIDTH 512
13+
#define BUFFER_HEIGHT 272
14+
#define SCREEN_WIDTH 480
15+
#define SCREEN_HEIGHT BUFFER_HEIGHT
16+
17+
typedef struct
18+
{
19+
float u, v;
20+
uint32_t colour;
21+
float x, y, z;
22+
} TextureVertex;
23+
24+
typedef struct
25+
{
26+
int width, height;
27+
uint32_t * data;
28+
} Texture;
29+
30+
char list[0x20000] __attribute__((aligned(64)));
31+
32+
void * fbp0;
33+
void * fbp1;
34+
Texture texture;
35+
int running;
36+
37+
int exit_callback(int arg1, int arg2, void *common) {
38+
running = 0;
39+
return 0;
40+
}
41+
42+
int callback_thread(SceSize args, void *argp) {
43+
int cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
44+
sceKernelRegisterExitCallback(cbid);
45+
sceKernelSleepThreadCB();
46+
return 0;
47+
}
48+
49+
int setup_callbacks(void) {
50+
int thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
51+
if(thid >= 0)
52+
sceKernelStartThread(thid, 0, 0);
53+
return thid;
54+
}
55+
56+
void initGu(){
57+
sceGuInit();
58+
59+
fbp0 = guGetStaticVramBuffer(BUFFER_WIDTH, BUFFER_HEIGHT, GU_PSM_8888);
60+
fbp1 = guGetStaticVramBuffer(BUFFER_WIDTH, BUFFER_HEIGHT, GU_PSM_8888);
61+
62+
//Set up buffers
63+
sceGuStart(GU_DIRECT, list);
64+
sceGuDrawBuffer(GU_PSM_8888, fbp0, BUFFER_WIDTH);
65+
sceGuDispBuffer(SCREEN_WIDTH,SCREEN_HEIGHT,fbp1, BUFFER_WIDTH);
66+
67+
// We do not care about the depth buffer in this example
68+
sceGuDepthBuffer(fbp0, 0); // Set depth buffer to a length of 0
69+
sceGuDisable(GU_DEPTH_TEST); // Disable depth testing
70+
71+
//Set up viewport
72+
sceGuOffset(2048 - (SCREEN_WIDTH / 2), 2048 - (SCREEN_HEIGHT / 2));
73+
sceGuViewport(2048, 2048, SCREEN_WIDTH, SCREEN_HEIGHT);
74+
sceGuEnable(GU_SCISSOR_TEST);
75+
sceGuScissor(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
76+
77+
// Start a new frame and enable the display
78+
sceGuFinish();
79+
sceGuDisplay(GU_TRUE);
80+
}
81+
82+
void endGu(){
83+
sceGuDisplay(GU_FALSE);
84+
sceGuTerm();
85+
}
86+
87+
void startFrame(){
88+
sceGuStart(GU_DIRECT, list);
89+
sceGuClearColor(0xFFFFFFFF); // White background
90+
sceGuClear(GU_COLOR_BUFFER_BIT);
91+
}
92+
93+
void endFrame(){
94+
sceGuFinish();
95+
sceGuSync(0, 0);
96+
sceDisplayWaitVblankStart();
97+
sceGuSwapBuffers();
98+
}
99+
100+
void drawTexture(float x, float y, float w, float h) {
101+
static TextureVertex vertices[2];
102+
103+
vertices[0].u = 0.0f;
104+
vertices[0].v = 0.0f;
105+
vertices[0].colour = 0xFFFFFFFF;
106+
vertices[0].x = x;
107+
vertices[0].y = y;
108+
vertices[0].z = 0.0f;
109+
110+
vertices[1].u = w;
111+
vertices[1].v = h;
112+
vertices[1].colour = 0xFFFFFFFF;
113+
vertices[1].x = x + w;
114+
vertices[1].y = y + h;
115+
vertices[1].z = 0.0f;
116+
117+
sceGuTexMode(GU_PSM_8888, 0, 0, GU_FALSE);
118+
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
119+
sceGuTexImage(0, texture.width, texture.height, texture.width, texture.data);
120+
121+
sceGuEnable(GU_TEXTURE_2D);
122+
sceGuDrawArray(GU_SPRITES, GU_COLOR_8888 | GU_TEXTURE_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_2D, 2, 0, vertices);
123+
sceGuDisable(GU_TEXTURE_2D);
124+
}
125+
126+
127+
int main() {
128+
initGu();
129+
130+
texture.data = (uint32_t *) stbi_load("grass.png", &texture.width, &texture.height, NULL, 4);
131+
132+
running = 1;
133+
while(running){
134+
startFrame();
135+
136+
drawTexture(SCREEN_WIDTH / 2 - 8, SCREEN_HEIGHT / 2 - 8, 16, 16);
137+
138+
endFrame();
139+
}
140+
141+
return 0;
142+
}

0 commit comments

Comments
 (0)