[56] | 1 | /* |
---|
| 2 | * Abuse - dark 2D side-scrolling platform game |
---|
| 3 | * Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au> |
---|
[494] | 4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
[56] | 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 | */ |
---|
[2] | 20 | |
---|
[56] | 21 | #include "config.h" |
---|
| 22 | |
---|
[2] | 23 | #include <SDL.h> |
---|
| 24 | |
---|
[481] | 25 | #include "video.h" |
---|
| 26 | #include "sprite.h" |
---|
| 27 | #include "image.h" |
---|
| 28 | #include "filter.h" |
---|
| 29 | #include "mouse.h" |
---|
[2] | 30 | |
---|
| 31 | unsigned char def_mouse[]= |
---|
| 32 | { |
---|
[124] | 33 | 0,2,0,0,0,0,0,0, |
---|
| 34 | 2,1,2,0,0,0,0,0, |
---|
| 35 | 2,1,1,2,0,0,0,0, |
---|
| 36 | 2,1,1,1,2,0,0,0, |
---|
| 37 | 2,1,1,1,1,2,0,0, |
---|
| 38 | 2,1,1,1,1,1,2,0, |
---|
| 39 | 0,2,1,1,2,2,0,0, |
---|
| 40 | 0,0,2,1,1,2,0,0, |
---|
| 41 | 0,0,2,1,1,2,0,0, |
---|
| 42 | 0,0,0,2,2,0,0,0 |
---|
[2] | 43 | }; |
---|
| 44 | |
---|
| 45 | // |
---|
| 46 | // Constructor |
---|
| 47 | // |
---|
| 48 | JCMouse::JCMouse( image *Screen, palette *pal ) |
---|
| 49 | { |
---|
[124] | 50 | image *im; |
---|
| 51 | int br, dr; |
---|
| 52 | filter f; |
---|
| 53 | but = 0; |
---|
| 54 | cx = cy = 0; |
---|
| 55 | here = 1; |
---|
| 56 | sp = NULL; |
---|
[2] | 57 | |
---|
[124] | 58 | screen = Screen; |
---|
| 59 | br = pal->brightest( 1 ); |
---|
| 60 | dr = pal->darkest( 1 ); |
---|
| 61 | f.set( 1, br ); |
---|
| 62 | f.set( 2, dr ); |
---|
| 63 | im = new image( 8, 10, def_mouse ); |
---|
| 64 | f.apply( im ); |
---|
| 65 | sp = new sprite( Screen, im, 100, 100 ); |
---|
| 66 | mx = Screen->width() / 2; |
---|
| 67 | my = Screen->height() / 2; |
---|
[2] | 68 | } |
---|
| 69 | |
---|
| 70 | // |
---|
| 71 | // Destructor |
---|
| 72 | // |
---|
| 73 | JCMouse::~JCMouse() |
---|
| 74 | { |
---|
[124] | 75 | if( sp ) |
---|
| 76 | { |
---|
| 77 | delete sp->visual; |
---|
| 78 | delete sp; |
---|
| 79 | } |
---|
[2] | 80 | } |
---|
| 81 | |
---|
| 82 | // |
---|
| 83 | // set_shape() |
---|
| 84 | // Set the shape of the mouse cursor |
---|
| 85 | // |
---|
| 86 | void JCMouse::set_shape( image *im, int centerx, int centery ) |
---|
| 87 | { |
---|
[124] | 88 | sp->change_visual( im, 1 ); |
---|
| 89 | cx = -centerx; |
---|
| 90 | cy = -centery; |
---|
[2] | 91 | } |
---|
| 92 | |
---|
| 93 | // |
---|
| 94 | // set_position() |
---|
| 95 | // Set the position of the mouse cursor |
---|
| 96 | // |
---|
| 97 | void JCMouse::set_position( int new_mx, int new_my ) |
---|
| 98 | { |
---|
[124] | 99 | // Make sure the values we are given are sensible. |
---|
[477] | 100 | if( new_mx > screen->width() - 1 ) |
---|
[124] | 101 | { |
---|
[477] | 102 | new_mx = screen->width() - 1; |
---|
[124] | 103 | } |
---|
[477] | 104 | if( new_my > screen->height() - 1 ) |
---|
[124] | 105 | { |
---|
[477] | 106 | new_my = screen->height() - 1; |
---|
[124] | 107 | } |
---|
[2] | 108 | |
---|
[124] | 109 | // Set the new position |
---|
| 110 | mx = new_mx; |
---|
| 111 | my = new_my; |
---|
| 112 | SDL_WarpMouse( new_mx, new_my ); |
---|
[2] | 113 | } |
---|
| 114 | |
---|
| 115 | // |
---|
| 116 | // update() |
---|
| 117 | // Update the mouses position and buttons states |
---|
| 118 | // |
---|
| 119 | void JCMouse::update( int newx, int newy, int new_but ) |
---|
| 120 | { |
---|
[124] | 121 | if( newx < 0 ) |
---|
| 122 | { |
---|
| 123 | Uint8 mask; |
---|
[2] | 124 | |
---|
[124] | 125 | lx = mx; |
---|
| 126 | ly = my; |
---|
| 127 | lbut = but; |
---|
| 128 | mask = SDL_GetMouseState( &mx, &my ); |
---|
| 129 | but = ( ( mask & SDL_BUTTON(1) ) != 0 ) | |
---|
| 130 | ( ( mask & SDL_BUTTON(2) ) != 0 ) << 2 | |
---|
| 131 | ( ( mask & SDL_BUTTON(3) ) != 0 ) << 1; |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | mx = newx; |
---|
| 136 | my = newy; |
---|
| 137 | but = new_but; |
---|
| 138 | } |
---|
[2] | 139 | } |
---|