Thuis
Contacten

    Hoofdpagina


Programmeren in C++ voor beginners

Dovnload 5.04 Mb.

Programmeren in C++ voor beginners



Pagina36/49
Datum25.10.2017
Grootte5.04 Mb.

Dovnload 5.04 Mb.
1   ...   32   33   34   35   36   37   38   39   ...   49

Volgende week


Double-buffering voor vloeiend uitziende applicaties. En we leren onze eigen data types maken! Daar bovenop leren we nog iets meer STL functies en onze eigen template functies te maken.
    1. Code PompebledRainer




      1. Unit1.h

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

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------#include

#include

#include

#include

#include

#include

#include

//---------------------------------------------------------//Richel's #includes

#include

//---------------------------------------------------------class TForm1 : public TForm

{

__published: // IDE-managed Components

TImage *ImagePompebled;

TImage *ImageBackground;

TTimer *Timer1;



void __fastcall FormResize(TObject *Sender);

void __fastcall Timer1Timer(TObject *Sender);

private: // User declarations

std::vector<int> mX; //X coordinaat

std::vector<int> mY; //Y coordinaat

std::vector<int> mDx; //Delta x, snelheid in x

std::vector<int> mDy; //Delta y, snelheid in y

public: // User declarations

__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------extern PACKAGE TForm1 *Form1;



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

void PaintAll(TImage * const image,

const char red,

const char green,

const char blue );
#endif

      1. Unit1.cpp

//---------------------------------------------------------#include

#pragma hdrstop

#include "Unit1.h"

//---------------------------------------------------------#pragma package(smart_init)

#pragma resource "*.xfm"

TForm1 *Form1;

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

//Richel's #includes

#include

//---------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

const int n = 10; //Aantal images

mX.resize(n);

mY.resize(n);

mDx.resize(n);

mDy.resize(n);
for (int i=0; i!=n; ++i)

{

mX[i] = (i*70) % Form1->ClientWidth;



mY[i] = (i*90) % Form1->ClientHeight;

mDx[i] = i+1;

mDy[i] = i+1;

}
ImageBackground->Visible = false;

ImagePompebled->Visible = false;
ImageBackground->AutoSize = true;

ImagePompebled->AutoSize = true;


ImagePompebled->Transparent = true;
Caption = "De Pompebled Rainer";
this->Resize(); //Roept de OnResize Event aan

}

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



void __fastcall TForm1::FormResize(TObject *Sender)

{

ImageBackground->Picture->Bitmap->Width = ClientWidth;



ImageBackground->Picture->Bitmap->Height = ClientHeight;

PaintAll(ImageBackground,255,255,255);

}

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



void __fastcall TForm1::Timer1Timer(TObject *Sender)

{

const int n = mX.size();



const int maxx = Form1->ClientWidth;

const int maxy = Form1->ClientHeight;
//Bitblit de achtergrond

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


for (int i=0; i!=n; ++i)

{

//Beweeg de coordinaten



mX[i]+=mDx[i];

mY[i]+=mDy[i];

//Hou de coordinaten in het scherm

if (mX[i] > maxx) mX[i]=0;

if (mY[i] > maxy) mY[i]=0;

//Bitblit de plaatjes

Canvas->Draw(

mX[i], //X coordinaat

mY[i], //Y coordinaat

ImagePompebled->Picture->Graphic);

}

//Form1->Refresh(); //Niet nodig in dit geval



}

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



void PaintAll(TImage * const image,

const char red,

const char green,

const char blue )

{

assert(image!=0);



assert(image->Picture->Bitmap != 0);

assert(image->Picture->Bitmap->PixelFormat == pf32bit);


const int maxx = image->Picture->Bitmap->Width;

const int maxy = image->Picture->Bitmap->Height;

for (int y = 0; y != maxy; ++y)

{

unsigned char * const myLine

= static_cast<unsigned char*>(

image->Picture->Bitmap->ScanLine[y]);



for (int x = 0; x != maxx; ++x)

{

myLine[x*4+2] = red ; //Red



myLine[x*4+1] = green; //Green

myLine[x*4+0] = blue ; //Blue



}

}

}



//---------------------------------------------------------
  1. Dag 6




1   ...   32   33   34   35   36   37   38   39   ...   49

  • Unit1.h
  • Unit1.cpp
  • Dag 6

  • Dovnload 5.04 Mb.