2017-11-30 21:00:47 +01:00
|
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#include "texture.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2017-12-02 23:32:40 +01:00
|
|
|
Texture* texture_create(const char *path, SDL_Renderer *renderer)
|
2017-11-30 21:00:47 +01:00
|
|
|
{
|
|
|
|
Texture *newTexture = NULL;
|
|
|
|
SDL_Surface *surface = IMG_Load(path);
|
|
|
|
|
|
|
|
if (surface == NULL)
|
|
|
|
{
|
2017-12-11 08:23:30 +01:00
|
|
|
printf("[!!] Failed to load texture (%s): %s\n", path, IMG_GetError());
|
2017-11-30 21:00:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newTexture = ec_malloc(sizeof(Texture));
|
|
|
|
newTexture->dim.height = surface->h;
|
|
|
|
newTexture->dim.width = surface->w;
|
|
|
|
newTexture->texture = SDL_CreateTextureFromSurface(renderer,
|
|
|
|
surface);
|
|
|
|
if (newTexture->texture == NULL)
|
|
|
|
{
|
2017-12-11 08:23:30 +01:00
|
|
|
printf("[!!] Failed to create texture (%s): %s\n",
|
2017-11-30 21:00:47 +01:00
|
|
|
path,
|
|
|
|
SDL_GetError());
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_FreeSurface(surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newTexture;
|
|
|
|
}
|
|
|
|
|
2017-12-11 08:23:30 +01:00
|
|
|
void
|
|
|
|
texture_render(Texture *texture, Position *p, Camera *cam)
|
|
|
|
{
|
|
|
|
SDL_Rect draw_box = (SDL_Rect) {
|
|
|
|
p->x,
|
|
|
|
p->y,
|
|
|
|
texture->dim.width,
|
|
|
|
texture->dim.height
|
|
|
|
};
|
|
|
|
|
2017-12-14 12:01:05 +01:00
|
|
|
SDL_Rect clip = (SDL_Rect) {
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
texture->dim.width,
|
|
|
|
texture->dim.height
|
|
|
|
};
|
|
|
|
|
|
|
|
SDL_RenderCopy(cam->renderer,
|
|
|
|
texture->texture,
|
|
|
|
&clip,
|
|
|
|
&draw_box);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
texture_render_clip(Texture *texture, Position *p, SDL_Rect *clip, Camera *cam)
|
|
|
|
{
|
|
|
|
SDL_Rect draw_box = (SDL_Rect) {
|
|
|
|
p->x,
|
|
|
|
p->y,
|
|
|
|
texture->dim.width,
|
|
|
|
texture->dim.height
|
|
|
|
};
|
|
|
|
|
2017-12-11 08:23:30 +01:00
|
|
|
SDL_RenderCopy(cam->renderer,
|
|
|
|
texture->texture,
|
2017-12-14 12:01:05 +01:00
|
|
|
clip,
|
2017-12-11 08:23:30 +01:00
|
|
|
&draw_box);
|
|
|
|
}
|
|
|
|
|
2017-11-30 21:00:47 +01:00
|
|
|
void texture_destroy(Texture *texture)
|
|
|
|
{
|
|
|
|
SDL_DestroyTexture(texture->texture);
|
|
|
|
free(texture);
|
|
|
|
}
|