Skip to content

Commit

Permalink
hehe
Browse files Browse the repository at this point in the history
  • Loading branch information
heweitykc committed Jun 25, 2014
1 parent db70804 commit 7a1cab9
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 40 deletions.
4 changes: 2 additions & 2 deletions proj/动画资源/UI/DemoHead_UI/Json/DemoHead_UI.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"tag": 14,
"touchAble": false,
"useMergedTexture": false,
"visible": false,
"visible": true,
"width": 452,
"x": -23,
"y": -28,
Expand Down Expand Up @@ -332,7 +332,7 @@
"tag": 14,
"touchAble": false,
"useMergedTexture": false,
"visible": false,
"visible": true,
"width": 452,
"x": -78,
"y": -27,
Expand Down
63 changes: 59 additions & 4 deletions proj/动画资源/cocos/animation3d.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "animation3d.h"
#include "binarytool.h"

Animation3D* Animation3D::create(const std::string &modelPath, const std::string &texturePath, const std::string &animationPath)
{
Expand All @@ -11,21 +12,75 @@ bool Animation3D::loadFromObj(const std::string& path)
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
cocos2d::Data meshdata = FileUtils::getInstance()->getDataFromFile(fullPath);
unsigned int len = meshdata.getSize();
const unsigned char *rawdata = meshdata.getBytes();
int pos = 0;
unsigned int magic = *((int*)rawdata);
pos += 4;

unsigned int version = *((int*)(rawdata+pos));
pos += 4;

unsigned short aniFileLen = *((short*)(rawdata + pos));
pos += 2;

pos += aniFileLen;

std::vector<float> v;
std::vector<float> vn;
std::vector<float> vt;
std::vector<std::vector<vertex_index> > faceGroup;
unsigned short textureFileLen = *((short*)(rawdata + pos));
pos += 2;

pos += textureFileLen;

unsigned int meshNum = *((int*)(rawdata + pos));
pos += 4;

log("magic=%u, ver=%u, meshnum=%u", magic,version,meshNum);

unsigned int i = 0;
while (i < meshNum){
MeshHead *meshhead = new MeshHead();
_meshHeads.push_back(meshhead);
readMesh(rawdata, pos, meshhead);
i++;
}

unsigned int vertNum = *((int*)(rawdata + pos));
pos += 4;

i = 0;
while (i < vertNum){

i++;
}

return true;
}

void Animation3D::readMesh(const unsigned char *rawdata, int& pos, MeshHead *meshhead)
{
unsigned short nameLen = *((short*)(rawdata + pos));
pos += 2;

pos += nameLen;

meshhead->vertexStart = *((int*)(rawdata + pos));
pos += 4;

meshhead->vertexCount = *((int*)(rawdata + pos));
pos += 4;

meshhead->indexStart = *((int*)(rawdata + pos));
pos += 4;

meshhead->indexCount = *((int*)(rawdata + pos));
pos += 4;
}

void readVertex(const unsigned char *rawdata, int& pos, Vertex *vertex)
{

}

void Animation3D::setBlendFunc(const BlendFunc &blendFunc)
{
if (_blend.src != blendFunc.src || _blend.dst != blendFunc.dst)
Expand Down
24 changes: 19 additions & 5 deletions proj/动画资源/cocos/animation3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@

USING_NS_CC;

struct vertex_index
struct MeshHead
{
int v_idx, vt_idx, vn_idx;
vertex_index() {};
vertex_index(int idx) : v_idx(idx), vt_idx(idx), vn_idx(idx) {};
vertex_index(int vidx, int vtidx, int vnidx) : v_idx(vidx), vt_idx(vtidx), vn_idx(vnidx) {};
char name[1024];
unsigned int vertexStart;
unsigned int vertexCount;
unsigned int indexStart;
unsigned int indexCount;
};

struct Vertex{
float x , y , z;
float nx , ny , nz;
float u, v;
int bones[4];
int weights[4];
};

class Animation3D : public Node, public BlendProtocol
{
public:
static void readVertex(const unsigned char *rawdata, int& pos, Vertex *vertex);
static void readMesh(const unsigned char *rawdata, int& pos, MeshHead *meshhead);

static Animation3D* create(const std::string &modelPath, const std::string &texturePath, const std::string &animationPath);

bool Animation3D::loadFromObj(const std::string& path);
Expand All @@ -28,6 +39,9 @@ class Animation3D : public Node, public BlendProtocol
MeshCommand _meshCommand;
Texture2D* _texture;
BlendFunc _blend;
std::vector<MeshHead*> _meshHeads;
std::vector<Vertex> _vertices; //¶¥µãÊý¾Ý
std::vector<unsigned int> _indices; //Ë÷ÒýÊý¾Ý
};

#endif
17 changes: 17 additions & 0 deletions proj/动画资源/cocos/binarytool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "binarytool.h"

void BinaryTool::fillInt(unsigned char* in, int pos, unsigned int intVar)
{
in[pos + 3] = intVar & 0xff;
in[pos + 2] = (intVar & 0xff00) >> 8;
in[pos + 1] = (intVar & 0xff0000) >> 16;
in[pos] = (intVar & 0xff000000) >> 24;
}

unsigned int BinaryTool::getInt(const unsigned char* in, int pos)
{
return (unsigned char)(*(in + pos + 3))
+ ((unsigned char)(*(in + pos + 2)) << 8)
+ ((unsigned char)(*(in + pos + 1)) << 16)
+ ((unsigned char)(*(in + pos)) << 16);
}
11 changes: 11 additions & 0 deletions proj/动画资源/cocos/binarytool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef __BINARY_TOOL_H__
#define __BINARY_TOOL_H__

class BinaryTool{
public:
static void fillInt(unsigned char* in, int pos, unsigned int intVar);
static unsigned int getInt(const unsigned char* in, int pos);

};

#endif
2 changes: 1 addition & 1 deletion proj/动画资源/cocos/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bool GameMap::init()

_role = nullptr;

_tdRole = Sprite3D::create("Sprite3DTest/boss1.obj");
_tdRole = Sprite3D::create("Sprite3DTest/boss.obj");
_tdRole->setScale(13.f);
_tdRole->setTexture("Sprite3DTest/boss.png");
_road->addChild(_tdRole);
Expand Down
13 changes: 7 additions & 6 deletions proj/动画资源/cocos/net.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "net.h"
#include "events.h"
#include "binarytool.h"

static NetLayer* instance = nullptr;

Expand Down Expand Up @@ -68,8 +69,8 @@ void NetLayer::clientMove(int x, int y)
pack.cmd = pkgUtil::NetProtocol::mvrole;
pack.len = 8;
pack.raw = new unsigned char[8];
pkgUtil::fillInt(pack.raw, 0, x);
pkgUtil::fillInt(pack.raw, 4, y);
BinaryTool::fillInt(pack.raw, 0, x);
BinaryTool::fillInt(pack.raw, 4, y);
send(pack);
}

Expand All @@ -85,15 +86,15 @@ void NetLayer::onMessage(cocos2d::network::WebSocket* ws, const cocos2d::network
pkgUtil::unpkg((unsigned char*)data.bytes,&pack);

if (pack.cmd == pkgUtil::NetProtocol::login){
_uid = pkgUtil::getInt(pack.raw, 0);
_uid = BinaryTool::getInt(pack.raw, 0);
log("login uid=%d", _uid);

dispatcher.dispatchCustomEvent(NET_LOGIN);
} else if (pack.cmd == pkgUtil::NetProtocol::addrole){
Add_Role addrole;
addrole.uid = pkgUtil::getInt(pack.raw, 0);
addrole.x = pkgUtil::getInt(pack.raw, 4);
addrole.y = pkgUtil::getInt(pack.raw, 8);
addrole.uid = BinaryTool::getInt(pack.raw, 0);
addrole.x = BinaryTool::getInt(pack.raw, 4);
addrole.y = BinaryTool::getInt(pack.raw, 8);
addrole.isHero = (_uid == addrole.uid);
log("addrole=%d,x=%d,y=%d", addrole.uid, addrole.x, addrole.y);

Expand Down
26 changes: 6 additions & 20 deletions proj/动画资源/cocos/pkgutil.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "pkgutil.h"
#include "binarytool.h"

#include <iostream>

using namespace std;
Expand All @@ -13,26 +15,10 @@ void pkgUtil::printRaw(unsigned char* data, int len)
printf("\n");
}

void pkgUtil::fillInt(unsigned char* in, int pos, unsigned int intVar)
{
in[pos + 3] = intVar & 0xff;
in[pos + 2] = (intVar & 0xff00) >> 8;
in[pos + 1] = (intVar & 0xff0000) >> 16;
in[pos] = (intVar & 0xff000000) >> 24;
}

unsigned int pkgUtil::getInt(const unsigned char* in, int pos)
{
return (unsigned char)(*(in + pos + 3))
+ ((unsigned char)(*(in + pos + 2)) << 8)
+ ((unsigned char)(*(in + pos + 1)) << 16)
+ ((unsigned char)(*(in + pos)) << 16);
}

void pkgUtil::pkg(netpack *pack, unsigned char* out)
{
fillInt(out, 0, pack->len);
fillInt(out, 4, pack->cmd);
BinaryTool::fillInt(out, 0, pack->len);
BinaryTool::fillInt(out, 4, pack->cmd);

unsigned int i = 0;
while (i < pack->len){
Expand All @@ -43,8 +29,8 @@ void pkgUtil::pkg(netpack *pack, unsigned char* out)

void pkgUtil::unpkg(const unsigned char* in, netpack *pack)
{
int len = getInt(in, 0);
int cmd = getInt(in, 4);
int len = BinaryTool::getInt(in, 0);
int cmd = BinaryTool::getInt(in, 4);
pack->raw = new unsigned char[len];
pack->len = len;
pack->cmd = cmd;
Expand Down
2 changes: 0 additions & 2 deletions proj/动画资源/cocos/pkgutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class pkgUtil
static void printRaw(unsigned char* data, int len);
static void pkg(netpack *pack, unsigned char* out); //封包
static void unpkg(const unsigned char* in, netpack *pack); //解包
static void fillInt(unsigned char* in, int pos, unsigned int intVar);
static unsigned int getInt(const unsigned char* in, int pos);
pkgUtil();
void append(const char* str, int len);
void getNext(netpack* pack);
Expand Down

0 comments on commit 7a1cab9

Please sign in to comment.