[85] | 1 | /* |
---|
[268] | 2 | * demo demo for libcaca |
---|
| 3 | * Copyright (c) 2003 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
[85] | 5 | * |
---|
[268] | 6 | * $Id: demo.c 689 2006-03-24 16:51:59Z sam $ |
---|
[85] | 7 | * |
---|
[268] | 8 | * This program is free software; you can redistribute it and/or |
---|
[522] | 9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
| 10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
| 11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
[85] | 12 | */ |
---|
| 13 | |
---|
[105] | 14 | #include "config.h" |
---|
| 15 | |
---|
[99] | 16 | #include <math.h> |
---|
| 17 | #include <string.h> |
---|
[159] | 18 | #include <stdio.h> |
---|
[99] | 19 | |
---|
[185] | 20 | #include "caca.h" |
---|
[85] | 21 | |
---|
[99] | 22 | static void display_menu(void); |
---|
| 23 | |
---|
[123] | 24 | static void demo_all(void); |
---|
| 25 | |
---|
[159] | 26 | static void demo_color(void); |
---|
[99] | 27 | static void demo_dots(void); |
---|
| 28 | static void demo_lines(void); |
---|
[151] | 29 | static void demo_boxes(void); |
---|
[133] | 30 | static void demo_ellipses(void); |
---|
[107] | 31 | static void demo_triangles(void); |
---|
[117] | 32 | static void demo_sprites(void); |
---|
[246] | 33 | static void demo_render(void); |
---|
[99] | 34 | |
---|
[181] | 35 | int bounds = 0; |
---|
[151] | 36 | int outline = 0; |
---|
[195] | 37 | int dithering = 0; |
---|
[524] | 38 | struct cucul_sprite *sprite = NULL; |
---|
[99] | 39 | |
---|
[524] | 40 | cucul_t *qq; |
---|
| 41 | caca_t *kk; |
---|
| 42 | |
---|
[85] | 43 | int main(int argc, char **argv) |
---|
| 44 | { |
---|
[99] | 45 | void (*demo)(void) = NULL; |
---|
[85] | 46 | int quit = 0; |
---|
| 47 | |
---|
[677] | 48 | qq = cucul_create(0, 0); |
---|
[524] | 49 | if(!qq) |
---|
[85] | 50 | return 1; |
---|
[524] | 51 | kk = caca_attach(qq); |
---|
| 52 | if(!kk) |
---|
| 53 | return 1; |
---|
[85] | 54 | |
---|
[524] | 55 | caca_set_delay(kk, 40000); |
---|
[177] | 56 | |
---|
[117] | 57 | /* Initialize data */ |
---|
[524] | 58 | sprite = cucul_load_sprite(qq, DATADIR "/caca.txt"); |
---|
[193] | 59 | if(!sprite) |
---|
[524] | 60 | sprite = cucul_load_sprite(qq, "caca.txt"); |
---|
[202] | 61 | if(!sprite) |
---|
[524] | 62 | sprite = cucul_load_sprite(qq, "examples/caca.txt"); |
---|
[117] | 63 | |
---|
[687] | 64 | /* Disable cursor */ |
---|
[689] | 65 | caca_set_mouse(kk, 0); |
---|
[687] | 66 | |
---|
[117] | 67 | /* Main menu */ |
---|
[99] | 68 | display_menu(); |
---|
[527] | 69 | caca_display(kk); |
---|
[99] | 70 | |
---|
[85] | 71 | /* Go ! */ |
---|
| 72 | while(!quit) |
---|
| 73 | { |
---|
[681] | 74 | struct caca_event ev; |
---|
[287] | 75 | int menu = 0, mouse = 0, xmouse = 0, ymouse = 0; |
---|
[181] | 76 | |
---|
[681] | 77 | while(caca_get_event(kk, CACA_EVENT_ANY, &ev)) |
---|
[85] | 78 | { |
---|
[681] | 79 | if(demo && (ev.type & CACA_EVENT_KEY_PRESS)) |
---|
[99] | 80 | { |
---|
[287] | 81 | menu = 1; |
---|
[99] | 82 | demo = NULL; |
---|
| 83 | } |
---|
[681] | 84 | else if(ev.type & CACA_EVENT_KEY_PRESS) |
---|
[287] | 85 | { |
---|
[681] | 86 | switch(ev.data.key.c) |
---|
[287] | 87 | { |
---|
| 88 | case 'q': |
---|
| 89 | case 'Q': |
---|
| 90 | demo = NULL; |
---|
| 91 | quit = 1; |
---|
| 92 | break; |
---|
| 93 | case 'o': |
---|
| 94 | case 'O': |
---|
| 95 | outline = (outline + 1) % 3; |
---|
| 96 | display_menu(); |
---|
| 97 | break; |
---|
| 98 | case 'b': |
---|
| 99 | case 'B': |
---|
| 100 | bounds = (bounds + 1) % 2; |
---|
| 101 | display_menu(); |
---|
| 102 | break; |
---|
| 103 | case 'd': |
---|
| 104 | case 'D': |
---|
| 105 | dithering = (dithering + 1) % 5; |
---|
[524] | 106 | cucul_set_feature(qq, dithering); |
---|
[287] | 107 | display_menu(); |
---|
| 108 | break; |
---|
| 109 | case 'c': |
---|
| 110 | demo = demo_color; |
---|
| 111 | break; |
---|
| 112 | case 'f': |
---|
| 113 | case 'F': |
---|
| 114 | demo = demo_all; |
---|
| 115 | break; |
---|
| 116 | case '1': |
---|
| 117 | demo = demo_dots; |
---|
| 118 | break; |
---|
| 119 | case '2': |
---|
| 120 | demo = demo_lines; |
---|
| 121 | break; |
---|
| 122 | case '3': |
---|
| 123 | demo = demo_boxes; |
---|
| 124 | break; |
---|
| 125 | case '4': |
---|
| 126 | demo = demo_triangles; |
---|
| 127 | break; |
---|
| 128 | case '5': |
---|
| 129 | demo = demo_ellipses; |
---|
| 130 | break; |
---|
| 131 | case 's': |
---|
| 132 | case 'S': |
---|
| 133 | if(sprite) |
---|
| 134 | demo = demo_sprites; |
---|
| 135 | break; |
---|
| 136 | case 'r': |
---|
| 137 | case 'R': |
---|
| 138 | demo = demo_render; |
---|
| 139 | break; |
---|
| 140 | } |
---|
[181] | 141 | |
---|
[287] | 142 | if(demo) |
---|
[524] | 143 | cucul_clear(qq); |
---|
[287] | 144 | } |
---|
[681] | 145 | else if(ev.type & CACA_EVENT_MOUSE_MOTION) |
---|
[287] | 146 | { |
---|
| 147 | mouse = 1; |
---|
[681] | 148 | xmouse = ev.data.mouse.x; |
---|
| 149 | ymouse = ev.data.mouse.y; |
---|
[287] | 150 | } |
---|
| 151 | } |
---|
[181] | 152 | |
---|
[287] | 153 | if(menu || (mouse && !demo)) |
---|
[204] | 154 | { |
---|
| 155 | display_menu(); |
---|
[287] | 156 | if(mouse && !demo) |
---|
| 157 | { |
---|
[524] | 158 | cucul_set_color(qq, CUCUL_COLOR_RED, CUCUL_COLOR_BLACK); |
---|
| 159 | cucul_putstr(qq, xmouse, ymouse, "|\\"); |
---|
[287] | 160 | } |
---|
[527] | 161 | caca_display(kk); |
---|
[287] | 162 | mouse = menu = 0; |
---|
[204] | 163 | } |
---|
[93] | 164 | |
---|
[99] | 165 | if(demo) |
---|
[181] | 166 | { |
---|
[99] | 167 | demo(); |
---|
[181] | 168 | |
---|
[524] | 169 | cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
| 170 | cucul_draw_thin_box(qq, 1, 1, cucul_get_width(qq) - 2, cucul_get_height(qq) - 2); |
---|
| 171 | cucul_printf(qq, 4, 1, "[%i.%i fps]----", |
---|
| 172 | 1000000 / caca_get_rendertime(kk), |
---|
| 173 | (10000000 / caca_get_rendertime(kk)) % 10); |
---|
[527] | 174 | caca_display(kk); |
---|
[181] | 175 | } |
---|
[85] | 176 | } |
---|
| 177 | |
---|
| 178 | /* Clean up */ |
---|
[653] | 179 | cucul_free_sprite(sprite); |
---|
[524] | 180 | caca_detach(kk); |
---|
[677] | 181 | cucul_free(qq); |
---|
[85] | 182 | |
---|
| 183 | return 0; |
---|
| 184 | } |
---|
| 185 | |
---|
[99] | 186 | static void display_menu(void) |
---|
| 187 | { |
---|
[524] | 188 | int xo = cucul_get_width(qq) - 2; |
---|
| 189 | int yo = cucul_get_height(qq) - 2; |
---|
[99] | 190 | |
---|
[524] | 191 | cucul_clear(qq); |
---|
| 192 | cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
| 193 | cucul_draw_thin_box(qq, 1, 1, xo, yo); |
---|
[99] | 194 | |
---|
[524] | 195 | cucul_putstr(qq, (xo - strlen("libcaca demo")) / 2, 3, "libcaca demo"); |
---|
| 196 | cucul_putstr(qq, (xo - strlen("==============")) / 2, 4, "=============="); |
---|
[99] | 197 | |
---|
[524] | 198 | cucul_putstr(qq, 4, 6, "demos:"); |
---|
| 199 | cucul_putstr(qq, 4, 7, "'f': full"); |
---|
| 200 | cucul_putstr(qq, 4, 8, "'1': dots"); |
---|
| 201 | cucul_putstr(qq, 4, 9, "'2': lines"); |
---|
| 202 | cucul_putstr(qq, 4, 10, "'3': boxes"); |
---|
| 203 | cucul_putstr(qq, 4, 11, "'4': triangles"); |
---|
| 204 | cucul_putstr(qq, 4, 12, "'5': ellipses"); |
---|
| 205 | cucul_putstr(qq, 4, 13, "'c': colour"); |
---|
| 206 | cucul_putstr(qq, 4, 14, "'r': render"); |
---|
[226] | 207 | if(sprite) |
---|
[524] | 208 | cucul_putstr(qq, 4, 15, "'s': sprites"); |
---|
[99] | 209 | |
---|
[524] | 210 | cucul_putstr(qq, 4, 16, "settings:"); |
---|
| 211 | cucul_printf(qq, 4, 17, "'o': outline: %s", |
---|
| 212 | outline == 0 ? "none" : outline == 1 ? "solid" : "thin"); |
---|
| 213 | cucul_printf(qq, 4, 18, "'b': drawing boundaries: %s", |
---|
| 214 | bounds == 0 ? "screen" : "infinite"); |
---|
| 215 | cucul_printf(qq, 4, 19, "'d': dithering (%s)", |
---|
| 216 | cucul_get_feature_name(dithering)); |
---|
[99] | 217 | |
---|
[524] | 218 | cucul_putstr(qq, 4, yo - 2, "'q': quit"); |
---|
[618] | 219 | |
---|
| 220 | caca_display(kk); |
---|
[99] | 221 | } |
---|
| 222 | |
---|
[123] | 223 | static void demo_all(void) |
---|
| 224 | { |
---|
| 225 | static int i = 0; |
---|
| 226 | |
---|
[159] | 227 | int j, xo, yo, xa, ya, xb, yb, xc, yc; |
---|
[123] | 228 | |
---|
| 229 | i++; |
---|
| 230 | |
---|
[524] | 231 | cucul_clear(qq); |
---|
[123] | 232 | |
---|
| 233 | /* Draw the sun */ |
---|
[524] | 234 | cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK); |
---|
| 235 | xo = cucul_get_width(qq) / 4; |
---|
| 236 | yo = cucul_get_height(qq) / 4 + 5 * sin(0.03*i); |
---|
[123] | 237 | |
---|
| 238 | for(j = 0; j < 16; j++) |
---|
| 239 | { |
---|
[159] | 240 | xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8); |
---|
| 241 | ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8); |
---|
[524] | 242 | cucul_draw_thin_line(qq, xo, yo, xa, ya); |
---|
[123] | 243 | } |
---|
| 244 | |
---|
[151] | 245 | j = 15 + sin(0.03*i) * 8; |
---|
[524] | 246 | cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK); |
---|
[678] | 247 | cucul_fill_ellipse(qq, xo, yo, j, j / 2, "#"); |
---|
[524] | 248 | cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK); |
---|
[678] | 249 | cucul_draw_ellipse(qq, xo, yo, j, j / 2, "#"); |
---|
[123] | 250 | |
---|
| 251 | /* Draw the pyramid */ |
---|
[524] | 252 | xo = cucul_get_width(qq) * 5 / 8; |
---|
[123] | 253 | yo = 2; |
---|
| 254 | |
---|
[524] | 255 | xa = cucul_get_width(qq) / 8 + sin(0.03*i) * 5; |
---|
| 256 | ya = cucul_get_height(qq) / 2 + cos(0.03*i) * 5; |
---|
[123] | 257 | |
---|
[524] | 258 | xb = cucul_get_width(qq) - 10 - cos(0.02*i) * 10; |
---|
| 259 | yb = cucul_get_height(qq) * 3 / 4 - 5 + sin(0.02*i) * 5; |
---|
[123] | 260 | |
---|
[524] | 261 | xc = cucul_get_width(qq) / 4 - sin(0.02*i) * 5; |
---|
| 262 | yc = cucul_get_height(qq) * 3 / 4 + cos(0.02*i) * 5; |
---|
[123] | 263 | |
---|
[524] | 264 | cucul_set_color(qq, CUCUL_COLOR_GREEN, CUCUL_COLOR_BLACK); |
---|
[678] | 265 | cucul_fill_triangle(qq, xo, yo, xb, yb, xa, ya, "%"); |
---|
[524] | 266 | cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK); |
---|
| 267 | cucul_draw_thin_triangle(qq, xo, yo, xb, yb, xa, ya); |
---|
[123] | 268 | |
---|
[524] | 269 | cucul_set_color(qq, CUCUL_COLOR_RED, CUCUL_COLOR_BLACK); |
---|
[678] | 270 | cucul_fill_triangle(qq, xa, ya, xb, yb, xc, yc, "#"); |
---|
[524] | 271 | cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK); |
---|
| 272 | cucul_draw_thin_triangle(qq, xa, ya, xb, yb, xc, yc); |
---|
[123] | 273 | |
---|
[524] | 274 | cucul_set_color(qq, CUCUL_COLOR_BLUE, CUCUL_COLOR_BLACK); |
---|
[678] | 275 | cucul_fill_triangle(qq, xo, yo, xb, yb, xc, yc, "%"); |
---|
[524] | 276 | cucul_set_color(qq, CUCUL_COLOR_YELLOW, CUCUL_COLOR_BLACK); |
---|
| 277 | cucul_draw_thin_triangle(qq, xo, yo, xb, yb, xc, yc); |
---|
[123] | 278 | |
---|
| 279 | /* Draw a background triangle */ |
---|
[159] | 280 | xa = 2; |
---|
| 281 | ya = 2; |
---|
[123] | 282 | |
---|
[524] | 283 | xb = cucul_get_width(qq) - 3; |
---|
| 284 | yb = cucul_get_height(qq) / 2; |
---|
[123] | 285 | |
---|
[524] | 286 | xc = cucul_get_width(qq) / 3; |
---|
| 287 | yc = cucul_get_height(qq) - 3; |
---|
[123] | 288 | |
---|
[524] | 289 | cucul_set_color(qq, CUCUL_COLOR_CYAN, CUCUL_COLOR_BLACK); |
---|
| 290 | cucul_draw_thin_triangle(qq, xa, ya, xb, yb, xc, yc); |
---|
[123] | 291 | |
---|
[524] | 292 | xo = cucul_get_width(qq) / 2 + cos(0.027*i) * cucul_get_width(qq) / 3; |
---|
| 293 | yo = cucul_get_height(qq) / 2 - sin(0.027*i) * cucul_get_height(qq) / 2; |
---|
[123] | 294 | |
---|
[524] | 295 | cucul_draw_thin_line(qq, xa, ya, xo, yo); |
---|
| 296 | cucul_draw_thin_line(qq, xb, yb, xo, yo); |
---|
| 297 | cucul_draw_thin_line(qq, xc, yc, xo, yo); |
---|
[123] | 298 | |
---|
[151] | 299 | /* Draw a sprite on the pyramid */ |
---|
[524] | 300 | cucul_draw_sprite(qq, xo, yo, sprite, 0); |
---|
[123] | 301 | |
---|
| 302 | /* Draw a trail behind the foreground sprite */ |
---|
| 303 | for(j = i - 60; j < i; j++) |
---|
| 304 | { |
---|
[524] | 305 | int delta = cucul_rand(-5, 5); |
---|
| 306 | cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15)); |
---|
| 307 | cucul_putchar(qq, cucul_get_width(qq) / 2 |
---|
| 308 | + cos(0.02*j) * (delta + cucul_get_width(qq) / 4), |
---|
| 309 | cucul_get_height(qq) / 2 |
---|
| 310 | + sin(0.02*j) * (delta + cucul_get_height(qq) / 3), |
---|
[147] | 311 | '#'); |
---|
[123] | 312 | } |
---|
| 313 | |
---|
| 314 | /* Draw foreground sprite */ |
---|
[524] | 315 | cucul_draw_sprite(qq, cucul_get_width(qq) / 2 + cos(0.02*i) * cucul_get_width(qq) / 4, |
---|
| 316 | cucul_get_height(qq) / 2 + sin(0.02*i) * cucul_get_height(qq) / 3, |
---|
[153] | 317 | sprite, 0); |
---|
[123] | 318 | } |
---|
| 319 | |
---|
[99] | 320 | static void demo_dots(void) |
---|
| 321 | { |
---|
[524] | 322 | int xmax = cucul_get_width(qq) - 1; |
---|
| 323 | int ymax = cucul_get_height(qq) - 1; |
---|
[99] | 324 | int i; |
---|
[226] | 325 | static char chars[10] = |
---|
| 326 | { |
---|
| 327 | '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W' |
---|
| 328 | }; |
---|
[99] | 329 | |
---|
[103] | 330 | for(i = 1000; i--;) |
---|
[99] | 331 | { |
---|
| 332 | /* Putpixel */ |
---|
[524] | 333 | cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15)); |
---|
| 334 | cucul_putchar(qq, cucul_rand(0, xmax), cucul_rand(0, ymax), |
---|
| 335 | chars[cucul_rand(0, 9)]); |
---|
[99] | 336 | } |
---|
| 337 | } |
---|
| 338 | |
---|
[159] | 339 | static void demo_color(void) |
---|
| 340 | { |
---|
[226] | 341 | int i, j; |
---|
[159] | 342 | char buf[BUFSIZ]; |
---|
| 343 | |
---|
[524] | 344 | cucul_clear(qq); |
---|
[159] | 345 | for(i = 0; i < 16; i++) |
---|
| 346 | { |
---|
[524] | 347 | sprintf(buf, "'%c': %i (%s)", 'a' + i, i, cucul_get_color_name(i)); |
---|
| 348 | cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
| 349 | cucul_putstr(qq, 4, i + (i >= 8 ? 4 : 3), buf); |
---|
[226] | 350 | for(j = 0; j < 16; j++) |
---|
| 351 | { |
---|
[524] | 352 | cucul_set_color(qq, i, j); |
---|
| 353 | cucul_putstr(qq, (j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# "); |
---|
[226] | 354 | } |
---|
[159] | 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
[99] | 358 | static void demo_lines(void) |
---|
| 359 | { |
---|
[524] | 360 | int w = cucul_get_width(qq); |
---|
| 361 | int h = cucul_get_height(qq); |
---|
[159] | 362 | int xa, ya, xb, yb; |
---|
[103] | 363 | |
---|
[181] | 364 | if(bounds) |
---|
[99] | 365 | { |
---|
[524] | 366 | xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h); |
---|
| 367 | xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h); |
---|
[99] | 368 | } |
---|
| 369 | else |
---|
| 370 | { |
---|
[524] | 371 | xa = cucul_rand(0, w - 1); ya = cucul_rand(0, h - 1); |
---|
| 372 | xb = cucul_rand(0, w - 1); yb = cucul_rand(0, h - 1); |
---|
[99] | 373 | } |
---|
| 374 | |
---|
[524] | 375 | cucul_set_color(qq, cucul_rand(0, 15), CUCUL_COLOR_BLACK); |
---|
[181] | 376 | if(outline > 1) |
---|
[524] | 377 | cucul_draw_thin_line(qq, xa, ya, xb, yb); |
---|
[103] | 378 | else |
---|
[678] | 379 | cucul_draw_line(qq, xa, ya, xb, yb, "#"); |
---|
[103] | 380 | } |
---|
| 381 | |
---|
[151] | 382 | static void demo_boxes(void) |
---|
[99] | 383 | { |
---|
[524] | 384 | int w = cucul_get_width(qq); |
---|
| 385 | int h = cucul_get_height(qq); |
---|
[159] | 386 | int xa, ya, xb, yb; |
---|
[103] | 387 | |
---|
[181] | 388 | if(bounds) |
---|
[99] | 389 | { |
---|
[524] | 390 | xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h); |
---|
| 391 | xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h); |
---|
[99] | 392 | } |
---|
| 393 | else |
---|
| 394 | { |
---|
[524] | 395 | xa = cucul_rand(0, w - 1); ya = cucul_rand(0, h - 1); |
---|
| 396 | xb = cucul_rand(0, w - 1); yb = cucul_rand(0, h - 1); |
---|
[151] | 397 | } |
---|
[99] | 398 | |
---|
[524] | 399 | cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15)); |
---|
[678] | 400 | cucul_fill_box(qq, xa, ya, xb, yb, "#"); |
---|
[99] | 401 | |
---|
[524] | 402 | cucul_set_color(qq, cucul_rand(0, 15), CUCUL_COLOR_BLACK); |
---|
[181] | 403 | if(outline == 2) |
---|
[524] | 404 | cucul_draw_thin_box(qq, xa, ya, xb, yb); |
---|
[181] | 405 | else if(outline == 1) |
---|
[678] | 406 | cucul_draw_box(qq, xa, ya, xb, yb, "#"); |
---|
[99] | 407 | } |
---|
| 408 | |
---|
[133] | 409 | static void demo_ellipses(void) |
---|
| 410 | { |
---|
[524] | 411 | int w = cucul_get_width(qq); |
---|
| 412 | int h = cucul_get_height(qq); |
---|
[151] | 413 | int x, y, a, b; |
---|
[133] | 414 | |
---|
[181] | 415 | if(bounds) |
---|
[133] | 416 | { |
---|
[524] | 417 | x = cucul_rand(- w, 2 * w); y = cucul_rand(- h, 2 * h); |
---|
| 418 | a = cucul_rand(0, w); b = cucul_rand(0, h); |
---|
[133] | 419 | } |
---|
| 420 | else |
---|
| 421 | { |
---|
[151] | 422 | do |
---|
[133] | 423 | { |
---|
[524] | 424 | x = cucul_rand(0, w); y = cucul_rand(0, h); |
---|
| 425 | a = cucul_rand(0, w); b = cucul_rand(0, h); |
---|
[133] | 426 | |
---|
[151] | 427 | } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h); |
---|
[133] | 428 | } |
---|
| 429 | |
---|
[524] | 430 | cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15)); |
---|
[678] | 431 | cucul_fill_ellipse(qq, x, y, a, b, "#"); |
---|
[133] | 432 | |
---|
[524] | 433 | cucul_set_color(qq, cucul_rand(0, 15), CUCUL_COLOR_BLACK); |
---|
[181] | 434 | if(outline == 2) |
---|
[524] | 435 | cucul_draw_thin_ellipse(qq, x, y, a, b); |
---|
[181] | 436 | else if(outline == 1) |
---|
[678] | 437 | cucul_draw_ellipse(qq, x, y, a, b, "#"); |
---|
[107] | 438 | } |
---|
| 439 | |
---|
[151] | 440 | static void demo_triangles(void) |
---|
[113] | 441 | { |
---|
[524] | 442 | int w = cucul_get_width(qq); |
---|
| 443 | int h = cucul_get_height(qq); |
---|
[159] | 444 | int xa, ya, xb, yb, xc, yc; |
---|
[113] | 445 | |
---|
[181] | 446 | if(bounds) |
---|
[113] | 447 | { |
---|
[524] | 448 | xa = cucul_rand(- w, 2 * w); ya = cucul_rand(- h, 2 * h); |
---|
| 449 | xb = cucul_rand(- w, 2 * w); yb = cucul_rand(- h, 2 * h); |
---|
| 450 | xc = cucul_rand(- w, 2 * w); yc = cucul_rand(- h, 2 * h); |
---|
[113] | 451 | } |
---|
| 452 | else |
---|
| 453 | { |
---|
| 454 | |
---|
[524] | 455 | xa = cucul_rand(0, w - 1); ya = cucul_rand(0, h - 1); |
---|
| 456 | xb = cucul_rand(0, w - 1); yb = cucul_rand(0, h - 1); |
---|
| 457 | xc = cucul_rand(0, w - 1); yc = cucul_rand(0, h - 1); |
---|
[123] | 458 | } |
---|
[113] | 459 | |
---|
[524] | 460 | cucul_set_color(qq, cucul_rand(0, 15), cucul_rand(0, 15)); |
---|
[678] | 461 | cucul_fill_triangle(qq, xa, ya, xb, yb, xc, yc, "#"); |
---|
[113] | 462 | |
---|
[524] | 463 | cucul_set_color(qq, cucul_rand(0, 15), CUCUL_COLOR_BLACK); |
---|
[181] | 464 | if(outline == 2) |
---|
[524] | 465 | cucul_draw_thin_triangle(qq, xa, ya, xb, yb, xc, yc); |
---|
[181] | 466 | else if(outline == 1) |
---|
[678] | 467 | cucul_draw_triangle(qq, xa, ya, xb, yb, xc, yc, "#"); |
---|
[113] | 468 | } |
---|
| 469 | |
---|
[117] | 470 | static void demo_sprites(void) |
---|
| 471 | { |
---|
[524] | 472 | cucul_draw_sprite(qq, cucul_rand(0, cucul_get_width(qq) - 1), |
---|
| 473 | cucul_rand(0, cucul_get_height(qq) - 1), sprite, 0); |
---|
[117] | 474 | } |
---|
| 475 | |
---|
[246] | 476 | #if 0 |
---|
| 477 | static void demo_render(void) |
---|
| 478 | { |
---|
[524] | 479 | struct cucul_bitmap *bitmap; |
---|
[246] | 480 | //short buffer[256*256]; |
---|
| 481 | //short *dest = buffer; |
---|
| 482 | int buffer[256*256]; |
---|
| 483 | int *dest = buffer; |
---|
| 484 | int x, y, z; |
---|
| 485 | static int i = 0; |
---|
| 486 | |
---|
| 487 | i = (i + 1) % 512; |
---|
| 488 | z = i < 256 ? i : 511 - i; |
---|
| 489 | |
---|
| 490 | for(x = 0; x < 256; x++) |
---|
| 491 | for(y = 0; y < 256; y++) |
---|
| 492 | { |
---|
| 493 | //*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3)); |
---|
| 494 | *dest++ = (x << 16) | (y << 8) | (z); |
---|
| 495 | } |
---|
| 496 | |
---|
[524] | 497 | //bitmap = cucul_create_bitmap(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000); |
---|
| 498 | bitmap = cucul_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); |
---|
| 499 | cucul_draw_bitmap(qq, 0, 0, cucul_get_width(qq) - 1, cucul_get_height(qq) - 1, |
---|
[246] | 500 | bitmap, buffer); |
---|
[653] | 501 | cucul_free_bitmap(bitmap); |
---|
[246] | 502 | } |
---|
| 503 | #endif |
---|
| 504 | |
---|
| 505 | static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val); |
---|
| 506 | |
---|
| 507 | static void demo_render(void) |
---|
| 508 | { |
---|
[524] | 509 | struct cucul_bitmap *bitmap; |
---|
[246] | 510 | int buffer[256*256]; |
---|
| 511 | int *dest; |
---|
[249] | 512 | int x, y, z, xo, yo; |
---|
[246] | 513 | static int i = 0; |
---|
| 514 | |
---|
| 515 | i++; |
---|
| 516 | |
---|
| 517 | dest = buffer; |
---|
| 518 | for(x = 0; x < 256; x++) |
---|
| 519 | for(y = 0; y < 256; y++) |
---|
| 520 | { |
---|
[249] | 521 | *dest++ = 0xff000000; |
---|
[246] | 522 | } |
---|
| 523 | |
---|
| 524 | /* red */ |
---|
| 525 | xo = 128 + 48 * sin(0.02 * i); |
---|
| 526 | yo = 128 + 48 * cos(0.03 * i); |
---|
| 527 | for(z = 0; z < 240; z++) |
---|
[249] | 528 | draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16); |
---|
[246] | 529 | |
---|
| 530 | /* green */ |
---|
| 531 | xo = 128 + 48 * sin(2 + 0.06 * i); |
---|
| 532 | yo = 128 + 48 * cos(2 + 0.05 * i); |
---|
| 533 | for(z = 0; z < 240; z++) |
---|
[249] | 534 | draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8); |
---|
[246] | 535 | |
---|
| 536 | /* blue */ |
---|
| 537 | xo = 128 + 48 * sin(1 + 0.04 * i); |
---|
| 538 | yo = 128 + 48 * cos(1 + 0.03 * i); |
---|
| 539 | for(z = 0; z < 240; z++) |
---|
[249] | 540 | draw_circle(buffer, xo, yo, z, 0x000000ff, 200); |
---|
[246] | 541 | |
---|
[666] | 542 | bitmap = cucul_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); |
---|
[524] | 543 | cucul_draw_bitmap(qq, 0, 0, cucul_get_width(qq) - 1, cucul_get_height(qq) - 1, bitmap, (char *)buffer); |
---|
[653] | 544 | cucul_free_bitmap(bitmap); |
---|
[246] | 545 | } |
---|
| 546 | |
---|
| 547 | static void draw_circle(int *buffer, int x, int y, int r, int mask, int val) |
---|
| 548 | { |
---|
| 549 | int t, dx, dy; |
---|
| 550 | |
---|
| 551 | #define POINT(X,Y) \ |
---|
[249] | 552 | buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val; |
---|
[246] | 553 | |
---|
| 554 | for(t = 0, dx = 0, dy = r; dx <= dy; dx++) |
---|
| 555 | { |
---|
| 556 | POINT(x - dx / 3, y - dy / 3); |
---|
| 557 | POINT(x + dx / 3, y - dy / 3); |
---|
| 558 | POINT(x - dx / 3, y + dy / 3); |
---|
| 559 | POINT(x + dx / 3, y + dy / 3); |
---|
| 560 | |
---|
| 561 | POINT(x - dy / 3, y - dx / 3); |
---|
| 562 | POINT(x + dy / 3, y - dx / 3); |
---|
| 563 | POINT(x - dy / 3, y + dx / 3); |
---|
| 564 | POINT(x + dy / 3, y + dx / 3); |
---|
| 565 | |
---|
| 566 | t += t > 0 ? dx - dy-- : dx; |
---|
| 567 | } |
---|
| 568 | } |
---|
| 569 | |
---|