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->clip.x = 0;
|
|
|
|
newTexture->clip.y = 0;
|
|
|
|
newTexture->clip.w = surface->w;
|
2017-12-11 08:23:30 +01:00
|
|
|
newTexture->clip.h = surface->h;
|
2017-11-30 21:00:47 +01:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
SDL_RenderCopy(cam->renderer,
|
|
|
|
texture->texture,
|
|
|
|
&texture->clip,
|
|
|
|
&draw_box);
|
|
|
|
}
|
|
|
|
|
2017-11-30 21:00:47 +01:00
|
|
|
void texture_destroy(Texture *texture)
|
|
|
|
{
|
|
|
|
SDL_DestroyTexture(texture->texture);
|
|
|
|
free(texture);
|
|
|
|
}
|