mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2025-12-20 17:26:01 +08:00
[cocos2d-objc] Added batcher, incomplete
This commit is contained in:
parent
8c6a91c2a1
commit
0be704dc66
@ -43,7 +43,7 @@
|
||||
self = [super init];
|
||||
if (!self) return nil;
|
||||
|
||||
skeletonNode = [SkeletonAnimation skeletonWithFile:@"goblins-mesh.json" atlasFile:@"goblins-mesh.atlas" scale:1];
|
||||
skeletonNode = [SkeletonAnimation skeletonWithFile:@"goblins-mesh.json" atlasFile:@"goblins.atlas" scale:1];
|
||||
[skeletonNode setSkin:@"goblin"];
|
||||
[skeletonNode setAnimationForTrack:0 name:@"walk" loop:YES];
|
||||
|
||||
|
||||
@ -33,6 +33,7 @@
|
||||
652107961895250000B1FF07 /* CoreText.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 652107951895250000B1FF07 /* CoreText.framework */; };
|
||||
765A2EF61D7D7A08003FB779 /* goblins.atlas in Resources */ = {isa = PBXBuildFile; fileRef = 765A2EF41D7D7A08003FB779 /* goblins.atlas */; };
|
||||
765A2EF71D7D7A08003FB779 /* goblins.png in Resources */ = {isa = PBXBuildFile; fileRef = 765A2EF51D7D7A08003FB779 /* goblins.png */; };
|
||||
76BF7E071E66ED9C00485998 /* TwoColorBatcher.c in Sources */ = {isa = PBXBuildFile; fileRef = 76BF7E051E66ED9C00485998 /* TwoColorBatcher.c */; };
|
||||
76F28D161DEC810300CDE54D /* Animation.c in Sources */ = {isa = PBXBuildFile; fileRef = 76F28CF41DEC810200CDE54D /* Animation.c */; };
|
||||
76F28D171DEC810300CDE54D /* AnimationState.c in Sources */ = {isa = PBXBuildFile; fileRef = 76F28CF51DEC810300CDE54D /* AnimationState.c */; };
|
||||
76F28D181DEC810300CDE54D /* AnimationStateData.c in Sources */ = {isa = PBXBuildFile; fileRef = 76F28CF61DEC810300CDE54D /* AnimationStateData.c */; };
|
||||
@ -159,6 +160,8 @@
|
||||
652107951895250000B1FF07 /* CoreText.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreText.framework; path = System/Library/Frameworks/CoreText.framework; sourceTree = SDKROOT; };
|
||||
765A2EF41D7D7A08003FB779 /* goblins.atlas */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = goblins.atlas; path = Resources/goblins.atlas; sourceTree = "<group>"; };
|
||||
765A2EF51D7D7A08003FB779 /* goblins.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = goblins.png; path = Resources/goblins.png; sourceTree = "<group>"; };
|
||||
76BF7E051E66ED9C00485998 /* TwoColorBatcher.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = TwoColorBatcher.c; path = src/spine/TwoColorBatcher.c; sourceTree = "<group>"; };
|
||||
76BF7E061E66ED9C00485998 /* TwoColorBatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TwoColorBatcher.h; path = src/spine/TwoColorBatcher.h; sourceTree = "<group>"; };
|
||||
76F28CF41DEC810200CDE54D /* Animation.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = Animation.c; path = "../spine-c/spine-c/src/spine/Animation.c"; sourceTree = "<group>"; };
|
||||
76F28CF51DEC810300CDE54D /* AnimationState.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = AnimationState.c; path = "../spine-c/spine-c/src/spine/AnimationState.c"; sourceTree = "<group>"; };
|
||||
76F28CF61DEC810300CDE54D /* AnimationStateData.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = AnimationStateData.c; path = "../spine-c/spine-c/src/spine/AnimationStateData.c"; sourceTree = "<group>"; };
|
||||
@ -326,6 +329,8 @@
|
||||
43F7FF861927F94800CA4038 /* SkeletonRenderer.m */,
|
||||
43C3282E170B0C19004A9460 /* spine-cocos2d-objc.h */,
|
||||
43C3282D170B0C19004A9460 /* spine-cocos2d-objc.m */,
|
||||
76BF7E051E66ED9C00485998 /* TwoColorBatcher.c */,
|
||||
76BF7E061E66ED9C00485998 /* TwoColorBatcher.h */,
|
||||
);
|
||||
name = "spine-cocos2d-objc";
|
||||
sourceTree = "<group>";
|
||||
@ -557,6 +562,7 @@
|
||||
76F28D171DEC810300CDE54D /* AnimationState.c in Sources */,
|
||||
76F28D221DEC810300CDE54D /* extension.c in Sources */,
|
||||
76F28D231DEC810300CDE54D /* IkConstraint.c in Sources */,
|
||||
76BF7E071E66ED9C00485998 /* TwoColorBatcher.c in Sources */,
|
||||
43C3282F170B0C19004A9460 /* spine-cocos2d-objc.m in Sources */,
|
||||
76F28D1F1DEC810300CDE54D /* BoundingBoxAttachment.c in Sources */,
|
||||
76F28D281DEC810300CDE54D /* PathConstraint.c in Sources */,
|
||||
@ -673,6 +679,7 @@
|
||||
);
|
||||
INFOPLIST_FILE = "Resources-ios/Info.plist";
|
||||
LIBRARY_SEARCH_PATHS = "$(inherited)";
|
||||
OTHER_CFLAGS = "";
|
||||
OTHER_LDFLAGS = (
|
||||
"-lz",
|
||||
"-lsqlite3",
|
||||
@ -700,6 +707,7 @@
|
||||
);
|
||||
INFOPLIST_FILE = "Resources-ios/Info.plist";
|
||||
LIBRARY_SEARCH_PATHS = "$(inherited)";
|
||||
OTHER_CFLAGS = "";
|
||||
OTHER_LDFLAGS = (
|
||||
"-lz",
|
||||
"-lsqlite3",
|
||||
|
||||
@ -29,6 +29,7 @@
|
||||
*****************************************************************************/
|
||||
|
||||
#import <spine/spine.h>
|
||||
#import "TwoColorBatcher.h"
|
||||
#import "cocos2d.h"
|
||||
|
||||
/** Draws a skeleton. */
|
||||
@ -45,6 +46,7 @@
|
||||
spAtlas* _atlas;
|
||||
float* _worldVertices;
|
||||
CCBlendMode* screenMode;
|
||||
spTwoColorBatcher* batcher;
|
||||
}
|
||||
|
||||
+ (id) skeletonWithData:(spSkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData;
|
||||
|
||||
172
spine-cocos2d-objc/src/spine/TwoColorBatcher.c
Normal file
172
spine-cocos2d-objc/src/spine/TwoColorBatcher.c
Normal file
@ -0,0 +1,172 @@
|
||||
/******************************************************************************
|
||||
* Spine Runtimes Software License v2.5
|
||||
*
|
||||
* Copyright (c) 2013-2016, Esoteric Software
|
||||
* All rights reserved.
|
||||
*
|
||||
* You are granted a perpetual, non-exclusive, non-sublicensable, and
|
||||
* non-transferable license to use, install, execute, and perform the Spine
|
||||
* Runtimes software and derivative works solely for personal or internal
|
||||
* use. Without the written permission of Esoteric Software (see Section 2 of
|
||||
* the Spine Software License Agreement), you may not (a) modify, translate,
|
||||
* adapt, or develop new applications using the Spine Runtimes or otherwise
|
||||
* create derivative works or improvements of the Spine Runtimes or (b) remove,
|
||||
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
|
||||
* or other intellectual property or proprietary rights notices on or in the
|
||||
* Software, including any copy thereof. Redistributions in binary or source
|
||||
* form must include this license and terms.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
|
||||
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*****************************************************************************/
|
||||
|
||||
#include "TwoColorBatcher.h"
|
||||
|
||||
#include <spine/extension.h>
|
||||
|
||||
#include <TargetConditionals.h>
|
||||
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
|
||||
#include <OpenGLES/ES2/gl.h>
|
||||
#include <OpenGLES/ES2/glext.h>
|
||||
#else
|
||||
#include <OpenGL/gl.h>
|
||||
#include <OpenGL/glu.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define STRINGIFY(A) #A
|
||||
#define MAX_VERTICES 64000
|
||||
#define MAX_INDICES 64000
|
||||
|
||||
const char* TWO_COLOR_TINT_VERTEX_SHADER = STRINGIFY(
|
||||
attribute vec4 a_position;
|
||||
attribute vec4 a_color;
|
||||
attribute vec4 a_color2;
|
||||
attribute vec2 a_texCoords;
|
||||
|
||||
\n#ifdef GL_ES\n
|
||||
varying lowp vec4 v_light;
|
||||
varying lowp vec4 v_dark;
|
||||
varying mediump vec2 v_texCoord;
|
||||
\n#else\n
|
||||
varying vec4 v_light;
|
||||
varying vec4 v_dark;
|
||||
varying vec2 v_texCoord;
|
||||
|
||||
\n#endif\n
|
||||
|
||||
void main() {
|
||||
v_light = a_color;
|
||||
v_dark = a_color2;
|
||||
v_texCoord = a_texCoords;
|
||||
gl_Position = CC_PMatrix * a_position;
|
||||
}
|
||||
);
|
||||
|
||||
const char* TWO_COLOR_TINT_FRAGMENT_SHADER = STRINGIFY(
|
||||
\n#ifdef GL_ES\n
|
||||
precision lowp float;
|
||||
\n#endif\n
|
||||
|
||||
varying vec4 v_light;
|
||||
varying vec4 v_dark;
|
||||
varying vec2 v_texCoord;
|
||||
|
||||
void main() {
|
||||
vec4 texColor = texture2D(CC_Texture0, v_texCoord);
|
||||
float alpha = texColor.a * v_light.a;
|
||||
gl_FragColor.a = alpha;
|
||||
gl_FragColor.rgb = (1.0 - texColor.rgb) * v_dark.rgb * alpha + texColor.rgb * v_light.rgb;
|
||||
}
|
||||
);
|
||||
|
||||
GLuint compileShader(GLenum shaderType, const char* shaderSource) {
|
||||
GLuint shader = glCreateShader(shaderType);
|
||||
glShaderSource(shader, 1, &shaderSource, 0);
|
||||
glCompileShader(shader);
|
||||
GLint status;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
||||
if (!status) {
|
||||
GLsizei length;
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
|
||||
if (length < 1) {
|
||||
printf("Unknown error while compiling shader\n");
|
||||
exit(-1);
|
||||
} else {
|
||||
char* log = MALLOC(char, length);
|
||||
glGetShaderInfoLog(shader, length, 0, log);
|
||||
printf("Error compiling shader: %s\n", log);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
return shader;
|
||||
}
|
||||
|
||||
spShader* spShader_create(const char* vertexShaderSource, const char* fragmentShaderSource) {
|
||||
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
|
||||
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
|
||||
|
||||
GLuint program = glCreateProgram();
|
||||
glAttachShader(program, vertexShader);
|
||||
glAttachShader(program, fragmentShader);
|
||||
glLinkProgram(program);
|
||||
|
||||
GLint status;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &status);
|
||||
if (!status) {
|
||||
printf("Unknown error while linking program\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
spShader* shader = MALLOC(spShader, 1);
|
||||
shader->program = program;
|
||||
shader->vertexShader = vertexShader;
|
||||
shader->fragmentShader = fragmentShader;
|
||||
return shader;
|
||||
}
|
||||
|
||||
spTwoColorBatcher* _spTwoColorBatcher_create() {
|
||||
spTwoColorBatcher* batcher = MALLOC(spTwoColorBatcher, 1);
|
||||
|
||||
batcher->shader = spShader_create(TWO_COLOR_TINT_VERTEX_SHADER, TWO_COLOR_TINT_FRAGMENT_SHADER);
|
||||
batcher->positionAttributeLocation = glGetAttribLocation(batcher->shader->program, "a_position");
|
||||
batcher->colorAttributeLocation = glGetAttribLocation(batcher->shader->program, "a_color");
|
||||
batcher->color2AttributeLocation = glGetAttribLocation(batcher->shader->program, "a_color2");
|
||||
batcher->texCoordsAttributeLocation = glGetAttribLocation(batcher->shader->program, "a_texCoords");
|
||||
|
||||
glGenBuffers(1, &batcher->vertexBufferHandle);
|
||||
glGenBuffers(1, &batcher->indexBufferHandle);
|
||||
batcher->verticesBuffer = MALLOC(spVertex, MAX_VERTICES);
|
||||
batcher->indicesBuffer = MALLOC(unsigned short, MAX_INDICES);
|
||||
batcher->numIndices = 0;
|
||||
batcher->numVertices = 0;
|
||||
return batcher;
|
||||
}
|
||||
|
||||
void _spTwoColorBatcher_add(spTwoColorBatcher* batcher, spVertex* triangles, unsigned short* indices, uint32_t textureHandle, uint32_t srcBlend, uint32_t dstBlend) {
|
||||
|
||||
}
|
||||
|
||||
void _spTwoColorBatcher_flush(spTwoColorBatcher* batcher) {
|
||||
|
||||
}
|
||||
|
||||
void _spDisposeTwoColorBatcher(spTwoColorBatcher* batcher) {
|
||||
glDeleteProgram(batcher->shader->program);
|
||||
glDeleteShader(batcher->shader->vertexShader);
|
||||
glDeleteShader(batcher->shader->fragmentShader);
|
||||
FREE(batcher->shader);
|
||||
glDeleteBuffers(1, &batcher->vertexBufferHandle);
|
||||
FREE(batcher->verticesBuffer);
|
||||
glDeleteBuffers(1, &batcher->indexBufferHandle);
|
||||
FREE(batcher->indicesBuffer);
|
||||
}
|
||||
71
spine-cocos2d-objc/src/spine/TwoColorBatcher.h
Normal file
71
spine-cocos2d-objc/src/spine/TwoColorBatcher.h
Normal file
@ -0,0 +1,71 @@
|
||||
/******************************************************************************
|
||||
* Spine Runtimes Software License v2.5
|
||||
*
|
||||
* Copyright (c) 2013-2016, Esoteric Software
|
||||
* All rights reserved.
|
||||
*
|
||||
* You are granted a perpetual, non-exclusive, non-sublicensable, and
|
||||
* non-transferable license to use, install, execute, and perform the Spine
|
||||
* Runtimes software and derivative works solely for personal or internal
|
||||
* use. Without the written permission of Esoteric Software (see Section 2 of
|
||||
* the Spine Software License Agreement), you may not (a) modify, translate,
|
||||
* adapt, or develop new applications using the Spine Runtimes or otherwise
|
||||
* create derivative works or improvements of the Spine Runtimes or (b) remove,
|
||||
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
|
||||
* or other intellectual property or proprietary rights notices on or in the
|
||||
* Software, including any copy thereof. Redistributions in binary or source
|
||||
* form must include this license and terms.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
|
||||
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef TwoColorBatcher_h
|
||||
#define TwoColorBatcher_h
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct spVertex {
|
||||
float x, y, z, w;
|
||||
uint32_t color;
|
||||
uint32_t color2;
|
||||
float u, v;
|
||||
} spVertex;
|
||||
|
||||
typedef struct spShader {
|
||||
uint32_t program;
|
||||
uint32_t vertexShader;
|
||||
uint32_t fragmentShader;
|
||||
} spShader;
|
||||
|
||||
typedef struct spTwoColorBatcher {
|
||||
spShader* shader;
|
||||
|
||||
uint32_t vertexBufferHandle;
|
||||
spVertex* verticesBuffer;
|
||||
uint32_t numVertices;
|
||||
|
||||
uint32_t indexBufferHandle;
|
||||
unsigned short* indicesBuffer;
|
||||
uint32_t numIndices;
|
||||
|
||||
int32_t positionAttributeLocation;
|
||||
int32_t colorAttributeLocation;
|
||||
int32_t color2AttributeLocation;
|
||||
int32_t texCoordsAttributeLocation;
|
||||
} spTwoColorBatcher;
|
||||
|
||||
spTwoColorBatcher* _spTwoColorBatcher_create();
|
||||
void _spTwoColorBatcher_add(spTwoColorBatcher* batcher, spVertex* triangles, unsigned short* indices, uint32_t textureHandle, uint32_t srcBlend, uint32_t dstBlend);
|
||||
void _spTwoColorBatcher_flush(spTwoColorBatcher* batcher);
|
||||
void _spDisposeTwoColorBatcher(spTwoColorBatcher* batcher);
|
||||
|
||||
#endif /* TwoColorBatcher_h */
|
||||
Loading…
x
Reference in New Issue
Block a user