source: abuse/trunk/src/sdlport/mouse.cpp @ 534

Last change on this file since 534 was 513, checked in by Sam Hocevar, 12 years ago

imlib: some more vec2i transition for simplicity.

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