From 3f8ebf3ccaebe2636724181be1c594721215f3df Mon Sep 17 00:00:00 2001 From: CobaltXII Date: Thu, 3 Jan 2019 19:35:55 -0500 Subject: [PATCH] Removed button.hpp and sprite.hpp temporarily --- src/inc/button.hpp | 43 ----------- src/inc/sprite.hpp | 184 --------------------------------------------- src/main.hpp | 6 +- 3 files changed, 1 insertion(+), 232 deletions(-) delete mode 100644 src/inc/button.hpp delete mode 100644 src/inc/sprite.hpp diff --git a/src/inc/button.hpp b/src/inc/button.hpp deleted file mode 100644 index 198b1dd..0000000 --- a/src/inc/button.hpp +++ /dev/null @@ -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); -} \ No newline at end of file diff --git a/src/inc/sprite.hpp b/src/inc/sprite.hpp deleted file mode 100644 index 19eca47..0000000 --- a/src/inc/sprite.hpp +++ /dev/null @@ -1,184 +0,0 @@ -#include - -// 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; -} \ No newline at end of file diff --git a/src/main.hpp b/src/main.hpp index ec9b2bb..4fa2684 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -60,8 +60,4 @@ #include -#include - -#include - -#include \ No newline at end of file +#include \ No newline at end of file