summaryrefslogtreecommitdiffstats
path: root/menu_demo/menu.c
blob: e6a15ee70f3d8a2634fe0d15f43deb60741b5bad (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifdef G_OS_WIN32
  #define WIN32_LEAN_AND_MEAN 1
  #include <windows.h>
#endif

#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>

#include "menu.h"
#include "button.h"
#include "font.h"

#define MENU_MAX_BUTTONS 16

enum MenuState {
  MENU_MAIN,
  MENU_START,
  MENU_OPTIONS
};

struct MenuStruct {
  enum MenuState m_eMenuId;
  int m_iButtonCount;
  struct ButtonStruct m_sButtons[MENU_MAX_BUTTONS];
};

struct MenuStruct  g_sMenuMain;    // definition of the main menu
struct MenuStruct  g_sMenuStart;   // definition of the start menu
struct MenuStruct  g_sMenuOptions; // definition of the options menu
struct MenuStruct *g_pCurMenu;     // current menu

int g_iWinWidth;
int g_iWinHeight;



void MenuAddButton(int f_dXPos, int f_dYPos, int f_dWidth, int f_dHeight, char *f_pcTitle, int f_iType)
{
  int i;
  
  i = g_sMenuMain.m_iButtonCount;
  g_sMenuMain.m_sButtons[i] = ButtonCreate(f_dXPos, f_dYPos, f_dWidth, f_dHeight, f_pcTitle, f_iType);

  i++;
  g_sMenuMain.m_iButtonCount = i;

} // MenuAddButton



void MenuInit(int f_iWinWidth, int f_iWinHeight)
{
  // init menu props
  g_iWinWidth  = f_iWinWidth;
  g_iWinHeight = f_iWinHeight;

  // init main menu
  g_sMenuMain.m_eMenuId = MENU_MAIN;
  MenuAddButton(32, 16,  256, 128, "Button 1", BUTTON_CLICK);
  MenuAddButton(32, 128, 256, 128, "Button 2", BUTTON_CLICK);
  MenuAddButton(312, 16, 110, 110, "Radio 1", BUTTON_RADIO);

  // init start menu
  g_sMenuStart.m_eMenuId = MENU_START;
  g_sMenuStart.m_iButtonCount = 0;

  // init options menu
  g_sMenuOptions.m_eMenuId = MENU_OPTIONS;
  g_sMenuOptions.m_iButtonCount = 0;

  // set pointer to the current menu
  g_pCurMenu = &g_sMenuMain;

  // init font
  FontInit(f_iWinWidth, f_iWinHeight);

} // MenuInit



void MenuRender(void)
{
  int i;

  glDisable(GL_DEPTH_TEST);
  glEnable(GL_TEXTURE_2D);

  glColor4d(1, 1, 1, 1);

  glMatrixMode(GL_PROJECTION);
  glPushMatrix();

    glLoadIdentity();
    glOrtho(0, g_iWinWidth, g_iWinHeight, 0, 0, 1);
    glMatrixMode(GL_MODELVIEW);

    glEnable(GL_BLEND);
    //glBlendFunc(GL_ONE, GL_ZERO);

    //glColor4d(0.25, 0.5, 1, 0.1);
    //glBegin(GL_TRIANGLE_STRIP);
    //  glVertex2i(0, 0);
    //  glVertex2i(g_iWinWidth, 0);
    //  glVertex2i(0, g_iWinHeight);
    //  glVertex2i(g_iWinWidth, g_iWinHeight);
    //glEnd();

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // render the buttons of the current menu
    i = 0;
    while (i < g_pCurMenu->m_iButtonCount)
    {
      ButtonRender(&g_pCurMenu->m_sButtons[i]);
      i++;
    }

    glDisable(GL_BLEND);

    //restore the previous projection matrix
    glMatrixMode(GL_PROJECTION);

  glPopMatrix();

} // MenuRender



void MenuMouseHandle(int f_iGlutButton, int f_iGlutState, int f_iXPos, int f_iYPos)
{
  int i = 0;
  double l_dXPos, l_dYPos;
  double l_iWidth, l_iHeight;

  // f_iGlutButton:
  // # GLUT_LEFT_BUTTON
  // # GLUT_MIDDLE_BUTTON
  // # GLUT_RIGHT_BUTTON

  // f_iGlutState
  // # GLUT_DOWN
  // # GLUT_UP
  
  // button released
  if (f_iGlutButton == GLUT_LEFT_BUTTON  && f_iGlutState == GLUT_UP)
  {
    i = 0;
    while (i < g_pCurMenu->m_iButtonCount)
    {
      if (ButtonGetType(&g_pCurMenu->m_sButtons[i]) == BUTTON_CLICK)
      {
        ButtonRelease(&g_pCurMenu->m_sButtons[i]);
      }
      i++;
    }
  }

  // check if any button needs attention from the mouse
  i = 0;
  while (i < g_pCurMenu->m_iButtonCount)
  {
    l_dXPos   = g_pCurMenu->m_sButtons[i].m_dXPos;
    l_dYPos   = g_pCurMenu->m_sButtons[i].m_dYPos;
    l_iWidth  = g_pCurMenu->m_sButtons[i].m_dWidth;
    l_iHeight = g_pCurMenu->m_sButtons[i].m_dHeight;

    if (l_dXPos <= f_iXPos && l_dXPos + l_iWidth >= f_iXPos &&l_dYPos <= f_iYPos && l_dYPos + l_iHeight >= f_iYPos)
    {
      ButtonEnter(&g_pCurMenu->m_sButtons[i]);

      // button pressed?
      if (f_iGlutButton == GLUT_LEFT_BUTTON  && f_iGlutState == GLUT_DOWN)
      {
        ButtonPress(&g_pCurMenu->m_sButtons[i]);
      }
    }
    else // mouse leaves button
    {
      ButtonExit(&g_pCurMenu->m_sButtons[i]);
    }
    i++;
  }

} // MenuMouseHandle