sprite.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "sprite.h"
  2. #include "sdl_helper.h"
  3. struct dd_sprite *dd_sprite_create() {
  4. struct dd_sprite *w = malloc(sizeof(struct dd_sprite));
  5. dd_sprite_init(w);
  6. return w;
  7. }
  8. void dd_sprite_load(struct dd_sprite *this, const char *name) {
  9. this->texture = dd_load_image(name);
  10. }
  11. void dd_sprite_draw(struct dd_sprite *this) {
  12. this->rect.x = this->x;
  13. this->rect.y = this->y;
  14. this->rect.w = this->w;
  15. this->rect.h = this->h;
  16. SDL_RenderCopy(dd_ren, this->texture, NULL, &this->rect);
  17. }
  18. void dd_sprite_init(struct dd_sprite *this) {
  19. this->load = dd_sprite_load;
  20. this->draw = dd_sprite_draw;
  21. this->collide = dd_sprite_collide;
  22. this->rect.x = this->x = 0;
  23. this->rect.y = this->y = 0;
  24. this->rect.w = this->w = 100;
  25. this->rect.h = this->h = 100;
  26. }
  27. int dd_sprite_collide(struct dd_sprite *a, struct dd_sprite *b) {
  28. return (
  29. // collision on X
  30. (a->rect.x < (b->rect.x +b->rect.w)) !=
  31. ((a->rect.x +a->rect.w) <= b->rect.x) &&
  32. // collision on Y
  33. (a->rect.y < (b->rect.y +b->rect.h)) !=
  34. ((a->rect.y +a->rect.h) <= b->rect.y)
  35. );
  36. }