Skip to content

Commit 46837cb

Browse files
committed
Fix sprite example
1 parent 621f748 commit 46837cb

File tree

1 file changed

+30
-7
lines changed
  • _includes/samples/sprite

1 file changed

+30
-7
lines changed

_includes/samples/sprite/main.c

+30-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
#include <pspctrl.h>
33
#include <pspdisplay.h>
44
#include <pspgu.h>
5+
#include <memory.h>
56

67
#define STB_IMAGE_IMPLEMENTATION
8+
#define STBI_NO_THREAD_LOCALS
79
#include <stb_image.h>
810

911
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);
1113

1214
#define BUFFER_WIDTH 512
1315
#define BUFFER_HEIGHT 272
@@ -31,7 +33,6 @@ char list[0x20000] __attribute__((aligned(64)));
3133

3234
void * fbp0;
3335
void * fbp1;
34-
Texture texture;
3536
int running;
3637

3738
int exit_callback(int arg1, int arg2, void *common) {
@@ -97,7 +98,18 @@ void endFrame(){
9798
sceGuSwapBuffers();
9899
}
99100

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) {
101113
static TextureVertex vertices[2];
102114

103115
vertices[0].u = 0.0f;
@@ -116,7 +128,7 @@ void drawTexture(float x, float y, float w, float h) {
116128

117129
sceGuTexMode(GU_PSM_8888, 0, 0, GU_FALSE);
118130
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);
120132

121133
sceGuEnable(GU_TEXTURE_2D);
122134
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) {
125137

126138

127139
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");
129145

130-
texture.data = (uint32_t *) stbi_load("grass.png", &texture.width, &texture.height, NULL, 4);
146+
// Start rendering
147+
initGu();
131148

132149
running = 1;
133150
while(running){
134151
startFrame();
135152

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);
137154

138155
endFrame();
139156
}
157+
// Stop rendering
158+
endGu();
159+
160+
// Clean up
161+
stbi_image_free(texture->data);
162+
free(texture);
140163

141164
return 0;
142165
}

0 commit comments

Comments
 (0)