pingus-cvs
[Top][All Lists]
Advanced

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

[Pingus-CVS] r2628 - branches/pingus_sdl/src/display


From: grumbel at BerliOS
Subject: [Pingus-CVS] r2628 - branches/pingus_sdl/src/display
Date: Sun, 12 Feb 2006 16:55:53 +0100

Author: grumbel
Date: 2006-02-12 16:55:52 +0100 (Sun, 12 Feb 2006)
New Revision: 2628

Added:
   branches/pingus_sdl/src/display/scene_graph.cxx
   branches/pingus_sdl/src/display/scene_graph.hxx
   branches/pingus_sdl/src/display/scene_group.hxx
   branches/pingus_sdl/src/display/scene_node.hxx
   branches/pingus_sdl/src/display/sprite_node.cxx
   branches/pingus_sdl/src/display/sprite_node.hxx
Log:
- some scene graph framework

Added: branches/pingus_sdl/src/display/scene_graph.cxx
===================================================================
--- branches/pingus_sdl/src/display/scene_graph.cxx     2006-02-11 14:32:52 UTC 
(rev 2627)
+++ branches/pingus_sdl/src/display/scene_graph.cxx     2006-02-12 15:55:52 UTC 
(rev 2628)
@@ -0,0 +1,95 @@
+/*  $Id$
+**   __      __ __             ___        __   __ __   __
+**  /  \    /  \__| ____    __| _/_______/  |_|__|  | |  |   ____
+**  \   \/\/   /  |/    \  / __ |/  ___/\   __\  |  | |  | _/ __ \
+**   \        /|  |   |  \/ /_/ |\___ \  |  | |  |  |_|  |_\  ___/
+**    \__/\  / |__|___|  /\____ /____  > |__| |__|____/____/\___  >
+**         \/          \/      \/    \/                         \/
+**  Copyright (C) 2005 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 "scene_graph.hpp"
+
+SceneGraph::SceneGraph()
+{
+  screen = Field(800/32, 600/32); // could use microtiles instead
+}
+
+SceneGraph::~SceneGraph()
+{
+}
+
+void
+SceneGraph::add(SceneNode* node)
+{
+  nodes.push_back(node);
+}
+
+void
+SceneGraph::render()
+{
+  screen.clear(false);
+  // Find out what regions of the screen have changed
+  for(Nodes::iterator i = nodes.begin(); i != nodes.end(); ++i)
+    { // could limit this to stuff that has changed
+      mark_screen_region(i, i->get_screen_rect());
+    }
+  
+  // Redraw said regions
+  for(int y = 0; y < screen.get_height(); ++y)
+    for(int x = 0; x < screen.get_width(); ++x)
+      {
+        if (screen(x, y))
+          {
+            int width = 1;
+
+            // Join cells horizontally
+            while(screen(x+width,y)) { width += 1; }
+            
+            for(Nodes::iterator i = nodes.begin(); i != nodes.end(); ++i)
+              { // FIXME: could optimize this to only draw the ones that touch 
the region
+
+                SDL_Rect clip_rect;
+
+                clip_rect.x = x*32;
+                clip_rect.y = y*32;
+                clip_rect.w = 32*width;
+                clip_rect.h = 32;
+
+                SDL_SetClipRect(screen, &rect);
+
+                (*i)->render(screen);
+              }
+
+            x += width;
+          }
+      }
+}
+
+void
+SceneGraph::mark_screen_region(SceneNode* node, const Rect& rect)
+{
+  for(int y = rect.top/32; y < rect.bottom/32; ++y)
+    for(int x = rect.left/32; x < rect.left/32; ++x)
+      {
+        screen(x, y) = true;
+        // screen(x, y).push_back(screen_node); bad?
+      }
+}
+
+/* EOF */

Added: branches/pingus_sdl/src/display/scene_graph.hxx
===================================================================
--- branches/pingus_sdl/src/display/scene_graph.hxx     2006-02-11 14:32:52 UTC 
(rev 2627)
+++ branches/pingus_sdl/src/display/scene_graph.hxx     2006-02-12 15:55:52 UTC 
(rev 2628)
@@ -0,0 +1,56 @@
+//  $Id$
+// 
+//  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_SCENE_GRAPH_HPP
+#define HEADER_SCENE_GRAPH_HPP
+
+#define EMPTY_TILE 0x00000000
+#define FULL_TILE  0x00002020
+
+#define MICROTILE(x0, y0, x1, y1) (((x0) << 24) | ((y0) << 16) | ((x1) << 8) | 
(y1))
+
+class SceneNode;
+
+/** */
+class SceneGraph
+{
+private:
+  /** true if the region needs a refresh, false otherwise */
+  Field<bool> screen;
+
+  typedef std::vector<SceneNode*> Nodes;
+  Nodes nodes;
+  
+public:
+  SceneGraph();
+  ~SceneGraph();
+
+  void add(SceneGroup* );
+  
+  /** Draw the SceneGraph to the screen */
+  void render();
+  void mark_screen_region(SceneNode* node, const Rect& rect);
+private:
+  SceneGraph (const SceneGraph&);
+  SceneGraph& operator= (const SceneGraph&);
+};
+
+#endif
+
+/* EOF */

Added: branches/pingus_sdl/src/display/scene_group.hxx
===================================================================
--- branches/pingus_sdl/src/display/scene_group.hxx     2006-02-11 14:32:52 UTC 
(rev 2627)
+++ branches/pingus_sdl/src/display/scene_group.hxx     2006-02-12 15:55:52 UTC 
(rev 2628)
@@ -0,0 +1,36 @@
+//  $Id$
+// 
+//  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_SCENE_GROUP_HPP
+#define HEADER_SCENE_GROUP_HPP
+
+/** */
+class Scene_Group
+{
+private:
+public:
+
+private:
+  Scene_Group (const Scene_Group&);
+  Scene_Group& operator= (const Scene_Group&);
+};
+
+#endif
+
+/* EOF */

Added: branches/pingus_sdl/src/display/scene_node.hxx
===================================================================
--- branches/pingus_sdl/src/display/scene_node.hxx      2006-02-11 14:32:52 UTC 
(rev 2627)
+++ branches/pingus_sdl/src/display/scene_node.hxx      2006-02-12 15:55:52 UTC 
(rev 2628)
@@ -0,0 +1,43 @@
+//  $Id$
+// 
+//  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_SCENE_NODE_HPP
+#define HEADER_SCENE_NODE_HPP
+
+/** */
+class SceneNode
+{
+private:
+public:
+  SceneNode();
+  ~SceneNode();
+
+  /** Return the area that the SceneNode covers on the screen */
+  virtual Rect get_screen_rect() const =0;
+
+  virtual bool has_changed() const =0;
+
+private:
+  SceneNode (const SceneNode&);
+  SceneNode& operator= (const SceneNode&);
+};
+
+#endif
+
+/* EOF */

Added: branches/pingus_sdl/src/display/sprite_node.cxx
===================================================================
--- branches/pingus_sdl/src/display/sprite_node.cxx     2006-02-11 14:32:52 UTC 
(rev 2627)
+++ branches/pingus_sdl/src/display/sprite_node.cxx     2006-02-12 15:55:52 UTC 
(rev 2628)
@@ -0,0 +1,144 @@
+//  $Id$
+// 
+//  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 <SDL.h>
+#include "sprite_node.hxx"
+
+SpriteNode::SpriteNode(const std::string resourcename)
+{
+  surface = IMG_Load(resourcename.c_str());
+}
+
+SpriteNode::~SpriteNode() 
+{
+  SDL_FreeSurface(surface);
+}
+
+void
+SpriteNode::set_pos(const Vector2& pos_)
+{
+  pos = pos_;
+}
+
+Vector2
+SpriteNode::get_pos() const
+{
+  return pos;
+}
+
+void
+SpriteNode::render(SDL_Surface* screen)
+{
+  SDL_Rect dest;
+
+  dest.x = pos.x;
+  dest.y = pos.y;
+
+  SDL_BlitSurface(surface, NULL
+                  screen,  dest);
+
+  has_changed_ = false;
+  old_repeat = repeat;
+  old_pos    = pos;
+}
+
+Rect
+SpriteNode::get_old_screen_rect() const
+{
+  return Rect(old_pos.x - (old_repeat.left * surface->w),
+              old_pos.y - (old_repeat.top  * surface->h), 
+              surface->w * (old_repeat.left + old_repeat.right + 1),
+              surface->h * (old_repeat.top  + old_repeat.bottom + 1));
+}
+
+Rect
+SpriteNode::get_screen_rect() const
+{
+  return Rect(pos.x - (repeat.left * surface->w),
+              pos.y - (repeat.top  * surface->h), 
+              surface->w * (repeat.left + repeat.right + 1),
+              surface->h * (repeat.top  + repeat.bottom + 1));
+}
+
+bool
+SpriteNode::has_changed() const
+{
+  return has_changed_;
+}
+
+void
+SpriteNode::set_pos(const Vector2& pos_)
+{
+  if (pos != pos_) has_changed_ = true;
+  pos = pos_;
+}
+
+void
+SpriteNode::set_left_repeat(int left)
+{
+  if (left != repeat.left) has_changed_ = true;
+  repeat.left = left;
+}
+
+void
+SpriteNode::set_right_repeat(int right)
+{
+  if (right != repeat.right) has_changed_ = true;
+  repeat.right = right;
+}
+
+void
+SpriteNode::set_top_repeat(int top)
+{
+  if (top != repeat.top) has_changed_ = true;
+  repeat.top = top;
+}
+
+void
+SpriteNode::set_bottom_repeat(int bottom)
+{
+  if (bottom != repeat.bottom) has_changed_ = true;
+  repeat.bottom = bottom;
+}
+
+int
+SpriteNode::get_left_repeat(int left) const
+{
+  return repeat.left;
+}
+
+int
+SpriteNode::get_right_repeat(int right) const
+{
+  return repeat.right;
+}
+
+int
+SpriteNode::get_top_repeat(int top) const
+{
+  return repeat.top;
+}
+
+int
+SpriteNode::get_bottom_repeat(int bottom) const
+{
+  return repeat.bottom;
+}
+
+/* EOF */

Added: branches/pingus_sdl/src/display/sprite_node.hxx
===================================================================
--- branches/pingus_sdl/src/display/sprite_node.hxx     2006-02-11 14:32:52 UTC 
(rev 2627)
+++ branches/pingus_sdl/src/display/sprite_node.hxx     2006-02-12 15:55:52 UTC 
(rev 2628)
@@ -0,0 +1,62 @@
+//  $Id$
+// 
+//  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 "vector2.hxx"
+#include "scene_node.hxx"
+
+class SpriteNode : public SceneNode
+{
+private:
+  Vector2 pos;
+  Rect    repeat;
+
+  Vector2 old_pos;
+  Rect    old_repeat;
+
+  SDL_Surface* surface;
+  bool has_changed_;
+
+public:
+  SpriteNode(const std::string resourcename);
+  ~SpriteNode();
+
+  bool has_changed() const;
+  Rect get_screen_rect() const;
+  Rect get_old_screen_rect() const;
+    
+  void    set_pos(const Vector2& pos);
+  Vector2 get_pos() const;
+  
+  void set_left_repeat(int left);
+  void set_right_repeat(int right);
+
+  void set_top_repeat(int top);
+  void set_bottom_repeat(int bottom);
+
+  int get_left_repeat(int left) const;
+  int get_right_repeat(int right) const;
+
+  int get_top_repeat(int top) const;
+  int get_bottom_repeat(int bottom) const;
+
+  /** Draw the SDL_Surface to the screen */
+  void render(SDL_Surface* screen);
+};
+
+/* EOF */





reply via email to

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