Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) SimPredator source code (version 1.0)

 

SimPredator version 1.0 source code.

 

Operating system: Windows XP

IDE: C++ Builder 6.0 Enterprise Edition

Project type: GUI application

Compiler: supplied with C++ Builder 6.0 Enterprise Edition

Libraries used:

 

 

 

 

 

ProjectSimPredator.cpp

 

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("UnitHunting.cpp", FormHunting);
USEFORM("UnitMenu.cpp", FormMenu);
USEFORM("UnitChart.cpp", FormChart);
USEFORM("UnitStartHunt.cpp", FormStartHunt);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        try
        {
                 Application->Initialize();
                 Application->Title = "SimPredator";
                 Application->CreateForm(__classid(TFormMenu), &FormMenu);
                 Application->CreateForm(__classid(TFormStartHunt), &FormStartHunt);
                 Application->CreateForm(__classid(TFormChart), &FormChart);
                 Application->Run();
        }
        catch (Exception &exception)
        {
                 Application->ShowException(&exception);
        }
        catch (...)
        {
                 try
                 {
                         throw Exception("");
                 }
                 catch (Exception &exception)
                 {
                         Application->ShowException(&exception);
                 }
        }
        return 0;
}
//---------------------------------------------------------------------------

 

 

 

 

 

UnitChart.cpp

 

//---------------------------------------------------------------------------
/*
 *  SimPredator. A simple game to demonstrate selection and mutation
 *  Copyright (C) 2006  Richel Bilderbeek
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//---------------------------------------------------------------------------
// The program was developed from 19-05-2006 to 18-07-2006
// The program can be downloaded at http://www.richelbilderbeek.nl
// The author's e-mail adress can be found at http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitChart.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormChart *FormChart;
//---------------------------------------------------------------------------
__fastcall TFormChart::TFormChart(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormChart::Timer1Timer(TObject *Sender)
{
  TextContinue->Enabled = true;
  Timer1->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TFormChart::TextContinueClick(TObject *Sender)
{
  this->ModalResult = mrOk;
  //this->Hide();
}
//---------------------------------------------------------------------------

void __fastcall TFormChart::FormShow(TObject *Sender)
{
  TextContinue->Enabled = false;
  Timer1->Enabled = true;

  #ifdef RICHEL_BILDERBEEK_IS_TESTING
  Timer1->Interval = 100;
  #endif

}
//---------------------------------------------------------------------------



 

 

 

 

 

UnitChart.h

 

//---------------------------------------------------------------------------
/*
 *  SimPredator. A simple game to demonstrate selection and mutation
 *  Copyright (C) 2006  Richel Bilderbeek
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//---------------------------------------------------------------------------
// The program was developed from 19-05-2006 to 18-07-2006
// The program can be downloaded at http://www.richelbilderbeek.nl
// The author's e-mail adress can be found at http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitChartH
#define UnitChartH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Chart.hpp>
#include <ExtCtrls.hpp>
#include <Series.hpp>
#include <TeEngine.hpp>
#include <TeeProcs.hpp>
//---------------------------------------------------------------------------
class TFormChart : public TForm
{
__published: // IDE-managed Components
        TChart *Chart1;
        TFastLineSeries *Series1;
        TFastLineSeries *Series2;
        TFastLineSeries *Series3;
        TFastLineSeries *Series4;
        TFastLineSeries *Series5;
        TFastLineSeries *Series6;
        TFastLineSeries *Series7;
        TFastLineSeries *Series8;
        TStaticText *TextSimPredator;
        TStaticText *TextContinue;
        TTimer *Timer1;
        void __fastcall Timer1Timer(TObject *Sender);
        void __fastcall TextContinueClick(TObject *Sender);
        void __fastcall FormShow(TObject *Sender);
private: // User declarations
public: // User declarations
        __fastcall TFormChart(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormChart *FormChart;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

UnitHunting.cpp

 

//---------------------------------------------------------------------------
/*
 *  SimPredator. A simple game to demonstrate selection and mutation
 *  Copyright (C) 2006  Richel Bilderbeek
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//---------------------------------------------------------------------------
// The program was developed from 19-05-2006 to 18-07-2006
// The program can be downloaded at http://www.richelbilderbeek.nl
// The author's e-mail adress can be found at http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitHunting.h"
//---------------------------------------------------------------------------
#include <Assert.hpp>
#include <mmsystem.hpp>
#include <math>
#include <boost/timer.hpp>
#include "UnitChart.h"
#include "UnitStartHunt.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormHunting *FormHunting;
//---------------------------------------------------------------------------
__fastcall TFormHunting::TFormHunting(
    TComponent* Owner,
    const int& backgroundColor,
    const double& mutationRate)
  : TForm(Owner),
  mPrey(std::vector<Prey>(100)),
  #ifdef RICHEL_BILDERBEEK_IS_TESTING
    mHuntingTime(1),
  #else
    mHuntingTime(20), //Originally: 30
  #endif
  mPreyMax(100),
  mRound(0),
  mMutationRate(mutationRate)
{
  mClientWidth = Screen->Width;
  mClientHeight = Screen->Height;
  ImageBackground->Picture->Bitmap->Width  = mClientWidth;
  ImageBackground->Picture->Bitmap->Height = mClientHeight;
  ImageBuffer->Picture->Bitmap->Width  = mClientWidth;
  ImageBuffer->Picture->Bitmap->Height = mClientHeight;
  fillImage(ImageBackground, backgroundColor);

  for (int i=0; i<mPreyMax; ++i)
  {
    mPrey[i].mX = std::rand()%(mClientWidth - FormStartHunt->ImageList1->Width );
    mPrey[i].mY = std::rand()%(mClientHeight- FormStartHunt->ImageList1->Height);
    mPrey[i].mIndex = i%FormStartHunt->mNprey;
  }
  scorePrey();

  TimerStart->Enabled = true;
  TimerStart->OnTimer(0);

}
//---------------------------------------------------------------------------
void __fastcall TFormHunting::TimerStartTimer(TObject *Sender)
{
  ++TimerStart->Tag;
  Panel1->Visible = true;
  Panel1->Caption = "Only " + IntToStr(3 - TimerStart->Tag) + " second" + (3-TimerStart->Tag>1 ? "s" : "") +" before hunting...";
  if (TimerStart->Tag==3)
  {
    TimerStart->Tag = 0;
    TimerStart->Enabled = false;
    TimerGameDraw->Enabled = true;
    TimerGameTime->Enabled = true;
    Panel1->Visible = false;
    //rmChart->Visible = false;
    drawScreen();
  }
}
//---------------------------------------------------------------------------
void __fastcall TFormHunting::TimerGameDrawTimer(TObject *Sender)
{
  drawScreen();
}
//---------------------------------------------------------------------------
void __fastcall TFormHunting::TimerGameTimeTimer(TObject *Sender)
{
  ++TimerGameTime->Tag;
  //Caption = "SimPredator - Only " + IntToStr(mHuntingTime - TimerGameTime->Tag)
  //  + " seconds hunting left, " + IntToStr(mPrey.size()) + " prey left";
  if (TimerGameTime->Tag==mHuntingTime  || mPrey.size() < 25)
  {
    TimerGameTime->Tag=0;
    TimerGameDraw->Tag=0;
    TimerGameTime->Enabled = false;
    TimerGameDraw->Enabled = false;
    TimerReproduce->Enabled = false;
    TimerStart->Enabled = false;
    //Increment round
    ++mRound;
    if (mRound==5)
    {
      Panel1->Caption = "Finished hunting successfully!";
      Panel1->Refresh();
      //Sleep(3000); //DOES NOT WORK ANYMORE?!?!?
    }
    //Show prey chart
    scorePrey();
    FormChart->ShowModal();

    //while(FormChart->Visible==true)
    //{
    //  Application->ProcessMessages();
    //}
    //Finished?
    if (mRound==5) Close();

    TimerReproduce->Enabled = true;
    Panel1->Visible = true;
    Panel1->Caption = "Prey is reproducing...";
  }


}
//---------------------------------------------------------------------------
void __fastcall TFormHunting::TimerReproduceTimer(TObject *Sender)
{
  if (static_cast<int>(mPrey.size()) < mPreyMax)
  {
    const int i = std::rand() % mPrey.size();
    const int width  = FormStartHunt->ImageList1->Width;
    const int height = FormStartHunt->ImageList1->Height;
    const int nPrey = FormStartHunt->mNprey;

    Prey prey = mPrey[i];
    prey.mX += (std::rand()%(width+width))-width;
    prey.mY += (std::rand()%(height+height))-height;
    if (uniform() < mMutationRate)
    {
      prey.mIndex = std::rand()%nPrey;
    }

    mPrey.push_back(prey);
    drawScreen();
  }
  else
  {
    //Shuffle prey
    for (int i=0; i<mPreyMax; ++i)
    {
      mPrey[i].mX = std::rand() % (mClientWidth  - ImagePrey->Width );
      mPrey[i].mY = std::rand() % (mClientHeight - ImagePrey->Height);
    }
    /*
    //Increment round
    ++mRound;
    if (mRound==5)
    {
      Panel1->Caption = "Finished hunting successfully!";
      Panel1->Refresh();
      Sleep(3000);
    }
    //Show prey chart
    FormChart->Show();
    //Finished?
    if (mRound==5) Close();
    */
    //Continue
    TimerReproduce->Enabled = false;
    TimerStart->Enabled = true;


  }

}
//---------------------------------------------------------------------------
void TFormHunting::drawScreen()
{
  ImageBuffer->Canvas->Draw(0,0,ImageBackground->Picture->Graphic);

  const int nPrey = mPrey.size();
  for (int i=0; i<nPrey; ++i)
  {
    FormStartHunt->ImageList1->Draw(ImageBuffer->Canvas,mPrey[i].mX,mPrey[i].mY,mPrey[i].mIndex);
  }

  Canvas->Draw(0,0,ImageBuffer->Picture->Graphic);

}
//---------------------------------------------------------------------------
void __fastcall TFormHunting::FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  if (Panel1->Visible == true) return;

  static std::vector<Prey>::iterator iter;
  const int nPrey = mPrey.size();
  if (nPrey < FormStartHunt->mNprey) return;
  for (int i=nPrey-1; i>-1; --i)
  {
    if (
         X > mPrey[i].mX
      && Y > mPrey[i].mY
      && X < mPrey[i].mX + FormStartHunt->ImageList1->Width
      && Y < mPrey[i].mY + FormStartHunt->ImageList1->Height
      )
    { //Prey found!
      iter = &mPrey[i];
      // = find(myVector.begin(),myVector.end(),myElement);
      //if (i==myVector.end()) break;
      mPrey.erase(iter);

      //mPrey[i] = mPrey.back();
      //mPrey.pop_back();
      drawScreen();
      return;
    }
  }
  drawScreen();
}
//---------------------------------------------------------------------------
void TFormHunting::scorePrey()
{
  const int nPrey = FormStartHunt->mNprey;
  const int maxPreyIndex = this->mPrey.size();
  std::vector<int> histo(nPrey,0);
  for (int i=0; i<maxPreyIndex; ++i)
  {
    ++histo[ mPrey[i].mIndex ];
  }
  for (int i=0; i<nPrey; ++i)
  {
    FormChart->Chart1->Series[i]->AddY(histo[i]);
  }
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

void __fastcall TFormHunting::FormCreate(TObject *Sender)
{
  const TCursor myCursor = static_cast<TCursor>(22);
  Screen->Cursors[myCursor] = LoadCursorFromFile("CursorSkull.cur");
  Cursor = myCursor;
  Screen->Cursor = myCursor;
  this->Cursor = myCursor;
  Panel1->Cursor = myCursor;
  FormChart->Chart1->Cursor = myCursor;
}
//---------------------------------------------------------------------------
void fillImage(TImage * image, const int& color)
{
  const char red = color%(256*256);
  const char green = color/(256)%256;
  const char blue = color/(256*256);

  //assert(image!=0);
  //assert(image->AutoSize==true);
  const int width = image->Width;
  const int height = image->Height;
  for (int y=0; y<height; ++y)
  {
    unsigned char * line=static_cast<unsigned char *>(image->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x<width; ++x)
    {
      line[x*3+0] = blue;  //Blue
      line[x*3+1] = green; //Green
      line[x*3+2] = red;   //Red
    }
  }
}
//---------------------------------------------------------------------------
double uniform()
{
  return static_cast<double>(std::rand())/static_cast<double>(RAND_MAX);
}
//---------------------------------------------------------------------------


 

 

 

 

 

UnitHunting.h

 

//---------------------------------------------------------------------------
/*
 *  SimPredator. A simple game to demonstrate selection and mutation
 *  Copyright (C) 2006  Richel Bilderbeek
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//---------------------------------------------------------------------------
// The program was developed from 19-05-2006 to 18-07-2006
// The program can be downloaded at http://www.richelbilderbeek.nl
// The author's e-mail adress can be found at http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitHuntingH
#define UnitHuntingH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <ImgList.hpp>
#include <Chart.hpp>
#include <Series.hpp>
#include <TeEngine.hpp>
#include <TeeProcs.hpp>
//---------------------------------------------------------------------------
#include <vector>
//---------------------------------------------------------------------------
struct Prey
{
  int mX, mY, mIndex;
};
//---------------------------------------------------------------------------
class TFormHunting : public TForm
{
__published: // IDE-managed Components
        TImage *ImageBuffer;
        TImage *ImageBackground;
        TPanel *Panel1;
        TTimer *TimerStart;
        TTimer *TimerGameDraw;
        TTimer *TimerReproduce;
        TImage *ImagePrey;
        TTimer *TimerGameTime;
        void __fastcall TimerStartTimer(TObject *Sender);
        void __fastcall TimerGameDrawTimer(TObject *Sender);
        void __fastcall TimerGameTimeTimer(TObject *Sender);
        void __fastcall TimerReproduceTimer(TObject *Sender);
        void __fastcall FormMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y);
        void __fastcall FormCreate(TObject *Sender);
private: // User declarations
  std::vector<Prey> mPrey;
  void drawScreen();
  void scorePrey();
  int mRound;
  int mClientWidth;
  int mClientHeight;
public: // User declarations
  __fastcall TFormHunting(
    TComponent* Owner,
    const int& backgroundColor,
    const double& mutationRate);
  const int mHuntingTime;
  const int mPreyMax;
  const double mMutationRate;


};
//---------------------------------------------------------------------------
extern PACKAGE TFormHunting *FormHunting;
//---------------------------------------------------------------------------
void fillImage(TImage * image, const int& color);
double uniform();

#endif

 

 

 

 

 

UnitMenu.cpp

 

//---------------------------------------------------------------------------
/*
 *  SimPredator. A simple game to demonstrate selection and mutation
 *  Copyright (C) 2006  Richel Bilderbeek
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//---------------------------------------------------------------------------
// The program was developed from 19-05-2006 to 18-07-2006
// The program can be downloaded at http://www.richelbilderbeek.nl
// The author's e-mail adress can be found at http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop


#include "UnitMenu.h"
//---------------------------------------------------------------------------
#include <memory>
#include "UnitStartHunt.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMenu *FormMenu;
//---------------------------------------------------------------------------
__fastcall TFormMenu::TFormMenu(TComponent* Owner)
  : TForm(Owner),
  mPlayedHunt1(0)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMenu::TextAboutClick(TObject *Sender)
{
  ImagePredatorLeft->Visible  = !ImagePredatorLeft->Visible;
  ImagePredatorLeft->Align = (ImagePredatorLeft->Align==alNone ? alLeft : alNone);
  ImagePredatorRight->Visible = !ImagePredatorRight->Visible;
  ImagePredatorRight->Align = (ImagePredatorRight->Align==alNone ? alRight : alNone);
  ImageRichelLeft->Visible  = !ImageRichelLeft->Visible;
  ImageRichelLeft->Align = (ImageRichelLeft->Align==alNone ? alLeft : alNone);
  ImageRichelRight->Visible = !ImageRichelRight->Visible;
  ImageRichelRight->Align = (ImageRichelRight->Align==alNone ? alRight : alNone);
  RichEditAbout->Visible = !RichEditAbout->Visible;
  TextAbout->Caption = (TextAbout->Caption=="About" ? "Not about" : "About");
}
//---------------------------------------------------------------------------
void __fastcall TFormMenu::TextStartHunt1Click(TObject *Sender)
{
  ++mPlayedHunt1;
  FormStartHunt->setHunt(1);
  FormStartHunt->ShowModal();
  if (mPlayedHunt1==3) TextStartHunt2->Enabled = true;
}
//---------------------------------------------------------------------------


void __fastcall TFormMenu::TextQuitClick(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------

void __fastcall TFormMenu::TextStartHunt2Click(TObject *Sender)
{
  FormStartHunt->setHunt(2);
  FormStartHunt->ShowModal();
  if (mPlayedHunt1==3) TextStartHunt2->Enabled = true;

}
//---------------------------------------------------------------------------


 

 

 

 

 

UnitMenu.h

 

//---------------------------------------------------------------------------
/*
 *  SimPredator. A simple game to demonstrate selection and mutation
 *  Copyright (C) 2006  Richel Bilderbeek
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//---------------------------------------------------------------------------
// The program was developed from 19-05-2006 to 18-07-2006
// The program can be downloaded at http://www.richelbilderbeek.nl
// The author's e-mail adress can be found at http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitMenuH
#define UnitMenuH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <jpeg.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TFormMenu : public TForm
{
__published: // IDE-managed Components
        TStaticText *TextSimPredator;
        TStaticText *TextStartHunt1;
        TStaticText *TextQuit;
        TStaticText *TextStartHunt2;
        TStaticText *TextAbout;
        TPanel *PanelMenu;
        TImage *ImagePredatorLeft;
        TImage *ImagePredatorRight;
        TImage *ImageRichelLeft;
        TImage *ImageRichelRight;
        TRichEdit *RichEditAbout;
        void __fastcall TextAboutClick(TObject *Sender);
        void __fastcall TextStartHunt1Click(TObject *Sender);
        void __fastcall TextQuitClick(TObject *Sender);
        void __fastcall TextStartHunt2Click(TObject *Sender);
private: // User declarations
  int mPlayedHunt1;
public: // User declarations
        __fastcall TFormMenu(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormMenu *FormMenu;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

UnitStartHunt.cpp

 

//---------------------------------------------------------------------------
/*
 *  SimPredator. A simple game to demonstrate selection and mutation
 *  Copyright (C) 2006  Richel Bilderbeek
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//---------------------------------------------------------------------------
// The program was developed from 19-05-2006 to 18-07-2006
// The program can be downloaded at http://www.richelbilderbeek.nl
// The author's e-mail adress can be found at http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitStartHunt.h"
//---------------------------------------------------------------------------
#include <vector>
#include <memory>
#include <algorithm>
#include <assert.hpp>
#include "UnitHunting.h"
#include "UnitChart.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormStartHunt *FormStartHunt;
//---------------------------------------------------------------------------
__fastcall TFormStartHunt::TFormStartHunt(TComponent* Owner)
  : TForm(Owner),
  mPreyWidth(48),
  mPreyHeight(48),
  mNprey(8),
  mPreyColor0(0x000000),
  mPreyColor1(0xFF0000),
  mPreyColor2(0x00FF00),
  mPreyColor3(0x0000FF),
  mPreyColor4(0xFFFF00),
  mPreyColor5(0x00FFFF),
  mPreyColor6(0xFF00FF),
  mPreyColor7(0xFFFFFF),
  mBackgroundColor0(0x080808),
  mBackgroundColor1(0xF80808),
  mBackgroundColor2(0x08F808),
  mBackgroundColor3(0x0808F8),
  mBackgroundColor4(0xF8F808),
  mBackgroundColor5(0x08F8F8),
  mBackgroundColor6(0xF808F8),
  mBackgroundColor7(0xF8F8F8),
  mHunt(0)
{
  ImageList1->Width  = mPreyWidth;
  ImageList1->Height = mPreyHeight;


  //Get a vector of images
  std::vector<TImage*> images(mNprey*2); //*2 as there are as many backgrounds
  images[ 0] = Image0;
  images[ 1] = Image1;
  images[ 2] = Image2;
  images[ 3] = Image3;
  images[ 4] = Image4;
  images[ 5] = Image5;
  images[ 6] = Image6;
  images[ 7] = Image7;
  images[ 8] = Image8;
  images[ 9] = Image9;
  images[10] = Image10;
  images[11] = Image11;
  images[12] = Image12;
  images[13] = Image13;
  images[14] = Image14;
  images[15] = Image15;

  //Resize the images
  for (int i=0; i<mNprey*2; ++i)
  {
    images[i]->Picture->Bitmap->Width  = mPreyWidth;
    images[i]->Picture->Bitmap->Height = mPreyHeight;
  }

  //Fill the images
  fillImage(Image0, mPreyColor0);
  fillImage(Image1, mPreyColor1);
  fillImage(Image2, mPreyColor2);
  fillImage(Image3, mPreyColor3);
  fillImage(Image4, mPreyColor4);
  fillImage(Image5, mPreyColor5);
  fillImage(Image6, mPreyColor6);
  fillImage(Image7, mPreyColor7);
  //Fill the background images
  fillImage(Image8 , mBackgroundColor0);
  fillImage(Image9 , mBackgroundColor1);
  fillImage(Image10, mBackgroundColor2);
  fillImage(Image11, mBackgroundColor3);
  fillImage(Image12, mBackgroundColor4);
  fillImage(Image13, mBackgroundColor5);
  fillImage(Image14, mBackgroundColor6);
  fillImage(Image15, mBackgroundColor7);

  ImageList1->AddMasked(Image0->Picture->Bitmap,clMoneyGreen);
  ImageList1->AddMasked(Image1->Picture->Bitmap,clMoneyGreen);
  ImageList1->AddMasked(Image2->Picture->Bitmap,clMoneyGreen);
  ImageList1->AddMasked(Image3->Picture->Bitmap,clMoneyGreen);
  ImageList1->AddMasked(Image4->Picture->Bitmap,clMoneyGreen);
  ImageList1->AddMasked(Image5->Picture->Bitmap,clMoneyGreen);
  ImageList1->AddMasked(Image6->Picture->Bitmap,clMoneyGreen);
  ImageList1->AddMasked(Image7->Picture->Bitmap,clMoneyGreen);
}
//---------------------------------------------------------------------------
void fillImage(TImage * image, const int& color)
{
  const char red = color%(256*256);
  const char green = color/(256)%256;
  const char blue = color/(256*256);

  //assert(image!=0);
  //assert(image->AutoSize==true);
  const int width = image->Width;
  const int height = image->Height;
  for (int y=0; y<height; ++y)
  {
    unsigned char * line=static_cast<unsigned char *>(image->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x<width; ++x)
    {
      line[x*3+0] = blue;  //Blue
      line[x*3+1] = green; //Green
      line[x*3+2] = red;   //Red
    }
  }
}
//---------------------------------------------------------------------------
void TFormStartHunt::setHunt(const int& hunt)
{
  Assert(hunt==1 || hunt==2);
  TextHunt->Caption = "Hunt #" + IntToStr(hunt);
  TextSubtitle->Caption = (hunt==1 ? "Practice hunting" : "Hunting on mutating prey");
  mHunt = hunt;
}
//---------------------------------------------------------------------------
void __fastcall TFormStartHunt::ImageBackgroundClick(TObject *Sender)
{
  int backgroundColor = 0;
  if (Sender == Image8 ) backgroundColor = mBackgroundColor0;
  if (Sender == Image9 ) backgroundColor = mBackgroundColor1;
  if (Sender == Image10) backgroundColor = mBackgroundColor2;
  if (Sender == Image11) backgroundColor = mBackgroundColor3;
  if (Sender == Image12) backgroundColor = mBackgroundColor4;
  if (Sender == Image13) backgroundColor = mBackgroundColor5;
  if (Sender == Image14) backgroundColor = mBackgroundColor6;
  if (Sender == Image15) backgroundColor = mBackgroundColor7;

  Assert(mHunt!=0);
  std::auto_ptr<TFormHunting> formHunt(new TFormHunting(
    this,
    backgroundColor,
    (mHunt==1 ? 0.0 : 0.1) ));
  formHunt->ShowModal();
  Close();
}
//---------------------------------------------------------------------------


void __fastcall TFormStartHunt::FormPaint(TObject *Sender)
{
  for (int i=0; i<mNprey; ++i)
  {
    FormChart->Chart1->Series[i]->Clear();
  }
  FormChart->Chart1->Series[0]->SeriesColor = static_cast<TColor>(mPreyColor0);
  FormChart->Chart1->Series[1]->SeriesColor = static_cast<TColor>(mPreyColor1);
  FormChart->Chart1->Series[2]->SeriesColor = static_cast<TColor>(mPreyColor2);
  FormChart->Chart1->Series[3]->SeriesColor = static_cast<TColor>(mPreyColor3);
  FormChart->Chart1->Series[4]->SeriesColor = static_cast<TColor>(mPreyColor4);
  FormChart->Chart1->Series[5]->SeriesColor = static_cast<TColor>(mPreyColor5);
  FormChart->Chart1->Series[6]->SeriesColor = static_cast<TColor>(mPreyColor6);
  FormChart->Chart1->Series[7]->SeriesColor = static_cast<TColor>(mPreyColor7);
        
}
//---------------------------------------------------------------------------


 

 

 

 

 

UnitStartHunt.h

 

//---------------------------------------------------------------------------
/*
 *  SimPredator. A simple game to demonstrate selection and mutation
 *  Copyright (C) 2006  Richel Bilderbeek
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//---------------------------------------------------------------------------
// The program was developed from 19-05-2006 to 18-07-2006
// The program can be downloaded at http://www.richelbilderbeek.nl
// The author's e-mail adress can be found at http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitStartHuntH
#define UnitStartHuntH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <ImgList.hpp>
//---------------------------------------------------------------------------
class TFormStartHunt : public TForm
{
__published: // IDE-managed Components
        TStaticText *TextHunt;
        TRichEdit *RichEdit1;
        TImageList *ImageList1;
        TPanel *PanelPreyHolder;
        TPanel *PanelPrey;
        TImage *Image0;
        TImage *Image1;
        TImage *Image2;
        TImage *Image3;
        TImage *Image4;
        TImage *Image5;
        TImage *Image6;
        TImage *Image7;
        TRichEdit *RichEdit2;
        TPanel *PanelBackgroundHolder;
        TPanel *PanelBackground;
        TImage *Image8;
        TImage *Image9;
        TImage *Image10;
        TImage *Image11;
        TImage *Image12;
        TImage *Image13;
        TImage *Image14;
        TImage *Image15;
        TStaticText *TextSubtitle;
        void __fastcall ImageBackgroundClick(TObject *Sender);
        void __fastcall FormPaint(TObject *Sender);
private: // User declarations
  int mHunt;
public: // User declarations
  const int mPreyWidth;
  const int mPreyHeight;
  const int mNprey;
  const int mPreyColor0;
  const int mPreyColor1;
  const int mPreyColor2;
  const int mPreyColor3;
  const int mPreyColor4;
  const int mPreyColor5;
  const int mPreyColor6;
  const int mPreyColor7;
  const int mBackgroundColor0;
  const int mBackgroundColor1;
  const int mBackgroundColor2;
  const int mBackgroundColor3;
  const int mBackgroundColor4;
  const int mBackgroundColor5;
  const int mBackgroundColor6;
  const int mBackgroundColor7;
  void setHunt(const int& hunt);

        __fastcall TFormStartHunt(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormStartHunt *FormStartHunt;
//---------------------------------------------------------------------------
void fillImage(TImage * image, const int& color);
//---------------------------------------------------------------------------

#endif

 

 

 

 

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict