Skip to content

Commit

Permalink
Add a texture class
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkive committed Sep 27, 2018
1 parent 6a18968 commit 73caa6d
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
8 changes: 8 additions & 0 deletions projectfiles/VC14/wesnoth.vcxproj
Expand Up @@ -2530,6 +2530,13 @@
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)OGL\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\ogl\texture.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)OGL\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\ogl\utils.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)OGL\</ObjectFileName>
Expand Down Expand Up @@ -3878,6 +3885,7 @@
<ClInclude Include="..\..\src\multiplayer_error_codes.hpp" />
<ClInclude Include="..\..\src\network_asio.hpp" />
<ClInclude Include="..\..\src\ogl\context.hpp" />
<ClInclude Include="..\..\src\ogl\texture.hpp" />
<ClInclude Include="..\..\src\ogl\utils.hpp" />
<ClInclude Include="..\..\src\overlay.hpp" />
<ClInclude Include="..\..\src\pathfind\pathfind.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions projectfiles/VC14/wesnoth.vcxproj.filters
Expand Up @@ -1546,6 +1546,9 @@
<ClCompile Include="..\..\src\gui\dialogs\game_version_dialog.cpp">
<Filter>Gui\Dialogs</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ogl\texture.cpp">
<Filter>OGL</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\addon\client.hpp">
Expand Down Expand Up @@ -3005,6 +3008,9 @@
<ClInclude Include="..\..\src\gui\dialogs\game_version_dialog.hpp">
<Filter>Gui\Dialogs</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ogl\texture.hpp">
<Filter>OGL</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="..\..\src\tests\test_sdl_utils.hpp">
Expand Down
1 change: 1 addition & 0 deletions source_lists/libwesnoth_sdl
@@ -1,4 +1,5 @@
ogl/context.cpp
ogl/texture.cpp
ogl/utils.cpp
sdl/exception.cpp
sdl/rect.cpp
Expand Down
8 changes: 8 additions & 0 deletions src/ogl/context.cpp
Expand Up @@ -14,6 +14,7 @@
#include "ogl/context.hpp"

#include "log.hpp"
#include "ogl/texture.hpp"

#include <GL/glew.h>
#include <SDL.h>
Expand Down Expand Up @@ -54,6 +55,13 @@ void context::init(sdl::window* window)
throw std::runtime_error("error initializing GLEW");
}

int maxTextureSize;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
if(maxTextureSize < texture::MAX_DIMENSION) {
ERR_GL << "Too low texture size limit\n";
throw std::runtime_error("too low texture size limit");
}

// Print some information.
GLint profile;
glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &profile);
Expand Down
62 changes: 62 additions & 0 deletions src/ogl/texture.cpp
@@ -0,0 +1,62 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "ogl/texture.hpp"

#include <SDL.h>

#include <cassert>
#include <stdexcept>

namespace gl
{

std::pair<int, int> texture::get_size() const
{
std::pair<int, int> size;
glBindTexture(GL_TEXTURE_2D, name_);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &size.first);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &size.second);
glBindTexture(GL_TEXTURE_2D, 0u);
return size;
}

void texture::set_size(const std::pair<int, int>& size)
{
if(size.first > MAX_DIMENSION || size.second > MAX_DIMENSION) {
throw std::invalid_argument("Too large texture size");
}

glBindTexture(GL_TEXTURE_2D, name_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, size.first, size.second, 0,
GL_BGRA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0u);
}

void texture::set_pixels(surface pixels)
{
assert(check_format(*pixels->format));
surface_locker<surface> lock(pixels);

glBindTexture(GL_TEXTURE_2D, name_);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pixels->w, pixels->h,
GL_BGRA, GL_UNSIGNED_BYTE, lock.pixels());
glBindTexture(GL_TEXTURE_2D, 0u);
}

bool texture::check_format(const SDL_PixelFormat& format)
{
return format.format == SDL_PIXELFORMAT_ARGB8888;
}

}
80 changes: 80 additions & 0 deletions src/ogl/texture.hpp
@@ -0,0 +1,80 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#pragma once

#include "sdl/surface.hpp"

#include <GL/glew.h>

#include <utility>

namespace gl
{
/// Thin wrapper for textures.
/// All textures are two-dimensional and in the RGBA8 format.
class texture
{
public:
static const int MAX_DIMENSION = 8192;

/** Constructor.
Initial texture size is 0x0. Use set_size() to actually allocate memory for
the texture. */
texture()
{
glGenTextures(1, &name_);
// Bind the texture as 2D to make it two-dimensional.
glBindTexture(GL_TEXTURE_2D, name_);
// Unbind the 2D texture target.
glBindTexture(GL_TEXTURE_2D, 0u);
}

/// Destructor.
~texture()
{
glDeleteTextures(1, &name_);
}

/** @return the OpenGL texture name.
It can be passed directly to the OpenGL API.
The name of a texture does not change and can be safely cached.
*/
GLuint get_name() const
{
return name_;
}

/// @return the size of the texture.
std::pair<int, int> get_size() const;

/** Resizes the texture.
@param size Desired texture size.
Invalidates existing content of the texture, if any.
@note Maximum texture size is 8192x8192. It's enforced both ways: Wesnoth refuses
to launch if the hardware doesn't support 8192x8192 textures, but it doesn't allow
larger textures even if the hardware supports them. */
void set_size(const std::pair<int, int>& size);

/** Uploads the provided texture data.
@param pixels Texture data. */
void set_pixels(surface pixels);

private:
/// OpenGL texture name.
GLuint name_;

/// Checks that the SDL surface format matches the format we expect.
static bool check_format(const SDL_PixelFormat& format);
};
}

0 comments on commit 73caa6d

Please sign in to comment.