kettek2/wiki/games/newsboy/Newsboy_0x00/engine/sprite.c

57 lines
1.6 KiB
C

#include "sprite.h"
#include <stdlib.h>
/*
We load in a surface temporarily, copy over the data to a texture (via createTexture), then free the surface. Return the Sprite, of course.
*/
struct Sprite *createSpriteFromFile(char *name, char *file) {
struct Sprite *sprite = malloc(sizeof(struct Sprite));
SDL_Surface *surface = NULL;
if ( (surface = IMG_Load(file)) == NULL ) {
printf("ERR: loadSpriteFromFile:IMG_Load(%s): \"%s\"", file, SDL_GetError());
return NULL;
}
sprite->texture = createTexture(surface);
sprite->width = surface->w;
sprite->height = surface->h;
// we're done with our surface, free it
SDL_FreeSurface(surface);
//printf("loaded with: %dx%d\n", sprite->width, sprite->height);
return sprite;
}
int freeSprite(struct Sprite *sprite) {
if (sprite == NULL) return 1;
glDeleteTextures(1, &sprite->texture);
free(sprite);
return 0;
}
int renderSprite(struct Sprite *sprite, int x, int y) {
if (sprite == NULL) return 1;
float sx = sprite->width;
float sy = sprite->height;
// bind our texture
glBindTexture(GL_TEXTURE_2D, sprite->texture);
// make sure we're rendering textures
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// reset view
glLoadIdentity();
// translate to a quad
glTranslated(x, y, 1.0f);
// draw !
glBegin(GL_QUADS);
// top left
glTexCoord2d(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
// top right
glTexCoord2d(1.0, 0.0f); glVertex2f(sx, 0.0f);
// bottom right
glTexCoord2d(1.0f, 1.0f); glVertex2f(sx, sy);
// bottom left
glTexCoord2d(0.0f, 1.0f); glVertex2f(0.0f, sy);
glEnd();
return 0;
}