00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "pgdraw.h"
00032 #include <cmath>
00033
00034 void PG_Draw::DrawTile(SDL_Surface* surface, const PG_Rect& ref, const PG_Rect& drawrect, SDL_Surface* tilemap) {
00035 PG_Point index1;
00036 PG_Point index2;
00037 PG_Rect oldclip;
00038
00039 if (!surface || !tilemap) {
00040 return;
00041 }
00042
00043 if (!tilemap->w || !tilemap->h || !surface->w || !surface->h) {
00044 return;
00045 }
00046
00047 int dx = (int)std::abs((double)(drawrect.x - ref.x));
00048 int dy = (int)std::abs((double)(drawrect.y - ref.y));
00049
00050 index1.x = dx / tilemap->w;
00051 index1.y = dy / tilemap->h;
00052
00053 index2.x = (dx + drawrect.w + tilemap->w - 1) / tilemap->w;
00054 index2.y = (dy + drawrect.h + tilemap->h - 1) / tilemap->h;
00055
00056 SDL_GetClipRect(surface, const_cast<PG_Rect*>(&oldclip));
00057 SDL_SetClipRect(surface, const_cast<PG_Rect*>(&drawrect));
00058
00059 PG_Rect src(0,0, tilemap->w, tilemap->h);
00060 PG_Rect dst = src;
00061
00062 for(int y = index1.y; y < index2.y; y++) {
00063 for(int x = index1.x; x < index2.x; x++) {
00064
00065 dst.x = ref.x + x * tilemap->w;
00066 dst.y = ref.y + y * tilemap->h;
00067
00068 PG_Draw::BlitSurface(tilemap, src, surface, dst);
00069 }
00070 }
00071
00072 SDL_SetClipRect(surface, const_cast<PG_Rect*>(&oldclip));
00073 }