From: = <=> Date: Wed, 30 Oct 2024 22:47:32 +0000 (-0700) Subject: Added hud.cpp X-Git-Url: https://isoptera.lcsc.edu/gitweb/?a=commitdiff_plain;h=5ddbbd482b5e80841883f2168bed6097b7e54e97;p=example_engine%2F.git Added hud.cpp --- diff --git a/Makefile b/Makefile index 94730d8..7e26100 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS = main.o stb_image.o helpers.o tiny_obj_loader.o baseclass.o old_objects.o geometric_objects.o +OBJS = main.o stb_image.o helpers.o tiny_obj_loader.o baseclass.o old_objects.o geometric_objects.o hud.o CXXFLAGS = -g -Wall LDFLAGS = -lGL -lglfw -lGLEW -pthread -g diff --git a/hud.cpp b/hud.cpp new file mode 100644 index 0000000..c46d2b8 --- /dev/null +++ b/hud.cpp @@ -0,0 +1,146 @@ +#include "game.h" +#include "hud.h" +#include + +int aimpoint::init(){ + float vertices[] { + -.2, .002, 0, + .2, .002, 0, + .2, -.002, 0, + -.2, .002, 0, + .2, -.002, 0, + -.2, -.002, 0, + + -.002, .2, 0, + .002, .2, 0, + .002, -.2, 0, + -.002, .2, 0, + .002, -.2, 0, + -.002, -.2, 0, + }; + glGenBuffers(1, &vbuf); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbuf); + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + program = make_program("aimpoint_vertex_shader.glsl",0, 0, 0, "aimpoint_fragment_shader.glsl"); + if (!program) + return 1; + v_attrib = glGetAttribLocation(program, "in_vertex"); + return 0; +} + +void aimpoint::draw(glm::mat4 vp){ + glUseProgram(program); + glEnableVertexAttribArray(v_attrib); + glBindBuffer(GL_ARRAY_BUFFER, vbuf); + glVertexAttribPointer(v_attrib, 3, GL_FLOAT, GL_FALSE, 0, 0); + glDrawArrays(GL_TRIANGLES, 0, 12); +} + +void hud::set_char(int col, int line, char c){ + float position = c - 32; + position /= 95; + size_t cstart = (col + line * char_width) * 12; + texcoords[cstart + 0] = position; + texcoords[cstart + 1] = 1.0f; + texcoords[cstart + 2] = position + 1.0f/95; + texcoords[cstart + 3] = 1.0f; + texcoords[cstart + 4] = position; + texcoords[cstart + 5] = 0.0f; + texcoords[cstart + 6] = position + 1.0f/95; + texcoords[cstart + 7] = 1.0f; + texcoords[cstart + 8] = position + 1.0f/95; + texcoords[cstart + 9] = 0.0f; + texcoords[cstart + 10] = position; + texcoords[cstart + 11] = 0.0f; +} + +void hud::lprintf(int line, const char *fmt, ...){ + va_list args; + va_start(args, fmt); + char text[100]; + vsprintf(text, fmt, args); + set_text_line(line, text); +} + +void hud::set_text_line(int line, const char* text){ + for(int i = line * 1200; i < (line + 1) * 1200; i++) + texcoords[i] =0; + int col = 0; + for(; *text; text++){ + set_char(col, line, *text); + col++; + } +} + +void hud::set_text(const char* text){ + for(int i = 0; i < 3600; i++) + texcoords[i] = 0; + int col = 0, line = 0; + for(; *text; text++) + if(*text != '\n'){ + set_char(col, line, *text); + col++; + } else { + col = 0; + line++; + } +} + +int hud::init(){ + std::vector vertices; + /* 100 character display, 3 lines */ + for(float y = y_space; y >= -y_space; y -= y_space) + for(int i = 0; i < char_width; i++){ + vertices.push_back(0.01f * i); + vertices.push_back(y); + + vertices.push_back(0.01f * (i + 1)); + vertices.push_back(y); + + vertices.push_back(0.01f * i); + vertices.push_back(y - y_space); + + vertices.push_back(0.01f * (i + 1)); + vertices.push_back(y); + + vertices.push_back(0.01f * (i + 1)); + vertices.push_back(y - y_space); + + vertices.push_back(0.01f * i); + vertices.push_back(y - y_space); + + } + glGenBuffers(1, &vbuf); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbuf); + glBufferData(GL_SHADER_STORAGE_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW); + + for(int i = 0; i < char_width * 3; i++) + set_char(i % char_width, i / char_width, '#'); + glGenBuffers(1, &cbuf); + + tex = load_texture("letters.png"); + + program = make_program("hud_vertex_shader.glsl", 0, 0, 0, "hud_fragment_shader.glsl"); + if(!program) + return 1; + v_attrib = glGetAttribLocation(program, "in_vertex"); + t_attrib = glGetAttribLocation(program, "in_texcoord"); + return 0; +} + +void hud::draw(glm::mat4 vp){ + glUseProgram(program); + glEnableVertexAttribArray(v_attrib); + glBindBuffer(GL_ARRAY_BUFFER, vbuf); + glVertexAttribPointer(v_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0); + + glBindBuffer(GL_SHADER_STORAGE_BUFFER, cbuf); + glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW); + glEnableVertexAttribArray(t_attrib); + glBindBuffer(GL_ARRAY_BUFFER, cbuf); + glVertexAttribPointer(t_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, tex); + + glDrawArrays(GL_TRIANGLES, 0, 1800); +} diff --git a/hud.h b/hud.h index ad669bc..dad38bf 100644 --- a/hud.h +++ b/hud.h @@ -1,40 +1,9 @@ -#include +#pragma once class aimpoint : public gameobject { public: - int init(){ - float vertices[] { - -.2, .002, 0, - .2, .002, 0, - .2, -.002, 0, - -.2, .002, 0, - .2, -.002, 0, - -.2, -.002, 0, - - -.002, .2, 0, - .002, .2, 0, - .002, -.2, 0, - -.002, .2, 0, - .002, -.2, 0, - -.002, -.2, 0, - }; - glGenBuffers(1, &vbuf); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbuf); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - program = make_program("aimpoint_vertex_shader.glsl",0, 0, 0, "aimpoint_fragment_shader.glsl"); - if (!program) - return 1; - v_attrib = glGetAttribLocation(program, "in_vertex"); - return 0; - } - - void draw(glm::mat4 vp){ - glUseProgram(program); - glEnableVertexAttribArray(v_attrib); - glBindBuffer(GL_ARRAY_BUFFER, vbuf); - glVertexAttribPointer(v_attrib, 3, GL_FLOAT, GL_FALSE, 0, 0); - glDrawArrays(GL_TRIANGLES, 0, 12); - } + int init(); + void draw(glm::mat4 vp); }; class hud : public gameobject { @@ -42,112 +11,11 @@ class hud : public gameobject { float texcoords[3600]; int char_width = 100; float y_space = 0.02f; - void set_char(int col, int line, char c){ - float position = c - 32; - position /= 95; - size_t cstart = (col + line * char_width) * 12; - texcoords[cstart + 0] = position; - texcoords[cstart + 1] = 1.0f; - texcoords[cstart + 2] = position + 1.0f/95; - texcoords[cstart + 3] = 1.0f; - texcoords[cstart + 4] = position; - texcoords[cstart + 5] = 0.0f; - texcoords[cstart + 6] = position + 1.0f/95; - texcoords[cstart + 7] = 1.0f; - texcoords[cstart + 8] = position + 1.0f/95; - texcoords[cstart + 9] = 0.0f; - texcoords[cstart + 10] = position; - texcoords[cstart + 11] = 0.0f; - } - - void lprintf(int line, const char *fmt, ...){ - va_list args; - va_start(args, fmt); - char text[100]; - vsprintf(text, fmt, args); - set_text_line(line, text); - } - - void set_text_line(int line, const char* text){ - for(int i = line * 1200; i < (line + 1) * 1200; i++) - texcoords[i] =0; - int col = 0; - for(; *text; text++){ - set_char(col, line, *text); - col++; - } - } - - void set_text(const char* text){ - for(int i = 0; i < 3600; i++) - texcoords[i] = 0; - int col = 0, line = 0; - for(; *text; text++) - if(*text != '\n'){ - set_char(col, line, *text); - col++; - } else { - col = 0; - line++; - } - } - - int init(){ - std::vector vertices; - /* 100 character display, 3 lines */ - for(float y = y_space; y >= -y_space; y -= y_space) - for(int i = 0; i < char_width; i++){ - vertices.push_back(0.01f * i); - vertices.push_back(y); - - vertices.push_back(0.01f * (i + 1)); - vertices.push_back(y); - - vertices.push_back(0.01f * i); - vertices.push_back(y - y_space); - - vertices.push_back(0.01f * (i + 1)); - vertices.push_back(y); - - vertices.push_back(0.01f * (i + 1)); - vertices.push_back(y - y_space); - - vertices.push_back(0.01f * i); - vertices.push_back(y - y_space); - - } - glGenBuffers(1, &vbuf); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbuf); - glBufferData(GL_SHADER_STORAGE_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW); - - for(int i = 0; i < char_width * 3; i++) - set_char(i % char_width, i / char_width, '#'); - glGenBuffers(1, &cbuf); - - tex = load_texture("letters.png"); - program = make_program("hud_vertex_shader.glsl", 0, 0, 0, "hud_fragment_shader.glsl"); - if(!program) - return 1; - v_attrib = glGetAttribLocation(program, "in_vertex"); - t_attrib = glGetAttribLocation(program, "in_texcoord"); - return 0; - } - - void draw(glm::mat4 vp){ - glUseProgram(program); - glEnableVertexAttribArray(v_attrib); - glBindBuffer(GL_ARRAY_BUFFER, vbuf); - glVertexAttribPointer(v_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0); - - glBindBuffer(GL_SHADER_STORAGE_BUFFER, cbuf); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW); - glEnableVertexAttribArray(t_attrib); - glBindBuffer(GL_ARRAY_BUFFER, cbuf); - glVertexAttribPointer(t_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, tex); - - glDrawArrays(GL_TRIANGLES, 0, 1800); - } + int init(); + void draw(glm::mat4 vp); + void set_char(int col, int line, char c); + void lprintf(int line, const char *fmt, ...); + void set_text_line(int line, const char* text); + void set_text(const char* text); }; diff --git a/main.cpp b/main.cpp index 4c9b2ac..dbf9d15 100644 --- a/main.cpp +++ b/main.cpp @@ -16,8 +16,8 @@ std::mutex grand_mutex; -float height = 1550; -float width = 2600; +float height = 1600; +float width = 2550; /* Global section */ int time_resolution = 10; @@ -175,7 +175,7 @@ void player_movement(){ player_position.y = floor_height + player_height; } } - main_hud.lprintf(2, "Player Position: (%f, %f, %f) %d", player_position.x, player_position.y, player_position.z, 50); + main_hud.lprintf(2, "Player Position: (%f, %f, %f)", player_position.x, player_position.y, player_position.z); // grand_mutex.unlock(); auto end = std::chrono::system_clock::now(); // double difference = std::chrono::duration_cast(start - end).count();