Rev 8072 | Rev 8081 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8071 | maxcodehac | 1 | #include |
2 | #include |
||
3 | #include |
||
8080 | maxcodehac | 4 | #include |
8071 | maxcodehac | 5 | |
8080 | maxcodehac | 6 | #include "kolibri_libimg.h" |
7 | |||
8071 | maxcodehac | 8 | /* |
9 | EVENTS LOOK LIKE IN C-- |
||
10 | */ |
||
11 | |||
12 | #define evReDraw 1 |
||
13 | #define evKey 2 |
||
14 | #define evButton 3 |
||
15 | |||
16 | |||
17 | |||
18 | #define _size 4 |
||
19 | |||
20 | #define bs 44 |
||
21 | |||
22 | |||
23 | int field[_size][_size] = { |
||
24 | {0, 0, 0, 1}, |
||
25 | {0, 0, 1, 0}, |
||
26 | {0, 0, 0, 1}, |
||
8072 | maxcodehac | 27 | {1, 0, 0, 0} |
8071 | maxcodehac | 28 | }; |
29 | |||
30 | char* title = "Fridge v0.1"; |
||
31 | |||
8080 | maxcodehac | 32 | // PICTURES LOADING CODE |
33 | |||
34 | char temp_path[4096]; |
||
35 | char* HOR; |
||
36 | char* VER; |
||
37 | |||
38 | |||
39 | char* load_file_inmem(char* fname, int32_t* read_sz) |
||
40 | { |
||
41 | FILE *f = fopen(fname, "rb"); |
||
42 | if (!f) { |
||
43 | exit(1); |
||
44 | } |
||
45 | if (fseek(f, 0, SEEK_END)) { |
||
46 | exit(1); |
||
47 | } |
||
48 | int filesize = ftell(f); |
||
49 | rewind(f); |
||
50 | char* fdata = malloc(filesize); |
||
51 | if(!fdata) { |
||
52 | exit(1); |
||
53 | } |
||
54 | *read_sz = fread(fdata, 1, filesize, f); |
||
55 | if (ferror(f)) { |
||
56 | exit(1); |
||
57 | } |
||
58 | fclose(f); |
||
59 | |||
60 | return fdata; |
||
61 | } |
||
62 | |||
63 | |||
64 | |||
65 | |||
66 | void load_pict() { |
||
67 | const int icon_rgb_size = bs*bs; |
||
68 | char *image_data, |
||
69 | *filedata; |
||
70 | |||
71 | strcpy(temp_path, "h.png"); |
||
72 | |||
73 | int32_t read_bytes; |
||
74 | filedata = load_file_inmem(temp_path, &read_bytes); |
||
75 | HOR = malloc(icon_rgb_size * 3); |
||
76 | |||
77 | image_data = img_decode(filedata, read_bytes, 0); |
||
78 | |||
79 | img_to_rgb2(image_data, HOR); |
||
80 | |||
81 | |||
82 | strcpy(temp_path, "v.png"); |
||
83 | |||
84 | filedata = load_file_inmem(temp_path, &read_bytes); |
||
85 | VER = malloc(icon_rgb_size * 3); |
||
86 | |||
87 | image_data = img_decode(filedata, read_bytes, 0); |
||
88 | |||
89 | img_to_rgb2(image_data, VER); |
||
90 | |||
91 | img_destroy(image_data); |
||
92 | free(filedata); |
||
93 | } |
||
94 | |||
95 | |||
96 | |||
97 | |||
98 | // END OF PIC LOAD CODE |
||
99 | |||
100 | |||
101 | void redraw_buttons() { |
||
102 | for (int j = 5, yy = 0; yy<_size; j+=bs, yy++) |
||
103 | for (int i = 15, xx = 0; xx<_size; i+=bs, xx++) |
||
104 | { |
||
105 | define_button(65536 * i + (bs-1), 65536 * j + (bs-1), ((xx+1)*10)+yy+1, 0xbbbbbb); |
||
106 | |||
107 | if (field[yy][xx]) draw_bitmap(VER, i, j, bs, bs); |
||
108 | else draw_bitmap(HOR, i, j, bs, bs); |
||
109 | } |
||
110 | } |
||
111 | |||
8071 | maxcodehac | 112 | void draw_window(){ |
113 | BeginDraw(); |
||
8080 | maxcodehac | 114 | DrawWindow(215,100,220, 220,title,0xbbbbbb,0x34); |
115 | redraw_buttons(); |
||
8071 | maxcodehac | 116 | EndDraw(); |
117 | } |
||
118 | |||
119 | void Button() { |
||
120 | int id = get_os_button(); |
||
121 | if (id == 1) exit(0); else |
||
122 | { |
||
123 | int x = (id/10)-1; |
||
124 | int y = (id%10)-1; |
||
125 | |||
126 | for (int i = 0; i<_size; i++) |
||
127 | if (field[i][x]) field[i][x] = 0; else field[i][x] = 1; |
||
128 | |||
129 | for (int i = 0; i<_size; i++) |
||
130 | if (field[y][i]) field[y][i] = 0; else field[y][i] = 1; |
||
131 | |||
132 | if (field[y][x]) field[y][x] = 0; else field[y][x] = 1; |
||
133 | |||
8080 | maxcodehac | 134 | redraw_buttons(); |
8071 | maxcodehac | 135 | } |
136 | } |
||
137 | |||
8080 | maxcodehac | 138 | int main(int argc, char **argv) |
8071 | maxcodehac | 139 | { |
8080 | maxcodehac | 140 | if (kolibri_libimg_init() == -1) |
141 | { |
||
142 | printf("Can not load libimg.obj!\n"); |
||
143 | exit(1); |
||
144 | } |
||
145 | |||
146 | load_pict(); |
||
147 | |||
8071 | maxcodehac | 148 | draw_window(); |
149 | while(1) |
||
150 | { |
||
151 | switch(get_os_event()) |
||
152 | { |
||
153 | case evButton: |
||
154 | Button(); |
||
155 | break; |
||
156 | |||
157 | case evKey: |
||
158 | get_key(); |
||
159 | break; |
||
160 | |||
161 | case evReDraw: |
||
162 | draw_window(); |
||
163 | break; |
||
164 | } |
||
165 | } |
||
166 | } |