summaryrefslogtreecommitdiffstats
path: root/menu_demo/button.c
diff options
context:
space:
mode:
Diffstat (limited to 'menu_demo/button.c')
-rw-r--r--menu_demo/button.c202
1 files changed, 78 insertions, 124 deletions
diff --git a/menu_demo/button.c b/menu_demo/button.c
index 82fe504..d452fd0 100644
--- a/menu_demo/button.c
+++ b/menu_demo/button.c
@@ -1,10 +1,10 @@
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN 1
- #include <windows.h>
-#else
- #define FALSE 0
- #define TRUE !FALSE
- #define max(a, b) (((a) > (b)) ? (a) : (b))
+ #include <windows.h>
+#else
+ #define FALSE 0
+ #define TRUE !FALSE
+ #define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
@@ -19,17 +19,16 @@
#include "font.h"
-
-struct ButtonStruct ButtonCreate(int f_dXPos, int f_dYPos, int f_dWidth, int f_dHeight, char *f_pcTitle, int f_iType, int f_iGroup, int f_iId)
+struct ButtonStruct ButtonCreate(int f_iXPos, int f_iYPos, int f_iWidth, int f_iHeight, char *f_pcTitle, int f_iType, int f_iGroup, int f_iId)
{
struct ButtonStruct l_sButton;
// copy parameters
- l_sButton.m_dXPos = f_dXPos;
- l_sButton.m_dYPos = f_dYPos;
+ l_sButton.m_iXPos = f_iXPos;
+ l_sButton.m_iYPos = f_iYPos;
l_sButton.m_iType = f_iType;
- l_sButton.m_dHeight = f_dHeight;
- l_sButton.m_dWidth = f_dWidth;
+ l_sButton.m_iHeight = f_iHeight;
+ l_sButton.m_iWidth = f_iWidth;
l_sButton.m_iGroup = f_iGroup;
l_sButton.m_iId = f_iId;
@@ -48,17 +47,17 @@ struct ButtonStruct ButtonCreate(int f_dXPos, int f_dYPos, int f_dWidth, int f_d
l_sButton.m_psImgHover[1] = BitmapLoad("img/button_click_center_hover.bmp");
l_sButton.m_psImgHover[2] = BitmapLoad("img/button_click_right_hover.bmp");
- BitmapConvertWidth(&l_sButton.m_psImgNormal[0], f_dHeight);
- BitmapConvertWidth(&l_sButton.m_psImgNormal[1], f_dHeight);
- BitmapConvertWidth(&l_sButton.m_psImgNormal[2], f_dHeight);
+ BitmapConvertWidth(&l_sButton.m_psImgNormal[0], f_iHeight);
+ BitmapConvertWidth(&l_sButton.m_psImgNormal[1], f_iHeight);
+ BitmapConvertWidth(&l_sButton.m_psImgNormal[2], f_iHeight);
- BitmapConvertWidth(&l_sButton.m_psImgHover[0], f_dHeight);
- BitmapConvertWidth(&l_sButton.m_psImgHover[1], f_dHeight);
- BitmapConvertWidth(&l_sButton.m_psImgHover[2], f_dHeight);
+ BitmapConvertWidth(&l_sButton.m_psImgHover[0], f_iHeight);
+ BitmapConvertWidth(&l_sButton.m_psImgHover[1], f_iHeight);
+ BitmapConvertWidth(&l_sButton.m_psImgHover[2], f_iHeight);
// calculate (center)width of image
- l_sButton.m_dCenterWidth = max(f_dWidth - l_sButton.m_psImgNormal[0].m_iWidth - l_sButton.m_psImgNormal[2].m_iWidth, 0);
- l_sButton.m_dWidth = l_sButton.m_dCenterWidth + l_sButton.m_psImgNormal[0].m_iWidth + l_sButton.m_psImgNormal[2].m_iWidth;
+ l_sButton.m_iCenterWidth = max(f_iWidth - l_sButton.m_psImgNormal[0].m_iWidth - l_sButton.m_psImgNormal[2].m_iWidth, 0);
+ l_sButton.m_iWidth = l_sButton.m_iCenterWidth + l_sButton.m_psImgNormal[0].m_iWidth + l_sButton.m_psImgNormal[2].m_iWidth;
break;
case BUTTON_RADIO:
@@ -66,13 +65,13 @@ struct ButtonStruct ButtonCreate(int f_dXPos, int f_dYPos, int f_dWidth, int f_d
l_sButton.m_psImgHover[0] = BitmapLoad("img/button_radio_hover.bmp");
l_sButton.m_psImgPressed[0] = BitmapLoad("img/button_radio_pressed.bmp");
- BitmapConvertWidth(&l_sButton.m_psImgNormal[0], f_dHeight);
- BitmapConvertWidth(&l_sButton.m_psImgHover[0], f_dHeight);
- BitmapConvertWidth(&l_sButton.m_psImgPressed[0], f_dHeight);
+ BitmapConvertWidth(&l_sButton.m_psImgNormal[0], f_iHeight);
+ BitmapConvertWidth(&l_sButton.m_psImgHover[0], f_iHeight);
+ BitmapConvertWidth(&l_sButton.m_psImgPressed[0], f_iHeight);
- l_sButton.m_dCenterWidth = 0;
- l_sButton.m_dHeight = f_dHeight;
- l_sButton.m_dWidth = f_dWidth;
+ l_sButton.m_iCenterWidth = 0;
+ l_sButton.m_iHeight = f_iHeight;
+ l_sButton.m_iWidth = f_iWidth;
break;
default:
@@ -87,38 +86,36 @@ struct ButtonStruct ButtonCreate(int f_dXPos, int f_dYPos, int f_dWidth, int f_d
} // ButtonCreate
-
void ButtonClickRender(struct ButtonStruct *f_sButton)
{
char l_pcTitle[BUTTON_MAX_TITLE];
struct BitmapStruct *l_sImgButton;
- int l_iImgWidth, l_iImageIdLeft, l_iImageIdCenter, l_iImageIdRight;
- int l_iLen, l_iXOffset, l_iYOffset;
- double l_dXOffset, l_dYOffset;
+ int l_iImageIdLeft, l_iImageIdCenter, l_iImageIdRight;
+ int l_iLen, l_iXOffset, l_iYOffset, l_iWidth;
struct ColorStruct l_sColor = {0, 0, 0};
// copy button attributes
- double l_dCnWdt = f_sButton->m_dCenterWidth;
- double l_dHeight = f_sButton->m_dHeight;
- double l_dXPos = f_sButton->m_dXPos;
- double l_dYPos = f_sButton->m_dYPos;
+ int l_iCnWdt = f_sButton->m_iCenterWidth;
+ int l_iHeight = f_sButton->m_iHeight;
+ int l_iXPos = f_sButton->m_iXPos;
+ int l_iYPos = f_sButton->m_iYPos;
- l_iLen = (int)strlen(f_sButton->m_pcTitle);
- l_iXOffset = (int)(0.5 * f_sButton->m_dWidth - 0.5 * l_iLen * FONT_SPACING);
- l_iYOffset = (int)(0.5 * f_sButton->m_dHeight - 0.5 * FONT_HEIGHT);
-
- if (f_sButton->m_bPressed) {
+ if (f_sButton->m_bPressed)
+ {
glColor3d(0.8, 0.9, 1);
}
- else {
+ else
+ {
glColor3d(1, 1, 1);
}
// get the right image struct
- if (f_sButton->m_bHover) {
+ if (f_sButton->m_bHover)
+ {
l_sImgButton = f_sButton->m_psImgHover;
}
- else {
+ else
+ {
l_sImgButton = f_sButton->m_psImgNormal;
}
@@ -131,76 +128,55 @@ void ButtonClickRender(struct ButtonStruct *f_sButton)
memcpy(&l_pcTitle, f_sButton->m_pcTitle, strlen(f_sButton->m_pcTitle));
// render left side of button
- glBindTexture(GL_TEXTURE_2D, l_iImageIdLeft);
-
- l_iImgWidth = l_sImgButton[0].m_iWidth;
- l_dXOffset = l_dXPos;
- l_dYOffset = l_dYPos;
-
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2d(0, 1); glVertex3d(l_dXOffset + 1, l_dYOffset, 0);
- glTexCoord2d(1, 1); glVertex3d(l_dXOffset + l_iImgWidth, l_dYOffset, 0);
- glTexCoord2d(0, 0); glVertex3d(l_dXOffset + 1, l_dYOffset + l_dHeight, 0);
- glTexCoord2d(1, 0); glVertex3d(l_dXOffset + l_iImgWidth, l_dYOffset + l_dHeight, 0);
- glEnd();
-
- l_dXOffset = l_dXPos + l_iImgWidth + l_dCnWdt;
+ l_iWidth = l_sImgButton[0].m_iWidth;
+ BitmapRender(l_iXPos,
+ l_iYPos,
+ l_iWidth + 1,
+ l_iHeight,
+ l_iImageIdLeft);
// render right side of button
- glBindTexture(GL_TEXTURE_2D, l_iImageIdRight);
-
- l_iImgWidth = l_sImgButton[2].m_iWidth;
-
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2d(0, 1); glVertex3d(l_dXOffset - 1, l_dYOffset, 0);
- glTexCoord2d(1, 1); glVertex3d(l_dXOffset + l_iImgWidth, l_dYOffset, 0);
- glTexCoord2d(0, 0); glVertex3d(l_dXOffset - 1, l_dYOffset + l_dHeight, 0);
- glTexCoord2d(1, 0); glVertex3d(l_dXOffset + l_iImgWidth, l_dYOffset + l_dHeight, 0);
- glEnd();
-
- l_dXOffset = l_dXOffset - l_dCnWdt;
+ l_iWidth = l_sImgButton[2].m_iWidth;
+ BitmapRender(l_iXPos + l_iCnWdt + l_iWidth - 1,
+ l_iYPos,
+ l_iWidth,
+ l_iHeight,
+ l_iImageIdRight);
// render center of button
- if (l_dCnWdt > 0)
+ if (l_iCnWdt > 0)
{
- glBindTexture(GL_TEXTURE_2D, l_iImageIdCenter);
-
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2d(0, 1); glVertex3d(l_dXOffset - 1, l_dYOffset, 0);
- glTexCoord2d(1, 1); glVertex3d(l_dXOffset + l_dCnWdt, l_dYOffset, 0);
- glTexCoord2d(0, 0); glVertex3d(l_dXOffset - 1, l_dYOffset + l_dHeight, 0);
- glTexCoord2d(1, 0); glVertex3d(l_dXOffset + l_dCnWdt, l_dYOffset + l_dHeight, 0);
- glEnd();
+ BitmapRender(l_iXPos + l_iWidth - 1,
+ l_iYPos,
+ l_iCnWdt + 1,
+ l_iHeight,
+ l_iImageIdCenter);
}
- if (f_sButton->m_bPressed)
- {
- glColor3d(1, 1, 1);
- }
+ l_iLen = (int)strlen(f_sButton->m_pcTitle);
+ l_iXOffset = (int)(0.5 * f_sButton->m_iWidth - 0.5 * l_iLen * FONT_SPACING);
+ l_iYOffset = (int)(0.5 * f_sButton->m_iHeight - 0.5 * FONT_HEIGHT);
- glPrint(f_sButton->m_dXPos + l_iXOffset,
- f_sButton->m_dYPos + l_iYOffset,
+ glPrint(f_sButton->m_iXPos + l_iXOffset,
+ f_sButton->m_iYPos + l_iYOffset,
f_sButton->m_pcTitle,
l_sColor);
} // ButtonClickRender
-
void ButtonRadioRender(struct ButtonStruct *f_sButton)
{
struct BitmapStruct *l_sImgButton;
- int l_iXOffset, l_iYOffset, l_iImageId;
+ int l_iXOffset, l_iYOffset, l_iImageRadioId, l_iImageEnabledId;
// copy button attributes
- double l_dWidth = f_sButton->m_dWidth;
- double l_dHeight = f_sButton->m_dHeight;
- double l_dXPos = f_sButton->m_dXPos;
- double l_dYPos = f_sButton->m_dYPos;
+ int l_iWidth = f_sButton->m_iWidth;
+ int l_iHeight = f_sButton->m_iHeight;
+ int l_iXPos = f_sButton->m_iXPos;
+ int l_iYPos = f_sButton->m_iYPos;
struct ColorStruct l_sColor = {0.2, 0.4, 0.8};
- glColor3d(1, 1, 1);
-
// get the right image struct
if (f_sButton->m_bHover)
{
@@ -211,45 +187,31 @@ void ButtonRadioRender(struct ButtonStruct *f_sButton)
l_sImgButton = f_sButton->m_psImgNormal;
}
- // copy buttom images
- l_iImageId = l_sImgButton[0].m_iImageId;
+ l_iImageRadioId = l_sImgButton[0].m_iImageId;
+ l_iImageEnabledId = f_sButton->m_psImgPressed[0].m_iImageId;
- // render left side of button
- glBindTexture(GL_TEXTURE_2D, l_iImageId);
+ glColor3d(1, 1, 1);
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2d(0, 1); glVertex3d(l_dXPos, l_dYPos, 0);
- glTexCoord2d(1, 1); glVertex3d(l_dXPos + l_dWidth, l_dYPos, 0);
- glTexCoord2d(0, 0); glVertex3d(l_dXPos, l_dYPos + l_dHeight, 0);
- glTexCoord2d(1, 0); glVertex3d(l_dXPos + l_dWidth, l_dYPos + l_dHeight, 0);
- glEnd();
+ // render the radio button
+ BitmapRender(l_iXPos, l_iYPos, l_iWidth, l_iHeight, l_iImageRadioId);
+ // render the enable-dot if button is pressed
if (f_sButton->m_bPressed)
{
- l_iImageId = f_sButton->m_psImgPressed[0].m_iImageId;
- glBindTexture(GL_TEXTURE_2D, l_iImageId);
-
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2d(0, 1); glVertex3d(l_dXPos, l_dYPos, 0);
- glTexCoord2d(1, 1); glVertex3d(l_dXPos + l_dWidth, l_dYPos, 0);
- glTexCoord2d(0, 0); glVertex3d(l_dXPos, l_dYPos + l_dHeight, 0);
- glTexCoord2d(1, 0); glVertex3d(l_dXPos + l_dWidth, l_dYPos + l_dHeight, 0);
- glEnd();
+ BitmapRender(l_iXPos, l_iYPos, l_iWidth, l_iHeight, l_iImageEnabledId);
}
- l_iXOffset = (int)f_sButton->m_dWidth;
- l_iYOffset = (int)(0.5 * f_sButton->m_dHeight - 0.5 * FONT_HEIGHT);
+ l_iXOffset = (int)f_sButton->m_iWidth;
+ l_iYOffset = (int)(0.5 * f_sButton->m_iHeight - 0.5 * FONT_HEIGHT);
- glPrint(f_sButton->m_dXPos + l_iXOffset,
- f_sButton->m_dYPos + l_iYOffset,
+ glPrint(f_sButton->m_iXPos + l_iXOffset,
+ f_sButton->m_iYPos + l_iYOffset,
f_sButton->m_pcTitle,
l_sColor);
} // ButtonRadioRender
-
-
void ButtonRender(struct ButtonStruct *f_sButton)
{
int l_iType = f_sButton->m_iType;
@@ -271,7 +233,6 @@ void ButtonRender(struct ButtonStruct *f_sButton)
} // ButtonRender
-
void ButtonEnter(struct ButtonStruct *f_sButton)
{
f_sButton->m_bHover = TRUE;
@@ -279,7 +240,6 @@ void ButtonEnter(struct ButtonStruct *f_sButton)
} // ButtonHover
-
void ButtonExit(struct ButtonStruct *f_sButton)
{
f_sButton->m_bHover = FALSE;
@@ -287,7 +247,6 @@ void ButtonExit(struct ButtonStruct *f_sButton)
} // ButtonHover
-
void ButtonClickPress(struct ButtonStruct *f_sButton)
{
if (!f_sButton->m_bPressed) {
@@ -297,7 +256,6 @@ void ButtonClickPress(struct ButtonStruct *f_sButton)
} // ButtonClickPress
-
void ButtonRadioPress(struct ButtonStruct *f_sButton)
{
int i = 0;
@@ -310,7 +268,6 @@ void ButtonRadioPress(struct ButtonStruct *f_sButton)
} // ButtonClickPress
-
void ButtonPress(struct ButtonStruct *f_sButton)
{
int l_iType = f_sButton->m_iType;
@@ -332,7 +289,6 @@ void ButtonPress(struct ButtonStruct *f_sButton)
} // ButtonPress
-
void ButtonClickRelease(struct ButtonStruct *f_sButton)
{
ButtonDisable(f_sButton);
@@ -340,7 +296,6 @@ void ButtonClickRelease(struct ButtonStruct *f_sButton)
} // ButtonClickRelease
-
void ButtonRelease(struct ButtonStruct *f_sButton)
{
int l_iType = f_sButton->m_iType;
@@ -359,7 +314,6 @@ void ButtonRelease(struct ButtonStruct *f_sButton)
} // ButtonRelease
-
void ButtonEnable(struct ButtonStruct *f_sButton)
{
f_sButton->m_bPressed = TRUE;
@@ -371,4 +325,4 @@ void ButtonDisable(struct ButtonStruct *f_sButton)
{
f_sButton->m_bPressed = FALSE;
-} // ButtonDisable
+} // ButtonDisable