libcaca  0.99.beta19
About: libcaca is a graphics library that outputs text instead of pixels, so that it can work on older video cards or text terminals (something like an advanced AAlib library).
  Fossies Dox: libcaca-0.99.beta19.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

cacadraw.c
Go to the documentation of this file.
1 /*
2  * event event lister for libcaca
3  * Copyright (c) 2004-2012 Sam Hocevar <sam@hocevar.net>
4  * All Rights Reserved
5  *
6  * This program is free software. It comes without any warranty, to
7  * the extent permitted by applicable law. You can redistribute it
8  * and/or modify it under the terms of the Do What the Fuck You Want
9  * to Public License, Version 2, as published by Sam Hocevar. See
10  * http://www.wtfpl.net/ for more details.
11  */
12 
13 #include "config.h"
14 
15 #if !defined(__KERNEL__)
16 # include <stdio.h>
17 # include <string.h>
18 # include <stdlib.h>
19 #endif
20 
21 #include "caca.h"
22 
23 static int refresh_screen(void);
24 
27 static int x = 0, y = 0;
28 
29 int main(int argc, char **argv)
30 {
31  int refresh = 1, file = 1;
32  unsigned int iw = 0, ih = 0;
33 
34  if(argc < 2)
35  {
36  fprintf(stderr, "%s: missing argument (filename).\n", argv[0]);
37  return 1;
38  }
39 
40  cv = caca_create_canvas(0, 0);
41  if(!cv)
42  return 1;
44  if(!dp)
45  return 1;
46 
47  for(;;)
48  {
49  caca_event_t ev;
50  unsigned int w, h;
51  int dx = 0, dy = 0;
52 
53  if(!image)
54  {
55  image = caca_create_canvas(0, 0);
56  if(caca_import_canvas_from_file(image, argv[file], "ansi") < 0)
57  {
58  fprintf(stderr, "%s: invalid file `%s'.\n", argv[0], argv[1]);
59  return 1;
60  }
61 
64  x = y = 0;
65 
66  caca_set_display_title(dp, argv[file]);
67 
68  refresh = 1;
69  }
70 
71  if(refresh)
72  {
74  refresh = 0;
75  }
76 
77  while(caca_get_event(dp, CACA_EVENT_ANY, &ev, -1))
78  {
79  switch(caca_get_event_type(&ev))
80  {
81  case CACA_EVENT_QUIT:
82  goto quit;
84  switch(caca_get_event_key_ch(&ev))
85  {
86  case CACA_KEY_LEFT: dx -= 2; break;
87  case CACA_KEY_RIGHT: dx += 2; break;
88  case CACA_KEY_UP: dy--; break;
89  case CACA_KEY_DOWN: dy++; break;
90  case CACA_KEY_PAGEUP: dy -= 12; break;
91  case CACA_KEY_PAGEDOWN: dy += 12; break;
92  case CACA_KEY_ESCAPE:
93  case 'q':
94  goto quit;
95  case 'n':
96  file = file + 1 < argc ? file + 1 : 1;
98  image = NULL;
99  goto stopevents;
100  case 'p':
101  file = file > 1 ? file - 1 : argc - 1;
103  image = NULL;
104  goto stopevents;
105  default:
106  break;
107  }
108  case CACA_EVENT_RESIZE:
109  refresh = 1;
110  goto stopevents;
111  default:
112  break;
113  }
114  }
115 stopevents:
116 
119 
120  if(dx | dy)
121  {
122  refresh = 1;
123  x += dx;
124  y += dy;
125 
126  if(x < 0) x = 0; else if(x + w > iw) x = iw > w ? iw - w : 0;
127  if(y < 0) y = 0; else if(y + h > ih) y = ih > h ? ih - h : 0;
128  }
129  }
130 quit:
131 
132  /* Clean up */
136 
137  return 0;
138 }
139 
140 static int refresh_screen(void)
141 {
144 
145  caca_blit(cv, - x, - y, image, NULL);
146 
148 
149  return 0;
150 }
151 
CACA_KEY_DOWN
Definition: caca.h:198
caca_blit
int caca_blit(caca_canvas_t *, int, int, caca_canvas_t const *, caca_canvas_t const *)
Blit a canvas onto another one.
Definition: string.c:455
caca_create_display
caca_display_t * caca_create_display(caca_canvas_t *cv)
Attach a caca graphical context to a caca canvas.
Definition: caca.c:68
y
static int y
Definition: cacadraw.c:27
cv
static caca_canvas_t * cv
Definition: cacadraw.c:25
main
int main(int argc, char **argv)
Definition: cacadraw.c:29
CACA_KEY_LEFT
Definition: caca.h:199
caca_event
Handling of user events.
Definition: caca.h:129
caca_create_canvas
caca_canvas_t * caca_create_canvas(int, int)
Initialise a libcaca canvas.
Definition: canvas.c:54
caca_display
Definition: caca_internals.h:167
caca_free_canvas
int caca_free_canvas(caca_canvas_t *)
Free a libcaca canvas.
Definition: canvas.c:308
caca_get_event_type
enum caca_event_type caca_get_event_type(caca_event_t const *)
Return an event's type.
Definition: event.c:202
caca_get_canvas_height
int caca_get_canvas_height(caca_canvas_t const *)
Get the canvas height.
Definition: canvas.c:253
CACA_EVENT_QUIT
Definition: caca.h:117
CACA_KEY_PAGEUP
Definition: caca.h:205
caca_get_canvas_width
int caca_get_canvas_width(caca_canvas_t const *)
Get the canvas width.
Definition: canvas.c:239
CACA_EVENT_ANY
Definition: caca.h:119
dy
float dy
Definition: cacaview.c:59
image
Definition: common-image.h:13
dp
static caca_display_t * dp
Definition: cacadraw.c:26
CACA_EVENT_RESIZE
Definition: caca.h:116
refresh_screen
static int refresh_screen(void)
Definition: cacadraw.c:140
CACA_KEY_ESCAPE
Definition: caca.h:192
caca_refresh_display
int caca_refresh_display(caca_display_t *)
Flush pending changes and redraw the screen.
Definition: graphics.c:146
image
static caca_canvas_t * image
Definition: cacadraw.c:25
CACA_DEFAULT
Definition: caca.h:87
caca_free_display
int caca_free_display(caca_display_t *dp)
Detach a caca graphical context from a caca backend context.
Definition: caca.c:238
CACA_KEY_RIGHT
Definition: caca.h:200
caca_set_display_title
int caca_set_display_title(caca_display_t *, char const *)
Set the display title.
Definition: graphics.c:40
caca_set_color_ansi
int caca_set_color_ansi(caca_canvas_t *cv, uint8_t fg, uint8_t bg)
Set the default colour pair for text (ANSI version).
Definition: attr.c:234
caca.h
The libcaca public header.
CACA_KEY_UP
Definition: caca.h:197
config.h
caca_import_canvas_from_file
ssize_t caca_import_canvas_from_file(caca_canvas_t *, char const *, char const *)
Import a file into a canvas.
Definition: import.c:152
caca_get_event
#define caca_get_event(x)
Definition: caca0.h:136
caca_clear_canvas
int caca_clear_canvas(caca_canvas_t *)
Clear the canvas.
Definition: string.c:374
CACA_EVENT_KEY_PRESS
Definition: caca.h:111
dx
float dx
Definition: cacaview.c:59
caca_get_event_key_ch
int caca_get_event_key_ch(caca_event_t const *)
Return a key press or key release event's value.
Definition: event.c:219
CACA_KEY_PAGEDOWN
Definition: caca.h:206
caca_canvas
Definition: caca_internals.h:47
x
static int x
Definition: cacadraw.c:27