1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au> |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software Foundation, |
---|
17 | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #include <SDL.h> |
---|
23 | |
---|
24 | #include "video.h" |
---|
25 | #include "sprite.h" |
---|
26 | #include "image.h" |
---|
27 | #include "filter.h" |
---|
28 | #include "mouse.h" |
---|
29 | |
---|
30 | unsigned char def_mouse[]= |
---|
31 | { |
---|
32 | 0,2,0,0,0,0,0,0, |
---|
33 | 2,1,2,0,0,0,0,0, |
---|
34 | 2,1,1,2,0,0,0,0, |
---|
35 | 2,1,1,1,2,0,0,0, |
---|
36 | 2,1,1,1,1,2,0,0, |
---|
37 | 2,1,1,1,1,1,2,0, |
---|
38 | 0,2,1,1,2,2,0,0, |
---|
39 | 0,0,2,1,1,2,0,0, |
---|
40 | 0,0,2,1,1,2,0,0, |
---|
41 | 0,0,0,2,2,0,0,0 |
---|
42 | }; |
---|
43 | |
---|
44 | // |
---|
45 | // Constructor |
---|
46 | // |
---|
47 | JCMouse::JCMouse( image *Screen, palette *pal ) |
---|
48 | { |
---|
49 | image *im; |
---|
50 | int br, dr; |
---|
51 | filter f; |
---|
52 | but = 0; |
---|
53 | cx = cy = 0; |
---|
54 | here = 1; |
---|
55 | sp = NULL; |
---|
56 | |
---|
57 | screen = Screen; |
---|
58 | br = pal->brightest( 1 ); |
---|
59 | dr = pal->darkest( 1 ); |
---|
60 | f.set( 1, br ); |
---|
61 | f.set( 2, dr ); |
---|
62 | im = new image( 8, 10, def_mouse ); |
---|
63 | f.apply( im ); |
---|
64 | sp = new sprite( Screen, im, 100, 100 ); |
---|
65 | mx = Screen->width() / 2; |
---|
66 | my = Screen->height() / 2; |
---|
67 | } |
---|
68 | |
---|
69 | // |
---|
70 | // Destructor |
---|
71 | // |
---|
72 | JCMouse::~JCMouse() |
---|
73 | { |
---|
74 | if( sp ) |
---|
75 | { |
---|
76 | delete sp->visual; |
---|
77 | delete sp; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | // |
---|
82 | // set_shape() |
---|
83 | // Set the shape of the mouse cursor |
---|
84 | // |
---|
85 | void JCMouse::set_shape( image *im, int centerx, int centery ) |
---|
86 | { |
---|
87 | sp->change_visual( im, 1 ); |
---|
88 | cx = -centerx; |
---|
89 | cy = -centery; |
---|
90 | } |
---|
91 | |
---|
92 | // |
---|
93 | // set_position() |
---|
94 | // Set the position of the mouse cursor |
---|
95 | // |
---|
96 | void JCMouse::set_position( int new_mx, int new_my ) |
---|
97 | { |
---|
98 | // Make sure the values we are given are sensible. |
---|
99 | if( new_mx > screen->width() - 1 ) |
---|
100 | { |
---|
101 | new_mx = screen->width() - 1; |
---|
102 | } |
---|
103 | if( new_my > screen->height() - 1 ) |
---|
104 | { |
---|
105 | new_my = screen->height() - 1; |
---|
106 | } |
---|
107 | |
---|
108 | // Set the new position |
---|
109 | mx = new_mx; |
---|
110 | my = new_my; |
---|
111 | SDL_WarpMouse( new_mx, new_my ); |
---|
112 | } |
---|
113 | |
---|
114 | // |
---|
115 | // update() |
---|
116 | // Update the mouses position and buttons states |
---|
117 | // |
---|
118 | void JCMouse::update( int newx, int newy, int new_but ) |
---|
119 | { |
---|
120 | if( newx < 0 ) |
---|
121 | { |
---|
122 | Uint8 mask; |
---|
123 | |
---|
124 | lx = mx; |
---|
125 | ly = my; |
---|
126 | lbut = but; |
---|
127 | mask = SDL_GetMouseState( &mx, &my ); |
---|
128 | but = ( ( mask & SDL_BUTTON(1) ) != 0 ) | |
---|
129 | ( ( mask & SDL_BUTTON(2) ) != 0 ) << 2 | |
---|
130 | ( ( mask & SDL_BUTTON(3) ) != 0 ) << 1; |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | mx = newx; |
---|
135 | my = newy; |
---|
136 | but = new_but; |
---|
137 | } |
---|
138 | } |
---|