こちらで質問することが適切かどうか判断出来ないため、不適切でしたら言って頂けますと助かります!
OpenGL系を軽く触った程度の知識しかないのですが、
cocos2D-xについて勉強しております。
Spriteという描画処理で戸惑っており、こちらを自作用に修正したいと考えております。
Dxライブラリなどでもそうだと思うのですが、
「毎フレーム画面をリセット」→「Draw系の関数を呼んで描画」
みたな流れだと思います。
↑このようにライブラリを改造したいです。
こういったことは可能なのでしょうか。
cocos2D-xだと描画処理をInitなどでセットして、
動かしたい時だけspriteの情報をgetByNameやらTagやらで
取得しないといけなく、直感的に修正したいです。
そこで無理やり描画用のクラスを作ってみたのですが、
処理落ちがひどくて何とか修正したいです。
何とかアドバイスを頂けないでしょうか。
■使うシーン例
void TitleScene::update( float delta ){
// 画面を初期化
this->removeAllChildren();
TitleScene::Loop();
TitleScene::Draw();
}
■DrawUtill.h
#ifndef __Game__DrawUtill__
#define __Game__DrawUtill__
#include "cocos2d.h"
USING_NS_CC;
class DrawUtill{
public:
static int test;
/******************** ロード関連 ********************/
static std::string LoadImage( const std::string& filename );
/******************** 画像描画関連 ********************/
static void DrawMulti( cocos2d::Layer *layer, int x, int y, const std::string& filename, int sx, int sy, int cx, int cy, int color, int alpha, float scaleX, float scaleY, float angle );
static void DrawScale( cocos2d::Layer *layer, int x, int y, const std::string& filename, int sx, int sy, int cx, int cy, int color, int alpha, float scaleX, float scaleY );
static void DrawRotation( cocos2d::Layer *layer, int x, int y, const std::string& filename, int sx, int sy, int cx, int cy, int color, int alpha, float angle );
static void DrawTexture( cocos2d::Layer *layer, int x, int y, const std::string& filename, int sx, int sy, int cx, int cy, int color, int alpha );
/******************** 図形描画関連 ********************/
static void DrawLine( cocos2d::Layer *layer, Point p1, Point p2, float width, int color, int alpha );
static void DrawTriangle( cocos2d::Layer *layer, Point p1, Point p2, Point p3, int color, int alpha );
static void DrawBox( cocos2d::Layer *layer, Point p1, float width, float height, int color, int alpha );
};
#endif /* defined(__Game__DrawUtill__) */
■DrawUtill.cpp
#include "DrawUtill.h"
#include <array>
USING_NS_CC;
/******************** ロード関連 ********************/
std::string DrawUtill::LoadImage( const std::string& filename )
{
return filename;
}
/******************** 画像描画関連 ********************/
void DrawUtill::DrawMulti( cocos2d::Layer *layer, int x, int y, const std::string& filename, int sx, int sy, int cx, int cy, int color, int alpha, float scaleX, float scaleY, float angle )
{
int r = color >> 16;
int g = (color >> 8) & 0xff;
int b = color & 0xff;
//auto sprite = (Sprite*)layer->getChildByName( filename );
BlendFunc blend = { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA };
auto sprite = Sprite::create( filename );
layer->addChild( sprite, 0 );
sprite->setPosition( x, y );
sprite->setTextureRect( Rect( sx, sy, cx, cy ) );
sprite->setColor( Color3B( r, g, b ) );
sprite->setOpacity( alpha );
sprite->setScaleX( scaleX );
sprite->setScaleY( scaleY );
sprite->setRotation( angle );
sprite->setBlendFunc( blend );
}
void DrawUtill::DrawScale( cocos2d::Layer *layer, int x, int y, const std::string& filename, int sx, int sy, int cx, int cy, int color, int alpha, float scaleX, float scaleY )
{
DrawMulti( layer, x, y, filename, sx, sy, cx, cy, color, alpha, scaleX, scaleY, 0 );
}
void DrawUtill::DrawRotation( cocos2d::Layer *layer, int x, int y, const std::string& filename, int sx, int sy, int cx, int cy, int color, int alpha, float angle )
{
DrawMulti( layer, x, y, filename, sx, sy, cx, cy, color, alpha, 1.0f, 1.0f, angle );
}
void DrawUtill::DrawTexture( cocos2d::Layer *layer, int x, int y, const std::string& filename, int sx, int sy, int cx, int cy, int color, int alpha )
{
DrawMulti( layer, x, y, filename, sx, sy, cx, cy, color, alpha, 1.0f, 1.0f, 0 );
}
/******************** 図形描画関連 ********************/
void DrawUtill::DrawLine( cocos2d::Layer *layer, Point p1, Point p2, float weight, int color, int alpha )
{
// 各種色を個別に取得
float r = (float)( color >> 16 ) / 255.0f;
float g = (float)( (color >> 8) & 0xff ) / 255.0f;
float b = (float)( color & 0xff ) / 255.0f;
float alp = (float)alpha / 255.0f;
// 描画用のノードを作成
DrawNode* drawNode = DrawNode::create();
drawNode->setPosition( Point(0.0f, 0.0f) );
layer->addChild(drawNode);
// 描画
BlendFunc blend = { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA };
drawNode->setBlendFunc( blend );
drawNode->drawSegment( p1, p2, weight, Color4F( r, g, b, alp ) );
}
void DrawUtill::DrawTriangle( cocos2d::Layer *layer, Point p1, Point p2, Point p3, int color, int alpha )
{
// 各種色を個別に取得
float r = (float)( color >> 16 ) / 255.0f;
float g = (float)( (color >> 8) & 0xff ) / 255.0f;
float b = (float)( color & 0xff ) / 255.0f;
float alp = (float)alpha / 255.0f;
// 描画用のノードを作成
DrawNode* drawNode = DrawNode::create();
drawNode->setPosition( Point(0.0f, 0.0f) );
layer->addChild(drawNode);
// 描画
BlendFunc blend = { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA };
drawNode->setBlendFunc( blend );
drawNode->drawTriangle( p1, p2, p3, Color4F( r, g, b, alp ));
}
void DrawUtill::DrawBox( cocos2d::Layer *layer, Point p1, float width, float height, int color, int alpha )
{
int r = color >> 16;
int g = (color >> 8) & 0xff;
int b = color & 0xff;
// 画面サイズ
Size screenSize = Director::getInstance()->getVisibleSize();
// 描画用のノードを作成
DrawNode* drawNode = DrawNode::create();
drawNode->setPosition(Point(0.0f, 0.0f));
layer->addChild(drawNode);
// 描画
std::array<Point, 4> vertexes = {
Point(50, 50),
Point(screenSize.width - 50, 50),
Point(screenSize.width - 50, screenSize.height - 50),
Point(50, screenSize.height - 50),
};
float weight = 5.0f;
BlendFunc blend = { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA };
drawNode->setBlendFunc( blend );
drawNode->drawPolygon(vertexes.data(), vertexes.size(), Color4F::WHITE, weight, Color4F::RED);
}
■テクスチャーの作り方
cocos2d::Texture2D *tex = Director::getInstance()->getTextureCache()->addImage( "card_back.png" );
auto sprite = Sprite::createWithTexture( tex );