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

Contents of /users/barnsey123/grid/main.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: 8458 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 // 11-04-2011 request help from DBUG - bug in OSDK
8 // 14-04-2011 DBUG fixed OSDK bug!
9 // 15-04-2011 tidied code up
10
11 #include <lib.h>
12 /******************* Function Declarations ************************/
13 void drawbox(unsigned char,unsigned char,unsigned char); // draws a box at x,y length z
14 void drawcursor(unsigned char,unsigned char,unsigned char,unsigned char); // draws cursor at x,y, length z, foreground or background
15 void drawdefendtile(unsigned char,unsigned char); // draws defenders background tile at x,y
16 void drawattacktile(unsigned char,unsigned char); // draws attackers background tile at x,y
17 void drawkingtile(unsigned char,unsigned char); // draws kings backround tile at x,y
18 void drawtiles(unsigned char,unsigned char,unsigned char); // draws all tiles at board x,y boxsize z (uses draw*tile functions)
19 void drawgrid(unsigned char,unsigned char,unsigned char); // draws grid at board x,y boxsize z
20 void drawboard(unsigned char,unsigned char,unsigned char); // kicks off drawgrid/drawtiles
21 void playerturn(unsigned char,unsigned char,unsigned char); // takes user input to move cursor
22 /****************** GLOBAL VARIABLES *******************************/
23
24 /****************** MAIN PROGRAM ***********************************/
25 main()
26 {
27 const unsigned char boardx=12; // starting x co-ord of board
28 const unsigned char boardy=0; // starting y co-ord of board
29 const unsigned char boxsize=17; // set boxsize
30 drawboard(boardx,boardy,boxsize,version); // draw the board
31 playerturn(boardx,boardy,boxsize); // player input
32 }
33 /********************* FUNCTION DEFINITIONS ************************/
34 // DRAWBOX
35 void drawbox(unsigned char x, unsigned char y, unsigned char z)
36 {
37 /* - draw a box at x,y size z
38 x=xcoord for curset
39 y=ycoord for curset
40 z=size of line to draw
41 */
42 curset(x,y,1);
43 draw(z,0,1);
44 draw(0,z,1);
45 draw(-z,0,1);
46 draw(0,-z,1);
47 }
48 // DRAWCURSOR
49 void drawcursor(unsigned char x, unsigned char y, unsigned char z, unsigned char f)
50 {
51 /* - draw the cursor at x,y size z, foreground/background color (1 or 0)
52 x=xcoord for curset
53 y=ycoord for curset
54 z=size of line to draw
55 f=foreground/background 1=visible, 0=blank (to overwrite old cursor position)
56 */
57 pattern(170);
58 curset(x,y,f);
59 draw(z,0,f);
60 draw(0,z,f);
61 draw(-z,0,f);
62 draw(0,-z,f);
63 pattern(255);
64 }
65 // DRAW DEFENDERS TILE
66 void drawdefendtile(unsigned char x, unsigned char y)
67 {
68 /* draws the defenders tile (a series of 2 concentric squares )
69 x=xcoord for drawbox
70 y=xcoord for drawbox
71 */
72 unsigned char a=x; // copy of x
73 unsigned char b=y; // copy of y
74 unsigned char loop=0; // loop counter
75 unsigned char z=13; // line length
76 for (loop=0;loop<2;loop++)
77 {
78 drawbox(a,b,z); // drawbox at x,y position
79 z=z-4; // decrease size of line
80 a+=2; // increase x position
81 b+=2; // increase y position
82 }
83 }
84 // DRAW ATTACKERS TILE
85 void drawattacktile(unsigned char x, unsigned char y)
86 {
87 /* draws the attackers tile ( a series of 3 concentric squares )
88 x=xcoord for drawbox
89 y=xcoord for drawbox
90 */
91 unsigned char a=x; // copy of x
92 unsigned char b=y; // copy of y
93 unsigned char loop=0; // loop counter
94 unsigned char z=13; // line length
95 for (loop=0;loop<3;loop++)
96 {
97 drawbox(a,b,z); // drawbox at x,y position
98 z=z-4; // decrease size of line
99 a+=2; // increase x position
100 b+=2; // increase y position
101 }
102 }
103 // DRAW KINGS TILE
104 void drawkingtile(unsigned char x, unsigned char y)
105 {
106 /* draws the king tile ( a series of 5 sqares - one in each corner of the tile and one
107 square straddling all 4 ) nice, simple pattern.
108 x=xcoord for drawbox
109 y=xcoord for drawbox
110 */
111 drawbox(x,y,5); // box 1
112 drawbox(x+8,y,5); // box 2
113 drawbox(x,y+8,5); // box 3
114 drawbox(x+8,y+8,5); // box 4
115 drawbox(x+2,y+2,9); // box 5 (central, larger box)
116 }
117 // DRAW ALL THE TILES ON THE BOARD
118 void drawtiles(unsigned char x, unsigned char y, unsigned char z)
119 {
120 /* routine to draw the "background tiles"
121 Tile types:
122 0=blank
123 1=king square
124 2=attacker square
125 3=defender square
126 Arguments:
127 x=starting x coord
128 y=starting y coord
129 z=boxsize
130 */
131 unsigned char tilex=x; // starting tile x position
132 unsigned char tiley=y; // starting tile y position
133 unsigned char row=0; // loop through tiles array by row
134 unsigned char col=0; // loop through tiles array by column
135 // Populate array with tile types
136 unsigned char tiles[11][11]={
137 {1,0,0,2,2,2,2,2,0,0,1},
138 {0,0,0,0,0,2,0,0,0,0,0},
139 {0,0,0,0,0,0,0,0,0,0,0},
140 {2,0,0,0,0,3,0,0,0,0,2},
141 {2,0,0,0,3,3,3,0,0,0,2},
142 {2,2,0,3,3,1,3,3,0,2,2},
143 {2,0,0,0,3,3,3,0,0,0,2},
144 {2,0,0,0,0,3,0,0,0,0,2},
145 {0,0,0,0,0,0,0,0,0,0,0},
146 {0,0,0,0,0,2,0,0,0,0,0},
147 {1,0,0,2,2,2,2,2,0,0,1}};
148 tilex+=2; // starting tile x position (offset by 2 to avoid grid lines)
149 tiley+=2; // starting tile y position (offset by 2 to avoid grid lines)
150 for (row=0;row<11;row++)
151 {
152 for (col=0;col<11;col++)
153 {
154 if ( tiles[row][col] == 1 ) drawkingtile(tilex,tiley); // draw king tile
155 if ( tiles[row][col] == 2 ) drawattacktile(tilex,tiley); // draw attackers tile
156 if ( tiles[row][col] == 3 ) drawdefendtile(tilex,tiley); // draw defenders tile
157 tilex+=z; // the x position is advanced to the next square "across" - including blank squares
158 }
159 tilex=x; // reset x
160 tilex+=2; // tilex offset to avoid gridlines
161 tiley+=z; // advance tiley position to next "row"
162 }
163 }
164 // DRAWGRID
165 void drawgrid(unsigned char x, unsigned char y, unsigned char z)
166 {
167 /* drawgrid - draws main 11x11 grid
168 x=starting x position
169 y=starting y position
170 z=boxsize
171 */
172 unsigned char a=x; // copy of boardx (board offset x position)
173 unsigned char b=y; // copy of boardy (board offset y position)
174 unsigned char c=0; // loop counter
175 unsigned char length=z; // copy of boxsize
176 length*=11; // multiply length by 11 (determines length of line to draw)
177 for (c=0;c<=11;c++) // draw vertical lines
178 {
179 curset(a,b,1);
180 draw(0,length,1);
181 a+=z; // add boxsize to a
182 }
183 a=x; // reset a
184 for (c=0;c<=11;c++) // draw horizontal lines
185 {
186 curset(a,b,1);
187 draw(length,0,1);
188 b+=z; // add boxsize to b
189 }
190 }
191 // DRAW THE BOARD
192 void drawboard(unsigned char x, unsigned char y, unsigned char z)
193 {
194 /* drawboard
195 x=starting x coord
196 y=starting y coord
197 z=boxsize
198 v=version number
199 */
200 hires();
201 ink(6); // color of board
202 drawgrid(x,y,z); // draw the 11x11 gridlines
203 drawtiles(x,y,z); // draw the background tiles
204 }
205 // Playerturn -
206 void playerturn(unsigned char x, unsigned char y, unsigned char z) // filter keyboard input
207 {
208 /*
209 x=x pos of board (top left)
210 y=y pos of board (top left)
211 z=boxsize
212 */
213 unsigned char key=0; // code of key pressed
214 unsigned char ns=5; // default north/south position of central square
215 unsigned char ew=5; // default east/west position of central square
216 unsigned char cx=1+x+(z*ns); // cursor x position
217 unsigned char cy=1+y+(z*ew); // cursor y position
218 unsigned char cz=z-2; // size of cursor
219 drawcursor(cx,cy,cz,1); // print dotted cursor at central square
220 while (key != 81) // repeat until 'Q' pressed
221 {
222 key=getchar(); // get code of pressed key
223 if ((key == 8 )&&(ew>0)) // move cursor left
224 {
225 drawcursor(cx,cy,cz,0); // print blank cursor (effect=remove dots)
226 cx-=z;
227 drawcursor(cx,cy,cz,1); // print dotted cursor
228 ew--;
229 }
230 if ((key == 9)&&(ew<10)) // move cursor right
231 {
232 drawcursor(cx,cy,cz,0); // print blank cursor (effect=remove dots)
233 cx+=z;
234 drawcursor(cx,cy,cz,1); // print dotted cursor
235 ew++;
236 }
237 if ((key == 10)&&(ns<10)) // move cursor down
238 {
239 drawcursor(cx,cy,cz,0); // print blank cursor (effect=remove dots)
240 cy+=z;
241 drawcursor(cx,cy,cz,1); // print dotted cursor
242 ns++;
243 }
244 if ((key == 11)&&(ns>0)) // move cursor up
245 {
246 drawcursor(cx,cy,cz,0); // print blank cursor (effect=remove dots)
247 cy-=z;
248 drawcursor(cx,cy,cz,1); // print dotted cursor
249 ns--;
250 }
251 }
252 }

  ViewVC Help
Powered by ViewVC 1.1.26