Sign in to follow this  

New to MFC, making a tile editor

This topic is 4204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys. I need to learn MFC for my job, and I chose to make a simple tile editor for practice. It's just going to be a 2D grid with four possible tiles (grass, water, dirt, stone). I have a CSplitterWnd that separates the toolbar from the editable area. The toolbar is a CFormView-derived class, and I need to derive a class from CView that will allow users to easily edit the document. I'm thinking it should have a grid of cells, much like a spreadsheet, but of fixed size (64x64, perhaps), so that the user can simply select the right "brush" (grass, dirt, etc.), and then click away. Do you think I should do this by getting a device context and drawing my own gridlines? Or is there a better way, perhaps a cell view that has already been provided by the sdk? Thanks. No need to post a whole lot of code for me, but if a more-experienced person can help point me in the right direction, that would be awesome.

Share this post


Link to post
Share on other sites
I'm unaware of any such object. Nothing wrong with using a few LineTo()'s to draw your grid in whatever view you want to do your work in. You could also popup a new window and do some work in there. It'd just be your prefrence for how you want your tool to work and what you want to learn.

If you already have your seperate views and are having no trouble getting them to communicate with each other, then you got the stuff figured out that I always have the worst time with.

Share this post


Link to post
Share on other sites
Thanks. I decided to go for the manual drawing option. When the View's OnDraw method is called, I just get a device context for the view and I start drawing. I have a bitmap that represents an empty square, and then I have a bitmap for each of the tiles.

I am having a problem though. In order to do this drawing, I create a device context to represent the view:

CDC * viewDC = GetDC();

Then, I create a memory device context to hold the bitmap:

CDC * memDC = new CDC;
memDC->CreateCompatibleDC(viewDC);

I also have a CBitmap object which holds the bitmaps, and I select the bitmaps in and out of the memDC, and I blit the memDC to the viewDC whenever I want to draw a tile to the view. When I'm done drawing tiles, I called ReleaseDC():

ReleaseDC(viewDC);
ReleaseDC(memDC);

Now, I run the program and debug mode and it seems to work great. Whenever I click on one of the tiles in the grid, that tile changes from the blank tile to the water tile, just as I programmed it to do (I haven't added the ability to select which tile to use yet so it's always just water). The OnLButtonDown() function of my CView-derived class called OnDraw(). So basically, every time you click on a tile to change it, it redraws everything.

Now, that's fine with me because it's plenty fast. However, when I close the program, the debugger alerts me to several memory leaks: one for each device context I created. Why is it doing that? Shouldn't ReleaseDC() properly delete the device context? Just in case, I tried deleting the CDC pointers I created after calling ReleaseDC(), but I got ASSERTS failing every time I tried that.

Share this post


Link to post
Share on other sites
Update: I was able to work around this by making memDC a non-pointer, and by getting rid of viewDC altogether (I just replaced all instances of viewDC with GetDC). However, I am still unsure as to why my previous code had memory leaks.

Share this post


Link to post
Share on other sites

This topic is 4204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this