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

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

imlib: clean up EventHandler? and JCMouse.

File size: 2.8 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#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
35unsigned 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//
52JCMouse::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
61    m_screen = screen;
62    br = pal->brightest( 1 );
63    dr = pal->darkest( 1 );
64    f.Set( 1, br );
65    f.Set( 2, dr );
66    im = new image(vec2i(8, 10), def_mouse);
67    f.Apply( im );
68    sp = new sprite(Screen, im, 100, 100);
69    mx = Screen->Size().x / 2;
70    my = Screen->Size().y / 2;
71}
72
73//
74// Destructor
75//
76JCMouse::~JCMouse()
77{
78    delete sp->visual;
79    delete sp;
80}
81
82//
83// set_shape()
84// Set the shape of the mouse cursor
85//
86void JCMouse::set_shape(image *im, int centerx, int centery)
87{
88    sp->change_visual(im, 1);
89    cx = -centerx;
90    cy = -centery;
91}
92
93//
94// set_position()
95// Set the position of the mouse cursor
96//
97void JCMouse::set_position(int new_mx, int new_my)
98{
99    // Make sure the values we are given are sensible.
100    mx = Min(new_mx, m_screen->Size().x - 1);
101    my = Min(new_my, m_screen->Size().y - 1);
102
103    // Set the new position
104    SDL_WarpMouse(mx, my);
105}
106
107//
108// update()
109// Update the mouse position and button states
110//
111void JCMouse::update(int newx, int newy, int new_but)
112{
113    if (newx < 0)
114    {
115        lx = mx;
116        ly = my;
117        lbut = but;
118
119        uint8_t mask = SDL_GetMouseState(&mx, &my);
120        but = ((mask & SDL_BUTTON(1)) != 0) |
121              ((mask & SDL_BUTTON(2)) != 0) << 2 |
122              ((mask & SDL_BUTTON(3)) != 0) << 1;
123    }
124    else
125    {
126        mx = newx;
127        my = newy;
128        but = new_but;
129    }
130}
131
Note: See TracBrowser for help on using the repository browser.