direct.cpp

来自「DGen源码最后版本」· C++ 代码 · 共 1,821 行 · 第 1/4 页

CPP
1,821
字号
    v=0.88*(r-y);

    ir=ig=ib=0;

    y=(y*contrast)+brightness;
    u*=umul;
    v*=vmul;

    r=v/0.88+y;
    b=u/0.49+y;
    g=(y - 0.30*r - 0.11*b)/0.59;

    ir=(int)(r*255.0);
    ig=(int)(g*255.0);
    ib=(int)(b*255.0);

    if (ir<0) ir=0; if (ig<0) ig=0; if (ib<0) ib=0;
    if (ir>255) ir=255; if (ig>255) ig=255; if (ib>255) ib=255;

    pal[i*4+0]=ir; pal[i*4+1]=ig; pal[i*4+2]=ib;
  }
  return 0;
}

/*
static int put_in_ddraw_pal(unsigned char pal[1024])
{
  int i;
  if (ddpal==0) return 1;
  for (i=0;i<256;i++)
  {
    ape[i].peRed  =pal[i*4+0];
    ape[i].peGreen=pal[i*4+1];
    ape[i].peBlue =pal[i*4+2];
  }
  ddpal->SetEntries(0,0,256,ape);
  return 0;
}

static make_ddraw_pal(IDirectDraw *pdd)
{
  int i;
  if (pdd->CreatePalette(DDPCAPS_8BIT, ape, &ddpal, NULL)!=DD_OK)
    return 1;
  return 0;
}
*/

long int  PASCAL  MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL InitApplication(HINSTANCE);

/***  Global Variables  ***/
char szName[256];
//HINSTANCE hInst;  // current instance

static int get_megadrive_pads()
{
  int p;
  for (p=0;p<2;p++)
    megad.pad[p]=0x000f303f; // 0x000f303f; = untouched pad

  // Now calculate the sound for the next seg
  if (we_are_active)
  {
    input_update();
    for (p=0;p<2;p++)
    {
      int bse=0,k;
      static const int msk[12]={~1,~2,~4,~8,~0x1000,~0x10,~0x20,~0x2000,
        ~0x40000,~0x20000,~0x10000,~0x80000,};
      if (p==1) bse=16;
      // Putting extra buttons in the top f
      // X=1011 Y=1101 Z=1110 Mode=0111

      for (k=0;k<12;k++)
        if (input_check(pad_code[bse+k])) megad.pad[p]&=msk[k];
    }
  }
  if (medley>0)
  {
    if (medley_record>=0)
    {
      // recording keypress for next music
      if (medley_record>=60*10)
      {
        medley_record=-1;
        medley_beep=2;
        // Start timer
        medley_countdown=medley+60*9; // a sec to press it
      }
      else
      {
        int i;
        for (i=0;i<2;i++)
        {
          if (megad.pad[i]!=0xf303f)
          {
            if (medley_press_mark<=0)
              medley_press_mark=60*10-(medley_record+0);
              // but not medley_had_fire yet - maybe the start

            medley_nothing_after=60*10-(medley_record+1);
          }

          if ((megad.pad[i]&0xf3030)!=0xf3030)
          {
            if (medley_press_mark<=0 || !medley_had_fire)
              // mark where they start the music on the countdown
                medley_press_mark=60*10-(medley_record+0);
            medley_had_fire=1; // definitely the start
          }
        }
        medley_keys[medley_record*2+0]=megad.pad[0];
        medley_keys[medley_record*2+1]=megad.pad[1];
        medley_record++;
      }
    }
    else
    {
      if (medley_countdown<medley_nothing_after)
      {
        // Start timer again
        medley_countdown=medley+60*9; // a sec to press it
      }
      else if (medley_countdown<60*10)
      {
        // Start keying in next music
        int fram;
        fram=(60*10)-1-medley_countdown;
        megad.pad[0]&=medley_keys[fram*2+0];
        megad.pad[1]&=medley_keys[fram*2+1];
      }
      medley_countdown--;
    }
  }
  return 0;
}

static unsigned int frame_count=0;
static unsigned int start_time=0,end_time=0;

static int run_megadrive_without_sound()
{
  static DWORD lasttime=0,thistime;
  static int firsttime=1;
  static int extramsgone=0;
  int msgone,frgone,f;

  thistime=timeGetTime();
  msgone=thistime-lasttime;
  lasttime=thistime;

  if (firsttime) { msgone=0; firsttime=0; }
  msgone+=extramsgone;
  frgone=msgone*virtual_fps/1000;
  extramsgone=msgone-(frgone*1000/virtual_fps);

  if (frgone<0) frgone=0;
  else if (frgone>12) frgone=12;

  for (f=0;f<frgone;f++)
  {
    get_megadrive_pads();
    if (f<frgone-1)
    {
      get_megadrive_pads();
      megad.one_frame(NULL,NULL,NULL);
    }
    else if (mddisp)
    {
      struct bmap sbm;
      sbm=mddisp->lock();
      if (sbm.data!=NULL)
      {
        get_megadrive_pads();
        megad.one_frame(&sbm,NULL,NULL);
        mess_with_bm(&sbm);
        mddisp->unlock();
        mddisp->update(any_size);
        frame_count++;
      }
    }
  }
  return 0;
}


static int run_megadrive_with_sound()
{
  DWORD posPlay,posWrite;
  int read_ptr=0;
  static int write_ptr=0;
  int pic_and_sound=0;
  if (lpds==NULL) return 1;
  if (lpBuf==NULL) return 1;
  if (lpBufPrim==NULL) return 1;

  {
    extern int medley_marker;
    medley_marker=(medley>0);
  }

// Sound buffer loop round a number of segs of size snd_size
// SSSSSSSSS SSSSSSSSS SSSSSSSSS SSSSSSSSS 
//            Playing   Writing

  pic_and_sound=0;
  if (sound_is_okay)
  {
    lpBuf->GetCurrentPosition(&posPlay,&posWrite);
  
    read_ptr=posPlay/(snd_size*(stereo16?4:1));
    while (write_ptr!=read_ptr)
    {
      fill_snd_seg(write_ptr);


  
      write_ptr=(write_ptr+1)%snd_segs;
  
      if (write_ptr!=read_ptr)
      {
        {
          struct sndinfo si;
          si.l=fmbuf[0]; si.r=fmbuf[1];
          si.len=snd_size;
          // Just the sound - don't need the picture yet
          get_megadrive_pads();
          megad.one_frame(NULL,NULL,&si);
        }
      }
      else
        pic_and_sound=1;
    }
  }

  if (pic_and_sound && !dgen_display_on)
  {
    struct sndinfo si;
    si.l=fmbuf[0]; si.r=fmbuf[1];
    si.len=snd_size;
    get_megadrive_pads();
    megad.one_frame(NULL,NULL,&si);
  }

  if (pic_and_sound && dgen_display_on && mddisp)
  {
    // Right - now we need the picture and the sound too
    // Turn the buf surface into something we can write into
    struct bmap sbm;
    sbm=mddisp->lock();
    if (sbm.data!=NULL)
    {
      struct sndinfo si;
      si.l=fmbuf[0]; si.r=fmbuf[1];
      si.len=snd_size;
      get_megadrive_pads();
      megad.one_frame(&sbm,NULL,&si);
      mess_with_bm(&sbm);
      mddisp->unlock();
      mddisp->update(any_size);

      frame_count++;
    }
  }

  return 0;
}

static int run_megadrive()
{
  if (sound_is_okay) return run_megadrive_with_sound();
  return run_megadrive_without_sound();
}

static int do_save_load(int save,int sram,int alert,char *name)
{
  FILE *hand=NULL; int sl;
  if (megad.romfilename[0]==0) return 1;

  // sram==0 Normal RAM, sram==1 Save RAM

  if (sram==1 && !megad.has_save_ram())
  {
    if (alert)
    {
      sprintf (temp+0x4000,"Couldn't %s SRAM since %s doesn't have SRAM.\n",
        save?"save":"load",gst_name(megad.romfilename));
      MessageBox(hWnd,temp+0x4000,
      save?"DGen SRAM Save Warning":"DGen SRAM Load Warning",
      MB_OK);
    }
    return 1;
  }

  if (name==NULL)
  {
    sprintf (temp,"%s\\%.8s.%s",state_path,gst_name(megad.romfilename),
      sram==0?"gsx":"srm");
    name=temp;
  }


  hand=fopen(name,save?"wb":"rb");
  if (hand!=NULL)
  {
    if (sram==0)
    {
      if (save) megad.export_gst(hand);
      else      megad.import_gst(hand);
    }
    if (sram==1)
    {
      if (save) megad.put_save_ram(hand);
      else      megad.get_save_ram(hand);
    }
    fclose(hand);

    if (alert)
    {
      sprintf (temp+0x4000,"Succeeded in %s '%s'\n",save?"save to":"load from",name);
      MessageBox(hWnd,temp+0x4000,
      save?"DGen Saved Okay":"DGen Loaded Okay",
      MB_OK);
    }
  }
  else if (alert)
  {
    sprintf (temp+0x4000,"Couldn't %s '%s'\n%s",
      save?"save to":"load from",name,
      save?"Try making a directory there.\n":""
      );
    MessageBox(hWnd,temp+0x4000,"DGen Save Warning",MB_OK);
  }

  medley=0;
  return 0;
}

static int maybe_auto_save_load(int save)
{
  if (auto_saveload) do_save_load(save,0,0,NULL);
  if (sram_saveload) do_save_load(save,1,0,NULL);
  return 0;
}

static int common_codes[3][32]=
{
  {
    0xc8,0xd0,0xcb,0xcd,
    0x1e,0x1f,0x20,0x1c,
    0x10,0x11,0x12,0x13,
    0x00,0x00,0x00,0x00,
    0x10001,0x10002,0x10003,0x10004,
    0x10010,0x10012,0x10013,0x10011,
    0x10014,0x10015,0x10016,0x10017,
    0x00,0x00,0x00,0x00
  },
  {
    0x10001,0x10002,0x10003,0x10004,
    0x10010,0x10012,0x10013,0x10011,
    0x10014,0x10015,0x10016,0x10017,
    0x00,0x00,0x00,0x00,
    0xc8,0xd0,0xcb,0xcd,
    0x1e,0x1f,0x20,0x1c,
    0x10,0x11,0x12,0x13,
    0x00,0x00,0x00,0x00
  },
  {
    0x10001,0x10002,0x10003,0x10004,
    0x10010,0x10012,0x10013,0x10011,
    0x10014,0x10015,0x10016,0x10017,
    0x00,0x00,0x00,0x00,
    0x10101,0x10102,0x10103,0x10104,
    0x10110,0x10112,0x10113,0x10111,
    0x10114,0x10115,0x10116,0x10117,
    0x00,0x00,0x00,0x00
  }
};
static int common_keys(int type)
{
  if (type>=0 && type<3)
    memcpy(pad_code,common_codes[type],32*sizeof(int));

  return 0;
}

extern int split_screen;
static int update_menu()
{
  HMENU mnu;
  int i;
  mnu=GetSubMenu(GetMenu(hWnd), 1);

  CheckMenuItem(mnu, 0x0104,megad.pause?MF_CHECKED:MF_UNCHECKED);

  CheckMenuItem(mnu, 0x0112,(megad.country_ver&0xff0)==0x80?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0113,(megad.country_ver&0xff0)==0x00?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0114,(megad.country_ver&0xff0)==0xc0?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0115,(megad.country_ver&0xff0)==0x40?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0116,(megad.country_ver&0xff0)==0xff0?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0117,auto_saveload?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0118,sram_saveload?MF_CHECKED:MF_UNCHECKED);

  CheckMenuItem(mnu, 0x0170,virtual_fps==50?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0171,virtual_fps==60?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0172,virtual_fps==70?MF_CHECKED:MF_UNCHECKED);
  for (i=0;i<2;i++)
    CheckMenuItem(mnu, 0x0120+i,(megad.country_ver&0x0f)==i?MF_CHECKED:MF_UNCHECKED);

  CheckMenuItem(mnu, 0x0130,megad.cpu_emu==0?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0131,megad.cpu_emu==1?MF_CHECKED:MF_UNCHECKED);

  mnu=GetMenu(hWnd);
  CheckMenuItem(mnu, 0x0150,fullscreen?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0151,split_screen?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0152,crap_tv?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0153,any_size?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0154,dgen_display_on?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0155,megad.render_method==0?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0156,megad.render_method==1?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0157,megad.render_method==2?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0158,megad.render_method==3?MF_CHECKED:MF_UNCHECKED);

  for (i=0;i<3;i++)
  {
    int match; match=(memcmp(pad_code,common_codes[i],32*sizeof(int))==0);
    CheckMenuItem(mnu, 0x0142+i,match?MF_CHECKED:MF_UNCHECKED);
  }

  for (i=0;i<0x10;i++)
    CheckMenuItem(mnu, 0x0330+i,(megad.layer_sel&(1<<i))?MF_CHECKED:MF_UNCHECKED);

  mnu=GetSubMenu(GetMenu(hWnd), 5);
  CheckMenuItem(mnu, 0x0163, no_sound?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x01b0, megad.mjazz?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0161,!stereo16?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0162, stereo16?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0168,snd_rate==8000?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0169,snd_rate==11025?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x016a,snd_rate==22050?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x016b,snd_rate==44100?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0204,snd_segs==4?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0208,snd_segs==8?MF_CHECKED:MF_UNCHECKED);
  CheckMenuItem(mnu, 0x0220,snd_segs==32?MF_CHECKED:MF_UNCHECKED);

  CheckMenuItem(mnu, 0x0180,megad.snd_mute&1?MF_UNCHECKED:MF_CHECKED);
  CheckMenuItem(mnu, 0x0181,megad.snd_mute&2?MF_UNCHECKED:MF_CHECKED);
  CheckMenuItem(mnu, 0x0182,megad.snd_mute&4?MF_UNCHECKED:MF_CHECKED);

  for (i=0;i<6;i++)
    CheckMenuItem(mnu, 0x0190+i,medley==i*30*60?MF_CHECKED:MF_UNCHECKED);

  for (i=0;i<6;i++)
    CheckMenuItem(mnu, 0x01a0+i,(fm_chan_on&(1<<i))?MF_CHECKED:MF_UNCHECKED);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?