- memleak fix and menu fix from MatzeB
[supertux.git] / src / button.cpp
index 54f889e..793f489 100644 (file)
@@ -1,14 +1,22 @@
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
 //
-// C Implementation: button
-//
-// Description:
-//
-//
-// Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
-//
-// Copyright: See COPYING file that comes with this distribution
-//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
 //
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+// 
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+//  02111-1307, USA.
 
 #include <string.h>
 #include <stdlib.h>
 #include "globals.h"
 #include "button.h"
 
-timer_type Button::popup_timer;
+Timer Button::popup_timer;
 
 Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x, int y, int mw, int mh)
 {
-  timer_init(&popup_timer,false);
+  popup_timer.init(false);
 
   char filename[1024];
 
@@ -38,21 +46,22 @@ Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x
 
   if(mw != -1 || mh != -1)
     {
-      texture_load(&icon,filename,USE_ALPHA);
+      icon = new Surface(filename,USE_ALPHA);
       if(mw != -1)
-        icon.w = mw;
+        icon->w = mw;
       if(mh != -1)
-        icon.h = mh;
+        icon->h = mh;
 
       SDL_Rect dest;
       dest.x = 0;
       dest.y = 0;
-      dest.w = icon.w;
-      dest.h = icon.h;
-      SDL_SoftStretch(icon.sdl_surface, NULL, icon.sdl_surface, &dest);
+      dest.w = icon->w;
+      dest.h = icon->h;
+      SDL_SoftStretch(icon->impl->get_sdl_surface(), NULL,
+          icon->impl->get_sdl_surface(), &dest);
     }
   else
-    texture_load(&icon,filename,USE_ALPHA);
+    icon = new Surface(filename,USE_ALPHA);
 
   info = ninfo;
 
@@ -60,15 +69,15 @@ Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x
 
   rect.x = x;
   rect.y = y;
-  rect.w = icon.w;
-  rect.h = icon.h;
+  rect.w = icon->w;
+  rect.h = icon->h;
   tag = -1;
   state = BUTTON_NONE;
   show_info = false;
   bkgd = NULL;
 }
 
-void Button::change_icon(std::string icon_file, int mw, int mh)
+void Button::change_icon(std::string icon_file, int /*mw*/, int /*mh*/)
 {
   char filename[1024];
 
@@ -83,35 +92,35 @@ void Button::change_icon(std::string icon_file, int mw, int mh)
       snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
     }
 
-  texture_free(&icon);
-  texture_load(&icon,filename,USE_ALPHA);
+  delete icon;
+  icon = new Surface(filename,USE_ALPHA);
 }
 
 void Button::draw()
 {
   if(state == BUTTON_HOVER)
-    if(!timer_check(&popup_timer))
+    if(!popup_timer.check())
      show_info = true;
 
   fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
   fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
   if(bkgd != NULL)
     {
-      texture_draw(bkgd,rect.x,rect.y);
+      bkgd->draw(rect.x,rect.y);
     }
-  texture_draw(&icon,rect.x,rect.y);
+  icon->draw(rect.x,rect.y);
   if(show_info)
     {
       char str[80];
       int i = -32;
 
-      if(0 > rect.x - (int)strlen(info.c_str()) * white_small_text.w)
-        i = rect.w + strlen(info.c_str()) * white_small_text.w;
+      if(0 > rect.x - (int)strlen(info.c_str()) * white_small_text->w)
+        i = rect.w + strlen(info.c_str()) * white_small_text->w;
 
       if(!info.empty())
-        text_draw(&white_small_text, info.c_str(), i + rect.x - strlen(info.c_str()) * white_small_text.w, rect.y, 1);
+        white_small_text->draw(info.c_str(), i + rect.x - strlen(info.c_str()) * white_small_text->w, rect.y, 1);
       sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
-      text_draw(&white_small_text, str, i + rect.x - strlen(str) * white_small_text.w, rect.y + white_small_text.h+2, 1);
+      white_small_text->draw(str, i + rect.x - strlen(str) * white_small_text->w, rect.y + white_small_text->h+2, 1);
     }
   if(state == BUTTON_PRESSED)
     fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
@@ -121,7 +130,7 @@ void Button::draw()
 
 Button::~Button()
 {
-  texture_free(&icon);
+  delete icon;
 }
 
 void Button::event(SDL_Event &event)
@@ -181,7 +190,7 @@ void Button::event(SDL_Event &event)
     }
   else if(event.type == SDL_MOUSEMOTION)
     {
-      timer_start(&popup_timer, 1500);
+      popup_timer.start(1500);
     
       if(show_info)
         {