Root/
Source at commit 77bd5797d3a8d0faffc464199b20d5a41a50a688 created 11 years 9 months ago. By Werner Almesberger, tools/plot.c: clear sample mode screen with C | |
---|---|
1 | /* |
2 | * tools/plot.c - Sample plot |
3 | * |
4 | * Written 2012 by Werner Almesberger |
5 | * Copyright 2012 Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdlib.h> |
15 | |
16 | #include "SDL.h" |
17 | #include "SDL_gfxPrimitives.h" |
18 | |
19 | #include "plot.h" |
20 | |
21 | |
22 | #define XRES 1024 |
23 | #define YRES 1024 |
24 | |
25 | #define RADIUS 3 |
26 | |
27 | #define OLD_RGBA 0xffffff20 |
28 | #define NEW_RGBA 0xffffffff |
29 | |
30 | |
31 | static SDL_Surface *surf; |
32 | static int first = 1; |
33 | static int last_x, last_y; |
34 | |
35 | static SDL_Surface *back; |
36 | static SDL_Rect back_rect = { |
37 | .x = 0, |
38 | .y = 0, |
39 | .w = 2*RADIUS+1, |
40 | .h = 2*RADIUS+1, |
41 | }; |
42 | |
43 | static SDL_Rect front_rect = { |
44 | .w = 2*RADIUS+1, |
45 | .h = 2*RADIUS+1, |
46 | }; |
47 | |
48 | |
49 | int plot(int x, int y) |
50 | { |
51 | SDL_Event event; |
52 | |
53 | if (!first) { |
54 | SDL_BlitSurface(back, &back_rect, surf, &front_rect); |
55 | SDL_LockSurface(surf); |
56 | filledCircleColor(surf, last_x, last_y, RADIUS, OLD_RGBA); |
57 | SDL_UnlockSurface(surf); |
58 | } |
59 | |
60 | front_rect.x = x; |
61 | front_rect.y = y; |
62 | x += RADIUS; |
63 | y += RADIUS; |
64 | |
65 | SDL_BlitSurface(surf, &front_rect, back, &back_rect); |
66 | SDL_LockSurface(surf); |
67 | filledCircleColor(surf, x, y, RADIUS, NEW_RGBA); |
68 | SDL_UnlockSurface(surf); |
69 | |
70 | if (!first) |
71 | SDL_UpdateRect(surf, |
72 | last_x-RADIUS, last_y-RADIUS, 2*RADIUS+1, 2*RADIUS+1); |
73 | SDL_UpdateRect(surf, x-RADIUS, y-RADIUS, 2*RADIUS+1, 2*RADIUS+1); |
74 | |
75 | first = 0; |
76 | last_x = x; |
77 | last_y = y; |
78 | |
79 | while (SDL_PollEvent(&event)) |
80 | switch (event.type) { |
81 | case SDL_KEYDOWN: |
82 | switch (event.key.keysym.sym) { |
83 | case SDLK_c: |
84 | SDL_LockSurface(surf); |
85 | SDL_FillRect(surf, NULL, |
86 | SDL_MapRGB(surf->format, 0, 0, 0)); |
87 | SDL_UpdateRect(surf, 0, 0, 0, 0); |
88 | SDL_UnlockSurface(surf); |
89 | break; |
90 | case SDLK_q: |
91 | return 0; |
92 | default: |
93 | break; |
94 | } |
95 | break; |
96 | case SDL_QUIT: |
97 | return 0; |
98 | default: |
99 | break; |
100 | } |
101 | |
102 | return 1; |
103 | } |
104 | |
105 | |
106 | void plot_init(void) |
107 | { |
108 | const SDL_PixelFormat *f; |
109 | |
110 | if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
111 | fprintf(stderr, "SDL_init: %s\n", SDL_GetError()); |
112 | exit(1); |
113 | } |
114 | atexit(SDL_Quit); |
115 | |
116 | surf = SDL_SetVideoMode(XRES+2*RADIUS+1, YRES+2*RADIUS+1, 0, |
117 | SDL_SWSURFACE); |
118 | if (!surf) { |
119 | fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); |
120 | exit(1); |
121 | } |
122 | |
123 | f = surf->format; |
124 | back = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*RADIUS+1, 2*RADIUS+1, |
125 | f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask); |
126 | if (!back) { |
127 | fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); |
128 | exit(1); |
129 | } |
130 | } |
131 |
Branches:
master
tornado-v1