で、まったく見た目上同じ動作をするプログラムを2つ組んで見ました。で、
古い方のコードにくらべて、新しく組んだ方は行数が100行ほど縮んだのですが・・・
といってもアニメーションさせるやり方を変えただけですが、
そもそもこのコードはゲームプログラミングなど皆無な僕が「動けばいいや」で組んだものなので拡張性とか、管理のしやすさとか
そういうのは考えずに組んだのでなんか超スパゲッティーソースです。
で、試しに2つ、動作比較をして見ました(画面キャプチャなので若干FPS不安定だけど無視。)
旧

新

・・・変わらん・・・だと?
やはり計算部分は早いんですね・・・ほとんど変わりませんでした。
ちなみにコード
旧
► スポイラーを表示
#include
#include
#include
#include
int key[SDLK_LAST]; // キーが押されているフレーム数を格納する
int gpUpdateKey()
{
Uint8 *tmpKey; // 現在のキーの入力状態を格納する
tmpKey = SDL_GetKeyState(NULL);
SDL_PumpEvents();
for(int i=0; i 0 )
{
SDL_Delay(waitTime); //計算上待つべき時間を待つ
}
}
};
int main(int argc, char* argv[])
{
/************* 変数の宣言 ************/
SDL_Rect m_rect, scr_m_rect, fsc = {0 ,0 ,640 ,480}; //画像位置情報 (自機)
SDL_Rect s_rect, scr_s_rect; //ショットの画像位置
SDL_Event event;//eventk; //イベントの監視
int quit = 0; //イベント結果の返り値
a_i = 0;
Fps gamefps;
SDL_Surface* letters_image = IMG_Load("image/letters.png"); /* 文字画像 */
/***************ここまで**************/
Init(&scr,&image,&m_rect,&scr_m_rect,&s_rect,&scr_s_rect); //初期化に画像ハンドルと位置情報を渡す
while(!quit) //終了処理が行われるまで無限ループ
{
Move(&scr_m_rect);
gpUpdateKey();
Draw(image,&m_rect,&scr_m_rect,scr);
gamefps.Update(); //更新
gamefps.Wait(); //待機
SDL_Flip(scr); //表に出す
SDL_FillRect(scr, &fsc, SDL_MapRGB(scr->format, 0,0,0)); //画面のクリア
if(key[SDLK_ESCAPE] >= 1) break;
}
SDL_FreeSurface(image); //画像の開放
SDL_FreeSurface(letters_image);
SDL_Quit(); //終了処理
return 0;
}
void Init(SDL_Surface **scr,SDL_Surface **image,SDL_Rect *m_rect,SDL_Rect *scr_m_rect,SDL_Rect *s_rect,SDL_Rect *scr_s_rect)
{
short int sel_Ima;
SDL_Init(SDL_INIT_EVERYTHING); //SDL初期化処理
SDL_WM_SetCaption("SDL_SampleProgram", NULL); // タイトルを設定する
*scr = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); //ウィンドウ作成
//画像読み込み
*image = IMG_Load("image/Reimu.png");
if(*image == NULL)
{
std::cout x = 0;
m_rect->y = 0;
m_rect->w = 50;
m_rect->h = 50;
scr_m_rect->x = 0;
scr_m_rect->y = 0;
s_rect->x = 0;
s_rect->y = 0;
s_rect->w = 0;
s_rect->h = 0;
scr_s_rect->x = 0;
scr_s_rect->y = 0;
}
}
void Move(SDL_Rect *scr_m_rect)
{
//移動させる
if (key[SDLK_DOWN] >= 1)
{
scr_m_rect->y += 6;
if(key[SDLK_LSHIFT] >= 1) scr_m_rect->y -= 3; //shift押されてたら低速移動
}
if (key[SDLK_UP] >= 1)
{
scr_m_rect->y -= 6;
if(key[SDLK_LSHIFT] >= 1) scr_m_rect->y += 3;
}
if (key[SDLK_LEFT] >= 1)
{
scr_m_rect->x -= 6;
if(key[SDLK_LSHIFT] >= 1) scr_m_rect->x += 3;
}
if (key[SDLK_RIGHT] >= 1)
{
scr_m_rect->x += 6;
if(key[SDLK_LSHIFT] >= 1) scr_m_rect->x -= 3;
}
}
bool Draw(SDL_Surface *image,SDL_Rect *m_rect,SDL_Rect *scr_m_rect,SDL_Surface *scr)
{
Animation(m_rect); //アニメーション関数呼ぶ
SDL_BlitSurface(image, m_rect, scr, scr_m_rect); //画像描画
return true;
}
bool Animation(SDL_Rect *m_rect)
{
int ani;
a_i++;
chara_ani(&ani);
switch(ani) //今のフレームで表示すべき画像
{
case 1:
m_rect->x = 0;
m_rect->y = 0;
break;
case 2:
m_rect->x = 50;
m_rect->y = 0;
break;
case 3:
m_rect->x = 100;
m_rect->y = 0;
break;
case 4:
m_rect->x = 150;
m_rect->y = 0;
break;
case 5:
m_rect->x = 0;
m_rect->y = 50;
break;
case 6:
m_rect->x = 50;
m_rect->y = 50;
break;
case 7:
m_rect->x = 100;
m_rect->y = 50;
break;
case 8:
m_rect->x = 150;
m_rect->y = 50;
break;
case 9:
m_rect->x = 0;
m_rect->y = 100;
break;
case 10:
m_rect->x = 50;
m_rect->y = 100;
break;
case 11:
m_rect->x = 100;
m_rect->y = 100;
break;
case 12:
m_rect->x = 150;
m_rect->y = 100;
break;
}
return true;
}
void chara_ani(int *ani)
{
if(key[SDLK_RIGHT] >= 1)//右が押されていたら
{
switch((a_i/6)) //右用のアニメーション
{
case 0:
*ani = 5;
break;
case 1:
*ani = 6;
break;
case 2:
*ani = 7;
break;
case 3:
*ani = 8;
a_i = 0;
break;
}
} else
if(key[SDLK_LEFT] >= 1) //右が押されてなくて左が押されていたら
{
switch((a_i/6)) //左用アニメーション
{
case 0:
*ani = 9;
break;
case 1:
*ani = 10;
break;
case 2:
*ani = 11;
break;
case 3:
*ani = 12;
a_i = 0;
break;
}
}
else
switch((a_i/6)+1) //そうでなくて何も押されていなければ通常のアニメーション
{
case 1:
*ani = 1;
break;
case 2:
*ani = 2;
break;
case 3:
*ani = 3;
break;
case 4:
*ani = 4;
a_i = 0;
break;
}
}
► スポイラーを表示
#include
#include
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#define R_SPEED 6
int Key[SDLK_LAST]; // キーが押されているフレーム数を格納する
int gpUpdateKey()
{
Uint8 *tmpKey; // 現在のキーの入力状態を格納する
tmpKey = SDL_GetKeyState(NULL);
SDL_PumpEvents();
for(int i=0; i = 1) //上移動処理
{
scr_m_rect->y-=R_SPEED;
if(Key[SDLK_LSHIFT] >= 1)//低速移動とか
{
scr_m_rect->y+=(R_SPEED - 3);
}
j=0;
}
if(Key[SDLK_DOWN] >= 1) //下移動処理
{
scr_m_rect->y+=R_SPEED;
if(Key[SDLK_LSHIFT] >= 1)
{
scr_m_rect->y-=(R_SPEED - 3);
}
j=0;
}
if(Key[SDLK_RIGHT] >= 1) //右移動処理
{
scr_m_rect->x+=R_SPEED;
if(Key[SDLK_LSHIFT] >= 1)
{
scr_m_rect->x-=(R_SPEED - 3);
}
j=4;
}
if(Key[SDLK_LEFT] >= 1) //左移動処理
{
scr_m_rect->x-=R_SPEED;
if(Key[SDLK_LSHIFT] >= 1)
{
scr_m_rect->x+=(R_SPEED - 3);
}
j=8;
}
if(scr_m_rect->x x = 0;//画面端から出ないように
if(scr_m_rect->y y = 0;
if(scr_m_rect->x > 640 - 50) scr_m_rect->x = 640 - 50;
if(scr_m_rect->y > 480 - 50) scr_m_rect->y = 480 - 50;
if((*i + 1) % 6 == 0)//アニメーションさせる
{
*m_i = (*m_i + 1) % 4 + j;
}
return true;
}
bool Init(SDL_Surface **mainScr,SDL_Surface **img,SDL_Rect *fsc,SDL_Rect m_rect[],SDL_Rect *scr_m_rect,SDL_Rect *scr_s_rect)
{
int j_m = 0;
int j_s = 0;
fsc->x = 0,fsc->y = 0,fsc->w = 640,fsc->h = 480;
*img = IMG_Load("image/Reimu.png");//画像ロード
if(*img == NULL)
{
std::cout x = 0;
scr_m_rect->y = 0;
scr_s_rect->x = 0;
scr_s_rect->y = 0;
SDL_Init(SDL_INIT_EVERYTHING); //SDL初期化
*mainScr = SDL_SetVideoMode(640, 480, 32,SDL_DOUBLEBUF); //ウィンドウ作成
return true;
}
class Fps
{
int mStartTime; //測定開始時刻
int mCount; //カウンタ
float mFps; //fps
static const int N = 60; //平均を取るサンプル数
public:
static const int uFPS = 60;//設定したFPS
Fps()
{
mStartTime = 0;
mCount = 0;
mFps = 0;
}
bool Update()
{
if( mCount == 0 )
{ //1フレーム目なら時刻を記憶
mStartTime = SDL_GetTicks();
}
if( mCount == N )
{ //Nフレーム目なら平均を計算する
int t = SDL_GetTicks();
mFps = 1000.f/((t-mStartTime)/(float)N);
mCount = 0;
mStartTime = t;
std::cout 0 )
{
SDL_Delay(waitTime); //計算上待つべき時間を待つ
}
}
};
int main()
{
Fps mainfps;
SDL_Surface *mainScr,*img; //ウィンドウと画像のハンドル
SDL_Rect m_rect[21],scr_m_rect,fsc; //プレイヤーとかの位置情報
SDL_Rect scr_s_rect[64]; //ショット(未実装
int i=0; //アニメーションのためのカウンタ
int m_i = 0; //表示すべき画像の要素指定用カウンタ
Init(&mainScr,&img,&fsc,m_rect,&scr_m_rect,scr_s_rect); //初期化関数呼ぶ
std::cout format, 0,0,0));//画面のクリア
}
SDL_FreeSurface(mainScr);//開放
SDL_Quit();
std::cout << "終了処理完了" << std::endl;
return 0;
}
とりあえず私立入試終わったので書いて見ました。