1 /************************************************************************
2  * This file is part of Wizznic.                                        *
3  * Copyright 2009-2015 Jimmy Christensen <dusted@dusted.dk>             *
4  * Wizznic 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 3 of the License, or    *
7  * (at your option) any later version.                                  *
8  *                                                                      *
9  * Wizznic is distributed in the hope that it will be useful,           *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
12  * GNU General Public License for more details.                         *
13  *                                                                      *
14  * You should have received a copy of the GNU General Public License    *
15  * along with Wizznic.  If not, see <http://www.gnu.org/licenses/>.     *
16  ************************************************************************/
17 
18 #include "pointer.h"
19 #include "ticks.h"
20 #include "defs.h"
21 #include "sprite.h"
22 #include "pixel.h"
23 #include "input.h"
24 
25 static inpPointerState_t inpPointer;
26 static SDL_Surface* ptrBackImg;
27 static SDL_Rect ptrBackRect;
28 static SDL_Rect backBtnDstRect;
29 
getInpPointerState()30 inpPointerState_t* getInpPointerState()
31 {
32   return(&inpPointer);
33 }
34 
initPointer(SDL_Surface * screen)35 void initPointer(SDL_Surface* screen)
36 {
37   //Load the "escape" overlay image
38   ptrBackImg = loadImg(DATADIR"data/ptr-back.png");
39   if( !ptrBackImg )
40     printf("Couldn't open %s\n",DATADIR"data/ptr-back.png");
41 
42   ptrBackRect.x=HSCREENW-160;;
43   ptrBackRect.y=HSCREENH-120;;
44   ptrBackRect.w=ptrBackRect.x+ptrBackImg->w;
45   ptrBackRect.h=ptrBackRect.y+ptrBackImg->h;
46 
47   backBtnDstRect.x = HSCREENW-160;
48   backBtnDstRect.y = HSCREENH-120;
49 
50   memset( &inpPointer, 0, sizeof(inpPointerState_t) );
51   inpPointer.timeSinceMoved=POINTER_SHOW_TIMEOUT;
52 
53   //The color white
54   inpPointer.colWhite = SDL_MapRGBA( screen->format, 255,255,255,255 );
55 }
56 
drawPointer(SDL_Surface * screen)57 void drawPointer(SDL_Surface* screen)
58 {
59   if( inpPointer.timeSinceMoved < POINTER_SHOW_TIMEOUT  )
60   {
61     //Update pointer-holddown time
62     if(inpPointer.isDown)
63       inpPointer.downTime += getTicks();
64 
65     if( inpPointer.escEnable )
66     {
67       inpPointer.escEnable=0;
68       SDL_BlitSurface( ptrBackImg, NULL, screen,&backBtnDstRect);
69     }
70 
71     plotPixel(screen, inpPointer.vpX, inpPointer.vpY, inpPointer.colWhite );
72 
73     plotPixel(screen, inpPointer.vpX, inpPointer.vpY-2, inpPointer.colWhite );
74     plotPixel(screen, inpPointer.vpX, inpPointer.vpY+2, inpPointer.colWhite );
75 
76     plotPixel(screen, inpPointer.vpX-2, inpPointer.vpY, inpPointer.colWhite );
77     plotPixel(screen, inpPointer.vpX+2, inpPointer.vpY, inpPointer.colWhite );
78 
79   }
80 }
81 
82 //The mousepointer is only "clicked" the first iteration that it is held down (when downTime is still 0)
83 //So, if the downtime is 0, then there is a chance that it is being hold down.
isPointerClicked()84 int_fast8_t isPointerClicked()
85 {
86   if( inpPointer.downTime==0 )
87   {
88     return(inpPointer.isDown);
89   }
90   return(0);
91 }
92 
isPointerInBox(SDL_Rect * r)93 int_fast8_t isPointerInBox( SDL_Rect* r )
94 {
95   if( r->x < inpPointer.vpX && r->w > inpPointer.vpX && r->y < inpPointer.vpY && r->h > inpPointer.vpY )
96   {
97     inpPointer.hitABox=1;
98     return(1);
99   }
100   return(0);
101 }
102 
isBoxClicked(SDL_Rect * r)103 int_fast8_t isBoxClicked( SDL_Rect* r )
104 {
105   if( isPointerClicked() )
106   {
107     return( isPointerInBox(r));
108   }
109   return(0);
110 }
111 
isPointerEscapeClicked()112 int_fast8_t isPointerEscapeClicked()
113 {
114   if( inpPointer.escEnable && isBoxClicked( &ptrBackRect ) )
115   {
116     inpPointer.hitABox=0;
117     resetMouseBtn();
118     return(1);
119   }
120   return(0);
121 }
122 
isAnyBoxHit()123 int_fast8_t isAnyBoxHit()
124 {
125   return(inpPointer.hitABox);
126 }
127 
128