audio.c: make it possible to play pseudo empty tune
[rofl0r-openDOW.git] / utils / spritetag.c
blobe3549435655adef2b6595d8f28124fb0e60d22f8
1 #include <stdint.h>
2 #include <SDL/SDL.h>
3 //RcB: LINK "-lSDL"
4 #include "../vec2f.h"
5 #define VMODE_W 1200
6 #define VMODE_H 800
7 int SCALE = 16;
9 #include "../sprites/flame.c"
10 #include "../palpic.h"
11 const struct palpic* pic = &flame.header;
13 SDL_Surface *surface;
14 struct vo_desc video;
16 static void init_video() {
17 SDL_Init(SDL_INIT_VIDEO);
18 surface = SDL_SetVideoMode(VMODE_W, VMODE_H, 32, SDL_RESIZABLE | SDL_HWPALETTE);
19 video.mem = surface->pixels;
20 video.pitch = surface->pitch;
21 video.width = VMODE_W;
22 video.height = VMODE_H;
25 vec2f mousepos;
26 vec2f tag;
27 int activesprite;
28 int fullscreen_active;
30 #include "../sdl_rgb.h"
32 static void update_screen() {
33 uint_fast16_t w = palpic_getspritewidth(pic);
34 uint_fast16_t h = palpic_getspriteheight(pic);
35 SDL_UpdateRect(surface, 0, 0, w* SCALE, h*SCALE);
38 static void clear() {
39 uint_fast16_t x, w = palpic_getspritewidth(pic);
40 uint_fast16_t y, h = palpic_getspriteheight(pic);
41 sdl_rgb_t *pix, *ptr = pix = (sdl_rgb_t*) surface->pixels;
42 uint_fast32_t pitch = surface->pitch / 4, inc = pitch - w*SCALE;
43 for(y=0;y<h*SCALE;y++) {
44 for(x=0;x<w*SCALE;x++) *ptr++ = SRGB(0,0,0);
45 ptr+=inc;
49 static float rounddot5(float f) {
50 int r = f;
51 if (f-r >= 0.5) return r + 0.5;
52 return r;
55 static void sprint_tag(char* buf) {
56 sprintf(buf, "[%d] = { %.1f, %.1f },\n", activesprite, rounddot5(tag.x / SCALE), rounddot5(tag.y / SCALE));
59 static void print_spriteinfo() {
60 char buf[128];
61 char buf2[128];
62 sprint_tag(buf2);;
63 snprintf(buf, sizeof buf, "sprite %d/%u mouse: %u,%u scale:%dX ::: %s",
64 activesprite, palpic_getspritecount(pic),
65 (int) (mousepos.x / SCALE), (int) (mousepos.y / SCALE), SCALE, buf2);
66 SDL_WM_SetCaption(buf, 0);
70 static void print_tag() {
71 char buf[128];
72 sprint_tag(buf);
73 printf("%s", buf);
76 /* draws a grid around each pixel. if SCALE == 1, the entire sprite will be overpainted */
77 static void draw_grid() {
78 int x, w = palpic_getspritewidth(pic);
79 int y, h = palpic_getspriteheight(pic);
80 sdl_rgb_t *pix = (sdl_rgb_t*) surface->pixels;
81 unsigned pitch = surface->pitch / 4;
82 /* draw horizontal lines */
83 for(y = 0; y <= h * SCALE; y += SCALE) for(x = 0; x <= w * SCALE; x++)
84 pix[y * pitch + x] = SRGB(255,255,255);
85 /* draw vertical lines */
86 for(y = 0; y <= h * SCALE; y ++) for(x = 0; x <= w * SCALE; x += SCALE)
87 pix[y * pitch + x] = SRGB(255,0,255);
90 static int grid_enabled = 0;
92 static void draw() {
93 int x, w = palpic_getspritewidth(pic);
94 int y, h = palpic_getspriteheight(pic);
95 sdl_rgb_t *pix = (sdl_rgb_t*) surface->pixels;
96 unsigned pitch = surface->pitch / 4;
98 clear();
100 blit_sprite(0, 0, &video, SCALE, pic, activesprite, 0);
102 if(grid_enabled) draw_grid();
104 if(tag.x >= 0 && tag.y >= 0)
105 pix[(int) (tag.y * pitch) + (int) (tag.x)] = SRGB(255, 0, 0);
106 update_screen();
107 print_spriteinfo();
110 static void switch_sprite(int dir) {
111 activesprite += dir;
112 if(activesprite < 0) activesprite = pic->spritecount - 1;
113 else if(activesprite >= pic->spritecount) activesprite = 0;
114 tag.x = -1;
115 tag.y = -1;
116 draw();
119 static void tick(unsigned need_redraw) {
120 if(need_redraw) draw();
123 int main() {
124 init_video();
125 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
126 SDL_EnableKeyRepeat(100, 20);
127 tick(1);
128 while(1) {
129 SDL_Event sdl_event;
130 unsigned need_redraw = 0;
131 while (SDL_PollEvent(&sdl_event)) {
132 switch (sdl_event.type) {
133 case SDL_MOUSEMOTION:
134 mousepos.x = sdl_event.motion.x;
135 mousepos.y = sdl_event.motion.y;
136 print_spriteinfo();
137 break;
138 case SDL_MOUSEBUTTONDOWN:
139 mousepos.x = sdl_event.button.x;
140 mousepos.y = sdl_event.button.y;
141 if(sdl_event.button.button == SDL_BUTTON_WHEELUP) {
142 SCALE++;
143 if(SCALE * palpic_getspritewidth(pic) < VMODE_W &&
144 SCALE * palpic_getspriteheight(pic) < VMODE_H); else SCALE--;
145 } else if(sdl_event.button.button == SDL_BUTTON_WHEELDOWN) {
146 clear();
147 update_screen();
148 SCALE--;
149 if (SCALE == 0) SCALE = 1;
150 } else {
151 tag = mousepos;
153 need_redraw = 1;
154 break;
155 case SDL_MOUSEBUTTONUP:
156 mousepos.x = sdl_event.button.x;
157 mousepos.y = sdl_event.button.y;
158 print_spriteinfo();
159 break;
160 case SDL_QUIT:
161 dun_goofed:
162 // restore desktop video mode correctly...
163 if(fullscreen_active)
164 SDL_WM_ToggleFullScreen(surface);
165 return 0;
166 case SDL_KEYDOWN:
167 switch(sdl_event.key.keysym.sym) {
168 case SDLK_g:
169 grid_enabled = !grid_enabled;
170 need_redraw = 1;
171 break;
172 case SDLK_ESCAPE:
173 goto dun_goofed;
174 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
175 case SDLK_UP:
176 case SDLK_DOWN:
177 case SDLK_RIGHT:
178 case SDLK_LEFT:
179 case SDLK_RETURN:
180 if((sdl_event.key.keysym.mod & KMOD_LALT) ||
181 (sdl_event.key.keysym.mod & KMOD_RALT)) {
182 SDL_WM_ToggleFullScreen(surface);
183 fullscreen_active = !fullscreen_active;
184 } else {
185 print_tag();
187 break;
188 case SDLK_KP_PLUS:
189 switch_sprite(+1);
190 break;
191 case SDLK_KP_MINUS:
192 switch_sprite(-1);
193 break;
194 default:
195 break;
197 break;
198 case SDL_KEYUP:
199 switch(sdl_event.key.keysym.sym) {
200 case SDLK_w: case SDLK_a: case SDLK_s: case SDLK_d:
201 case SDLK_UP:
202 case SDLK_DOWN:
203 case SDLK_RIGHT:
204 case SDLK_LEFT:
205 default:
206 break;
208 default:
209 break;
212 tick(need_redraw);
214 //SDL_ShowCursor(0);