]> Dogcows Code - chaz/yoink/commitdiff
converted image management to resource handles
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Mon, 21 Jun 2010 21:47:41 +0000 (15:47 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Mon, 21 Jun 2010 21:47:41 +0000 (15:47 -0600)
25 files changed:
Makefile
configure
src/Character.cc
src/Character.hh
src/GameLayer.cc
src/Hud.cc
src/Hud.hh
src/Main.cc
src/Scene.cc
src/TilemapFont.cc
src/TilemapFont.hh
src/moof/aabb.hh
src/moof/image.cc
src/moof/image.hh
src/moof/line.hh
src/moof/ray.hh
src/moof/resource.cc
src/moof/resource.hh
src/moof/sound.cc
src/moof/sound.hh
src/moof/sprite.cc [new file with mode: 0644]
src/moof/sprite.hh [new file with mode: 0644]
src/moof/texture.cc [deleted file]
src/moof/texture.hh [deleted file]
src/yoink.rc

index 95ed652182519087c4f7a07274c73df624013ff5..b5a149f4df2c706fc60ea14e6dbbae72b5823987 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -80,7 +80,7 @@ COMMAND_LDX        = $(CXX) $(LDFLAGS) $(LF_TGT) -o $@ $^ $(LL_TGT) $(LIBS)
 COMMAND_CCLD       = $(COMPILE) $(CC)  $(CFLAGS)    $(CF_TGT) $(LDFLAGS) $(LF_TGT) -o $@ $< $(LL_TGT) $(LIBS)
 COMMAND_CXXLD      = $(COMPILE) $(CXX) $(CXXFLAGS)  $(CF_TGT) $(LDFLAGS) $(LF_TGT) -o $@ $< $(LL_TGT) $(LIBS)
 COMMAND_AR         = $(AR) rcs $@ $^;  $(RANLIB)    $@
-COMMAND_RC         = $(WINDRES)        $(DDEFINES)  $(DF_TGT) -o $@ -i $<
+COMMAND_RC         = $(WINDRES)        -I.          $(DF_TGT) -o $@ -i $<
 COMMAND_INSTALL    = ./tools/install.sh -m $1 $2 -d $3
 COMMAND_RM         = rm -f $1
 COMMAND_IN         = sed -f config.sed <"$1" >"$2"
@@ -101,15 +101,15 @@ SHELL_LINE_PREFIX  =
 else
 DO_CC              = @echo " CC     $@"; $(COMMAND_CC)
 DO_CXX             = @echo " CXX    $@"; $(COMMAND_CXX)
-DO_LD              = @echo " LD     $@"; $(COMMAND_LD)
-DO_LDX             = @echo " LD     $@"; $(COMMAND_LDX)
-DO_CCLD            = @echo " CCLD   $@"; $(COMMAND_CCLD)
-DO_CXXLD           = @echo " CXXLD  $@"; $(COMMAND_CXXLD)
+DO_LD              = @echo " LINK   $@"; $(COMMAND_LD)
+DO_LDX             = @echo " LINK   $@"; $(COMMAND_LDX)
+DO_CCLD            = @echo " LINK   $@"; $(COMMAND_CCLD)
+DO_CXXLD           = @echo " LINK   $@"; $(COMMAND_CXXLD)
 DO_AR              = @echo " AR     $@"; $(COMMAND_AR)
 DO_RC              = @echo " RC     $@"; $(COMMAND_RC)
 DO_INSTALL         = @echo " CP     $2"; $(COMMAND_INSTALL)
 DO_RM              = @echo " RM     $1"; $(COMMAND_RM)
-DO_IN              = @echo " MAKE   $2"; $(COMMAND_IN)
+DO_IN              = @echo " GEN    $2"; $(COMMAND_IN)
 SHELL_LINE_PREFIX  = @
 endif
 
index d4435162569e366a992f552e31ac3cdaff3added..9f97b6424af101c05f87ef3c13a75cdd932b0b01 100755 (executable)
--- a/configure
+++ b/configure
@@ -71,8 +71,8 @@ end
 -- Setup a temporary file to collect error messages.
 --
 
-error_log = "config.log"
-os.remove(error_log)
+config_log = "config.log"
+os.remove(config_log)
 
 
 --
@@ -89,10 +89,10 @@ function die(...)
        for _,value in ipairs(arg) do
                print("fatal: "..tostring(value))
        end
-       if file_exists(error_log) then
+       if file_exists(config_log) then
                print()
                print("Look through the file `config.log' for more information:\n")
-               os.execute("tail "..error_log)
+               os.execute("tail "..config_log)
        end
        os.exit(1)
 end
@@ -100,7 +100,8 @@ end
 -- Execute a command and return its output or nil if the command failed to
 -- run.
 function backtick_run(command)
-       local fd = io.popen(command.." 2>>"..error_log)
+       os.execute("echo '# "..command.."' >>"..config_log)
+       local fd = io.popen(command.." 2>>"..config_log)
        if fd then
                local stdout = fd:read("*l")
                fd:close()
@@ -112,7 +113,8 @@ end
 -- Try to execute a command and return true if the command finished
 -- successfully (with an exit code of zero).
 function try_run(command)
-       return os.execute(command.." >/dev/null 2>>"..error_log) == 0
+       os.execute("echo '# "..command.."' >>"..config_log)
+       return os.execute(command.." >/dev/null 2>>"..config_log) == 0
 end
 
 -- Remove the whitespace surrounding a string.
@@ -388,7 +390,7 @@ else
 end
 
 -- Check for CXX.
-tmpname                        = os.tmpname()..".c"
+tmpname                        = os.tmpname()..".cpp"
 tmpfile, err   = io.open(tmpname, "w")
 if tmpfile then
        tmpfile:write([[
index 01b4753f0f2287372e23b527293a050ee4fd1117..ccb2f9776dacc3216e75616235186c2a94665292 100644 (file)
@@ -98,37 +98,27 @@ void Character::update(moof::scalar t, moof::scalar dt)
 
        aabb_.init(a, b);
        sphere_.init(center, a);
+
+       int frame = animation.getFrame();
+       tilemap.tile(frame);
 }
 
 
 void Character::draw(moof::scalar alpha) const
 {
        moof::state2 state = moof::rigid_body2::state(alpha);
-       moof::vector2 position = state.position;
-
-       //glColor3f(1.0f, 1.0f, 1.0f);
-       tilemap.bind();
-
-       int frame = animation.getFrame();
-       moof::texture::orientation orientation = moof::texture::normal;
+       const moof::vector2& position = state.position;
 
-       if (state_.velocity[0] < 0.0) orientation = moof::texture::reverse;
+       const moof::scalar s = 0.5;
 
-       moof::scalar coords[8];
-       tilemap.tile_coordinates(frame, coords, orientation);
+       moof::vector3 coords[4];
 
-       moof::scalar s = 0.5;
+       coords[0] = moof::vector3(position[0] - s, position[1] - s, SCALAR(0.0));
+       coords[1] = moof::vector3(position[0] + s, position[1] - s, SCALAR(0.0));
+       coords[2] = moof::vector3(position[0] + s, position[1] + s, SCALAR(0.0));
+       coords[3] = moof::vector3(position[0] - s, position[1] + s, SCALAR(0.0));
 
-       glBegin(GL_TRIANGLE_FAN);
-               glTexCoord(coords[0], coords[1]);
-               glVertex(position[0]-s, position[1]-s);
-               glTexCoord(coords[2], coords[3]);
-               glVertex(position[0]+s, position[1]-s);
-               glTexCoord(coords[4], coords[5]);
-               glVertex(position[0]+s, position[1]+s);
-               glTexCoord(coords[6], coords[7]);
-               glVertex(position[0]-s, position[1]+s);
-       glEnd();
+       tilemap.draw(coords);
 }
 
 
index 651cc885c4f239f10a6779c94481e1060d5b335a..bdbdb29414f0a6ce436e440691bab99ae53f203a 100644 (file)
@@ -21,7 +21,7 @@
 #include <moof/math.hh>
 #include <moof/rigid_body.hh>
 #include <moof/sphere.hh>
-#include <moof/texture.hh>
+#include <moof/sprite.hh>
 
 #include "Animation.hh"
 
@@ -51,8 +51,8 @@ public:
 
        //virtual int getOctant(const moof::Aabb<3>& aabb) const;
 
-       moof::texture   tilemap;
-       Animation       animation;
+       moof::sprite    tilemap;
+       Animation               animation;
 };
 
 
index 8299d638211f57db283bd2e74ae7413af070fff1..86b871076ef68aca1d27fddda59f3a3c6b7dcf54 100644 (file)
@@ -187,7 +187,7 @@ void GameLayer::draw(moof::scalar alpha) const
        state_.camera.upload_to_gl(alpha);
 
        // DRAW THE SCENE
-       moof::texture::reset_binding();
+       moof::image::reset_binding();
 
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
index d5ed41281c70e1f7f1d2bfd3d2b025e49d5c5114..58bc7210422e54b0b0a98fb03b1835277db8e90e 100644 (file)
 #include "Hud.hh"
 
 
-ProgressBar::ProgressBar(const moof::texture& tilemap, int index) :
+ProgressBar::ProgressBar(const moof::sprite& tilemap, int index) :
        mProgress(0.0),
-       mTilemap(tilemap)
+       mTilemap(tilemap, index)
 {
-       tilemap.tile_coordinates(index, mTexCoords);
-
+       // TODO this is now broken
        moof::scalar half = (mTexCoords[2] - mTexCoords[0]) / 2.0 + mTexCoords[0];
        mMidCoords[0] = half - 0.01;
        mMidCoords[1] = half + 0.01;
@@ -105,8 +104,8 @@ void ProgressBar::draw(moof::scalar alpha) const
 
 Hud::Hud(GameState& state) :
        state_(state),
-       mBar1(moof::texture("StatusBars"), 0),
-       mBar2(moof::texture("StatusBars"), 2),
+       mBar1(moof::sprite("StatusBars"), 0),
+       mBar2(moof::sprite("StatusBars"), 2),
        mFont("Font")
 {
        moof::video* video = moof::video::current();
index 7cf2504b7d4a376fe1888d25c3b76acda7542c84..1bb557633c6d73581d3d91b2336d7e5c2120cc8e 100644 (file)
@@ -20,7 +20,7 @@
 #include <moof/drawable.hh>
 #include <moof/math.hh>
 //#include <moof/rectangle.hh>
-#include <moof/texture.hh>
+#include <moof/sprite.hh>
 #include <moof/view.hh>
 
 #include "GameState.hh"
@@ -34,7 +34,7 @@ class ProgressBar : public moof::drawable
 {
 public:
 
-       ProgressBar(const moof::texture& tilemap, int index);
+       ProgressBar(const moof::sprite& tilemap, int index);
 
        void resize(const moof::rectangle& rect);
 
@@ -49,7 +49,7 @@ private:
        moof::vector2   mVertices[8];
        moof::scalar    mWidth;
 
-       moof::texture   mTilemap;
+       moof::sprite    mTilemap;
        moof::scalar    mTexCoords[8];
        moof::scalar    mMidCoords[2];
 };
@@ -97,7 +97,7 @@ private:
        ProgressBar     mBar2;
 
        unsigned        mNumber;
-       moof::texture   mFont;
+       moof::sprite    mFont;
 
        moof::matrix4   mProjection;
 };
index adda533e40fcc595e8cf5784d3be4de6361a9bad..046c7bcffb71560e42b24b3b9e9f5d74c971946d 100644 (file)
@@ -216,9 +216,9 @@ void Main::printInfo(int argc, char* argv[])
 #ifdef YOINK_GITHEAD
                          << "       Commit: "YOINK_GITHEAD << std::endl
 #endif
-                         << "      Version: "PACKAGE_VERSION << std::endl
+                         << "      Version: " << PACKAGE_VERSION << std::endl
                          << "        Built: " << COMPILE_TIME << std::endl
-                         << "     Compiler: "COMPILER_STRING << std::endl
+                         << "     Compiler: " << COMPILER_STRING << std::endl
                          << "       Assets: " << assets << std::endl
                          << "Build options: "
 #ifndef HAVE_CLOCK_GETTIME
@@ -272,8 +272,21 @@ void goodbye()
 }
 
 
+#include <moof/image.hh>
+#include <moof/backend.hh>
+#include <moof/sprite.hh>
+
+
 int main(int argc, char* argv[])
 {
+       //moof::backend backend;
+       //moof::resource::add_search_paths(Main::getSearchPath());
+
+       //moof::image hey("textures/AlienWarrior.png");
+
+       //return 0;
+
+
        // FIXME: This is temporary.
        moof::timer reloadTimer(boost::bind(&moof::resource::reload_as_needed),
                                                        SCALAR(2.0),
@@ -307,10 +320,12 @@ int main(int argc, char* argv[])
 
        try
        {
-               std::string iconPath(PACKAGE".png");
-               iconPath = moof::resource::find_file(iconPath);
-               moof::image icon(iconPath);
-               icon.set_as_icon();
+               //std::string iconPath(PACKAGE".png");
+               //iconPath = moof::resource::find_file(iconPath);
+               //moof::image icon(iconPath);
+               //icon.set_as_icon();
+               moof::image_handle icon(PACKAGE".png");
+               if (icon) icon->set_as_icon();
 
                class moof::video::attributes attributes(settings);
                moof::video video(PACKAGE_STRING, attributes);
index 2d8f12025d3c8fd119d99d86e58368f03793a876..c756ec89c296decd840baaa14eeb67ce2f8ff50c 100644 (file)
@@ -20,7 +20,7 @@
 #include <moof/math.hh>
 #include <moof/script.hh>
 #include <moof/settings.hh>
-#include <moof/texture.hh>
+#include <moof/sprite.hh>
 
 #include "Character.hh"
 #include "Scene.hh"
@@ -41,7 +41,7 @@ struct Scene::impl : public moof::manager<impl>
                Quad(const moof::vector3* vertices[4],
                         const std::string& texture,
                         int tileIndex) :
-                       mTilemap(texture),
+                       mTilemap(texture, tileIndex),
                        mBlending(false),
                        mFog(false),
                        mSurface(NONE)
@@ -55,17 +55,6 @@ struct Scene::impl : public moof::manager<impl>
                                //}
                        }
 
-                       if (!mTilemap.tile_coordinates(tileIndex, mTexCoords))
-                       {
-                               moof::log_warning << "no index " << tileIndex <<
-                                       " in texture " << texture << std::endl;
-
-                               mTexCoords[0] = mTexCoords[1] =
-                                       mTexCoords[3] = mTexCoords[6] = 0.0;
-                               mTexCoords[2] = mTexCoords[4] =
-                                       mTexCoords[5] = mTexCoords[7] = 1.0;
-                       }
-
                        aabb_.enclose_vertices(mVertices, 4);
                        sphere_.point = aabb_.center();
                        sphere_.radius = (aabb_.min - sphere_.point).length();
@@ -106,12 +95,7 @@ struct Scene::impl : public moof::manager<impl>
                        }
 
                        //glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
-                       mTilemap.bind();
-
-                       glVertexPointer(3, GL_SCALAR, 0, mVertices[0].data());
-                       glTexCoordPointer(2, GL_SCALAR, 0, mTexCoords);
-
-                       glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+                       mTilemap.draw(mVertices);
 
                        glDisable(GL_BLEND);
                        glDisable(GL_FOG);
@@ -123,10 +107,9 @@ struct Scene::impl : public moof::manager<impl>
                }
 
 
-               moof::vector3           mVertices[4];
-               moof::scalar            mTexCoords[8];
+               moof::vector3   mVertices[4];
                
-               moof::texture           mTilemap;
+               moof::sprite    mTilemap;
 
                bool                    mBlending;
                bool                    mFog;
@@ -135,8 +118,8 @@ struct Scene::impl : public moof::manager<impl>
 
 
 
-       moof::matrix4                           mTransform;
-       std::string                             mTexture;
+       moof::matrix4           mTransform;
+       std::string                     mTexture;
 
        std::list< boost::shared_ptr<impl::Quad> >      mObjects;
        std::list<moof::line2>                                          mLines;
@@ -348,7 +331,7 @@ struct Scene::impl : public moof::manager<impl>
                                vertices[h][wPlus1] = moof::demote(mTransform *
                                                moof::vector4(wPlus1, h, 0.0, 1.0));
 
-                               if (index == moof::texture::no_tile) continue;
+                               if (index == moof::image::no_tile) continue;
 
                                const moof::vector3* corners[4] = {
                                        &vertices[h][w],
index cc995ba71746a549aa9ccb4d5fa987da96382477..b060ccf26e038c2c5c7c28834c0fde3f2c64644e 100644 (file)
@@ -14,6 +14,7 @@
 #include "TilemapFont.hh"
 
 
+/*
 TilemapFont::TilemapFont() :
        moof::texture("Font") {}
 
@@ -38,4 +39,5 @@ void TilemapFont::tile_coordinates(char symbol, moof::scalar coords[8],
 
        moof::texture::tile_coordinates(index, coords, what);
 }
+*/
 
index 5820a5a6b7853a2ee11b17b666eebb7deef5726a..3a7d378bccedde576340338fcbebcbca271054d5 100644 (file)
  * Text on the screen.
  */
 
-#include <moof/texture.hh>
+#include <moof/sprite.hh>
 
 
+/*
 class TilemapFont : public moof::texture
 {
 public:
@@ -29,6 +30,7 @@ public:
        void tile_coordinates(char symbol, moof::scalar coords[8],
                                           moof::texture::orientation what = moof::texture::normal);
 };
+*/
 
 
 #endif // _TILEMAPFONT_HH_
index 64e8968be114d72268808c65ae3630ac69a24582..b2fc4b0d23835ce44d1d7a6bb48afbe06bc6f3db 100644 (file)
@@ -24,8 +24,8 @@
 #include <moof/shape.hh>
 
 #include <moof/frustum.hh>             // FIXME: this file is quite broken
+#include <moof/image.hh>
 #include <moof/opengl.hh>
-#include <moof/texture.hh>
 
 
 namespace moof {
@@ -245,7 +245,7 @@ inline void aabb<3>::draw(scalar alpha) const
        glVertexPointer(3, GL_SCALAR, 0, vertices);
 
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
-       texture::reset_binding();
+       image::reset_binding();
 
        glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE,
                                   indices);
index 93ef6d6d0e8e7e7f00f9a3b5b0e074b33c42ba22..abe1677095c9bb12df0fb55826a1c35a143441bf 100644 (file)
@@ -11,8 +11,7 @@
 
 #include <cstdio>              // FILE
 #include <cstring>             // strncmp
-
-#include <boost/algorithm/string.hpp>
+#include <stdexcept>
 
 #include <png.h>
 #include <SDL/SDL.h>
 #include "backend.hh"
 #include "image.hh"
 #include "log.hh"
-#include "manager.hh"
+#include "opengl.hh"
+#include "script.hh"
+#include "video.hh"
 
 
 namespace moof {
 
 
-class image::impl : public manager<impl>
-{
-public:
+//static int power_of_two(int input)
+//{
+       //int value = 1;
 
-       explicit impl() :
-               context_(0),
-               pixels_(0) {}
-
-       ~impl()
-       {
-               SDL_FreeSurface(context_);
-               delete[] pixels_;
-       }
+       //while (value < input)
+       //{
+               //value <<= 1;
+       //}
+       //return value;
+//}
 
+unsigned image::global_object_ = 0;
 
-       void flip()
-       {
-               unsigned char*  pixels = (Uint8*)(context_->pixels);
 
-               unsigned                pitch = context_->pitch;
-               unsigned char   line[pitch];
+image::image(const std::string& path) :
+       pixels_(0),
+       object_(0),
+       min_filter_(GL_NEAREST),
+       mag_filter_(GL_NEAREST),
+       wrap_s_(GL_CLAMP),
+       wrap_t_(GL_CLAMP),
+       tile_width_(1),
+       tile_height_(1)
+{
+       FILE* fp = resource::open_file(path);
+       if (!fp) throw std::runtime_error("image not found at " + path);
 
-               int                             yBegin = 0;
-               int                             yEnd = context_->h - 1;
+       png_byte        signature[8];
+       size_t          bytesRead;
 
-               if (SDL_MUSTLOCK(context_)) SDL_LockSurface(context_);
-               while (yBegin < yEnd)
-               {
-                       memcpy(line,                    pixels + pitch * yBegin, pitch);
-                       memcpy(pixels + pitch * yBegin, pixels + pitch * yEnd,   pitch);
-                       memcpy(pixels + pitch * yEnd,   line,                    pitch);
-                       yBegin++;
-                       yEnd--;
-               }
-               if (SDL_MUSTLOCK(context_)) SDL_UnlockSurface(context_);
-       }
+       png_infop       pngInfo = 0;
+       png_infop       pngInfoEnd = 0;
+       png_structp pngObj = 0;
 
-       void set_as_icon() const
-       {
-               SDL_WM_SetIcon(context_, 0);
-       }
+       int bpp;
 
+       png_byte        colors;
+       png_bytepp      rows = 0;
 
-       void init(const std::string& name, bool flipped = false)
-       {
-               std::string path(name);
+       png_textp       texts = 0;
+       int                     nutext_s;
 
-               FILE* fp = resource::open_file(path);
-               if (!fp) return;
+       bytesRead = fread(signature, 1, sizeof(signature), fp);
+       if (bytesRead < sizeof(signature) ||
+               png_sig_cmp(signature, 0, sizeof(signature)) != 0) goto cleanup;
 
-               png_byte        signature[8];
-               size_t          bytesRead;
+       pngObj = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
+       if (!pngObj)     goto cleanup;
 
-               png_infop       pngInfo = 0;
-               png_infop       pngInfoEnd = 0;
-               png_structp pngObj = 0;
+       pngInfo = png_create_info_struct(pngObj);
+       if (!pngInfo)    goto cleanup;
 
-               int     width;
-               int     height;
-               int     pitch;
-               int bpp;
-               int channels;
+       pngInfoEnd = png_create_info_struct(pngObj);
+       if (!pngInfoEnd) goto cleanup;
 
-               png_byte        colors;
-               png_bytepp      rows = 0;
+       if (setjmp(png_jmpbuf(pngObj))) goto cleanup;
 
-               png_textp       texts = 0;
-               int                     nutext_s;
+       png_init_io(pngObj, fp);
+       png_set_sig_bytes(pngObj, sizeof(signature));
+       png_read_info(pngObj, pngInfo);
 
-               bytesRead = fread(signature, 1, sizeof(signature), fp);
-               if (bytesRead < sizeof(signature) ||
-                       png_sig_cmp(signature, 0, sizeof(signature)) != 0) goto cleanup;
+       bpp = png_get_bit_depth(pngObj, pngInfo);
+       colors = png_get_color_type(pngObj, pngInfo);
+       switch (colors)
+       {
+               case PNG_COLOR_TYPE_PALETTE:
+                       png_set_palette_to_rgb(pngObj);
+                       break;
 
-               pngObj = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
-               if (!pngObj)     goto cleanup;
+               case PNG_COLOR_TYPE_GRAY:
+                       if (bpp < 8) png_set_expand(pngObj);
+                       break;
 
-               pngInfo = png_create_info_struct(pngObj);
-               if (!pngInfo)    goto cleanup;
+               case PNG_COLOR_TYPE_GRAY_ALPHA:
+                       png_set_gray_to_rgb(pngObj);
+                       break;
+       }
 
-               pngInfoEnd = png_create_info_struct(pngObj);
-               if (!pngInfoEnd) goto cleanup;
+       if (bpp == 16) png_set_strip_16(pngObj);
 
-               if (setjmp(png_jmpbuf(pngObj))) goto cleanup;
+       png_read_update_info(pngObj, pngInfo);
 
-               png_init_io(pngObj, fp);
-               png_set_sig_bytes(pngObj, sizeof(signature));
-               png_read_info(pngObj, pngInfo);
+       bpp = png_get_bit_depth(pngObj, pngInfo);
+       channels_ = png_get_channels(pngObj, pngInfo);
+       depth_ = bpp * channels_;
 
-               bpp = png_get_bit_depth(pngObj, pngInfo);
-               colors = png_get_color_type(pngObj, pngInfo);
-               switch (colors)
+       // read comments
+       png_get_text(pngObj, pngInfo, &texts, &nutext_s);
+       for (int i = 0; i < nutext_s; ++i)
+       {
+               if (strncmp(texts[i].key, "TextureInfo", 11) == 0)
                {
-                       case PNG_COLOR_TYPE_PALETTE:
-                               png_set_palette_to_rgb(pngObj);
-                               break;
-
-                       case PNG_COLOR_TYPE_GRAY:
-                               if (bpp < 8) png_set_expand(pngObj);
-                               break;
-
-                       case PNG_COLOR_TYPE_GRAY_ALPHA:
-                               png_set_gray_to_rgb(pngObj);
-                               break;
+                       set_texture_info(texts[i].text);
+                       break;
                }
+       }
 
-               if (bpp == 16) png_set_strip_16(pngObj);
+       width_  = png_get_image_width(pngObj, pngInfo);
+       height_ = png_get_image_height(pngObj, pngInfo);
 
-               png_read_update_info(pngObj, pngInfo);
+       pitch_ = png_get_rowbytes(pngObj, pngInfo);
+       pixels_ = new char[width_ * pitch_];
 
-               bpp = png_get_bit_depth(pngObj, pngInfo);
-               channels = png_get_channels(pngObj, pngInfo);
-               depth_ = bpp * channels;
+       rows = new png_bytep[height_];
+       for (int i = 0; i < height_; ++i)
+       {
+               rows[i] = (png_bytep)(pixels_ + i * channels_ * width_);
+       }
 
-               if (channels == 3) color_mode_ = GL_RGB;
-               else               color_mode_ = GL_RGBA;
-               
-               // read comments
-               png_get_text(pngObj, pngInfo, &texts, &nutext_s);
-               for (int i = 0; i < nutext_s; ++i)
-               {
-                       if (strncmp(texts[i].key, "TextureInfo", 11) == 0)
-                       {
-                               comment_ = texts[i].text;
-                               break;
-                       }
-               }
+       png_read_image(pngObj, rows);
+       png_read_end(pngObj, 0);
 
-               width  = png_get_image_width(pngObj, pngInfo);
-               height = png_get_image_height(pngObj, pngInfo);
+cleanup:
 
-               pitch = png_get_rowbytes(pngObj, pngInfo);
-               pixels_ = new char[width * pitch];
+       delete[] rows;
+       png_destroy_read_struct(pngObj     ? &pngObj     : 0,
+                                                       pngInfo    ? &pngInfo    : 0,
+                                                       pngInfoEnd ? &pngInfoEnd : 0);
+       fclose(fp);
+}
 
-               rows = new png_bytep[height];
-               if (flipped)
-               {
-                       for (int i = 0; i < height; ++i)
-                       {
-                               rows[height - 1 - i] = (png_bytep)(pixels_ +
-                                                                                                  i * channels * width);
-                       }
-               }
-               else
-               {
-                       for (int i = 0; i < height; ++i)
-                       {
-                               rows[i] = (png_bytep)(pixels_ + i * channels * width);
-                       }
-               }
+image::~image()
+{
+       unload_from_gl();
+       delete[] pixels_;
+}
 
-               png_read_image(pngObj, rows);
-               png_read_end(pngObj, 0);
 
-               context_ = SDL_CreateRGBSurfaceFrom
-               (
-                       pixels_,
-                       width,
-                       height, 
-                       bpp * channels,
-                       pitch,
+void image::set_as_icon() const
+{
+       backend backend;
+
+       SDL_Surface* context = SDL_CreateRGBSurfaceFrom
+       (
+               pixels_,
+               width_,
+               height_, 
+               depth_,
+               pitch_,
 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
-                       0x000000FF,
-                       0x0000FF00,
-                       0x00FF0000,
-                       0xFF000000
+               0x000000FF,
+               0x0000FF00,
+               0x00FF0000,
+               0xFF000000
 #else
-                       0xFF000000,
-                       0x00FF0000,
-                       0x0000FF00,
-                       0x000000FF
+               0xFF000000,
+               0x00FF0000,
+               0x0000FF00,
+               0x000000FF
 #endif
-               );
-
-       cleanup:
-
-               delete[] rows;
-               png_destroy_read_struct(pngObj     ? &pngObj     : 0,
-                                                               pngInfo    ? &pngInfo    : 0,
-                                                               pngInfoEnd ? &pngInfoEnd : 0);
-               fclose(fp);
-       }
-
-
-       SDL_Surface*    context_;
-       char*                   pixels_;
-
-       unsigned                depth_;
-       GLuint                  color_mode_;
+       );
 
-       std::string             comment_;
+       SDL_WM_SetIcon(context, 0);
 
-private:
-
-       backend                 backend_;
-};
-
-
-image::image(const std::string& name) :
-       // pass through
-       impl_(image::impl::instance(name)) {}
+       SDL_FreeSurface(context);
+}
 
 
-bool image::is_valid() const
+bool image::tile_coordinates(int index, scalar coords[8]) const
 {
-       return impl_->context_;
+       // make sure the index represents a real tile
+       if (index < 0 && index >= tile_width_ * tile_height_) return false;
+
+       scalar w = 1.0 / scalar(tile_width_);
+       scalar h = 1.0 / scalar(tile_height_);
+
+       coords[0] = scalar(index % tile_width_) * w;
+       coords[1] = (scalar(tile_height_ - 1) - scalar(index / tile_width_)) * h;
+       coords[2] = coords[0] + w;
+       coords[3] = coords[1];
+       coords[4] = coords[2];
+       coords[5] = coords[1] + h;
+       coords[6] = coords[0];
+       coords[7] = coords[5];
+
+       return true;
 }
 
-int image::width() const
-{
-       return impl_->context_->w;
-}
 
-int image::height() const
+void image::bind() const
 {
-       return impl_->context_->h;
-}
+       ASSERT(video::current() && "should have a video context set");
 
-unsigned image::depth() const
-{
-       return impl_->depth_;
+       if (object_ == 0)
+       {
+               upload_to_gl();
+       }
+       if (object_ != global_object_)
+       {
+               glBindTexture(GL_TEXTURE_2D, (GLuint)object_);
+               global_object_ = object_;
+       }
 }
 
-unsigned image::pitch() const
+void image::reset_binding()
 {
-       return impl_->context_->pitch;
+       ASSERT(video::current() && "should have a video context set");
+
+       glBindTexture(GL_TEXTURE_2D, 0);
+       global_object_ = 0;
 }
 
-GLuint image::mode() const
+
+/*
+ * Upload the image to GL so that it will be accessible by a much more
+ * manageable handle and hopefully reside in video memory.
+ */
+void image::upload_to_gl() const
 {
-       return impl_->color_mode_;
+       if (object_)
+       {
+               // already loaded
+               return;
+       }
+
+       glGenTextures(1, (GLuint*)&object_);
+       glBindTexture(GL_TEXTURE_2D, (GLuint)object_);
+
+       GLuint mode;
+       if (channels_ == 3) mode = GL_RGB;
+       else                mode = GL_RGBA;
+       
+       glTexImage2D
+       //gluBuild2DMipmaps
+       (
+               GL_TEXTURE_2D,
+               0,
+               mode,
+               //3,
+               width_,
+               height_,
+               0,
+               mode,
+               GL_UNSIGNED_BYTE,
+               pixels_
+       );
+
+       set_properties();
+
+       // we want to know when the GL context is recreated
+       //dispatcher& dispatcher = dispatcher::global();
+       //new_context_ = dispatcher.add_target("video.newcontext",
+                                               //boost::bind(&image::context_recreated, this));
+       // FIXME this has const issues
 }
 
-std::string image::comment() const
+void image::unload_from_gl() const
 {
-       return impl_->comment_;
+       if (object_)
+       {
+               if (object_ == global_object_)
+               {
+                       global_object_ = 0;
+               }
+
+               glDeleteTextures(1, (GLuint*)&object_);
+               object_ = 0;
+       }
 }
 
-const char* image::pixels() const
+void image::context_recreated()
 {
-       return impl_->pixels_;
+       object_ = global_object_ = 0;
+       upload_to_gl();
 }
 
-char* image::pixels()
+/*
+ * Sets some texture properties such as the filters and external
+ * coordinate behavior.
+ */
+void image::set_properties() const
 {
-       return impl_->pixels_;
+       //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter_);
+       //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter_);
+       //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s_);
+       //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t_);
 }
 
 
-void image::flip()
+void image::set_texture_info(const std::string& info)
 {
-       // pass through
-       impl_->flip();
+       script script;
+       log::import(script);
+
+       script::slot g = script.globals();
+       g.set_field("CLAMP",   GL_CLAMP);
+       g.set_field("REPEAT",  GL_REPEAT);
+       g.set_field("LINEAR",  GL_LINEAR);
+       g.set_field("NEAREST", GL_NEAREST);
+       g.set_field("LINEAR_MIPMAP_LINEAR",   GL_LINEAR_MIPMAP_LINEAR);
+       g.set_field("LINEAR_MIPMAP_NEAREST",  GL_LINEAR_MIPMAP_NEAREST);
+       g.set_field("NEAREST_MIPMAP_LINEAR",  GL_NEAREST_MIPMAP_LINEAR);
+       g.set_field("NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST);
+
+       if (script.do_string(info) != script::success)
+       {
+               std::string str;
+               script[-1].get(str);
+               log_warning(str);
+       }
+       else
+       {
+               log_info("loading texture information...");
+
+               script::slot globals = script.globals();
+               globals.get(tile_width_, "tiles_s");
+               globals.get(tile_height_, "tiles_t");
+               globals.get(min_filter_, "min_filter");
+               globals.get(mag_filter_, "mag_filter");
+               globals.get(wrap_s_, "wrap_s");
+               globals.get(wrap_t_, "wrap_t");
+       }
 }
 
-void image::set_as_icon() const
+
+class image_resource_loader
 {
-       // pass through
-       impl_->set_as_icon();
-}
+public:
+       
+       image_resource_loader()
+       {
+               resource::register_type<image>("png");
+       }
+
+       ~image_resource_loader()
+       {
+               resource::unregister_type("png");
+       }
+};
+
+static image_resource_loader loader;
 
 
 } // namespace moof
index 614e614459a0fd0c5279d02e87f1bc895945c850..82c8f557206392980cddb6ee066a68a458e58899 100644 (file)
  * Defines classes for loading and manipulating images.
  */
 
-#include <boost/shared_ptr.hpp>
+#include <boost/noncopyable.hpp>
 
-#include <moof/opengl.hh>
+#include <moof/dispatcher.hh>
+#include <moof/math.hh>
 #include <moof/resource.hh>
 
 
 namespace moof {
 
 
-class image;
-typedef boost::shared_ptr<image> image_ptr;
-
-class image : public resource
+class image : public boost::noncopyable
 {
 public:
 
-       static image_ptr alloc(const std::string& name)
+       static const int no_tile = -1;
+
+
+       explicit image(const std::string& path);
+
+       ~image();
+
+       int width() const
        {
-               return image_ptr(new image(name));
+               return width_;
        }
 
-       explicit image(const std::string& name);
+       int height() const
+       {
+               return height_;
+       }
 
-       bool is_valid() const;
+       int depth() const
+       {
+               return depth_;
+       }
 
-       int width() const;
-       int height() const;
+       int pitch() const
+       {
+               return pitch_;
+       }
 
-       unsigned depth() const;
-       unsigned pitch() const;
-       GLuint mode() const;
+       int channels() const
+       {
+               return channels_;
+       }
 
-       std::string comment() const;
 
-       const char* pixels() const;
-       char* pixels();
+       std::string comment() const
+       {
+               return comment_;
+       }
+
+       const char* pixels() const
+       {
+               return pixels_;
+       }
 
-       void flip();
 
        void set_as_icon() const;
 
 
+       /**
+        * Calculate texture coordinates for a tile at a certain index.  Tiles
+        * are indexed start with zero as the top-left tile and moving across,
+        * then down.
+        * \param index The tile index.
+        * \param coords An array of scalars where the texture coordinates will
+        * be stored after this call.  The first coordinate (u,v) will be in
+        * the first two places and so on until all four coordinates are
+        * stored, therefore requiring enough room for an array of eight
+        * scalars.  The winding of the coordinates is always counter-clockwise
+        * (the GL default).
+        * \return True if index is valid, false otherwise.
+        */
+       bool tile_coordinates(int index, scalar coords[8]) const;
+
+
+       void bind() const;
+       static void reset_binding();
+
+
 private:
 
-       static FILE* open_file(std::string& name);
+       void upload_to_gl() const;
+       void unload_from_gl() const;
+       void context_recreated();
+       void set_properties() const;
+       void set_texture_info(const std::string& info);
 
-       class impl;
-       boost::shared_ptr<impl> impl_;
+
+       char*                           pixels_;
+
+       mutable unsigned        object_;
+       static unsigned         global_object_;
+
+       int                                     width_;
+       int                                     height_;
+       int                                     depth_;
+       int                                     pitch_;
+       int                                     channels_;
+
+       std::string                     comment_;
+
+       unsigned                        min_filter_;
+       unsigned                        mag_filter_;
+       unsigned                        wrap_s_;
+       unsigned                        wrap_t_;
+       int                                     tile_width_;
+       int                                     tile_height_;
+
+       //mutable dispatcher::handle    new_context_;
 };
 
+typedef resource_handle<image> image_handle;
+
 
 } // namespace moof
 
index 35ebf29199afbfa78ef6329fd611f334428892c0..0ba6bb368c6d56b628ed8ec9a38862fb91e0abac 100644 (file)
 
 #include <moof/contact.hh>
 #include <moof/drawable.hh>
+#include <moof/image.hh>
 #include <moof/log.hh>
 #include <moof/math.hh>
 #include <moof/opengl.hh>
 #include <moof/ray.hh>
 #include <moof/shape.hh>
 #include <moof/sphere.hh>
-#include <moof/texture.hh>
 
 
 namespace moof {
@@ -225,7 +225,7 @@ struct line : public drawable, public shape<D>
 
        void draw(scalar alpha = 0.0) const
        {
-               texture::reset_binding();
+               image::reset_binding();
                glBegin(GL_LINES);
                        glVertex(a);
                        glVertex(b);
@@ -255,7 +255,7 @@ struct polygon : public drawable, public shape<D>
 
        void draw(scalar alpha = 0.0) const
        {
-               texture::reset_binding();
+               image::reset_binding();
                glBegin(GL_POLYGON);
                for (int i = 0; i < D; ++i)
                {
index 9413f0daba240cbeb88d0d15d4a65dde605365d6..b1f7bdde88e54d937b124ee06433900df56c2de4 100644 (file)
@@ -19,9 +19,9 @@
  */
 
 #include <moof/drawable.hh>
+#include <moof/image.hh>
 #include <moof/math.hh>
 #include <moof/opengl.hh>
-#include <moof/texture.hh>
 
 
 namespace moof {
@@ -60,7 +60,7 @@ struct ray : public drawable
                vector end = point + 1000.0 * direction;
                // FIXME: this is kinda cheesy
 
-               moof::texture::reset_binding();
+               image::reset_binding();
                glBegin(GL_LINES);
                        glVertex(point);
                        glVertex(end);
index 314f80b5db81707e73b4cb3d91300f6d5b5a8ea0..053fc0b31849e9b37991d90f671e71ee7983800d 100644 (file)
@@ -127,7 +127,7 @@ resource_ptr resource::load(const std::string& path)
 {
        std::string extension = stlplus::extension_part(path);
 
-       std::string path1 = find_file(path);
+       std::string path1 = path;
        if (!find(path1))
        {
                log_error("trying to load missing resource:", path1);
@@ -212,21 +212,35 @@ void resource::add_search_paths(const std::string& paths)
 
 bool resource::find(const std::string& path)
 {
-       std::string file = stlplus::lookup(path, search_paths_, ":");
-       log_info("found file", file, "in", search_paths_);
-       return !stlplus::lookup(path, search_paths_, ":").empty();
+       //std::string file = stlplus::lookup(path, search_paths_, ":");
+       //log_info("found file", file, "in", search_paths_);
+       //return !stlplus::lookup(path, search_paths_, ":").empty();
+       return find_file(path) != "";
 }
 
 std::string resource::find_file(const std::string& name)
 {
-       log_info("looking for", name, "in", search_paths_);
-       return stlplus::lookup(name, search_paths_, ":");
+       //log_info("looking for", name, "in", search_paths_);
+       //return stlplus::lookup(name, search_paths_, ":");
+
+       std::vector<std::string> paths;
+       boost::split(paths, search_paths_, boost::is_any_of(":"));
+
+       std::vector<std::string>::iterator it;
+       for (it = paths.begin(); it != paths.end(); ++it)
+       {
+               *it += "/";
+               *it += name;
+               log_info("looking for", name, "in", *it);
+               if (stlplus::file_exists(*it)) return *it;
+       }
+
+       return std::string();
 }
 
 FILE* resource::open_file(const std::string& path, const std::string& mode)
 {
-       std::string file = stlplus::lookup(path, search_paths_, ":");
-       return fopen(file.c_str(), mode.c_str());
+       return fopen(find_file(path).c_str(), mode.c_str());
 }
 
 
index f8fd4f6327ee9b18ad47b6e0360e0c25115a5d1d..5f1a71ab41e366596ef890f26806fd9f4d097173 100644 (file)
@@ -285,6 +285,9 @@ public:
        resource_handle(resource_ptr ptr) :
                resource_(ptr) {}
 
+       explicit resource_handle(const std::string& path) :
+               resource_(resource::load(path)) {}
+
 
        /**
         * Get whether or not the handle is dereferenceable to the type of this
index 853c21a3d806f92bbb48fb91cb188b6d70e5c678..a6fbf0310679faacc76175aa9d67b71079b1e9e5 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <boost/algorithm/string.hpp>
 #include <boost/cstdint.hpp>
+#include <boost/noncopyable.hpp>
 #include <AL/al.h>
 #include <AL/alc.h>
 #include <vorbis/codec.h>
@@ -233,7 +234,7 @@ buffer::retcount_lookup buffer::retain_counts_;
 
 // SOUND RESOURCE
 
-class sound_resource
+class sound_resource : public boost::noncopyable
 {
 public:
 
@@ -252,7 +253,7 @@ public:
        }
 
 
-       bool read(buffer& buf)
+       bool read(buffer& buf) const
        {
                if (buffer_)
                {
@@ -305,7 +306,7 @@ public:
        }
 
 
-       bool read(buffer& buf, uint64_t& sample)
+       bool read(buffer& buf, uint64_t& sample) const
        {
                if (ov_pcm_seek_lap(&file_, sample) != 0)
                {
@@ -344,8 +345,8 @@ public:
 
 private:
 
-       OggVorbis_File  file_;
-       buffer                  buffer_;
+       mutable OggVorbis_File  file_;
+       mutable buffer                  buffer_;
 };
 
 
index a3c94f1009bbe698bf7cd9d5fa716a8ac9d5a92c..7fdfd0948b49c68fea6600660567eb3c08af8807 100644 (file)
@@ -53,10 +53,10 @@ public:
        void loop(bool looping);
        
 
-       void listener_position(const vector3& position);
-       void listener_velocity(const vector3& velocity);
-       void listener_orientation(const vector3& forward,
-                                                         const vector3& up);
+       static void listener_position(const vector3& position);
+       static void listener_velocity(const vector3& velocity);
+       static void listener_orientation(const vector3& forward,
+                                                                        const vector3& up);
 
 
 private:
diff --git a/src/moof/sprite.cc b/src/moof/sprite.cc
new file mode 100644 (file)
index 0000000..7548ab0
--- /dev/null
@@ -0,0 +1,142 @@
+
+/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
+**]  All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
+
+#include <cstdio>              // FILE
+#include <cstring>             // strncmp
+#include <stdexcept>
+
+#include <boost/algorithm/string.hpp>
+#include <boost/bind.hpp>
+
+#include "dispatcher.hh"
+#include "log.hh"
+#include "opengl.hh"
+#include "sprite.hh"
+
+
+namespace moof {
+
+
+sprite::sprite(const std::string& path, int tile)
+{
+       image_ = resource::load(path);
+       image_->tile_coordinates(tile, tile_);
+}
+
+sprite::sprite(const image_handle& image, int tile) :
+       image_(image)
+{
+       image_->tile_coordinates(tile, tile_);
+}
+
+sprite::sprite(const sprite& sprite, int tile)
+{
+       // FIXME broken
+       image_ = sprite.image_;
+}
+
+
+void sprite::image(const std::string& path)
+{
+       image_ = resource::load(path);
+       // FIXME what about tiles?
+}
+
+void sprite::tile(int tile)
+{
+       if (image_) image_->tile_coordinates(tile, tile_);
+}
+
+
+void sprite::bind() const
+{
+       if (image_) image_->bind();
+}
+
+void sprite::reset_binding()
+{
+       image::reset_binding();
+}
+
+
+void sprite::draw(const vector3 vertices[4]) const
+{
+       bind();
+
+       glVertexPointer(3, GL_SCALAR, 0, vertices[0].data());
+       glTexCoordPointer(2, GL_SCALAR, 0, tile_);
+
+       glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+}
+
+
+/*
+       void min_filter(GLuint filter)
+       {
+               bind();
+               mMinFilter = filter;
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mMinFilter);
+       }
+
+       void mag_filter(GLuint filter)
+       {
+               bind();
+               mMagFilter = filter;
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mMagFilter);
+       }
+
+       void wrap_s(GLuint wrap)
+       {
+               bind();
+               mWrapS = wrap;
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mWrapS);
+       }
+
+       void wrap_t(GLuint wrap)
+       {
+               bind();
+               mWrapT = wrap;
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mWrapT);
+       }
+
+
+bool sprite::tile_coordinates(int index, scalar coords[8],
+               orientation orientation) const
+{
+       if (tile_coordinates(index, coords))
+       {
+               if (orientation & flip)
+               {
+                       // this looks kinda weird, but it's just swapping in a way that
+                       // doesn't require an intermediate variable
+                       coords[1] = coords[5];
+                       coords[5] = coords[3];
+                       coords[3] = coords[7];
+                       coords[7] = coords[5];
+               }
+               if (orientation & reverse)
+               {
+                       coords[0] = coords[2];
+                       coords[2] = coords[6];
+                       coords[4] = coords[6];
+                       coords[6] = coords[0];
+               }
+
+               return true;
+       }
+
+       return false;
+}
+*/
+
+
+} // namespace moof
+
diff --git a/src/moof/sprite.hh b/src/moof/sprite.hh
new file mode 100644 (file)
index 0000000..3cee872
--- /dev/null
@@ -0,0 +1,57 @@
+
+/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
+**]  All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
+
+#ifndef _MOOF_SPRITE_HH_
+#define _MOOF_SPRITE_HH_
+
+/**
+ * \file sprite.hh
+ * Image-loading and OpenGL texture loading.
+ */
+
+#include <string>
+
+#include <moof/image.hh>
+#include <moof/math.hh>
+
+
+namespace moof {
+
+
+class sprite
+{
+public:
+
+       sprite() {}
+       explicit sprite(const std::string& path, int tile = image::no_tile);
+       explicit sprite(const image_handle& image, int tile = image::no_tile);
+       explicit sprite(const sprite& sprite, int tile = image::no_tile);
+
+       void image(const std::string& path);
+       void tile(int tile);
+
+       void bind() const;
+       static void reset_binding();
+
+       void draw(const vector3 vertices[4]) const;
+
+
+private:
+
+       image_handle    image_;
+       scalar                  tile_[8];
+};
+
+
+} // namespace moof
+
+#endif // _MOOF_SPRITE_HH_
+
diff --git a/src/moof/texture.cc b/src/moof/texture.cc
deleted file mode 100644 (file)
index 94d792d..0000000
+++ /dev/null
@@ -1,399 +0,0 @@
-
-/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
-**]  All rights reserved.
-*
-* vi:ts=4 sw=4 tw=75
-*
-* Distributable under the terms and conditions of the 2-clause BSD license;
-* see the file COPYING for a complete text of the license.
-*
-**************************************************************************/
-
-#include <cstdio>              // FILE
-#include <cstring>             // strncmp
-#include <stdexcept>
-
-#include <boost/algorithm/string.hpp>
-#include <boost/bind.hpp>
-
-#include "dispatcher.hh"
-#include "manager.hh"
-#include "log.hh"
-#include "opengl.hh"
-#include "script.hh"
-#include "texture.hh"
-#include "video.hh"
-
-
-namespace moof {
-
-
-/**
- * The texture implementation just contains all the information about the
- * image which is worth having in memory.  The image data itself is not
- * worth keeping in memory if the texture has been loaded to GL, but the
- * name of the resource is retained so that it can be reloaded if
- * necessary.  The implementation is a manager so that multiple texture
- * objects can share the same internal objects and avoid having duplicate
- * textures loaded to GL.
- */
-
-class texture::impl : public manager<impl>
-{
-
-       /**
-        * Delete the texture (if it is loaded) from GL.
-        */
-
-       void unload_from_gl()
-       {
-               if (mObject)
-               {
-                       if (mObject == gObject)
-                       {
-                               gObject = 0;
-                       }
-
-                       glDeleteTextures(1, &mObject);
-                       mObject = 0;
-               }
-       }
-
-       /**
-        * If the GL context was recreated, we need to reload the texture.
-        * This may involve reading it from disk again, but hopefully the OS
-        * was smart enough to cache it if the client has plenty of RAM.
-        */
-
-       void context_recreated()
-       {
-               mObject = gObject = 0;
-               upload_to_gl();
-       }
-
-       /**
-        * This is a helper method used by some of the texture loading code.
-        * It returns the first power of two which is greater than the input
-        * value.
-        */
-
-       static int power_of_two(int input)
-       {
-               int value = 1;
-
-               while (value < input)
-               {
-                       value <<= 1;
-               }
-               return value;
-       }
-
-
-       static void bind_script_constants(script& script)
-       {
-               script::slot g = script.globals();
-
-               g.set_field("CLAMP",   GL_CLAMP);
-               g.set_field("REPEAT",  GL_REPEAT);
-               g.set_field("LINEAR",  GL_LINEAR);
-               g.set_field("NEAREST", GL_NEAREST);
-               g.set_field("LINEAR_MIPMAP_LINEAR",   GL_LINEAR_MIPMAP_LINEAR);
-               g.set_field("LINEAR_MIPMAP_NEAREST",  GL_LINEAR_MIPMAP_NEAREST);
-               g.set_field("NEAREST_MIPMAP_LINEAR",  GL_NEAREST_MIPMAP_LINEAR);
-               g.set_field("NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST);
-       }
-
-public:
-
-       /*
-        * Construction is initialization.
-        */
-       impl() :
-               mMinFilter(GL_NEAREST),
-               mMagFilter(GL_NEAREST),
-               mWrapS(GL_CLAMP),
-               mWrapT(GL_CLAMP),
-               mTilesS(1),
-               mTilesT(1),
-               mObject(0)
-       {
-               // make sure we have a video context
-               video* video = video::current();
-               ASSERT(video && "should have a video context set");
-
-               // we want to know when the GL context is recreated
-               dispatcher& dispatcher = dispatcher::global();
-               mNewContextDispatch = dispatcher.add_target("video.newcontext",
-                                                       boost::bind(&impl::context_recreated, this));
-       }
-
-       ~impl()
-       {
-               unload_from_gl();
-       }
-
-
-       void init(const std::string& name)
-       {
-               std::string path(name);
-               
-               resource::find(path);
-
-               mImage = image::alloc(path);
-               if (!mImage->is_valid())
-               {
-                       throw std::runtime_error("texture not found: " + name);
-               }
-
-               mImage->flip();
-
-               script script;
-
-               bind_script_constants(script);
-               log::import(script);
-
-               if (script.do_string(mImage->comment()) != script::success)
-               {
-                       std::string str;
-                       script[-1].get(str);
-                       log_warning(str);
-               }
-               else
-               {
-                       log_info("loading tiles from texture", path);
-
-                       script::slot globals = script.globals();
-                       globals.get(mTilesS, "tiles_s");
-                       globals.get(mTilesT, "tiles_t");
-                       globals.get(mMinFilter, "min_filter");
-                       globals.get(mMagFilter, "mag_filter");
-                       globals.get(mWrapS, "wrap_s");
-                       globals.get(mWrapT, "wrap_t");
-               }
-       }
-
-
-       /*
-        * Upload the image to GL so that it will be accessible by a much more
-        * manageable handle and hopefully reside in video memory.
-        */
-       void upload_to_gl()
-       {
-               if (mObject)
-               {
-                       // already loaded
-                       return;
-               }
-
-               glGenTextures(1, &mObject);
-               glBindTexture(GL_TEXTURE_2D, mObject);
-
-               glTexImage2D
-               //gluBuild2DMipmaps
-               (
-                       GL_TEXTURE_2D,
-                       0,
-                       mImage->mode(),
-                       //3,
-                       mImage->width(),
-                       mImage->height(),
-                       0,
-                       mImage->mode(),
-                       GL_UNSIGNED_BYTE,
-                       mImage->pixels()
-               );
-
-               set_properties();
-       }
-
-
-       /*
-        * Sets some texture properties such as the filters and external
-        * coordinate behavior.
-        */
-       void set_properties()
-       {
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mMinFilter);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mMagFilter);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mWrapS);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mWrapT);
-       }
-
-       void min_filter(GLuint filter)
-       {
-               bind();
-               mMinFilter = filter;
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mMinFilter);
-       }
-
-       void mag_filter(GLuint filter)
-       {
-               bind();
-               mMagFilter = filter;
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mMagFilter);
-       }
-
-       void wrap_s(GLuint wrap)
-       {
-               bind();
-               mWrapS = wrap;
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mWrapS);
-       }
-
-       void wrap_t(GLuint wrap)
-       {
-               bind();
-               mWrapT = wrap;
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mWrapT);
-       }
-
-
-       void bind()
-       {
-               if (mObject == 0)
-               {
-                       upload_to_gl();
-               }
-               if (mObject != gObject)
-               {
-                       glBindTexture(GL_TEXTURE_2D, mObject);
-                       gObject = mObject;
-               }
-       }
-
-
-       bool tile_coordinates(int index, scalar coords[8]) const
-       {
-               // make sure the index represents a real tile
-               if (index < 0 && index >= mTilesS * mTilesT) return false;
-
-               scalar w = 1.0 / scalar(mTilesS);
-               scalar h = 1.0 / scalar(mTilesT);
-
-               coords[0] = scalar(index % mTilesS) * w;
-               coords[1] = (scalar(mTilesT - 1) - scalar(index / mTilesS)) * h;
-               coords[2] = coords[0] + w;
-               coords[3] = coords[1];
-               coords[4] = coords[2];
-               coords[5] = coords[1] + h;
-               coords[6] = coords[0];
-               coords[7] = coords[5];
-
-               return true;
-       }
-
-       image_ptr                       mImage;
-
-       GLuint                          mMinFilter;     ///< Minification filter.
-       GLuint                          mMagFilter;     ///< Magnification filter.
-       GLuint                          mWrapS;         ///< Wrapping behavior horizontally.
-       GLuint                          mWrapT;         ///< Wrapping behavior vertically.
-       int                                     mTilesS;
-       int                                     mTilesT;
-
-       GLuint                          mObject;        ///< GL texture handle.
-       static GLuint           gObject;        ///< Global GL texture handle.
-
-       dispatcher::handle      mNewContextDispatch;
-};
-
-GLuint texture::impl::gObject = 0;
-
-
-texture::texture(const std::string& name) : // FIXME: this is really weird
-       image(name),
-       // pass through
-       impl_(texture::impl::instance(name)) {}
-
-
-/**
- * Bind the GL texture for mapping, etc.
- */
-
-void texture::bind() const
-{
-       // pass through
-       impl_->bind();
-}
-
-
-/**
- * Get the texture object, for the curious.
- */
-
-GLuint texture::object() const
-{
-       // pass through
-       return impl_->mObject;
-}
-
-
-void texture::reset_binding()
-{
-       glBindTexture(GL_TEXTURE_2D, 0);
-       impl::gObject = 0;
-}
-
-
-void texture::min_filter(GLuint filter)
-{
-       // pass through
-       impl_->min_filter(filter);
-}
-
-void texture::mag_filter(GLuint filter)
-{
-       // pass through
-       impl_->mag_filter(filter);
-}
-
-void texture::wrap_s(GLuint wrap)
-{
-       // pass through
-       impl_->wrap_s(wrap);
-}
-
-void texture::wrap_t(GLuint wrap)
-{
-       // pass through
-       impl_->wrap_t(wrap);
-}
-
-
-bool texture::tile_coordinates(int index, scalar coords[8]) const
-{
-       // pass through
-       return impl_->tile_coordinates(index, coords);
-}
-
-bool texture::tile_coordinates(int index, scalar coords[8],
-               orientation orientation) const
-{
-       if (tile_coordinates(index, coords))
-       {
-               if (orientation & flip)
-               {
-                       // this looks kinda weird, but it's just swapping in a way that
-                       // doesn't require an intermediate variable
-                       coords[1] = coords[5];
-                       coords[5] = coords[3];
-                       coords[3] = coords[7];
-                       coords[7] = coords[5];
-               }
-               if (orientation & reverse)
-               {
-                       coords[0] = coords[2];
-                       coords[2] = coords[6];
-                       coords[4] = coords[6];
-                       coords[6] = coords[0];
-               }
-
-               return true;
-       }
-
-       return false;
-}
-
-
-} // namespace moof
-
diff --git a/src/moof/texture.hh b/src/moof/texture.hh
deleted file mode 100644 (file)
index a53dcd1..0000000
+++ /dev/null
@@ -1,107 +0,0 @@
-
-/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
-**]  All rights reserved.
-*
-* vi:ts=4 sw=4 tw=75
-*
-* Distributable under the terms and conditions of the 2-clause BSD license;
-* see the file COPYING for a complete text of the license.
-*
-**************************************************************************/
-
-#ifndef _MOOF_TEXTURE_HH_
-#define _MOOF_TEXTURE_HH_
-
-/**
- * \file texture.hh
- * Image-loading and OpenGL texture loading.
- */
-
-#include <string>
-
-#include <boost/shared_ptr.hpp>
-
-#include <moof/image.hh>
-#include <moof/opengl.hh>
-
-
-namespace moof {
-
-
-class texture;
-typedef boost::shared_ptr<texture> texture_ptr;
-
-
-class texture : public image
-{
-public:
-
-       static const int no_tile = -1;
-
-       /**
-        * Possible orientations for texture coordinates.
-        */
-       typedef enum
-       {
-               normal =                        0,                      ///< Normal orientation.
-               flip =                          1,                      ///< Flip over a horizontal axis.
-               reverse =                       2,                      ///< Flip over a vertical axis.
-               flip_and_reverse =      3                       ///< Flip over both.
-       } orientation;
-
-
-       static texture_ptr alloc(const std::string& name)
-       {
-               return texture_ptr(new texture(name));
-       }
-
-       explicit texture(const std::string& name);
-
-       void bind() const;
-       GLuint object() const;
-
-       static void reset_binding();
-
-       void min_filter(GLuint filter);
-       void mag_filter(GLuint filter);
-       void wrap_s(GLuint wrap);
-       void wrap_t(GLuint wrap);
-
-
-       /**
-        * Calculate texture coordinates for a tile at a certain index.  Tiles
-        * are indexed start with zero as the to-left tile and moving across,
-        * then down.
-        * \param index The tile index.
-        * \param coords An array of scalars where the texture coordinates will
-        * be stored after this call.  The first coordinate (u,v) will be in
-        * the first two places and so on until all four coordinates are
-        * stored, therefore requiring enough room for an array of eight
-        * scalars.  The winding of the coordinates is always counter-clockwise
-        * (the GL default).
-        * \return True if index is valid, false otherwise.
-        */
-       bool tile_coordinates(int index, scalar coords[8]) const;
-
-       /**
-        * This version let's you specify an orientation that will be reflected
-        * in the texture coordinates.  This allows you to easily map a texture
-        * backwards or upside-down.
-        * \param what The orientation; can be flip, reverse, or
-        * flip_and_reverse.
-        * \return True if index is valid, false otherwise.
-        */
-       bool tile_coordinates(int index, scalar coords[8], orientation what) const;
-
-
-private:
-
-       class impl;
-       boost::shared_ptr<impl> impl_;
-};
-
-
-} // namespace moof
-
-#endif // _MOOF_TEXTURE_HH_
-
index 7c224036aa7d59967a5af158ba8fe49b34273f33..25ca5d56e40b7ee3a7293367393f34a3e1d13e75 100644 (file)
@@ -11,7 +11,7 @@
 FILEVERSION            VERSION_MAJOR,VERSION_MINOR,VERSION_REVISION,0
 PRODUCTVERSION VERSION_MAJOR,VERSION_MINOR,VERSION_REVISION,0
 FILEFLAGSMASK  VS_FFI_FILEFLAGSMASK
-#ifdef NDEBUG
+#if NDEBUG
        FILEFLAGS       0
 #else
        FILEFLAGS       VS_FF_DEBUG
This page took 0.083949 seconds and 4 git commands to generate.