2
2
#include <pspctrl.h>
3
3
#include <pspdisplay.h>
4
4
#include <pspgu.h>
5
+ #include <memory.h>
5
6
6
7
#define STB_IMAGE_IMPLEMENTATION
8
+ #define STBI_NO_THREAD_LOCALS
7
9
#include <stb_image.h>
8
10
9
11
PSP_MODULE_INFO ("texture" , 0 , 1 , 0 );
10
- PSP_MAIN_THREAD_ATTR (THREAD_ATTR_VFPU | THREAD_ATTR_USER );
12
+ PSP_MAIN_THREAD_ATTR (THREAD_ATTR_USER );
11
13
12
14
#define BUFFER_WIDTH 512
13
15
#define BUFFER_HEIGHT 272
@@ -31,7 +33,6 @@ char list[0x20000] __attribute__((aligned(64)));
31
33
32
34
void * fbp0 ;
33
35
void * fbp1 ;
34
- Texture texture ;
35
36
int running ;
36
37
37
38
int exit_callback (int arg1 , int arg2 , void * common ) {
@@ -97,7 +98,18 @@ void endFrame(){
97
98
sceGuSwapBuffers ();
98
99
}
99
100
100
- void drawTexture (float x , float y , float w , float h ) {
101
+ Texture * loadTexture (const char * filename ) {
102
+ Texture * texture = (Texture * ) calloc (1 , sizeof (Texture ));
103
+
104
+ texture -> data = (uint32_t * ) stbi_load ("grass.png" , & (texture -> width ), & (texture -> height ), NULL , STBI_rgb_alpha );
105
+
106
+ // Make sure the texture cache is reloaded
107
+ sceKernelDcacheWritebackInvalidateAll ();
108
+
109
+ return texture ;
110
+ }
111
+
112
+ void drawTexture (Texture * texture , float x , float y , float w , float h ) {
101
113
static TextureVertex vertices [2 ];
102
114
103
115
vertices [0 ].u = 0.0f ;
@@ -116,7 +128,7 @@ void drawTexture(float x, float y, float w, float h) {
116
128
117
129
sceGuTexMode (GU_PSM_8888 , 0 , 0 , GU_FALSE );
118
130
sceGuTexFunc (GU_TFX_REPLACE , GU_TCC_RGB );
119
- sceGuTexImage (0 , texture . width , texture . height , texture . width , texture . data );
131
+ sceGuTexImage (0 , texture -> width , texture -> height , texture -> width , texture -> data );
120
132
121
133
sceGuEnable (GU_TEXTURE_2D );
122
134
sceGuDrawArray (GU_SPRITES , GU_COLOR_8888 | GU_TEXTURE_32BITF | GU_VERTEX_32BITF | GU_TRANSFORM_2D , 2 , 0 , vertices );
@@ -125,18 +137,29 @@ void drawTexture(float x, float y, float w, float h) {
125
137
126
138
127
139
int main () {
128
- initGu ();
140
+ // Make exiting with the home button possible
141
+ setup_callbacks ();
142
+
143
+ // Create a texture from a file
144
+ Texture * texture = loadTexture ("grass.png" );
129
145
130
- texture .data = (uint32_t * ) stbi_load ("grass.png" , & texture .width , & texture .height , NULL , 4 );
146
+ // Start rendering
147
+ initGu ();
131
148
132
149
running = 1 ;
133
150
while (running ){
134
151
startFrame ();
135
152
136
- drawTexture (SCREEN_WIDTH / 2 - 8 , SCREEN_HEIGHT / 2 - 8 , 16 , 16 );
153
+ drawTexture (texture , SCREEN_WIDTH / 2 - texture -> width / 2 , SCREEN_HEIGHT / 2 - texture -> height / 2 , texture -> width , texture -> height );
137
154
138
155
endFrame ();
139
156
}
157
+ // Stop rendering
158
+ endGu ();
159
+
160
+ // Clean up
161
+ stbi_image_free (texture -> data );
162
+ free (texture );
140
163
141
164
return 0 ;
142
165
}
0 commit comments