1 /*	SCCS Id: @(#)jtp_mou.c	3.0	2000/11/12	*/
2 /* Copyright (c) Jaakko Peltonen, 2000				  */
3 /* NetHack may be freely redistributed.  See license for details. */
4 
5 #ifndef _jtp_mou_c_
6 #define _jtp_mou_c_
7 
8 #include "jtp_def.h"
9 #ifdef USE_DIRECTX_SYSCALLS
10 #include "jtp_dirx.h"
11 #endif
12 #ifdef USE_DOS_SYSCALLS
13 #include "jtp_dos.h"
14 #endif
15 #ifdef USE_SDL_SYSCALLS
16 #include "jtp_sdl.h"
17 #endif
18 #include "jtp_gra.h"
19 #include "jtp_mou.h"
20 
21 short int jtp_mousex = 100, jtp_mousey = 100, jtp_mouseb = 0;
22 short int jtp_oldmx = 100, jtp_oldmy = 100, jtp_oldmb = 0;
23 
24 jtp_mouse_cursor * jtp_mcursor[JTP_MAX_MCURSOR];
25 
jtp_readmouse()26 void jtp_readmouse()
27 {
28   jtp_oldmx = jtp_mousex; jtp_oldmy = jtp_mousey; jtp_oldmb = jtp_mouseb;
29 #ifdef USE_DIRECTX_SYSCALLS
30   jtp_DXReadMouse();
31   jtp_mousex = jtp_dx_mousex;
32   jtp_mousey = jtp_dx_mousey;
33   jtp_mouseb = jtp_dx_mouseb;
34 #endif
35 #ifdef USE_DOS_SYSCALLS
36   jtp_DOSReadMouse(&jtp_screen);
37   jtp_mousex = jtp_dos_mousex;
38   jtp_mousey = jtp_dos_mousey;
39   jtp_mouseb = jtp_dos_mouseb;
40 #endif
41 #ifdef USE_SDL_SYSCALLS
42   jtp_SDLReadMouse();
43   jtp_mousex = jtp_sdl_mousex;
44   jtp_mousey = jtp_sdl_mousey;
45   jtp_mouseb = jtp_sdl_mouseb;
46 #endif
47 }
48 
49 
jtp_setmouse(int tempx,int tempy)50 void jtp_setmouse(int tempx,int tempy)
51 {
52   jtp_mousex=tempx;
53   jtp_mousey=tempy;
54 }
55 
56 
jtp_mouse_area(int x1,int y1,int x2,int y2)57 char jtp_mouse_area(int x1,int y1,int x2,int y2)
58 {
59   if ((jtp_mousex>=x1)&&(jtp_mousex<=x2)&&(jtp_mousey>=y1)&&(jtp_mousey<=y2))
60     return(1);
61   return(0);
62 }
63 
jtp_oldm_area(int x1,int y1,int x2,int y2)64 char jtp_oldm_area(int x1,int y1,int x2,int y2)
65 {
66   if ((jtp_oldmx>=x1)&&(jtp_oldmx<=x2)&&(jtp_oldmy>=y1)&&(jtp_oldmy<=y2))
67    return(1);
68   return(0);
69 }
70 
71 
jtp_repeatmouse(jtp_mouse_cursor * m_cursor,int whenstop)72 void jtp_repeatmouse
73 (
74   jtp_mouse_cursor *m_cursor,
75   int whenstop
76 )
77 {
78   unsigned char * m_bg;
79 
80   m_bg = jtp_get_img(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
81                      jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
82                      jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]);
83   jtp_put_stencil(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod, m_cursor->graphic);
84   jtp_refresh_region(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
85                      jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
86                      jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]);
87   do
88   {
89     jtp_readmouse();
90 
91     if ((jtp_oldmx!=jtp_mousex) || (jtp_oldmy!=jtp_mousey))
92     {
93        jtp_put_img(jtp_oldmx + m_cursor->xmod, jtp_oldmy + m_cursor->ymod, m_bg);
94        free(m_bg);
95 
96        m_bg = jtp_get_img(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
97                           jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
98                           jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]);
99        jtp_put_stencil(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod, m_cursor->graphic);
100 
101        jtp_refresh_region(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
102                           jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
103                           jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]);
104        jtp_refresh_region(jtp_oldmx + m_cursor->xmod, jtp_oldmy + m_cursor->ymod,
105                           jtp_oldmx + m_cursor->xmod + m_cursor->graphic[3],
106                           jtp_oldmy + m_cursor->ymod + m_cursor->graphic[1]);
107     }
108   }
109   while (jtp_mouseb != whenstop);
110 
111   jtp_put_img(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod, m_bg);
112   /* jtp_refresh_region(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
113                      jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
114                      jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]); */
115   free(m_bg);
116 }
117 
118 
jtp_keymouse(jtp_mouse_cursor * m_cursor,int whenstop)119 void jtp_keymouse
120 (
121   jtp_mouse_cursor *m_cursor,
122   int whenstop
123 )
124 {
125   unsigned char * m_bg;
126 
127   m_bg = jtp_get_img(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
128                      jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
129                      jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]);
130   jtp_put_stencil(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod, m_cursor->graphic);
131   jtp_refresh_region(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
132                      jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
133                      jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]);
134   do
135   {
136     jtp_readmouse();
137 
138     if ((jtp_oldmx!=jtp_mousex) || (jtp_oldmy!=jtp_mousey))
139     {
140        jtp_put_img(jtp_oldmx + m_cursor->xmod, jtp_oldmy + m_cursor->ymod, m_bg);
141        free(m_bg);
142 
143        m_bg = jtp_get_img(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
144                           jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
145                           jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]);
146        jtp_put_stencil(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod, m_cursor->graphic);
147 
148        jtp_refresh_region(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
149                           jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
150                           jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]);
151        jtp_refresh_region(jtp_oldmx + m_cursor->xmod, jtp_oldmy + m_cursor->ymod,
152                           jtp_oldmx + m_cursor->xmod + m_cursor->graphic[3],
153                           jtp_oldmy + m_cursor->ymod + m_cursor->graphic[1]);
154     }
155   }
156   while ((jtp_mouseb != whenstop) && (!jtp_kbhit()));
157 
158   jtp_put_img(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod, m_bg);
159   /* jtp_refresh_region(jtp_mousex + m_cursor->xmod, jtp_mousey + m_cursor->ymod,
160                      jtp_mousex + m_cursor->xmod + m_cursor->graphic[3],
161                      jtp_mousey + m_cursor->ymod + m_cursor->graphic[1]); */
162   free(m_bg);
163 }
164 
165 
jtp_press_button(int x1,int y1,int x2,int y2,jtp_mouse_cursor * m_cursor)166 void jtp_press_button
167 (
168   int x1, int y1, int x2, int y2,
169   jtp_mouse_cursor * m_cursor
170 )
171 {
172  unsigned char *button_bg = jtp_get_img(x1, y1, x2, y2);
173  unsigned char *temp_button = jtp_get_img(x1, y1, x2-2,y2-2);
174 
175  jtp_fill_rect(x1, y1, x2, y1 + 1, 0);
176  jtp_fill_rect(x1, y1, x1 + 1, y2, 0);
177  jtp_put_img(x1 + 2, y1 + 2, temp_button);
178  free(temp_button);
179  jtp_refresh_region(x1, y1, x2, y2);
180  jtp_repeatmouse(m_cursor, JTP_MBUTTON_NONE);
181  jtp_put_img(x1, y1, button_bg);
182  jtp_refresh_region(x1, y1, x2, y2);
183  free(button_bg);
184 }
185 
186 
187 jtp_mouse_cursor *
jtp_get_mcursor(x1,y1,x2,y2)188 jtp_get_mcursor(x1, y1, x2, y2)
189 int x1, y1, x2, y2;
190 {
191   jtp_mouse_cursor *temp1 = (jtp_mouse_cursor *)malloc(sizeof(jtp_mouse_cursor));
192   int i, j, tx1, ty1, tx2, ty2;
193   unsigned char * tempg;
194 
195   for (i = y1; i <= y2; i++)
196     for (j = x1; j <= x2; j++)
197       if (jtp_screen.vpage[i*jtp_screen.width+j])
198       {
199         ty1 = i; i = y2+1; j = x2+1;
200       }
201  // printf("Check1\n");
202 
203   for (i = y2; i >= y1; i--)
204     for (j = x1; j <= x2; j++)
205       if (jtp_screen.vpage[i*jtp_screen.width+j])
206       {
207         ty2 = i; i = y1-1; j = x2+1;
208       }
209  // printf("Check2\n");
210   for (j = x1; j <= x2; j++)
211     for (i = ty1; i <= ty2; i++)
212       if (jtp_screen.vpage[i*jtp_screen.width+j])
213       {
214         tx1 = j; i = ty2+1; j = x2+1;
215       }
216  // printf("Check3\n");
217   for (j = x2; j >= x1; j--)
218     for (i = ty1; i <= ty2; i++)
219       if (jtp_screen.vpage[i*jtp_screen.width+j])
220       {
221         tx2 = j; i = ty2+1; j = x1-1;
222       }
223  // printf("Check4\n");
224 
225   temp1->xmod = 0;
226   for (i = x1; i <= x2; i++)
227     if (jtp_screen.vpage[(y1-1)*jtp_screen.width+i] == 16)
228       temp1->xmod = tx1 - i;
229 
230   temp1->ymod = 0;
231   for (i = y1; i <= y2; i++)
232     if (jtp_screen.vpage[i*jtp_screen.width+x1-1] == 16)
233       temp1->ymod = ty1 - i;
234 
235   temp1->graphic = jtp_get_img(tx1, ty1, tx2, ty2);
236   return(temp1);
237 }
238 
239 
240 
241 #endif
242