[AD] exsprite update

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Just looked at the examples, and felt like updating exsprite (yes, I
know there's more important things to do right now :) The patch:
- Fixes a bug which made the animation run too fast where vsync is not
available
- Fixes a bug which would cut off parts of the sprite while rotating
- Fixes a bug with color conversion (there is no extra buffer needed,
despite the comments claiming so)
- Changes the default resolution to 640x480
- Adds some lines and circles, without a real reason
- The running.dat we ship for years contains a sound, which was never
used.. now it is used, not sure if in the intended way.

Oh, and I'm planning to finally commit the patch to change #include ""
to <> for all examples as was discussed some time ago - but it's not
worth attaching.

-- 
Elias Pschernig
Index: examples/exsprite.c
===================================================================
RCS file: /cvsroot/alleg/allegro/examples/exsprite.c,v
retrieving revision 1.14
diff -u -p -r1.14 exsprite.c
--- examples/exsprite.c	30 Oct 2004 11:47:19 -0000	1.14
+++ examples/exsprite.c	4 Feb 2005 21:35:29 -0000
@@ -28,7 +28,7 @@
  *    video card. I think, I don't have to remind how slow is it.
  */
 
-
+#include <math.h>
 #include "allegro.h"
 #include "running.h"
 
@@ -50,12 +50,13 @@ int next;
 
 void animate(void)
 {
-   /* waits for vertical retrace interrupt */
-   vsync();
+   /* waits for vertical retrace interrupt, and at least 40 ms */
+   rest(40);
    vsync();
 
    /* blits sprite buffer to screen */
-   blit(sprite_buffer, screen, 0, 0, 120, 80, 82, 82);
+   blit(sprite_buffer, screen, 0, 0, (SCREEN_W - sprite_buffer->w) / 2,
+	(SCREEN_H - sprite_buffer->h) / 2, sprite_buffer->w, sprite_buffer->h);
 
    /* clears sprite buffer with color 0 */
    clear_bitmap(sprite_buffer);
@@ -66,6 +67,9 @@ void animate(void)
    else
       next = FALSE;
 
+   if (frame_number == 0)
+      play_sample(running_data[SOUND_01].dat, 128, 128, 1000, FALSE);
+
    /* increase frame number, or if it's equal 9 (last frame) set it to 0 */
    if (frame_number == 9)
       frame_number = 0;
@@ -77,15 +81,19 @@ void animate(void)
 
 int main(int argc, char *argv[])
 {
-   BITMAP *running;
    char datafile_name[256];
    int angle = 0;
+   int x, y;
+   int text_y;
+   int color;
 
    if (allegro_init() != 0)
       return 1;
    install_keyboard();
+   install_timer();
+   install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL);
 
-   if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0) {
+   if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
       if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
 	 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
 	 allegro_message("Unable to set any graphic mode\n%s\n",
@@ -110,94 +118,95 @@ int main(int argc, char *argv[])
    /* select the palette which was loaded from the datafile */
    set_palette(running_data[PALETTE_001].dat);
 
-   /* aha, set a palette and let Allegro convert colors when blitting */
-   set_color_conversion(COLORCONV_TOTAL);
-   
-   /* create and clear a bitmap for sprite buffering */
-   sprite_buffer = create_bitmap(82, 82);
+   /* create and clear a bitmap for sprite buffering, big
+    * enough to hold the diagonal(sqrt(2)) when rotating */
+   sprite_buffer = create_bitmap(82 * sqrt(2) + 2, 82 * sqrt(2) + 2);
    clear_bitmap(sprite_buffer);
 
-   /* create another bitmap for color conversion from the datafile */
-   running = create_bitmap(82, 82);
+   x = (sprite_buffer->w - 82) / 2;
+   y = (sprite_buffer->h - 82) / 2;
+   color = makecol(0, 80, 0);
+   text_y = SCREEN_H - 10 - text_height(font);
 
    /* write current sprite drawing method */
-   textout_ex(screen, font, "Press a key for next part...",
-	      40, 10, palette_color[1], -1);
-   textout_ex(screen, font, "Using draw_sprite",
-	      1, 190, palette_color[15], -1);
+   textout_centre_ex(screen, font, "Press a key for next part...",
+	      SCREEN_W / 2, 10, palette_color[1], -1);
+   textout_centre_ex(screen, font, "Using draw_sprite",
+		     SCREEN_W / 2, text_y, palette_color[15], -1);
 
    do {
-      blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
-      draw_sprite(sprite_buffer, running, 0, 0);
+      hline(sprite_buffer, 0, y + 82, sprite_buffer->w - 1, color);
+      draw_sprite(sprite_buffer, running_data[frame_number].dat, x, y);
       animate();
    } while (!next);
 
    clear_keybuf();
-   rectfill(screen, 0, 190, 320, 200, 0);
-   textout_ex(screen, font, "Using draw_sprite_h_flip",
-	      1, 190, palette_color[15], -1);
+   rectfill(screen, 0, text_y, SCREEN_W, SCREEN_H, 0);
+   textout_centre_ex(screen, font, "Using draw_sprite_h_flip",
+		     SCREEN_W / 2, text_y, palette_color[15], -1);
 
    do {
-      blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
-      draw_sprite_h_flip(sprite_buffer, running, 0, 0);
+      hline(sprite_buffer, 0, y + 82, sprite_buffer->w - 1, color);
+      draw_sprite_h_flip(sprite_buffer, running_data[frame_number].dat, x, y);
       animate();
    } while (!next);
 
    clear_keybuf();
-   rectfill(screen, 0, 190, 320, 200, 0);
-   textout_ex(screen, font, "Using draw_sprite_v_flip",
-	      1, 190, palette_color[15], -1);
+   rectfill(screen, 0, text_y, SCREEN_W, SCREEN_H, 0);
+   textout_centre_ex(screen, font, "Using draw_sprite_v_flip",
+		     SCREEN_W / 2, text_y, palette_color[15], -1);
 
    do {
-      blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
-      draw_sprite_v_flip(sprite_buffer, running, 0, 0);
+      hline(sprite_buffer, 0, y - 1, sprite_buffer->w - 1, color);
+      draw_sprite_v_flip(sprite_buffer, running_data[frame_number].dat, x, y);
       animate();
    } while (!next);
 
    clear_keybuf();
-   rectfill(screen, 0, 190, 320, 200, 0);
-   textout_ex(screen, font, "Using draw_sprite_vh_flip",
-	      1, 190, palette_color[15], -1);
+   rectfill(screen, 0, text_y, SCREEN_W, SCREEN_H, 0);
+   textout_centre_ex(screen, font, "Using draw_sprite_vh_flip",
+		     SCREEN_W / 2, text_y, palette_color[15], -1);
 
    do {
-      blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
-      draw_sprite_vh_flip(sprite_buffer, running, 0, 0);
+      hline(sprite_buffer, 0, y - 1, sprite_buffer->w - 1, color);
+      draw_sprite_vh_flip(sprite_buffer, running_data[frame_number].dat, x, y);
       animate();
    } while (!next);
 
    clear_keybuf();
-   rectfill(screen, 0, 190, 320, 200, 0);
-   textout_ex(screen, font, "Now with rotating - rotate_sprite",
-	      1, 190, palette_color[15], -1);
+   rectfill(screen, 0, text_y, SCREEN_W, SCREEN_H, 0);
+   textout_centre_ex(screen, font, "Now with rotating - pivot_sprite",
+		     SCREEN_W / 2, text_y, palette_color[15], -1);
 
    do {
-      /* The last argument to rotate_sprite() is a fixed point type,
+      /* The last argument to pivot_sprite() is a fixed point type,
        * so I had to use itofix() routine (integer to fixed).
        */
-      blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
-      rotate_sprite(sprite_buffer, running, 0, 0, itofix(angle));
+      circle(sprite_buffer, x + 41, y + 41, 47, color);
+      pivot_sprite(sprite_buffer, running_data[frame_number].dat, sprite_buffer->w / 2,
+	 sprite_buffer->h / 2, 41, 41, itofix(angle));
       animate();
-      angle += 4;
+      angle -= 4;
    } while (!next);
 
    clear_keybuf();
-   rectfill(screen, 0, 190, 320, 200, 0);
-   textout_ex(screen, font, "Now using rotate_sprite_v_flip", 1, 190,
-	      palette_color[15], -1);
+   rectfill(screen, 0, text_y, SCREEN_W, SCREEN_H, 0);
+   textout_centre_ex(screen, font, "Now using pivot_sprite_v_flip",
+		     SCREEN_W / 2, text_y, palette_color[15], -1);
 
    do {
-      /* The last argument to rotate_sprite_v_flip() is a fixed point type,
+      /* The last argument to pivot_sprite_v_flip() is a fixed point type,
        * so I had to use itofix() routine (integer to fixed).
        */
-      blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
-      rotate_sprite_v_flip(sprite_buffer, running, 0, 0, itofix(angle));
+      circle(sprite_buffer, x + 41, y + 41, 47, color);
+      pivot_sprite_v_flip(sprite_buffer, running_data[frame_number].dat,
+	 sprite_buffer->w / 2, sprite_buffer->h / 2, 41, 41, itofix(angle));
       animate();
       angle += 4;
    } while (!next);
 
    unload_datafile(running_data);
    destroy_bitmap(sprite_buffer);
-   destroy_bitmap(running);
    return 0;
 }
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/