From 1d595f048c9e59bbff14217e2fe3821ee3e1aaa1 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Sun, 9 May 2010 19:17:52 +0200 Subject: changed from SDL rendering to OpenGL draw, repaint methods do not have the SDL_Surface* parameter anymore --- src/base/image.cpp | 5 ++- src/base/image.hpp | 4 +- src/base/texture.cpp | 83 ++++++++++++++++++++++++++++++++++++- src/base/texture.hpp | 52 +++++++++++++++++------ src/menu/application.cpp | 52 ++++++++++++++++++----- src/menu/application.hpp | 4 +- src/menu/container.cpp | 6 +-- src/menu/container.hpp | 3 +- src/menu/drawable.cpp | 4 +- src/menu/drawable.hpp | 6 +-- src/menu/frame.cpp | 6 ++- src/menu/frame.hpp | 2 +- src/menu/frame_background.hpp | 7 ---- src/menu/frame_background_color.cpp | 13 +++++- src/menu/frame_background_color.hpp | 5 +-- src/menu/reflection.cpp | 2 +- src/menu/reflection.hpp | 2 +- src/menu/software_mouse_pointer.cpp | 79 +++++++++++++++++++++++++++++++---- src/menu/software_mouse_pointer.hpp | 15 +++++-- src/utils/dimension.cpp | 16 +++++-- src/utils/dimension.hpp | 13 +++--- src/utils/rgb_color.cpp | 9 +++- src/utils/rgb_color.hpp | 1 + 23 files changed, 310 insertions(+), 79 deletions(-) (limited to 'src') diff --git a/src/base/image.cpp b/src/base/image.cpp index 9f849533..d1d944eb 100644 --- a/src/base/image.cpp +++ b/src/base/image.cpp @@ -33,7 +33,8 @@ namespace usdx log4cxx::LoggerPtr Image::log = log4cxx::Logger::getLogger("usdx.base.Image"); - ImageLoadException::ImageLoadException(std::string error) : error(error) + ImageLoadException::ImageLoadException(std::string error) : + error(error) { } @@ -72,7 +73,7 @@ namespace usdx } } - const SDL_Surface* Image::get_surface(void) + SDL_Surface* Image::get_surface(void) { if (surface == NULL) { load(); diff --git a/src/base/image.hpp b/src/base/image.hpp index 9b8c5871..9e9ea6e4 100644 --- a/src/base/image.hpp +++ b/src/base/image.hpp @@ -53,7 +53,7 @@ namespace usdx static log4cxx::LoggerPtr log; boost::filesystem::wpath filename; - SDL_Surface *surface; + SDL_Surface* surface; void load(void); @@ -61,7 +61,7 @@ namespace usdx Image(boost::filesystem::wpath filename); virtual ~Image(); - const SDL_Surface* get_surface(void); + SDL_Surface* get_surface(void); }; }; diff --git a/src/base/texture.cpp b/src/base/texture.cpp index ace2857f..1b9ed970 100644 --- a/src/base/texture.cpp +++ b/src/base/texture.cpp @@ -28,5 +28,86 @@ namespace usdx { - + log4cxx::LoggerPtr Texture::log = + log4cxx::Logger::getLogger("usdx.base.texture"); + + Texture::Texture(boost::filesystem::wpath filename) : + filename(filename), texture(0), size(0, 0), rotation(0) + { + Image image(filename); + + size.set_width(image.get_surface()->w); + size.set_height(image.get_surface()->h); + + // // Check that the image dimensions are a power of 2 + // if ((image.get_surface()->w & (image.get_surface()->w - 1)) != 0 ) { + // LOG4CXX_ERROR(log, L"" << filename << L" has a width of " << image.get_surface()->w << L" that is not a power of 2"); + // throw TextureSizeException(size); + // } + + // if ((image.get_surface()->h & (image.get_surface()->h - 1)) != 0 ) { + // LOG4CXX_ERROR(log, L"" << filename << L" has a height of " << image.get_surface()->h << L" that is not a power of 2"); + // throw TextureSizeException(size); + // } + + + switch (image.get_surface()->format->BytesPerPixel) { + case 4: + if (image.get_surface()->format->Rmask == 0x000000ff) { + texture_format = GL_RGBA; + } + else { + texture_format = GL_BGRA; + } + + break; + + case 3: + if (image.get_surface()->format->Rmask == 0x000000ff) { + texture_format = GL_RGB; + } + else { + texture_format = GL_BGR; + } + + break; + + default: + LOG4CXX_ERROR(log, L"" << filename << L" is not in true color! Could not handle that!"); + throw TextureColorDepthException(image.get_surface()->format->BytesPerPixel); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures(1, &texture); + + // Bind the texture object + glBindTexture(GL_TEXTURE_2D, texture); + + // Set the texture's stretching properties + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D(GL_TEXTURE_2D, + 0, + image.get_surface()->format->BytesPerPixel, + image.get_surface()->w, + image.get_surface()->h, + 0, + texture_format, + GL_UNSIGNED_BYTE, + image.get_surface()->pixels); + } + + Texture::~Texture() + { + if (glIsTexture(texture)) { + glDeleteTextures(1, &texture); + } + } + + GLuint Texture::get_texture(void) const + { + return texture; + } }; diff --git a/src/base/texture.hpp b/src/base/texture.hpp index 016bb307..008707d6 100644 --- a/src/base/texture.hpp +++ b/src/base/texture.hpp @@ -29,33 +29,61 @@ #include #include + #include "utils/point_3d.hpp" #include "utils/dimension.hpp" #include "utils/rectangle.hpp" +#include "image.hpp" +#include "drawable.hpp" namespace usdx { - class Texture + class TextureLoadException + { + public: + TextureLoadException() {}; + virtual ~TextureLoadException() {}; + }; + + class TextureSizeException : public TextureLoadException { private: - GLuint tex_num; - Point3D position; Dimension size; - Dimension scale; ///< for dynamic scaling - float rotation; ///< radiant (0 - 2*pi) + public: + TextureSizeException(Dimension size) : size(size) { }; + virtual ~TextureSizeException() { }; + }; + + class TextureColorDepthException : public TextureLoadException + { + private: + unsigned int number_of_colors; - Dimension tex; ///< percentage of size to use [0..1] - Rectangle tex_rect; + public: + TextureColorDepthException(unsigned int number_of_colors) : number_of_colors(number_of_colors) {}; + virtual ~TextureColorDepthException() {}; + }; + + class Texture + { + private: + static log4cxx::LoggerPtr log; - boost::filesystem::wpath filename; ///< experimental for - ///handling cache - ///images. maybe it's useful - ///for dynamic skins + boost::filesystem::wpath filename; + + GLuint texture; + GLenum texture_format; + + Dimension size; + + float rotation; ///< radiant (0 - 2*pi) public: - Texture(); + Texture(boost::filesystem::wpath filename); virtual ~Texture(); + + GLuint get_texture(void) const; }; }; diff --git a/src/menu/application.cpp b/src/menu/application.cpp index 48f1291c..e8cbde1a 100644 --- a/src/menu/application.cpp +++ b/src/menu/application.cpp @@ -28,7 +28,7 @@ #include "event_manager.hpp" #include #include "software_mouse_pointer.hpp" - +#include namespace usdx { @@ -74,21 +74,21 @@ namespace usdx return instance; } - void Application::draw(SDL_Surface* display) const + void Application::draw(void) { if (frame) { - frame->repaint(display); + frame->repaint(); } } - void Application::repaint(SDL_Surface* display) const { - DrawableControl::repaint(display); + void Application::repaint(void) { + DrawableControl::repaint(); for (std::list::const_iterator it = overlays.begin(); it != overlays.end(); it++) { - (*it)->repaint(display); + (*it)->repaint(); } } @@ -112,8 +112,8 @@ namespace usdx running = true; while (running) { // repaint everything - repaint(display); - SDL_Flip(display); + repaint(); + SDL_GL_SwapBuffers(); LOG4CXX_TRACE(log, L"repaint"); @@ -123,7 +123,6 @@ namespace usdx case SDL_QUIT: running = false; event_thread.interrupt(); - break; default: event_manager.add_event(event); @@ -143,11 +142,42 @@ namespace usdx void Application::run(void) { if (! display) { + // opengl settings + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + // center screen + char test[] = "SDL_VIDEO_CENTERED=center"; + SDL_putenv(test); + + // create screen display = SDL_SetVideoMode(display_width, display_height, 24, - SDL_SWSURFACE | - SDL_DOUBLEBUF); + SDL_OPENGL); + + glEnable( GL_TEXTURE_2D ); + + glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); + + glViewport( 0, 0, display_width, display_height ); + + glClear( GL_COLOR_BUFFER_BIT ); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_COLOR_MATERIAL); + + glMatrixMode( GL_PROJECTION ); + glLoadIdentity(); + + glOrtho(0.0f, display_width, display_height, 0.0f, -1.0f, 1.0f); + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); } if (! display) { diff --git a/src/menu/application.hpp b/src/menu/application.hpp index cc9ba0c2..914eda71 100644 --- a/src/menu/application.hpp +++ b/src/menu/application.hpp @@ -66,7 +66,7 @@ namespace usdx protected: Application(Control* parent); - void draw(SDL_Surface* display) const; + void draw(void); public: virtual ~Application(); @@ -84,7 +84,7 @@ namespace usdx const int get_frames_per_second(void) const; void set_frames_per_second(int fps); - void repaint(SDL_Surface* display) const; + void repaint(void); }; }; diff --git a/src/menu/container.cpp b/src/menu/container.cpp index edcd5e10..3bed894e 100644 --- a/src/menu/container.cpp +++ b/src/menu/container.cpp @@ -43,16 +43,16 @@ namespace usdx controls.clear(); } - void Container::repaint(SDL_Surface* display) const + void Container::repaint(void) { if (get_visible()) { - draw(display); + draw(); for (std::list::const_iterator it = controls.begin(); it != controls.end(); it++) { - (*it)->repaint(display); + (*it)->repaint(); } } } diff --git a/src/menu/container.hpp b/src/menu/container.hpp index 8ba9466c..6ee6bde2 100644 --- a/src/menu/container.hpp +++ b/src/menu/container.hpp @@ -27,7 +27,6 @@ #ifndef CONTAINER_HPP #define CONTAINER_HPP -#include #include #include "drawable_control.hpp" @@ -46,7 +45,7 @@ namespace usdx /** * Method for redraw all contained objects. */ - void repaint(SDL_Surface* display) const; + void repaint(void); }; }; diff --git a/src/menu/drawable.cpp b/src/menu/drawable.cpp index 0c68affc..37c42b25 100644 --- a/src/menu/drawable.cpp +++ b/src/menu/drawable.cpp @@ -32,10 +32,10 @@ namespace usdx { } - void Drawable::repaint(SDL_Surface* display) const + void Drawable::repaint(void) { if (visible) { - draw(display); + draw(); } }; diff --git a/src/menu/drawable.hpp b/src/menu/drawable.hpp index 28cbd223..bca0ac03 100644 --- a/src/menu/drawable.hpp +++ b/src/menu/drawable.hpp @@ -27,8 +27,6 @@ #ifndef DRAWABLE_HPP #define DRAWABLE_HPP -#include - namespace usdx { /** @@ -43,7 +41,7 @@ namespace usdx /** * Pure virtual method, that descendant classes have to implement. */ - virtual void draw(SDL_Surface* display) const = 0; + virtual void draw(void) = 0; public: Drawable(void); @@ -52,7 +50,7 @@ namespace usdx /** * Method for redraw this Object. If visible issues draw. */ - void repaint(SDL_Surface* display) const; + void repaint(void); /** * Setter for visible. diff --git a/src/menu/frame.cpp b/src/menu/frame.cpp index 228df767..08cc833a 100644 --- a/src/menu/frame.cpp +++ b/src/menu/frame.cpp @@ -56,8 +56,10 @@ namespace usdx return background; } - void Frame::draw(SDL_Surface* display) const + void Frame::draw(void) { - background->repaint(display); + if (background) { + background->repaint(); + } } }; diff --git a/src/menu/frame.hpp b/src/menu/frame.hpp index b337275c..55e0a4db 100644 --- a/src/menu/frame.hpp +++ b/src/menu/frame.hpp @@ -40,7 +40,7 @@ namespace usdx FrameBackground* background; protected: - virtual void draw(SDL_Surface* display) const; + virtual void draw(void); public: Frame(Control* parent); diff --git a/src/menu/frame_background.hpp b/src/menu/frame_background.hpp index a8e7daea..9cb7c504 100644 --- a/src/menu/frame_background.hpp +++ b/src/menu/frame_background.hpp @@ -39,19 +39,12 @@ namespace usdx { public: FrameBackground(); - - // optional methods virtual ~FrameBackground(); - virtual void on_show(void) {} - virtual void on_finish(void) {} - protected: // no copy and no assignment FrameBackground(const FrameBackground&); FrameBackground& operator=(const FrameBackground&); - - virtual void draw(SDL_Surface* display) = 0; }; }; diff --git a/src/menu/frame_background_color.cpp b/src/menu/frame_background_color.cpp index 18483e54..43b1c748 100644 --- a/src/menu/frame_background_color.cpp +++ b/src/menu/frame_background_color.cpp @@ -25,14 +25,23 @@ */ #include "frame_background_color.hpp" +#include namespace usdx { - FrameBackgroundColor::FrameBackgroundColor(RgbColor &color) : color(color) + FrameBackgroundColor::FrameBackgroundColor(RgbColor &color) : + color(color) { } - void FrameBackgroundColor::draw(SDL_Surface* display) const + FrameBackgroundColor::FrameBackgroundColor(void) : + color(0, 0, 0) { } + + void FrameBackgroundColor::draw() + { + glClearColor(color.get_red(), color.get_green(), color.get_blue(), 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } }; diff --git a/src/menu/frame_background_color.hpp b/src/menu/frame_background_color.hpp index a52e1f13..2e850d2c 100644 --- a/src/menu/frame_background_color.hpp +++ b/src/menu/frame_background_color.hpp @@ -27,8 +27,6 @@ #ifndef FRAME_BACKGROUND_COLOR_HPP #define FRAME_BACKGROUND_COLOR_HPP -#include - #include "frame_background.hpp" #include "utils/rgb_color.hpp" @@ -40,9 +38,10 @@ namespace usdx RgbColor color; protected: - virtual void draw(SDL_Surface* display) const; + void draw(void); public: + FrameBackgroundColor(void); FrameBackgroundColor(RgbColor &color); }; diff --git a/src/menu/reflection.cpp b/src/menu/reflection.cpp index 92ab01e1..1b453f5e 100644 --- a/src/menu/reflection.cpp +++ b/src/menu/reflection.cpp @@ -36,7 +36,7 @@ namespace usdx { } - void Reflection::draw(SDL_Surface* display) const + void Reflection::draw(void) { } diff --git a/src/menu/reflection.hpp b/src/menu/reflection.hpp index aa43491a..af0d70db 100644 --- a/src/menu/reflection.hpp +++ b/src/menu/reflection.hpp @@ -37,7 +37,7 @@ namespace usdx float spacing; protected: - virtual void draw(SDL_Surface* display) const; + virtual void draw(void); public: Reflection(float spacing); diff --git a/src/menu/software_mouse_pointer.cpp b/src/menu/software_mouse_pointer.cpp index 47e00047..1723ce9b 100644 --- a/src/menu/software_mouse_pointer.cpp +++ b/src/menu/software_mouse_pointer.cpp @@ -25,14 +25,55 @@ */ #include "software_mouse_pointer.hpp" -#include +#include +#include namespace usdx { SoftwareMousePointer::SoftwareMousePointer(Control* parent, EventManager* event_manager) : DrawableControl(parent), x(-1), y(-1) { - texture = new Image(L"game/themes/Deluxe/interface/cursor.png"); + this->vertices[0] = 0.0f; + this->vertices[1] = 40.0f; + this->vertices[2] = 0.0f; + this->vertices[3] = 40.0f; + this->vertices[4] = 40.0f; + this->vertices[5] = 0.0f; + this->vertices[6] = 40.0f; + this->vertices[7] = 0.0f; + this->vertices[8] = 0.0f; + this->vertices[9] = 0.0f; + this->vertices[10] = 0.0f; + this->vertices[11] = 0.0f; + + this->color[0] = 255; + this->color[1] = 255; + this->color[2] = 255; + this->color[3] = 100; + this->color[4] = 255; + this->color[5] = 255; + this->color[6] = 255; + this->color[7] = 100; + this->color[8] = 255; + this->color[9] = 255; + this->color[10] = 255; + this->color[11] = 100; + this->color[12] = 255; + this->color[13] = 255; + this->color[14] = 255; + this->color[15] = 100; + + this->texture[0] = 0.0f; + this->texture[1] = 1.0f; + this->texture[2] = 1.0f; + this->texture[3] = 1.0f; + this->texture[4] = 1.0f; + this->texture[5] = 0.0f; + this->texture[6] = 0.0f; + this->texture[7] = 0.0f; + + texture_normal = new Texture(L"game/themes/Deluxe/interface/cursor.png"); + texture_pressed = new Texture(L"game/themes/Deluxe/interface/cursor_pressed.png"); mouse_move_connection = event_manager->mouse_move.connect( boost::bind(&SoftwareMousePointer::on_mouse_move, this, _1, _2)); @@ -42,22 +83,44 @@ namespace usdx { mouse_move_connection.disconnect(); - if (texture != NULL) { - delete texture; - texture = NULL; + if (texture_normal != NULL) { + delete texture_normal; + texture_normal = NULL; } } - void SoftwareMousePointer::draw(SDL_Surface* display) const + void SoftwareMousePointer::draw(void) { + glLoadIdentity(); + + boost::mutex::scoped_lock lock(mutex); + if (x >= 0 && y >= 0) { - SDL_Rect rcDest = { x, y, 0, 0 }; - SDL_BlitSurface((SDL_Surface*)texture->get_surface(), NULL, display, &rcDest); + glTranslatef(x, y, 0.0f); + lock.unlock(); + + glBindTexture(GL_TEXTURE_2D, texture_normal->get_texture()); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + glVertexPointer(3, GL_FLOAT, 0, vertices); + glColorPointer(4, GL_UNSIGNED_BYTE, 0, color); + glTexCoordPointer(2, GL_FLOAT, 0, texture); + + glDrawArrays(GL_QUADS, 0, 4); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); } } void SoftwareMousePointer::on_mouse_move(int x, int y) { + boost::mutex::scoped_lock lock(mutex); + this->x = x; this->y = y; } diff --git a/src/menu/software_mouse_pointer.hpp b/src/menu/software_mouse_pointer.hpp index dad27543..3b29e50e 100644 --- a/src/menu/software_mouse_pointer.hpp +++ b/src/menu/software_mouse_pointer.hpp @@ -30,9 +30,11 @@ #include "drawable_control.hpp" #include "control.hpp" #include "event_manager.hpp" -#include "image.hpp" +#include "texture.hpp" #include +#include +#include namespace usdx { @@ -42,12 +44,19 @@ namespace usdx int x; int y; - Image* texture; + GLfloat vertices[12]; + GLubyte color[16]; + GLfloat texture[8]; + + Texture* texture_normal; + Texture* texture_pressed; boost::signals2::connection mouse_move_connection; + boost::mutex mutex; + protected: - void draw(SDL_Surface* display) const; + void draw(void); public: SoftwareMousePointer(Control* parent, EventManager* event_manager); diff --git a/src/utils/dimension.cpp b/src/utils/dimension.cpp index 11a1feb3..f83636ff 100644 --- a/src/utils/dimension.cpp +++ b/src/utils/dimension.cpp @@ -28,7 +28,7 @@ namespace usdx { - Dimension::Dimension(float width, float height) : + Dimension::Dimension(unsigned int width, unsigned int height) : width(width), height(height) { } @@ -38,13 +38,23 @@ namespace usdx { } - float Dimension::get_width(void) const + unsigned int Dimension::get_width(void) const { return width; } - float Dimension::get_height(void) const + void Dimension::set_width(unsigned int width) + { + this->width = width; + } + + unsigned int Dimension::get_height(void) const { return height; } + + void Dimension::set_height(unsigned int height) + { + this->height = height; + } }; diff --git a/src/utils/dimension.hpp b/src/utils/dimension.hpp index 656efefb..4518fbb8 100644 --- a/src/utils/dimension.hpp +++ b/src/utils/dimension.hpp @@ -32,14 +32,17 @@ namespace usdx class Dimension { private: - float width; - float height; + unsigned int width; + unsigned int height; public: - Dimension(float width, float height); + Dimension(unsigned int width, unsigned int height); Dimension(const Dimension& dimension); - float get_width(void) const; - float get_height(void) const; + unsigned int get_width(void) const; + void set_width(unsigned int width); + + unsigned int get_height(void) const; + void set_height(unsigned int height); }; }; diff --git a/src/utils/rgb_color.cpp b/src/utils/rgb_color.cpp index 04c10ba5..d470d495 100644 --- a/src/utils/rgb_color.cpp +++ b/src/utils/rgb_color.cpp @@ -28,8 +28,13 @@ namespace usdx { - RgbColor::RgbColor(float red, float green, float blue) : - red(red), green(green), blue(blue) + RgbColor::RgbColor(int red, int green, int blue) + : red(red / 255.0f), green(green / 255.0f), blue(blue / 255.0f) + { + } + + RgbColor::RgbColor(float red, float green, float blue) + : red(red), green(green), blue(blue) { } diff --git a/src/utils/rgb_color.hpp b/src/utils/rgb_color.hpp index f5b36e7a..dbc504cd 100644 --- a/src/utils/rgb_color.hpp +++ b/src/utils/rgb_color.hpp @@ -36,6 +36,7 @@ namespace usdx float green; float blue; public: + RgbColor(int red, int green, int blue); RgbColor(float red, float green, float blue); float get_red(void) const; -- cgit v1.2.3