[Tux4kids-commits] r1140 - tuxmath/trunk/src
Bolesław Kulbabiński
bolekk-guest at alioth.debian.org
Sat Jul 4 00:26:59 UTC 2009
Author: bolekk-guest
Date: 2009-07-04 00:26:59 +0000 (Sat, 04 Jul 2009)
New Revision: 1140
Modified:
tuxmath/trunk/src/SDL_extras.c
tuxmath/trunk/src/SDL_extras.h
tuxmath/trunk/src/menu.c
tuxmath/trunk/src/menu.h
Log:
added code animating sprites to run_menu()
Modified: tuxmath/trunk/src/SDL_extras.c
===================================================================
--- tuxmath/trunk/src/SDL_extras.c 2009-07-03 23:24:04 UTC (rev 1139)
+++ tuxmath/trunk/src/SDL_extras.c 2009-07-04 00:26:59 UTC (rev 1140)
@@ -356,6 +356,11 @@
return ret;
}
+void UpdateRect(SDL_Surface* surf, SDL_Rect* rect)
+{
+ SDL_UpdateRect(surf, rect->x, rect->y, rect->w, rect->h);
+}
+
int inRect( SDL_Rect r, int x, int y) {
if ((x < r.x) || (y < r.y) || (x > r.x + r.w) || (y > r.y + r.h))
return 0;
Modified: tuxmath/trunk/src/SDL_extras.h
===================================================================
--- tuxmath/trunk/src/SDL_extras.h 2009-07-03 23:24:04 UTC (rev 1139)
+++ tuxmath/trunk/src/SDL_extras.h 2009-07-04 00:26:59 UTC (rev 1140)
@@ -34,6 +34,7 @@
Uint8 r, Uint8 g, Uint8 b, Uint8 a);
void RoundCorners(SDL_Surface* s, Uint16 radius);
SDL_Surface* Flip(SDL_Surface *in, int x, int y);
+void UpdateRect(SDL_Surface* surf, SDL_Rect* rect);
int inRect(SDL_Rect r, int x, int y);
void DarkenScreen(Uint8 bits);
void SwitchScreenMode(void);
Modified: tuxmath/trunk/src/menu.c
===================================================================
--- tuxmath/trunk/src/menu.c 2009-07-03 23:24:04 UTC (rev 1139)
+++ tuxmath/trunk/src/menu.c 2009-07-04 00:26:59 UTC (rev 1140)
@@ -16,6 +16,7 @@
#include "titlescreen.h"
#include "options.h"
#include "fileops.h"
+#include "setup.h"
#include <stdio.h>
#include <stdlib.h>
@@ -148,7 +149,7 @@
}
else
{
- DEBUGMSG(debug_menu_parser, "load_menu_from_file(): unknown tag: %s\n, exiting", buffer);
+ DEBUGMSG(debug_menu_parser, "load_menu_from_file(): unknown tag: %s\n, exiting\n", buffer);
return NULL;
}
@@ -200,16 +201,16 @@
SDL_Event event;
SDL_Rect left_arrow_rect, right_arrow_rect, stopRect, tmp_rect;
- int redraw, i;
+ sprite* tmp_sprite;
+ int i;
int stop = 0;
int items;
int action = NONE;
Uint32 frame_start = 0; //For keeping frame rate constant
- /*Uint32 frame_counter = 0;
Uint32 frame_now = 0;
- int tux_frame = 0;*/
+ Uint32 frame_counter = 0;
int loc = 0; //The currently selected menu item
int old_loc = 1;
int click_flag = 1;
@@ -248,19 +249,22 @@
stopRect.y = 0;
}
- old_loc = loc = 0;
- for(i = 0; i < menu->submenu_size; i++)
- SDL_BlitSurface(menu_item_unselected[i], NULL, screen, &menu->submenu[i]->button_rect);
+ for(i = 0; i < items; i++)
+ {
+ 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);
+ }
SDL_UpdateRect(screen, 0, 0, 0, 0);
/* Move mouse to current button: */
//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
stop = false;
@@ -273,6 +277,12 @@
{
switch (event.type)
{
+ case SDL_QUIT:
+ {
+ cleanup();
+ break;
+ }
+
case SDL_MOUSEMOTION:
{
loc = -1; // By default, don't be in any entry
@@ -534,17 +544,22 @@
} // End event switch statement
if (old_loc != loc) {
+ DEBUGMSG(debug_menu, "run_menu(): changed button focus, old=%d, new=%d\n", old_loc, loc);
if(old_loc >= 0)
{
tmp_rect = menu->submenu[old_loc + menu->first_entry]->button_rect;
SDL_BlitSurface(menu_item_unselected[old_loc], NULL, screen, &tmp_rect);
- SDL_UpdateRect(screen, tmp_rect.x, tmp_rect.y, tmp_rect.x + tmp_rect.w, tmp_rect.y + tmp_rect.h);
+ if(menu->submenu[menu->first_entry + old_loc]->icon)
+ SDL_BlitSurface(menu->submenu[menu->first_entry + old_loc]->icon->default_img, NULL, screen, &menu->submenu[menu->first_entry + old_loc]->icon_rect);
+ SDL_UpdateRect(screen, tmp_rect.x, tmp_rect.y, tmp_rect.w, tmp_rect.h);
}
if(loc >= 0)
{
tmp_rect = menu->submenu[loc + menu->first_entry]->button_rect;
SDL_BlitSurface(menu_item_selected[loc], NULL, screen, &tmp_rect);
- SDL_UpdateRect(screen, tmp_rect.x, tmp_rect.y, tmp_rect.x + tmp_rect.w, tmp_rect.y + tmp_rect.h);
+ if(menu->submenu[menu->first_entry + loc]->icon)
+ SDL_BlitSurface(menu->submenu[menu->first_entry + loc]->icon->default_img, NULL, screen, &menu->submenu[menu->first_entry + loc]->icon_rect);
+ SDL_UpdateRect(screen, tmp_rect.x, tmp_rect.y, tmp_rect.w, tmp_rect.h);
}
old_loc = loc;
}
@@ -556,10 +571,32 @@
break;
}
- } // End SDL_PollEvent while loop
- }
+ } // End of SDL_PollEvent while loop
+ if(frame_counter % 5 == 0 && loc >= 0)
+ {
+ tmp_sprite = menu->submenu[menu->first_entry + loc]->icon;
+ if(tmp_sprite)
+ {
+ SDL_BlitSurface(menu_item_selected[loc], NULL, screen, &menu->submenu[menu->first_entry + loc]->icon_rect);
+ SDL_BlitSurface(tmp_sprite->frame[tmp_sprite->cur], NULL, screen, &menu->submenu[menu->first_entry + loc]->icon_rect);
+ UpdateRect(screen, &menu->submenu[menu->first_entry + loc]->icon_rect);
+ NextFrame(tmp_sprite);
+ }
+ }
+
+ /* 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));
+
+ frame_counter++;
+ } // End of while(!stop) loop
+
/* free button surfaces */
+ DEBUGMSG(debug_menu, "run_menu(): freeing button surfaces\n");
for(i = 0; i < items; i++)
{
SDL_FreeSurface(menu_item_unselected[i]);
@@ -572,10 +609,11 @@
return -1;
}
+/* return button surfaces that are currently displayed (without sprites) */
SDL_Surface** render_buttons(MenuNode* menu, bool selected)
{
SDL_Surface** menu_items = NULL;
- SDL_Rect curr_rect, tmp_rect;
+ SDL_Rect curr_rect;
SDL_Surface* tmp_surf = NULL;
int i;
int items = min(menu->entries_per_screen, menu->submenu_size - menu->first_entry);
@@ -583,7 +621,7 @@
menu_items = (SDL_Surface**) malloc(items * sizeof(SDL_Surface*));
if(NULL == menu_items)
{
- DEBUGMSG(debug_menu, "render_buttons(): failed to allocate memory for buttons!");
+ DEBUGMSG(debug_menu, "render_buttons(): failed to allocate memory for buttons!\n");
return NULL; // error
}
@@ -609,20 +647,8 @@
/* text */
tmp_surf = BlackOutline(_(menu->submenu[menu->first_entry + i]->title),
DEFAULT_MENU_FONT_SIZE, selected ? &yellow : &white);
- tmp_rect = tmp_surf->clip_rect;
- tmp_rect.x = curr_rect.h * 2;
- tmp_rect.y = (curr_rect.h - tmp_surf->h) / 2;
- SDL_BlitSurface(tmp_surf, NULL, menu_items[i], &tmp_rect);
+ SDL_BlitSurface(tmp_surf, NULL, menu_items[i], &menu->submenu[menu->first_entry + i]->text_rect);
SDL_FreeSurface(tmp_surf);
-
- /* icon */
- if(menu->submenu[menu->first_entry + i]->icon)
- {
- tmp_rect = menu->submenu[menu->first_entry + i]->icon->default_img->clip_rect;
- tmp_rect.x = 0;
- tmp_rect.y = 0;
- SDL_BlitSurface(menu->submenu[menu->first_entry + i]->icon->default_img, NULL, menu_items[i], &tmp_rect);
- }
}
return menu_items;
@@ -642,20 +668,20 @@
if(NULL == menu)
{
- DEBUGMSG(debug_menu, "render_menu(): NULL pointer, exiting !");
+ DEBUGMSG(debug_menu, "render_menu(): NULL pointer, exiting !\n");
return;
}
if(0 == menu->submenu_size)
{
- DEBUGMSG(debug_menu, "render_menu(): no submenu, exiting.");
+ DEBUGMSG(debug_menu, "render_menu(): no submenu, exiting.\n");
return;
}
- menu_rect.x = 0.3 * screen->w;
+ menu_rect.x = 0.4 * screen->w;
menu_rect.y = 0.25 * screen->h;
- menu_rect.w = 0.65 * screen->w;
- menu_rect.h = 0.75 * screen->h;
+ menu_rect.w = 0.55 * screen->w;
+ menu_rect.h = 0.7 * screen->h;
for(i = 0; i < menu->submenu_size; i++)
{
@@ -673,7 +699,7 @@
button_w = max_text_w + 3 * max_text_h;
gap = 0.2;
- menu->entries_per_screen = (int) ( (menu_rect.h - gap * button_h) / (1.0 + gap) );
+ menu->entries_per_screen = (int) ( (menu_rect.h - gap * button_h) / ( (1.0 + gap) * button_h ));
for(i = 0; i < menu->submenu_size; i++)
{
@@ -682,6 +708,16 @@
curr_node->button_rect.y = menu_rect.y + i * button_h + (i + 1) * gap * button_h;
curr_node->button_rect.w = button_w;
curr_node->button_rect.h = button_h;
+
+ curr_node->icon_rect = curr_node->button_rect;
+ curr_node->icon_rect.w = curr_node->icon_rect.h;
+
+ curr_node->text_rect = curr_node->button_rect;
+ curr_node->text_rect.y = 0.25 * button_h;
+ curr_node->text_rect.x = (1.0 + gap) * button_h;
+ curr_node->text_rect.h -= 0.25 * button_h;
+ curr_node->text_rect.w -= (1.0 + gap) * button_h;
+
curr_node->font_size = DEFAULT_MENU_FONT_SIZE;
if(curr_node->icon)
Modified: tuxmath/trunk/src/menu.h
===================================================================
--- tuxmath/trunk/src/menu.h 2009-07-03 23:24:04 UTC (rev 1139)
+++ tuxmath/trunk/src/menu.h 2009-07-04 00:26:59 UTC (rev 1140)
@@ -62,6 +62,8 @@
sprite* icon;
SDL_Rect button_rect;
+ SDL_Rect icon_rect;
+ SDL_Rect text_rect;
/* submenu_size = 0 if no submenu */
int submenu_size;
More information about the Tux4kids-commits
mailing list