Skip to content

Commit

Permalink
Basic idea
Browse files Browse the repository at this point in the history
  • Loading branch information
codecat committed Jun 27, 2017
1 parent 1f5ff32 commit 7e18137
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 105 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "ext/layout"]
path = ext/layout
url = https://github.com/randrew/layout.git
[submodule "ext/glm"]
path = ext/glm
url = https://github.com/g-truc/glm.git
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.0)

option(NA_BUILD_EXAMPLE "Build the Nimble App example" ON)

set(CMAKE_CXX_STANDARD 11)

set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "")
Expand All @@ -8,7 +10,7 @@ set(GLFW_BUILD_DOCS OFF CACHE BOOL "")

add_subdirectory(ext/glfw)

add_executable(NimbleApp
add_library(nimbleapp
ext/nanovg/src/fontstash.h
ext/nanovg/src/nanovg.c
ext/nanovg/src/nanovg.h
Expand All @@ -19,18 +21,26 @@ add_executable(NimbleApp

src/scratch2.cpp
src/layout.cpp
src/nanovg.cpp

src/main.cpp
src/nimble/app.cpp
src/nimble/widget.cpp
)

include_directories(
ext/nanovg/src/
ext/scratch2/scratch2/
ext/layout/
ext/glm/glm/
include/
)

target_link_libraries(NimbleApp
target_link_libraries(nimbleapp
glfw
GL
GLEW
)

if(NA_BUILD_EXAMPLE)
add_subdirectory(example)
endif()
7 changes: 7 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_executable(example
main.cpp
)

target_link_libraries(example
nimbleapp
)
22 changes: 22 additions & 0 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <nimble/app.h>

class ExampleApplication : public na::Application
{
public:
ExampleApplication()
{
}

~ExampleApplication()
{
}
};

int main()
{
ExampleApplication* app = new ExampleApplication();
app->Run();
delete app;

return 0;
}
1 change: 1 addition & 0 deletions ext/glm
Submodule glm added at 26b3e3
49 changes: 49 additions & 0 deletions include/nimble/app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include "common.h"
#include "widget.h"

class GLFWwindow;
struct lay_context;
struct NVGcontext;

namespace na
{
class Application
{
protected:
lay_context* m_layout = nullptr;
GLFWwindow* m_window = nullptr;
NVGcontext* m_nvg = nullptr;

bool m_initializedLayout = false;
bool m_initializedRendering = false;
bool m_initializedWindow = false;

Widget* m_root = nullptr;

bool m_invalidatedLayout = true;
bool m_invalidatedRendering = true;

public:
Application();
virtual ~Application();

virtual void Run();
virtual void DoLayout();
virtual void Frame();

virtual void InvalidateLayout();
virtual void InvalidateRendering();

virtual bool IsInvalidated();

protected:
virtual void InitializeLayout();
virtual void InitializeRendering();
virtual void InitializeWindow();

virtual void CleanupLayout();
virtual void CleanupRendering();
};
}
9 changes: 9 additions & 0 deletions include/nimble/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <cstdio>
#include <cstdint>

#include <s2string.h>
#include <s2list.h>

#include <glm.hpp>
38 changes: 38 additions & 0 deletions include/nimble/widget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include "common.h"

typedef uint32_t lay_id;

struct lay_context;
struct NVGcontext;

namespace na
{
class Application;

class Widget
{
private:
Application* m_app;
Widget* m_parent;

bool m_visible = true;

s2::list<Widget*> m_children;

protected:
Widget(Application* app, Widget* parent);
virtual ~Widget();

virtual void InvalidateLayout();
virtual void InvalidateRendering();

public:
virtual void DoLayout(lay_context* l, lay_id parent);
virtual void Draw(NVGcontext* vg);

inline bool IsVisible() { return m_visible; }
virtual void SetVisible(bool visible);
};
}
4 changes: 4 additions & 0 deletions src/nanovg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <GL/glew.h>
#include <nanovg.h>
#define NANOVG_GL3_IMPLEMENTATION
#include <nanovg_gl.h>
192 changes: 192 additions & 0 deletions src/nimble/app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#include <nimble/common.h>
#include <nimble/app.h>

#include <GL/glew.h>
#ifdef __APPLE__
#define GLFW_INCLUDE_GLCOREARB
#endif
#define GLFW_INCLUDE_GLEXT
#include <GLFW/glfw3.h>

#include <nanovg.h>
#define NANOVG_GL3
#include <nanovg_gl.h>

#include <layout.h>

static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
//
}

static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
//
}

na::Application::Application()
{
InitializeLayout();
}

na::Application::~Application()
{
CleanupLayout();
CleanupRendering();
}

void na::Application::Run()
{
InitializeRendering();
InitializeWindow();

while (!glfwWindowShouldClose(m_window)) {
if (IsInvalidated()) {
Frame();
}

if (IsInvalidated()) {
glfwPollEvents();
} else {
glfwWaitEvents(); // interrupt using glfwPostEmptyEvent
}
}
}

void na::Application::DoLayout()
{
lay_reset_context(m_layout);
lay_id root = lay_item(m_layout);

if (m_root != nullptr) {
m_root->DoLayout(m_layout, root);
}

lay_run_context(m_layout);
}

void na::Application::Frame()
{
if (m_invalidatedLayout) {
static int _layoutCount = 0;
printf("Layout %d\n", _layoutCount++);

m_invalidatedLayout = false;
DoLayout();
m_invalidatedRendering = true;
}

if (!m_invalidatedRendering) {
return;
}
m_invalidatedRendering = false;

static int _renderCount = 0;
printf("Render %d\n", _renderCount++);

glClear(GL_COLOR_BUFFER_BIT);

nvgBeginFrame(m_nvg, 1024, 768, 1.0f);

if (m_root != nullptr) {
m_root->Draw(m_nvg);
}

nvgEndFrame(m_nvg);

glfwSwapBuffers(m_window);
}

void na::Application::InvalidateLayout()
{
m_invalidatedLayout = true;
}

void na::Application::InvalidateRendering()
{
m_invalidatedRendering = true;
}

bool na::Application::IsInvalidated()
{
return m_invalidatedLayout || m_invalidatedRendering;
}

void na::Application::InitializeLayout()
{
m_layout = new lay_context;
lay_init_context(m_layout);
m_initializedLayout = true;
}

void na::Application::InitializeRendering()
{
if (!glfwInit()) {
printf("Glfw failed\n");
return;
}

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4);

m_initializedRendering = true;
}

void na::Application::InitializeWindow()
{
m_window = glfwCreateWindow(1024, 768, "Nimble App", nullptr, nullptr);
if (m_window == nullptr) {
printf("No window\n");
CleanupRendering();
return;
}

glfwSetCursorPosCallback(m_window, cursor_position_callback);
glfwSetMouseButtonCallback(m_window, mouse_button_callback);
glfwMakeContextCurrent(m_window);

glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
printf("Glew failed\n");
CleanupRendering();
return;
}
glGetError();

m_nvg = nvgCreateGL3(/*NVG_ANTIALIAS | */NVG_STENCIL_STROKES);
if (m_nvg == nullptr) {
printf("Nvg failed\n");
CleanupRendering();
return;
}

m_initializedWindow = true;
}

void na::Application::CleanupLayout()
{
if (!m_initializedLayout) {
return;
}

lay_destroy_context(m_layout);
delete m_layout;
m_layout = nullptr;

m_initializedLayout = false;
}

void na::Application::CleanupRendering()
{
if (!m_initializedRendering) {
return;
}

glfwTerminate();

m_initializedRendering = false;
m_initializedWindow = false;
}
Loading

0 comments on commit 7e18137

Please sign in to comment.