pingus-cvs
[Top][All Lists]
Advanced

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

[Pingus-CVS] CVS: Games/Pingus/src blitter.cxx,NONE,1.1 blitter.hxx,NONE


From: grumbel
Subject: [Pingus-CVS] CVS: Games/Pingus/src blitter.cxx,NONE,1.1 blitter.hxx,NONE,1.1 bmp_map.cxx,NONE,1.1 bmp_map.hxx,NONE,1.1 button_panel.cxx,NONE,1.1 button_panel.hxx,NONE,1.1
Date: 12 Jun 2002 19:04:47 -0000

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

Added Files:
        blitter.cxx blitter.hxx bmp_map.cxx bmp_map.hxx 
        button_panel.cxx button_panel.hxx 
Log Message:
The big rename...

--- NEW FILE: blitter.cxx ---
//  $Id: blitter.cxx,v 1.1 2002/06/12 19:04:45 grumbel Exp $
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 1999 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 <algorithm>
#include <iostream>
#include <cstdio>
#include <assert.h>
#include <ClanLib/Display/Display/surface.h>
#include <ClanLib/Display/Display/surfaceprovider.h>
#include <ClanLib/Display/SurfaceProviders/canvas.h>
#include <ClanLib/Display/Display/palette.h>
#include "pingus_error.hxx"
#include "color.hxx"
#include "string_converter.hxx"
#include "globals.hxx"
#include "blitter.hxx"

/* Headers needed for i18n / gettext */
#include <clocale>
#include <config.h>
#include "my_gettext.hxx"

using namespace std;

void
Blitter::put_surface(CL_Canvas* canvas, const CL_Surface& sur,
                     int x, int y)
{
  //Blitter::put_surface(canvas, sur->get_provider(), x, y);
  /*  if (sur->get_provider()->get_depth() != 8)
      sur->put_target(x, y, 0, canvas);
  else*/
  assert (sur);
  assert (canvas);
  Blitter::put_surface(canvas, sur.get_provider(), x, y);
}

void 
Blitter::put_surface(CL_Canvas* canvas, CL_SurfaceProvider* provider,
                     int x, int y)
{
  assert (provider);
  assert (canvas);
  switch(provider->get_depth())
    {
    case  8:
      put_surface_8bit(canvas, provider, x, y);
      break;
    case 32:
      put_surface_32bit(canvas, provider, x, y);
      break;
    default: 
      throw PingusError("Blitter:put_surface:Unknown color depth: " + 
to_string(provider->get_depth()));
      break;
    }    
}

void
Blitter::put_surface_8bit(CL_Canvas* provider, CL_SurfaceProvider* sprovider,
                          int x, int y)
{
  assert (provider);
  assert (sprovider);
  int start_i;
  unsigned char* tbuffer; // Target buffer
  int twidth, theight, tpitch;
  
  unsigned char* sbuffer; // Source buffer
  int swidth, sheight, spitch;

  int x_offset, y_offset;

  provider->lock();
  sprovider->lock();

  tbuffer = static_cast<unsigned char*>(provider->get_data());
  sbuffer = static_cast<unsigned char*>(sprovider->get_data());

  //std::cout << "Colorkey: " << sprovider->get_src_colorkey() << std::endl;

  CL_Palette* cl_palette = sprovider->get_palette();

  if (!cl_palette)
    {
      char str[1024];
      sprintf(str, _("Couldn't find palette: %d"), sprovider->get_depth());
      throw PingusError(str);
    }

  twidth = provider->get_width();
  theight = provider->get_height();
  tpitch = provider->get_pitch();

  swidth = sprovider->get_width();
  sheight = sprovider->get_height();
  spitch = sprovider->get_pitch();

  if (y < 0)
    y_offset = 0-y;
  else 
    y_offset = 0;
  
  if (x < 0) 
    x_offset = -x;
  else
    x_offset = 0;

  unsigned char* palette = cl_palette->palette;
 
  if (sprovider->uses_src_colorkey ())
    {
      unsigned int colorkey = sprovider->get_src_colorkey();
      
      for(int line=y_offset;
          line < sheight && (line + y) < theight; 
          ++line) 
        {
          start_i = ((line + y) * tpitch) + (x*4);
      
          for(int i=start_i+(4*x_offset),j=line*spitch+x_offset; 
              (i < start_i + (4*swidth))
                && ((i-start_i+(x*4)) < (4*twidth));
              i += 4, ++j) 
            {
              if (sbuffer[j] != colorkey) 
                {
                  tbuffer[i + 0] = 255;                                  // 
alpha
                  tbuffer[i + 1] = palette[sbuffer[j] * 3 + 2]; // blue
                  tbuffer[i + 2] = palette[sbuffer[j] * 3 + 1]; // green
                  tbuffer[i + 3] = palette[sbuffer[j] * 3 + 0]; // red
                }
            }
        }
    }
  else
    {
      for(int line=y_offset;
          line < sheight && (line + y) < theight; 
          ++line) 
        {
          start_i = ((line + y) * tpitch) + (x*4);
      
          for(int i=start_i+(4*x_offset),j=line*spitch+x_offset; 
              (i < start_i + (4*swidth))
                && ((i-start_i+(x*4)) < (4*twidth));
              i += 4, ++j) 
            {
              tbuffer[i + 0] = 255;                                  // alpha
              tbuffer[i + 1] = palette[sbuffer[j] * 3 + 2]; // blue
              tbuffer[i + 2] = palette[sbuffer[j] * 3 + 1]; // green
              tbuffer[i + 3] = palette[sbuffer[j] * 3 + 0]; // red
            }
        }
    }

  // FIXME: Memory hole
  //sprovider->unlock();
  //provider->unlock();  
}

void
Blitter::put_surface_32bit(CL_Canvas* canvas, CL_SurfaceProvider* provider,
                           const int x_pos, const int y_pos)
{
  assert (canvas);
  assert (provider);

  if (pingus_debug_flags & PINGUS_DEBUG_BLITTER)
    {
      std::cout << "Blitting: SurfaceProvider:" << provider->get_width () << 
"x" << provider->get_height ()
                << ":" << provider 
                << " Canvas:" << canvas->get_width () << "x" 
                << canvas->get_height () << ":" << canvas << std::endl;
    }

  //std::cout << "Blitter::put_surface_32bit() --- not implemented" << 
std::endl;
  //return;
  float red, green, blue, alpha;
  float tred, tgreen, tblue, talpha;

  int swidth = provider->get_width();
  int sheight = provider->get_height();

  int twidth = canvas->get_width();
  int theight = canvas->get_height();

  // Surface is out of the screen
  if (x_pos > twidth-1 || y_pos > theight-1)
    return;

  canvas->lock();
  provider->lock();
  if (1) // slow
    {
      for(int y = max(0, -y_pos); y < sheight && (y + y_pos) < theight ; y++) 
        for(int x = max(0,-x_pos); x < swidth && (x + x_pos) < twidth; x++) 
          {
            provider->get_pixel(x, y, &red, &green, &blue, &alpha);
            canvas->get_pixel(x + x_pos, y + y_pos, &tred, &tgreen, &tblue, 
&talpha);
          
            // FIXME: This doesn't give correct alpha values
            canvas->draw_pixel(x + x_pos, y + y_pos, 
                               max(0.0, min(1.0, (red * alpha) + (tred * 
(1.0-alpha)))),
                               max(0.0, min(1.0, (green * alpha) +(tgreen * 
(1.0-alpha)))),
                               max(0.0, min(1.0, (blue * alpha)  + (tblue * 
(1.0-alpha)))),
                               max(0.0, min(1.0, alpha * alpha + 
(talpha*(1.0-alpha)))));
          }
    }
  else // fast?!
    {
      
    }

  // FIXME: Memory hole
  //provider->unlock();
  //canvas->unlock();
}

void
Blitter::put_alpha_surface(CL_Canvas* provider, CL_SurfaceProvider* sprovider,
                           int x, int y)
{
  assert (provider);
  assert (sprovider);
  int start_i;
  unsigned char* tbuffer; // Target buffer
  int twidth, theight, tpitch;
  
  unsigned char* sbuffer; // Source buffer
  int swidth, sheight, spitch;

  CL_Palette* palette;
  int x_offset, y_offset;

  provider->lock();
  sprovider->lock();

  //  assert(sprovider->get_depth() == 8);
  if (sprovider->get_depth() != 8)
    {
      // FIXME: Memory hole
      //sprovider->unlock ();
      //provider->unlock ();
      throw PingusError("Image has wrong color depth: " + 
to_string(sprovider->get_depth()));
    }
  //  assert(provider->get_pixel_format() == RGBA8888);

  tbuffer = static_cast<unsigned char*>(provider->get_data());
  sbuffer = static_cast<unsigned char*>(sprovider->get_data());
  
  palette = sprovider->get_palette();
  assert(palette);

  twidth = provider->get_width();
  theight = provider->get_height();
  tpitch = provider->get_pitch();

  swidth = sprovider->get_width();
  sheight = sprovider->get_height();
  spitch = sprovider->get_pitch();

  if (y < 0)
    y_offset = 0-y;
  else 
    y_offset = 0;
  
  if (x < 0) 
    x_offset = -x;
  else
    x_offset = 0;

  for(int line=y_offset; line < sheight && (line + y) < theight; ++line) {
    start_i = ((line + y) * tpitch) + (x*4);

    for(int i=start_i+(4*x_offset),j=line*spitch+x_offset; 
        i < start_i + (4*swidth) && (i-start_i+(x*4)) < (4*twidth); i+=4,++j) {
      if (sbuffer[j]) {
        tbuffer[i + 0] = 0;                                  // alpha
      }
    }
  }

  // FIXME: Memory hole
  //sprovider->unlock();
  //provider->unlock();  
}

CL_Canvas*
Blitter::clear_canvas(CL_Canvas* canvas)
{
  assert (canvas);
  unsigned char* buffer;
  
  canvas->lock();
  buffer = static_cast<unsigned char*>(canvas->get_data());
  memset(buffer, 0, sizeof(unsigned char) * canvas->get_pitch() * 
canvas->get_height());
  // FIXME: Memory hole
  //canvas->unlock();

  return canvas;
}

CL_Canvas*
Blitter::create_canvas(const CL_Surface& sur)
{
  assert (sur);
  return create_canvas(sur.get_provider());
}

CL_Canvas*
Blitter::create_canvas(CL_SurfaceProvider* prov)
{
  assert (prov);
  CL_Canvas* canvas = new CL_Canvas(prov->get_width(), prov->get_height());

  switch (prov->get_bytes_per_pixel())
    {
    case 3:
      {
        canvas->lock();
        prov->lock();
        
        int buffer_size = prov->get_pitch () * prov->get_height ();
        unsigned char* sbuffer = static_cast<unsigned char*>(prov->get_data ());
        unsigned char* tbuffer = static_cast<unsigned char*>(canvas->get_data 
());
        
        for (int si = 0, ti = 0; si < buffer_size; si += 3, ti += 4)
          {
            tbuffer[ti + 0] = 255;
            tbuffer[ti + 1] = sbuffer[si + 0];
            tbuffer[ti + 2] = sbuffer[si + 1];
            tbuffer[ti + 3] = sbuffer[si + 2];
          }
          
        // -FIXME: Memory hole
        prov->unlock();
        canvas->unlock();
      }
      break;

    case 4:
      canvas->lock();
      prov->lock();
      memcpy(canvas->get_data(), prov->get_data(),
             sizeof(unsigned char) * prov->get_height() * prov->get_pitch());
      // -FIXME: Memory hole
      prov->unlock();
      canvas->unlock();
      break;

    default:
      put_surface(canvas, prov, 0, 0);
      break;
    }
  return canvas;
}

CL_Surface 
Blitter::scale_surface (const CL_Surface& sur, int width, int height)
{
  assert (sur);
  return CL_Surface(Blitter::scale_surface_to_canvas(sur, width, height), true);
}

CL_Canvas* 
Blitter::scale_surface_to_canvas (const CL_Surface& sur, int width, int height)
{
  assert (sur);

  Color color;
  CL_SurfaceProvider* provider = sur.get_provider ();
  CL_Canvas* canvas = new CL_Canvas (width, height);

  provider->lock ();
  canvas->lock ();

  unsigned char* sbuffer = static_cast<unsigned char*>(provider->get_data ());
  unsigned char* tbuffer = static_cast<unsigned char*>(canvas->get_data ());
  int pwidth = provider->get_width ();
  int pheight = provider->get_height ();
  
  switch (provider->get_bytes_per_pixel ())
    {
    case 3:
      {
        // We assume that we have the data in RGB888, which might not be
        // the case
        for (int y = 0; y < height; y++)
          for (int x = 0; x < width; x++)
            {
              int ti = (y * width + x) * 4;
              int si = ((y * pheight / height) * pwidth
                        + (x * pwidth / width)) * 3;
                
              tbuffer[ti + 0] = 255; // alpha
              tbuffer[ti + 1] = sbuffer[(si + 0)]; // blue
              tbuffer[ti + 2] = sbuffer[(si + 1)]; // green
              tbuffer[ti + 3] = sbuffer[(si + 2)]; // red
            }
      }
      break;
    case 4:
      {
        // We assume that we have the data in RGBA8888, which might not be
        // the case
        for (int y = 0; y < height; y++)
          for (int x = 0; x < width; x++)
            {
              int ti = (y * width + x) * 4;
              int si = ((y * pheight / height) * pwidth
                        + (x * pwidth / width)) * 4;
                
              tbuffer[ti + 0] = sbuffer[(si + 0)]; // alpha
              tbuffer[ti + 1] = sbuffer[(si + 1)]; // blue
              tbuffer[ti + 2] = sbuffer[(si + 2)]; // green
              tbuffer[ti + 3] = sbuffer[(si + 3)]; // red
            }
      }
      break;
    default:
      // Slow but generic, using get_data () would be better, but would
      // require quite a bit of work
      for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
          {
            // std::cout << "X: " << x << " Y: " << y << std::endl;
            provider->get_pixel (x * provider->get_width () / width ,
                                 y * provider->get_height () / height,
                                 &color.red, &color.green, &color.blue, 
&color.alpha);
            // ignoring the source alpha due to get_pixel brokeness... no time 
to test the patch
            canvas->draw_pixel (x, y, color.red, color.green, color.blue, 
color.alpha);
          }
      break;
    }

  // FIXME: Memory hole
  //canvas->unlock ();
  //provider->unlock ();

  return canvas;
}

/*
  // Converts a SurfaceProvider based surface, to a Canvas
  // based one. The old one will not be deleted.
  CL_Surface
  Blitter::convert_to_emptyprovider(CL_Surface ssurf)
  {
  CL_Canvas* tprov = convert_to_emptyprovider(ssurf->get_provider());
  return CL_Surface::create(tprov, true);
  }

  // Converts a SurfaceProvider, to an Canvas and returns
  // the newly allocated provider, you need to delete it yourself.
  CL_Canvas*
  Blitter::convert_to_emptyprovider(CL_SurfaceProvider* sprov)
  {
  CL_Canvas* tprov;
  CL_Palette* palette;
  unsigned char* sbuffer;
  unsigned char* tbuffer;
  int i;

  sprov->lock();
  switch(sprov->get_depth()) 
  {
  case 32:
  tprov = new CL_Canvas(sprov->get_width(),
  sprov->get_height());
  tprov->lock();

  sbuffer = static_cast<unsigned char*>(sprov->get_data());
  tbuffer = static_cast<unsigned char*>(tprov->get_data());

  for(i=0; i < (tprov->get_height() * tprov->get_pitch()); ++i)
  {
  tbuffer[i] = sbuffer[i];
  }

  tprov->unlock();
  break;
  case 8:
  tprov = new CL_Canvas(sprov->get_width(),
  sprov->get_height());
  palette = sprov->get_palette();
  tprov->lock();
      
  sbuffer = static_cast<unsigned char*>(sprov->get_data());
  tbuffer = static_cast<unsigned char*>(tprov->get_data());    

  for(i=0; i < (sprov->get_height() * sprov->get_pitch()); ++i)
  {
  tbuffer[i * 4 + 0] = 255;
  tbuffer[i * 4 + 1] = palette->palette[sbuffer[i] * 3 + 2];
  tbuffer[i * 4 + 2] = palette->palette[sbuffer[i] * 3 + 1];
  tbuffer[i * 4 + 3] = palette->palette[sbuffer[i] * 3 + 0];
  }
      
  tprov->unlock();      
  break;
  default:
  std::cout << "convert_to_emptyprovider(): Wrong source format: " 
  << static_cast<int>(sprov->get_depth()) << std::endl;
  assert(false);
  break;
  }
  sprov->unlock();
  
  return tprov;
  }
*/ 

/* EOF */

--- NEW FILE: blitter.hxx ---
//  $Id: blitter.hxx,v 1.1 2002/06/12 19:04:45 grumbel Exp $
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 1999 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 BLITTER_HH
#define BLITTER_HH

class CL_Canvas;
class CL_Surface;
class CL_SurfaceProvider;

/** A bunch of blitting and creation functions to operate on
    CL_Canvas.  Some of them a similar to the ones in ClanLib, but
    this are slower and work. */
class Blitter
{
private:
  ///
  static void put_surface_8bit(CL_Canvas*, CL_SurfaceProvider*,
                               int x, int y);
  ///
  static void put_surface_32bit(CL_Canvas*, CL_SurfaceProvider*,
                               int x, int y);
public:
  /*void put_surface(CL_LockableSurface* provider, CL_Surface surface,
    int x, int y);
    void put_surface(CL_LockableSurface* provider, CL_SurfaceProvider* surface,
    int x, int y);
  */

  /// Puts a given surface to a given canvas at position x, y.
  static void put_surface(CL_Canvas*, const CL_Surface&,
                          int x, int y);

  /// Puts a given surface provider to a given canvas at position x, y.
  static void put_surface(CL_Canvas*, CL_SurfaceProvider*,
                          int x, int y);

  /** Makes all pixels in canvas tranparent, when their indexed value
      in provider is larger than zero.*/
  static void put_alpha_surface(CL_Canvas* canvas, CL_SurfaceProvider* provider,
                                int x, int y);

  /** Returns a newly allocated canvas. The canvas contains the same
      image as the given surface. */
  static CL_Canvas* create_canvas(const CL_Surface&);

  /** Returns a newly allocated canvas. The canvas contains the same
      image as the given surface provider */
  static CL_Canvas* create_canvas(CL_SurfaceProvider*);

  /** Sets all pixels of a canvas to zero */
  static CL_Canvas* clear_canvas(CL_Canvas*);
  
  /** Creates a new surface (based on a canvas) with the given width
      and height and stretches the source surface onto it

      @param sur The source surface
      @param width The new width of the surface. 
      @param height The new height of the surface. 
      @return A newly created surface, the caller is responsible to delete it. 
*/
  static CL_Surface scale_surface (const CL_Surface& sur, int width, int 
height);

  /** Creates a new canvas with the given width and height and
      stretches the source surface onto it

      @param sur The source surface
      @param width The new width of the surface. 
      @param height The new height of the surface. 
      @return A newly created surface, the caller is responsible to delete it. 
*/
  static CL_Canvas* Blitter::scale_surface_to_canvas (const CL_Surface& sur, 
int width, int height);
};

#endif 

/* EOF */


--- NEW FILE: bmp_map.cxx ---
//  $Id: bmp_map.cxx,v 1.1 2002/06/12 19:04:45 grumbel Exp $
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 1999 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 <ClanLib/Display/Display/surfaceprovider.h>
#include "bmp_map.hxx"
#include "globals.hxx"
#include "pingus_error.hxx"
#include "pingus_resource.hxx"
#include "col_map.hxx"

/* Headers needed for i18n / gettext */
#include <clocale>
#include <config.h>
#include "my_gettext.hxx"


PinguBmpMap::PinguBmpMap()
{
  colmap  = 0;
}

PinguBmpMap::PinguBmpMap(ResDescriptor res_desc)
{
  colmap = 0;
  if (verbose > 1)
    std::cout << "Creating BMP Map" << std::endl;

  surface = PingusResource::load_surface(res_desc);
  
  if (!surface) {
    throw PingusError(res_desc.res_name + _(": Could not open file\n"));;
  }
}

PinguBmpMap::~PinguBmpMap()
{
  /* BUG: Segfault
  if (surface)
    delete surface;
  */
}

int
PinguBmpMap::get_width()
{
  return surface.get_width();
}

int
PinguBmpMap::get_height()
{
  return surface.get_height();  
}

void
PinguBmpMap::draw_offset(int x, int y, float s)
{
  if (s == 1.0)
    surface.put_screen(x,y);      
  else
    surface.put_screen((int)(x * s), (int)(y * s),s , s);
}

ColMap*
PinguBmpMap::get_colmap()
{
  unsigned char* buffer;
  void* vbuffer;
  CL_SurfaceProvider* provider;

  std::cout << "PinguBmpMap::get_colmap" << std::endl;

  assert(surface);

  if (colmap) {
    return colmap;
  } else {
    provider = surface.get_provider();
    assert(provider);
    provider->lock();

    vbuffer = provider->get_data();
    assert(vbuffer);
    if (provider->get_depth() != 8)
      throw PingusError(_("PingusBmpMap::get_colmap: Surface has wrong pixel 
format, need 8bpp!"));
    
    buffer = new unsigned char [provider->get_pitch() * provider->get_height()];
    memcpy(buffer, vbuffer, provider->get_pitch() *  provider->get_height());

    colmap = new ColMap();
    colmap->load(buffer, get_width(), get_height());

    provider->unlock();
    return colmap;
  }

  //  return 0;
}

CL_Surface
PinguBmpMap::get_surface()
{
  return CL_Surface ();
}

/* EOF */

--- NEW FILE: bmp_map.hxx ---
//  $Id: bmp_map.hxx,v 1.1 2002/06/12 19:04:45 grumbel Exp $
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 1999 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 PINGUBMPMAP_HH
#define PINGUBMPMAP_HH

#include <ClanLib/Display/Display/surface.h>

#include "pingu_map.hxx"

class ColMap;
class ResDescriptor;

///
class PinguBmpMap : public PinguMap 
{
private:
  ///
  CL_Surface surface;
  ///
  ColMap*     colmap;
public:
  ///
  PinguBmpMap();
  ///
  PinguBmpMap(ResDescriptor fg);
  ///
  virtual ~PinguBmpMap();

  ///
  void draw_offset(int x, int y, float s=1.0);
  ///
  int  get_width(void);
  ///
  int  get_height(void);
  ///
  ColMap* get_colmap(void);
  ///
  CL_Surface get_surface(void);
};

#endif

/* EOF */


--- NEW FILE: button_panel.cxx ---
//  $Id: button_panel.cxx,v 1.1 2002/06/12 19:04:45 grumbel Exp $
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 1999 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 <ClanLib/Core/System/system.h>
#include <ClanLib/Display/Input/mouse.h>
#include <ClanLib/Display/Display/display.h>
#include "pingus_resource.hxx"
#include "globals.hxx"
#include "pingu.hxx"
#include "button_panel.hxx"
#include "controller.hxx"
#include "client.hxx"
#include "server.hxx"
#include "world.hxx"
#include "plf.hxx"

CL_Surface ButtonPanel::button_cap;

ButtonPanel::ButtonPanel(PLF* plf, Controller* arg_controller,
                                         int arg_x_pos, int arg_y_pos)
  : last_press(0),
    controller (arg_controller),
    x_pos (arg_x_pos), 
    y_pos (arg_y_pos)
{

  std::vector<ActionData> buttons_data = plf->get_actions();

  y_pos -= (buttons_data.size() * 38)/2 + 70;

  if (buttons_data.size() == 0)
    {
      std::cout << "ButtonPanel: No actions given in this level, fall back to 
default" << std::endl;
      buttons_data.push_back(ActionData("bridger", 20));
      buttons_data.push_back(ActionData("basher", 20));
      buttons_data.push_back(ActionData("digger", 20));
      buttons_data.push_back(ActionData("miner", 20));
    }

  for(std::vector<ActionData>::size_type i = 0; i < buttons_data.size(); i++)
    {
      a_buttons.push_back(new VerticalActionButton (x_pos, i * 38 + y_pos,
                                                    buttons_data[i].name,
                                                    controller->get_owner ()));
    }

  armageddon = new ArmageddonButton(CL_Display::get_width() - 40,     
CL_Display::get_height() - 62);
  forward    = new ForwardButton   (CL_Display::get_width() - 40 * 2, 
CL_Display::get_height() - 62);
  pause      = new PauseButton     (CL_Display::get_width() - 40 * 3, 
CL_Display::get_height() - 62);

  forward->pressed = false;
  pause->pressed   = false;
  armageddon_pressed = false;
  
  left_pressed = 0;
  
  pressed_button = a_buttons.begin();
}

ButtonPanel::~ButtonPanel()
{
  for (AButtonIter it = a_buttons.begin(); it != a_buttons.end(); ++it) 
  {
    delete *it;
  }
  
  delete armageddon;
  delete forward;
  delete pause;
}

void
ButtonPanel::update(float delta)
{
  (*pressed_button)->update(delta);

  if (last_press + 350 < CL_System::get_time()) 
    {
      armageddon_pressed = 0;
    } 
}

std::string
ButtonPanel::get_action_name()
{
  return (*pressed_button)->get_action_name();
}

void 
ButtonPanel::draw() 
{
  float alpha;

  if (fast_mode)
    alpha = 1.0;
  else 
    alpha = 0.5;
  
  // draw the buttons
  for(AButtonIter button = a_buttons.begin(); button != a_buttons.end(); 
++button) 
    {
      if (button == pressed_button) 
        (*button)->pressed = true;
      else
        (*button)->pressed = false;

      (*button)->draw();
    }

  armageddon->draw();
  pause->draw();
  forward->draw();
}

void
ButtonPanel::set_server(Server* s)
{
  server = s;
  world = server->get_world();

  for(AButtonIter button = a_buttons.begin(); button != a_buttons.end(); 
++button) 
    {
      (*button)->set_action_holder(server->get_action_holder());
    }

  pause->server = server;
  armageddon->server = server;
  forward->server = server;
}

void
ButtonPanel::set_client(Client* c)
{
  client = c;
}

void
ButtonPanel::set_button(int i)
{
  if ((unsigned int)(i) < a_buttons.size())
    {
      pressed_button = a_buttons.begin() + i;
    }
}

void
ButtonPanel::on_button_press(const CL_Key &key)
{
  if (key.id == CL_MOUSE_LEFTBUTTON)
    {
      for(AButtonIter button = a_buttons.begin(); button != a_buttons.end(); 
button++)
        {
          if ((*button)->mouse_over(controller->get_pos ()))
            pressed_button = button;
        }
  
      if (armageddon->mouse_over(CL_Vector (key.x, key.y)))
        {
          last_press = CL_System::get_time();
      
          if (verbose) std::cout << "Armageddon: " << armageddon_pressed << 
std::endl;
          armageddon_pressed++;
           
          if (armageddon_pressed == 2)
            {
              arma_counter = 0;
              armageddon_pressed = 4;
              armageddon->pressed = true;
              world->armageddon();
            }
          return;
        }
    
      if (pause->mouse_over(controller->get_pos ()))
        {
          client->set_pause(!client->get_pause());
          return;
        }
      else if (forward->mouse_over(controller->get_pos ()))
        {
          client->set_fast_forward(!client->get_fast_forward());
          return;
        }
    }
}

void
ButtonPanel::on_button_release(const CL_Key & /*key*/)
{

  //forward->pressed = false;
  //pause->pressed = false;
  
}

/// Select the next action
void 
ButtonPanel::next_action ()
{
  ++pressed_button;
  if (pressed_button == a_buttons.end ())
    pressed_button = a_buttons.begin ();
}

/// Select the previous action
void 
ButtonPanel::previous_action ()
{
  if (pressed_button == a_buttons.begin ())
    pressed_button = a_buttons.end ();

  --pressed_button;
}

/* EOF */

--- NEW FILE: button_panel.hxx ---
//  $Id: button_panel.hxx,v 1.1 2002/06/12 19:04:45 grumbel Exp $
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 1999 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 BUTTON_PANEL_HH
#define BUTTON_PANEL_HH

#include <vector>

#include "gui_obj.hxx"
#include "action_button.hxx"

class CL_Key;
class Client;
class PLF;
class World;
class Controller;

class ButtonPanel : public GuiObj
{
private:
  friend class ClientEvent;

  std::vector<ActionButton*> a_buttons;
  typedef std::vector<ActionButton*>::iterator AButtonIter;
  AButtonIter  pressed_button;
  ArmageddonButton* armageddon;
  ForwardButton*    forward;
  PauseButton*      pause;

  Server* server;
  Client* client;

  int  armageddon_pressed;
  AnimCounter arma_counter;

  bool left_pressed;
  unsigned int  last_press;
  static CL_Surface button_cap;
  World* world;

  Controller* controller;

  int x_pos, y_pos;
public:
  ButtonPanel(PLF* plf, Controller*, int arg_x_pos, int arg_y_pos);
  ~ButtonPanel();

  void on_button_press(const CL_Key& key);
  void on_button_release(const CL_Key& key);

  std::string get_action_name();
  void   update(float delta);
  void   draw();
  void   set_server(Server*);
  void   set_client(Client*);
  void   set_button(int);

  /// Select the next action
  void next_action ();

  /// Select the previous action
  void previous_action ();
};

#endif

/* EOF */




reply via email to

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