Removed button.hpp and sprite.hpp temporarily

This commit is contained in:
CobaltXII
2019-01-03 19:35:55 -05:00
parent f8cff3e410
commit 3f8ebf3cca
3 changed files with 1 additions and 232 deletions

View File

@@ -1,43 +0,0 @@
// States of buttons.
enum button_state
{
button_disabled,
button_default,
button_hover
};
// Generate, draw and destroy a button.
void draw_button(int x, int y, int x_res, int y_res, int width, int factor, button_state state, int mouse_x, int mouse_y)
{
if (state == button_default && width > 8 && mouse_x > x && mouse_x < x + width * factor && mouse_y > y && mouse_y < y + 20 * factor)
{
state = button_hover;
}
gpu_sprite* btn_0;
gpu_sprite* btn_1;
gpu_sprite* btn_2;
// The left bumper. The left bumper is a 2 by 20 rectangle that reflects
// the state of the button and is not affected by the button's width.
btn_0 = generate_rectangle_gpu_sprite(x, y, x + 2 * factor, y + 20 * factor, 0.0f, float(state) * 20.0f / 60.0f, 2.0f / 200.0f, (float(state) + 1.0f) * 20.0f / 60.0f, x_res, y_res, interface_textures.at("buttons")->storage);
// The right bumper. The right bumper is a 2 by 20 rectangle that reflects
// the state of the button and is not affected by the button's width.
btn_1 = generate_rectangle_gpu_sprite(x + (width - 2) * factor, y, x + width * factor, y + 20 * factor, 198.0f / 200.0f, float(state) * 20.0f / 60.0f, 1.0f, (float(state) + 1.0f) * 20.0f / 60.0f, x_res, y_res, interface_textures.at("buttons")->storage);
// The center block. The center block is a width - 4 by 20 rectangle that
// reflects the state of the button and is affected by the button's width.
btn_2 = generate_rectangle_gpu_sprite(x + 2 * factor, y, x + (width - 2) * factor, y + 20 * factor, 2.0f / 200.0f, float(state) * 20.0f / 60.0f, (float(width) - 2) / 200.0f, (float(state) + 1.0f) * 20.0f / 60.0f, x_res, y_res, interface_textures.at("buttons")->storage);
draw_destroy_gpu_sprite(btn_0);
draw_destroy_gpu_sprite(btn_1);
draw_destroy_gpu_sprite(btn_2);
}

View File

@@ -1,184 +0,0 @@
#include <iostream>
// you must call load_sprite_preliminaries() before doing any sprite based operations.
GLuint sprite_shader_program;
void load_sprite_preliminaries()
{
sprite_shader_program = load_program("../glsl/sprite_vertex.glsl", "../glsl/sprite_fragment.glsl");
}
// A structure that represents a sprite loaded to the CPU.
struct cpu_sprite
{
float* vertices;
unsigned int size_in_bytes;
unsigned int size_in_floats;
unsigned int size_in_vertices;
unsigned int offset;
};
// Allocate a chunk of memory (on the CPU) that can hold vertex_count vertices
// with 2-dimensional coordinates and 2-dimensional texture coordinates.
cpu_sprite* allocate_sprite(unsigned int vertex_count)
{
float* memory = (float*)malloc(vertex_count * 4 * sizeof(float));
if (!memory)
{
std::cout << "Could not allocate a vertex array for " << vertex_count << " vertices." << std::endl;
exit(20);
}
cpu_sprite* the_cpu_sprite = new cpu_sprite();
the_cpu_sprite->vertices = memory;
the_cpu_sprite->size_in_bytes = vertex_count * 4 * sizeof(float);
the_cpu_sprite->size_in_floats = vertex_count * 4;
the_cpu_sprite->size_in_vertices = vertex_count;
the_cpu_sprite->offset = 0;
return the_cpu_sprite;
}
// Write a vertex to a cpu_sprite.
inline void write_vertex(cpu_sprite* the_cpu_sprite, float x, float y, float xt, float yt)
{
the_cpu_sprite->vertices[the_cpu_sprite->offset++] = x;
the_cpu_sprite->vertices[the_cpu_sprite->offset++] = y;
the_cpu_sprite->vertices[the_cpu_sprite->offset++] = xt;
the_cpu_sprite->vertices[the_cpu_sprite->offset++] = yt;
}
// A structure that represents a sprite loaded to the GPU.
struct gpu_sprite
{
GLuint sprite_vao;
GLuint sprite_vbo;
GLuint sprite_texture;
unsigned int size_in_bytes;
unsigned int size_in_floats;
unsigned int size_in_vertices;
};
// Generate a gpu_sprite* from a cpu_sprite*.
gpu_sprite* make_gpu_sprite(cpu_sprite* the_cpu_sprite, GLuint the_texture)
{
gpu_sprite* the_gpu_sprite = new gpu_sprite();
the_gpu_sprite->sprite_texture = the_texture;
the_gpu_sprite->size_in_bytes = the_cpu_sprite->size_in_bytes;
the_gpu_sprite->size_in_floats = the_cpu_sprite->size_in_floats;
the_gpu_sprite->size_in_vertices = the_cpu_sprite->size_in_vertices;
GLuint the_vao;
GLuint the_vbo;
glGenVertexArrays(1, &the_vao);
glGenBuffers(1, &the_vbo);
glBindVertexArray(the_vao);
glBindBuffer(GL_ARRAY_BUFFER, the_vbo);
glBufferData(GL_ARRAY_BUFFER, the_cpu_sprite->size_in_bytes, the_cpu_sprite->vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(0 * sizeof(float)));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
the_gpu_sprite->sprite_vao = the_vao;
the_gpu_sprite->sprite_vbo = the_vbo;
free(the_cpu_sprite->vertices);
delete the_cpu_sprite;
return the_gpu_sprite;
}
// Draw and destroy a gpu_sprite*.
void draw_destroy_gpu_sprite(gpu_sprite* the_gpu_sprite)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(sprite_shader_program);
glBindVertexArray(the_gpu_sprite->sprite_vao);
glBindTexture(GL_TEXTURE_2D, the_gpu_sprite->sprite_texture);
glDrawArrays(GL_TRIANGLES, 0, the_gpu_sprite->size_in_vertices);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
glUseProgram(0);
glDeleteVertexArrays(1, &the_gpu_sprite->sprite_vao);
glDeleteBuffers(1, &the_gpu_sprite->sprite_vbo);
glDisable(GL_BLEND);
}
// Generate a rectangle sprite.
gpu_sprite* generate_rectangle_gpu_sprite(int x_tl, int y_tl, int x_br, int y_br, float x_ttl, float y_ttl, float x_tbr, float y_tbr, int x_res, int y_res, GLuint texture)
{
cpu_sprite* my_cpu_sprite = allocate_sprite(6);
float x_tlf = x_tl / float(x_res) * 2.0f - 1.0f;
float y_tlf = y_tl / float(y_res) * 2.0f - 1.0f;
float x_brf = x_br / float(x_res) * 2.0f - 1.0f;
float y_brf = y_br / float(y_res) * 2.0f - 1.0f;
write_vertex(my_cpu_sprite, x_tlf, -y_tlf, x_ttl, y_ttl);
write_vertex(my_cpu_sprite, x_tlf, -y_brf, x_ttl, y_tbr);
write_vertex(my_cpu_sprite, x_brf, -y_brf, x_tbr, y_tbr);
write_vertex(my_cpu_sprite, x_tlf, -y_tlf, x_ttl, y_ttl);
write_vertex(my_cpu_sprite, x_brf, -y_brf, x_tbr, y_tbr);
write_vertex(my_cpu_sprite, x_brf, -y_tlf, x_tbr, y_ttl);
gpu_sprite* my_gpu_sprite = make_gpu_sprite(my_cpu_sprite, texture);
return my_gpu_sprite;
}

View File

@@ -60,8 +60,4 @@
#include <file.hpp>
#include <hitbox.hpp>
#include <sprite.hpp>
#include <button.hpp>
#include <hitbox.hpp>