/+junk/c_sdl_joypad_ducktape

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad_ducktape
1 by Gustav Hartvigsson
Initial code.
1
#include "MousePointer.h"
2
#include "game_utils.h"
3
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
4
/**
5
 * This file, as the rest of the project is under MIT license.
6
 * see http://opensource.org/licenses/MIT
7
 *
8
 * Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
9
 */
10
1 by Gustav Hartvigsson
Initial code.
11
MousePointer * mouse_pointer_new (int  b_box_h, int b_box_w){
12
  MousePointer * self = malloc (sizeof(MousePointer));
13
  fprintf (stdout, "Creating MousePointer: address: %ld\n", (long) self);
14
  /* TODO: add image... */
15
  self->image = NULL;
16
  self->draw_rect = game_rect_new (0,0,32,32);
17
  self->bounding_box = game_rect_new (0,0,b_box_h, b_box_w);
18
  self->possision.x = 0;
19
  self->possision.y = 0;
20
  self->pressed = false;
21
  return self;
22
}
23
24
void mouse_pointer_move (MousePointer * self, int deltax, int deltay) {
25
  self->draw_rect->x += deltax;
26
  self->draw_rect->y += deltay;
27
  self->possision.x += deltax;
28
  self->possision.y += deltay;
29
}
30
31
void mouse_pointer_move_to (MousePointer * self, int x, int y) {
32
  self->draw_rect->x = x;
33
  self->draw_rect->y = y;
34
  self->possision.x = x; /* Will change to some x and y plus off set */
35
  self->possision.y = y;
36
}
37
38
39
void mouse_pointer_draw (MousePointer * self, SDL_Renderer * renderer) {
40
  int ret = SDL_SetRenderDrawColor(renderer,255,0,0,255);
41
  if (ret < 0) {
42
    fprintf(stderr, "Couldn't set Colour: %s\n", SDL_GetError());
43
  }
44
  
45
  ret = SDL_RenderDrawRect (renderer, self->draw_rect);
46
  if (ret < 0) {
47
    fprintf(stderr, "Couldn't render MousePointer: %s\n", SDL_GetError());
48
  }
49
  
50
  mouse_pointer_force_pos (self);
51
}
52
53
void mouse_pointer_force_pos (MousePointer * self) {
54
  if (self->draw_rect->x > self->bounding_box->h) {
55
    self->draw_rect->x -= 10;
56
  }
57
  if (self->draw_rect->y > self->bounding_box->w) {
58
    self->draw_rect->y -= 10;
59
  }
60
  if (self->draw_rect->y < self->bounding_box->y) {
61
    self->draw_rect->y += 10;
62
  }
63
  if (self->draw_rect->x < self->bounding_box->x) {
64
    self->draw_rect->x += 10;
65
  }
66
  self->possision.x = self->draw_rect->x; /* Again, some ofset can be needed. */
67
  self->possision.y = self->draw_rect->y;
68
}
69
70
void mouse_pointer_free (MousePointer * self) {
71
  fprintf (stdout, "Freeing MousePointer: address: %ld\n", (long) self);
72
  if (self->image) {
73
    SDL_DestroyTexture (self->image);
74
  }
75
  free (self->draw_rect);
76
  free (self->bounding_box);
77
  free (self);
78
}