- bullet tweaks
[supertux.git] / src / mousecursor.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (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
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include "screen.h"
21 #include "mousecursor.h"
22
23 MouseCursor::MouseCursor(std::string cursor_file, int frames)
24 {
25   cursor = new Surface(cursor_file, USE_ALPHA);
26
27   cur_state = MC_NORMAL;
28   cur_frame = 0;
29   tot_frames = frames;
30
31   timer.init(false);
32   timer.start(MC_FRAME_PERIOD);
33
34   SDL_ShowCursor(SDL_DISABLE);
35 }
36
37 MouseCursor::~MouseCursor()
38 {
39   delete cursor;
40
41   SDL_ShowCursor(SDL_ENABLE);
42 }
43
44 int MouseCursor::state()
45 {
46   return cur_state;
47 }
48
49 void MouseCursor::set_state(int nstate)
50 {
51   cur_state = nstate;
52 }
53
54 void MouseCursor::draw()
55 {
56   int x,y,w,h;
57   Uint8 ispressed = SDL_GetMouseState(&x,&y);
58   w = cursor->w / tot_frames;
59   h = cursor->h / MC_STATES_NB;
60   if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2))
61     {
62       if(cur_state != MC_CLICK)
63         {
64           state_before_click = cur_state;
65           cur_state = MC_CLICK;
66         }
67     }
68   else
69     {
70       if(cur_state == MC_CLICK)
71         cur_state = state_before_click;
72     }
73
74   if(timer.get_left() < 0 && tot_frames > 1)
75     {
76       cur_frame++;
77       if(cur_frame++ >= tot_frames)
78         cur_frame = 0;
79
80       timer.start(MC_FRAME_PERIOD);
81     }
82
83   cursor->draw_part(w*cur_frame, h*cur_state , x, y, w, h);
84 }