pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] CVS: Games/Pingus/src screen_ptr.cxx,NONE,1.1 screen_ptr.hx


From: grumbel
Subject: [Pingus-CVS] CVS: Games/Pingus/src screen_ptr.cxx,NONE,1.1 screen_ptr.hxx,NONE,1.1 Makefile.am,1.111,1.112 game_session.cxx,1.13,1.14 game_session.hxx,1.9,1.10 gui_screen.cxx,1.10,1.11 screen.hxx,1.10,1.11 screen_manager.cxx,1.19,1.20 screen_manager.hxx,1.13,1.14
Date: 2 Oct 2002 12:54:20 -0000

Update of /usr/local/cvsroot/Games/Pingus/src
In directory dark:/tmp/cvs-serv30646

Modified Files:
        Makefile.am game_session.cxx game_session.hxx gui_screen.cxx 
        screen.hxx screen_manager.cxx screen_manager.hxx 
Added Files:
        screen_ptr.cxx screen_ptr.hxx 
Log Message:
- cleaned and fixed up the screen manager a bit
- some more tweaks to the timing code

--- NEW FILE: screen_ptr.cxx ---
//  $Id: screen_ptr.cxx,v 1.1 2002/10/02 12:54:18 grumbel Exp $
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 2002 Ingo Ruhnke <address@hidden>
//
//  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 <typeinfo>
#include <iostream>
#include "screen_ptr.hxx"

void
ScreenPtr::decrease_refcount() 
{
  //std::cout << "ScreenPtr::decrease_refcount(): " << screen << std::endl;

  if (delete_it)
    {
      //std::cout << "*ref_count: " << *ref_count << std::endl;

      *ref_count -= 1;
        
      if (*ref_count == 0)
        {
          std::cout << "XXXXXXXXXXXXXX ScreenPtr: deleting: " 
                    << screen << " = " << typeid(*screen).name() << std::endl;
          delete screen;
          delete ref_count;
        }
    }
}

void
ScreenPtr::increase_refcount()
{
  //std::cout << "ScreenPtr::increase_refcount(): " << screen << std::endl;

  if (delete_it)
    {
      //std::cout << "*ref_count: " << *ref_count << std::endl;
      *ref_count += 1;
    }    
}

/* EOF */

--- NEW FILE: screen_ptr.hxx ---
//  $Id: screen_ptr.hxx,v 1.1 2002/10/02 12:54:18 grumbel Exp $
// 
//  Pingus - A free Lemmings clone
//  Copyright (C) 2002 Ingo Ruhnke <address@hidden>
//
//  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.

#ifndef HEADER_PINGUS_SCREEN_PTR_HXX
#define HEADER_PINGUS_SCREEN_PTR_HXX

#include "screen.hxx"

/** The ScreenPtr is a simple smart pointer to point at a Screen. It
    either deletes the pointer or not, depending on the value of
    delete_it */
class ScreenPtr
{
private:
  Screen* screen;
  bool    delete_it;
  long*   ref_count;

public:
  ScreenPtr() 
    : screen(0), delete_it(false), ref_count(0)
  {
  }

  ScreenPtr(Screen* arg_screen, bool arg_delete_it)
    : screen(arg_screen), delete_it(arg_delete_it)
  {
    if (delete_it)
      {
        ref_count = new long;
        *ref_count = 1;
      }
    else
      {
        ref_count = 0;
      }
  }

  ScreenPtr (const ScreenPtr& ptr)
    : screen(ptr.screen), delete_it(ptr.delete_it), ref_count(ptr.ref_count)
  {
    increase_refcount();
  }

  bool operator==(const ScreenPtr& ptr)
  {
    return screen == ptr.screen;
  }

  ScreenPtr& operator= (const ScreenPtr& ptr)
  {
    if (this != &ptr)
      {
        decrease_refcount();
        
        screen    = ptr.screen;
        delete_it = ptr.delete_it;
        ref_count = ptr.ref_count;
        
        increase_refcount();
      }
    return *this;
  }

  ~ScreenPtr()
  {
    decrease_refcount();
  }

  Screen* operator->()
  {
    return screen;
  }

  const Screen& operator*()
  {
    return *screen;
  }

  Screen* get()
  {
    return screen;
  }  

private:
  void decrease_refcount();
  void increase_refcount();
};

#endif

/* EOF */

Index: Makefile.am
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/Makefile.am,v
retrieving revision 1.111
retrieving revision 1.112
diff -u -d -r1.111 -r1.112
--- Makefile.am 28 Sep 2002 11:52:21 -0000      1.111
+++ Makefile.am 2 Oct 2002 12:54:17 -0000       1.112
@@ -254,6 +254,8 @@
 screen.hxx \
 screen_manager.hxx \
 screen_manager.cxx \
+screen_ptr.hxx \
+screen_ptr.cxx \
 server.cxx \
 server.hxx \
 smallmap.cxx \

Index: game_session.cxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/game_session.cxx,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- game_session.cxx    1 Oct 2002 21:48:32 -0000       1.13
+++ game_session.cxx    2 Oct 2002 12:54:18 -0000       1.14
@@ -47,12 +47,20 @@
   last_redraw = CL_System::get_time();
   last_update = CL_System::get_time();
   
-  frames = 0;
-  frame_skips = 0;
+  number_of_redraws = 0;
+  number_of_updates = 0;
+
+  left_over_time = 0;
 }
 
 PingusGameSession::~PingusGameSession ()
 {
+  std::cout << "XXXXXXXX"
+           << " Redraws: " << number_of_redraws
+           << " Updates: " << number_of_updates 
+           << " FrameSkip: " << number_of_updates - number_of_redraws
+           << std::endl;
+
   delete client;
   delete server;
   delete plf;
@@ -67,6 +75,7 @@
 bool
 PingusGameSession::draw (GraphicContext& gc)
 {
+  ++number_of_redraws;
   client->draw (gc);
   last_redraw = CL_System::get_time();
   return true;
@@ -75,16 +84,25 @@
 void
 PingusGameSession::update (const GameDelta& delta)
 {
-  int time_passed = (CL_System::get_time() - last_update);
+  //std::cout << "Left Over Time: " << left_over_time << std::endl;
+
+  int time_passed = (CL_System::get_time() - last_update) + left_over_time;
   int update_time = game_speed;
 
+  left_over_time = 0;
+
   if (time_passed > update_time)
     {
-      for (int i = 0;  i < time_passed; i += update_time)
+      int i;
+      for (i = 0; i < time_passed; i += update_time)
        {
          // This updates the world and all objects
          server->update ();
+         ++number_of_updates;
        }
+
+      left_over_time = time_passed % update_time;
+
       // This updates something else... what?! Well, userinterface and
       // things like that...
       last_update = CL_System::get_time();

Index: game_session.hxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/game_session.hxx,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- game_session.hxx    1 Oct 2002 21:48:32 -0000       1.9
+++ game_session.hxx    2 Oct 2002 12:54:18 -0000       1.10
@@ -52,8 +52,13 @@
   unsigned int last_redraw;
   unsigned int last_update;
 
-  unsigned int frames;
-  unsigned int frame_skips;
+  int left_over_time;
+
+  /** Number of updates */
+  unsigned int number_of_updates;
+
+  /** Number of redraws (frameskip == number_of_updates - number_of_redraws) */
+  unsigned int number_of_redraws;
   
 public:
   /** Create a new game session which is launched on start ()
@@ -62,7 +67,7 @@
   PingusGameSession (std::string arg_filename);
 
   /** Clean up */
-  virtual ~PingusGameSession ();
+  ~PingusGameSession ();
 
   /** Get the results of the last gaming session */
   PingusGameSessionResult get_result ();

Index: gui_screen.cxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/gui_screen.cxx,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- gui_screen.cxx      1 Oct 2002 21:48:32 -0000       1.10
+++ gui_screen.cxx      2 Oct 2002 12:54:18 -0000       1.11
@@ -97,7 +97,7 @@
 void
 GUIScreen::process_button_event (Input::ButtonEvent* event)
 {
-  std::cout << "GUIScreen::process_button_event (Input::ButtonEvent* event)" 
<< std::endl;
+  //std::cout << "GUIScreen::process_button_event (Input::ButtonEvent* event)" 
<< std::endl;
 
   if (event->state == Input::pressed) 
     {

Index: screen.hxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/screen.hxx,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- screen.hxx  1 Oct 2002 21:48:32 -0000       1.10
+++ screen.hxx  2 Oct 2002 12:54:18 -0000       1.11
@@ -32,8 +32,8 @@
 private:
     
 public:
-
   Screen () { }
+  virtual ~Screen () {}
   
   /** Draw this screen @return true if draw was successfull, false if
       frameskip has taken place ('causes a skip of flip_display) */

Index: screen_manager.cxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/screen_manager.cxx,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- screen_manager.cxx  1 Oct 2002 21:48:32 -0000       1.19
+++ screen_manager.cxx  2 Oct 2002 12:54:18 -0000       1.20
@@ -32,10 +32,8 @@
 ScreenManager* ScreenManager::instance_ = 0;
 
 ScreenManager::ScreenManager ()
-  : display_gc (0, 0, CL_Display::get_width (), CL_Display::get_height (), 0, 
0),
-    last_screen (0)
+  : display_gc (0, 0, CL_Display::get_width (), CL_Display::get_height (), 0, 
0)
 {
-  replace_screen_arg = std::pair<Screen*, bool>(0, false);
   cached_action = none;
 
   // Set the root screen
@@ -92,7 +90,7 @@
        }
       else if (cached_action == replace)
        {
-         real_replace_screen (replace_screen_arg.first, 
replace_screen_arg.second);
+         real_replace_screen (replace_screen_arg);
          cached_action = none;
        }
       
@@ -105,27 +103,21 @@
       else
        {
          std::cout << "ScreenManager: fading screens" << std::endl;
-         fade_over (last_screen, get_current_screen());
+         //fade_over (last_screen, get_current_screen());
        }
 
       // Stupid hack to make this thing take less CPU
       CL_System::sleep (0);
 
       /** Delete all screens that are no longer needed */
-      for (std::vector<Screen*>::iterator i = delete_screens.begin (); 
-          i != delete_screens.end (); 
-          ++i)
-       {
-         delete *i;
-       }
-      delete_screens.clear ();
+      delete_screens.clear();
     } 
 }
 
-Screen*
+ScreenPtr
 ScreenManager::get_current_screen()
 {
-  return screens.back ().first;
+  return screens.back ();
 }
 
 ScreenManager*
@@ -145,11 +137,11 @@
   if (!screens.empty())
     {
       std::cout << "ScreenManager::push_screen" << std::endl;
-      screens.back ().first->on_shutdown ();
+      screens.back ()->on_shutdown ();
     }
 
   screen->on_startup ();
-  screens.push_back (std::pair<Screen*, bool> (screen, delete_screen));
+  screens.push_back (ScreenPtr(screen, delete_screen));
 }
 
 void
@@ -164,21 +156,20 @@
 {
   assert (cached_action == none);
   cached_action = replace;
-  replace_screen_arg = std::pair<Screen*, bool> (screen, delete_screen);
+  replace_screen_arg = ScreenPtr(screen, delete_screen);
 }
 
 void
-ScreenManager::real_replace_screen (Screen* screen, bool delete_screen)
+ScreenManager::real_replace_screen (const ScreenPtr& ptr)
 {
   std::cout << "XXXXXXXX ScreenManager::replace_screen" << std::endl;
 
-  screens.back ().first->on_shutdown ();
+  screens.back ()->on_shutdown ();
 
-  if (screens.back ().second) // delete_screen
-    delete_screens.push_back(screens.back ().first);
+  delete_screens.push_back(screens.back ());
   
-  screens.back () = std::pair<Screen*, bool> (screen, delete_screen);
-  screens.back ().first->on_startup ();
+  screens.back () = ptr;
+  screens.back ()->on_startup ();
 }
 
 void
@@ -186,26 +177,25 @@
 {
   std::cout << "XXXXXXXX ScreenManager::pop_screen" << std::endl;
 
-  screens.back ().first->on_shutdown ();
+  screens.back ()->on_shutdown ();
 
-  if (screens.back ().second) // delete_screen
-    delete_screens.push_back(screens.back ().first);
+  delete_screens.push_back(screens.back ());
   
   std::cout << "ScreenManager::real_pop_screen ()" << std::endl;
   screens.pop_back ();
 
   if (!screens.empty ())
     {
-      screens.back ().first->on_startup ();
+      screens.back ()->on_startup ();
     }
 }
 
 void
-ScreenManager::fade_over (Screen* old_screen, Screen* new_screen)
+ScreenManager::fade_over (const ScreenPtr& old_screen, const ScreenPtr& 
new_screen)
 {
   FadeOut::fade_to_black();
-  UNUSED_ARG(old_screen);
-  UNUSED_ARG(new_screen);
+  //UNUSED_ARG(old_screen);
+  //UNUSED_ARG(new_screen);
 
 #if 0
   DeltaManager delta_manager;

Index: screen_manager.hxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/screen_manager.hxx,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- screen_manager.hxx  28 Sep 2002 22:24:24 -0000      1.13
+++ screen_manager.hxx  2 Oct 2002 12:54:18 -0000       1.14
@@ -23,6 +23,7 @@
 #include "pingus.hxx"
 #include <vector>
 
+#include "screen_ptr.hxx"
 #include "display_graphic_context.hxx"
 
 class Screen;
@@ -37,17 +38,18 @@
   /** Screen stack (first is the screen, second is delete_screen,
       which tells if the screen should be deleted onces it got poped
       or replaced) */
-  std::vector<std::pair<Screen*, bool> > screens;
+  std::vector<ScreenPtr> screens;
 
   /** Screens in this vector will be deleted at the end of the
-      main-loop */
-  std::vector<Screen*> delete_screens;
+      main-loop. Its really more a keep alive vector, than a deleting
+      one, but well... */
+  std::vector<ScreenPtr> delete_screens;
 
   /** the screen that was used in the last update() */
-  Screen* last_screen;
+  ScreenPtr last_screen;
 
   enum { none, pop, replace } cached_action;
-  std::pair<Screen*, bool> replace_screen_arg;
+  ScreenPtr replace_screen_arg;
 
 protected:
   ScreenManager ();
@@ -69,16 +71,16 @@
 
 private:
   /** Replace the current screen */
-  void real_replace_screen (Screen*, bool delete_screen);
+  void real_replace_screen (const ScreenPtr&);
     
   /** Remove the current screen and fall back to the last one */
   void real_pop_screen ();
 
   /** FadeOver test*/
-  void fade_over (Screen* old_screen, Screen* new_screen);
+  void fade_over (const ScreenPtr& old_screen, const ScreenPtr& new_screen);
 
   /** @return a pointer to the current Screen */
-  Screen* get_current_screen();
+  ScreenPtr get_current_screen();
 
 public:  
   static ScreenManager* instance ();





reply via email to

[Prev in Thread] Current Thread [Next in Thread]