jazykc  2013.3
upoljazykc
 All Classes Files Functions Variables Typedefs Macros Pages
canvas.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "canvas.h"
10 
11 /*
12  [0][0], ...[0,width-1], [0,width]='\0'
13  [1][0], ...[1,width-1], [1,width]='\0'
14 
15 Poznamka: v canvas.h by melo stacit typedef struct canvas;
16 */
17 typedef
18 struct canvas_s {
19  int width;
20  int height;
21  char *canvas;
22 } canvas;
23 
24 
31 canvas *canvas_create(int width, int height) {
32  canvas *pcanvas;
33  pcanvas = malloc(sizeof *pcanvas);//note: sizeof *pcanvas misto sizeof(canvas)
34  pcanvas->width = width;
35  pcanvas->height = height;
36  pcanvas->canvas = (char*)malloc(width * height + height/*kvuli koncovym '\0'*/);
37  canvas_clear(pcanvas);
38  return pcanvas;
39 }
40 
41 /* nastavi dany bod na zadanou hodnotu */
42 void canvas_set_point(canvas *c, int x, int y, char character);
43 
44 /* vrati znak daneho bodu */
45 int canvas_get_point(canvas *c, int x, int y);
46 
47 /* nakresli obdelnik */
48 void canvas_draw_rect(canvas *c, int x, int y, int width, int height, char ch);
49 
50 /* vycisti platno */
51 void canvas_clear(canvas *c) {
52  int i;
53  char *end;
54 //vyplnime mezerami:
55  memset(c->canvas, '.', c->height * (c->width+1));//na sirce mame o znak vice
56  for(i=0, end=c->canvas; i < c->height; i++) {
57  end[c->width]='\0'; end += c->width + 1;
58  }
59 }
60 
65 void canvas_print(canvas *c) {
66  int i;
67  char *line;
68  for(i=0, line=c->canvas; i< c->height; i++) {
69  printf("%s\n", line);
70  line += c->width + 1;
71  }
72 }
73 
74 /* vykresli obsah platna do souboru */
75 void canvas_output(canvas *c, FILE *f) {
76 }
77 
canvas * canvas_create(int width, int height)
Definition: canvas.c:31
int canvas_get_point(canvas *c, int x, int y)
void canvas_draw_rect(canvas *c, int x, int y, int width, int height, char ch)
void canvas_print(canvas *c)
Definition: canvas.c:65
char * canvas
Definition: canvas.c:21
struct canvas_s canvas
Definition: canvas.h:15
void canvas_set_point(canvas *c, int x, int y, char character)
void canvas_output(canvas *c, FILE *f)
Definition: canvas.c:75
void canvas_clear(canvas *c)
Definition: canvas.c:51