Skip to content

Commit

Permalink
tmp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Di Wu committed May 21, 2012
1 parent 926f44b commit 0ef5ca5
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 82 deletions.
85 changes: 56 additions & 29 deletions twxes10/Classes/GameLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@

#include "GameLayer.h"

void GameLayer::draw() {

glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);


glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
}

ccColor3B GameLayer::generateDarkColor() {
const int maxValue = 100;
const int maxSum = 250;
const int maxValue = 200;
const int minValue = 100;
const int maxSum = 350;
int r, g, b;
while (true) {
r = arc4random()%maxValue;
g = arc4random()%maxValue;
b = arc4random()%maxValue;
r = arc4random()%(maxValue-minValue)+minValue;
g = arc4random()%(maxValue-minValue)+minValue;
b = arc4random()%(maxValue-minValue)+minValue;
if (r+g+b > maxSum) break;
}
return ccc3(r, g, b);
Expand All @@ -24,10 +37,10 @@ ccColor3B GameLayer::generateDarkColor() {
void GameLayer::createBox2DWorld() {

b2Vec2 gravity;
gravity.Set(0.0f, -7.0f);
gravity.Set(0.0f, -9.8f);

world = new b2World(gravity);
world->SetContinuousPhysics(true);
//world->SetContinuousPhysics(true);

// debugDraw = new GLESDebugDraw(PTM_RATIO);
// world->SetDebugDraw(debugDraw);
Expand All @@ -48,13 +61,13 @@ CCScene * GameLayer::scene() {
return scene;
}

CCSprite * GameLayer::generateBackground() {
void GameLayer::generateBackground() {

//CCSize textureSize = CCSizeMake(screenW, screenH);
int textureSize = 512;

//ccColor3B c = (ccColor3B){140,205,221};
ccColor3B c = generateDarkColor();
ccColor3B c = (ccColor3B){140,205,221};
//ccColor3B c = generateDarkColor();
ccColor4F cf = ccc4FFromccc3B(c);

//CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize.width height:textureSize.height];
Expand All @@ -64,7 +77,7 @@ CCSprite * GameLayer::generateBackground() {

// layer 1: gradient

float gradientAlpha = 0.5f;
float gradientAlpha = 0.25f;

glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Expand All @@ -73,17 +86,18 @@ CCSprite * GameLayer::generateBackground() {
ccColor4F colors[4];
int nVertices = 0;

vertices[nVertices] = CCPointMake(0, 0);
colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
vertices[nVertices] = CCPointMake(textureSize, 0);
colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
vertices[nVertices] = CCPointMake(0, textureSize/2);
colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
vertices[nVertices] = CCPointMake(textureSize, textureSize/2);
colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
vertices[nVertices] = ccp(0, 0);
colors[nVertices++] = (ccColor4F){1, 1, 1, 0};
vertices[nVertices] = ccp(textureSize, 0);
colors[nVertices++] = (ccColor4F){1, 1, 1, 0};
vertices[nVertices] = ccp(0, textureSize);
colors[nVertices++] = (ccColor4F){1, 1, 1, gradientAlpha};
vertices[nVertices] = ccp(textureSize, textureSize);
colors[nVertices++] = (ccColor4F){1, 1, 1, gradientAlpha};

glVertexPointer(2, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_FLOAT, 0, colors);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Expand All @@ -109,9 +123,20 @@ CCSprite * GameLayer::generateBackground() {
s->visit();
//[rt end];
rt->end();

//self.background = [CCSprite spriteWithTexture:rt.sprite.texture];
setBackground(CCSprite::spriteWithTexture(rt->getSprite()->getTexture()));
ccTexParams tp = {GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_REPEAT};
//[_background.texture setTexParameters:&tp];
getBackground()->getTexture()->setTexParameters(&tp);
//_background.position = ccp(screenW/2,screenH/2);
getBackground()->setPosition(ccp(screenW/2,screenH/2));
// _background.scale = 0.5f;

//return [CCSprite spriteWithTexture:rt.sprite.texture];
return CCSprite::spriteWithTexture(rt->getSprite()->getTexture());
//return CCSprite::spriteWithTexture(rt->getSprite()->getTexture());

addChild(getBackground());
}

bool GameLayer::init() {
Expand All @@ -126,17 +151,19 @@ bool GameLayer::init() {
createBox2DWorld();

//setBackground(CCSprite::spriteWithFile("background.png"));
setBackground(generateBackground());
getBackground()->setPosition(ccp(screenW/2, screenH/2));
//setBackground(generateBackground());
//getBackground()->setPosition(ccp(screenW/2, screenH/2));

getBackground()->setScaleX(screenW/getBackground()->getTextureRect().size.width);
getBackground()->setScaleY(screenH/getBackground()->getTextureRect().size.height);
//getBackground()->setScaleX(screenW/getBackground()->getTextureRect().size.width);
//getBackground()->setScaleY(screenH/getBackground()->getTextureRect().size.height);

//CCLog("sw = %f, sh = %f, bw = %f, bh = %f", screenW, screenH, getBackground()->getTextureRect().size.width, getBackground()->getTextureRect().size.height);

ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
getBackground()->getTexture()->setTexParameters(&tp);
addChild(getBackground());
//ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
//getBackground()->getTexture()->setTexParameters(&tp);
//addChild(getBackground());

generateBackground();

setTerrain(Terrain::terrainWithWorld(world));

Expand Down Expand Up @@ -220,9 +247,9 @@ void GameLayer::update(ccTime dt) {
//terrain->setOffsetX(hero->getPosition().x - screenW/4);

int32 velocityIterations = 8;
int32 positionIterations = 1;
int32 positionIterations = 3;
world->Step(dt, velocityIterations, positionIterations);
world->ClearForces();
//world->ClearForces();

hero->updateNodePosition();

Expand Down
4 changes: 3 additions & 1 deletion twxes10/Classes/GameLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ class GameLayer : public CCLayer {
bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);

CCSprite * generateBackground();
void generateBackground();

void update(ccTime dt);

void createBox2DWorld();

ccColor3B generateDarkColor();

void draw();

};

Expand Down
22 changes: 18 additions & 4 deletions twxes10/Classes/Hero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

#define PTM_RATIO 32 // pixel to metre ratio

Hero::~Hero() {
getSprite()->release();
setSprite(NULL);
}

void Hero::createBox2DBody() {

CCSize size = CCDirector::sharedDirector()->getWinSize();
Expand All @@ -19,8 +24,8 @@ void Hero::createBox2DBody() {

b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.linearDamping = 0.1f;
bd.fixedRotation = true;
bd.linearDamping = 0.05f;
//bd.fixedRotation = true;
bd.position.Set(startPosition.x/PTM_RATIO, startPosition.y/PTM_RATIO);
body = world->CreateBody(&bd);

Expand All @@ -45,7 +50,10 @@ Hero * Hero::heroWithWorld(b2World * w) {

bool Hero::initWithWorld(b2World * w) {

initWithFile("hero.png");
//initWithFile("hero.png");

setSprite(CCSprite::spriteWithFile("hero.png"));
addChild(getSprite());

world = w;
radius = 16.0f;
Expand Down Expand Up @@ -126,12 +134,18 @@ void Hero::limitVelocity() {
}

void Hero::updateNodePosition() {
float x = body->GetPosition().x*PTM_RATIO;
float y = body->GetPosition().y*PTM_RATIO;
if (y < radius) {
y = radius;
}
//self.position = ccp(body->GetPosition().x*PTM_RATIO, body->GetPosition().y*PTM_RATIO);
setPosition(ccp(body->GetPosition().x*PTM_RATIO, body->GetPosition().y*PTM_RATIO));
setPosition(ccp(x, y));
b2Vec2 vel = body->GetLinearVelocity();
float angle = atan2f(vel.y, vel.x);
//self.rotation = -1 * CC_RADIANS_TO_DEGREES(angle);
setRotation(-1 * CC_RADIANS_TO_DEGREES(angle));
//body->SetTransform(body->GetPosition(), angle);
}


Expand Down
5 changes: 4 additions & 1 deletion twxes10/Classes/Hero.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

using namespace cocos2d;

class Hero : public CCSprite {
class Hero : public CCNode {

public:
b2World *world;
Expand All @@ -33,8 +33,11 @@ class Hero : public CCSprite {
//void draw();

CC_SYNTHESIZE_READONLY(bool, awake, Awake);
CC_SYNTHESIZE_RETAIN(CCSprite *, sprite, Sprite);

void createBox2DBody();

~Hero();
};

#endif
Loading

0 comments on commit 0ef5ca5

Please sign in to comment.