#ifdef G_OS_WIN32 #define WIN32_LEAN_AND_MEAN 1 #include #endif #include #include #include #include "button.h" struct ButtonStruct ButtonCreate(double f_dXPos, double f_dYPos, double f_dWidth, char *f_pcTitle, int f_iType) { struct ButtonStruct l_sButton; // copy parameters l_sButton.m_dXPos = f_dXPos; l_sButton.m_dYPos = f_dYPos; l_sButton.m_iType = f_iType; l_sButton.m_dWidth = f_dWidth; // copy title memcpy(l_sButton.m_pcTitle, f_pcTitle, sizeof(f_pcTitle)); switch (f_iType) { case BUTTON_CLICK: l_sButton.m_piImgNormal[0] = BitmapLoad("img/button_click_left.bmp"); l_sButton.m_piImgNormal[1] = BitmapLoad("img/button_click_center.bmp"); l_sButton.m_piImgNormal[2] = BitmapLoad("img/button_click_right.bmp"); l_sButton.m_piImgPressed[0] = BitmapLoad("img/button_click_left_hover.bmp"); l_sButton.m_piImgPressed[1] = BitmapLoad("img/button_click_center_hover.bmp"); l_sButton.m_piImgPressed[2] = BitmapLoad("img/button_click_right_hover.bmp"); // calculate width of center image l_sButton.m_dCenterWidth = f_dWidth - l_sButton.m_piImgNormal[0].m_iWidth - l_sButton.m_piImgNormal[2].m_iWidth; // retrieve image height l_sButton.m_dHeight = l_sButton.m_piImgNormal[0].m_iHeight; break; case BUTTON_RADIO: case BUTTON_LABEL: default: break; } l_sButton.m_bPressed = FALSE; l_sButton.m_bHover = FALSE; return l_sButton; } // ButtonCreate void ButtonClickRender(struct ButtonStruct *f_sButton) { char l_pcTitle[BUTTON_MAX_TITLE]; // copy button attributes double l_dWidth = f_sButton->m_dCenterWidth; double l_dXPos = f_sButton->m_dXPos; double l_dYPos = f_sButton->m_dYPos; int l_iType = f_sButton->m_iType; // copy buttom images int l_iImageIdLeft = (!f_sButton->m_bHover) ? f_sButton->m_piImgNormal[0].m_iImageId : f_sButton->m_piImgPressed[0].m_iImageId; int l_iImageIdCenter = (!f_sButton->m_bHover) ? f_sButton->m_piImgNormal[1].m_iImageId : f_sButton->m_piImgPressed[1].m_iImageId; int l_iImageIdRight = (!f_sButton->m_bHover) ? f_sButton->m_piImgNormal[2].m_iImageId : f_sButton->m_piImgPressed[2].m_iImageId; // copy button title memset(&l_pcTitle, 0, sizeof(l_pcTitle)); memcpy(&l_pcTitle, f_sButton->m_pcTitle, sizeof(f_sButton->m_pcTitle)); // render left side of button glBindTexture(GL_TEXTURE_2D, l_iImageIdLeft); glBegin(GL_QUADS); glTexCoord3d(0, 1, 0); glVertex3d(l_dXPos, l_dYPos, 0); glTexCoord3d(1, 1, 0); glVertex3d(l_dXPos + 26, l_dYPos, 0); glTexCoord3d(1, 0, 0); glVertex3d(l_dXPos + 26, l_dYPos + BUTTON_CLICK_HEIGHT, 0); glTexCoord3d(0, 0, 0); glVertex3d(l_dXPos, l_dYPos + BUTTON_CLICK_HEIGHT, 0); glEnd(); // render right side of button glBindTexture(GL_TEXTURE_2D, l_iImageIdRight); glBegin(GL_QUADS); glTexCoord3d(0, 1, 0); glVertex3d(l_dXPos + 24 + l_dWidth, l_dYPos, 0); glTexCoord3d(1, 1, 0); glVertex3d(l_dXPos + 24 + l_dWidth + 25, l_dYPos, 0); glTexCoord3d(1, 0, 0); glVertex3d(l_dXPos + 24 + l_dWidth + 25, l_dYPos + BUTTON_CLICK_HEIGHT, 0); glTexCoord3d(0, 0, 0); glVertex3d(l_dXPos + 24 + l_dWidth, l_dYPos + BUTTON_CLICK_HEIGHT, 0); glEnd(); // render center of button if (l_dWidth > 0) { glBindTexture(GL_TEXTURE_2D, l_iImageIdCenter); glBegin(GL_QUADS); glTexCoord3d(0, 1, 0); glVertex3d(l_dXPos + 25, l_dYPos, 0); glTexCoord3d(1, 1, 0); glVertex3d(l_dXPos + 25 + l_dWidth, l_dYPos, 0); glTexCoord3d(1, 0, 0); glVertex3d(l_dXPos + 25 + l_dWidth, l_dYPos + BUTTON_CLICK_HEIGHT, 0); glTexCoord3d(0, 0, 0); glVertex3d(l_dXPos + 25, l_dYPos + BUTTON_CLICK_HEIGHT, 0); glEnd(); } } // ButtonClickRender void ButtonRender(struct ButtonStruct *f_sButton) { int l_iType = f_sButton->m_iType; switch (l_iType) { case BUTTON_CLICK: ButtonClickRender(f_sButton); break; case BUTTON_RADIO: case BUTTON_LABEL: default: break; } } // ButtonRender void ButtonHover(struct ButtonStruct *f_sButton) { f_sButton->m_bHover = TRUE; } // ButtonHover