Advertisement

Troubles with Tiles

Started by February 21, 2000 08:20 AM
10 comments, last by Kavos 24 years, 9 months ago
I'm working on tile-based top down engine here is the map drawing functions: #include "Standard.h" #define MAP_WIDTH 25 #define MAP_HEIGHT 18 #define TILE_WIDTH 32 #define TILE_HEIGHT 32 MAP_INFO map[MAP_WIDTH][MAP_HEIGHT]; RECT rcDest; // No map loading functions yet, gotta fill it with somthing void Setup_Map() { for(int i = 0; i < MAP_HEIGHT; i++) for(int j = 0; j < MAP_WIDTH; j++) map[j].tile_number = 0; } void Draw_Map(int scroll_x, int scroll_y) { int tile_to_draw; for(int i = 0; i < MAP_HEIGHT; i++) for(int j = 0; j < MAP_WIDTH; j++) { rcDest.right = TILE_WIDTH * j + scroll_x; rcDest.bottom = TILE_HEIGHT * i + scroll_y; rcDest.left = rcDest.right - TILE_WIDTH; rcDest.top = rcDest.bottom - TILE_HEIGHT; tile_to_draw = map[j] .tile_number; Draw_Tile(tile_to_draw); } } void Draw_Tile(int tile) { RECT rcSource; int offset_x, offset_y; if(tile < 10) { offset_x = tile * TILE_WIDTH; offset_y = 0; } rcSource.left = offset_x; rcSource.top = offset_y; rcSource.right = offset_x + TILE_WIDTH; rcSource.bottom = offset_y + TILE_WIDTH; lpDDSBack->Blt(&rcDest, lpDDSOff, &rcSource, NULL, NULL); } </code> It works great with small maps but it really slows down when I set the map size larger. If I set my hieght and width to 100 my map array should only be 1000 bytes, the clipper should make sure Blt clips everything not being shown (if I understand it correctly) so why does it slow down. I've been basically piecing this together from info I have found on the net, Am I just missing something? Or am I just dumb… Thanx for any help you can give, -Trev Edited by - Kavos on 2/21/00 10:38:04 PM
You should only draw those tiles that are actually visible in the window. The clipper makes sure that nothing is drawn outside the view but it still has to process the calls.
Advertisement
Make 2 arrays like this (map of 128x128 tiles):
unsigned char (or short) field[128][128];
unsigned char (or short) shownarea[25][18];
Copy the piece of the map you want to see in showarea. In field is the total map.
This way you''ll get a ''viewpoint'' effect. Update it as the
map scrolls.
Hope that helps

Hey thanks... But can I get a short example?
Use thjis code for your For..Next loops, I think it should work but I''ve just done it quickly off the top of my head. I''m presuming scroll_x/y are pixel values and not tile values...

for(int i = scroll_y / TILE_HEIGHT; i < (scroll_y / TILE_HEIGHT) + (screen_height / TILE_HEIGHT); i++)

for(int j = scroll_x / TILE_WIDTH; i < (scroll_x / TILE_WIDTH) + (screen_width / TILE_WIDTH); j++)

Hope this helps
----------------
Black Edge Games
----------------
Thanks i''ll give it a try...
Advertisement

Damn I can''t get it to work! Need more info...

-Trev
Use two global vars that indicates the start x and y-axis of the viewport. like xofs and yofs (unsigned char is enough). when you got this:
________________(              )( _____        )( (   )        ) <-- Map( (___)        )(              )(______________)  

the small rectangle is the viewport. It's x,y is for example
5,10. Then you should fill xofs with 5 and yofs with 10.
Now some code to paste a piece of the map in the rectangle (not the fastest way, but the most readable).
void pasteviewport(){   unsigned char x,y;   for (y=0;y      for (x=0;x         viewport[y][x]=map[y+yofs][x+xofs];};  

Hope that is the solution you wanted.

Edited by - bosjoh on 2/21/00 11:50:24 PM

Edited by - bosjoh on 2/21/00 11:51:44 PM
Hmm, in my post above the map doesn''t show right.
hmm, what world does 100 height and 100 width give you 1000?
last i checked 100*100 is 10,000
-werdup-

This topic is closed to new replies.

Advertisement