mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2025-12-21 01:36:02 +08:00
[sdl] Better leak detection in example
This commit is contained in:
parent
dcaa4f935e
commit
d3c8ccbfc1
@ -29,64 +29,74 @@
|
|||||||
|
|
||||||
#include <spine-sdl-cpp.h>
|
#include <spine-sdl-cpp.h>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
#include <spine/Debug.h>
|
||||||
#undef main
|
#undef main
|
||||||
|
spine::DebugExtension dbgExtension(spine::SpineExtension::getInstance());
|
||||||
|
extern spine::SkeletonRenderer *skeletonRenderer;
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main() {
|
||||||
if (SDL_Init(SDL_INIT_VIDEO)) {
|
spine::SpineExtension::setInstance(&dbgExtension);
|
||||||
printf("Error: %s", SDL_GetError());
|
{
|
||||||
return -1;
|
if (SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
}
|
printf("Error: %s", SDL_GetError());
|
||||||
SDL_Window *window = SDL_CreateWindow("Spine SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
|
return -1;
|
||||||
if (!window) {
|
}
|
||||||
printf("Error: %s", SDL_GetError());
|
SDL_Window *window = SDL_CreateWindow("Spine SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600,
|
||||||
return -1;
|
0);
|
||||||
}
|
if (!window) {
|
||||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
printf("Error: %s", SDL_GetError());
|
||||||
if (!renderer) {
|
return -1;
|
||||||
printf("Error: %s", SDL_GetError());
|
}
|
||||||
return -1;
|
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||||
}
|
if (!renderer) {
|
||||||
|
printf("Error: %s", SDL_GetError());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
spine::SDLTextureLoader textureLoader(renderer);
|
spine::SDLTextureLoader textureLoader(renderer);
|
||||||
spine::Atlas atlas("data/spineboy-pma.atlas", &textureLoader);
|
spine::Atlas atlas("data/spineboy-pma.atlas", &textureLoader);
|
||||||
spine::AtlasAttachmentLoader attachmentLoader(&atlas);
|
spine::AtlasAttachmentLoader attachmentLoader(&atlas);
|
||||||
spine::SkeletonJson json(&attachmentLoader);
|
spine::SkeletonJson json(&attachmentLoader);
|
||||||
json.setScale(0.5f);
|
json.setScale(0.5f);
|
||||||
spine::SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.json");
|
spine::SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.json");
|
||||||
spine::SkeletonDrawable drawable(skeletonData);
|
spine::SkeletonDrawable drawable(skeletonData);
|
||||||
drawable.usePremultipliedAlpha = true;
|
drawable.usePremultipliedAlpha = true;
|
||||||
drawable.animationState->getData()->setDefaultMix(0.2f);
|
drawable.animationState->getData()->setDefaultMix(0.2f);
|
||||||
drawable.skeleton->setPosition(400, 500);
|
drawable.skeleton->setPosition(400, 500);
|
||||||
drawable.skeleton->setToSetupPose();
|
drawable.skeleton->setToSetupPose();
|
||||||
drawable.animationState->setAnimation(0, "portal", true);
|
drawable.animationState->setAnimation(0, "portal", true);
|
||||||
drawable.animationState->addAnimation(0, "run", true, 0);
|
drawable.animationState->addAnimation(0, "run", true, 0);
|
||||||
drawable.update(0, spine::Physics_Update);
|
drawable.update(0, spine::Physics_Update);
|
||||||
|
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
uint64_t lastFrameTime = SDL_GetPerformanceCounter();
|
uint64_t lastFrameTime = SDL_GetPerformanceCounter();
|
||||||
while (!quit) {
|
while (!quit) {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event) != 0) {
|
while (SDL_PollEvent(&event) != 0) {
|
||||||
if (event.type == SDL_QUIT) {
|
if (event.type == SDL_QUIT) {
|
||||||
quit = true;
|
quit = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(renderer, 94, 93, 96, 255);
|
SDL_SetRenderDrawColor(renderer, 94, 93, 96, 255);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
uint64_t now = SDL_GetPerformanceCounter();
|
uint64_t now = SDL_GetPerformanceCounter();
|
||||||
double deltaTime = (now - lastFrameTime) / (double) SDL_GetPerformanceFrequency();
|
double deltaTime = (now - lastFrameTime) / (double) SDL_GetPerformanceFrequency();
|
||||||
lastFrameTime = now;
|
lastFrameTime = now;
|
||||||
|
|
||||||
drawable.update(deltaTime, spine::Physics_Update);
|
drawable.update(deltaTime, spine::Physics_Update);
|
||||||
drawable.draw(renderer);
|
drawable.draw(renderer);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
delete skeletonData;
|
||||||
|
}
|
||||||
|
delete skeletonRenderer;
|
||||||
|
dbgExtension.reportLeaks();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,10 +33,11 @@
|
|||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
|
||||||
#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
|
#include <spine/Debug.h>
|
||||||
|
|
||||||
using namespace spine;
|
using namespace spine;
|
||||||
|
|
||||||
SkeletonRenderer skeletonRenderer;
|
SkeletonRenderer *skeletonRenderer = nullptr;
|
||||||
|
|
||||||
SkeletonDrawable::SkeletonDrawable(SkeletonData *skeletonData, AnimationStateData *animationStateData) {
|
SkeletonDrawable::SkeletonDrawable(SkeletonData *skeletonData, AnimationStateData *animationStateData) {
|
||||||
Bone::setYDown(true);
|
Bone::setYDown(true);
|
||||||
@ -61,14 +62,15 @@ void SkeletonDrawable::update(float delta, Physics physics) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline void toSDLColor(uint32_t color, SDL_Color *sdlColor) {
|
inline void toSDLColor(uint32_t color, SDL_Color *sdlColor) {
|
||||||
sdlColor->r = (color >> 24) & 0xFF;
|
sdlColor->a = (color >> 24) & 0xFF;
|
||||||
sdlColor->g = (color >> 16) & 0xFF;
|
sdlColor->r = (color >> 16) & 0xFF;
|
||||||
sdlColor->b = (color >> 8) & 0xFF;
|
sdlColor->g = (color >> 8) & 0xFF;
|
||||||
sdlColor->a = color & 0xFF;
|
sdlColor->b = color & 0xFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonDrawable::draw(SDL_Renderer *renderer) {
|
void SkeletonDrawable::draw(SDL_Renderer *renderer) {
|
||||||
RenderCommand *command = skeletonRenderer.render(*skeleton);
|
if (!skeletonRenderer) skeletonRenderer = new (__FILE__, __LINE__) SkeletonRenderer();
|
||||||
|
RenderCommand *command = skeletonRenderer->render(*skeleton);
|
||||||
while(command) {
|
while(command) {
|
||||||
float *positions = command->positions;
|
float *positions = command->positions;
|
||||||
float *uvs = command->uvs;
|
float *uvs = command->uvs;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user