update
[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::current_ = 0;
24
25 MouseCursor::MouseCursor(std::string cursor_file, int frames) : mid_x(0), mid_y(0)
26 {
27   cursor = new Surface(cursor_file, USE_ALPHA);
28   
29   cur_state = MC_NORMAL;
30   cur_frame = 0;
31   tot_frames = frames;
32
33   timer.init(false);
34   timer.start(MC_FRAME_PERIOD);
35   
36   SDL_ShowCursor(SDL_DISABLE);
37 }
38
39 MouseCursor::~MouseCursor()
40 {
41   delete cursor;
42
43   SDL_ShowCursor(SDL_ENABLE);
44 }
45
46 int MouseCursor::state()
47 {
48   return cur_state;
49 }
50
51 void MouseCursor::set_state(int nstate)
52 {
53   cur_state = nstate;
54 }
55
56 void MouseCursor::set_mid(int x, int y)
57 {
58   mid_x = x;
59   mid_y = y;
60 }
61
62 void MouseCursor::draw()
63 {
64   int x,y,w,h;
65   Uint8 ispressed = SDL_GetMouseState(&x,&y);
66   w = cursor->w / tot_frames;
67   h = cursor->h / MC_STATES_NB;
68   if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2))
69     {
70       if(cur_state != MC_CLICK)
71         {
72           state_before_click = cur_state;
73           cur_state = MC_CLICK;
74         }
75     }
76   else
77     {
78       if(cur_state == MC_CLICK)
79         cur_state = state_before_click;
80     }
81
82   if(timer.get_left() < 0 && tot_frames > 1)
83     {
84       cur_frame++;
85       if(cur_frame++ >= tot_frames)
86         cur_frame = 0;
87
88       timer.start(MC_FRAME_PERIOD);
89     }
90
91   cursor->draw_part(w*cur_frame, h*cur_state , x-mid_x, y-mid_y, w, h);
92 }