/[projet1]/users/barnsey123/grid/main-correct.c
Defence Force logotype

Contents of /users/barnsey123/grid/main-correct.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 451 - (show annotations)
Fri Apr 15 07:36:17 2011 UTC (9 years, 1 month ago) by barnsey123
File MIME type: text/plain
File size: 6326 byte(s)


1 // main.c by Barnsey123
2 // 22-03-2011 prog to draw a grid (for viking game)
3 // 23-03-2011 create new drawgrid function
4 // 01-04-2011 create new drawtile functions and use compact code (char instead of int etc)
5 // 02-04-2011 routine to read keyboard
6 // 06-04-2011 cursor drawing correct
7
8 #include <lib.h>
9 // DRAWBOX - draw a box at x,y length xbox
10 void drawbox(int x, int y, int z)
11 {
12 /*
13 x=xcoord for curset
14 y=ycoord for curset
15 z=size of line to draw
16 */
17 curset(x,y,1);
18 draw(z,0,1);
19 draw(0,z,1);
20 draw(-z,0,1);
21 draw(0,-z,1);
22 }
23 // DRAW CURSOR
24 void drawcursor(int x, int y, int z)
25 {
26 pattern(170);
27 curset(x,y,1);
28 draw(z,0,1);
29 draw(0,z,1);
30 draw(-z,0,1);
31 draw(0,-z,1);
32 pattern(255);
33 }
34 // blank cursor
35 void drawblankcursor(int x, int y, int z)
36 {
37 pattern(170);
38 curset(x,y,0);
39 draw(z,0,0);
40 draw(0,z,0);
41 draw(-z,0,0);
42 draw(0,-z,0);
43 pattern(255);
44 }
45 // DRAW DEFENDERS TILE
46 void drawdefendtile(int x, int y)
47 {
48 // draws the attackers tile (a series of 3 concentric squares )
49 // ARGUMENTS starting x, y position for curset
50 unsigned char loop=0; // loop counter
51 int z=13; // line length
52 for (loop=0;loop<2;loop++)
53 {
54 drawbox(x,y,z); // drawbox at x,y position
55 z=z-4; // decrease size of line
56 x+=2; // increase x position
57 y+=2; // increase y position
58 }
59 }
60 // DRAW ATTACKERS TILE
61 void drawattacktile(int x, int y)
62 {
63 // draws the attackers tile ( a series of 2 concentric squares )
64 // ARGUMENTS starting x, y position for curset
65 unsigned char loop=0; // loop counter
66 int z=13; // line length
67 for (loop=0;loop<3;loop++)
68 {
69 drawbox(x,y,z); // drawbox at x,y position
70 z=z-4; // decrease size of line
71 x+=2; // increase x position
72 y+=2; // increase y position
73 }
74 }
75 // DRAW KINGS TILE
76 void drawkingtile(int x, int y)
77 {
78 /* draws the king tile ( a series of 5 sqares - one in each corner of the tile and one
79 square straddling all 4 ) nice, simple pattern.
80 ARGUMENTS starting x, y position for curset
81 */
82 drawbox(x,y,5); // box 1
83 drawbox(x+8,y,5); // box 2
84 drawbox(x,y+8,5); // box 3
85 drawbox(x+8,y+8,5); // box 4
86 drawbox(x+2,y+2,9); // box 5 (central, larger box)
87 }
88 // DRAW ALL THE TILES ON THE BOARD
89 void drawtiles(const int x, const int y, const int z)
90 {
91 /* routine to draw the "background tiles"
92 Tile types:
93 0=blank
94 1=king square
95 2=attacker square
96 3=defender square
97 */
98 int tilex=x; // starting tile x position
99 int tiley=y; // starting tile y position
100 unsigned char row=0; // loop through tiles array by row
101 unsigned char col=0; // loop through tiles array by column
102 // Populate array with tile types
103 unsigned char tiles[11][11]={
104 {1,0,0,2,2,2,2,2,0,0,1},
105 {0,0,0,0,0,2,0,0,0,0,0},
106 {0,0,0,0,0,0,0,0,0,0,0},
107 {2,0,0,0,0,3,0,0,0,0,2},
108 {2,0,0,0,3,3,3,0,0,0,2},
109 {2,2,0,3,3,1,3,3,0,2,2},
110 {2,0,0,0,3,3,3,0,0,0,2},
111 {2,0,0,0,0,3,0,0,0,0,2},
112 {0,0,0,0,0,0,0,0,0,0,0},
113 {0,0,0,0,0,2,0,0,0,0,0},
114 {1,0,0,2,2,2,2,2,0,0,1}};
115 tilex+=2; // starting tile x position (offset by 2 to avoid grid lines)
116 tiley+=2; // starting tile y position (offset by 2 to avoid grid lines)
117 for (row=0;row<11;row++)
118 {
119 for (col=0;col<11;col++)
120 {
121 if ( tiles[row][col] == 1 ) drawkingtile(tilex,tiley); // draw king tile
122 if ( tiles[row][col] == 2 ) drawattacktile(tilex,tiley); // draw attackers tile
123 if ( tiles[row][col] == 3 ) drawdefendtile(tilex,tiley); // draw defenders tile
124 tilex+=z; // the x position is advanced to the next square "across" - including blank squares
125 }
126 tilex=x; // reset x
127 tilex+=2; // tilex offset to avoid gridlines
128 tiley+=z; // advance tiley position to next "row"
129 }
130 }
131 // DRAW THE MAIN 11x11 GRID
132 void drawgrid(const int x, const int y, const int z)
133 {
134 int a=x; // copy of boardx (board offset x position)
135 int b=y; // copy of boardy (board offset y position)
136 unsigned char c=0; // loop counter
137 int length=z; // copy of boxsize
138 length*=11; // multiply length by 11 (determines length of line to draw)
139 for (c=0;c<=11;c++) // draw vertical lines
140 {
141 curset(a,b,1);
142 draw(0,length,1);
143 a+=z; // add boxsize to a
144 }
145 a=x; // reset a
146 for (c=0;c<=11;c++) // draw horizontal lines
147 {
148 curset(a,b,1);
149 draw(length,0,1);
150 b+=z; // add boxsize to b
151 }
152 }
153 // DRAW THE BOARD
154 void drawboard(const int x, const int y, const int z)
155 {
156 hires();
157 ink(6); // color of board
158 drawgrid(x,y,z); // draw the 11x11 gridlines
159 drawtiles(x,y,z); // draw the background tiles
160 }
161 // READKEYB
162 void readkeyb() // filter keyboard input
163 {
164 int key=0;
165 int cx=98;
166 int cy=86;
167 unsigned char ns=5, ew=5; // north-south=5, east-west=5
168 //coordx=5*17;
169 //coordy=coordx;
170 //coordx=coordx+xboardx;
171 //coordy=coordy+xboardy;
172 //printf("\n coordx=%d:",coordx);
173 //printf("\n coordy=%d:",coordy);
174 drawcursor(cx,cy,15); // print dotted cursor at central square
175 while (key != 81) // repeat until 'Q' pressed
176 {
177 key=getchar(); // get code of pressed key
178 if ((key == 8 )&&(ew>0)) // cursor left
179 {
180 drawblankcursor(cx,cy,15); // print blank cursor (effect=remove dots)
181 cx=cx-17;
182 drawcursor(cx,cy,15); // print dotted cursor
183 ew--;
184 }
185 if ((key == 9)&&(ew<10)) // cursor right
186 {
187 drawblankcursor(cx,cy,15); // print blank cursor (effect=remove dots)
188 cx=cx+17;
189 drawcursor(cx,cy,15); // print dotted cursor
190 ew++;
191 }
192 if ((key == 10)&&(ns<10)) // cursor down
193 {
194 drawblankcursor(cx,cy,15); // print blank cursor (effect=remove dots)
195 cy=cy+17;
196 drawcursor(cx,cy,15); // print dotted cursor
197 ns++;
198 }
199 if ((key == 11)&&(ns>0)) // cursor up
200 {
201 drawblankcursor(cx,cy,15); // print blank cursor (effect=remove dots)
202 cy=cy-17;
203 drawcursor(cx,cy,15); // print dotted cursor
204 ns--;
205 }
206 // printf("\nKEY=%d",key); // debugging - prints code of key pressed
207 }
208 printf ("\n*** Q pressed ***"); // print message when Q is pressed
209 }
210 main()
211 {
212 const int boardx=12; // starting x co-ord of board
213 const int boardy=0; // starting y co-ord of board
214 const int boxsize=17; // set boxsize
215 drawboard(boardx,boardy,boxsize); // draw the board
216 readkeyb(); // player input
217
218 }

  ViewVC Help
Powered by ViewVC 1.1.26