120 likes | 129 Views
Learn about event-driven programming paradigm and its example - event handling and double buffering in C++/CLI. Explore topics like event registration, graphics using GDI+, state representation, and triple buffering.
E N D
Outline • Event-driven programming paradigm • Example: • Event Handling • Class • Array/Collection • Graphics • Double Buffering
Event Handling Source Object Receiver Object H_Func(Event) { } Event Registration/delegate
Source vs. Receiver Source/Receiver Receiver Source Source
Mouse Event Handling in C++/CLI • Event: MouseEventArgs • Mouse Event Handlers: Form1_MouseDown, Form1_MouseUp, Form1_MouseMove • Registration (delegate): • this->MouseDown += gcnew MouseEventHandler(…); • this->MouseUp += gcnew MouseEventHandler(…); • this->MouseMove += gcnew MouseEventHandler(…);
A look at Graphics using GDI+ • GDI: Graphical Device Interface • Paint event is associated with all controls. Triggered when create, resize, restore, etc. • Graphicsclass • PaintEventHandler() • Invalidate() • Manually trigger Paint event
Event-driven Programming • Programming paradigm • State representation • State presentation : Form1_Paint in C++/CLI • Event handling and state change • Request for presentation to reflect new state:Invalidate in C++/CLI
Double Buffering • Flicking problem with single buffering • Double buffering • Front buffer for display • Back buffer for composition • Triple buffering
Single Buffering Display Frame Buffer Composition
Double Buffering in Computer Graphics Display Front Back Composition Swap Buffers Display Back Front Composition
Double Buffering in C++/CLI Window Image Front Back Draw Composition
Double Buffering in C++/CLI • Step 1: Create the back buffer and its graphics • Bitmap^ backBuffer = gcnew Bitmap(width, height); • Graphics^ gBackBuffer = Graphics::FromImage(backBuffer); • Step 2: Compose the scene in the back buffer • gBackBuffer->Clear(Color::White); • gBackBuffer->FillEllipse(…); • .... • Step 3: copy the back buffer to the front • e->Graphics->DrawImageUnscaled(backBuffer, 0, 0);
An Example • Event-driven programming paradigm • Class • Array/Clollection • Event Handling • Double Buffering