Skip to content

Commit

Permalink
renderer: legacy opengl square rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJJ committed Jun 26, 2015
1 parent 2e6049a commit 0815ae5
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cpp/renderer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ add_sources(${PROJECT_NAME}

add_demo_cpp(openage::renderer::tests::renderer_demo "open the render window")

add_subdirectory(gl/)
add_subdirectory(opengl/)
41 changes: 25 additions & 16 deletions cpp/renderer/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../util/error.h"

#if WITH_OPENGL
#include "gl/context.h"
#include "opengl/context.h"
#endif

#if WITH_VULKAN
Expand All @@ -22,30 +22,39 @@ Context::Context() {}
Context::~Context() {}

std::unique_ptr<Context> Context::generate(context_type t) {
if (t == context_type::opengl and not WITH_OPENGL) {
throw util::Error(MSG(err) << "OpenGL support not enabled!");
}
else if (t == context_type::vulkan and not WITH_VULKAN) {
throw util::Error(MSG(err) << "Vulkan support not enabled!");
}
else if (t == context_type::autodetect) {
context_type ctx_requested = t;

if (t == context_type::autodetect) {
// priority: vulkan > opengl
if (WITH_VULKAN) {
#if WITH_VULKAN
log::log(MSG(dbg) << "Using Vulkan context...");
return std::make_unique<VulkanContext>();
#endif
ctx_requested = context_type::vulkan;
}
else if (WITH_OPENGL) {
#if WITH_OPENGL
log::log(MSG(dbg) << "Using OpenGL context...");
return std::make_unique<GLContext>();
#endif
ctx_requested = context_type::opengl;
}
else {
throw util::Error(MSG(err) << "No render context available!");
}
}

if (ctx_requested == context_type::opengl) {
if (not WITH_OPENGL) {
throw util::Error(MSG(err) << "OpenGL support not enabled!");
}
#if WITH_OPENGL
log::log(MSG(dbg) << "Using OpenGL context...");
return std::make_unique<opengl::Context>();
#endif
}
else if (ctx_requested == context_type::vulkan) {
if (not WITH_VULKAN) {
throw util::Error(MSG(err) << "Vulkan support not enabled!");
}
#if WITH_VULKAN
log::log(MSG(dbg) << "Using Vulkan context...");
return std::make_unique<vulkan::Context>();
#endif
}
else {
throw util::Error(MSG(err) << "Unknown context type requested!");
}
Expand Down
3 changes: 0 additions & 3 deletions cpp/renderer/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

#include <memory>

#include "../coord/window.h"


namespace openage {
namespace renderer {

Expand Down
File renamed without changes.
17 changes: 9 additions & 8 deletions cpp/renderer/gl/context.cpp → cpp/renderer/opengl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@

namespace openage {
namespace renderer {
namespace opengl {

// TODO: get max available gl version
constexpr int opengl_version_major = 2;
constexpr int opengl_version_minor = 1;

GLContext::GLContext() {}
GLContext::~GLContext() {}
Context::Context() {}
Context::~Context() {}

uint32_t GLContext::get_window_flags() {
uint32_t Context::get_window_flags() {
return SDL_WINDOW_OPENGL;
}

void GLContext::prepare() {
void Context::prepare() {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, opengl_version_major);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, opengl_version_minor);
Expand All @@ -34,7 +35,7 @@ void GLContext::prepare() {
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
}

void GLContext::create(SDL_Window *window) {
void Context::create(SDL_Window *window) {
this->glcontext = SDL_GL_CreateContext(window);

if (this->glcontext == nullptr) {
Expand All @@ -50,7 +51,7 @@ void GLContext::create(SDL_Window *window) {
}
}

void GLContext::setup() {
void Context::setup() {
// to quote the standard doc: 'The value gives a rough estimate of the
// largest texture that the GL can handle'
// -> wat? anyways, we need at least 1024x1024.
Expand Down Expand Up @@ -83,10 +84,10 @@ void GLContext::setup() {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void GLContext::destroy() {
void Context::destroy() {
SDL_GL_DeleteContext(this->glcontext);
}

}} // namespace openage::renderer
}}} // namespace openage::renderer::opengl

#endif // if WITH_OPENGL
15 changes: 8 additions & 7 deletions cpp/renderer/gl/context.h → cpp/renderer/opengl/context.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// Copyright 2015-2015 the openage authors. See copying.md for legal info.

#ifndef OPENAGE_RENDERER_GL_CONTEXT_H_
#define OPENAGE_RENDERER_GL_CONTEXT_H_
#ifndef OPENAGE_RENDERER_OPENGL_CONTEXT_H_
#define OPENAGE_RENDERER_OPENGL_CONTEXT_H_

#include <SDL2/SDL.h>
#include <memory>

#include "../context.h"
#include "../../config.h"


namespace openage {
namespace renderer {
namespace opengl {

class GLContext : public Context {
class Context : public renderer::Context {
public:
GLContext();
~GLContext();
Context();
~Context();

SDL_GLContext glcontext;

Expand All @@ -26,6 +27,6 @@ class GLContext : public Context {
virtual void destroy();
};

}} // namespace openage::renderer
}}} // namespace openage::renderer

#endif
72 changes: 67 additions & 5 deletions cpp/renderer/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Copyright 2015-2015 the openage authors. See copying.md for legal info.

#include <cstdlib>
#include <epoxy/gl.h>
#include <functional>
#include <unordered_map>

#include "../log/log.h"
#include "window.h"

Expand All @@ -8,22 +13,33 @@ namespace renderer {
namespace tests {


void renderer_demo(int /*argc*/, char **/*argv*/) {
Window window{"testing!"};
struct render_demo {
std::function<void(Window *)> setup;
std::function<void()> frame;
std::function<void(const coord::window &)> resize;
};


void create_window(const render_demo *actions) {
Window window{"openage renderer testing"};
SDL_Event event;

actions->setup(&window);

bool running = true;
while (running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_WINDOWEVENT:
switch(event.window.event) {
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED: {
coord::window new_size{event.window.data1, event.window.data2};
log::log(
MSG(info) << "new window size: "
<< new_size.x << " x " << new_size.y << "."
<< new_size.x << " x " << new_size.y
);
window.set_size(new_size);
actions->resize(new_size);
break;
}}
break;
Expand All @@ -45,10 +61,56 @@ void renderer_demo(int /*argc*/, char **/*argv*/) {
}}
}

actions->frame();

window.swap();
}
}

return;

void renderer_demo(int argc, char **argv) {
int demo_id ;
if (argc == 2) {
demo_id = atoi(argv[1]);
log::log(MSG(info) << "running renderer demo " << demo_id << "...");
} else {
demo_id = 0;
log::log(MSG(info) << "executing default renderer demo " << demo_id << "...");
}

render_demo test0{
// init
[=](Window */*window*/) {
glEnable(GL_BLEND);
},
// frame
[] {
glClearColor(0.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1, 0, 0);
glBegin(GL_QUADS); {
glVertex2f(.0f, .0f);
glVertex2f(1.0f, .0f);
glVertex2f(1.0f, 1.0f);
glVertex2f(.0f, 1.0f);
} glEnd();
},
// resize
[](const coord::window &new_size) {
glViewport(0, 0, new_size.x, new_size.y);
}
};

std::unordered_map<int, render_demo*> demos;
demos[0] = &test0;

if (demos.find(demo_id) != demos.end()) {
create_window(demos[demo_id]);
}
else {
log::log(MSG(err) << "unknown renderer demo " << demo_id << " requested.");
}
}


Expand Down
3 changes: 2 additions & 1 deletion cpp/renderer/vulkan/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace openage {
namespace renderer {
namespace vulkan {

class VulkanContext : public Context {
class Context : public renderer::Context {
public:
SDL_VulkanContext vkcontext;

Expand Down
22 changes: 19 additions & 3 deletions cpp/renderer/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
namespace openage {
namespace renderer {

Window::Window(const char *title) {
Window::Window(const char *title)
:
size{800, 600} {

this->context = Context::generate(context_type::autodetect);

if (SDL_Init(SDL_INIT_VIDEO) < 0) {
Expand All @@ -29,8 +32,8 @@ Window::Window(const char *title) {
title,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
this->window_size.x,
this->window_size.y,
this->size.x,
this->size.y,
window_flags
);

Expand Down Expand Up @@ -68,4 +71,17 @@ void Window::swap() {
SDL_GL_SwapWindow(this->window);
}

coord::window Window::get_size() {
return this->size;
}

void Window::set_size(const coord::window &new_size, bool update) {
if (this->size.x != new_size.x or this->size.y != new_size.y) {
this->size = new_size;
if (update) {
SDL_SetWindowSize(this->window, this->size.x, this->size.y);
}
}
}

}} // namespace openage::renderer
4 changes: 3 additions & 1 deletion cpp/renderer/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ class Window {
Window &operator =(const Window &other) = delete;
Window &operator =(Window &&other) = delete;

coord::window get_size();
void set_size(const coord::window &, bool update=false);

Window(const char *title);
~Window();

void swap();

private:
coord::window window_size;
coord::window size;
SDL_Window *window;

std::unique_ptr<Context> context;
Expand Down

0 comments on commit 0815ae5

Please sign in to comment.