Skip to content

Commit

Permalink
more opengl wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
FolkertVanVerseveld committed Mar 31, 2024
1 parent 3bac878 commit a5540ad
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 55 deletions.
97 changes: 46 additions & 51 deletions game/src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "ui/imgui_user.hpp"

#include <cstdio>
#include <cstddef>
#include <cstdint>

#include <mutex>
Expand All @@ -46,6 +47,18 @@ namespace aoe {
Engine *eng;
std::mutex m_eng;

static struct BkgVertex {
GLfloat x, y, z;
GLfloat r, g, b;
GLfloat s, t;
} bkg_vertices[] = {
// positions // colors // texture coords
{ 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f}, // top right
{ 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // bottom right
{-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f}, // bottom left
{-1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f}, // top left
};

Engine::Engine()
: net(), show_demo(false), show_debug(false), font_scaling(true)
, connection_mode(0), connection_port(32768), connection_host("")
Expand All @@ -58,13 +71,7 @@ Engine::Engine()
, debug()
, cfg(*this, "config"), sdl(nullptr), is_fullscreen(false), m_gl(nullptr), assets(), assets_good(false)
, show_chat(false), m_show_achievements(false), show_timeline(false), show_diplomacy(false)
, bkg_vertices{
// positions // colors // texture coords
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // top right
1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // bottom right
-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // bottom left
-1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f // top left
}, vbo(0), vsync_mode(0), vsync_idx(0)
, vbo(0), vsync_mode(0), vsync_idx(0)
, cam_x(0), cam_y(0), keyctl(), gv(), tw(0), th(0), cv()
, player_tbl_y(0), ui(), fnt()
, texture1(0), tex1(nullptr)
Expand Down Expand Up @@ -193,6 +200,9 @@ void Engine::show_menubar() {

/** Load and validate game assets. */
void Engine::verify_game_data(const std::string &path) {
if (path.empty())
return;

assets_good = false;

if (!fnt.loaded()) {
Expand Down Expand Up @@ -495,20 +505,20 @@ void Engine::set_background(io::DrsId id) {
const gfx::ImageRef &r = a.at(id);
//printf("(%.2f,%.2f), (%.2f,%.2f)\n", r.s0, r.t0, r.s1, r.t1);

bkg_vertices[0 * 8 + 6] = r.s1;
bkg_vertices[1 * 8 + 6] = r.s1;
bkg_vertices[0].s = r.s1;
bkg_vertices[1].s = r.s1;

bkg_vertices[2 * 8 + 6] = r.s0;
bkg_vertices[3 * 8 + 6] = r.s0;
bkg_vertices[2].s = r.s0;
bkg_vertices[3].s = r.s0;

bkg_vertices[0 * 8 + 7] = r.t0;
bkg_vertices[3 * 8 + 7] = r.t0;
bkg_vertices[0].t = r.t0;
bkg_vertices[3].t = r.t0;

bkg_vertices[1 * 8 + 7] = r.t1;
bkg_vertices[2 * 8 + 7] = r.t1;
bkg_vertices[1].t = r.t1;
bkg_vertices[2].t = r.t1;

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, bkg_vertices.size() * sizeof(GLfloat), bkg_vertices.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(bkg_vertices), bkg_vertices, GL_STATIC_DRAW);

GLCHK;
}
Expand Down Expand Up @@ -921,46 +931,31 @@ int Engine::mainloop() {

GLuint vao, ebo;

glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
{
GLvertexArray vao;
GLbuffer vbo, ebo;
this->vbo = vbo;

glBindVertexArray(vao);
vao.bind();

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, bkg_vertices.size() * sizeof(GLfloat), bkg_vertices.data(), GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

// position attribute
GLint aPos = glGetAttribLocation(prog, "aPos");
glVertexAttribPointer(aPos, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(aPos);
// color attribute
GLint aColor = glGetAttribLocation(prog, "aColor");
glVertexAttribPointer(aColor, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(aColor);
// texture coord attribute
GLint aTexCoord = glGetAttribLocation(prog, "aTexCoord");
glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(aTexCoord);

glGenTextures(1, &texture1);
tex1 = (ImTextureID)texture1;
GL::bind2d(texture1, GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR);
vbo.setData(GL_ARRAY_BUFFER, sizeof(bkg_vertices), bkg_vertices, GL_STATIC_DRAW);
ebo.setData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

GLCHK;
prog.setVertexArray("aPos" , 3, GL_FLOAT, sizeof(BkgVertex), offsetof(BkgVertex, x));
prog.setVertexArray("aColor" , 3, GL_FLOAT, sizeof(BkgVertex), offsetof(BkgVertex, r));
prog.setVertexArray("aTexCoord", 2, GL_FLOAT, sizeof(BkgVertex), offsetof(BkgVertex, s));

// autoload game data if available
if (!cfg.game_dir.empty())
verify_game_data(cfg.game_dir);
glGenTextures(1, &texture1);
tex1 = (ImTextureID)texture1;
GL::bind2d(texture1, GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR);

eventloop(sdl, prog, vao);
GLCHK;

glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
// autoload game data if available
verify_game_data(cfg.game_dir);

eventloop(sdl, prog, vao);
}

// Cleanup
ImGui_ImplOpenGL3_Shutdown();
Expand Down Expand Up @@ -1050,7 +1045,7 @@ void Engine::eventloop(SDL &sdl, gfx::GLprogram &prog, GLuint vao) {

// Rendering
ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
GL::viewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

videoRecorder.step(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
Expand Down
1 change: 0 additions & 1 deletion game/src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class Engine final {
bool show_timeline;
bool show_diplomacy;

std::vector<GLfloat> bkg_vertices;
GLuint vbo;
int vsync_mode, vsync_idx;

Expand Down
54 changes: 53 additions & 1 deletion game/src/engine/gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,36 @@ void GL::clearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) {
glClear(GL_COLOR_BUFFER_BIT);
}

GLprogram::GLprogram() : id(glCreateProgram()), uniforms() {
void GL::viewport(GLint x, GLint y, GLsizei width, GLsizei height) {
glViewport(x, y, width, height);
}

GLvertexArray::GLvertexArray() : id(0) {
glGenVertexArrays(1, &id);
}

GLvertexArray::~GLvertexArray() {
glDeleteVertexArrays(1, &id);
}

void GLvertexArray::bind() {
glBindVertexArray(id);
}

GLbuffer::GLbuffer() : id(0) {
glGenBuffers(1, &id);
}

GLbuffer::~GLbuffer() {
glDeleteBuffers(1, &id);
}

void GLbuffer::setData(GLenum target, GLsizeiptr size, const void *data, GLenum usage) {
glBindBuffer(target, id);
glBufferData(target, size, data, usage);
}

GLprogram::GLprogram() : id(glCreateProgram()), uniforms(), attributes() {
if (!id)
throw std::runtime_error("Failed to create program");
}
Expand Down Expand Up @@ -152,5 +181,28 @@ void GLprogram::setUniform(const char *name, GLint v) {
glUniform1i(id, v);
}

void GLprogram::setVertexArray(const char *name, GLint size, GLenum type, GLsizei stride, unsigned offset) {
setVertexArray(name, size, type, GL_FALSE, stride, offset);
}

void GLprogram::setVertexArray(const char *name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, unsigned offset) {
auto it = attributes.find(name);
GLint id = -1;

if (it != attributes.end()) {
id = it->second;
} else {
id = glGetAttribLocation(this->id, name);

if (id == -1)
return;

attributes.emplace(name, id);
}

glVertexAttribPointer(id, size, type, normalized, stride, (void*)(0 + offset));
glEnableVertexAttribArray(id);
}

}
}
36 changes: 35 additions & 1 deletion game/src/engine/gfx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,47 @@ class GL final {
static void bind2d(GLuint tex, GLint wrapS, GLint wrapT, GLint minFilter, GLint magFilter);

static void clearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a);

static void viewport(GLint x, GLint y, GLsizei width, GLsizei height);
};

class GLvertexArray final {
GLuint id;
public:
GLvertexArray();
~GLvertexArray();

void bind();

constexpr operator GLuint() const noexcept { return id; }
};

class GLbuffer final {
GLuint id;
public:
GLbuffer();
~GLbuffer();

/*
void glBufferData( GLenum target,
GLsizeiptr size,
const void * data,
GLenum usage);
*/
void setData(GLenum target, GLsizeiptr size, const void *data, GLenum usage);

constexpr operator GLuint() const noexcept { return id; }
};

class GLprogram final {
GLuint id;
std::map<const char*, GLint> uniforms;
std::map<const char*, GLint> attributes;
public:
GLprogram();
~GLprogram();

operator GLuint() const noexcept { return id; }
constexpr operator GLuint() const noexcept { return id; }

GLprogram &operator+=(GLuint shader) {
glAttachShader(id, shader);
Expand All @@ -129,6 +160,9 @@ class GLprogram final {

// NOTE const char* must be compile time constant
void setUniform(const char *s, GLint v);

void setVertexArray(const char *s, GLint size, GLenum type, GLsizei stride, unsigned offset);
void setVertexArray(const char *s, GLint size, GLenum type, GLboolean normalized, GLsizei stride, unsigned offset);
};

}
Expand Down
2 changes: 1 addition & 1 deletion game/src/engine/sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Window::Window(const char *title, int x, int y, int w, int h)
: flags((SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI))
#pragma warning(default: 26812)
, win(SDL_CreateWindow(title, x, y, w, h, flags), SDL_DestroyWindow)
, mode_def{ 0 }, mode_full{ 0 }, pos_def{ 0 }, disp_full(-1) {}
, mode_def{ 0 }, mode_full{ 0 }, pos_def{ 0 }, pos_full{ 0 }, disp_full(-1) {}

GLctx::GLctx(SDL_Window *win) : gl_context(SDL_GL_CreateContext(win)) {}

Expand Down

0 comments on commit a5540ad

Please sign in to comment.