breakhack/src/texture.c

59 lines
1.2 KiB
C
Raw Normal View History

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)
{
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;
newTexture->clip.h = surface->h;
2017-11-30 21:00:47 +01:00
newTexture->texture = SDL_CreateTextureFromSurface(renderer,
surface);
if (newTexture->texture == NULL)
{
printf("[!!] Failed to create texture (%s): %s\n",
2017-11-30 21:00:47 +01:00
path,
SDL_GetError());
}
SDL_FreeSurface(surface);
}
return newTexture;
}
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);
}