Skip to content

Instantly share code, notes, and snippets.

@Shaptic
Shaptic / Font.cpp
Last active October 5, 2021 00:53
CFont::RenderText() - Renders a line of text at the given position in OpenGL.
rect_t CFont::RenderText(const std::string& text, const vector2_t& Pos)
{
// VAO and buffers that will contain our text vertex data.
uint32_t vao = 0, vbo = 0, ibo = 0;
// Track total text size.
rect_t Size(Pos.x, Pos.y, 0, 0);
if(!m_loaded || !CFont::s_loaded || text.empty()) return Size;
@Shaptic
Shaptic / FontRender.fs
Created February 4, 2013 06:35
Fragment shader for mono-chrome texture rendering.
#version 330 core
// Our texture
uniform sampler2D texture;
// Incoming attributes from the vertex shader
smooth in vec2 fs_texc;
smooth in vec4 fs_color;
// Output color
@Shaptic
Shaptic / Animation.hpp
Last active December 12, 2015 09:28
Animation class header
#ifndef ANIMATION_HPP
#define ANIMATION_HPP
// Framebuffer object
#include "Graphics/Framebuffer.hpp"
// Animation inherits rigid body entity
#include "Entity/RigidBody.hpp"
// For the timer
@Shaptic
Shaptic / Animation.cpp
Last active December 12, 2015 09:28
Animation loading implementation
#include "Entity/Animation.hpp"
bool CAnimation::LoadFromFile(const std::string& filename,
gfx::CVertexBuffer& VBO)
{
// Open the .icanim file.
std::ifstream anim(filename, std::ios::binary);
CAnimation::AnimationHeader& header = m_SheetDetails;
memset(&header, NULL, sizeof header);
@Shaptic
Shaptic / Animate.fs
Created February 10, 2013 22:14
Animation fragment shader
#version 330 core
uniform sampler2D texture;
// Individual sprite dimensions expressed in the proper texcoord range.
// For example, a 32x32 sprite would be <0.03125, 0.03125>
// This would then be interpolated from [0, 0.03125], properly rendering
// all of the texels in the desired sprite.
uniform vec2 tc_offset;
@Shaptic
Shaptic / Animate.fs
Last active December 12, 2015 09:28
#version 330 core
uniform sampler2D texture;
uniform vec2 tc_offset;
uniform vec2 tc_start;
smooth in vec2 fs_texc;
smooth in vec4 fs_color;
out vec4 out_color;
@Shaptic
Shaptic / Animation.cpp
Created February 10, 2013 22:23
Iterating over the current sprite sheet.
bool CAnimation::NextSprite()
{
// Stay within bounds!
if(++m_active >= this->GetAnimationCount())
{
++m_loops_done;
m_active = 0;
}
// Adjust for current texture
@Shaptic
Shaptic / Animation.cpp
Created February 10, 2013 22:31
Swap out sprite sheets on the fly.
void CAnimation::SwapSpriteSheet(const AnimationHeader& Header)
{
// New texture for the renderer and new dimensions for the shader.
m_Mesh.GetSurfaces()[0]->pMaterial->pTexture = Header.pTexture;
m_TexcDim = math::vector2_t(1.f / Header.columns, 1.f / Header.rows);
m_SheetDetails = Header;
m_loops_done = 0;
// Adjust shader for new texture dimensions.
gfx::CShaderPair* pShader = m_Mesh.GetSurfaces()[0]->pMaterial->pShader;
@Shaptic
Shaptic / QTreeNode.hpp
Last active December 15, 2015 11:59
Bare-bones quad tree node structure.
#include <list>
#include <cstdint>
// A generic object class (supporting collision) that is within the nodes.
// See here: https://github.com/Ruskiy69/IronClad/blob/master/Source/include/Entity/RigidBody.hpp
// For a real definition of the objects I use.
class CQuadTree;
class CEntity;
// This is a rectangle representation containing x, y position, and
@Shaptic
Shaptic / QuadTree.hpp
Last active December 15, 2015 12:08
Bare-bones QuadTree class.
// See this here: https://gist.github.com/Ruskiy69/5257421
#include "gists/QTreeNode.hpp"
class CQuadTree
{
public:
// Provide the starting dimensions to the root node.
// This will likely be something like Window::GetWidth()
// and Window::GetHeight() in a game.
CQuadTree(const uint16_t w, const uint16_t h);