| 1 | #include <SDL.h> |
| 2 | #include "files.h" |
| 3 | #include "menu.h" |
| 4 | #include "draw.h" |
| 5 | #include "dfont.h" |
| 6 | |
| 7 | |
| 8 | SDL_Surface *menu_screen; |
| 9 | |
| 10 | int Main_Menu(int *mission_number, SDL_Surface *screen) |
| 11 | { |
| 12 | SDL_Rect dest; |
| 13 | int command = 0; |
| 14 | int selected_button = BUTTON_PLAY; |
| 15 | int action; |
| 16 | Uint32 menu_colour = SDL_MapRGB(screen->format, 0, 200,0); |
| 17 | menu_screen = screen; |
| 18 | |
| 19 | dest.x = 400; dest.y = 0; dest.w = 624; dest.h = 768; |
| 20 | Display_Mission(*mission_number); |
| 21 | while (! command) |
| 22 | { |
| 23 | Draw_Main_Menu(selected_button, menu_colour); |
| 24 | SDL_UpdateRects(menu_screen, 1, &dest); |
| 25 | action = Menu_Input(); |
| 26 | if (action == BUTTON_ACTIVATE) |
| 27 | action = selected_button; |
| 28 | switch (action) |
| 29 | { |
| 30 | case BUTTON_PLAY: command = ACTION_MISSION; break; |
| 31 | case BUTTON_QUIT: command = ACTION_QUIT; break; |
| 32 | case BUTTON_PREV_MISSION: |
| 33 | selected_button = BUTTON_PREV_MISSION; |
| 34 | *mission_number = New_Mission(MISSION_PREVIOUS, *mission_number); |
| 35 | Display_Mission(*mission_number); |
| 36 | break; |
| 37 | case BUTTON_NEXT_MISSION: |
| 38 | selected_button = BUTTON_NEXT_MISSION; |
| 39 | *mission_number = New_Mission(MISSION_NEXT, *mission_number); |
| 40 | Display_Mission(*mission_number); |
| 41 | break; |
| 42 | case BUTTON_MOVE_UP: |
| 43 | selected_button--; |
| 44 | if (selected_button < BUTTON_PLAY) |
| 45 | selected_button = BUTTON_NEXT_MISSION; |
| 46 | break; |
| 47 | case BUTTON_MOVE_DOWN: |
| 48 | selected_button++; |
| 49 | if (selected_button > BUTTON_NEXT_MISSION) |
| 50 | selected_button = BUTTON_PLAY; |
| 51 | break; |
| 52 | |
| 53 | } |
| 54 | SDL_Delay(100); |
| 55 | } |
| 56 | |
| 57 | #ifdef DEBUG |
| 58 | printf("Returning menu command: %d\n", command); |
| 59 | #endif |
| 60 | return command; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | int New_Mission(int next_prev, int old_mission) |
| 66 | { |
| 67 | FILE *mission_file; |
| 68 | char *mission_path; |
| 69 | char mission_filename[256]; |
| 70 | int new_mission; |
| 71 | |
| 72 | if (next_prev == MISSION_PREVIOUS) |
| 73 | { |
| 74 | new_mission = old_mission - 1; |
| 75 | if (new_mission < 0) |
| 76 | new_mission = 0; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | new_mission = old_mission + 1; |
| 81 | } |
| 82 | |
| 83 | #ifndef WIN32 |
| 84 | snprintf(mission_filename, 256, "data/mission%d.dat", new_mission); |
| 85 | #else |
| 86 | sprintf(mission_filename, "data/mission%d.dat", new_mission); |
| 87 | #endif |
| 88 | mission_path = Find_Data_File(mission_filename); |
| 89 | mission_file = fopen(mission_path, "r"); |
| 90 | if ( (mission_path) && (mission_path != mission_filename) ) |
| 91 | free(mission_path); |
| 92 | |
| 93 | // found mission file, return the new number |
| 94 | if (mission_file) |
| 95 | { |
| 96 | fclose(mission_file); |
| 97 | return new_mission; |
| 98 | } |
| 99 | |
| 100 | // we do not have a matching mission file, return the last good one |
| 101 | return old_mission; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | int Display_Mission(int mission_number) |
| 107 | { |
| 108 | char *full_path, filename[256]; |
| 109 | FILE *mission_file; |
| 110 | char line[256]; |
| 111 | int y = 610; |
| 112 | DFont my_font("images/font.png", "data/font.dat"); |
| 113 | Uint32 menu_background = SDL_MapRGB(menu_screen->format, 0, 0, 0); |
| 114 | |
| 115 | #ifndef WIN32 |
| 116 | snprintf(filename, 256, "data/orders%d.txt", mission_number); |
| 117 | #else |
| 118 | sprintf(filename, "data/orders%d.txt", mission_number); |
| 119 | #endif |
| 120 | full_path = Find_Data_File(filename); |
| 121 | mission_file = fopen(full_path, "r"); |
| 122 | if ( (full_path) && (full_path != filename) ) |
| 123 | free(full_path); |
| 124 | if (mission_file) |
| 125 | { |
| 126 | FillRectangle(menu_screen, 440, 610, 980, 730, menu_background); |
| 127 | full_path = fgets(line, 256, mission_file); |
| 128 | while (full_path) |
| 129 | { |
| 130 | my_font.PutString(menu_screen, 440, y, line); |
| 131 | full_path = fgets(line, 256, mission_file); |
| 132 | y += 12; |
| 133 | } |
| 134 | fclose(mission_file); |
| 135 | } |
| 136 | return TRUE; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | int Draw_Main_Menu(int selected, Uint32 menu_colour) |
| 142 | { |
| 143 | Uint32 menu_background = SDL_MapRGB(menu_screen->format, 0, 0, 0); |
| 144 | // wipe everything first |
| 145 | DrawRectangle(menu_screen, 540, 160, 885, 235, menu_background); |
| 146 | DrawRectangle(menu_screen, 540, 260, 885, 335, menu_background); |
| 147 | FillCircle(menu_screen, 465, 565, 5, menu_background); |
| 148 | FillCircle(menu_screen, 940, 565, 5, menu_background); |
| 149 | |
| 150 | switch (selected) |
| 151 | { |
| 152 | case BUTTON_PLAY: |
| 153 | DrawRectangle(menu_screen, 540, 160, 885, 235, menu_colour); |
| 154 | break; |
| 155 | case BUTTON_QUIT: |
| 156 | DrawRectangle(menu_screen, 540, 260, 885, 335, menu_colour); |
| 157 | break; |
| 158 | case BUTTON_PREV_MISSION: |
| 159 | FillCircle(menu_screen, 465, 565, 5, menu_colour); |
| 160 | break; |
| 161 | case BUTTON_NEXT_MISSION: |
| 162 | FillCircle(menu_screen, 940, 565, 5, menu_colour); |
| 163 | break; |
| 164 | |
| 165 | } |
| 166 | return TRUE; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | int Menu_Input() |
| 172 | { |
| 173 | int action = 0; |
| 174 | SDL_Event event; |
| 175 | int mousex, mousey; |
| 176 | |
| 177 | #ifdef DEBUG |
| 178 | printf("About to check for event.\n"); |
| 179 | #endif |
| 180 | while ( SDL_PollEvent(&event) ) |
| 181 | { |
| 182 | #ifdef DEBUG |
| 183 | printf("Got event.\n"); |
| 184 | #endif |
| 185 | switch (event.type) |
| 186 | { |
| 187 | case SDL_MOUSEBUTTONDOWN: |
| 188 | mousex = event.button.x; |
| 189 | mousey = event.button.y; |
| 190 | if ( (mousex > 540) && (mousex < 885) && |
| 191 | (mousey > 160) && (mousey < 235) ) |
| 192 | action = BUTTON_PLAY; |
| 193 | else if ( (mousex > 540) && (mousex < 885) && |
| 194 | (mousey > 260) && (mousey < 335) ) |
| 195 | action = BUTTON_QUIT; |
| 196 | else if ( (mousex > 440) && (mousex < 475) && |
| 197 | (mousey > 550) && (mousey < 580) ) |
| 198 | action = BUTTON_PREV_MISSION; |
| 199 | else if ( (mousex > 920) && (mousex < 960) && |
| 200 | (mousey > 550) && (mousey < 580) ) |
| 201 | action = BUTTON_NEXT_MISSION; |
| 202 | |
| 203 | break; // end of mouse stuff |
| 204 | case SDL_KEYDOWN: |
| 205 | if (event.key.keysym.sym == SDLK_UP) |
| 206 | action = BUTTON_MOVE_UP; |
| 207 | else if (event.key.keysym.sym == SDLK_DOWN) |
| 208 | action = BUTTON_MOVE_DOWN; |
| 209 | else if (event.key.keysym.sym == SDLK_ESCAPE) |
| 210 | action = BUTTON_QUIT; |
| 211 | else if ( (event.key.keysym.sym == SDLK_SPACE) || |
| 212 | (event.key.keysym.sym == SDLK_RETURN) ) |
| 213 | action = BUTTON_ACTIVATE; |
| 214 | break; |
| 215 | } // end of switch |
| 216 | } // end of which event |
| 217 | |
| 218 | #ifdef DEBUG |
| 219 | printf("Returning action: %d\n", action); |
| 220 | #endif |
| 221 | return action; |
| 222 | } |
| 223 |