From d20cf957944bf243b7496d4c6eed47edad37cda9 Mon Sep 17 00:00:00 2001 From: Wilrik de Loose Date: Thu, 17 Apr 2008 13:34:36 +0000 Subject: Textured + hover buttons --- menu_demo/Debug/BuildLog.htm | Bin 7164 -> 7086 bytes menu_demo/Debug/main.obj | Bin 13290 -> 13527 bytes menu_demo/Debug/menu.obj | Bin 9423 -> 12620 bytes menu_demo/Debug/menu_demo.exe | Bin 516096 -> 520192 bytes menu_demo/Debug/menu_demo.ilk | Bin 1204032 -> 1216652 bytes menu_demo/Debug/menu_demo.pdb | Bin 2075648 -> 2100224 bytes menu_demo/Debug/mt.dep | 2 +- menu_demo/Debug/vc80.idb | Bin 125952 -> 125952 bytes menu_demo/Debug/vc80.pdb | Bin 61440 -> 69632 bytes menu_demo/bitmap.c | 121 ++++++++++++++++++++++++------------------ menu_demo/bitmap.h | 8 ++- menu_demo/button.c | 91 ++++++++++++++++++++++++++----- menu_demo/button.h | 25 +++++++-- menu_demo/main.c | 54 +++++++++++++------ menu_demo/menu.c | 89 ++++++++++++++++++++++++++----- menu_demo/menu.h | 1 + menu_demo/menu_demo.suo | Bin 20480 -> 20992 bytes 17 files changed, 292 insertions(+), 99 deletions(-) diff --git a/menu_demo/Debug/BuildLog.htm b/menu_demo/Debug/BuildLog.htm index 7eef82b..7f8a7b8 100644 Binary files a/menu_demo/Debug/BuildLog.htm and b/menu_demo/Debug/BuildLog.htm differ diff --git a/menu_demo/Debug/main.obj b/menu_demo/Debug/main.obj index 93f703d..8903e96 100644 Binary files a/menu_demo/Debug/main.obj and b/menu_demo/Debug/main.obj differ diff --git a/menu_demo/Debug/menu.obj b/menu_demo/Debug/menu.obj index 3e829eb..778e34a 100644 Binary files a/menu_demo/Debug/menu.obj and b/menu_demo/Debug/menu.obj differ diff --git a/menu_demo/Debug/menu_demo.exe b/menu_demo/Debug/menu_demo.exe index ec6ec8e..098e87c 100644 Binary files a/menu_demo/Debug/menu_demo.exe and b/menu_demo/Debug/menu_demo.exe differ diff --git a/menu_demo/Debug/menu_demo.ilk b/menu_demo/Debug/menu_demo.ilk index 372a9b1..2dd334b 100644 Binary files a/menu_demo/Debug/menu_demo.ilk and b/menu_demo/Debug/menu_demo.ilk differ diff --git a/menu_demo/Debug/menu_demo.pdb b/menu_demo/Debug/menu_demo.pdb index 2653140..47c70fd 100644 Binary files a/menu_demo/Debug/menu_demo.pdb and b/menu_demo/Debug/menu_demo.pdb differ diff --git a/menu_demo/Debug/mt.dep b/menu_demo/Debug/mt.dep index 5e3a360..419dc51 100644 --- a/menu_demo/Debug/mt.dep +++ b/menu_demo/Debug/mt.dep @@ -1 +1 @@ -Manifest resource last updated at 17:03:43,17 on 16-04-2008 +Manifest resource last updated at 15:25:11,03 on 17-04-2008 diff --git a/menu_demo/Debug/vc80.idb b/menu_demo/Debug/vc80.idb index f50aada..3ed2827 100644 Binary files a/menu_demo/Debug/vc80.idb and b/menu_demo/Debug/vc80.idb differ diff --git a/menu_demo/Debug/vc80.pdb b/menu_demo/Debug/vc80.pdb index 5ce9f7c..21f28dd 100644 Binary files a/menu_demo/Debug/vc80.pdb and b/menu_demo/Debug/vc80.pdb differ diff --git a/menu_demo/bitmap.c b/menu_demo/bitmap.c index 1a32f68..edb981d 100644 --- a/menu_demo/bitmap.c +++ b/menu_demo/bitmap.c @@ -2,55 +2,72 @@ #define WIN32_LEAN_AND_MEAN 1 #include #define GL_BGR 0x80E0 -#endif - -#include -#include -#include - -#include "bitmap.h" - -#define BITMAP_FILESIZE 0x02 -#define BITMAP_OFFSET 0x0a -#define BITMAP_HEADERSIZE 0x0e -#define BITMAP_WIDTH 0x12 -#define BITMAP_HEIGHT 0x16 - -void BitmapLoad(char *filename) -{ - GLuint texture; - FILE *bitmap; - - // try to open the file - bitmap = fopen(filename, "rb"); - - // does the bitmap exist? - if (bitmap > 0) - { - unsigned int dataoffset, filesize; - GLsizei width, height; - unsigned char *imagedata; - - fseek(bitmap, BITMAP_FILESIZE, SEEK_SET); - fread(&filesize, 4, 1, bitmap); - fseek(bitmap, BITMAP_OFFSET, SEEK_SET); - fread(&dataoffset, 4, 1, bitmap); - fseek(bitmap, BITMAP_WIDTH, SEEK_SET); - fread(&width, 4, 1, bitmap); - fread(&height, 4, 1, bitmap); - - imagedata = (unsigned char *)malloc((size_t)(filesize -dataoffset)); - - fseek(bitmap, dataoffset, SEEK_SET); - fread(imagedata, (size_t)(filesize -dataoffset), 1, bitmap); - - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_BGR, GL_UNSIGNED_BYTE, imagedata); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - free(imagedata); - fclose(bitmap); - } -} +#endif + +#include +#include +#include +#include + +#include "bitmap.h" + +#define BITMAP_FILESIZE 0x02 +#define BITMAP_OFFSET 0x0a +#define BITMAP_HEADERSIZE 0x0e +#define BITMAP_WIDTH 0x12 +#define BITMAP_HEIGHT 0x16 + + + +struct ImageStruct BitmapLoad(char *filename) +{ + struct ImageStruct l_sImage; + GLuint texture; + FILE *bitmap; + + // try to open the file + bitmap = fopen(filename, "r"); + + // does the bitmap exist? + if (bitmap > 0) + { + unsigned int dataoffset, filesize; + GLsizei width, height; + unsigned char *imagedata; + + fseek(bitmap, BITMAP_FILESIZE, SEEK_SET); + fread(&filesize, 4, 1, bitmap); + fseek(bitmap, BITMAP_OFFSET, SEEK_SET); + fread(&dataoffset, 4, 1, bitmap); + fseek(bitmap, BITMAP_WIDTH, SEEK_SET); + fread(&width, 4, 1, bitmap); + fread(&height, 4, 1, bitmap); + + imagedata = (unsigned char *)malloc((size_t)(filesize -dataoffset)); + + fseek(bitmap, dataoffset, SEEK_SET); + fread(imagedata, (size_t)(filesize -dataoffset), 1, bitmap); + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_BGR, GL_UNSIGNED_BYTE, imagedata); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + l_sImage.m_iImageId = texture; + l_sImage.m_iWidth = width; + l_sImage.m_iHeight = height; + + free(imagedata); + fclose(bitmap); + + return l_sImage; + } + else + { + // couldn't find bitmap + exit(0); + } +} + + diff --git a/menu_demo/bitmap.h b/menu_demo/bitmap.h index 9e68dae..dc300b5 100644 --- a/menu_demo/bitmap.h +++ b/menu_demo/bitmap.h @@ -1,6 +1,12 @@ #ifndef _CBITMAP_H #define _CBITMAP_H -void BitmapLoad(char *filename); +struct ImageStruct { + int m_iImageId; + int m_iWidth; + int m_iHeight; +}; + +struct ImageStruct BitmapLoad(char *filename); #endif diff --git a/menu_demo/button.c b/menu_demo/button.c index 7df7b08..365619b 100644 --- a/menu_demo/button.c +++ b/menu_demo/button.c @@ -4,56 +4,115 @@ #endif #include +#include +#include #include "button.h" -#include "bitmap.h" -#define BUTTON_CLICK_HEIGHT 49 + + +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]; - double l_dWidth = f_sButton->m_dWidth; + // 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)); - BitmapLoad("img/button_click_left.bmp"); + // 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); + 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(); - BitmapLoad("img/button_click_right.bmp"); + // 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); + 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) { - BitmapLoad("img/button_click_center.bmp"); + 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); + 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; @@ -71,3 +130,11 @@ void ButtonRender(struct ButtonStruct *f_sButton) } } // ButtonRender + + + +void ButtonHover(struct ButtonStruct *f_sButton) +{ + f_sButton->m_bHover = TRUE; + +} // ButtonHover diff --git a/menu_demo/button.h b/menu_demo/button.h index 77d365e..3103a5d 100644 --- a/menu_demo/button.h +++ b/menu_demo/button.h @@ -1,14 +1,29 @@ #ifndef _CBUTTON_H #define _CBUTTON_H +#include "bitmap.h" + #define BUTTON_MAX_TITLE 256 +#define BUTTON_MAX_IMGS 3 +#define BUTTON_CLICK_HEIGHT 49 struct ButtonStruct { - double m_dXPos; - double m_dYPos; - double m_dWidth; + double m_dXPos; // x positie + double m_dYPos; // y positie + double m_dCenterWidth; // breedte van middenstuk + double m_dWidth; // totale breedte + double m_dHeight; // totale hoogte + int m_bPressed; // pressed or not + int m_bHover; // mouse hovering or not + int m_iType; // button type + + // button caption/title char m_pcTitle[BUTTON_MAX_TITLE]; - int m_iType; + + // button images + struct ImageStruct m_piImgNormal[BUTTON_MAX_IMGS]; + struct ImageStruct m_piImgHover[BUTTON_MAX_IMGS]; + struct ImageStruct m_piImgPressed[BUTTON_MAX_IMGS]; }; enum ButtonType { @@ -17,6 +32,8 @@ enum ButtonType { BUTTON_LABEL }; +struct ButtonStruct ButtonCreate(double f_dXPos, double f_dYPos, double f_dWidth, char *f_pcTitle, int f_iType); void ButtonRender(struct ButtonStruct *f_sButton); +void ButtonHover(struct ButtonStruct *f_sButton); #endif diff --git a/menu_demo/main.c b/menu_demo/main.c index 89e0b31..3903002 100644 --- a/menu_demo/main.c +++ b/menu_demo/main.c @@ -12,23 +12,15 @@ #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 + + void init_gl(void) { - double l_dHalfWidth = SCREEN_WIDTH / 2; - double l_dHalfHeight = SCREEN_HEIGHT / 2; + // do sumting - glMatrixMode(GL_PROJECTION); +} // init_gl - glOrtho(-l_dHalfWidth, - l_dHalfWidth, - -l_dHalfHeight, - l_dHalfHeight, - -1, - 1 - ); - glMatrixMode(GL_MODELVIEW); -} void render_scene(void) { @@ -38,13 +30,19 @@ void render_scene(void) MenuRender(); glutSwapBuffers(); -} + +} // render_scene + + void process_normal_keys(unsigned char key, int x, int y) { // escape if (key == 27) exit(0); -} + +} // process_normal_keys + + void process_special_keys(int key, int x, int y) { @@ -52,7 +50,26 @@ void process_special_keys(int key, int x, int y) { // do sumting } -} + +} // process_special_keys + + + +void process_mouse(int button, int state, int x, int y) +{ + MenuMouseHandle(button, state, x, y); + +} // process_mouse + + + +void process_passive_mouse(int x, int y) +{ + process_mouse(0, 0, x, y); + +} // process_passive_mouse + + int main(int argc, char **argv) { @@ -64,6 +81,8 @@ int main(int argc, char **argv) glutIdleFunc(render_scene); glutKeyboardFunc(process_normal_keys); glutSpecialFunc(process_special_keys); + glutMouseFunc(process_mouse); + glutPassiveMotionFunc(process_passive_mouse); init_gl(); MenuInit(SCREEN_WIDTH, SCREEN_HEIGHT); @@ -71,4 +90,7 @@ int main(int argc, char **argv) glutMainLoop(); return 0; -} + +} // main + + diff --git a/menu_demo/menu.c b/menu_demo/menu.c index 265c4d8..9159d13 100644 --- a/menu_demo/menu.c +++ b/menu_demo/menu.c @@ -32,6 +32,22 @@ struct MenuStruct *g_pCurMenu; // current menu int g_iWinWidth; int g_iWinHeight; + + +void MenuAddButton(double f_dXPos, double f_dYPos, double f_dWidth, 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_pcTitle, f_iType); + + i++; + g_sMenuMain.m_iButtonCount = i; + +} // MenuAddButton + + + void MenuInit(int f_iWinWidth, int f_iWinHeight) { // init menu props @@ -40,12 +56,7 @@ void MenuInit(int f_iWinWidth, int f_iWinHeight) // init main menu g_sMenuMain.m_eMenuId = MENU_MAIN; - g_sMenuMain.m_sButtons[0].m_dWidth = 200; - g_sMenuMain.m_sButtons[0].m_dXPos = 0; - g_sMenuMain.m_sButtons[0].m_dYPos = 0; - g_sMenuMain.m_sButtons[0].m_iType = BUTTON_CLICK; - - g_sMenuMain.m_iButtonCount = 1; + MenuAddButton(0, 0, 200, "Start", BUTTON_CLICK); // init start menu g_sMenuStart.m_eMenuId = MENU_START; @@ -58,7 +69,9 @@ void MenuInit(int f_iWinWidth, int f_iWinHeight) // set pointer to the current menu g_pCurMenu = &g_sMenuMain; -} // InitMenu +} // MenuInit + + void MenuRender(void) { @@ -67,16 +80,66 @@ void MenuRender(void) glDisable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); - glColor3d(1, 1, 1); - - glPushMatrix(); - + glColor3d(1, 1, 1); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + + glLoadIdentity(); + glOrtho(0, (GLfloat)g_iWinWidth, (GLfloat)g_iWinHeight, 0, 0, 1); + glMatrixMode(GL_MODELVIEW); + + // render the buttons on the current menu while (i < g_pCurMenu->m_iButtonCount) { ButtonRender(&g_pCurMenu->m_sButtons[i]); i++; + } + + //restore the previous projection matrix + glMatrixMode(GL_PROJECTION); + + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + +} // 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 + + // check if any button needs attention from the mouse + 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) + { + ButtonHover(&g_pCurMenu->m_sButtons[i]); } + else + { + g_pCurMenu->m_sButtons[i].m_bHover = FALSE; + } + i++; + } + +} // MenuMouseHandle - glPopMatrix(); -} // Render diff --git a/menu_demo/menu.h b/menu_demo/menu.h index b574b74..b89c1aa 100644 --- a/menu_demo/menu.h +++ b/menu_demo/menu.h @@ -3,5 +3,6 @@ void MenuInit(int f_iWinWidth, int f_iWinHeight); void MenuRender(void); +void MenuMouseHandle(int button, int state, int x, int y); #endif diff --git a/menu_demo/menu_demo.suo b/menu_demo/menu_demo.suo index debaa58..31ed2a0 100644 Binary files a/menu_demo/menu_demo.suo and b/menu_demo/menu_demo.suo differ -- cgit v0.12