|
From: Stefano A. <xa...@us...> - 2009-12-22 20:58:15
|
Module: performous
Branch: master
Commit: e26d63a8be53076bcb104926793a3e502c430e66
Author: Xaldyz <xa...@us...>
Date: Tue Dec 22 21:57:59 2009 +0100
Added stump hacking to switch from Magick++ to SDL_Image (if this one is found)
---
game/CMakeLists.txt | 10 ++++++++
game/surface.cc | 26 ++++++++++++++++++++-
game/video_driver.cc | 59 ++++++++++++++++++++++++++++++++++++++++++++-----
3 files changed, 87 insertions(+), 8 deletions(-)
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt
index 7c411c9..0b58bf3 100644
--- a/game/CMakeLists.txt
+++ b/game/CMakeLists.txt
@@ -47,6 +47,16 @@ else()
message(STATUS "MIDI I/O: Disabled (libportmidi not found)")
endif()
+find_package(SDL_image)
+if(SDL_image_FOUND)
+ include_directories(${SDL_image_INCLUDE_DIRS})
+ add_definitions(-DLESS_MAGIC) # ;)
+ list(APPEND LIBS ${SDL_image_LIBRARIES})
+ message(STATUS "SDL_image: found, enabling replacements for Magick++ code")
+else()
+ message(STATUS "SDL_image: not found, falling back to Magick++")
+endif()
+
# Set default compile flags for GCC
if(CMAKE_COMPILER_IS_GNUCXX)
message(STATUS "GCC detected, adding compile flags")
diff --git a/game/surface.cc b/game/surface.cc
index f46936f..aa09180 100644
--- a/game/surface.cc
+++ b/game/surface.cc
@@ -6,13 +6,18 @@
#include <librsvg/rsvg.h>
#include <librsvg/rsvg-cairo.h>
-#include <Magick++.h>
+#ifdef LESS_MAGIC
+ #include <SDL_image.h>
+#else
+ #include <Magick++.h>
+ using boost::uint32_t;
+#endif
#include <fstream>
#include <stdexcept>
#include <sstream>
#include <vector>
-using boost::uint32_t;
+
float Dimensions::screenY() const {
switch (m_screenAnchor) {
@@ -85,6 +90,22 @@ template <typename T> void loader(T& target, std::string filename, bool autocrop
gdk_pixbuf_unref(pb);
rsvg_term();
} else {
+ #ifdef LESS_MAGIC
+ IMG_Init(0);
+ SDL_Surface* imgsurf = IMG_Load(filename.c_str());
+ IMG_Quit();
+ if (imgsurf == NULL) {
+ throw std::runtime_error("Unable to load " + filename + ": " + IMG_GetError());
+ }
+ // TODO: blit to a new RGBA surface to ensure it actually *is* RGBA
+ try {
+ target.load(imgsurf->w, imgsurf->h, pix::CHAR_RGBA, reinterpret_cast<const unsigned char*>(imgsurf->pixels));
+ SDL_FreeSurface(imgsurf);
+ } catch (...) {
+ SDL_FreeSurface(imgsurf);
+ throw;
+ }
+ #else
Magick::Image image;
Magick::Blob blob;
try {
@@ -112,6 +133,7 @@ template <typename T> void loader(T& target, std::string filename, bool autocrop
{
throw std::runtime_error("Image Error");
}
+ #endif
}
}
diff --git a/game/video_driver.cc b/game/video_driver.cc
index ac6a69d..7ecdffa 100644
--- a/game/video_driver.cc
+++ b/game/video_driver.cc
@@ -7,7 +7,17 @@
#include "joystick.hh"
#include <SDL.h>
-#include <Magick++.h>
+#ifdef LESS_MAGIC
+ #include <sstream>
+ #include <fstream>
+#else
+ #include <Magick++.h>
+#endif
+
+#ifdef _WIN32
+ // for GetTempPathA
+ #include <windows.h>
+#endif
namespace {
unsigned s_width;
@@ -63,8 +73,42 @@ void Window::screenshot() {
std::vector<char> buffer(width*height*3);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, &buffer[0]);
+ std::ostringstream fnstr;
+#ifdef _WIN32
+ char tmppath[256];
+ GetTempPathA(256, tmppath); /* this includes a backslash at the end */
+ fnstr << tmppath;
+#else
+ fnstr << "/tmp/";
+#endif
+ fnstr << "performous_screenshot_" << count;
+#ifdef LESS_MAGIC
+ /* Write the image out as an uncompressed tga. */
+ fnstr << ".tga";
+ std::ofstream tgaout(fnstr.str().c_str(), std::ios::binary);
+ char tga_hdr_part1[] = {
+ 0x00, /* no identification field */
+ 0x00, /* no palette */
+ 0x02, /* 24-bit RGB */
+ 0x00, 0x00, 0x00, 0x00, 0x00, /* palette info (ignored) */
+ 0x00, 0x00, /* x-origin */
+ 0x00, 0x00, /* y-origin */
+ };
+ tgaout.write(tga_hdr_part1, sizeof(tga_hdr_part1));
+ /* two 16-bit little endian words for width and height */
+ tgaout << static_cast<char>(width & 0xff);
+ tgaout << static_cast<char>(width >> 8);
+ tgaout << static_cast<char>(height & 0xff);
+ tgaout << static_cast<char>(height >> 8);
+ tgaout << static_cast<char>(24); /* bits per pixel */
+ tgaout << static_cast<char>(0x00); /* no special flags */
+ for (int i = 0; i < width*height*3; i += 3)
+ std::swap(buffer[i], buffer[i+2]); /* fix the channel order */
+ tgaout.write(&buffer[0], width*height*3); /* dump the image data */
+ tgaout.close();
+#else
+ fnstr << ".png";
Magick::Blob blob( &buffer[0], width*height*3);
-
Magick::Image image;
char geometry[16];
sprintf(geometry,"%dx%d",width,height);
@@ -73,6 +117,8 @@ void Window::screenshot() {
image.magick( "RGB" );
image.read(blob);
image.flip();
+ image.write(fnstr.str().c_str());
+#endif
char filename[256];
sprintf(filename,"/tmp/performous_screenshot_%u.png",count);
image.write(filename);
@@ -119,14 +165,15 @@ void Window::resize() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float h = virtH();
- const float near = 1.5f; // This determines FOV: the value is your distance from the monitor (the unit being the width of the Performous window)
- const float far = 100.0f; // How far away can things be seen
+ // stump: under MSVC, near and far are #defined to nothing for compatibility with ancient code, hence the underscores.
+ const float near_ = 1.5f; // This determines FOV: the value is your distance from the monitor (the unit being the width of the Performous window)
+ const float far_ = 100.0f; // How far away can things be seen
// Set model-view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
const float f = 0.9f; // Avoid texture surface being exactly at the near plane (MacOSX fix)
- glFrustum(-0.5f * f, 0.5f * f, 0.5f * h * f, -0.5f * h * f, f * near, far);
- glTranslatef(0.0f, 0.0f, -near); // So that z = 0.0f is still on monitor surface
+ glFrustum(-0.5f * f, 0.5f * f, 0.5f * h * f, -0.5f * h * f, f * near_, far_);
+ glTranslatef(0.0f, 0.0f, -near_); // So that z = 0.0f is still on monitor surface
}
|