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

Last change on this file since 811 was 811, checked in by Sam Hocevar, 14 years ago
  • Renamed caca_t into caca_display_t.
  • Renamed canvas handle variables from "c" to "cv". Eh ouais mon gros.
  • Property svn:keywords set to Id
File size: 3.3 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 811 2006-04-18 15:11:25Z 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(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(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_width(cv) - 1, cucul_get_height(cv) - 1);
107
108        cucul_putstr(cv, 3, 0, "[ Sprite editor for libcaca ]");
109
110        sprintf(buf, "sprite `%s'", argv[1]);
111        cucul_putstr(cv, 3, 2, buf);
112        sprintf(buf, "frame %i/%i", frame, cucul_get_sprite_frames(sprite) - 1);
113        cucul_putstr(cv, 3, 3, buf);
114
115        /* Crosshair */
116        cucul_draw_thin_line(cv, 57, 2, 57, 18);
117        cucul_draw_thin_line(cv, 37, 10, 77, 10);
118        cucul_putchar(cv, 57, 10, '+');
119
120        /* Boxed sprite */
121        xa = -1 - cucul_get_sprite_dx(sprite, frame);
122        ya = -1 - cucul_get_sprite_dy(sprite, frame);
123        xb = xa + 1 + cucul_get_sprite_width(sprite, frame);
124        yb = ya + 1 + cucul_get_sprite_height(sprite, frame);
125        cucul_set_color(cv, CUCUL_COLOR_BLACK, CUCUL_COLOR_BLACK);
126        cucul_fill_box(cv, 57 + xa, 10 + ya, 57 + xb, 10 + yb, " ");
127        cucul_set_color(cv, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
128        cucul_draw_thin_box(cv, 57 + xa, 10 + ya, 57 + xb, 10 + yb);
129        cucul_draw_sprite(cv, 57, 10, sprite, frame);
130
131        /* Free sprite */
132        cucul_draw_sprite(cv, 20, 10, sprite, frame);
133
134        caca_display(dp);
135    }
136
137    /* Clean up */
138    caca_detach(dp);
139    cucul_free(cv);
140
141    return 0;
142}
143
Note: See TracBrowser for help on using the repository browser.