- converted texture_type into a Surface class
[supertux.git] / src / mousecursor.cpp
1
2 /***************************************************************************
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  ***************************************************************************/
10
11 // by Ricardo Cruz <rick2@aeiou.pt>
12
13 #include "screen.h"
14 #include "mousecursor.h"
15
16 MouseCursor::MouseCursor(std::string cursor_file, int frames)
17 {
18   cursor = new Surface(cursor_file, USE_ALPHA);
19
20   cur_state = MC_NORMAL;
21   cur_frame = 0;
22   tot_frames = frames;
23
24   timer.init(false);
25   timer.start(MC_FRAME_PERIOD);
26
27   SDL_ShowCursor(SDL_DISABLE);
28 }
29
30 MouseCursor::~MouseCursor()
31 {
32   delete cursor;
33
34   SDL_ShowCursor(SDL_ENABLE);
35 }
36
37 int MouseCursor::state()
38 {
39   return cur_state;
40 }
41
42 void MouseCursor::set_state(int nstate)
43 {
44   cur_state = nstate;
45 }
46
47 void MouseCursor::draw()
48 {
49   int x,y,w,h;
50   Uint8 ispressed = SDL_GetMouseState(&x,&y);
51   w = cursor->w / tot_frames;
52   h = cursor->h / MC_STATES_NB;
53   if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2))
54     {
55       if(cur_state != MC_CLICK)
56         {
57           state_before_click = cur_state;
58           cur_state = MC_CLICK;
59         }
60     }
61   else
62     {
63       if(cur_state == MC_CLICK)
64         cur_state = state_before_click;
65     }
66
67   if(timer.get_left() < 0 && tot_frames > 1)
68     {
69       cur_frame++;
70       if(cur_frame++ >= tot_frames)
71         cur_frame = 0;
72
73       timer.start(MC_FRAME_PERIOD);
74     }
75
76   cursor->draw_part(w*cur_frame, h*cur_state , x, y, w, h);
77 }