/+junk/c_sdl_joypad_ducktape

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad_ducktape
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
73
74
75
76
77
78
#include "MousePointer.h"
#include "game_utils.h"

/**
 * This file, as the rest of the project is under MIT license.
 * see http://opensource.org/licenses/MIT
 *
 * Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
 */

MousePointer * mouse_pointer_new (int  b_box_h, int b_box_w){
  MousePointer * self = malloc (sizeof(MousePointer));
  fprintf (stdout, "Creating MousePointer: address: %ld\n", (long) self);
  /* TODO: add image... */
  self->image = NULL;
  self->draw_rect = game_rect_new (0,0,32,32);
  self->bounding_box = game_rect_new (0,0,b_box_h, b_box_w);
  self->possision.x = 0;
  self->possision.y = 0;
  self->pressed = false;
  return self;
}

void mouse_pointer_move (MousePointer * self, int deltax, int deltay) {
  self->draw_rect->x += deltax;
  self->draw_rect->y += deltay;
  self->possision.x += deltax;
  self->possision.y += deltay;
}

void mouse_pointer_move_to (MousePointer * self, int x, int y) {
  self->draw_rect->x = x;
  self->draw_rect->y = y;
  self->possision.x = x; /* Will change to some x and y plus off set */
  self->possision.y = y;
}


void mouse_pointer_draw (MousePointer * self, SDL_Renderer * renderer) {
  int ret = SDL_SetRenderDrawColor(renderer,255,0,0,255);
  if (ret < 0) {
    fprintf(stderr, "Couldn't set Colour: %s\n", SDL_GetError());
  }
  
  ret = SDL_RenderDrawRect (renderer, self->draw_rect);
  if (ret < 0) {
    fprintf(stderr, "Couldn't render MousePointer: %s\n", SDL_GetError());
  }
  
  mouse_pointer_force_pos (self);
}

void mouse_pointer_force_pos (MousePointer * self) {
  if (self->draw_rect->x > self->bounding_box->h) {
    self->draw_rect->x -= 10;
  }
  if (self->draw_rect->y > self->bounding_box->w) {
    self->draw_rect->y -= 10;
  }
  if (self->draw_rect->y < self->bounding_box->y) {
    self->draw_rect->y += 10;
  }
  if (self->draw_rect->x < self->bounding_box->x) {
    self->draw_rect->x += 10;
  }
  self->possision.x = self->draw_rect->x; /* Again, some ofset can be needed. */
  self->possision.y = self->draw_rect->y;
}

void mouse_pointer_free (MousePointer * self) {
  fprintf (stdout, "Freeing MousePointer: address: %ld\n", (long) self);
  if (self->image) {
    SDL_DestroyTexture (self->image);
  }
  free (self->draw_rect);
  free (self->bounding_box);
  free (self);
}