X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=scene.c;h=f9df3c82a42f345abf0964399cae38116c7ecd1c;hb=599da39add7dc804a35f79bd1033a33d9ab048ac;hp=f4467cc9ae0d23d17eb1bc7e76bb8a5205f2e529;hpb=5bdb30f5054f2e6e4831b0fea17b50b56ce7bb10;p=atyndall%2Fcits2231.git diff --git a/scene.c b/scene.c index f4467cc..f9df3c8 100644 --- a/scene.c +++ b/scene.c @@ -246,9 +246,35 @@ void processLightEvents(int id) { } -int makeSubmenuFromArray( const char *menuEntries[], void *callback ) { - int menuEntriesSize = 54;//sizeof(menuEntries) / sizeof(menuEntries[0]); - int menuNumber = menuEntriesSize / 10 + 1; +/** + * Rounds up numbers, from http://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number + * @param numToRound Number to round + * @param multiple Multiple to round up to + * @return Rounded number + */ +int roundUp(int numToRound, int multiple) { + if(multiple == 0) { + return numToRound; + } + + int remainder = numToRound % multiple; + if (remainder == 0) + return numToRound; + return numToRound + multiple - remainder; +} + +/** + * Makes a submenu from an array of items, splitting the list into subsubmenus + * of only 10 items. + * @param menuEntries Array of menu items + * @param menuEntriesSize Size of menuEntries + * @param callback Callback function for this array of menu items + * @return Reference to menu created + */ +int makeSubmenuFromArray( const char *menuEntries[], unsigned int menuEntriesSize, void *callback ) { + if ( menuEntriesSize == 0 ) return -1; + + int menuNumber = roundUp(menuEntriesSize, 10) / 10; int submenuObjects[menuNumber-1]; for( int i = 0; i < menuNumber; i++ ) { @@ -256,9 +282,9 @@ int makeSubmenuFromArray( const char *menuEntries[], void *callback ) { int startNum = i*11 - (i-1); for ( int j = startNum - 1; j < (startNum+9); j++ ) { if ( j == menuEntriesSize ) break; // Detect if we've reached the end of the array - glutAddMenuEntry( menuEntries[j], j ); + glutAddMenuEntry( menuEntries[j], j + 1 ); } - } + } int mainMenu = glutCreateMenu(callback); for ( int i = 0; i < menuNumber; i++ ) { @@ -266,7 +292,7 @@ int makeSubmenuFromArray( const char *menuEntries[], void *callback ) { int startNum = i*11 - (i-1); int endNum = startNum + 9; if ( i == menuNumber - 1 ) { // We're on the last one - endNum = startNum + (menuEntriesSize - startNum); + endNum = startNum + (menuEntriesSize - startNum); // Work out final number } sprintf(name, "%d-%d", startNum, endNum); glutAddSubMenu( name, submenuObjects[i] ); @@ -294,44 +320,28 @@ void makeMenu() { glutAddMenuEntry("Move Light 2", LIGHT_MOVE_LIGHT_2); glutAddMenuEntry("R/G/B/All Light 2", LIGHT_RGBALL_LIGHT_2); - // Construct add object submenus - int addObjectMenu = makeSubmenuFromArray( objectMenuEntries, processObjectEvents ); - - // Construct texture/ground texture submenus -/* int textureMenuEntries = sizeof(textureMenuEntries) / sizeof(textureMenuEntries[0]); - int menuNumber = textureMenuEntries / 10 + 1; - int textureSubmenu[menuNumber-1]; - - for( int i = 0; i < menuNumber; i++ ) { - textureSubmenu[i] = glutCreateMenu(processTextureEvents); - int startNum = i*11 - (i-1); - for ( int j = startNum - 1; j < (startNum+9); j++ ) { - if ( j == objectMenuEntriesSize ) break; // Detect if we've reached the end of the array - glutAddMenuEntry( objectMenuEntries[j], j ); - } - }*/ + // Construct object menu + int objectMenuEntriesSize = sizeof(objectMenuEntries) / sizeof(objectMenuEntries[0]); + int objectMenu = makeSubmenuFromArray( objectMenuEntries, objectMenuEntriesSize, processObjectEvents ); + // Construct texture / ground texture menus + int textureMenuEntriesSize = sizeof(textureMenuEntries) / sizeof(textureMenuEntries[0]); + int textureMenu = makeSubmenuFromArray( textureMenuEntries, textureMenuEntriesSize, processTextureEvents ); + int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, textureMenuEntriesSize, processGTextureEvents ); + // Construct main menu int mainMenu = glutCreateMenu(processMainEvents); - glutAddMenuEntry("Rotate/Move Camera", ROTATE_MOVE_CAMERA); - glutAddSubMenu("Add object", addObjectMenu); + glutAddSubMenu("Add object", objectMenu); glutAddMenuEntry("Position/Scale", POSITION_SCALE); glutAddMenuEntry("Rotation/Texture Scale", ROTATION_TEXTURE_SCALE); - //material - - - - glutSetMenu(mainMenu); glutAddSubMenu("Material", materialMenu); - - //texture - //ground texture - //lights - - + glutAddSubMenu("Texture", textureMenu); + glutAddSubMenu("Ground texture", gTextureMenu); + glutAddSubMenu("Lights", lightMenu); glutAddMenuEntry("Exit", EXIT); + // Bind to right mouse button glutAttachMenu(GLUT_RIGHT_BUTTON); }