all repos — katbug @ main

side-scrolling infinite arcade game in C with SDL 1.2

Catbug.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include "config.h"
#include "Engine.h"
#include "Catbug.h"
#include "Timer.h"
#include "Pickups.h"
#include "extern.h"
Catbug* newCatbug(int x, int y)
{
  Catbug* self = malloc(sizeof(Catbug));

  self->x = x;
  self->y = y;

  self->vX = 0;
  self->vY = 0;

  self->HP = STARTING_HP;
  self->frame = 0;
  self->spriteSheet=loadImage("assets/catbug.png");

  return self;
}

void deleteCatbug(Catbug* target)
{
  SDL_FreeSurface(target->spriteSheet);
  free(target);
}

void moveCatbug(Catbug* self)
{
  self->x += self->vX;
  self->y += self->vY;

  if (self->x <= 0 || self->x >= 320)
    self->x -= self->vX;
  if (self->y <=0 || self->y >= 180)
    self->y -= self->vY;
}

void drawCatbug(Catbug* self)
{
  SDL_Rect clip;
  clip.x = 0;
  clip.y = 0;
  clip.w = 43;
  clip.h = 39;

  switch (self->frame)
  {
    case 0:
      clip.x = 0;
      self->frame = 1;
      break;
    case 1:
      clip.x = 43;
      self->frame = 0;
      break;
  }
  applySurface(self->x - 22, self->y - 19, self->spriteSheet, screen, &clip);
}

int isInRect(Catbug* self, SDL_Rect* box)
{
  if (self->x >= box->x && self->x <= box->x + box->w
      && self->y >= box->y && self->y <= box->y + box->h)
    return 1;
  else return 0;
}