Skip to content

Instantly share code, notes, and snippets.

@10se1ucgo
Created November 16, 2018 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 10se1ucgo/20da541d2525430c0550c486cf11e036 to your computer and use it in GitHub Desktop.
Save 10se1ucgo/20da541d2525430c0550c486cf11e036 to your computer and use it in GitHub Desktop.
hm2 act text generator prototype stuff
#include "scene/hm2.h"
#include "game_client.h"
#include "draw/sprite.h"
using namespace ace::gl::literals;
namespace ace { namespace scene {
HM2Font::HM2Font() {
this->vao.attrib_pointer("2f,2f,1f,4f,4f", this->vbo.handle);
SDL_Surface *image = draw::load_image("fntPartTitle_1.png").first;
this->tex.copy_from_surface(image);
this->tex.set_filter_mode(GL_NEAREST);
this->tex.set_wrap_mode(GL_CLAMP_TO_EDGE);
SDL_FreeSurface(image);
}
void HM2Font::draw(char c, glm::vec2 pos, glm::vec4 top_color, glm::vec4 bottom_color) {
constexpr float ratio = 1.f / 512.f;
const HM2Glyph glyph(this->glyphs[int(c)]);
const float tx = glyph.x * ratio;
const float ty = glyph.y * ratio;
const float tw = glyph.width * ratio;
const float th = glyph.height * ratio;
const float x = pos.x + glyph.xoffset;
const float y = pos.y + glyph.yoffset;
const float w = glyph.width;
const float h = glyph.height;
this->vbo->push_back({ {x, y}, { tx, ty }, 0.125, top_color, bottom_color });
this->vbo->push_back({ {x + w, y}, { tx + tw, ty }, 0.125, top_color, bottom_color });
this->vbo->push_back({ {x, y + h}, { tx, ty + th }, 0.875, top_color, bottom_color });
this->vbo->push_back({ {x + w, y}, { tx + tw, ty }, 0.125, top_color, bottom_color });
this->vbo->push_back({ {x, y + h}, { tx, ty + th }, 0.875, top_color, bottom_color });
this->vbo->push_back({ {x + w, y + h}, { tx + tw, ty + th }, 0.875, top_color, bottom_color });
}
void HM2Font::flush(const glm::mat4 &pv, gl::ShaderProgram &s) {
if (this->vbo->empty()) return;
glActiveTexture(GL_TEXTURE0);
this->tex.bind();
s.uniform("mvp", pv);
this->vbo.upload();
this->vao.draw(GL_TRIANGLES, this->vbo.draw_count, this->vbo.draw_offset);
}
HM2Text::HM2Text(GameClient &client) :
Scene(client),
projection_2d(glm::ortho<float>(0.f, 480, 192, 0.0f)),
color_time(0.f) {
}
void HM2Text::start() {
}
void HM2Text::update(double dt) {
Scene::update(dt);
this->draw_title("STFU MARK", { 430.39999f, 96.00000f });
this->color_time -= dt;
}
void HM2Text::draw_title(const std::string &title, glm::vec2 pos) {
double time = this->time;
for(auto c = title.rbegin(); c != title.rend(); ++c) {
pos.x -= this->font.glyphs[int(*c)].xadvance;
glm::vec2 position(pos);
float color_weight = glm::clamp(-3 * abs(this->color_time - 1/3.f) + 1, 0.f, 1.f);
int i;
for (i = 0; i < coswave(time * 1.5, 6, 13); i++) {
this->font.draw(*c, position, mix(glm::vec4(i / 13.f, 0, 0, 1), glm::vec4(draw::colors::random() * 0.65f, 1), color_weight));
position.x += 1;
position.y -= 1;
}
glm::vec4 col(mix(glm::vec3(i / 13.f, 0, 0), glm::vec3(draw::colors::random() * 0.75f), color_weight), 1);
this->font.draw(*c, position - glm::vec2{ 0.85f, 1.16f }, col);
this->font.draw(*c, position + glm::vec2{ 0.16f, -1.16f }, col);
this->font.draw(*c, position + glm::vec2{ 0.16f, -0.16f }, { 1, 1, 1, 1 }, col);
time += 0.1;
}
}
void HM2Text::draw() {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// 2d
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
this->client.shaders->sprite.bind();
this->client.shaders->sprite.uniform("projection", this->projection_2d);
this->client.sprites.flush(this->client.shaders->sprite);
this->client.shaders->hm2.bind();
this->font.flush(this->projection_2d, this->client.shaders->hm2);
}
void HM2Text::on_key(SDL_Scancode scancode, int modifiers, bool pressed) {
if (!pressed) return;
this->color_time = 0.75f;
}
}}
#version 330 core
in vec2 f_uv;
in float f_weight;
in vec4 f_top;
in vec4 f_bottom;
out vec4 frag_color;
uniform sampler2D tex;
void main() {
frag_color = vec4(1, 1, 1, texture(tex, f_uv).r) * mix(f_top, f_bottom, f_weight);
}
#pragma once
#include "scene/scene.h"
#include "gl/shader.h"
namespace ace { namespace scene {
struct HM2Glyph {
int x, y, width, height, xoffset, yoffset, xadvance;
};
#pragma pack(push, 1)
struct HM2Vert {
glm::vec2 pos, uv;
float color_weight;
glm::vec4 top_color, bottom_color;
};
#pragma pack(pop)
struct HM2Font {
HM2Font();
void draw(char c, glm::vec2 pos, glm::vec4 top_color, glm::vec4 bottom_color);
void draw(char c, glm::vec2 pos, glm::vec4 color) {
return this->draw(c, pos, color, color);
}
void flush(const glm::mat4 &pv, gl::ShaderProgram &s);
HM2Glyph glyphs[256];
gl::experimental::texture2d tex;
gl::experimental::vao vao;
gl::experimental::streaming_vbo<HM2Vert> vbo;
};
class HM2Text final : public Scene {
public:
HM2Text(GameClient &client);
void start() override;
void update(double dt) override;
void draw() override;
void draw_title(const std::string &title, glm::vec2 pos);
void on_key(SDL_Scancode scancode, int modifiers, bool pressed) override;
HM2Font font;
glm::mat4 projection_2d;
float color_time;
};
}}
#version 330 core
layout (location = 0) in vec2 position;
layout (location = 1) in vec2 uv;
layout (location = 2) in float color_weight;
layout (location = 3) in vec4 top_color;
layout (location = 4) in vec4 bottom_color;
out vec2 f_uv;
out float f_weight;
out vec4 f_top;
out vec4 f_bottom;
uniform mat4 mvp;
void main() {
gl_Position = mvp * vec4(position, 0, 1);
f_uv = uv;
f_weight = color_weight;
f_top = top_color;
f_bottom = bottom_color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment