source: libcaca/trunk/test/spritedit.c @ 813

Last change on this file since 813 was 813, checked in by Sam Hocevar, 15 years ago
  • Renamed main canvas-related functions so that they have "canvas" in their name, eg. cucul_create() -> cucul_create_canvas() etc.
  • Moved buffer-related functions to cucul/buffer.c.
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1/*
2 *  spritedit     sprite editor for libcaca
3 *  Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: spritedit.c 813 2006-04-18 15:54:33Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
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.
12 */
13
14#include "config.h"
15
16#include <stdio.h>
17
18#include "cucul.h"
19#include "caca.h"
20
21#define DELAY 100
22
23int main(int argc, char **argv)
24{
25    cucul_canvas_t *cv;
26    caca_display_t *dp;
27
28    int quit = 0;
29    cucul_sprite_t *sprite;
30    int frame = 0;
31    unsigned char play = 0;
32    unsigned int delay = 0;
33
34    if(argc < 2)
35    {
36        fprintf(stderr, "%s: missing argument (filename).\n", argv[0]);
37        return 1;
38    }
39
40    cv = cucul_create_canvas(0, 0);
41    if(!cv)
42        return 1;
43    dp = caca_attach(cv);
44    if(!dp)
45        return 1;
46
47    sprite = cucul_load_sprite(argv[1]);
48
49    if(!sprite)
50    {
51        caca_detach(dp);
52        cucul_free_canvas(cv);
53        fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]);
54        return 1;
55    }
56
57    /* Go ! */
58    while(!quit)
59    {
60        caca_event_t ev;
61        int xa, ya, xb, yb;
62        char buf[BUFSIZ];
63
64        while(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
65        {
66            switch(ev.data.key.ch)
67            {
68            case 0:
69                break;
70            case 'q':
71                quit = 1;
72                break;
73            case '-':
74                if(frame > 0)
75                    frame--;
76                break;
77            case '+':
78                if(frame < cucul_get_sprite_frames(sprite) - 1)
79                    frame++;
80                break;
81            case 'p':
82                play=!play;
83
84            }
85        }
86
87        if(play) {
88            if(!delay) {
89                if(frame < cucul_get_sprite_frames(sprite) - 1) {
90                    frame++;
91                }
92                else {
93                    frame = 0;
94                }
95            }
96            delay++;
97            if(delay>=DELAY) {
98                delay = 0;
99            }
100        }
101
102
103        cucul_clear(cv);
104
105        cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
106        cucul_draw_thin_box(cv, 0, 0, cucul_get_canvas_width(cv) - 1,
107                            cucul_get_canvas_height(cv) - 1);
108
109        cucul_putstr(cv, 3, 0, "[ Sprite editor for libcaca ]");
110
111        sprintf(buf, "sprite `%s'", argv[1]);
112        cucul_putstr(cv, 3, 2, buf);
113        sprintf(buf, "frame %i/%i", frame, cucul_get_sprite_frames(sprite) - 1);
114        cucul_putstr(cv, 3, 3, buf);
115
116        /* Crosshair */
117        cucul_draw_thin_line(cv, 57, 2, 57, 18);
118        cucul_draw_thin_line(cv, 37, 10, 77, 10);
119        cucul_putchar(cv, 57, 10, '+');
120
121        /* Boxed sprite */
122        xa = -1 - cucul_get_sprite_dx(sprite, frame);
123        ya = -1 - cucul_get_sprite_dy(sprite, frame);
124        xb = xa + 1 + cucul_get_sprite_width(sprite, frame);
125        yb = ya + 1 + cucul_get_sprite_height(sprite, frame);
126        cucul_set_color(cv, CUCUL_COLOR_BLACK, CUCUL_COLOR_BLACK);
127        cucul_fill_box(cv, 57 + xa, 10 + ya, 57 + xb, 10 + yb, " ");
128        cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
129        cucul_draw_thin_box(cv, 57 + xa, 10 + ya, 57 + xb, 10 + yb);
130        cucul_draw_sprite(cv, 57, 10, sprite, frame);
131
132        /* Free sprite */
133        cucul_draw_sprite(cv, 20, 10, sprite, frame);
134
135        caca_display(dp);
136    }
137
138    /* Clean up */
139    caca_detach(dp);
140    cucul_free_canvas(cv);
141
142    return 0;
143}
144
Note: See TracBrowser for help on using the repository browser.