Permalink
Browse files

Port to Eglo (Pocket C.H.I.P)

  • Loading branch information...
thp committed Nov 27, 2016
1 parent f5eb7f6 commit 2ff6de153612b84d9649383f0f28add154a3ea70
@@ -0,0 +1,130 @@
/*
* This file is part of NumptyPhysics <http://thp.io/2015/numptyphysics/>
* Coyright (c) 2014, 2015, 2016 Thomas Perl <m@thp.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "EGLOSTBRenderer.h"
#include "Os.h"
#include "Config.h"
#include <cstring>
#include "stb_loader.h"
class EGLOFontData : public NP::FontData {
public:
EGLOFontData(const char *filename, int size);
~EGLOFontData();
std::string filename;
};
EGLOFontData::EGLOFontData(const char *filename, int size)
: NP::FontData(size)
, filename(filename)
{
}
EGLOFontData::~EGLOFontData()
{
}
EGLOSTBRenderer::EGLOSTBRenderer(Vec2 world_size, Vec2 framebuffer_size)
: GLRenderer(world_size)
, m_texture_cache()
{
GLRenderer::init(framebuffer_size);
}
EGLOSTBRenderer::~EGLOSTBRenderer()
{
}
NP::Texture
EGLOSTBRenderer::load(const char *filename, bool cache)
{
std::string fn(filename);
if (cache) {
// Cache lookup
for (auto &item: m_texture_cache) {
if (item.first == fn) {
return item.second;
}
}
}
Blob *blob = Config::readBlob(filename);
StbLoader_RGBA *rgba = StbLoader::decode_image(blob->data, blob->len);
delete blob;
NP::Texture result = GLRenderer::load((unsigned char *)rgba->data, rgba->w, rgba->h);
delete rgba;
if (cache) {
// Store loaded image in cache
m_texture_cache[fn] = result;
}
return result;
}
NP::Font
EGLOSTBRenderer::load(const char *filename, int size)
{
return NP::Font(new EGLOFontData(filename, size));
}
void
EGLOSTBRenderer::metrics(const NP::Font &font, const char *text, int *width, int *height)
{
EGLOFontData *data = static_cast<EGLOFontData *>(font.get());
Blob *blob = Config::readBlob(data->filename);
StbLoader_RGBA *rgba = StbLoader::render_font(blob->data, blob->len,
StbLoader_Color(0.f, 0.f, 0.f, 1.f), data->size, text);
*width = rgba->w;
*height = rgba->h;
delete rgba;
delete blob;
}
NP::Texture
EGLOSTBRenderer::text(const NP::Font &font, const char *text, int rgb)
{
if (strlen(text) == 0) {
return NP::Texture(new GLTextureData(nullptr, 10, 10));
}
EGLOFontData *data = static_cast<EGLOFontData *>(font.get());
float r = 1.f * (uint8_t)((rgb >> 16) & 0xff) / 255.f;
float g = 1.f * (uint8_t)((rgb >> 8) & 0xff) / 255.f;
float b = 1.f * (uint8_t)((rgb) & 0xff) / 255.f;
Blob *blob = Config::readBlob(data->filename);
StbLoader_RGBA *rgba = StbLoader::render_font(blob->data, blob->len,
StbLoader_Color(r, g, b, 1.f), data->size, text);
NP::Texture result = GLRenderer::load((unsigned char *)rgba->data, rgba->w, rgba->h);
delete rgba;
delete blob;
return result;
}
void
EGLOSTBRenderer::swap()
{
eglo_swap_buffers();
}
@@ -0,0 +1,45 @@
/*
* This file is part of NumptyPhysics <http://thp.io/2015/numptyphysics/>
* Coyright (c) 2014, 2016 Thomas Perl <m@thp.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#ifndef NUMPTYPHYSICS_EGLOSTBRENDERER_H
#define NUMPTYPHYSICS_EGLOSTBRENDERER_H
#include <map>
#include <string>
#include "Renderer.h"
#include "GLRenderer.h"
#include "eglo.h"
class EGLOSTBRenderer : public GLRenderer {
public:
EGLOSTBRenderer(Vec2 world_size, Vec2 framebuffer_size);
~EGLOSTBRenderer();
virtual NP::Texture load(const char *filename, bool cache);
virtual NP::Font load(const char *filename, int size);
virtual void metrics(const NP::Font &font, const char *text, int *width, int *height);
virtual NP::Texture text(const NP::Font &font, const char *text, int rgb);
virtual void swap();
private:
std::map<std::string, NP::Texture> m_texture_cache;
};
#endif /* NUMPTYPHYSICS_EGLOSTBRENDERER_H */
@@ -0,0 +1,151 @@
/*
* This file is part of NumptyPhysics <http://thp.io/2015/numptyphysics/>
* Coyright (c) 2014, 2015, 2016 Thomas Perl <m@thp.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "App.h"
#include "Os.h"
#include "EGLOSTBRenderer.h"
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include "eglo.h"
static void mapEGLOEventToToolkitEvent(EgloEvent &e, ToolkitEvent &ev)
{
switch (e.type) {
case EGLO_MOUSE_DOWN:
ev.type = ToolkitEvent::PRESS;
ev.x = e.mouse.x;
ev.y = e.mouse.y;
ev.finger = 0;
ev.key = 1;
break;
case EGLO_MOUSE_UP:
ev.type = ToolkitEvent::RELEASE;
ev.x = e.mouse.x;
ev.y = e.mouse.y;
ev.finger = 0;
ev.key = 1;
break;
case EGLO_MOUSE_MOTION:
ev.type = ToolkitEvent::MOVE;
ev.x = e.mouse.x;
ev.y = e.mouse.y;
ev.finger = 0;
ev.key = 1;
break;
//case EGL_KEY_DOWN:
//ev.type = ToolkitEvent::KEYDOWN;
//ev.x = ev.y = ev.finger = 0;
//ev.key = mapEGLOKeyToNumptyKey(e.key.key);
//break;
case EGLO_QUIT:
ev.type = ToolkitEvent::QUIT;
break;
default:
ev.type = ToolkitEvent::NONE;
break;
}
}
class OsEGLOSTB : public Os {
public:
OsEGLOSTB()
: Os()
, m_renderer(nullptr)
, m_width(0)
, m_height(0)
{
}
virtual void init()
{
eglo_init(&m_width, &m_height, 2);
}
virtual void window(Vec2 world_size)
{
if (!m_renderer) {
m_renderer = new EGLOSTBRenderer(world_size, Vec2(m_width, m_height));
}
}
virtual NP::Renderer *renderer()
{
return m_renderer;
}
virtual bool nextEvent(ToolkitEvent &ev)
{
EgloEvent e;
while (eglo_next_event(&e)) {
mapEGLOEventToToolkitEvent(e, ev);
m_renderer->mapXY(ev.x, ev.y);
return true;
}
return false;
}
virtual long ticks()
{
static bool time_inited = false;
static struct timespec ts0;
if (!time_inited) {
time_inited = true;
clock_gettime(CLOCK_MONOTONIC, &ts0);
}
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / (1000 * 1000);
}
virtual void delay(int ms)
{
::usleep(1000 * ms);
}
virtual bool openBrowser(const char *url)
{
return false;
}
virtual std::string userDataDir()
{
return ".numptyphysics-data-eglo";
}
private:
EGLOSTBRenderer *m_renderer;
int m_width;
int m_height;
};
int main(int argc, char** argv)
{
std::shared_ptr<Os> os(new OsEGLOSTB());
std::shared_ptr<MainLoop> mainloop(npmain(argc, argv));
while (mainloop->step());
return 0;
}
@@ -0,0 +1,9 @@
Eglo Platform plugin for Pocket C.H.I.P
=======================================
To build, you need to checkout the Eglo library:
git clone https://github.com/thp/eglo ../eglo
make -C ../eglo
make PLATFORM=eglo
./numptyphysics
@@ -0,0 +1,11 @@
# Platform build definition for EGLO
# Will be processed by makefile
add_platform(gl)
add_external(stb_loader)
CFLAGS += -DUSE_OPENGL_ES
CXXFLAGS += -DUSE_OPENGL_ES
LIBS += -L../eglo -leglo -lGLESv2 -Wl,-rpath,. -Wl,-rpath,../eglo
CXXFLAGS += -I../eglo

0 comments on commit 2ff6de1

Please sign in to comment.