ECE2035 class project
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
Fork of missile_command by
city_landscape/city_landscape.cpp@1:3da29f1d84b4, 2014-10-29 (annotated)
- Committer:
- arvahsu
- Date:
- Wed Oct 29 02:58:53 2014 +0000
- Revision:
- 1:3da29f1d84b4
- Parent:
- 0:532cb55d6136
- Child:
- 2:d39a6a36e0c0
fix typo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arvahsu | 0:532cb55d6136 | 1 | /* Gatech ECE2035 2014 FALL missile command |
arvahsu | 0:532cb55d6136 | 2 | * Copyright (c) 2014 Gatech ECE2035 |
arvahsu | 0:532cb55d6136 | 3 | * |
arvahsu | 0:532cb55d6136 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
arvahsu | 0:532cb55d6136 | 5 | * of this software and associated documentation files (the "Software"), to deal |
arvahsu | 0:532cb55d6136 | 6 | * in the Software without restriction, including without limitation the rights |
arvahsu | 0:532cb55d6136 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
arvahsu | 0:532cb55d6136 | 8 | * copies of the Software, and to permit persons to whom the Software is |
arvahsu | 0:532cb55d6136 | 9 | * furnished to do so, subject to the following conditions: |
arvahsu | 0:532cb55d6136 | 10 | * |
arvahsu | 0:532cb55d6136 | 11 | * The above copyright notice and this permission notice shall be included in |
arvahsu | 0:532cb55d6136 | 12 | * all copies or substantial portions of the Software. |
arvahsu | 0:532cb55d6136 | 13 | * |
arvahsu | 0:532cb55d6136 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
arvahsu | 0:532cb55d6136 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
arvahsu | 0:532cb55d6136 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
arvahsu | 0:532cb55d6136 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
arvahsu | 0:532cb55d6136 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
arvahsu | 0:532cb55d6136 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
arvahsu | 0:532cb55d6136 | 20 | * SOFTWARE. |
arvahsu | 0:532cb55d6136 | 21 | */ |
arvahsu | 0:532cb55d6136 | 22 | |
arvahsu | 0:532cb55d6136 | 23 | #include "city_landscape_private.h" |
arvahsu | 0:532cb55d6136 | 24 | |
arvahsu | 0:532cb55d6136 | 25 | CITY city_record[MAX_NUM_CITY]; |
arvahsu | 0:532cb55d6136 | 26 | int building_height[NUM_BUILDING]; |
arvahsu | 0:532cb55d6136 | 27 | |
arvahsu | 0:532cb55d6136 | 28 | // See the comments in city_landscape_public.h |
arvahsu | 0:532cb55d6136 | 29 | void city_landscape_init(int num_city) { |
arvahsu | 0:532cb55d6136 | 30 | int i; |
arvahsu | 0:532cb55d6136 | 31 | int city_distance = (SIZE_X-CITY_TO_SCREEN_MARGIN*2)/num_city; |
arvahsu | 0:532cb55d6136 | 32 | |
arvahsu | 0:532cb55d6136 | 33 | // All interface for user should have error checking |
arvahsu | 0:532cb55d6136 | 34 | ASSERT_P(num_city<=MAX_NUM_CITY,ERROR_CITY_NUMBER); |
arvahsu | 0:532cb55d6136 | 35 | |
arvahsu | 0:532cb55d6136 | 36 | //initialize the record of cities |
arvahsu | 0:532cb55d6136 | 37 | for(i=0;i<MAX_NUM_CITY;i++){ |
arvahsu | 0:532cb55d6136 | 38 | if(i<num_city){ |
arvahsu | 0:532cb55d6136 | 39 | // See the definition of CITY structure in city_landscape.h |
arvahsu | 0:532cb55d6136 | 40 | city_record[i].y = REVERSE_Y(LANDSCAPE_HEIGHT)-1; |
arvahsu | 0:532cb55d6136 | 41 | city_record[i].x = i*city_distance + CITY_TO_SCREEN_MARGIN; |
arvahsu | 0:532cb55d6136 | 42 | city_record[i].width = CITY_WIDTH; // the width is fix number |
arvahsu | 0:532cb55d6136 | 43 | city_record[i].height = MAX_BUILDING_HEIGHT; // the height is fix number |
arvahsu | 0:532cb55d6136 | 44 | city_record[i].status = EXIST; |
arvahsu | 0:532cb55d6136 | 45 | } |
arvahsu | 0:532cb55d6136 | 46 | else{ |
arvahsu | 1:3da29f1d84b4 | 47 | city_record[i].status = DESTROYED; |
arvahsu | 0:532cb55d6136 | 48 | } |
arvahsu | 0:532cb55d6136 | 49 | } |
arvahsu | 0:532cb55d6136 | 50 | |
arvahsu | 0:532cb55d6136 | 51 | //initialize the height of the buildings |
arvahsu | 0:532cb55d6136 | 52 | srand(1); |
arvahsu | 0:532cb55d6136 | 53 | for(i=0;i<NUM_BUILDING;i++){ |
arvahsu | 0:532cb55d6136 | 54 | building_height[i] = (rand() % MAX_BUILDING_HEIGHT*2/3)+MAX_BUILDING_HEIGHT/3; |
arvahsu | 0:532cb55d6136 | 55 | } |
arvahsu | 0:532cb55d6136 | 56 | |
arvahsu | 0:532cb55d6136 | 57 | //draw city landscape on the screen |
arvahsu | 0:532cb55d6136 | 58 | draw_cities(); |
arvahsu | 0:532cb55d6136 | 59 | draw_landscape(); |
arvahsu | 0:532cb55d6136 | 60 | |
arvahsu | 0:532cb55d6136 | 61 | } |
arvahsu | 0:532cb55d6136 | 62 | |
arvahsu | 0:532cb55d6136 | 63 | CITY city_get_info(int index){ |
arvahsu | 0:532cb55d6136 | 64 | // All interface for user should have error checking |
arvahsu | 0:532cb55d6136 | 65 | ASSERT_P(index<MAX_NUM_CITY,ERROR_CITY_INDEX_GET_INFO); |
arvahsu | 0:532cb55d6136 | 66 | |
arvahsu | 0:532cb55d6136 | 67 | return city_record[index]; |
arvahsu | 0:532cb55d6136 | 68 | } |
arvahsu | 0:532cb55d6136 | 69 | |
arvahsu | 1:3da29f1d84b4 | 70 | void city_destroy(int index){ |
arvahsu | 0:532cb55d6136 | 71 | int j; |
arvahsu | 0:532cb55d6136 | 72 | int city_x, city_y, building_x, building_y; |
arvahsu | 0:532cb55d6136 | 73 | int height; |
arvahsu | 0:532cb55d6136 | 74 | |
arvahsu | 0:532cb55d6136 | 75 | // error checking. the index must smaller than its max. |
arvahsu | 0:532cb55d6136 | 76 | ASSERT_P(index<MAX_NUM_CITY,ERROR_CITY_INDEX_DESTROY); |
arvahsu | 0:532cb55d6136 | 77 | |
arvahsu | 0:532cb55d6136 | 78 | // remove the record |
arvahsu | 1:3da29f1d84b4 | 79 | city_record[index].status = DESTROYED; |
arvahsu | 0:532cb55d6136 | 80 | |
arvahsu | 0:532cb55d6136 | 81 | // use the background color to cover the city |
arvahsu | 0:532cb55d6136 | 82 | city_x = city_record[index].x; |
arvahsu | 0:532cb55d6136 | 83 | city_y = city_record[index].y; |
arvahsu | 0:532cb55d6136 | 84 | for(j=0;j<NUM_BUILDING;j++){ |
arvahsu | 0:532cb55d6136 | 85 | building_x = city_x+j*BUILDING_WIDTH; |
arvahsu | 0:532cb55d6136 | 86 | building_y = city_y; |
arvahsu | 0:532cb55d6136 | 87 | height = building_y-building_height[j]+1; |
arvahsu | 0:532cb55d6136 | 88 | uLCD.filled_rectangle(building_x, building_y, building_x+BUILDING_WIDTH-1, height, BACKGROUND_COLOR); |
arvahsu | 0:532cb55d6136 | 89 | } |
arvahsu | 0:532cb55d6136 | 90 | } |
arvahsu | 0:532cb55d6136 | 91 | |
arvahsu | 0:532cb55d6136 | 92 | void draw_cities(void){ |
arvahsu | 0:532cb55d6136 | 93 | int i,j; |
arvahsu | 0:532cb55d6136 | 94 | int city_x, city_y, building_x, building_y; |
arvahsu | 0:532cb55d6136 | 95 | int height; |
arvahsu | 0:532cb55d6136 | 96 | |
arvahsu | 0:532cb55d6136 | 97 | for(i=0;i<MAX_NUM_CITY;i++){ |
arvahsu | 0:532cb55d6136 | 98 | |
arvahsu | 0:532cb55d6136 | 99 | // draw each city |
arvahsu | 0:532cb55d6136 | 100 | if(city_record[i].status==EXIST){ |
arvahsu | 0:532cb55d6136 | 101 | city_x = city_record[i].x; |
arvahsu | 0:532cb55d6136 | 102 | city_y = city_record[i].y; |
arvahsu | 0:532cb55d6136 | 103 | |
arvahsu | 0:532cb55d6136 | 104 | // draw each building |
arvahsu | 0:532cb55d6136 | 105 | for(j=0;j<NUM_BUILDING;j++){ |
arvahsu | 0:532cb55d6136 | 106 | building_x = city_x+j*BUILDING_WIDTH; |
arvahsu | 0:532cb55d6136 | 107 | building_y = city_y; |
arvahsu | 0:532cb55d6136 | 108 | height = building_y-building_height[j]+1; |
arvahsu | 0:532cb55d6136 | 109 | uLCD.filled_rectangle(building_x, building_y, building_x+BUILDING_WIDTH-1, height, BUILDING_COLOR); |
arvahsu | 0:532cb55d6136 | 110 | } |
arvahsu | 0:532cb55d6136 | 111 | } |
arvahsu | 0:532cb55d6136 | 112 | } |
arvahsu | 0:532cb55d6136 | 113 | } |
arvahsu | 0:532cb55d6136 | 114 | |
arvahsu | 0:532cb55d6136 | 115 | void draw_landscape(void){ |
arvahsu | 0:532cb55d6136 | 116 | uLCD.filled_rectangle(0, SIZE_Y-1, SIZE_X-1, REVERSE_Y(LANDSCAPE_HEIGHT), LANDSCAPE_COLOR); |
arvahsu | 0:532cb55d6136 | 117 | } |