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