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 |
||
4 |
MousePointer * mouse_pointer_new (int b_box_h, int b_box_w){ |
|
5 |
MousePointer * self = malloc (sizeof(MousePointer)); |
|
6 |
fprintf (stdout, "Creating MousePointer: address: %ld\n", (long) self); |
|
7 |
/* TODO: add image... */ |
|
8 |
self->image = NULL; |
|
9 |
self->draw_rect = game_rect_new (0,0,32,32); |
|
10 |
self->bounding_box = game_rect_new (0,0,b_box_h, b_box_w); |
|
11 |
self->possision.x = 0; |
|
12 |
self->possision.y = 0; |
|
13 |
self->pressed = false; |
|
14 |
return self; |
|
15 |
}
|
|
16 |
||
17 |
void mouse_pointer_move (MousePointer * self, int deltax, int deltay) { |
|
18 |
self->draw_rect->x += deltax; |
|
19 |
self->draw_rect->y += deltay; |
|
20 |
self->possision.x += deltax; |
|
21 |
self->possision.y += deltay; |
|
22 |
}
|
|
23 |
||
24 |
void mouse_pointer_move_to (MousePointer * self, int x, int y) { |
|
25 |
self->draw_rect->x = x; |
|
26 |
self->draw_rect->y = y; |
|
27 |
self->possision.x = x; /* Will change to some x and y plus off set */ |
|
28 |
self->possision.y = y; |
|
29 |
}
|
|
30 |
||
31 |
||
32 |
void mouse_pointer_draw (MousePointer * self, SDL_Renderer * renderer) { |
|
33 |
int ret = SDL_SetRenderDrawColor(renderer,255,0,0,255); |
|
34 |
if (ret < 0) { |
|
35 |
fprintf(stderr, "Couldn't set Colour: %s\n", SDL_GetError()); |
|
36 |
} |
|
37 |
|
|
38 |
ret = SDL_RenderDrawRect (renderer, self->draw_rect); |
|
39 |
if (ret < 0) { |
|
40 |
fprintf(stderr, "Couldn't render MousePointer: %s\n", SDL_GetError()); |
|
41 |
} |
|
42 |
|
|
43 |
mouse_pointer_force_pos (self); |
|
44 |
}
|
|
45 |
||
46 |
void mouse_pointer_force_pos (MousePointer * self) { |
|
47 |
if (self->draw_rect->x > self->bounding_box->h) { |
|
48 |
self->draw_rect->x -= 10; |
|
49 |
} |
|
50 |
if (self->draw_rect->y > self->bounding_box->w) { |
|
51 |
self->draw_rect->y -= 10; |
|
52 |
} |
|
53 |
if (self->draw_rect->y < self->bounding_box->y) { |
|
54 |
self->draw_rect->y += 10; |
|
55 |
} |
|
56 |
if (self->draw_rect->x < self->bounding_box->x) { |
|
57 |
self->draw_rect->x += 10; |
|
58 |
} |
|
59 |
self->possision.x = self->draw_rect->x; /* Again, some ofset can be needed. */ |
|
60 |
self->possision.y = self->draw_rect->y; |
|
61 |
}
|
|
62 |
||
63 |
void mouse_pointer_free (MousePointer * self) { |
|
64 |
fprintf (stdout, "Freeing MousePointer: address: %ld\n", (long) self); |
|
65 |
if (self->image) { |
|
66 |
SDL_DestroyTexture (self->image); |
|
67 |
} |
|
68 |
free (self->draw_rect); |
|
69 |
free (self->bounding_box); |
|
70 |
free (self); |
|
71 |
}
|