hge循環播放兩張圖片

//zxq 加入雲層代碼
#ifndef CLOUD_H_
#define CLOUD_H_
#pragma once

#include "hge.h"
#include "hgesprite.h"
#include "hgeanim.h"

class Cloud
{
public:
    Cloud();
    ~Cloud();

    bool OnFrame(float delta_time);
    bool OnRender(float offset_x, float offset_y, float hscale, float vscale);
    bool LoadGameResource();
private:
    static const int kCloudFPS = 120; //zxq   修改雲層播放幀率  16
    static const int kCloudFrames = 20;  //zxq   修改雲層播放幀率  16

private:
    HGE* hge_;
    float _cloud_x1;//給兩張雲圖的x座標
    float _cloud_x2;
    float _cloud_y;
    hgeAnimation* spr_cloud_1;//2張雲圖重複播放
    hgeAnimation* spr_cloud_2;
    HTEXTURE tex_cloud_;
    float  since_last_frame_;

};

#endif // WATER_H_



//zxq  加入雲層代碼

#include "StdAfx.h"
#include "cloud.h"
#include "game_manager.h"

#define start_width 1366 //設置雲層寬度

Cloud::Cloud() :_cloud_x1(0),since_last_frame_(-1),_cloud_y(0){
    hge_ = hgeCreate(HGE_VERSION);

}

Cloud::~Cloud() {
    hge_->Release();
}
//zxq 變更雲層的X軸座標
bool Cloud::OnFrame(float delta_time) {
    if (since_last_frame_ == -1.0f) since_last_frame_ = 0.0f;
    else since_last_frame_ += delta_time;
    static const float kSpeed = 1.0f / kCloudFrames;
    float screen_width = static_cast<float>(hge_->System_GetState(HGE_SCREENWIDTH));
    while (since_last_frame_ >= kSpeed)
    {
        since_last_frame_ -= kSpeed;
        _cloud_x1 += screen_width / (kCloudFPS * 6);
        _cloud_x2 += screen_width / (kCloudFPS * 6);
    }
    
    return false;
}
//zxq 繪製雲層
bool Cloud::OnRender(float offset_x, float offset_y, float hscale, float vscale) {
    float screen_width = static_cast<float>(hge_->System_GetState(HGE_SCREENWIDTH));
    float screen_height = static_cast<float>(hge_->System_GetState(HGE_SCREENHEIGHT));

     spr_cloud_1->RenderEx(offset_x+_cloud_x1-screen_width,_cloud_y, 0.0f, hscale, vscale);
     spr_cloud_2->RenderEx(offset_x+_cloud_x2-screen_width,_cloud_y, 0.0f, hscale, vscale);
     if (_cloud_x2-screen_width>=screen_width)//當雲2 圖片已經過場  重新設置雲1 和 雲2 的X座標
     {
         //         _cloud_y=rand()%600-300;  設置繪製高度
         _cloud_x1=screen_width;
         _cloud_x2=0;
         since_last_frame_ = -1.0f;
     }
     if (_cloud_x1-screen_width>=screen_width)//當雲1 圖片已經過場  重新設置雲1 和 雲2 的X座標
     {
//         _cloud_y=rand()%600-300;  設置繪製高度
         _cloud_x2=screen_width;
         _cloud_x1=0;
         since_last_frame_ = -1.0f;
     }
    return false;
}
//zxq 加載雲層圖片
bool Cloud::LoadGameResource() {

    hgeResourceManager* resource_manager = GameManager::GetInstance().GetResourceManager();    
    spr_cloud_1 = resource_manager->GetAnimation("scene_cloud1");
    spr_cloud_2= resource_manager->GetAnimation("scene_cloud2");
    _cloud_x2=-static_cast<float>(hge_->System_GetState(HGE_SCREENWIDTH)) ;//初始化雲2的x座標

    return true;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章