1 /*
2  *                               Alizarin Tetris
3  * A generic way to display buttons.
4  *
5  * Copyright 2000, Westley Weimer & Kiri Wagstaff
6  */
7 
8 #define BUFSIZE 256
9 
10 #include <config.h>
11 #include <assert.h>
12 
13 #include "atris.h"
14 #include "display.h"
15 #include "button.h"
16 
17 /***************************************************************************
18  *      button()
19  * Create a new button and return it.
20  *********************************************************************PROTO*/
button(char * text,Uint32 face_color0,Uint32 text_color0,Uint32 face_color1,Uint32 text_color1,int x,int y)21 ATButton* button(char* text, Uint32 face_color0, Uint32 text_color0,
22 	Uint32 face_color1, Uint32 text_color1,
23 	int x, int y)
24 {
25   SDL_Color sc;
26   ATButton* ab = (ATButton*)malloc(sizeof(ATButton)); Assert(ab);
27 
28   /* Copy the colors */
29   ab->face_color[0] = face_color0;
30   ab->text_color[0] = text_color0;
31   ab->face_color[1] = face_color1;
32   ab->text_color[1] = text_color1;
33   /* Copy the text */
34   SDL_GetRGB(text_color0, screen->format, &sc.r, &sc.g, &sc.b);
35   ab->bitmap[0] = TTF_RenderText_Blended(font, text, sc);
36   Assert(ab->bitmap[0]);
37   SDL_GetRGB(text_color1, screen->format, &sc.r, &sc.g, &sc.b);
38   ab->bitmap[1]  = TTF_RenderText_Blended(font, text, sc);
39   Assert(ab->bitmap[1]);
40   /* Make the button bg */
41   ab->area.x = x; ab->area.y = y;
42   ab->area.w = ab->bitmap[0]->w + 10;
43   ab->area.h = ab->bitmap[0]->h + 10;
44   return ab;
45 }
46 
47 /***************************************************************************
48  *      show_button()
49  * Displays the button, taking into account whether it's clicked or not.
50  *********************************************************************PROTO*/
show_button(ATButton * ab,int state)51 void show_button(ATButton* ab, int state)
52 {
53     SDL_FillRect(screen, &ab->area, ab->text_color[state]);
54     SDL_UpdateSafe(screen, 1, &ab->area);
55     ab->area.x += 2; ab->area.y += 2;
56     ab->area.w -= 4; ab->area.h -= 4;
57     SDL_FillRect(screen, &ab->area, ab->face_color[state]);
58     SDL_UpdateSafe(screen, 1, &ab->area);
59     ab->area.x += 3; ab->area.y += 3;
60     ab->area.w -= 6; ab->area.h -= 6;
61     SDL_BlitSurface(ab->bitmap[state], NULL, screen, &ab->area);
62 
63     ab->area.x -= 5;  ab->area.y -= 5;
64     ab->area.w += 10; ab->area.h += 10;
65     SDL_UpdateSafe(screen, 1, &ab->area);
66 }
67 
68 /***************************************************************************
69  *      check_button()
70  * Returns 1 if (x,y) is inside the button, else 0.
71  * Also sets the is_clicked field in ab.
72  *********************************************************************PROTO*/
check_button(ATButton * ab,int x,int y)73 char check_button(ATButton* ab, int x, int y)
74 {
75   if ((ab->area.x <= x && x <= ab->area.x + ab->area.w) &&
76 	  (ab->area.y <= y && y <= ab->area.y + ab->area.h)) {
77       return 1;
78   } else return 0;
79 }
80 
81 /*
82  * $Log: button.c,v $
83  * Revision 1.9  2000/11/06 01:22:40  wkiri
84  * Updated menu system.
85  *
86  * Revision 1.8  2000/10/29 21:23:28  weimer
87  * One last round of header-file changes to reflect my newest and greatest
88  * knowledge of autoconf/automake. Now if you fail to have some bizarro
89  * function, we try to go on anyway unless it is vastly needed.
90  *
91  * Revision 1.7  2000/10/21 01:14:42  weimer
92  * massic autoconf/automake restructure ...
93  *
94  * Revision 1.6  2000/10/18 23:57:49  weimer
95  * general fixup, color changes, display changes.
96  * Notable: "Safe" Blits and Updates now perform "clipping". No more X errors,
97  * we hope!
98  *
99  * Revision 1.5  2000/09/04 22:49:51  wkiri
100  * gamemenu now uses the new nifty menus.  Also, added delete_menu.
101  *
102  * Revision 1.4  2000/09/04 19:48:02  weimer
103  * interim menu for choosing among game styles, button changes (two states)
104  *
105  * Revision 1.3  2000/09/03 18:26:10  weimer
106  * major header file and automatic prototype generation changes, restructuring
107  *
108  * Revision 1.2  2000/08/26 03:11:34  wkiri
109  * Buttons now use borders.
110  *
111  * Revision 1.1  2000/08/26 02:45:28  wkiri
112  * Beginnings of button class; also modified atris to query for 'new
113  * game' vs. 'quit'.  (New Game doesn't work yet...)
114  *
115  */
116