Saving map for WM_PAINT

Started by
1 comment, last by ontheheap 19 years, 6 months ago
I've got a really weird problem which I've been working on for a couple of hours and I still can't figure out what's going on. Basically, I have a structure used to store information about tiles when they are placed on the map:

struct SAVED_BG_TILES {
int xpos;  // top left x coordinate
int ypos;  // top left y coordinate 

void * bmp; // will eventually point to an HBITMAP stored in a vector
};

SAVED_BG_TILES *saved_bg_tiles;

When a tile is put on the map (more precisely, when WM_LBUTTONDOWN gets called for the main window), I'm doing this (assume saved_bg_tiles has been allocated to hold the number of tiles on the map): saved_bg_tiles[(TILE_WIDTH*correctX)+correctY].bmp = currentlySelectedTile; correctX and correctY are found like this: int correctX = hwndMouseX / TILE_WIDTH; int correctY = hwndMouseY / TILE_HEIGHT; Now, when the map needs to be repainted this is the code I'm using to redraw all of the tiles onto the map:

for(int i = 0; i < TILES_WIDE*TILES_HIGH; i++)
{
     hdc = GetDC(hwnd);
     HDC image_hdc = CreateCompatibleDC(hdc);
     SelectObject(image_hdc,saved_bg_tiles.bmp);

   BitBlt(hdc,saved_bg_tiles.xpos,saved_bg_tiles.ypos,TILE_WIDTH,TILE_HEIGHT,image_hdc,0,0,SRCCOPY);       
                
     ReleaseDC(hwnd,hdc);
     DeleteDC(image_hdc);
} 

I thought that this would draw all of the bitmaps on the correct spots on the map, but it doesn't. Well it does and this is why it's weird. It only redraws a selection of the tiles on the map. Here's a couple of screenshots to show what I mean: Before the screen needs to be redrawn:
after it gets redrawn:

I cannot figure out why it would be doing this! Any ideas?
Advertisement
Not sure if this is it but here goes, when WM_PAINT is called it doesn't always redraw the whole window, rather what has changed since the last call (somewhat dependant on how you draw too). This is what might be happening with your drawing. To fix it you can try putting an 'InvalidateRect(NULL);' in WM_PAINT before you draw anything and just for safety a 'ValidateRect(NULL);' at the end of all your drawing (to make sure windows doesn't generate extra WM_PAINT messages after you have drawn, until something is changed). What InvalidateRect does is when called with NULL basically says everthing is invalid (the whole window), ValidateRect(NULL) will validate the whole window.

HTH
Thanks for the reply. Actually, I'm using RedrawWindow(hwnd,NULL,NULL,RDW_UPDATENOW); to make sure the window gets redrawn. The lines are redrawn every single time as well (in the same function which is Draw_Map()) so I'm pretty sure everything is being painted.

Draw_Map() gets called whenever a WM_PAINT message is sent because all of the drawing is actually done in Draw_Map. That way the map stays current regardless of what I do with the window.

It just isn't making sense to me why only a certain part of the grid is being stored in my saved_bg_tiles array. The way the array is set up is that the array elements corispond with the x,y position of the grid boxes. So if I want to change what's located in grid box 5,3 for example, bmp for that particular array element is changed to the currently selected tile.

I've been away from it for a couple hours now so maybe I'll find something I missed before.


Edit:
OK, my math is way wrong. TILE_WIDTH*currentY + currentX is giving me way too big numbers. Hrm... like if I have a 10x10 map with 32x32 tiles, then (32*7)+3 is _way_ bigger than my 100 element array. Must be another way to do this.

Edit 2:

Ahahaha! Walking away from a problem always makes the answer easier to find! Or does it make the problem easier to find... oh well who cares, point is it's working now. Well, almost working. I'm now using currentX * currentY instead of the other "formula." It still has some problems due to the fact that if x == 0 it gets screwed up so i just need to test for that.

nevermind, still not working right =(

[Edited by - ontheheap on October 9, 2004 10:56:08 PM]

This topic is closed to new replies.

Advertisement