Skip to content

Commit ba0e6b6

Browse files
committed
Add texture example
1 parent 417e5d4 commit ba0e6b6

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

Diff for: _includes/samples/texture/CMakeLists.txt

+21
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+
)

Diff for: _includes/samples/texture/main.c

+142
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+
}

Diff for: resources/grass.png

144 Bytes
Loading

0 commit comments

Comments
 (0)