Imported contents: kraptor_final_apr_03_2004.tar.gz
[kraptor.git] / extras / mapedit / eneedit.c
blob42dcefca1b1ae55ab8c617aad474d604acfee6ca
1 /*
2 Editor de la matriz de enemigos para Kraptor
3 NOTA: si se ejecuta con krapmain.dat presente en el mismo directorio, _MUESTRA_ los enemigos
4 con sprites, en vez de usar los numeros. [cool!]
6 Kronoman 2003
7 En memoria de mi querido padre
8 Teclas:
9 DELETE = casilla a 0
10 BARRA = situar valor seleccionado
11 FLECHAS = mover cursor
12 + y - del keypad: alterar valor en +1
13 1 y 2 : alterar valor en +10
14 0 : valor a 0
15 C : limpiar grilla a ceros
16 S : salvar grilla
17 L : cargar grilla
18 V : ver fondo sin grilla
19 R : agregar enemigos del tipo seleccionado al azar
20 ESC : salir
22 NOTA: los mapas se guardan con las funciones especiales de Allegro
23 para salvar los integers en un formato standard, de esa manera,
24 me ahorro los problemas con diferentes tama~os de enteros segun
25 la plataforma.
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <allegro.h>
33 /* Tama¤o del fondo */
34 #define W_FONDO 600
35 #define H_FONDO 4000
37 /* Grilla WxH */
38 #define W_GR 40
39 #define H_GR 40
41 /* Grilla */
42 long grilla[W_FONDO / W_GR][H_FONDO / H_GR];
44 /* En vgrilla pasar -1 para dibujarla, o 0 para ver el bitmap suelto */
45 int vgrilla = -1;
47 int xp = 0, yp = 0, vp = 1; /* x,y, valor a colocar */
49 // cache de sprites de los enemigos, para mostrarlos en pantalla... :)
50 // funciona si esta presenta krapmain.dat en el lugar
51 #define MAX_SPR 100 // sprites cargados 0..MAX_SPR - 1
52 BITMAP *sprite[MAX_SPR];
53 DATAFILE *data = NULL; // datafile
54 /* Bitmap cargado en RAM del fondo */
55 BITMAP *fnd;
56 PALETTE pal; /* paleta */
58 /* Buffer de dibujo */
59 BITMAP *buffer;
62 // Esto carga de disco el cache de sprites
63 // COOL!
64 void cargar_cache_sprites()
66 DATAFILE *p = NULL; // punteros necesarios
67 int i; // para el for
68 char str[1024]; // string de uso general
70 for (i = 0; i < MAX_SPR; i++) sprite[i] = NULL;
72 data = load_datafile("krapmain.dat");
74 if (data == NULL) return; // no esta presente el archivo - DEBUG, informar, o algo... :P
76 p = find_datafile_object(data, "enemigos");
77 if (p == NULL)
79 unload_datafile(data);
80 data = NULL;
81 return;
83 set_config_data((char *)p->dat, p->size);
85 for (i=0; i < MAX_SPR; i++)
87 /* Seccion ENEMIGO_n */
88 sprintf(str,"ENEMIGO_%d", i);
90 // buscar el 1er cuadro de animacion del enemigo unicamente...
91 p = find_datafile_object(data, get_config_string(str, "spr", "null"));
93 if (p == NULL) p = find_datafile_object(data, get_config_string(str, "spr_0", "null"));
95 if (p != NULL) sprite[i] = (BITMAP *)p->dat;
99 // NO se debe descargar el datafile, porque suena todo! [los sprites deben permanecer en RAM]
102 // Limpia la grilla con ceros
103 void limpiar_grilla() {
104 int xx, yy;
105 /* limpio la grilla a ceros */
106 for (xx =0; xx < W_FONDO / W_GR; xx++)
107 for (yy =0; yy < H_FONDO / H_GR; yy++)
108 grilla[xx][yy] = 0;
111 // Redibuja la pantalla
112 void redibujar() {
113 int iy, ix;
115 clear(buffer);
116 blit(fnd, buffer, 0, yp * H_GR,0,0,600,480);
118 /* grilla */
119 if (vgrilla)
121 for (ix=0; ix < W_FONDO / W_GR; ix ++ )
122 line(buffer, ix*W_GR, 0, ix*W_GR, 480, makecol(255,255,255));
124 for (iy=0; iy < H_FONDO / H_GR; iy ++ )
125 line(buffer, 0, iy*H_GR, fnd->w, iy*H_GR, makecol(255,255,255));
128 /* cursor */
129 line(buffer, xp * W_GR, 0, (xp * W_GR) + W_GR, H_GR, makecol(255, 255, 255));
130 line(buffer, xp * W_GR, H_GR, (xp * W_GR) + W_GR, 0, makecol(255, 255, 255));
132 /* mostrar los valores (o sprite, si esta disponible) que contiene la matriz */
133 text_mode(makecol(0,0,0));
134 for (ix=0; ix < W_FONDO / W_GR; ix ++ )
136 for (iy=0; iy < H_FONDO / H_GR; iy ++ )
138 if (yp + iy < H_FONDO / H_GR )
140 if (grilla[ix][yp+iy] != 0)
142 // ver si hay un sprite disponible...
143 if ( (grilla[ix][yp+iy] > 0) && (grilla[ix][yp+iy] < MAX_SPR) )
145 if ( sprite[grilla[ix][yp+iy]-1] != NULL )
147 if (vgrilla)
148 stretch_sprite(buffer, sprite[grilla[ix][yp+iy]-1], ix*W_GR, iy*H_GR, W_GR, H_GR);
149 else
150 draw_sprite(buffer, sprite[grilla[ix][yp+iy]-1],
151 (W_GR / 2) - (sprite[grilla[ix][yp+iy]-1]->w / 2) + (ix*W_GR),
152 (H_GR / 2) - (sprite[grilla[ix][yp+iy]-1]->h / 2) + (iy*H_GR));
156 // valor numerico
157 textprintf(buffer, font,
158 (ix*W_GR)+(W_GR/2), (iy*H_GR)+(H_GR/2), makecol(255,255,255),
159 "%d", (int)grilla[ix][yp+iy]);
166 /* panel de informacion */
167 text_mode(-1); // texto (-1=trans, 1 solido)
168 textprintf(buffer, font, 600,0,makecol(255,255,255),
169 "x:%d", xp);
171 textprintf(buffer, font, 600,20,makecol(255,255,255),
172 "y:%d", yp);
174 textprintf(buffer, font, 600,40,makecol(255,255,255),
175 "v:%d", vp);
177 if ((vp > 0) && (vp < MAX_SPR))
178 if (sprite[vp-1] != NULL)
179 stretch_sprite(buffer, sprite[vp-1], 600, (text_height(font)*2)+40, 40, 40);
181 /* mandar a pantalla */
182 scare_mouse();
183 blit(buffer, screen, 0,0, 0,0,SCREEN_W, SCREEN_H);
184 unscare_mouse();
188 void editar_mapa() {
189 int k;
190 char salvar[1024]; /* archivo a salvar */;
191 salvar[0] = '\0';
193 redibujar();
196 while (1) {
197 k = readkey() >> 8;
199 if (k == KEY_UP) yp--;
200 if (k == KEY_DOWN) yp++;
201 if (k == KEY_LEFT) xp--;
202 if (k == KEY_RIGHT) xp++;
203 if (k == KEY_SPACE) grilla[xp][yp] = vp;
204 if (k == KEY_DEL) grilla[xp][yp] = 0;
205 if (k == KEY_PLUS_PAD) vp++;
206 if (k == KEY_MINUS_PAD) vp--;
207 if (k == KEY_0) vp = 0;
208 if (k == KEY_1) vp += 10;
209 if (k == KEY_2) vp -= 10;
211 if (k == KEY_V) vgrilla = !vgrilla;
213 if (k == KEY_R) {
214 // agregar 1 enemigo al azar... :P
215 // en una casilla vacia solamente
216 int xd, yd;
217 xd = rand()% (W_FONDO / W_GR);
218 yd = rand()% (H_FONDO / H_GR);
219 if (grilla[xd][yd] == 0)
221 grilla[xd][yd] = vp;
222 yp = yd; xp = xd; // feedback al user
227 if (k == KEY_ESC) {
228 if ( alert("Salir de la edicion.", "Esta seguro", "Se perderan los datos no salvados", "Si", "No", 'S', 'N') == 1) return;
231 if (k == KEY_C) {
232 if ( alert("Limpiar grilla.", "Esta seguro", NULL, "Si", "No", 'S', 'N') == 1)
233 { limpiar_grilla(); alert("La grilla se reinicio a ceros (0)", NULL, NULL, "OK", NULL, 0, 0); }
236 if (k == KEY_S) {
237 if (file_select_ex("Archivo de grilla a salvar?", salvar, NULL, 512, 0, 0))
239 PACKFILE *fp;
240 int xx, yy;
241 if ((fp = pack_fopen(salvar, F_WRITE)) != NULL) {
243 // escribo la grilla en formato Intel de 32 bits
244 for (xx =0; xx < W_FONDO / W_GR; xx++)
245 for (yy =0; yy < H_FONDO / H_GR; yy++)
246 pack_iputl(grilla[xx][yy], fp);
248 pack_fclose(fp);
250 alert("Archivo salvado.", salvar, NULL, "OK", NULL, 0, 0);
251 } else
253 alert("Fallo la apertura del archivo!", salvar, NULL, "OK", NULL, 0, 0);
258 if (k == KEY_L)
260 if (file_select_ex("Archivo a cargar?", salvar, NULL, 512, 0, 0))
262 PACKFILE *fp;
263 int xx, yy;
264 if ((fp = pack_fopen(salvar, F_READ)) != NULL) {
266 // leo la grilla en formato Intel de 32 bits
267 for (xx =0; xx < W_FONDO / W_GR; xx++)
268 for (yy =0; yy < H_FONDO / H_GR; yy++)
269 grilla[xx][yy] = pack_igetl(fp);
271 pack_fclose(fp);
273 alert("Archivo cargado.", salvar, NULL, "OK", NULL, 0, 0);
275 else
277 alert("Fallo la apertura del archivo!", salvar, NULL, "OK", NULL, 0, 0);
282 if (yp < 0) yp =0;
283 if (yp > (H_FONDO / H_GR)-1) yp = (H_FONDO / H_GR)-1;
285 if (xp < 0) xp =0;
286 if (xp > (W_FONDO / W_GR)-1) xp = (W_FONDO / W_GR)-1;
288 redibujar();
296 int main() {
297 RGB_MAP tmp_rgb; /* acelera los calculos de makecol, etc */
298 char leerbmp[1024]; /* fondo a cargar */
301 int card = GFX_AUTODETECT, w = 640 ,h = 480 ,color_depth = 8; /* agregado por Kronoman */
303 allegro_init();
304 install_timer();
305 install_keyboard();
306 install_mouse();
308 srand(time(0));
310 set_color_depth(color_depth);
311 if (set_gfx_mode(card, w, h, 0, 0)) {
312 allegro_message("set_gfx_mode(%d x %d x %d bpp): %s\n", w,h, color_depth, allegro_error);
313 return 1;
316 leerbmp[0] = '\0';
318 if (!file_select_ex("Cargue el fondo por favor.", leerbmp, NULL, 512, 0, 0)) return 0;
320 fnd = load_bitmap(leerbmp, pal);
321 if (fnd == NULL || fnd->w != W_FONDO || fnd->h != H_FONDO) {
322 allegro_message("No se pueden cargar o es invalido \n %s!\n", leerbmp);
323 return 1;
326 set_palette(pal);
327 clear(screen);
329 cargar_cache_sprites(); // cargar la cache de sprites... COOL!
331 gui_fg_color = makecol(255,255,255);
332 gui_bg_color = makecol(0,0,0);
333 set_mouse_sprite(NULL);
335 /* esto aumenta un monton los fps (por el makecol acelerado... ) */
336 create_rgb_table(&tmp_rgb, pal, NULL); /* rgb_map es una global de Allegro! */
337 rgb_map = &tmp_rgb;
339 buffer=create_bitmap(SCREEN_W, SCREEN_H);
340 clear(buffer);
341 show_mouse(screen);
344 limpiar_grilla();
345 /* Rock & Roll! */
346 editar_mapa();
348 if (data != NULL) unload_datafile(data);
349 data = NULL;
351 return 0;
353 END_OF_MAIN();