Skip to content

Commit

Permalink
STARK: Fully separate bitmaps and textures
Browse files Browse the repository at this point in the history
  • Loading branch information
ccawley2011 authored and bluegr committed Jan 15, 2023
1 parent 63a2dc4 commit 5bd0ec4
Show file tree
Hide file tree
Showing 66 changed files with 473 additions and 360 deletions.
70 changes: 70 additions & 0 deletions engines/stark/gfx/bitmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef STARK_GFX_BITMAP_H
#define STARK_GFX_BITMAP_H

#include "common/hash-str.h"

namespace Graphics {
struct Surface;
}

namespace Stark {
namespace Gfx {

/**
* An abstract bitmap
*/
class Bitmap {
public:
Bitmap() : _width(0), _height(0) {}
virtual ~Bitmap() {}

enum SamplingFilter {
kNearest,
kLinear
};

/** Make the texture active */
virtual void bind() const = 0;

/** Define or update the texture pixel data */
virtual void update(const Graphics::Surface *surface, const byte *palette = nullptr) = 0;

/** Set the filter used when sampling the texture */
virtual void setSamplingFilter(SamplingFilter filter) = 0;

/** Get the texture width */
uint32 width() const { return _width; }

/** Get the texture height */
uint32 height() const { return _height; }

protected:
uint32 _width;
uint32 _height;
};

} // End of namespace Gfx
} // End of namespace Stark

#endif // STARK_GFX_BITMAP_H
7 changes: 4 additions & 3 deletions engines/stark/gfx/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace Gfx {

class SurfaceRenderer;
class FadeRenderer;
class Bitmap;
class Texture;

class Driver {
Expand Down Expand Up @@ -67,15 +68,15 @@ class Driver {
* The caller is responsible for freeing it.
*
*/
virtual Texture *createTexture(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) = 0;
virtual Texture *createTexture() = 0;

/**
* Create a new texture for 2D
* Create a new bitmap for 2D
*
* The caller is responsible for freeing it.
*
*/
virtual Texture *createBitmap(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) = 0;
virtual Bitmap *createBitmap(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) = 0;

/**
* Create a new actor renderer
Expand Down
17 changes: 9 additions & 8 deletions engines/stark/gfx/opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#if defined(USE_OPENGL_GAME)

#include "engines/stark/gfx/openglactor.h"
#include "engines/stark/gfx/openglbitmap.h"
#include "engines/stark/gfx/openglprop.h"
#include "engines/stark/gfx/openglsurface.h"
#include "engines/stark/gfx/openglfade.h"
Expand Down Expand Up @@ -188,18 +189,18 @@ void OpenGLDriver::setupLights(const LightEntryArray &lights) {
}
}

Texture *OpenGLDriver::createTexture(const Graphics::Surface *surface, const byte *palette) {
OpenGlTexture *texture = new OpenGlTexture();
Texture *OpenGLDriver::createTexture() {
return new OpenGlTexture();
}

Bitmap *OpenGLDriver::createBitmap(const Graphics::Surface *surface, const byte *palette) {
OpenGlBitmap *bitmap = new OpenGlBitmap();

if (surface) {
texture->update(surface, palette);
bitmap->update(surface, palette);
}

return texture;
}

Texture *OpenGLDriver::createBitmap(const Graphics::Surface *surface, const byte *palette) {
return createTexture(surface, palette);
return bitmap;
}

VisualActor *OpenGLDriver::createActorRenderer() {
Expand Down
4 changes: 2 additions & 2 deletions engines/stark/gfx/opengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class OpenGLDriver : public Driver {
void clearScreen() override;
void flipBuffer() override;

Texture *createTexture(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
Texture *createBitmap(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
Texture *createTexture() override;
Bitmap *createBitmap(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
VisualActor *createActorRenderer() override;
VisualProp *createPropRenderer() override;
SurfaceRenderer *createSurfaceRenderer() override;
Expand Down
96 changes: 96 additions & 0 deletions engines/stark/gfx/openglbitmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "engines/stark/gfx/openglbitmap.h"

#include "engines/stark/gfx/driver.h"

#include "graphics/surface.h"

#if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)

#include "graphics/opengl/context.h"

namespace Stark {
namespace Gfx {

OpenGlBitmap::OpenGlBitmap() :
Bitmap(),
_id(0) {
glGenTextures(1, &_id);

bind();

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}

OpenGlBitmap::~OpenGlBitmap() {
glDeleteTextures(1, &_id);
}

void OpenGlBitmap::bind() const {
glBindTexture(GL_TEXTURE_2D, _id);
}

void OpenGlBitmap::update(const Graphics::Surface *surface, const byte *palette) {
bind();

_width = surface->w;
_height = surface->h;

if (surface->format.bytesPerPixel != 4) {
// Convert the surface to texture format
Graphics::Surface *convertedSurface = surface->convertTo(Driver::getRGBAPixelFormat(), palette);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, convertedSurface->w, convertedSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, convertedSurface->getPixels());

convertedSurface->free();
delete convertedSurface;
} else {
assert(surface->format == Driver::getRGBAPixelFormat());

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->getPixels());
}
}

void OpenGlBitmap::setSamplingFilter(Bitmap::SamplingFilter filter) {
switch (filter) {
case kNearest:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
break;
case kLinear:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
break;
default:
warning("Unhandled sampling filter %d", filter);
}
}

} // End of namespace Gfx
} // End of namespace Stark

#endif // defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)
56 changes: 56 additions & 0 deletions engines/stark/gfx/openglbitmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef STARK_GFX_OPENGL_BITMAP_H
#define STARK_GFX_OPENGL_BITMAP_H

#include "engines/stark/gfx/bitmap.h"

#include "graphics/opengl/system_headers.h"

#if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)

namespace Stark {
namespace Gfx {

/**
* An OpenGL texture wrapper for 2D elements
*/
class OpenGlBitmap : public Bitmap {
public:
OpenGlBitmap();
virtual ~OpenGlBitmap();

// Bitmap API
void bind() const override;
void update(const Graphics::Surface *surface, const byte *palette = nullptr) override;
void setSamplingFilter(SamplingFilter filter) override;

protected:
GLuint _id;
};

} // End of namespace Gfx
} // End of namespace Stark

#endif // defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)

#endif // STARK_GFX_OPENGL_BITMAP_H
17 changes: 9 additions & 8 deletions engines/stark/gfx/opengls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#if defined(USE_OPENGL_SHADERS)

#include "engines/stark/gfx/openglsactor.h"
#include "engines/stark/gfx/openglbitmap.h"
#include "engines/stark/gfx/openglsprop.h"
#include "engines/stark/gfx/openglssurface.h"
#include "engines/stark/gfx/openglsfade.h"
Expand Down Expand Up @@ -130,18 +131,18 @@ void OpenGLSDriver::flipBuffer() {
g_system->updateScreen();
}

Texture *OpenGLSDriver::createTexture(const Graphics::Surface *surface, const byte *palette) {
OpenGlTexture *texture = new OpenGlTexture();
Texture *OpenGLSDriver::createTexture() {
return new OpenGlTexture();
}

Bitmap *OpenGLSDriver::createBitmap(const Graphics::Surface *surface, const byte *palette) {
OpenGlBitmap *bitmap = new OpenGlBitmap();

if (surface) {
texture->update(surface, palette);
bitmap->update(surface, palette);
}

return texture;
}

Texture *OpenGLSDriver::createBitmap(const Graphics::Surface *surface, const byte *palette) {
return createTexture(surface, palette);
return bitmap;
}

VisualActor *OpenGLSDriver::createActorRenderer() {
Expand Down
4 changes: 2 additions & 2 deletions engines/stark/gfx/opengls.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class OpenGLSDriver : public Driver {
void clearScreen() override;
void flipBuffer() override;

Texture *createTexture(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
Texture *createBitmap(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
Texture *createTexture() override;
Bitmap *createBitmap(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
VisualActor *createActorRenderer() override;
VisualProp *createPropRenderer() override;
SurfaceRenderer *createSurfaceRenderer() override;
Expand Down
10 changes: 5 additions & 5 deletions engines/stark/gfx/openglssurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "engines/stark/gfx/openglssurface.h"

#include "engines/stark/gfx/opengls.h"
#include "engines/stark/gfx/texture.h"
#include "engines/stark/gfx/bitmap.h"

#if defined(USE_OPENGL_SHADERS)

Expand All @@ -41,11 +41,11 @@ OpenGLSSurfaceRenderer::~OpenGLSSurfaceRenderer() {
delete _shader;
}

void OpenGLSSurfaceRenderer::render(const Texture *texture, const Common::Point &dest) {
render(texture, dest, texture->width(), texture->height());
void OpenGLSSurfaceRenderer::render(const Bitmap *bitmap, const Common::Point &dest) {
render(bitmap, dest, bitmap->width(), bitmap->height());
}

void OpenGLSSurfaceRenderer::render(const Texture *texture, const Common::Point &dest, uint width, uint height) {
void OpenGLSSurfaceRenderer::render(const Bitmap *bitmap, const Common::Point &dest, uint width, uint height) {
// Destination rectangle with given width and height
_gfx->start2DMode();

Expand All @@ -62,7 +62,7 @@ void OpenGLSSurfaceRenderer::render(const Texture *texture, const Common::Point
Common::Rect nativeViewport = _gfx->getViewport();
_shader->setUniform("viewport", Math::Vector2d(nativeViewport.width(), nativeViewport.height()));

texture->bind();
bitmap->bind();
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

_shader->unbind();
Expand Down
6 changes: 3 additions & 3 deletions engines/stark/gfx/openglssurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Stark {
namespace Gfx {

class OpenGLSDriver;
class Texture;
class Bitmap;

/**
* An programmable pipeline OpenGL surface renderer
Expand All @@ -47,8 +47,8 @@ class OpenGLSSurfaceRenderer : public SurfaceRenderer {
virtual ~OpenGLSSurfaceRenderer();

// SurfaceRenderer API
void render(const Texture *texture, const Common::Point &dest) override;
void render(const Texture *texture, const Common::Point &dest, uint width, uint height) override;
void render(const Bitmap *bitmap, const Common::Point &dest) override;
void render(const Bitmap *bitmap, const Common::Point &dest, uint width, uint height) override;

private:
Math::Vector2d normalizeOriginalCoordinates(int x, int y) const;
Expand Down
Loading

0 comments on commit 5bd0ec4

Please sign in to comment.