Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFC Concurrency using OnDraw()

I'm building Conway's game of life in MFC. I built it before using Allegro and want to try it out using MFC. So I start migrating the algorithms and stuff. It renders well using one core and it does the game of life algorithm rather beautiful just like in allegro. Conway's game of life is made up of little rectangle drawn using pDC->rectangle(x1, y1, x2, y2).

My problem is actually two, one of it is that everytime I call the pDC->rectangle(...) it draws it immideately, causing this chain reaction rendering style. I want it to display when it is done doing its job(i know you can do that in directx but I just want to do this using device context of MFC).

The other problem is the title, and probably my main problem. How do you using pDC with concurrency, I tried it and it did some weird stuff. I know I cant use pDC in OnDraw with concurrency because it is the same device context occupying the same memory but used in my 6 core. That's all, thanks in advance.

like image 434
Joey Arnold Andres Avatar asked Mar 31 '26 12:03

Joey Arnold Andres


1 Answers

To avoid drawing immediately, you can draw in abitmap in memory and then blit on the real dc when you finished. Have a look here as a starting point, or at this article on CodeProject. For the multithreading part, you should be able to use the same in memory device Context from multiple threads, just ensure to properly coordinate the creattion of that sharted DC and the blitting/release.

like image 196
Felice Pollano Avatar answered Apr 03 '26 00:04

Felice Pollano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!