[Tux4kids-commits] r1142 - tuxmath/trunk/src

Bolesław Kulbabiński bolekk-guest at alioth.debian.org
Sat Jul 4 15:41:30 UTC 2009


Author: bolekk-guest
Date: 2009-07-04 15:41:28 +0000 (Sat, 04 Jul 2009)
New Revision: 1142

Modified:
   tuxmath/trunk/src/menu.c
   tuxmath/trunk/src/menu.h
   tuxmath/trunk/src/titlescreen.c
   tuxmath/trunk/src/titlescreen.h
Log:
moved handling titlescreen events into separate functions

Modified: tuxmath/trunk/src/menu.c
===================================================================
--- tuxmath/trunk/src/menu.c	2009-07-04 03:11:11 UTC (rev 1141)
+++ tuxmath/trunk/src/menu.c	2009-07-04 15:41:28 UTC (rev 1142)
@@ -211,8 +211,8 @@
   Uint32 frame_start = 0;       //For keeping frame rate constant
   Uint32 frame_now = 0;
   Uint32 frame_counter = 0;
-  int loc = 0;                  //The currently selected menu item
-  int old_loc = 1;
+  int loc = -1;                  //The currently selected menu item
+  int old_loc = -1;
   int click_flag = 1;
 
   for(;;) /* one loop body execution for one menu page */
@@ -253,7 +253,10 @@
 
     for(i = 0; i < items; i++)
     {
-      SDL_BlitSurface(menu_item_unselected[i], NULL, screen, &menu->submenu[menu->first_entry + i]->button_rect);
+      if(loc == i)
+        SDL_BlitSurface(menu_item_selected[i], NULL, screen, &menu->submenu[menu->first_entry + i]->button_rect);
+      else
+        SDL_BlitSurface(menu_item_unselected[i], NULL, screen, &menu->submenu[menu->first_entry + i]->button_rect);
       if(menu->submenu[menu->first_entry + i]->icon)
         SDL_BlitSurface(menu->submenu[menu->first_entry + i]->icon->default_img, NULL, screen, &menu->submenu[menu->first_entry + i]->icon_rect);
     }
@@ -263,7 +266,6 @@
     //cursor.x = menu_button_rect[imod].x + menu_button_rect[imod].w/2;
     //cursor.y = menu_button_rect[imod].y + menu_button_rect[imod].h/2;
     SDL_WM_GrabInput(SDL_GRAB_OFF);
-    old_loc = loc = 0;
 
     /******** Main loop:                                *********/
     while (SDL_PollEvent(&event));  // clear pending events
@@ -564,6 +566,9 @@
           old_loc = loc;
         }
 
+        if(HandleTitleScreenEvents(&event))
+          stop = true;
+
         switch(action)
         {
           case RESIZED:
@@ -585,12 +590,14 @@
         }
       }
 
+      HandleTitleScreenAnimations();
+
       /* Wait so we keep frame rate constant: */
       frame_now = SDL_GetTicks();
       if (frame_now < frame_start)
         frame_start = frame_now;  // in case the timer wraps around
-      if (frame_now - frame_start < 33)
-        SDL_Delay(33-(frame_now-frame_start));
+      if((frame_now - frame_start) < 1000 / MAX_FPS)
+        SDL_Delay(1000 / MAX_FPS - (frame_now - frame_start));
 
       frame_counter++;
     } // End of while(!stop) loop

Modified: tuxmath/trunk/src/menu.h
===================================================================
--- tuxmath/trunk/src/menu.h	2009-07-04 03:11:11 UTC (rev 1141)
+++ tuxmath/trunk/src/menu.h	2009-07-04 15:41:28 UTC (rev 1142)
@@ -19,6 +19,11 @@
 #include "globals.h"
 #include "loaders.h"
 
+/* titlescreen & menu frame rate */
+#define MAX_FPS                    30
+/* number of "real" frames per one sprite frame */
+#define SPRITE_FRAME_DELAY         6
+
 /* these are all menu choices that are available in tuxmath.
    By using a define we can create both an enum and
    a string array without writing these names twice */

Modified: tuxmath/trunk/src/titlescreen.c
===================================================================
--- tuxmath/trunk/src/titlescreen.c	2009-07-04 03:11:11 UTC (rev 1141)
+++ tuxmath/trunk/src/titlescreen.c	2009-07-04 15:41:28 UTC (rev 1142)
@@ -228,7 +228,7 @@
 {
   Uint32 start_time = 0;
   SDL_Rect tux_anim, title_anim;
-  int i, tux_pix_skip, title_pix_skip;
+  int i, tux_pix_skip, title_pix_skip, curr_time;
 
   if (Opts_UsingSound())
   {
@@ -347,10 +347,9 @@
       SDL_UpdateRect(screen, title_anim.x, title_anim.y,
           min(title_anim.w + title_pix_skip, screen->w - title_anim.x), title_anim.h);
 
-      while((SDL_GetTicks() - start_time) < 1000 / ANIM_FPS)
-      {
-        SDL_Delay(2);
-      }
+      curr_time = SDL_GetTicks();
+      if((curr_time - start_time) < 1000 / ANIM_FPS)
+        SDL_Delay(1000 / ANIM_FPS - (curr_time - start_time));
     }
   }
 
@@ -398,7 +397,7 @@
   SDL_BlitSurface(current_bkg(), NULL, screen, &bkg_rect);
   SDL_BlitSurface(Tux->frame[0], NULL, screen, &tux_rect);
   SDL_BlitSurface(title, NULL, screen, &title_rect);
-  SDL_UpdateRect(screen, 0, 0, 0, 0);
+  //SDL_UpdateRect(screen, 0, 0, 0, 0);
 }
 
 /* Render and position all titlescreen items to match current
@@ -488,8 +487,53 @@
 }
 
 
+/* handle titlescreen events (easter egg)
+   this function should be called from event loops
+   return 1 if events require full redraw */
+int HandleTitleScreenEvents(const SDL_Event* evt)
+{
+  return handle_easter_egg(evt);
+}
 
+/* handle all titlescreen blitting
+   this function should be called after every animation frame */
+void HandleTitleScreenAnimations()
+{
+  static int frame_counter = 0;
+  int tux_frame;
 
+  /* --- make Tux blink --- */
+  switch (frame_counter % TUX6)
+  {
+    case 0:    tux_frame = 1; break;
+    case TUX1: tux_frame = 2; break;
+    case TUX2: tux_frame = 3; break;
+    case TUX3: tux_frame = 4; break;
+    case TUX4: tux_frame = 3; break;
+    case TUX5: tux_frame = 2; break;
+    default: tux_frame = 0;
+  }
+
+  if (Tux && tux_frame)
+  {
+    /* Redraw background to keep edges anti-aliased properly: */
+    SDL_BlitSurface(current_bkg(),&tux_rect, screen, &tux_rect);
+    SDL_BlitSurface(Tux->frame[tux_frame - 1], NULL, screen, &tux_rect);
+    UpdateRect(screen, &tux_rect);
+  }
+
+  if (egg_active) { //if we need to, draw the egg cursor
+    //who knows why GetMouseState() doesn't take Sint16's...
+    SDL_GetMouseState((int*)(&cursor.x), (int*)(&cursor.y));
+    cursor.x -= egg->w / 2; //center vertically
+    SDL_BlitSurface(egg, NULL, screen, &cursor);
+    UpdateRect(screen, &cursor);
+  }
+
+  frame_counter++;
+}
+
+
 /***********************************************************/
 /*                                                         */
 /*    "Private functions" (callable only from this file)   */
@@ -1530,7 +1574,6 @@
   int buttonheight = 0;
   int i = 0;
   int imod = 0;                 // i % n_entries_per_screen
-  int tux_frame = 0;
   int click_flag = 1;
   int use_sprite = 0;
   int warp_mouse = 0;
@@ -2082,10 +2125,9 @@
           }  /* End of key switch statement */
         }  // End of case SDL_KEYDOWN in outer switch statement
       }  // End event switch statement
-      if (handle_easter_egg(&event) )
+
+      if(HandleTitleScreenEvents(&event))
         redraw = 1;
-      else
-        ; //egg_active = 0;
     }  // End SDL_PollEvent while loop
 
 
@@ -2250,37 +2292,8 @@
     old_loc = loc;
     old_loc_screen_start = loc_screen_start;
 
+    HandleTitleScreenAnimations();
 
-
-    /* --- make Tux blink --- */
-    switch (frame_counter % TUX6)
-    {
-      case 0:    tux_frame = 1; break;
-      case TUX1: tux_frame = 2; break;
-      case TUX2: tux_frame = 3; break;
-      case TUX3: tux_frame = 4; break;
-      case TUX4: tux_frame = 3; break;
-      case TUX5: tux_frame = 2; break;
-      default: tux_frame = 0;
-    }
-
-    if (Tux && tux_frame)
-    {
-      /* Redraw background to keep edges anti-aliased properly: */
-      SDL_BlitSurface(current_bkg(),&tux_rect, screen, &tux_rect);
-      SDL_BlitSurface(Tux->frame[tux_frame - 1], NULL, screen, &tux_rect);
-      SDL_UpdateRect(screen, tux_rect.x, tux_rect.y, tux_rect.w, tux_rect.h);
-      //SDL_UpdateRect(screen, 0, 0, 0, 0);
-    }
-
-    if (egg_active) { //if we need to, draw the egg cursor
-      //who knows why GetMouseState() doesn't take Sint16's...
-      SDL_GetMouseState((int*)(&cursor.x), (int*)(&cursor.y));
-      cursor.x -= egg->w / 2; //center vertically
-      SDL_BlitSurface(egg, NULL, screen, &cursor);
-      SDL_UpdateRect(screen, cursor.x, cursor.y, cursor.w, cursor.h);
-    }
-
     /* Wait so we keep frame rate constant: */
     frame_now = SDL_GetTicks();
     if (frame_now < frame_start)

Modified: tuxmath/trunk/src/titlescreen.h
===================================================================
--- tuxmath/trunk/src/titlescreen.h	2009-07-04 03:11:11 UTC (rev 1141)
+++ tuxmath/trunk/src/titlescreen.h	2009-07-04 15:41:28 UTC (rev 1142)
@@ -74,12 +74,11 @@
 #define FULL_CIRCLE                           140
 
 
-/* Title screen animation constants */
+/* trans_wipe() animation constants */
 #define ANIM_FRAMES                30 /* frames to be displayed */
 #define ANIM_FPS                   25 /* max fps */
 
 
-
 extern SDL_Event  event;
 
 
@@ -126,6 +125,8 @@
 void TitleScreen(void);
 int RenderTitleScreen(void);
 void DrawTitleScreen(void);
+int HandleTitleScreenEvents(const SDL_Event* evt);
+void HandleTitleScreenAnimations();
 int ChooseMission(void);  //FIXME really should be in fileops.c
 int choose_menu_item(const char **menu_text, 
                      sprite **menu_sprites, 




More information about the Tux4kids-commits mailing list