Completed menu functionality, added two functions roundUp and makeSubmenuFromArray...
[atyndall/cits2231.git] / scene.c
diff --git a/scene.c b/scene.c
index 53b3187..f9df3c8 100644 (file)
--- a/scene.c
+++ b/scene.c
@@ -246,10 +246,35 @@ void processLightEvents(int id) {
 \r
 }\r
 \r
+/**\r
+ * Rounds up numbers, from http://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number\r
+ * @param numToRound Number to round\r
+ * @param multiple Multiple to round up to\r
+ * @return Rounded number\r
+ */\r
+int roundUp(int numToRound, int multiple) {\r
+  if(multiple == 0) {\r
+    return numToRound;\r
+  }\r
+\r
+  int remainder = numToRound % multiple;\r
+  if (remainder == 0)\r
+    return numToRound;\r
+  return numToRound + multiple - remainder;\r
+}\r
+\r
+/**\r
+ * Makes a submenu from an array of items, splitting the list into subsubmenus\r
+ * of only 10 items.\r
+ * @param menuEntries Array of menu items\r
+ * @param menuEntriesSize Size of menuEntries\r
+ * @param callback Callback function for this array of menu items\r
+ * @return Reference to menu created\r
+ */\r
 int makeSubmenuFromArray( const char *menuEntries[], unsigned int menuEntriesSize, void *callback ) {\r
   if ( menuEntriesSize == 0 ) return -1;\r
 \r
-  int menuNumber = menuEntriesSize / 10 + 1;\r
+  int menuNumber = roundUp(menuEntriesSize, 10) / 10;\r
   int submenuObjects[menuNumber-1];\r
 \r
   for( int i = 0; i < menuNumber; i++ ) {\r
@@ -257,9 +282,9 @@ int makeSubmenuFromArray( const char *menuEntries[], unsigned int menuEntriesSiz
     int startNum = i*11 - (i-1);\r
     for ( int j = startNum - 1; j < (startNum+9); j++ ) {\r
       if ( j == menuEntriesSize ) break; // Detect if we've reached the end of the array\r
-      glutAddMenuEntry( menuEntries[j], j );\r
+      glutAddMenuEntry( menuEntries[j], j + 1 );\r
     }\r
-  }\r
+  } \r
 \r
   int mainMenu = glutCreateMenu(callback);\r
   for ( int i = 0; i < menuNumber; i++ ) {\r

UCC git Repository :: git.ucc.asn.au