DxLibFanに戻る
//dkutilをダウンロードして設定するのが面倒であれば#define USE_DKUTILをコメントアウトしてもOK
#define USE_DKUTIL

#ifdef USE_DKUTIL

#  include <DxLibMake/DxLib.h>
#  include <dkutil/gui/select.hpp>
#  include <dxex/utility.hpp>

#else

#  include "DxLib.h"

#endif


///なんか無駄な構造体だけど・・・ 
///こんな感じのデータの種類があってLoadBMPDivのような処理をすればNowLoadingができるって事です。

typedef struct DivLoadFile{
  ///ファイルサイズ
  size_t mFileSize;
  ///今読みんでいるサイズ
  size_t mNowSize;
  ///パーセンテージ
  size_t mPer;
  ///1%のサイズ
  size_t m1Per;
}DIVLOADFILE;

///このサイズをいろいろ変えて実験してみよう!
const size_t TempSize = 256;

///ウンチク等は以下のURLから
///http://d.hatena.ne.jp/studiokingyo/20040516
int LoadBMPDiv(){
  //ファイル名を保存するバッファ
  char filename[MAX_PATH];

  FILE *fp;

  BYTE *bp = NULL;

  bool Loaded = false;

  int white = GetColor(255,255,255);

  int alltime;

#ifdef  USE_DKUTIL
  if(false==dkutil::FileOpenDialog(
    filename,sizeof(filename),"BMPファイルを選択",
    "BMP(*.bmp)\0*.bmp\0All files(*.*)\0*.*\0\0"
    "Bit Map File",
    NULL
    ))
  {
    return -1;
  }

  
#else
  strcpy(filename,"test1.bmp");
#endif

  {
    alltime = GetNowCount();
    DivLoadFile a;
  
    //初期化
    memset(&a,0,sizeof(a));


    //ここからが肝
    fp = fopen(filename,"rb");
    if(NULL==fp){
      goto End;
    }
    //エラーチェックするとなお安心 ちなみに以下の三行はファイルサイズを取得する慣用句
    fseek(fp,0,SEEK_END);
    a.mFileSize = (size_t)ftell(fp);
    fseek(fp,0,SEEK_SET);


    
    bp = (BYTE *)malloc(a.mFileSize);
    if(NULL==bp){
      goto End;
    }

    //約1%を取得
    a.m1Per = a.mFileSize / 100;

    //メモリ確保
    BYTE tb[TempSize];
#  ifdef USE_DKUTIL
    dxex::key_wrap key(KEY_INPUT_Z);
#  endif
    for(;;)
    {
      if(-1==ProcessMessage()){
        break;
      }

      ClsDrawScreen();

      if(ferror(fp) ){
        goto End;
      }
      if(feof(fp)){
        Loaded = true;
        break;
      }
      //とりあえず、少なめで指定。 1byteをTempSize個読みこむ
      size_t count = fread(tb,1,TempSize,fp);
      
      //ありえないエラーチェック
      if(count > TempSize){
        goto End;
      }



      //ここからが処理の密なところ


      //読みこんだサイズを保存 &bp[a.mNowSize] が肝
      memcpy(&bp[a.mNowSize] ,tb,count);
      //今のサイズを増やす
      a.mNowSize += count;

      //読みこんだサイズ分を取得
      //別に無くてもOK
      //if(a.mNowSize >= a.mFileSize){
      //  break;
      //}

      //パーセンテージを求める。
      a.mPer = a.mNowSize / a.m1Per ;

      //文字で表示
      DrawFormatString(0,0,GetColor(255,255,255),"Now %d % Loaded",a.mPer);

      //バーで表示
      if(a.mPer != 0){ 
        size_t bar_size = (640 / 100) * a.mPer;
        DrawBox(0,(480 / 2) - 40,bar_size,480 / 2,GetColor(255,0,255),TRUE);
      }

#  ifdef USE_DKUTIL
      if(key.isOnce()){
        SaveDrawScreen(0,0,640,480,"save.bmp");
        //WaitTimer(1000);
      }
#  endif




      ScreenFlip();
  
    }

    if(Loaded){
      fclose(fp);
      fp = NULL;
      ClsDrawScreen();
      //とりあえず終わったんだから100%に設定。
      a.mPer = 100;
      //文字で表示
      DrawFormatString(0,0,GetColor(255,255,255),"Now %d % Loaded",a.mPer);

      //バーで表示
      DrawBox(0,(480 / 2) - 40,640 / a.mPer,480 / 2,GetColor(255,0,255),TRUE);

      DrawFormatString(0,30,GetColor(255,255,255),"只今画像の表示を準備中(2秒待ち)");

      //あんまりこのような処理の仕方はしたくないけど・・・。1フレーム1フリップ志向で行いたい
      ScreenFlip();
      ScreenFlip();

      WaitTimer(2000);

      ClsDrawScreen();


      int t = GetNowCount();

      int handle = CreateGraphFromMem(bp,a.mNowSize);

      int t2 = GetNowCount();

      DrawGraph(0,0,handle,TRUE);

      DrawFormatString(0,0,white,"%d millisecondsで CreateGraphFromMem 完了",
        t2 - t
        );

      DrawFormatString(0,30,white,"%d millisecondsで 全体の処理が 完了",
        t2 - alltime - 2000 //2秒分を引いておく
      );

      ScreenFlip();
      ScreenFlip();

      WaitKey();
    }
  }






End:
  if(fp){
    fclose(fp);
    fp = NULL;
  }
  if(bp){
    free(bp);
    bp = NULL;
  }
  return 0;
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow )
{
  
  ChangeWindowMode(TRUE);

  if( DxLib_Init() == -1 )  // DXライブラリ初期化処理
  {
     return -1;        // エラーが起きたら直ちに終了
  }
  SetDrawScreen(DX_SCREEN_BACK);

  LoadBMPDiv();
  
  DxLib_End();
  

}
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送