summaryrefslogtreecommitdiffstats
path: root/menu_demo/button.c
blob: 365619b38fc1631e670ccf00a8f0036df2376f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifdef G_OS_WIN32
  #define WIN32_LEAN_AND_MEAN 1
  #include <windows.h>
#endif

#include <GL/gl.h>
#include <string.h>
#include <stdio.h>

#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