1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <SDL.h>
#include "context.h"
#include "input.h"
void setCameraLimits(Map *M, Tileset *T)
{
if(M->camera->x < 0)
M->camera->x = 0;
if(M->camera->y < 0)
M->camera->y = 0;
if(M->camera->x > M->nbtilesX*T->tileWidth)
M->camera->x = M->nbtilesX*T->tileWidth;
if(M->camera->y > M->nbtilesY*T->tileHeight)
M->camera->y = M->nbtilesY*T->tileHeight;
}
void renderMap(ScreenContext *SC, Tileset *T, Map *M)
{
short i, j;
short xmin, xmax, ymin, ymax;
unsigned numtile;
SDL_Rect dest;
xmin = M->camera->x / T->tileWidth;
ymin = M->camera->y / T->tileHeight;
xmax = (M->camera->x + M->camera->w) / T->tileWidth;
ymax = (M->camera->y + M->camera->h) / T->tileHeight;
for(j = ymin; j <= ymax; j++)
{
for(i = xmin; i <= xmax; i++)
{
dest.x = i*T->tileWidth - M->camera->x;
dest.y = j*T->tileHeight - M->camera->y;
if(i < 0 || i >= M->nbtilesX || j < 0 || i >= M->nbtilesY)
numtile = 0;
else
numtile = M->tileindex[j][i];
SDL_RenderCopy(SC->renderer, T->texture, &T->tileProps[numtile].rect, &dest);
}
}
}
void renderCurentTile(ScreenContext *SC, Tileset *T, Input in)
{
SDL_Rect dest;
dest.x = in.mousex - (T->tileWidth / 2);
dest.y = in.mousey - (T->tileHeight / 2);
dest.w = T->tileWidth;
dest.h = T->tileHeight;
SDL_RenderCopy(SC->renderer, T->texture, &T->tileProps[T->curentTile].rect, &dest);
}
void display(ScreenContext *SC, Tileset *T, Map *M, Input in)
{
SDL_SetRenderDrawColor(SC->renderer, 0x0, 0x0, 0x0, 0x0);
setCameraLimits(M, T);
renderMap(SC, T, M);
renderCurentTile(SC, T, in);
SDL_RenderPresent(SC->renderer);
SDL_Delay(5);
} |
Partager