Skip to content

Commit

Permalink
Add sprite, draw_op and vertex classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkive committed Apr 28, 2018
1 parent 6422be3 commit 4f5c866
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
4 changes: 4 additions & 0 deletions projectfiles/VC14/wesnoth.vcxproj
Expand Up @@ -2551,6 +2551,7 @@
<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\sprite.cpp" />
<ClCompile Include="..\..\src\ogl\texture.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)OGL\</ObjectFileName>
Expand Down Expand Up @@ -3909,8 +3910,11 @@
<ClInclude Include="..\..\src\multiplayer_error_codes.hpp" />
<ClInclude Include="..\..\src\network_asio.hpp" />
<ClInclude Include="..\..\src\ogl\context.hpp" />
<ClInclude Include="..\..\src\ogl\draw_op.hpp" />
<ClInclude Include="..\..\src\ogl\sprite.hpp" />
<ClInclude Include="..\..\src\ogl\texture.hpp" />
<ClInclude Include="..\..\src\ogl\utils.hpp" />
<ClInclude Include="..\..\src\ogl\vertex.hpp" />
<ClInclude Include="..\..\src\overlay.hpp" />
<ClInclude Include="..\..\src\pathfind\pathfind.hpp" />
<ClInclude Include="..\..\src\pathfind\teleport.hpp" />
Expand Down
12 changes: 12 additions & 0 deletions projectfiles/VC14/wesnoth.vcxproj.filters
Expand Up @@ -1558,6 +1558,9 @@
<ClCompile Include="..\..\src\ogl\texture.cpp">
<Filter>OGL</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ogl\sprite.cpp">
<Filter>OGL</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\addon\client.hpp">
Expand Down Expand Up @@ -3025,6 +3028,15 @@
<ClInclude Include="..\..\src\ogl\texture.hpp">
<Filter>OGL</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ogl\vertex.hpp">
<Filter>OGL</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ogl\draw_op.hpp">
<Filter>OGL</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ogl\sprite.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/sprite.cpp
ogl/texture.cpp
ogl/utils.cpp
sdl/exception.cpp
Expand Down
32 changes: 32 additions & 0 deletions src/ogl/draw_op.hpp
@@ -0,0 +1,32 @@
/*
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 "ogl/vertex.hpp"

#include <GL/glew.h>

#include <array>

namespace gl
{
/** Represents a single draw operation.
The shape is always a triangle. */
struct draw_op
{
std::array<vertex, 3> vertices;
GLuint texture_name;
};

}
30 changes: 30 additions & 0 deletions src/ogl/sprite.cpp
@@ -0,0 +1,30 @@
/*
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/sprite.hpp"

namespace gl
{

void sprite::normalize_coordinates(const std::pair<int, int>& texture_size)
{
x_min = static_cast<float>(x_min_px) / static_cast<float>(texture_size.first);
x_max = static_cast<float>(x_max_px) / static_cast<float>(texture_size.first);
y_min = static_cast<float>(y_min_px) / static_cast<float>(texture_size.second);
y_max = static_cast<float>(y_max_px) / static_cast<float>(texture_size.second);

half_width = (x_max_px - x_min_px) / 2.0f;
half_height = (y_max_px - y_min_px) / 2.0f;
}

}
111 changes: 111 additions & 0 deletions src/ogl/sprite.hpp
@@ -0,0 +1,111 @@
/*
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 "ogl/draw_op.hpp"
#include "ogl/texture.hpp"

#include <GL/glew.h>

#include <utility>

namespace gl
{

class sprite
{
public:
/** Texture name. */
const GLuint texture_name;

/** X coordinate of left edge. Normalized to the range [0, 1]. */
float x_min;
/** X coordinate of right edge. Normalized to the range [0, 1]. */
float x_max;
/** Y coordinate of bottom edge. Normalized to the range [0, 1]. */
float y_min;
/** Y coordinate of top edge. Normalized to the range [0, 1]. */
float y_max;

/** X coordinate of left edge as pixels. */
unsigned int x_min_px;
/** X coordinate of right edge as pixels. */
unsigned int x_max_px;
/** Y coordinate of bottom edge as pixels. */
unsigned int y_min_px;
/** Y coordinate of top edge as pixels. */
unsigned int y_max_px;

/** Half of the width of the sprite, in pixels. */
float half_width;

/** Half of the height of the sprite, in pixels. */
float half_height;

/** Constructor. */
sprite(const texture& tex) : texture_name(tex.get_name())
{

}

/** Calculates normalized texture coordinates from pixel coordinates and texture size. */
void normalize_coordinates(const std::pair<int, int>& texture_size);

/** Generates draw operations which draw the sprite.
The sprite is anchored to center.
@param x X coordinate for center of the sprite.
@param y Y coordinate for center of the sprite.
@param out An output iterator for the container where the draw operations will be pushed.
@todo Scaling support. */
template<typename T>
void to_draw_ops(float x, float y, T out) const
{
draw_op op;
op.texture_name = texture_name;

op.vertices[0].x = x - half_width;
op.vertices[0].y = y - half_height;
op.vertices[1].x = x + half_width;
op.vertices[1].y = y - half_height;
op.vertices[2].x = x + half_width;
op.vertices[2].y = y + half_height;
op.vertices[0].u = x_min;
op.vertices[0].v = y_min;
op.vertices[1].u = x_max;
op.vertices[1].v = y_min;
op.vertices[2].u = x_max;
op.vertices[2].v = y_max;

// Assigning to the iterator pushes the draw operation.
// It pushes a copy, allowing us to modify the original.
out = op;

op.vertices[0].x = x - half_width;
op.vertices[0].y = y - half_height;
op.vertices[1].x = x + half_width;
op.vertices[1].y = y + half_height;
op.vertices[2].x = x - half_width;
op.vertices[2].y = y + half_height;
op.vertices[0].u = x_min;
op.vertices[0].v = y_min;
op.vertices[1].u = x_max;
op.vertices[1].v = y_max;
op.vertices[2].u = x_min;
op.vertices[2].v = y_max;

out = op;
}
};

}
31 changes: 31 additions & 0 deletions src/ogl/vertex.hpp
@@ -0,0 +1,31 @@
/*
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 <GL/glew.h>

namespace gl
{

struct vertex
{
// Vertex coordinates
GLfloat x;
GLfloat y;
// Texture coordinates
GLfloat u;
GLfloat v;
};

}

0 comments on commit 4f5c866

Please sign in to comment.