pingus-cvs
[Top][All Lists]
Advanced

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

[Pingus-CVS] r3855 - in trunk/pingus: . src/display


From: grumbel at BerliOS
Subject: [Pingus-CVS] r3855 - in trunk/pingus: . src/display
Date: Thu, 17 Jul 2008 18:09:07 +0200

Author: grumbel
Date: 2008-07-17 18:09:07 +0200 (Thu, 17 Jul 2008)
New Revision: 3855

Added:
   trunk/pingus/src/display/opengl_framebuffer.cpp
   trunk/pingus/src/display/opengl_framebuffer.hpp
   trunk/pingus/src/display/opengl_framebuffer_surface_impl.cpp
   trunk/pingus/src/display/opengl_framebuffer_surface_impl.hpp
Modified:
   trunk/pingus/SConstruct
   trunk/pingus/src/display/delta_framebuffer.cpp
   trunk/pingus/src/display/delta_framebuffer.hpp
   trunk/pingus/src/display/display.cpp
Log:
Couldn't resist, implemented OpenGL renderer

Modified: trunk/pingus/SConstruct
===================================================================
--- trunk/pingus/SConstruct     2008-07-17 16:07:39 UTC (rev 3854)
+++ trunk/pingus/SConstruct     2008-07-17 16:09:07 UTC (rev 3855)
@@ -119,8 +119,13 @@
 'src/gui/component.cpp',
 'src/display/display.cpp', 
 'src/display/framebuffer_surface.cpp', 
+
 'src/display/sdl_framebuffer_surface_impl.cpp', 
 'src/display/sdl_framebuffer.cpp', 
+
+'src/display/opengl_framebuffer_surface_impl.cpp', 
+'src/display/opengl_framebuffer.cpp', 
+
 'src/display/delta_framebuffer.cpp', 
 'src/display/rect_merger.cpp',
 'src/gui/group_component.cpp', 
@@ -300,7 +305,7 @@
    opts.Add('LIBPATH',    'Additional library paths',      [])
    opts.Add('CPPFLAGS',   'Additional preprocessor flags', [])
    opts.Add('CPPDEFINES', 'defined constants', [])
-   opts.Add('LIBS',       'Additional libraries', [])
+   opts.Add('LIBS',       'Additional libraries', ['GL'])
    opts.Add('CCFLAGS',    'C Compiler flags', [])
    opts.Add('LINKFLAGS',  'Linker Compiler flags', [])
 

Modified: trunk/pingus/src/display/delta_framebuffer.cpp
===================================================================
--- trunk/pingus/src/display/delta_framebuffer.cpp      2008-07-17 16:07:39 UTC 
(rev 3854)
+++ trunk/pingus/src/display/delta_framebuffer.cpp      2008-07-17 16:09:07 UTC 
(rev 3855)
@@ -340,7 +340,7 @@
 }
 
 Size
-DeltaFramebuffer::get_size()
+DeltaFramebuffer::get_size() const
 {
   return framebuffer->get_size();
 }

Modified: trunk/pingus/src/display/delta_framebuffer.hpp
===================================================================
--- trunk/pingus/src/display/delta_framebuffer.hpp      2008-07-17 16:07:39 UTC 
(rev 3854)
+++ trunk/pingus/src/display/delta_framebuffer.hpp      2008-07-17 16:09:07 UTC 
(rev 3855)
@@ -51,7 +51,7 @@
   void draw_rect(const Rect& rect, const Color& color);
   void fill_rect(const Rect& rect, const Color& color);
 
-  Size get_size();
+  Size get_size() const;
 
 private:
   DeltaFramebuffer (const DeltaFramebuffer&);

Modified: trunk/pingus/src/display/display.cpp
===================================================================
--- trunk/pingus/src/display/display.cpp        2008-07-17 16:07:39 UTC (rev 
3854)
+++ trunk/pingus/src/display/display.cpp        2008-07-17 16:09:07 UTC (rev 
3855)
@@ -24,6 +24,7 @@
 #include "../math.hpp"
 #include "../screen/screen_manager.hpp"
 #include "sdl_framebuffer.hpp"
+#include "opengl_framebuffer.hpp"
 #include "delta_framebuffer.hpp"
 #include "display.hpp"
 
@@ -76,7 +77,9 @@
 {
   if (!framebuffer.get())
     {
-      if (delta_drawing)
+      if (0)
+        framebuffer = std::auto_ptr<Framebuffer>(new OpenGLFramebuffer());
+      else if (delta_drawing)
         framebuffer = std::auto_ptr<Framebuffer>(new DeltaFramebuffer());
       else
         framebuffer = std::auto_ptr<Framebuffer>(new SDLFramebuffer());

Added: trunk/pingus/src/display/opengl_framebuffer.cpp
===================================================================
--- trunk/pingus/src/display/opengl_framebuffer.cpp     2008-07-17 16:07:39 UTC 
(rev 3854)
+++ trunk/pingus/src/display/opengl_framebuffer.cpp     2008-07-17 16:09:07 UTC 
(rev 3855)
@@ -0,0 +1,169 @@
+//  Pingus - A free Lemmings clone
+//  Copyright (C) 2008 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 3 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, see <http://www.gnu.org/licenses/>.
+
+#include <sstream>
+#include <stdexcept>
+#include "surface.hpp"
+#include "opengl_framebuffer_surface_impl.hpp"
+#include "opengl_framebuffer.hpp"
+
+OpenGLFramebuffer::OpenGLFramebuffer()
+{
+}
+
+FramebufferSurface
+OpenGLFramebuffer::create_surface(const Surface& surface)
+{
+  return FramebufferSurface(new 
OpenGLFramebufferSurfaceImpl(surface.get_surface()));
+}
+
+void
+OpenGLFramebuffer::set_video_mode(const Size& size, bool fullscreen)
+{
+  int flags = SDL_OPENGL;
+  
+  if (fullscreen)
+    {
+      flags |= SDL_FULLSCREEN;
+    }
+  else
+    {
+      flags |= SDL_RESIZABLE;
+    }
+
+  int bpp = 0; // auto-detect 
+  screen = SDL_SetVideoMode(size.width, size.height, bpp, flags);
+
+  if(screen == 0) 
+    {
+      std::ostringstream msg;
+      msg << "Couldn't set video mode (" << size.width << "x" << size.height
+          << "-" << bpp << "bpp): " << SDL_GetError();
+      throw std::runtime_error(msg.str());
+    }
+
+  glEnableClientState(GL_VERTEX_ARRAY);
+  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+
+  // setup opengl state and transform
+  glDisable(GL_DEPTH_TEST);
+  glDisable(GL_CULL_FACE);
+  glEnable(GL_TEXTURE_2D);
+  glEnable(GL_BLEND);
+  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+  glViewport(0, 0, size.width, size.height);
+
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+
+  glOrtho(0, size.width, size.height, 0, -1, 1);
+
+  glMatrixMode(GL_MODELVIEW);
+  glLoadIdentity();
+}
+
+void
+OpenGLFramebuffer::flip()
+{
+  SDL_GL_SwapBuffers();
+}
+  
+void
+OpenGLFramebuffer::push_cliprect(const Rect& rect)
+{
+  cliprect_stack.push_back(rect);
+}
+
+void
+OpenGLFramebuffer::pop_cliprect()
+{
+  cliprect_stack.pop_back();
+}
+
+void
+OpenGLFramebuffer::draw_surface(const FramebufferSurface& src, const Vector2i& 
pos)
+{
+  draw_surface(src, Rect(Vector2i(0, 0), src.get_size()),  pos);
+}
+
+void
+OpenGLFramebuffer::draw_surface(const FramebufferSurface& src, const Rect& 
srcrect, const Vector2i& pos)
+{
+  const OpenGLFramebufferSurfaceImpl* texture = 
static_cast<OpenGLFramebufferSurfaceImpl*>(src.get_impl());
+  
+  glBindTexture(GL_TEXTURE_2D, texture->get_handle());
+  
+  float vertices[] = {
+    pos.x,                     pos.y,
+    pos.x+srcrect.get_width(), pos.y,
+    pos.x+srcrect.get_width(), pos.y+srcrect.get_height(),
+    pos.x,                     pos.y+srcrect.get_height(),
+  };
+  glVertexPointer(2, GL_FLOAT, 0, vertices);
+  
+  float uvs[] = {
+    float(srcrect.left)/texture->get_texture_size().width,  
float(srcrect.top)/texture->get_texture_size().height,
+    float(srcrect.right)/texture->get_texture_size().width, 
float(srcrect.top)/texture->get_texture_size().height,
+    float(srcrect.right)/texture->get_texture_size().width, 
float(srcrect.bottom)/texture->get_texture_size().height,
+    float(srcrect.left)/texture->get_texture_size().width,  
float(srcrect.bottom)/texture->get_texture_size().height
+  };
+  glTexCoordPointer(2, GL_FLOAT, 0, uvs);
+
+  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+  glBindTexture(GL_TEXTURE_2D, 0);
+}
+
+void
+OpenGLFramebuffer::draw_line(const Vector2i& pos1, const Vector2i& pos2, const 
Color& color)
+{
+}
+
+void
+OpenGLFramebuffer::draw_rect(const Rect& rect, const Color& color)
+{
+}
+
+void
+OpenGLFramebuffer::fill_rect(const Rect& rect, const Color& color)
+{
+  glDisable(GL_TEXTURE_2D);
+  glColor4i(color.r, color.g, color.b, color.a);
+  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+
+  float vertices[] = {
+    rect.left,                  rect.top,
+    rect.left+rect.get_width(), rect.top,
+    rect.left+rect.get_width(), rect.top+rect.get_height(),
+    rect.left,                  rect.top+rect.get_height()
+  };
+  glVertexPointer(2, GL_FLOAT, 0, vertices);
+
+  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+  glEnable(GL_TEXTURE_2D);
+  glColor4f(1, 1, 1, 1);
+}
+
+Size
+OpenGLFramebuffer::get_size() const
+{
+  return Size(screen->w, screen->h);
+}
+
+/* EOF */


Property changes on: trunk/pingus/src/display/opengl_framebuffer.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/pingus/src/display/opengl_framebuffer.hpp
===================================================================
--- trunk/pingus/src/display/opengl_framebuffer.hpp     2008-07-17 16:07:39 UTC 
(rev 3854)
+++ trunk/pingus/src/display/opengl_framebuffer.hpp     2008-07-17 16:09:07 UTC 
(rev 3855)
@@ -0,0 +1,52 @@
+//  Pingus - A free Lemmings clone
+//  Copyright (C) 2008 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 3 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, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_OPENGL_FRAMEBUFFER_HPP
+#define HEADER_OPENGL_FRAMEBUFFER_HPP
+
+#include "framebuffer.hpp"
+
+class OpenGLFramebuffer : public Framebuffer
+{
+private:
+  SDL_Surface* screen;
+  std::vector<Rect> cliprect_stack;
+
+public:
+  OpenGLFramebuffer();
+
+  FramebufferSurface create_surface(const Surface& surface);
+
+  void set_video_mode(const Size& size, bool fullscreen);
+  void flip();
+  
+  void push_cliprect(const Rect&);
+  void pop_cliprect();
+
+   void draw_surface(const FramebufferSurface& src, const Vector2i& pos);
+   void draw_surface(const FramebufferSurface& src, const Rect& srcrect, const 
Vector2i& pos);
+
+  void draw_line(const Vector2i& pos1, const Vector2i& pos2, const Color& 
color);
+
+   void draw_rect(const Rect& rect, const Color& color);
+   void fill_rect(const Rect& rect, const Color& color);
+
+  Size get_size() const;
+};
+
+#endif
+
+/* EOF */


Property changes on: trunk/pingus/src/display/opengl_framebuffer.hpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/pingus/src/display/opengl_framebuffer_surface_impl.cpp
===================================================================
--- trunk/pingus/src/display/opengl_framebuffer_surface_impl.cpp        
2008-07-17 16:07:39 UTC (rev 3854)
+++ trunk/pingus/src/display/opengl_framebuffer_surface_impl.cpp        
2008-07-17 16:09:07 UTC (rev 3855)
@@ -0,0 +1,79 @@
+//  Pingus - A free Lemmings clone
+//  Copyright (C) 2008 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 3 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, see <http://www.gnu.org/licenses/>.
+
+#include "opengl_framebuffer_surface_impl.hpp"
+
+namespace {
+
+inline int next_power_of_two(int val)
+{
+  int result = 1;
+  while(result < val)
+    result *= 2;
+  return result;
+}
+
+} // namespace 
+
+OpenGLFramebufferSurfaceImpl::OpenGLFramebufferSurfaceImpl(SDL_Surface* src)
+{
+  glGenTextures(1, &handle);
+
+  texture_size.width  = next_power_of_two(src->w);
+  texture_size.height = next_power_of_two(src->h);
+
+  //  Convert the src surface to a format usable for upload to OpenGL
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+  SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
+                                              texture_size.width, 
texture_size.height, 32,
+                                              0xff000000, 0x00ff0000, 
0x0000ff00, 0x000000ff);
+#else
+  SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
+                                              texture_size.width, 
texture_size.height, 32,
+                                              0x000000ff, 0x0000ff00, 
0x00ff0000, 0xff000000);
+#endif
+  SDL_SetAlpha(src, 0, 0);
+  SDL_BlitSurface(src, 0, convert, 0);
+  
+  GLenum sdl_format;
+  if(convert->format->BytesPerPixel == 3)
+    sdl_format = GL_RGB;
+  else if(convert->format->BytesPerPixel == 4)
+    sdl_format = GL_RGBA;
+  else
+    assert(!"OpenGLFramebufferSurfaceImpl: Unsupported surface format");
+
+  glBindTexture(GL_TEXTURE_2D, handle);
+  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+
+  glPixelStorei(GL_UNPACK_ROW_LENGTH, 
convert->pitch/convert->format->BytesPerPixel);
+
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+  // Upload the surface to a texture
+  SDL_LockSurface(convert);
+  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_size.width, 
texture_size.height, 0
+               , sdl_format, GL_UNSIGNED_BYTE, convert->pixels);
+  SDL_UnlockSurface(convert);
+   
+  SDL_FreeSurface(convert);  
+
+  // Unbind the texture
+  glBindTexture(GL_TEXTURE_2D, 0);
+}
+
+/* EOF */


Property changes on: 
trunk/pingus/src/display/opengl_framebuffer_surface_impl.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/pingus/src/display/opengl_framebuffer_surface_impl.hpp
===================================================================
--- trunk/pingus/src/display/opengl_framebuffer_surface_impl.hpp        
2008-07-17 16:07:39 UTC (rev 3854)
+++ trunk/pingus/src/display/opengl_framebuffer_surface_impl.hpp        
2008-07-17 16:09:07 UTC (rev 3855)
@@ -0,0 +1,51 @@
+//  Pingus - A free Lemmings clone
+//  Copyright (C) 2008 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 3 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, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_OPENGL_FRAMEBUFFER_SURFACE_IMPL_HPP
+#define HEADER_OPENGL_FRAMEBUFFER_SURFACE_IMPL_HPP
+
+#ifndef MACOSX
+#include <GL/gl.h>
+#include <GL/glext.h>
+#else
+#include <OpenGL/gl.h>
+#include <OpenGL/glext.h>
+#endif
+
+#include "SDL.h"
+#include "framebuffer_surface.hpp"
+
+class OpenGLFramebufferSurfaceImpl : public FramebufferSurfaceImpl
+{
+private:
+  GLuint handle;
+  Size   size;
+  Size   texture_size;
+
+public:
+  OpenGLFramebufferSurfaceImpl(SDL_Surface* src);
+
+  int get_width()  const { return size.width;  }
+  int get_height() const { return size.height; }
+
+  GLuint get_handle() const { return handle; }
+  Size get_texture_size() const { return texture_size; }
+  Size get_size() const { return size; }
+};
+
+#endif
+
+/* EOF */


Property changes on: 
trunk/pingus/src/display/opengl_framebuffer_surface_impl.hpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native





reply via email to

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