breakhack/src/texture.c

147 lines
2.6 KiB
C
Raw Normal View History

2017-11-30 21:00:47 +01:00
#include <SDL2/SDL_image.h>
2017-12-19 19:42:05 +01:00
#include <stdio.h>
#include <stdlib.h>
2017-11-30 21:00:47 +01:00
#include "texture.h"
#include "util.h"
2017-12-15 15:03:29 +01:00
Texture*
texture_create(void)
2017-12-15 15:03:29 +01:00
{
Texture *t = ec_malloc(sizeof(Texture));
t->dim.height = 0;
t->dim.width = 0;
t->texture = NULL;
t->font = NULL;
return t;
}
void
texture_load_from_file(Texture *texture,
const char *path,
SDL_Renderer *renderer)
2017-11-30 21:00:47 +01:00
{
SDL_Surface *surface = IMG_Load(path);
if (surface == NULL)
{
error("Failed to load texture (%s): %s",
2017-12-15 15:03:29 +01:00
path, IMG_GetError());
return;
2017-11-30 21:00:47 +01:00
}
2017-12-15 15:03:29 +01:00
if (texture->texture) {
SDL_DestroyTexture(texture->texture);
texture->texture = NULL;
}
texture->dim.height = surface->h;
texture->dim.width = surface->w;
texture->texture = SDL_CreateTextureFromSurface(renderer,
surface);
if (texture->texture == NULL)
2017-11-30 21:00:47 +01:00
{
error("Failed to create texture (%s): %s",
2017-12-15 15:03:29 +01:00
path,
SDL_GetError());
2017-11-30 21:00:47 +01:00
}
2017-12-15 15:03:29 +01:00
SDL_FreeSurface(surface);
}
void
texture_load_font(Texture *t, const char *path, unsigned int size)
{
if (t->font)
TTF_CloseFont(t->font);
t->font = TTF_OpenFont(path, size);
if (t->font == NULL) {
error("Failed to load font %s: %s",
2017-12-15 15:03:29 +01:00
path,
TTF_GetError());
return;
}
}
void
texture_load_from_text(Texture *t,
const char *text,
SDL_Color c,
SDL_Renderer *renderer)
{
SDL_Surface *surface = TTF_RenderText_Solid( t->font, text, c );
if (surface == NULL)
{
error("Unable to create texture from rendered text: %s",
2017-12-15 15:03:29 +01:00
IMG_GetError());
return;
}
if (t->texture) {
SDL_DestroyTexture(t->texture);
t->texture = NULL;
}
t->texture = SDL_CreateTextureFromSurface( renderer, surface );
if (t->texture == NULL) {
error("Failed to create texture from text: %s",
2017-12-15 15:03:29 +01:00
SDL_GetError());
return;
}
t->dim.width = surface->w;
t->dim.height = surface->h;
SDL_FreeSurface(surface);
2017-11-30 21:00:47 +01:00
}
void
texture_render(Texture *texture, Position *p, Camera *cam)
{
2017-12-15 15:03:29 +01:00
if (!texture->texture)
return;
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
};
SDL_RenderCopy(cam->renderer,
texture->texture,
2017-12-14 12:01:05 +01:00
clip,
&draw_box);
}
2017-11-30 21:00:47 +01:00
void texture_destroy(Texture *texture)
{
2017-12-15 15:03:29 +01:00
if (texture->texture)
SDL_DestroyTexture(texture->texture);
if (texture->font)
TTF_CloseFont(texture->font);
2017-11-30 21:00:47 +01:00
free(texture);
}