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